forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatErrorCard.swift
More file actions
235 lines (217 loc) · 7.59 KB
/
Copy pathChatErrorCard.swift
File metadata and controls
235 lines (217 loc) · 7.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import OmiTheme
import SwiftUI
// MARK: - ChatErrorCard
//
// Renders a single `ChatErrorState` as an inline, message-level card with
// a primary recovery CTA. Visual conventions follow the existing chat
// banners (tinted backdrop + SF Symbol + tinted border) so the surfaces
// feel like siblings.
struct ChatErrorCard: View {
let state: ChatErrorState
let onRecover: () -> Void
let onDismiss: (() -> Void)?
@State private var showDetails: Bool = false
var body: some View {
VStack(alignment: .leading, spacing: OmiSpacing.sm) {
HStack(alignment: .top, spacing: OmiSpacing.sm) {
Image(systemName: iconName)
.scaledFont(size: OmiType.body)
.foregroundColor(accentColor)
.frame(width: 16, alignment: .center)
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text(headline)
.scaledFont(size: OmiType.body, weight: .semibold)
.foregroundColor(Ink.primary)
Text(detail)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: 8)
if let onDismiss = onDismiss {
Button(action: onDismiss) {
Image(systemName: "xmark")
.scaledFont(size: OmiType.micro)
.foregroundColor(Ink.secondary)
}
.buttonStyle(.plain)
.help("Dismiss")
}
}
HStack(spacing: OmiSpacing.sm) {
Button(action: onRecover) {
Text(primaryCTATitle)
.scaledFont(size: OmiType.caption, weight: .medium)
// The label ladder inverted on the filled pill, and a full stadium.
.foregroundColor(Ink.surface)
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.xxs)
.background(accentColor)
.clipShape(Capsule(style: .continuous))
}
.buttonStyle(.plain)
if !detailsBody.isEmpty {
Button(action: { showDetails.toggle() }) {
HStack(spacing: OmiSpacing.hairline) {
Text(showDetails ? "Hide details" : "Show details")
.scaledFont(size: OmiType.caption)
Image(systemName: showDetails ? "chevron.up" : "chevron.down")
.scaledFont(size: OmiType.micro)
}
.foregroundColor(Ink.secondary)
}
.buttonStyle(.plain)
}
Spacer()
}
if showDetails, !detailsBody.isEmpty {
Text(detailsBody)
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.fixedSize(horizontal: false, vertical: true)
.padding(.top, OmiSpacing.hairline)
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
// A wash on the panel, tinted only enough to name the state. A saturated fill
// here is a second ground, and on glass it is the loudest thing on screen.
.background(Ink.rowFill)
.overlay(
RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous)
.strokeBorder(accentColor.opacity(0.35), lineWidth: 1)
)
.clipShape(RoundedRectangle(cornerRadius: PageGlass.rowRadius, style: .continuous))
}
// MARK: - State-specific copy
private var iconName: String {
switch state {
case .authRequired:
return "person.badge.key.fill"
case .timeout:
return "clock.badge.exclamationmark.fill"
case .bridgeUnavailable:
return "bolt.horizontal.circle.fill"
case .interrupted:
return "stop.circle.fill"
case .noDataFound:
return "magnifyingglass"
}
}
/// The state's voice. Named system colours off the shared palette: `Ink.accent` for the one thing
/// that is actionable and is not already a button, `PageGlass.warning` for a state with something
/// to do about it, and `Ink.errorRed` — the only place this app raises its voice — for a failure.
private var accentColor: Color {
switch state {
case .authRequired:
return Ink.accent
case .timeout, .interrupted:
return PageGlass.warning
case .bridgeUnavailable:
return Ink.errorRed
case .noDataFound:
return Ink.secondary
}
}
private var headline: String {
switch state {
case .authRequired:
return "Sign in to continue"
case .timeout(let toolName):
if let toolName = toolName {
return "\(toolName) timed out"
}
return "AI took too long to respond"
case .bridgeUnavailable(let reason):
switch reason {
case .nodeMissing:
return "AI runtime missing"
case .runtimeMissing:
return "AI components missing"
case .crashed:
return "AI stopped unexpectedly"
case .failedToStart:
return "AI couldn't start"
case .unknown:
return "AI isn't running"
}
case .interrupted:
return "Response stopped"
case .noDataFound:
return "I didn't find anything"
}
}
private var detail: String {
switch state {
case .authRequired:
return "Your session expired. Sign in again to keep chatting."
case .timeout:
return "Try again, or rephrase your question."
case .bridgeUnavailable(let reason):
switch reason {
case .nodeMissing:
return "Node.js wasn't found. Install the runtime to keep using AI chat."
case .runtimeMissing:
return "The AI components aren't installed. Install them to keep chatting."
case .crashed:
return "The AI stopped mid-turn. Try again to start a fresh response."
case .failedToStart(let failure):
switch failure {
case .handshakeTimedOut:
return "The AI took too long to start. Try again to start a fresh response."
case .incompatibleHandshake:
return "The AI needs to restart before it can respond. Try again."
case .exitedDuringStartup, .launchFailed:
return "The AI couldn't start. Try again to start a fresh response."
}
case .unknown:
return "The AI is not responding. Try again to start a fresh response."
}
case .interrupted:
return "Ask something new when you're ready."
case .noDataFound:
return "Try a different question, or be more specific."
}
}
private var primaryCTATitle: String {
switch state.primaryRecovery {
case .retry:
return "Retry"
case .signIn:
return "Sign in"
case .installRuntime:
return "Install runtime"
case .dismiss:
return "Try a different question"
}
}
/// Optional disclosure body. Empty string = hide the disclosure button
/// entirely. Kept generic — surfaces only a redacted, user-safe
/// description of the failure class, never raw error text.
private var detailsBody: String {
switch state {
case .timeout(let toolName):
if let toolName = toolName {
return "Tool: \(toolName)"
}
return ""
case .bridgeUnavailable(let reason):
switch reason {
case .nodeMissing:
return "Cause: node binary not found on PATH."
case .runtimeMissing:
// Covers both a missing bridge script and a bundle whose agent runtime
// payload (e.g. pi-mono-extension) never made it into the install.
return "Cause: the local AI runtime is missing files on disk."
case .crashed:
return "Cause: bridge process exited."
case .failedToStart(let failure):
return "Cause: bridge start failed (\(failure.rawValue))."
case .unknown:
return ""
}
case .authRequired, .interrupted, .noDataFound:
return ""
}
}
}