@useavalon/caelence-agent 0.2.3-canary.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/desktop/src/App.tsx +9 -2919
  2. package/desktop/src/BubbleClip.tsx +1 -1
  3. package/desktop/src/ComposerAttach.tsx +2 -2
  4. package/desktop/src/DeskBoot.tsx +28 -0
  5. package/desktop/src/DeskChrome.tsx +360 -0
  6. package/desktop/src/DeskComposer.tsx +311 -0
  7. package/desktop/src/DeskSidebar.tsx +191 -0
  8. package/desktop/src/FilesPanel.tsx +104 -36
  9. package/desktop/src/FloatNotice.test.ts +12 -0
  10. package/desktop/src/FloatNotice.tsx +5 -2
  11. package/desktop/src/IntegrationsPanel.tsx +273 -0
  12. package/desktop/src/McpPanel.tsx +8 -8
  13. package/desktop/src/MemoryPanel.tsx +138 -0
  14. package/desktop/src/QueuePanel.tsx +123 -0
  15. package/desktop/src/SessionsPanel.tsx +174 -0
  16. package/desktop/src/SettingsPanel.tsx +181 -0
  17. package/desktop/src/SkillsPanel.tsx +28 -12
  18. package/desktop/src/TranscriptViews.tsx +637 -0
  19. package/desktop/src/api.ts +36 -0
  20. package/desktop/src/app.css +159 -11
  21. package/desktop/src/composer-files.ts +2 -2
  22. package/desktop/src/desk-keys.ts +45 -0
  23. package/desktop/src/desk-prefs.ts +29 -0
  24. package/desktop/src/desk-types.ts +92 -0
  25. package/desktop/src/desktop-turn.ts +271 -0
  26. package/desktop/src/queue.ts +1 -1
  27. package/desktop/src/session-search.test.ts +20 -0
  28. package/desktop/src/session-search.ts +13 -0
  29. package/desktop/src/session-select.test.ts +23 -0
  30. package/desktop/src/session-select.ts +32 -0
  31. package/desktop/src/useDeskDictation.ts +123 -0
  32. package/desktop/src/useDeskFiles.ts +180 -0
  33. package/desktop/src/useDeskNotice.ts +20 -0
  34. package/desktop/src/useDeskPicker.ts +189 -0
  35. package/desktop/src/useDeskPrefs.ts +30 -0
  36. package/desktop/src/useDeskRuntime.ts +109 -0
  37. package/desktop/src/useDeskSessions.ts +195 -0
  38. package/desktop/src/useDeskSlash.ts +31 -0
  39. package/desktop/src/useDeskTurns.ts +264 -0
  40. package/desktop/src/useDesktopApp.ts +286 -0
  41. package/desktop/src/user-display.ts +2 -2
  42. package/package.json +2 -2
  43. package/src/desktop/bridge.test.ts +13 -0
  44. package/src/desktop/bridge.ts +45 -19
  45. package/src/desktop/uploads.test.ts +6 -0
  46. package/src/desktop/uploads.ts +178 -66
@@ -137,8 +137,17 @@ body {
137
137
  flex-direction: column;
138
138
  }
139
139
 
140
+ .desk-sessions-head {
141
+ display: flex;
142
+ align-items: center;
143
+ gap: 4px;
144
+ margin: 0 0 4px;
145
+ padding: 0 0 0 4px;
146
+ }
147
+
140
148
  .desk-sessions h2 {
141
- margin: 0 4px 4px;
149
+ margin: 0;
150
+ flex: 1;
142
151
  font-family: var(--sans);
143
152
  font-size: 13px;
144
153
  font-weight: 600;
@@ -146,6 +155,30 @@ body {
146
155
  color: var(--text);
147
156
  }
148
157
 
158
+ .desk-sessions-more {
159
+ display: grid;
160
+ place-items: center;
161
+ flex: none;
162
+ width: 24px;
163
+ height: 24px;
164
+ padding: 0;
165
+ border: 0;
166
+ border-radius: var(--r-sm);
167
+ background: transparent;
168
+ color: var(--icon);
169
+ cursor: pointer;
170
+ }
171
+
172
+ .desk-sessions-more:hover,
173
+ .desk-sessions-more.is-on {
174
+ background: var(--card);
175
+ color: var(--icon-hover);
176
+ }
177
+
178
+ .desk-sessions-more svg {
179
+ display: block;
180
+ }
181
+
149
182
  .desk-side-empty {
150
183
  margin: 0 4px;
151
184
  color: var(--text-secondary);
@@ -341,10 +374,10 @@ body {
341
374
  min-width: 132px;
342
375
  padding: 4px 2px 6px 8px;
343
376
  overflow: hidden;
344
- border: 1px solid var(--danger-mark);
377
+ border: 1px solid var(--line);
345
378
  border-radius: var(--r-sm);
346
- background: var(--danger-bg);
347
- color: var(--danger);
379
+ background: var(--card);
380
+ color: var(--text);
348
381
  font: inherit;
349
382
  font-size: 12px;
350
383
  font-weight: 500;
@@ -352,6 +385,12 @@ body {
352
385
  pointer-events: auto;
353
386
  }
354
387
 
388
+ .desk-notice--error {
389
+ border-color: var(--danger-mark);
390
+ background: var(--danger-bg);
391
+ color: var(--danger);
392
+ }
393
+
355
394
  .desk-notice__label {
356
395
  flex: 1;
357
396
  min-width: 0;
@@ -368,11 +407,20 @@ body {
368
407
  border: 0;
369
408
  border-radius: var(--r-sm);
370
409
  background: transparent;
371
- color: var(--danger);
410
+ color: var(--icon);
372
411
  cursor: pointer;
373
412
  }
374
413
 
375
414
  .desk-notice__close:hover {
415
+ background: var(--bg-inset);
416
+ color: var(--text);
417
+ }
418
+
419
+ .desk-notice--error .desk-notice__close {
420
+ color: var(--danger);
421
+ }
422
+
423
+ .desk-notice--error .desk-notice__close:hover {
376
424
  background: color-mix(in srgb, var(--danger-mark) 22%, var(--danger-bg));
377
425
  color: var(--danger);
378
426
  }
@@ -383,6 +431,10 @@ body {
383
431
  bottom: 0;
384
432
  left: 0;
385
433
  height: 3px;
434
+ background: var(--bg-inset);
435
+ }
436
+
437
+ .desk-notice--error .desk-notice__timer {
386
438
  background: color-mix(in srgb, var(--danger-mark) 28%, var(--danger-bg));
387
439
  }
388
440
 
@@ -391,12 +443,16 @@ body {
391
443
  width: 100%;
392
444
  height: 100%;
393
445
  transform-origin: left center;
394
- background: var(--danger);
446
+ background: var(--measure);
395
447
  animation-name: desk-notice-drain;
396
448
  animation-timing-function: linear;
397
449
  animation-fill-mode: forwards;
398
450
  }
399
451
 
452
+ .desk-notice--error .desk-notice__bar {
453
+ background: var(--danger);
454
+ }
455
+
400
456
  @keyframes desk-notice-drain {
401
457
  from {
402
458
  transform: scaleX(1);
@@ -1080,11 +1136,6 @@ p.desk-line__caption {
1080
1136
  color: var(--danger);
1081
1137
  }
1082
1138
 
1083
- .desk-saved {
1084
- color: var(--done);
1085
- margin: 0;
1086
- }
1087
-
1088
1139
  .desk-thought {
1089
1140
  max-width: 52rem;
1090
1141
  margin: 0 0 10px;
@@ -2518,6 +2569,12 @@ p.desk-line__caption {
2518
2569
  box-shadow: 0 1px 2px color-mix(in srgb, var(--ink) 14%, transparent);
2519
2570
  }
2520
2571
 
2572
+ .desk-pane-tabs button.is-live {
2573
+ background: var(--measure);
2574
+ color: #fff;
2575
+ box-shadow: none;
2576
+ }
2577
+
2521
2578
  .desk-pane-tabs button:focus-visible {
2522
2579
  outline: 1.5px solid var(--measure);
2523
2580
  outline-offset: 1px;
@@ -2564,6 +2621,87 @@ p.desk-line__caption {
2564
2621
  overflow-wrap: anywhere;
2565
2622
  }
2566
2623
 
2624
+ .desk-session-search {
2625
+ display: grid;
2626
+ grid-template-columns: minmax(0, 1fr) auto;
2627
+ align-items: stretch;
2628
+ gap: 8px;
2629
+ }
2630
+
2631
+ .desk-session-search .desk-field {
2632
+ min-width: 0;
2633
+ width: auto;
2634
+ }
2635
+
2636
+ .desk-session-sort {
2637
+ display: inline-flex;
2638
+ align-items: center;
2639
+ justify-content: center;
2640
+ gap: 6px;
2641
+ flex: none;
2642
+ height: 36px;
2643
+ padding: 0 10px;
2644
+ border: 1px solid var(--line-2);
2645
+ border-radius: var(--r-md);
2646
+ background: var(--card);
2647
+ color: var(--text);
2648
+ font-family: var(--sans);
2649
+ font-size: 13px;
2650
+ font-weight: 600;
2651
+ cursor: pointer;
2652
+ }
2653
+
2654
+ .desk-session-sort:hover,
2655
+ .desk-session-sort.is-on {
2656
+ background: var(--bg-inset);
2657
+ }
2658
+
2659
+ .desk-session-sort svg {
2660
+ display: block;
2661
+ flex: none;
2662
+ }
2663
+
2664
+ .desk-session-sort-label {
2665
+ display: grid;
2666
+ justify-items: start;
2667
+ }
2668
+
2669
+ .desk-session-sort-label span {
2670
+ grid-area: 1 / 1;
2671
+ visibility: hidden;
2672
+ }
2673
+
2674
+ .desk-session-sort-label span.is-on {
2675
+ visibility: visible;
2676
+ }
2677
+
2678
+ .desk-session-toolbar {
2679
+ display: grid;
2680
+ grid-template-columns: 20px minmax(0, 1fr) auto;
2681
+ gap: 10px;
2682
+ align-items: center;
2683
+ }
2684
+
2685
+ .desk-session-toolbar-meta {
2686
+ min-width: 0;
2687
+ font-size: 13px;
2688
+ color: var(--text-secondary);
2689
+ }
2690
+
2691
+ .desk-session-check {
2692
+ display: grid;
2693
+ place-items: center;
2694
+ width: 20px;
2695
+ height: 20px;
2696
+ }
2697
+
2698
+ .desk-session-check input {
2699
+ margin: 0;
2700
+ width: 13px;
2701
+ height: 13px;
2702
+ accent-color: var(--measure);
2703
+ }
2704
+
2567
2705
  .desk-integration-list {
2568
2706
  list-style: none;
2569
2707
  margin: 0;
@@ -2653,12 +2791,22 @@ p.desk-line__caption {
2653
2791
  color: var(--done);
2654
2792
  }
2655
2793
 
2794
+ .desk-integration-block {
2795
+ display: flex;
2796
+ flex-direction: column;
2797
+ min-width: 0;
2798
+ }
2799
+
2656
2800
  .desk-integration-split {
2657
2801
  margin: 0;
2658
2802
  padding: 14px 0 8px;
2659
2803
  border-top: 1px solid var(--line);
2660
2804
  }
2661
2805
 
2806
+ .desk-integration-split:not(:has(em)) {
2807
+ padding-bottom: 0;
2808
+ }
2809
+
2662
2810
  .desk-integration-list + .desk-integration-split {
2663
2811
  border-top: 0;
2664
2812
  }
@@ -1,4 +1,4 @@
1
- import { blobToBase64 } from "./dictation.ts";
1
+ import { blobToBase64 } from "./dictation";
2
2
 
3
3
  export const MAX_COMPOSER_FILES = 8;
4
4
  export const MAX_COMPOSER_FILE_BYTES = 10 * 1024 * 1024;
@@ -81,7 +81,7 @@ export function createAttachment(file: File): ComposerAttachment {
81
81
  : undefined;
82
82
  const sourcePath = nativeFilePath(file);
83
83
  return {
84
- id: `${file.name}-${file.size}-${file.lastModified}-${Math.random().toString(36).slice(2, 8)}`,
84
+ id: `${file.name}-${file.size}-${file.lastModified}-${crypto.randomUUID()}`,
85
85
  name: file.name || "file",
86
86
  mime,
87
87
  size: file.size,
@@ -0,0 +1,45 @@
1
+ import type { Dispatch, SetStateAction } from "react";
2
+ import type { SlashCommand } from "./api";
3
+ import type { UiStatus } from "./desk-types";
4
+
5
+ export function slashMenuKey(
6
+ key: string,
7
+ slashOpen: boolean,
8
+ count: number,
9
+ ): "down" | "up" | "tab" | undefined {
10
+ if (!slashOpen || count === 0) return undefined;
11
+ if (key === "ArrowDown") return "down";
12
+ if (key === "ArrowUp") return "up";
13
+ if (key === "Tab") return "tab";
14
+ return undefined;
15
+ }
16
+
17
+ export function composerEscapeAction(
18
+ editing: boolean,
19
+ status: UiStatus,
20
+ ): "cancel-edit" | "stop" | undefined {
21
+ if (editing) return "cancel-edit";
22
+ if (status === "running" || status === "approval") return "stop";
23
+ return undefined;
24
+ }
25
+
26
+ export function applySlashComposerKey(
27
+ slashKey: "down" | "up" | "tab" | undefined,
28
+ slashItems: SlashCommand[],
29
+ slashIndex: number,
30
+ setSlashIndex: Dispatch<SetStateAction<number>>,
31
+ setDraft: Dispatch<SetStateAction<string>>,
32
+ ): boolean {
33
+ if (slashKey === "down") {
34
+ setSlashIndex((index) => (index + 1) % slashItems.length);
35
+ return true;
36
+ }
37
+ if (slashKey === "up") {
38
+ setSlashIndex((index) => (index - 1 + slashItems.length) % slashItems.length);
39
+ return true;
40
+ }
41
+ if (slashKey !== "tab") return false;
42
+ const selected = slashItems[slashIndex] ?? slashItems[0];
43
+ if (selected) setDraft(selected.arg ? `/${selected.name} ` : `/${selected.name}`);
44
+ return true;
45
+ }
@@ -0,0 +1,29 @@
1
+ import type { ThemeChoice } from "./desk-types";
2
+
3
+ export function readTheme(): ThemeChoice {
4
+ const value = localStorage.getItem("harness-theme");
5
+ return value === "light" || value === "dark" ? value : "system";
6
+ }
7
+
8
+ export function applyTheme(choice: ThemeChoice): void {
9
+ const root = document.documentElement;
10
+ if (choice === "system") delete root.dataset.theme;
11
+ else root.dataset.theme = choice;
12
+ localStorage.setItem("harness-theme", choice);
13
+ }
14
+
15
+ export function readSidebar(): boolean {
16
+ return localStorage.getItem("harness-sidebar") !== "off";
17
+ }
18
+
19
+ export function writeSidebar(open: boolean): void {
20
+ localStorage.setItem("harness-sidebar", open ? "on" : "off");
21
+ }
22
+
23
+ export function readMediaMeta(): boolean {
24
+ return localStorage.getItem("harness-media-meta") === "on";
25
+ }
26
+
27
+ export function writeMediaMeta(open: boolean): void {
28
+ localStorage.setItem("harness-media-meta", open ? "on" : "off");
29
+ }
@@ -0,0 +1,92 @@
1
+ import type { DesktopState, PickerItem, SlashPickerKind } from "./api";
2
+ import type { NoticeKind } from "./FloatNotice";
3
+
4
+ export type { NoticeKind };
5
+ export type ShowNotice = (text: string, kind?: NoticeKind) => void;
6
+
7
+ export type ThemeChoice = "system" | "light" | "dark";
8
+ export type UiStatus = "boot" | "idle" | "running" | "approval" | "picker";
9
+ export type MainView =
10
+ | "chat"
11
+ | "settings"
12
+ | "integrations"
13
+ | "skills"
14
+ | "files"
15
+ | "memory"
16
+ | "sessions";
17
+
18
+ export interface PickerState {
19
+ title: string;
20
+ kind: SlashPickerKind;
21
+ items: PickerItem[];
22
+ index: number;
23
+ }
24
+
25
+ export interface SessionMenuState {
26
+ id: string;
27
+ label: string;
28
+ x: number;
29
+ y: number;
30
+ }
31
+
32
+ export interface PendingApproval {
33
+ callId: string;
34
+ toolName: string;
35
+ input: Record<string, unknown>;
36
+ }
37
+
38
+ export interface MediaModelStatus {
39
+ kind: string;
40
+ label: string;
41
+ }
42
+
43
+ export function isUploadEditPending(pending: PendingApproval): boolean {
44
+ return (
45
+ (pending.toolName === "edit_file" || pending.toolName === "write_file") &&
46
+ typeof pending.input.originalPath === "string" &&
47
+ pending.input.originalPath.trim().length > 0
48
+ );
49
+ }
50
+
51
+ export function pickerSelectedId(kind: SlashPickerKind, current: DesktopState): string | undefined {
52
+ if (kind === "model") return current.modelId;
53
+ if (kind === "mode") return current.mode;
54
+ if (kind === "image") return current.imageModelId;
55
+ if (kind === "video") return current.videoModelId;
56
+ if (kind === "transcribe") return current.transcribeModelId;
57
+ return undefined;
58
+ }
59
+
60
+ export function mainViewTitle(view: MainView): string {
61
+ if (view === "settings") return "Settings";
62
+ if (view === "integrations") return "Integrations";
63
+ if (view === "skills") return "Skills";
64
+ if (view === "files") return "Files";
65
+ if (view === "memory") return "Memory";
66
+ if (view === "sessions") return "Sessions";
67
+ return "Chat";
68
+ }
69
+
70
+ export function titleCase(value: string): string {
71
+ return value ? `${value[0]?.toUpperCase() ?? ""}${value.slice(1)}` : value;
72
+ }
73
+
74
+ export function composerMediaModels(current: DesktopState): MediaModelStatus[] {
75
+ return [
76
+ current.imageModelLabel ? { kind: "Image", label: current.imageModelLabel } : null,
77
+ current.videoModelLabel ? { kind: "Clip", label: current.videoModelLabel } : null,
78
+ current.transcribeModelLabel ? { kind: "Speech", label: current.transcribeModelLabel } : null,
79
+ ].filter((part): part is MediaModelStatus => Boolean(part));
80
+ }
81
+
82
+ export function composePlaceholder(listening: boolean, status: UiStatus): string {
83
+ if (listening) return "Listening";
84
+ if (status === "running") return "Queue a follow-up";
85
+ return "Ask the project";
86
+ }
87
+
88
+ export function desktopCommandView(cmd: string): "skills" | "integrations" | undefined {
89
+ if (cmd === "skills" || cmd === "skill find") return "skills";
90
+ if (cmd === "integrations" || cmd === "integration") return "integrations";
91
+ return undefined;
92
+ }
@@ -0,0 +1,271 @@
1
+ import type { Dispatch, MutableRefObject, SetStateAction } from "react";
2
+ import { PICKS_WHEN_EMPTY, parseSlashLine } from "../../src/cli/slash";
3
+ import {
4
+ type AgentEvent,
5
+ type BridgeClient,
6
+ type DesktopState,
7
+ getState,
8
+ isAbortError,
9
+ postSession,
10
+ type SlashOutcome,
11
+ startTurn,
12
+ } from "./api";
13
+ import type { ComposerAttachment } from "./composer-files";
14
+ import { serializeComposerAttachments, toAttachmentPreviews } from "./composer-files";
15
+ import type { MainView, PendingApproval, PickerState, ShowNotice, UiStatus } from "./desk-types";
16
+ import { desktopCommandView, pickerSelectedId } from "./desk-types";
17
+ import { pickerStartIndex } from "./picker-nav";
18
+ import type { QueuedMessage } from "./queue";
19
+ import {
20
+ applyEvent,
21
+ linesFromTranscript,
22
+ nextUserTurnIndex,
23
+ type StreamLine,
24
+ truncateAfterUserTurn,
25
+ } from "./stream";
26
+
27
+ export function applyDesktopTurnEvent(
28
+ event: AgentEvent,
29
+ setState: Dispatch<SetStateAction<DesktopState | null>>,
30
+ setPending: Dispatch<SetStateAction<PendingApproval | null>>,
31
+ setStatus: Dispatch<SetStateAction<UiStatus>>,
32
+ setLines: Dispatch<SetStateAction<StreamLine[]>>,
33
+ ): boolean {
34
+ if (event.kind === "session_meta") {
35
+ setState((s) => (s ? { ...s, title: event.title } : s));
36
+ }
37
+ if (event.kind === "approval_request") {
38
+ setPending({
39
+ callId: event.callId,
40
+ toolName: event.toolName,
41
+ input: event.input,
42
+ });
43
+ setStatus("approval");
44
+ }
45
+ setLines((prev) => applyEvent(prev, event));
46
+ return event.kind === "completion" || event.kind === "error";
47
+ }
48
+
49
+ export function failRunningTools(prev: StreamLine[]): StreamLine[] {
50
+ return prev.map((line) =>
51
+ line.type === "tool" && line.status === "running"
52
+ ? {
53
+ ...line,
54
+ status: "fail" as const,
55
+ error: "Turn stopped before this tool finished.",
56
+ }
57
+ : line,
58
+ );
59
+ }
60
+
61
+ export interface DesktopOutcomeLoop {
62
+ bridge: BridgeClient | null;
63
+ pushSystem: (text: string) => void;
64
+ refreshSessions: (client: BridgeClient) => Promise<void>;
65
+ setState: Dispatch<SetStateAction<DesktopState | null>>;
66
+ setDraft: Dispatch<SetStateAction<string>>;
67
+ setLines: Dispatch<SetStateAction<StreamLine[]>>;
68
+ setPicker: Dispatch<SetStateAction<PickerState | null>>;
69
+ setStatus: Dispatch<SetStateAction<UiStatus>>;
70
+ }
71
+
72
+ export async function applyDesktopOutcome(
73
+ loop: DesktopOutcomeLoop,
74
+ outcome: SlashOutcome,
75
+ nextState?: DesktopState,
76
+ ): Promise<void> {
77
+ if (nextState) loop.setState(nextState);
78
+ if (outcome.kind === "applied" || outcome.kind === "hold") return;
79
+ if (outcome.kind === "incomplete") {
80
+ loop.setDraft(outcome.draft);
81
+ return;
82
+ }
83
+ if (outcome.kind === "text") {
84
+ loop.pushSystem(outcome.text);
85
+ return;
86
+ }
87
+ if (outcome.kind === "clear") {
88
+ loop.setLines([]);
89
+ loop.pushSystem("New session.");
90
+ if (loop.bridge) await loop.refreshSessions(loop.bridge);
91
+ return;
92
+ }
93
+ if (outcome.kind === "session") {
94
+ if (!loop.bridge) return;
95
+ const loaded = await postSession(loop.bridge, { id: outcome.session.id });
96
+ loop.setState(loaded.state);
97
+ loop.setLines(linesFromTranscript(loaded.messages ?? []));
98
+ loop.pushSystem(`Resumed ${loaded.state.title}`);
99
+ await loop.refreshSessions(loop.bridge);
100
+ return;
101
+ }
102
+ if (outcome.kind === "exit") {
103
+ const { getCurrentWindow } = await import("@tauri-apps/api/window");
104
+ await getCurrentWindow().close();
105
+ return;
106
+ }
107
+ if (outcome.kind === "media") {
108
+ loop.setLines((prev) => [
109
+ ...prev,
110
+ {
111
+ key: `media-${prev.length}-${Date.now()}`,
112
+ type: "media",
113
+ kind: outcome.media.kind,
114
+ src: outcome.media.dataUrl,
115
+ caption: outcome.caption,
116
+ mimeType: outcome.media.mimeType,
117
+ },
118
+ ]);
119
+ return;
120
+ }
121
+ const currentId = nextState ? pickerSelectedId(outcome.picker, nextState) : undefined;
122
+ loop.setPicker({
123
+ title: outcome.title,
124
+ kind: outcome.picker,
125
+ items: outcome.items,
126
+ index: pickerStartIndex(outcome.items, currentId),
127
+ });
128
+ loop.setStatus("picker");
129
+ }
130
+
131
+ export function isDesktopSlashLine(current: string, edit?: number): boolean {
132
+ if (edit !== undefined) return false;
133
+ return current.startsWith("/") || current === "help" || current === "--help" || current === "-h";
134
+ }
135
+
136
+ export function slashOutcomeStopsLoop(kind: string | undefined): boolean {
137
+ return kind === "picker" || kind === "exit" || kind === "hold" || kind === "incomplete";
138
+ }
139
+
140
+ export interface DesktopTurnLoop {
141
+ bridge: BridgeClient;
142
+ applyOutcome: (outcome: SlashOutcome, next?: DesktopState) => Promise<void> | void;
143
+ noticeError: (err: unknown) => void;
144
+ refreshSessions: (client: BridgeClient) => Promise<void>;
145
+ showNotice: ShowNotice;
146
+ setSkillQuery: Dispatch<SetStateAction<string>>;
147
+ setView: Dispatch<SetStateAction<MainView>>;
148
+ setDraft: Dispatch<SetStateAction<string>>;
149
+ setStatus: Dispatch<SetStateAction<UiStatus>>;
150
+ setLines: Dispatch<SetStateAction<StreamLine[]>>;
151
+ setState: Dispatch<SetStateAction<DesktopState | null>>;
152
+ setPending: Dispatch<SetStateAction<PendingApproval | null>>;
153
+ setQueue: Dispatch<SetStateAction<QueuedMessage[]>>;
154
+ skipDrainRef: MutableRefObject<boolean>;
155
+ queueRef: MutableRefObject<QueuedMessage[]>;
156
+ }
157
+
158
+ export async function runSlashDesktopTurn(
159
+ loop: DesktopTurnLoop,
160
+ current: string,
161
+ ): Promise<boolean> {
162
+ const { cmd, arg } = parseSlashLine(current);
163
+ const viewForCmd = desktopCommandView(cmd);
164
+ if (viewForCmd) {
165
+ if (viewForCmd === "skills") loop.setSkillQuery(cmd === "skill find" ? arg : "");
166
+ loop.setView(viewForCmd);
167
+ loop.setDraft("");
168
+ return false;
169
+ }
170
+ if (arg || !PICKS_WHEN_EMPTY.has(cmd)) loop.setStatus("running");
171
+ loop.setView("chat");
172
+ try {
173
+ const result = await startTurn(loop.bridge, current);
174
+ if (result?.outcome) await loop.applyOutcome(result.outcome, result.state);
175
+ return !slashOutcomeStopsLoop(result?.outcome?.kind);
176
+ } catch (err) {
177
+ if (!isAbortError(err)) loop.noticeError(err);
178
+ return true;
179
+ }
180
+ }
181
+
182
+ export async function runChatDesktopTurn(
183
+ loop: DesktopTurnLoop,
184
+ current: string,
185
+ edit: number | undefined,
186
+ pendingFiles: ComposerAttachment[],
187
+ ): Promise<void> {
188
+ loop.setView("chat");
189
+ if (edit !== undefined) {
190
+ loop.setLines((prev) => truncateAfterUserTurn(prev, edit, current));
191
+ } else {
192
+ const previews = toAttachmentPreviews(pendingFiles);
193
+ loop.setLines((prev) => [
194
+ ...prev,
195
+ {
196
+ key: `u-${Date.now()}`,
197
+ type: "user",
198
+ text: current,
199
+ userTurnIndex: nextUserTurnIndex(prev),
200
+ ...(previews.length > 0 ? { attachments: previews } : {}),
201
+ },
202
+ ]);
203
+ }
204
+ loop.setStatus("running");
205
+ try {
206
+ let finished = false;
207
+ const payload =
208
+ pendingFiles.length > 0 ? await serializeComposerAttachments(pendingFiles) : undefined;
209
+ await startTurn(
210
+ loop.bridge,
211
+ current,
212
+ (event: AgentEvent) => {
213
+ if (
214
+ applyDesktopTurnEvent(
215
+ event,
216
+ loop.setState,
217
+ loop.setPending,
218
+ loop.setStatus,
219
+ loop.setLines,
220
+ )
221
+ ) {
222
+ finished = true;
223
+ }
224
+ },
225
+ undefined,
226
+ edit,
227
+ payload,
228
+ );
229
+ if (!finished) {
230
+ loop.setLines(failRunningTools);
231
+ loop.showNotice(
232
+ "The turn stopped before the model finished. Send the prompt again.",
233
+ "error",
234
+ );
235
+ }
236
+ const next = await getState(loop.bridge);
237
+ loop.setState(next);
238
+ await loop.refreshSessions(loop.bridge);
239
+ } catch (err) {
240
+ if (!isAbortError(err)) loop.noticeError(err);
241
+ } finally {
242
+ loop.setPending(null);
243
+ }
244
+ }
245
+
246
+ export async function runDesktopTurnLoop(
247
+ loop: DesktopTurnLoop,
248
+ message: string,
249
+ editUserTurn: number | undefined,
250
+ files: ComposerAttachment[],
251
+ ): Promise<void> {
252
+ let current = message;
253
+ let edit = editUserTurn;
254
+ let pendingFiles = files;
255
+ while (true) {
256
+ if (isDesktopSlashLine(current, edit)) {
257
+ const keepGoing = await runSlashDesktopTurn(loop, current);
258
+ if (!keepGoing) break;
259
+ } else {
260
+ await runChatDesktopTurn(loop, current, edit, pendingFiles);
261
+ }
262
+ if (loop.skipDrainRef.current) break;
263
+ const queued = loop.queueRef.current[0];
264
+ if (!queued) break;
265
+ loop.queueRef.current = loop.queueRef.current.slice(1);
266
+ loop.setQueue(loop.queueRef.current);
267
+ current = queued.text;
268
+ pendingFiles = queued.attachments ?? [];
269
+ edit = undefined;
270
+ }
271
+ }