forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentFailureTranscriptFormatter.swift
More file actions
137 lines (125 loc) · 5.03 KB
/
Copy pathAgentFailureTranscriptFormatter.swift
File metadata and controls
137 lines (125 loc) · 5.03 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import Foundation
enum AgentFailureTranscriptFormatter {
// Provider-neutral fallback: the default background agent is Omi-native, so a
// generic start failure must not blame OpenClaw (or any external provider) —
// the user may never have installed or intended to use one. Genuine
// missing-adapter errors still map to provider-specific setup copy below.
static let genericSpawnFailure = "Agent couldn't start — try again"
static func errorText(for projection: AgentRunProjection) -> String? {
switch projection.status {
case .failed, .timedOut, .orphaned:
let raw =
projection.failure?.displayMessage
?? projection.errorMessage
?? projection.statusText
?? "Agent failed"
return userFacingFailure(raw, harnessMode: harnessMode(from: projection))
case .idle, .queued, .starting, .running, .waitingInput, .waitingApproval, .cancelling, .succeeded, .cancelled:
return nil
}
}
static func transcriptText(for errorText: String) -> String? {
let trimmed = errorText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil }
let sanitized = userFacingFailure(trimmed, harnessMode: nil)
if sanitized.lowercased().hasPrefix("failed:") {
return sanitized
}
return "Failed: \(sanitized)"
}
/// Strip HTTP/URLSession guts; map missing OpenClaw/Hermes adapters to setup copy.
static func userFacingFailure(
_ errorText: String,
harnessMode: AgentHarnessMode? = nil,
directedProvider: AgentPillsManager.DirectedProvider? = nil
) -> String {
let trimmed = errorText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return genericSpawnFailure }
if looksLikeSetupNeeded(trimmed) {
if let directedProvider {
return directedProvider.setupNeededStatus
}
if let harnessMode {
switch harnessMode {
case .openclaw: return AgentPillsManager.DirectedProvider.openclaw.setupNeededStatus
case .hermes: return AgentPillsManager.DirectedProvider.hermes.setupNeededStatus
default: break
}
}
let lower = trimmed.lowercased()
if lower.contains("hermes") {
return AgentPillsManager.DirectedProvider.hermes.setupNeededStatus
}
if lower.contains("openclaw") || lower.contains("open claw") {
return AgentPillsManager.DirectedProvider.openclaw.setupNeededStatus
}
return genericSpawnFailure
}
if looksLikeRawTransportGuts(trimmed) {
return genericSpawnFailure
}
// Prefer short, non-technical copy already authored for the UI.
if trimmed.count <= 120,
!trimmed.contains("http"),
!trimmed.contains("URLSession"),
!trimmed.contains("NSURLError")
{
return trimmed
}
return genericSpawnFailure
}
static func userFacingFailure(
for error: Error,
harnessMode: AgentHarnessMode? = nil,
directedProvider: AgentPillsManager.DirectedProvider? = nil
) -> String {
if let runtime = error as? BridgeError, case .agentRuntimeFailure(let failure) = runtime {
let inferred = harnessModeFromAdapterId(failure.adapterId)
return userFacingFailure(
failure.displayMessage,
harnessMode: harnessMode ?? inferred,
directedProvider: directedProvider)
}
let raw = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription
return userFacingFailure(raw, harnessMode: harnessMode, directedProvider: directedProvider)
}
private static func looksLikeSetupNeeded(_ text: String) -> Bool {
let lower = text.lowercased()
if lower.contains("needs setup") { return true }
if lower.contains("omi_openclaw_adapter") || lower.contains("omi_hermes_adapter") { return true }
if lower.contains("adapter")
&& (lower.contains("missing") || lower.contains("unavailable") || lower.contains("not found")
|| lower.contains("not configured") || lower.contains("no such file"))
{
return true
}
if (lower.contains("openclaw") || lower.contains("hermes"))
&& (lower.contains("not found") || lower.contains("not installed")
|| lower.contains("not configured") || lower.contains("no such file"))
{
return true
}
return false
}
private static func looksLikeRawTransportGuts(_ text: String) -> Bool {
let lower = text.lowercased()
return lower.contains("urlsession")
|| lower.contains("nsurlerror")
|| lower.contains("http://")
|| lower.contains("https://")
|| lower.contains("(-1001)")
|| lower.contains("(-1009)")
|| lower.contains("status code")
|| lower.contains("task failed")
}
private static func harnessMode(from projection: AgentRunProjection) -> AgentHarnessMode? {
if let adapterId = projection.failure?.adapterId {
return harnessModeFromAdapterId(adapterId)
}
return nil
}
private static func harnessModeFromAdapterId(_ adapterId: String?) -> AgentHarnessMode? {
guard let adapterId else { return nil }
return AgentRuntimeRouting.harnessMode(from: adapterId)
}
}