forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentRuntimeFailure.swift
More file actions
111 lines (103 loc) · 3.59 KB
/
Copy pathAgentRuntimeFailure.swift
File metadata and controls
111 lines (103 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Foundation
enum AgentRuntimeFailureCode: String, CaseIterable, Equatable, Sendable {
case authentication
case quotaExceeded = "quota_exceeded"
case invalidRequest = "invalid_request"
case timeout
case transportInterruption = "transport_interruption"
case adapterUnavailable = "adapter_unavailable"
case adapterIncompatible = "adapter_incompatible"
case bridgeStartFailed = "bridge_start_failed"
case providerSetupNeeded = "provider_setup_needed"
case malformedOrOversizedToolResult = "malformed_or_oversized_tool_result"
case cancelled
case staleOwner = "stale_owner"
case policyDenied = "policy_denied"
case unknown
}
struct AgentRuntimeFailure: Equatable, Sendable {
let code: String
/// Closed wire classification; `code` remains the detailed diagnostic key.
let failureCode: AgentRuntimeFailureCode
let userMessage: String
let technicalMessage: String?
let source: String?
let adapterId: String?
let provider: String?
let retryable: Bool?
let recoveryAction: String?
let recoveryOutcome: String?
let retryDisposition: String?
/// Qualification-only gateway attribution; nil means unavailable.
let jitCostStatus: String?
let jitEstimatedCostUsd: Double?
init(
code: String,
failureCode: AgentRuntimeFailureCode = .unknown,
userMessage: String,
technicalMessage: String? = nil,
source: String? = nil,
adapterId: String? = nil,
provider: String? = nil,
retryable: Bool? = nil,
recoveryAction: String? = nil,
recoveryOutcome: String? = nil,
retryDisposition: String? = nil,
jitCostStatus: String? = nil,
jitEstimatedCostUsd: Double? = nil
) {
self.code = code
self.failureCode = failureCode
self.userMessage = userMessage
self.technicalMessage = technicalMessage
self.source = source
self.adapterId = adapterId
self.provider = provider
self.retryable = retryable
self.recoveryAction = recoveryAction
self.recoveryOutcome = recoveryOutcome
self.retryDisposition = retryDisposition
self.jitCostStatus = jitCostStatus
self.jitEstimatedCostUsd = jitEstimatedCostUsd
}
var displayMessage: String {
let trimmed = userMessage.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmed.isEmpty {
return trimmed
}
return technicalMessage?.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty ?? "Agent run failed"
}
static func parse(from value: Any?) -> AgentRuntimeFailure? {
guard let payload = value as? [String: Any],
let code = payload["code"] as? String
else {
return nil
}
let userMessage =
(payload["userMessage"] as? String)
?? (payload["message"] as? String)
?? (payload["technicalMessage"] as? String)
?? "Agent run failed"
return AgentRuntimeFailure(
code: code,
failureCode: (payload["failureCode"] as? String)
.flatMap(AgentRuntimeFailureCode.init(rawValue:)) ?? .unknown,
userMessage: userMessage,
technicalMessage: payload["technicalMessage"] as? String,
source: payload["source"] as? String,
adapterId: payload["adapterId"] as? String,
provider: payload["provider"] as? String,
retryable: payload["retryable"] as? Bool,
recoveryAction: payload["recoveryAction"] as? String,
recoveryOutcome: payload["recoveryOutcome"] as? String,
retryDisposition: payload["retryDisposition"] as? String,
jitCostStatus: payload["jitCostStatus"] as? String,
jitEstimatedCostUsd: payload["jitEstimatedCostUsd"] as? Double
)
}
}
extension String {
fileprivate var nilIfEmpty: String? {
isEmpty ? nil : self
}
}