Files
hapi/ios/Hapi/Features/Chat/ChatView.swift
T
weishu eed3dddb94 feat(ios): composer, permission actions, session config (A-M3ab)
Interaction layer turning the read-only chat into a working remote control,
mirroring the merged Android B-M3ab feature-for-feature (web authority where
the ports disagreed):

- ChatInteractor (HapiClient, fully swift-test covered): optimistic composer
  sends (appendOptimistic -> POST -> status settle), queue-by-default with a
  long-press Send&Steer intent while a turn is active, tap-to-retry on failed
  rows (steer retries degrade to queue), per-session drafts
  (UserDefaultsChatDrafts, hub-scoped keys, debounced + flushed on close).
- session_inactive (409) recovery: one POST /resume (current permissionMode)
  then retry; a superseding session id seeds the new window
  (MessageWindowControllers.seed), migrates the draft, retargets the
  optimistic row and emits sessionSuperseded -- ChatView/HomeView replace the
  navigation entry in place.
- Queued bar: uninvoked sends in web sort order with Cancel (optimistic
  DELETE; invoked-race ingests the authoritative row as sent), Edit
  (cancel + composer prefill, newer-draft guard) and Steer (invoked answers
  reconcile a missed consume); single-flight per-row op guard.
  reconcileQueuedState now runs on chat open and on session-pipe gap.
- Permission actions: flavor-exact bodies mirroring PermissionFooter.tsx --
  claude {} / allowTools (Bash(cmd)) / mode:acceptEdits, codex-family
  decision approved / approved_for_session / abort via deny -- plus
  AskUserQuestion flat answers (option cards, Other free text, no-questions
  fallback, cursor stable ids) and request_user_input nested answers
  (user_note suffix, required validation); optimistic Resolving /
  AlreadyHandled (404/409) overrides settled by the agentState patch.
  ChatPipeline now re-attaches the window row's client status so failed
  user rows actually render the retry affordance (web normalize.ts parity;
  the Android reference misses this overlay).
- Session config sheet (toolbar gear): catalog-driven permission-mode picker
  with tones, claude static model/effort catalogs (ModelCatalog port), codex
  models via new GET /sessions/:id/codex-models endpoint + wire types with
  per-model reasoning efforts; optimistic detail updates
  (SessionListStore.updateDetailLocal, new) rolled forward to server truth
  on error.
- Lifecycle: VisibilityReporter posts POST /api/visibility per tracked
  handshake subscriptionId on scene-phase flips (404 prunes); ChatSession
  exposes its subscriptionId and feeds the reporter; the global SSE pipe was
  already HubSession-lifetime on iOS.
- Tests: ChatInteractorTests transcribes the Android interaction suite
  against the real APIClient/AuthManager/SessionListStore/window registry
  with only HTTP scripted -- canonical JSON bodies asserted byte-for-byte
  (send/approve/deny/config), optimistic send happy/fail/retry, 409 resume
  both id paths, cancel invoked-race, steer reconcile, edit prefill,
  override lifecycle, config optimistic + rollback, drafts, abort.
2026-08-18 10:33:04 +08:00

345 lines
12 KiB
Swift

import HapiClient
import HapiProtocol
import HapiUI
import SwiftUI
/// The chat screen: `ScrollView + LazyVStack` over the reduced
/// `VisibleChatBlock`s, newest at the bottom (M2f), plus the A-M3ab
/// interaction chrome — composer + queued bar (bottom inset), permission
/// action footers (via `\.chatInteractions`), the session config sheet
/// (toolbar gear), supersede renavigation, and toast notices.
///
/// Scrolling model (iOS 17 APIs, kept deliberately simple):
/// - `defaultScrollAnchor(.bottom)` opens the thread at the newest message
/// AND keeps the bottom edge pinned while the reader is at the bottom —
/// that is the auto-stick;
/// - `scrollPosition(id:anchor:.top)` is used for the two programmatic
/// jumps: the new-messages pill (scroll to the bottom sentinel) and
/// **older-page re-anchoring** — before `loadOlder()` the current top
/// block id is captured, and when `historyVersion` bumps (rows were
/// prepended above the viewport, which shifts a non-bottom-anchored
/// scroll view) the position is re-set to that id so the reader stays on
/// the block they were looking at. Lazy estimated layout makes this
/// re-anchor approximate to the row, which is the documented trade-off;
/// - at-bottom detection uses a 1 pt bottom sentinel's lazy realization
/// (`onAppear`/`onDisappear`) — slightly eager because of lazy prefetch,
/// which errs toward sticking, the harmless direction.
struct ChatView: View {
@State private var model: ChatModel
/// Scroll position binding (anchor `.top`, so it tracks/targets the
/// top-visible block).
@State private var positionID: String?
@State private var isAtBottom = true
/// Newest block id last seen while at the bottom (new-messages pill).
@State private var newestSeenID: String?
/// Top-visible block captured when an older page was requested.
@State private var pendingAnchorID: String?
/// Session config sheet (toolbar gear).
@State private var configSheetOpen = false
/// Resume/reopen handed back a superseding session id — the host swaps
/// its navigation entry (HomeView replaces the path element).
private let onNavigateToSession: ((String) -> Void)?
private static let bottomSentinelID = "chat-bottom-sentinel"
init(
session: HubSession,
sessionId: String,
onNavigateToSession: ((String) -> Void)? = nil
) {
_model = State(initialValue: ChatModel(session: session, sessionId: sessionId))
self.onNavigateToSession = onNavigateToSession
}
var body: some View {
Group {
if model.isInitialLoading {
initialLoading
} else if model.loadFailed {
loadFailedState
} else if model.blocks.isEmpty {
emptyState
} else {
blockList
}
}
.safeAreaInset(edge: .top, spacing: 0) {
banners
}
// Toast overlays the thread; applying it before the bottom inset
// anchors it just above the composer instead of on top of it.
.overlay(alignment: .bottom) {
noticeToast
}
.safeAreaInset(edge: .bottom, spacing: 0) {
VStack(spacing: 0) {
QueuedMessagesBarView(interactor: model.interactor)
ChatComposerView(interactor: model.interactor)
}
}
.toolbar {
ToolbarItem(placement: .principal) {
headerTitle
}
ToolbarItem(placement: .topBarTrailing) {
Button {
configSheetOpen = true
} label: {
Image(systemName: "gearshape")
}
.accessibilityLabel("Session settings")
}
}
.navigationBarTitleDisplayMode(.inline)
.sheet(isPresented: $configSheetOpen) {
SessionConfigView(interactor: model.interactor)
}
.environment(\.chatMedia, model.imageLoader)
.environment(\.chatInteractions, model.interactor)
.onChange(of: model.supersededSessionId) {
if let superseding = model.supersededSessionId {
onNavigateToSession?(superseding)
}
}
.onAppear {
model.start()
}
.onDisappear {
// Pop-only navigation below this screen, so disappear == closed.
model.stop()
}
}
/// Transient interaction notice (Android snackbar analogue).
@ViewBuilder
private var noticeToast: some View {
if let notice = model.notice {
Text(notice)
.font(.footnote)
.lineLimit(3)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 12, style: .continuous))
.shadow(radius: 4, y: 2)
.padding(.horizontal, 24)
.padding(.bottom, 8)
.transition(.move(edge: .bottom).combined(with: .opacity))
}
}
// MARK: - Thread
private var blockList: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 10) {
if model.hasMore || model.isLoadingOlder {
OlderHistoryRow(isLoading: model.isLoadingOlder)
.onAppear {
requestOlderPage()
}
}
ForEach(model.blocks, id: \.stableId) { block in
ChatBlockCard(block: block, basePath: model.basePath)
}
bottomSentinel
}
.scrollTargetLayout()
.padding(.horizontal, 12)
.padding(.top, 10)
}
.defaultScrollAnchor(.bottom)
.scrollPosition(id: $positionID, anchor: .top)
.scrollDismissesKeyboard(.interactively)
.overlay(alignment: .bottom) {
newMessagesPill
}
.onChange(of: model.historyVersion) {
reanchorAfterPrepend()
}
.onChange(of: newestBlockID) {
if isAtBottom {
newestSeenID = newestBlockID
}
}
}
private var bottomSentinel: some View {
Color.clear
.frame(height: 1)
.id(Self.bottomSentinelID)
.onAppear {
isAtBottom = true
newestSeenID = newestBlockID
}
.onDisappear {
isAtBottom = false
}
}
private var newestBlockID: String? {
model.blocks.last?.stableId
}
/// Capture the anchor BEFORE the prepend lands, then ask for the page.
private func requestOlderPage() {
if pendingAnchorID == nil {
let id = positionID
// The binding may currently point at chrome rows; fall back to
// the oldest real block.
let isBlock = id.map { candidate in
model.blocks.contains { $0.stableId == candidate }
} ?? false
pendingAnchorID = isBlock ? id : model.blocks.first?.stableId
}
model.loadOlder()
}
/// `historyVersion` bumped: rows were prepended above the viewport.
/// Re-set the position to the captured block on the next runloop turn
/// (after SwiftUI laid the new rows out).
private func reanchorAfterPrepend() {
guard let anchor = pendingAnchorID else { return }
pendingAnchorID = nil
guard !isAtBottom else { return } // bottom-anchored: nothing shifted
Task { @MainActor in
positionID = anchor
}
}
// MARK: - New-messages pill
private var unseenCount: Int {
guard !isAtBottom, let seen = newestSeenID else { return 0 }
guard let index = model.blocks.lastIndex(where: { $0.stableId == seen }) else { return 0 }
return model.blocks.count - 1 - index
}
@ViewBuilder
private var newMessagesPill: some View {
let count = unseenCount
if count > 0 {
Button {
newestSeenID = newestBlockID
positionID = Self.bottomSentinelID
} label: {
Text(count == 1 ? "1 new message ↓" : "\(count) new messages ↓")
.font(.footnote.weight(.medium))
.padding(.horizontal, 14)
.padding(.vertical, 7)
.background(.tint, in: Capsule())
.foregroundStyle(.white)
.shadow(radius: 3, y: 1)
}
.buttonStyle(.plain)
.padding(.bottom, 12)
}
}
// MARK: - Chrome
private var headerTitle: some View {
HStack(spacing: 8) {
StatusDot(active: model.header.active, thinking: model.header.thinking)
VStack(alignment: .leading, spacing: 0) {
Text(model.header.title)
.font(.headline)
.lineLimit(1)
if let subtitle = model.header.subtitle {
Text(subtitle)
.font(.caption2)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
}
}
@ViewBuilder
private var banners: some View {
VStack(spacing: 0) {
if let warning = model.warning {
HStack(spacing: 8) {
Text(warning)
.font(.footnote)
.lineLimit(2)
Spacer(minLength: 8)
Button("Retry") {
model.retry()
}
.font(.footnote.weight(.semibold))
}
.padding(.horizontal, 16)
.padding(.vertical, 6)
.frame(maxWidth: .infinity)
.background(.red.opacity(0.14))
.foregroundStyle(.red)
}
if case .backoff = model.connectionState {
Text("Live updates interrupted — reconnecting…")
.font(.footnote)
.frame(maxWidth: .infinity)
.padding(.vertical, 5)
.background(.orange.opacity(0.15))
.foregroundStyle(.orange)
}
}
}
// MARK: - Empty / loading / error
private var initialLoading: some View {
VStack(spacing: 12) {
ProgressView()
Text("Loading messages…")
.font(.subheadline)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
private var loadFailedState: some View {
ContentUnavailableView {
Label("Couldn't load this session", systemImage: "wifi.slash")
} description: {
Text("Check the connection to your hub and try again.")
} actions: {
Button("Retry") {
model.retry()
}
.buttonStyle(.borderedProminent)
}
}
private var emptyState: some View {
ContentUnavailableView {
Label("No messages yet", systemImage: "bubble.left.and.bubble.right")
} description: {
Text("Messages will appear here as the agent works.")
}
}
}
/// Centered "· · ·" / spinner row doubling as the load-older top sentinel.
private struct OlderHistoryRow: View {
let isLoading: Bool
var body: some View {
HStack(spacing: 8) {
if isLoading {
ProgressView()
.controlSize(.small)
Text("Loading older messages…")
.font(.caption)
.foregroundStyle(.secondary)
} else {
Text("· · ·")
.font(.caption)
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity)
.padding(.vertical, 8)
}
}