Files
hapi/ios/Packages/HapiKit/Sources/HapiProtocol/Models/AgentState.swift
T
wushenghuaandHAPI 19b2435ea8 feat(ios): H5-style session rows and fix stuck queued actions
Session rows now match the web sidebar: the second line shows the last
two path components, active shares get a "Shared" pill, relative ages
read like the H5 list (now, 5 min ago, 10 hours ago, 2 days ago, then a
date), and the list font scale reaches 60%.

The running spinner is drawn from a timeline instead of ProgressView:
UIKit-backed spinners stop animating when a List cell is reconfigured,
which read as a spinner that often failed. Working rows also keep their
spinner while a pending approval is shown, as on web.

Queued-message actions no longer look dead. Cancelling a row the hub
reports as `busy` because it is already indeterminate used to restore
the row unchanged; it now keeps a `queueDismissed` hold (web #1839), so
Cancel and Retry take effect while the durable record stays available
for a late consume. Steer is hidden for flavors that cannot take it
(agy needs acp, cursor needs acp) via a port of the web steering gate.

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

Co-Authored-By: HAPI <noreply@hapi.run>
2026-12-05 12:11:13 +08:00

130 lines
4.9 KiB
Swift

import Foundation
/// A pending tool-approval / user-input request raised by the agent.
///
/// Mirrors `AgentStateRequestSchema` (`shared/src/schemas.ts`). Permission
/// requests are NOT messages: they live on `Session.agentState.requests`
/// keyed by request id and are resolved over
/// `POST /api/sessions/:id/permissions/:requestId/approve|deny`.
public struct AgentStateRequest: Codable, Equatable, Sendable {
public var tool: String
/// Correlates a native tool call; approval still uses the request dictionary key.
public var toolCallId: String?
/// Free-form tool arguments (`z.unknown()` on the wire).
public var arguments: JSONValue?
/// Epoch ms when the request was raised; older CLIs omit it.
public var createdAt: Int?
public init(tool: String, arguments: JSONValue? = nil, createdAt: Int? = nil, toolCallId: String? = nil) {
self.tool = tool
self.toolCallId = toolCallId
self.arguments = arguments
self.createdAt = createdAt
}
}
/// A resolved permission request, moved from `requests` to
/// `completedRequests` once decided.
///
/// Mirrors `AgentStateCompletedRequestSchema` (`shared/src/schemas.ts`).
public struct AgentStateCompletedRequest: Codable, Equatable, Sendable {
public enum Status: String, Codable, Sendable {
case resolved
case canceled
case denied
case approved
}
public enum Decision: String, Codable, Sendable {
case approved
case approvedForSession = "approved_for_session"
case denied
case abort
}
public var tool: String
/// Correlates a native tool call; approval still uses the request dictionary key.
public var toolCallId: String?
public var arguments: JSONValue?
public var createdAt: Int?
public var completedAt: Int?
public var status: Status
public var reason: String?
/// Permission mode switched to while approving, when any.
public var mode: String?
public var decision: Decision?
public var allowTools: [String]?
/// Two wire formats, kept raw:
/// flat `{question: [answers]}` (AskUserQuestion) or
/// nested `{question: {answers: [...]}}` (request_user_input).
public var answers: JSONValue?
public init(
tool: String,
arguments: JSONValue? = nil,
createdAt: Int? = nil,
completedAt: Int? = nil,
status: Status,
reason: String? = nil,
mode: String? = nil,
decision: Decision? = nil,
allowTools: [String]? = nil,
answers: JSONValue? = nil,
toolCallId: String? = nil
) {
self.tool = tool
self.toolCallId = toolCallId
self.arguments = arguments
self.createdAt = createdAt
self.completedAt = completedAt
self.status = status
self.reason = reason
self.mode = mode
self.decision = decision
self.allowTools = allowTools
self.answers = answers
}
}
/// Agent-owned session state carried on `Session.agentState` and patched over
/// SSE under the `agentStateVersion` gate.
///
/// Mirrors `AgentStateSchema` (`shared/src/schemas.ts:195-203`).
public struct AgentState: Codable, Equatable, Sendable {
/// `true` while the terminal owns the session (remote control paused).
public var controlledByUser: Bool?
/// Launch mode the session was started in (persisted for reopen/resume).
public var startingMode: SessionStartingMode?
/// Current actionable shared Codex proposal's tool-call id. Nil withdraws
/// the plan menu; proposals are not entries in `requests`.
public var codexPlanProposalId: String?
/// Pending requests keyed by request id.
public var requests: [String: AgentStateRequest]?
/// Resolved requests keyed by request id.
public var completedRequests: [String: AgentStateCompletedRequest]?
/// Epoch ms when the agent started compacting its context; nil otherwise.
public var contextCompactionStartedAt: Int?
/// Provider-reported active steerable turn (`steeringActive` in
/// `shared/src/schemas.ts`). Steer is offered only while this is true
/// (pi uses `thinking` instead).
public var steeringActive: Bool?
public init(
controlledByUser: Bool? = nil,
startingMode: SessionStartingMode? = nil,
requests: [String: AgentStateRequest]? = nil,
completedRequests: [String: AgentStateCompletedRequest]? = nil,
codexPlanProposalId: String? = nil,
contextCompactionStartedAt: Int? = nil,
steeringActive: Bool? = nil
) {
self.controlledByUser = controlledByUser
self.startingMode = startingMode
self.codexPlanProposalId = codexPlanProposalId
self.requests = requests
self.completedRequests = completedRequests
self.contextCompactionStartedAt = contextCompactionStartedAt
self.steeringActive = steeringActive
}
}