mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-26 05:27:06 +00:00
HapiProtocol (pure, mirroring the Android reference port):
- SummaryPatching: sessionSummary.ts derivations (pending requests
cap-5/oldest-first/id-tiebreak, kinds, todo progress), toSessionSummary
projection with the per-flavor agentSessionId resolution (legacy chain
omits piSessionId, replicated), applySessionSummaryPatch with the
deliberate >= version gates (vs the detail path's strict >), the
keep-alive render-irrelevance filter (pendingRequests compare ignores
'since'; metadata compare ignores hapiMcpUrl), max-monotonic updatedAt,
post-patch updatedAt as fallback 'since', and the deprecated legacy
detail-required gate kept for rule pinning.
- SessionSorting: exact list comparator (globalPinned > pinned > active >
pendingRequestsCount desc among active > updatedAt desc; stable).
- SessionMetadata: per-flavor session-id fields (claude/codex/gemini/
opencode/grok/agy/cursor/kimi/copilot/pi).
HapiClient stores (@MainActor @Observable, per hub):
- DiskCache: 500 ms debounced atomic JSON snapshots + SnapshotLocations.
- SessionListStore: sorted summaries + detail cache; full-session upsert
preserving hub-computed scheduled fields; strict-> detail vs >= summary
patch paths; keep-alive identity preservation (listRevision); empty-{}
and unparseable payloads take the REST fallback; 16 ms coalesced
refresh; optimistic pin (roll-forward) and archive (restore).
- MachineStore: the exact machine-updated decision tree.
- LastSeenStore: monotonic unread watermarks + per-scope baseline.
- SyncEventRouter: SyncEvent fan-out to the stores, global-scope message
events refresh the list, gap handshake triggers the full resync.
App:
- HubSession owns the stores + router (replaces the TODO(M2) routing);
background flushes snapshots.
- Features/Sessions: SessionListView + SessionListModel (status dot with
thinking pulse, title cascade, flavor·machine·worktree meta, relative
age on a minute timeline, pending/todo badges, unread dots, pinned
section, machine filter chips >= 2, pull-to-refresh, empty/loading/
offline states, long-press pin/archive context menu) + an M2f chat
placeholder pushed on row tap.
- HomePlaceholderView -> HomeView hosting the list (hub switcher +
connection dot kept in the toolbar).
Tests transcribe the Android suites: SummaryPatchingTest (18),
SessionSortingTest (6), SessionStoreTest, MachineStoreTest,
LastSeenStoreTest, JsonSnapshotStoreTest, StoreSyncTargetsTest — using
the existing HTTPPerforming recording stub plus a path-routing performer
for concurrent refetches.
144 lines
4.7 KiB
Swift
144 lines
4.7 KiB
Swift
import HapiClient
|
|
import SwiftUI
|
|
|
|
/// Post-pairing home: the session list for the active hub, with the hub
|
|
/// switcher (switch / add / sign out) and the live global-SSE connection dot
|
|
/// in the toolbar. Tapping a row pushes the M2f chat placeholder.
|
|
struct HomeView: View {
|
|
let session: HubSession
|
|
|
|
@Environment(AppModel.self) private var model
|
|
@State private var confirmSignOut = false
|
|
@State private var path: [String] = []
|
|
|
|
var body: some View {
|
|
@Bindable var model = model
|
|
NavigationStack(path: $path) {
|
|
VStack(spacing: 0) {
|
|
if let failedHub = model.authFailureNotice {
|
|
authFailureBanner(failedHub: failedHub)
|
|
}
|
|
SessionListView(session: session) { sessionId in
|
|
path.append(sessionId)
|
|
}
|
|
}
|
|
.navigationDestination(for: String.self) { sessionId in
|
|
SessionDetailStubView(sessionId: sessionId)
|
|
}
|
|
.navigationTitle(HubDisplay.host(session.hubUrl))
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarLeading) {
|
|
connectionIndicator
|
|
}
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
hubMenu
|
|
}
|
|
}
|
|
.confirmationDialog(
|
|
"Sign out of \(HubDisplay.host(session.hubUrl))?",
|
|
isPresented: $confirmSignOut,
|
|
titleVisibility: .visible
|
|
) {
|
|
Button("Sign Out", role: .destructive) {
|
|
model.signOut(hub: session.hubUrl)
|
|
}
|
|
} message: {
|
|
Text("Removes the stored access token for this hub. "
|
|
+ "Pair again to reconnect.")
|
|
}
|
|
}
|
|
.sheet(isPresented: $model.showAddHub) {
|
|
PairingFlowView(context: .addHub)
|
|
}
|
|
}
|
|
|
|
// MARK: - Connection state
|
|
|
|
private var connectionIndicator: some View {
|
|
HStack(spacing: 6) {
|
|
Circle()
|
|
.fill(connectionColor)
|
|
.frame(width: 8, height: 8)
|
|
Text(connectionLabel)
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.accessibilityElement(children: .combine)
|
|
.accessibilityLabel("Connection: \(connectionLabel)")
|
|
}
|
|
|
|
private var connectionColor: Color {
|
|
switch session.connectionState {
|
|
case .connected: .green
|
|
case .connecting, .backoff: .orange
|
|
case .idle, .suspended: .gray
|
|
}
|
|
}
|
|
|
|
private var connectionLabel: String {
|
|
switch session.connectionState {
|
|
case .connected: "Live"
|
|
case .connecting: "Connecting…"
|
|
case .backoff: "Reconnecting…"
|
|
case .suspended: "Paused"
|
|
case .idle: "Offline"
|
|
}
|
|
}
|
|
|
|
// MARK: - Hub switcher
|
|
|
|
private var hubMenu: some View {
|
|
Menu {
|
|
Section("Hubs") {
|
|
ForEach(model.hubs, id: \.self) { hub in
|
|
Button {
|
|
model.switchHub(to: hub)
|
|
} label: {
|
|
if hub == session.hubUrl {
|
|
Label(HubDisplay.host(hub), systemImage: "checkmark")
|
|
} else {
|
|
Text(HubDisplay.host(hub))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Button {
|
|
model.showAddHub = true
|
|
} label: {
|
|
Label("Add Hub…", systemImage: "plus")
|
|
}
|
|
Button(role: .destructive) {
|
|
confirmSignOut = true
|
|
} label: {
|
|
Label("Sign Out…", systemImage: "rectangle.portrait.and.arrow.right")
|
|
}
|
|
} label: {
|
|
Label("Hubs", systemImage: "server.rack")
|
|
}
|
|
}
|
|
|
|
private func authFailureBanner(failedHub: String) -> some View {
|
|
HStack(alignment: .top, spacing: 12) {
|
|
Image(systemName: "exclamationmark.triangle.fill")
|
|
.foregroundStyle(.orange)
|
|
Text("\(HubDisplay.host(failedHub)) rejected its stored "
|
|
+ "credentials and was signed out. Pair it again from the hub "
|
|
+ "menu.")
|
|
.font(.footnote)
|
|
Spacer(minLength: 0)
|
|
Button {
|
|
model.authFailureNotice = nil
|
|
} label: {
|
|
Image(systemName: "xmark")
|
|
.font(.footnote.bold())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
.padding(12)
|
|
.background(.orange.opacity(0.12), in: RoundedRectangle(cornerRadius: 12))
|
|
.padding(.horizontal, 16)
|
|
.padding(.top, 8)
|
|
}
|
|
}
|