mirror of
https://github.com/wu736139669/hapi.git
synced 2026-10-31 23:31:39 +00:00
- HapiProtocol/Pairing/BindLink: parses the companion deeplink (hapicompanion://bind?hub=&code=) and the web direct-access QR (?hub=&token=) with URLSearchParams form-decoding semantics, in lockstep with the Android port (tests mirror BindLinkTest.kt). - HapiClient/Auth/HubPairingService: normalize -> GET /health (reachability + protocolVersion) -> POST /api/auth -> persist Keychain + registry + active hub; unpair with fallback. Covered by PairingLogicTests through the HTTPPerforming seam. - App layer: AppModel (@Observable @MainActor pairing state machine: restore, pair, switch, sign out, deep-link routing, scenePhase, terminal-auth-failure banner) + HubSession (per-active-hub APIClient/ AuthManager/global SSEClient with suspend-resume and a connection state for the UI; store routing is TODO(M2)). - Pairing UI: welcome flow, VisionKit QR scanner (with Simulator/ permission fallbacks), manual entry (paste-friendly), shared confirm sheet with per-PairingFailure error states. - HapiApp routes hapicompanion:// through AppModel (paired hubs switch with a notice, never log the token); RootView switches unpaired/ paired and hosts the deep-link confirm sheet; HomePlaceholderView shows hub, connection dot, hub switcher (M2a replaces it with the session list). - Info.plist: NSCameraUsageDescription; README: pairing guide + manual test pass.
128 lines
5.0 KiB
Swift
128 lines
5.0 KiB
Swift
import HapiClient
|
|
import SwiftUI
|
|
|
|
/// Entry to pairing: a short self-hosted explanation plus the two ways in —
|
|
/// scan the hub's QR code, or type hub URL + access token. Used as the app
|
|
/// root while unpaired (`context: .initial`) and as the add-hub sheet from
|
|
/// the home screen (`context: .addHub`).
|
|
struct PairingFlowView: View {
|
|
enum Context {
|
|
/// Root screen while nothing is paired.
|
|
case initial
|
|
/// Presented as a sheet to pair an additional hub.
|
|
case addHub
|
|
}
|
|
|
|
var context: Context = .initial
|
|
|
|
@Environment(AppModel.self) private var model
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var showScanner = false
|
|
@State private var showManualEntry = false
|
|
|
|
private static let docsURL = URL(string: "https://app.hapi.run/docs/")
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ScrollView {
|
|
VStack(spacing: 24) {
|
|
if context == .initial, let failedHub = model.authFailureNotice {
|
|
authFailureBanner(failedHub: failedHub)
|
|
}
|
|
|
|
VStack(spacing: 12) {
|
|
Image(systemName: "antenna.radiowaves.left.and.right")
|
|
.font(.system(size: 56))
|
|
.foregroundStyle(.tint)
|
|
.padding(.top, context == .initial ? 48 : 24)
|
|
Text(context == .initial ? "Pair with your hub" : "Add another hub")
|
|
.font(.title.bold())
|
|
Text(
|
|
"HAPI is self-hosted: your agent sessions run on your "
|
|
+ "own machine, served by a hub you operate. Start the "
|
|
+ "hub on your computer, then pair this app with the "
|
|
+ "QR code it prints — or enter the hub URL and access "
|
|
+ "token by hand."
|
|
)
|
|
.font(.subheadline)
|
|
.foregroundStyle(.secondary)
|
|
.multilineTextAlignment(.center)
|
|
if let docsURL = Self.docsURL {
|
|
Link("Setup guide", destination: docsURL)
|
|
.font(.subheadline)
|
|
}
|
|
}
|
|
.padding(.horizontal, 24)
|
|
|
|
VStack(spacing: 12) {
|
|
Button {
|
|
showScanner = true
|
|
} label: {
|
|
Label("Scan QR Code", systemImage: "qrcode.viewfinder")
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.controlSize(.large)
|
|
|
|
Button {
|
|
showManualEntry = true
|
|
} label: {
|
|
Label("Enter Manually", systemImage: "keyboard")
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.controlSize(.large)
|
|
}
|
|
.padding(.horizontal, 24)
|
|
}
|
|
.frame(maxWidth: 480)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.navigationTitle(context == .addHub ? "Add Hub" : "")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
if context == .addHub {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Cancel") {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.sheet(isPresented: $showScanner) {
|
|
QRScannerView()
|
|
}
|
|
.sheet(isPresented: $showManualEntry) {
|
|
ManualEntryView()
|
|
}
|
|
}
|
|
}
|
|
|
|
private func authFailureBanner(failedHub: String) -> some View {
|
|
HStack(alignment: .top, spacing: 12) {
|
|
Image(systemName: "exclamationmark.triangle.fill")
|
|
.foregroundStyle(.orange)
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text("Signed out of \(HubDisplay.host(failedHub))")
|
|
.font(.subheadline.bold())
|
|
Text("The hub rejected the stored credentials — the access "
|
|
+ "token was probably rotated. Pair again to reconnect.")
|
|
.font(.footnote)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
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)
|
|
}
|
|
}
|