Files
hapi/ios/Hapi/Features/Chat/PendingRequests.swift
T
a2f7b30ddd feat(ios): add a pending-request center to chat
Port the web's pending-request affordances to the native chat:

- A collapsible "Awaiting your reply" panel above the composer collects
  non-blocking request_user_input questions, including ones outside the
  loaded transcript. It reuses the existing question form/draft machinery,
  so collapsing and scrolling never discard an answer.
- The composer status bar shows the pending count and kind. One request
  jumps directly; several open a native menu with each request's command
  or question.
- Async questions reveal and briefly highlight in the bottom panel.
  Blocking requests scroll the transcript to their correlated tool row and
  flash that row, using a new tokenized scroll target on
  AnchoredTranscriptList.

Adds pure pending-request presentation tests and Chinese strings.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-12-01 06:32:25 +08:00

183 lines
8.0 KiB
Swift

import HapiClient
import HapiProtocol
import HapiUI
import SwiftUI
/// One live request from `agentState.requests`, flattened for presentation.
/// Kept independent of the paginated transcript: an old question stays
/// reachable after its card scrolls out of the loaded window.
struct ChatPendingRequest: Identifiable, Equatable {
let id: String
let tool: String
let toolCallId: String?
let arguments: JSONValue?
let createdAt: Int
let kind: PendingRequestKind
var isAsyncQuestion: Bool {
guard isRequestUserInputToolName(tool),
let object = arguments?.chatObject,
case .bool(false) = object["isBlocking"] else { return false }
return true
}
/// One-line secondary text in the pending menu.
var detailText: String {
let args = arguments?.chatObject
if kind == .input {
if let questions = args?["questions"]?.chatArray,
let first = questions.first?.chatObject {
if let text = first["question"]?.chatString, !text.isEmpty { return text }
if let text = first["prompt"]?.chatString, !text.isEmpty { return text }
if let text = first["header"]?.chatString, !text.isEmpty { return text }
}
if let text = args?["question"]?.chatString, !text.isEmpty { return text }
if let text = args?["prompt"]?.chatString, !text.isEmpty { return text }
}
if let text = args?["command"]?.chatString, !text.isEmpty { return text }
if let text = args?["description"]?.chatString, !text.isEmpty { return text }
if let text = args?["path"]?.chatString, !text.isEmpty { return text }
if let text = args?["file_path"]?.chatString, !text.isEmpty { return text }
return tool
}
}
/// Build the synthetic tool call the existing question form expects. The
/// request dictionary key (not the correlated tool id) is the reply target.
func pendingRequestTool(_ request: ChatPendingRequest) -> ChatToolCall {
ChatToolCall(
id: request.id,
name: request.tool,
state: .pending,
input: request.arguments,
createdAt: request.createdAt,
permission: ToolPermission(id: request.id, status: .pending)
)
}
/// Collapsible "Awaiting your reply" section above the composer. Mirrors the
/// web AsyncQuestionsPanel: non-blocking `request_user_input` questions stay
/// answerable even when their transcript card is not loaded.
struct PendingQuestionsPanel: View {
let interactor: ChatInteractor
let requests: [ChatPendingRequest]
let enabled: Bool
let targetRequestID: String?
let targetToken: Int
var onTargetConsumed: (() -> Void)? = nil
@Environment(\.hapiTheme) private var theme
@Environment(\.hapiTypography) private var typography
@State private var expanded = false
@State private var highlightedRequestID: String?
@State private var highlightTask: Task<Void, Never>?
var body: some View {
if !requests.isEmpty {
VStack(spacing: 0) {
Button {
withAnimation(.snappy(duration: 0.2)) { expanded.toggle() }
} label: {
HStack(spacing: 8) {
Image(systemName: expanded ? "chevron.down" : "chevron.right")
.font(typography.captionFont)
.foregroundStyle(theme.textSecondary)
.accessibilityHidden(true)
Text("Awaiting your reply")
.font(typography.toolTitleFont)
.foregroundStyle(theme.textPrimary)
Spacer(minLength: 8)
Text("\(requests.count)")
.font(typography.captionFont.monospacedDigit())
.padding(.horizontal, 7)
.padding(.vertical, 2)
.background(theme.surface, in: Capsule())
.foregroundStyle(theme.textSecondary)
}
.padding(.horizontal, 12)
.frame(minHeight: 44)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityIdentifier("pending-questions-toggle")
if expanded {
ScrollViewReader { proxy in
ScrollView {
VStack(alignment: .leading, spacing: 8) {
Text("These questions do not block the agent. Answer here, or leave the ones you no longer need.")
.font(typography.captionFont)
.foregroundStyle(theme.textSecondary)
.padding(.horizontal, 12)
ForEach(requests) { request in
VStack(alignment: .leading, spacing: 0) {
QuestionAnswerFormView(
tool: pendingRequestTool(request),
requestId: request.id,
enabled: enabled,
submitting: interactor.permissionOverrides[request.id] == .resolving,
openDetails: nil,
submit: { action in
interactor.resolvePermission(requestId: request.id, action: action)
}
)
}
.background(theme.surface, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
.overlay {
if highlightedRequestID == request.id {
RoundedRectangle(cornerRadius: 14, style: .continuous)
.stroke(theme.warning, lineWidth: 2)
}
}
.id(request.id)
}
}
.padding(.horizontal, 8)
.padding(.bottom, 10)
}
.frame(maxHeight: 300)
.onChange(of: targetToken) { _, _ in
revealTarget(with: proxy)
}
.onAppear {
if targetRequestID != nil { revealTarget(with: proxy) }
}
}
}
}
.background(theme.background, in: RoundedRectangle(cornerRadius: 16, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.stroke(theme.divider, lineWidth: 0.5)
}
.hapiReadingColumn()
.padding(.horizontal, 8)
.padding(.bottom, 6)
.onChange(of: targetToken) { _, _ in
guard targetRequestID != nil else { return }
expanded = true
}
.onDisappear { highlightTask?.cancel() }
}
}
private func revealTarget(with proxy: ScrollViewProxy) {
guard let targetRequestID,
requests.contains(where: { $0.id == targetRequestID }) else { return }
expanded = true
highlightedRequestID = targetRequestID
DispatchQueue.main.async {
withAnimation(.snappy(duration: 0.2)) {
proxy.scrollTo(targetRequestID, anchor: .center)
}
onTargetConsumed?()
}
highlightTask?.cancel()
highlightTask = Task {
try? await Task.sleep(for: .seconds(1.8))
guard !Task.isCancelled else { return }
highlightedRequestID = nil
}
}
}