forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskChatPanel.swift
More file actions
464 lines (429 loc) · 16.4 KB
/
Copy pathTaskChatPanel.swift
File metadata and controls
464 lines (429 loc) · 16.4 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import OmiTheme
import SwiftUI
/// Task-scoped view into one durable Omi thread. Multiple tasks may project the
/// same messages, artifacts, and kernel run while keeping distinct UI scope.
struct TaskChatPanel: View {
@ObservedObject var taskState: TaskChatState
@ObservedObject var coordinator: TaskChatCoordinator
let task: TaskActionItem?
let onClose: () -> Void
let onOpenRewindEvidence: ((Int64) -> Void)?
@State private var showsThreadContext = true
@ObservedObject private var runtimeStatusStore = AgentRuntimeStatusStore.shared
var body: some View {
VStack(spacing: 0) {
// Compact header
panelHeader
Divider()
.background(Ink.rowFillHover)
if coordinator.activeTaskId == nil {
// No task selected — prompt user to pick one
noTaskSelectedView
} else if coordinator.isOpening {
// Loading state while session is being created
VStack(spacing: OmiSpacing.md) {
Spacer()
ProgressView()
.scaleEffect(0.8)
Text("Setting up chat...")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
} else {
if let projection = coordinator.activeThreadProjection {
TaskThreadOverview(
projection: projection,
runtimeProjection: runtimeStatusStore.projection(
for: .workstream(workstreamId: projection.workstreamID)
),
isExpanded: $showsThreadContext,
onOpenRewindEvidence: onOpenRewindEvidence
)
Divider().background(Ink.rowFillHover)
}
// Messages area fills all remaining space.
// ChatInputView lives in .safeAreaInset so its height changes (editorHeight,
// Wispr Flow insertions, etc.) never trigger re-measurement of ChatMessagesView.
// Putting both in the same VStack caused a recursive StackLayout sizing loop
// (FlexFrame → ZStack → StackLayout → FlexFrame at 100% CPU) every time the
// input field changed height.
ChatMessagesView(
messages: taskState.messages,
conversationIdentity: coordinator.activeTaskId ?? "task-chat-none",
isSending: taskState.isSending,
hasMoreMessages: false,
isLoadingMoreMessages: false,
isLoadingInitial: false,
app: nil,
onLoadMore: {},
onRate: { _, _, _ in },
localSendToken: taskState.localSendToken,
// The task panel renders the same interactable content blocks as the
// main window; taps route the one shell (`ChatFirstRichBlockContext.auxiliary`).
chatFirstRichBlockContext: .auxiliary(chatProvider: coordinator.chatProvider),
// The compact window, like the main chat's host: without it this panel
// mounts the 500-row default eagerly, which measured 910 ms and 607
// native views for 400 messages against 114 ms and 84 compact (see the
// comment on QueryAnswerThread's host). "Show older messages" is the
// way back to the rest of a long thread.
transcriptWindowPolicy: .compactHome,
enablesPromptTimeline: false,
// This thread is about one task; the day's summary belongs in the main chat.
showsDailySummary: false,
welcomeContent: { taskWelcome }
)
.frame(maxHeight: .infinity)
.safeAreaInset(edge: .bottom, spacing: 0) {
VStack(spacing: 0) {
// Error banner
if let error = taskState.errorMessage {
HStack(spacing: OmiSpacing.sm) {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(PageGlass.warning)
.scaledFont(size: OmiType.body)
Text(error)
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
Spacer()
Button {
taskState.errorMessage = nil
} label: {
Image(systemName: "xmark")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
}
.buttonStyle(.plain)
}
.padding(.horizontal, OmiSpacing.lg)
.padding(.vertical, OmiSpacing.sm)
.background(Ink.rowFill)
}
// Input area
ChatInputView(
onSend: { text in
Task {
await taskState.sendMessage(
text,
taskContext: coordinator.activeContextPacket,
onAcceptedWithAttemptID: { attemptID in
AnalyticsManager.shared.chatMessageSent(
messageLength: text.count, source: "task_chat", attemptID: attemptID)
}
)
await coordinator.refreshActiveThread()
}
},
onStop: {
taskState.stopAgent()
},
isSending: taskState.isSending,
isStopping: taskState.isStopping,
placeholder: "Continue this work...",
mode: $taskState.chatMode,
pendingText: $coordinator.pendingInputText,
inputText: $taskState.draftText,
// Task-thread voice routing is not wired yet; keep the shared
// composer from exposing the global push-to-talk route here.
showsPushToTalk: false
)
.padding(OmiSpacing.md)
}
.background(Ink.rowFill)
}
}
}
.background(Color.clear)
}
// MARK: - Header
/// Abbreviate a path for display: ~/Projects/my-app
private var displayPath: String {
let path = coordinator.workspacePath
let home = NSHomeDirectory()
if path.hasPrefix(home) {
return "~" + path.dropFirst(home.count)
}
return path
}
private var panelHeader: some View {
VStack(spacing: 0) {
HStack(spacing: OmiSpacing.sm) {
Image(systemName: "bubble.left.and.bubble.right")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
Text(task?.description ?? coordinator.activeThreadProjection?.title ?? "Omi thread")
.scaledFont(size: OmiType.body, weight: .semibold)
.foregroundColor(Ink.primary)
.lineLimit(1)
.truncationMode(.tail)
Spacer()
Button(action: onClose) {
Image(systemName: "xmark")
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
.frame(width: 20, height: 20)
}
.buttonStyle(.plain)
.help("Close chat panel")
}
// Workspace path indicator (only when a task is active)
if coordinator.activeTaskId != nil {
HStack(spacing: OmiSpacing.xxs) {
Image(systemName: "folder")
.scaledFont(size: OmiType.micro)
Text(displayPath)
.scaledFont(size: OmiType.micro)
.lineLimit(1)
.truncationMode(.middle)
Spacer()
}
.foregroundColor(Ink.secondary)
.padding(.top, OmiSpacing.xxs)
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.background(Ink.rowFillHover.opacity(0.5))
}
// MARK: - Empty State
private var noTaskSelectedView: some View {
VStack(spacing: OmiSpacing.lg) {
Spacer()
Image(systemName: "text.bubble")
.scaledFont(size: 36)
.foregroundColor(Ink.secondary)
Text("Open a task thread")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.secondary)
Text("Choose Work on this with Omi on a task, or open one that already has a thread.")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, OmiSpacing.xxl)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
// MARK: - Welcome
private var taskWelcome: some View {
VStack(spacing: OmiSpacing.md) {
Image(systemName: "bubble.left.and.bubble.right")
.scaledFont(size: 32)
.foregroundColor(Ink.secondary)
Text("Work on this with Omi")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.secondary)
Text("Continue the same work as context changes, without starting over.")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, OmiSpacing.xl)
}
.frame(maxWidth: .infinity)
.padding()
.padding(.vertical, 60)
}
}
private struct TaskThreadOverview: View {
let projection: TaskThreadProjection
let runtimeProjection: AgentRunProjection?
@Binding var isExpanded: Bool
let onOpenRewindEvidence: ((Int64) -> Void)?
var body: some View {
DisclosureGroup(isExpanded: $isExpanded) {
ScrollView(.vertical) {
VStack(alignment: .leading, spacing: OmiSpacing.md) {
contextSection("Current state") {
Text(projection.currentSummary)
}
if let runtimeProjection, runtimeProjection.status.isActive {
contextSection("Omi activity") {
HStack(spacing: OmiSpacing.xs) {
ProgressView().controlSize(.small)
Text(
runtimeProjection.statusText
?? runtimeProjection.status.rawValue.replacingOccurrences(of: "_", with: " ").capitalized)
}
}
}
if !projection.recentEvents.isEmpty {
contextSection("Recent changes") {
ForEach(projection.recentEvents, id: \.eventId) { event in
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
Text(event.summary)
evidenceRow(event.evidenceRefs ?? [])
}
}
}
}
if !projection.scopedTasks.isEmpty {
contextSection("Tasks") {
ForEach(projection.scopedTasks, id: \.id) { item in
HStack(alignment: .top, spacing: OmiSpacing.xs) {
Image(systemName: item.completed ? "checkmark.circle.fill" : "circle")
.foregroundColor(
item.id == projection.activeTaskID ? Ink.primary : Ink.secondary)
Text(item.description_)
.fontWeight(item.id == projection.activeTaskID ? .semibold : .regular)
}
}
}
}
if !projection.artifactVersions.isEmpty {
contextSection("Artifacts") {
ForEach(projection.artifactVersions, id: \.artifactId) { artifact in
VStack(alignment: .leading, spacing: OmiSpacing.hairline) {
HStack(spacing: OmiSpacing.xs) {
Text(
artifact.logicalKey
.replacingOccurrences(of: "_", with: " ")
.replacingOccurrences(of: "-", with: " ")
.capitalized
)
.fontWeight(.medium)
Text("v\(artifact.version)")
.foregroundColor(Ink.secondary)
if artifact.supersedesArtifactId == nil {
Text("Original")
.foregroundColor(Ink.secondary)
}
}
evidenceRow(artifact.evidenceRefs ?? [])
if let url = URL(string: artifact.uri), !artifact.uri.isEmpty {
Link("Open artifact", destination: url)
.foregroundColor(Ink.primary)
}
}
}
}
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.bottom, OmiSpacing.sm)
}
.frame(maxHeight: 340)
} label: {
HStack {
Text("Ongoing work")
.scaledFont(size: OmiType.caption, weight: .semibold)
Spacer()
Text("\(projection.scopedTasks.count) tasks · \(projection.artifactVersions.count) artifacts")
.scaledFont(size: OmiType.micro)
.foregroundColor(Ink.secondary)
}
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.foregroundColor(Ink.secondary)
.background(Ink.rowFill.opacity(0.5))
}
private func contextSection<Content: View>(
_ title: String,
@ViewBuilder content: () -> Content
) -> some View {
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
Text(title.uppercased())
.scaledFont(size: OmiType.micro, weight: .semibold)
.foregroundColor(Ink.secondary)
content()
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
@ViewBuilder
private func evidenceRow(_ refs: [OmiAPI.EvidenceRef]) -> some View {
if !refs.isEmpty {
VStack(alignment: .leading, spacing: OmiSpacing.xxs) {
let visibleRefs = Array(refs.prefix(3))
let currentDeviceID = ClientDeviceService.shared.clientDeviceId
ForEach(Array(visibleRefs.enumerated()), id: \.offset) { _, ref in
if let card = RewindEvidenceCardPolicy.card(for: ref, currentDeviceID: currentDeviceID) {
RewindEvidenceCardView(
card: card,
onOpen: RewindEvidenceCardPolicy.openHandler(
for: card.screenshotID,
onOpen: onOpenRewindEvidence
)
)
} else {
HStack(spacing: OmiSpacing.xxs) {
Image(systemName: "link")
Text("\(ref.kind.userFacingLabel):\(ref.id)")
.lineLimit(1)
.truncationMode(.middle)
}
}
}
}
.scaledFont(size: OmiType.micro)
.foregroundColor(Ink.secondary)
}
}
}
/// Placeholder shown when the chat panel is open but no task is selected.
struct TaskChatPanelPlaceholder: View {
@ObservedObject var coordinator: TaskChatCoordinator
let onClose: () -> Void
var body: some View {
VStack(spacing: 0) {
// Header
HStack(spacing: OmiSpacing.sm) {
Image(systemName: "bubble.left.and.bubble.right")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
Text("Task Chat")
.scaledFont(size: OmiType.body, weight: .semibold)
.foregroundColor(Ink.primary)
Spacer()
Button(action: onClose) {
Image(systemName: "xmark")
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
.frame(width: 20, height: 20)
}
.buttonStyle(.plain)
.help("Close chat panel")
}
.padding(.horizontal, OmiSpacing.md)
.padding(.vertical, OmiSpacing.sm)
.background(Ink.rowFillHover.opacity(0.5))
Divider()
.background(Ink.rowFillHover)
// Empty state
VStack(spacing: OmiSpacing.lg) {
Spacer()
Image(systemName: coordinator.errorMessage == nil ? "text.bubble" : "exclamationmark.triangle")
.scaledFont(size: 36)
.foregroundColor(Ink.secondary)
Text(coordinator.errorMessage == nil ? "Select a task to continue" : "Couldn’t open this work")
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.secondary)
Text(coordinator.errorMessage ?? "Choose Work on this with Omi when a task deserves ongoing context.")
.scaledFont(size: OmiType.caption)
.foregroundColor(Ink.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, OmiSpacing.xxl)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.background(Color.clear)
}
}
extension OmiAPI.EvidenceKind {
/// Human labels for evidence chips — never expose internal nouns like "workstream".
var userFacingLabel: String {
switch self {
case .conversation: return "Conversation"
case .memory_item: return "Memory"
case .workstream_event: return "Thread event"
case .artifact: return "Artifact"
case .chat_message: return "Chat"
case .local_screen: return "Screen"
case .external: return "Journal"
case ._unknown: return "Evidence"
}
}
}