SPB Git

Repository Public License

AgentEvents.swift 5.3 KB 141 lines swift

1import Foundation2import NavigatorCore34/// The orchestrator's roles, expressed as phases of one loop.5public enum AgentPhase: String, Sendable, Hashable, Codable {6    /// Navigating, searching, reading, inspecting and interacting.7    case research8    /// Extracting structured data, comparing, capturing, downloading.9    case extraction10    /// The model is writing the final answer (near the end of the budget only `finish` is offered).11    case synthesis12    /// The runtime checks the answer against the sources of the run.13    case verification14}1516// MARK: - Confirmation1718/// What the user is asked to approve. Everything in it is written by the runtime from real page state; values of19/// secret fields are masked before the request is built.20public struct ConfirmationRequest: Sendable, Hashable, Identifiable {21    public enum Kind: Sendable, Hashable {22        case formSubmission23        case sensitiveAction(SensitiveActionCategory)24        case irreversibleAction25        case download26        case navigationOutsideAllowedDomains27        case possibleExfiltration28        case closeSharedTab29    }3031    public struct DataItem: Sendable, Hashable {32        public var name: String33        public var value: String34        /// The real value was replaced by a mask.35        public var isMasked: Bool36        public init(name: String, value: String, isMasked: Bool = false) { self.name = name; self.value = value; self.isMasked = isMasked }37    }3839    public var id: Int40    public var runID: AgentRunID41    public var kind: Kind42    /// Canonical tool name (`browser.submit_form`).43    public var tool: String44    /// One sentence: what will happen if approved.45    public var action: String46    /// The origin that receives the data (`https://shop.example`).47    public var destination: String48    public var url: URL?49    /// Exactly what would be transmitted: field names and values.50    public var data: [DataItem]51    /// Why the runtime is asking.52    public var reasons: [String]53    public var risk: AgentRiskLevel54}5556public enum ConfirmationDecision: String, Sendable, Hashable, Codable {57    case approved58    case denied59    /// The run was stopped (or reached its time limit) while the question was open. Produced by the runtime.60    case cancelled61}6263/// The agent met something only a person may do and gives the tab back to the user.64public struct AgentHandoffRequest: Sendable, Hashable, Identifiable {65    public var id: Int66    public var runID: AgentRunID67    public var tab: TabID68    public var kind: AgentChallengeKind69    public var origin: String70    public var evidence: String71}7273public enum AgentHandoffDecision: String, Sendable, Hashable, Codable {74    /// The user dealt with it in the tab; the agent may look at the page again.75    case userCompleted76    /// The user does not want to; the agent leaves this page alone.77    case declined78    case cancelled79}8081/// Implemented by the UI. Both calls suspend until the user answers; the runtime abandons them when the run stops82/// (the task is cancelled and `confirmationResolved` / `handoffResolved` carry `.cancelled`).83public protocol AgentConfirmationDelegate: Sendable {84    func confirm(_ request: ConfirmationRequest) async -> ConfirmationDecision85    func handOff(_ request: AgentHandoffRequest) async -> AgentHandoffDecision86}8788// MARK: - Events8990public struct AgentWarning: Sendable, Hashable {91    public enum Kind: String, Sendable, Hashable, Codable {92        case injectionSuspected, hiddenTextOmitted, usageEstimated, costUnknown, contextCompacted, answerNotFullyVerified, persistenceFailed93    }9495    public var kind: Kind96    /// The step it concerns, when there is one.97    public var stepID: Int?98    public var message: String99    public var findings: [InjectionFinding]100}101102public struct AgentUsageReport: Sendable, Hashable {103    public var usage: TokenUsage104    /// At least one request reported no token counts; those were counted from character lengths (3 characters per105    /// token, deliberately high).106    public var tokensAreEstimated: Bool107    /// `false` when no price is known for the model: the cost limit then cannot be enforced and `usage.estimatedCostUSD` is `nil`.108    public var costIsKnown: Bool109    public var stepsUsed: Int110    public var elapsedSeconds: Double111}112113public struct AgentRunResult: Sendable, Hashable {114    public var run: AgentRun115    /// Why the run ended, in one sentence.116    public var reason: String117    /// Present only when the run completed.118    public var answer: VerifiedAnswer?119    public var usage: AgentUsageReport120}121122/// What the UI observes. Steps of kind `.toolCall` are created by the runtime when it executes (or rejects) a tool123/// call and never from model text: `textDelta` is the model's commentary and is not a record of anything done.124public enum AgentEvent: Sendable, Hashable {125    case started(AgentRun)126    case phaseChanged(AgentPhase)127    case stepStarted(AgentStep)128    case stepUpdated(AgentStep)129    case stepFinished(AgentStep)130    case textDelta(String)131    case citationAdded(Citation)132    case confirmationRequested(ConfirmationRequest)133    case confirmationResolved(id: Int, decision: ConfirmationDecision)134    case handoffRequested(AgentHandoffRequest)135    case handoffResolved(id: Int, decision: AgentHandoffDecision)136    case warning(AgentWarning)137    case usage(AgentUsageReport)138    /// Always the last event.139    case finished(AgentRunResult)140}141
Files main
Menu

Theme