@useavalon/caelence-agent 0.2.1-canary.0 → 0.2.1
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.
- package/README.md +8 -5
- package/desktop/README.md +26 -0
- package/desktop/bun.lock +321 -0
- package/desktop/index.html +20 -0
- package/desktop/package.json +28 -0
- package/desktop/scripts/make-icons.ts +105 -0
- package/desktop/scripts/run-tauri.ts +40 -0
- package/desktop/src/App.tsx +2657 -0
- package/desktop/src/ComposerAttach.tsx +111 -0
- package/desktop/src/FloatNotice.test.ts +23 -0
- package/desktop/src/FloatNotice.tsx +30 -0
- package/desktop/src/LogoMark.tsx +37 -0
- package/desktop/src/SkillsPanel.tsx +409 -0
- package/desktop/src/api.ts +503 -0
- package/desktop/src/app.css +1749 -0
- package/desktop/src/composer-files.test.ts +46 -0
- package/desktop/src/composer-files.ts +135 -0
- package/desktop/src/dictation.test.ts +22 -0
- package/desktop/src/dictation.ts +59 -0
- package/desktop/src/integration-search.test.ts +13 -0
- package/desktop/src/integration-search.ts +8 -0
- package/desktop/src/main.tsx +43 -0
- package/desktop/src/picker-nav.test.ts +27 -0
- package/desktop/src/picker-nav.ts +26 -0
- package/desktop/src/queue.test.ts +37 -0
- package/desktop/src/queue.ts +40 -0
- package/desktop/src/rich-text.test.ts +85 -0
- package/desktop/src/rich-text.ts +193 -0
- package/desktop/src/session-menu.test.ts +19 -0
- package/desktop/src/session-menu.ts +12 -0
- package/desktop/src/skill-search.test.ts +37 -0
- package/desktop/src/skill-search.ts +25 -0
- package/desktop/src/stream.test.ts +93 -0
- package/desktop/src/stream.ts +197 -0
- package/desktop/src/styles/fonts/chivo-mono-latin-ext-wght-normal.woff2 +0 -0
- package/desktop/src/styles/fonts/chivo-mono-latin-wght-normal.ttf +0 -0
- package/desktop/src/styles/fonts/chivo-mono-latin-wght-normal.woff2 +0 -0
- package/desktop/src/styles/fonts/instrument-sans-latin-ext-wght-normal.woff2 +0 -0
- package/desktop/src/styles/fonts/instrument-sans-latin-wght-normal.ttf +0 -0
- package/desktop/src/styles/fonts/instrument-sans-latin-wght-normal.woff2 +0 -0
- package/desktop/src/styles/fonts.css +53 -0
- package/desktop/src/styles/primitives.css +600 -0
- package/desktop/src/styles/tokens-matte.css +354 -0
- package/desktop/src/thought.test.ts +96 -0
- package/desktop/src/thought.ts +87 -0
- package/desktop/src/tool-label.test.ts +17 -0
- package/desktop/src/tool-label.ts +70 -0
- package/desktop/src/vite-env.d.ts +15 -0
- package/desktop/src-tauri/Cargo.lock +4791 -0
- package/desktop/src-tauri/Cargo.toml +21 -0
- package/desktop/src-tauri/Entitlements.plist +8 -0
- package/desktop/src-tauri/Info.plist +12 -0
- package/desktop/src-tauri/build.rs +16 -0
- package/desktop/src-tauri/capabilities/default.json +15 -0
- package/desktop/src-tauri/icons/128x128.png +0 -0
- package/desktop/src-tauri/icons/16x16.png +0 -0
- package/desktop/src-tauri/icons/32x32.png +0 -0
- package/desktop/src-tauri/icons/henry.w@example.net +0 -0
- package/desktop/src-tauri/icons/icon.icns +0 -0
- package/desktop/src-tauri/icons/icon.ico +0 -0
- package/desktop/src-tauri/icons/icon.png +0 -0
- package/desktop/src-tauri/icons/icon.svg +7 -0
- package/desktop/src-tauri/permissions/bridge.toml +24 -0
- package/desktop/src-tauri/src/main.rs +312 -0
- package/desktop/src-tauri/src/mic.rs +216 -0
- package/desktop/src-tauri/tauri.conf.json +46 -0
- package/desktop/tsconfig.json +21 -0
- package/desktop/vite.config.ts +18 -0
- package/package.json +5 -4
- package/src/cli/main.test.ts +6 -0
- package/src/cli/main.ts +5 -2
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
export type ChatTranscriptLine =
|
|
2
|
+
| { kind: "user"; text: string }
|
|
3
|
+
| { kind: "assistant"; text: string }
|
|
4
|
+
| { kind: "thought"; text: string }
|
|
5
|
+
| {
|
|
6
|
+
kind: "tool";
|
|
7
|
+
name: string;
|
|
8
|
+
callId: string;
|
|
9
|
+
status: "ok" | "fail";
|
|
10
|
+
preview: string;
|
|
11
|
+
error?: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export interface DesktopState {
|
|
15
|
+
name: string;
|
|
16
|
+
cwd: string;
|
|
17
|
+
mode: string;
|
|
18
|
+
modelId: string;
|
|
19
|
+
modelLabel: string;
|
|
20
|
+
modelTarget?: string;
|
|
21
|
+
imageModelId?: string;
|
|
22
|
+
imageModelLabel?: string;
|
|
23
|
+
videoModelId?: string;
|
|
24
|
+
videoModelLabel?: string;
|
|
25
|
+
transcribeModelId?: string;
|
|
26
|
+
transcribeModelLabel?: string;
|
|
27
|
+
sessionId?: string;
|
|
28
|
+
title: string;
|
|
29
|
+
spend?: string;
|
|
30
|
+
skills: Array<{ name: string; source: string }>;
|
|
31
|
+
busy: boolean;
|
|
32
|
+
hasApiKey: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PickerItem {
|
|
36
|
+
id: string;
|
|
37
|
+
label: string;
|
|
38
|
+
hint?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SlashCommand {
|
|
42
|
+
name: string;
|
|
43
|
+
hint: string;
|
|
44
|
+
arg?: boolean;
|
|
45
|
+
slot?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type SlashPickerKind = "model" | "mode" | "resume" | "image" | "video" | "transcribe";
|
|
49
|
+
|
|
50
|
+
export type SlashOutcome =
|
|
51
|
+
| { kind: "text"; text: string }
|
|
52
|
+
| { kind: "applied" }
|
|
53
|
+
| { kind: "incomplete"; draft: string }
|
|
54
|
+
| { kind: "hold" }
|
|
55
|
+
| { kind: "clear" }
|
|
56
|
+
| {
|
|
57
|
+
kind: "session";
|
|
58
|
+
session: {
|
|
59
|
+
id: string;
|
|
60
|
+
title: string;
|
|
61
|
+
model?: string;
|
|
62
|
+
messages: Array<{ kind: string; role?: string; content?: string }>;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
| { kind: "picker"; title: string; picker: SlashPickerKind; items: PickerItem[] }
|
|
66
|
+
| {
|
|
67
|
+
kind: "media";
|
|
68
|
+
media: { kind: "image" | "video"; mimeType: string; dataUrl: string };
|
|
69
|
+
caption: string;
|
|
70
|
+
}
|
|
71
|
+
| { kind: "exit" };
|
|
72
|
+
|
|
73
|
+
export type AgentEvent =
|
|
74
|
+
| { kind: "text_delta"; text: string }
|
|
75
|
+
| { kind: "reasoning_delta"; text: string }
|
|
76
|
+
| { kind: "tool_call_start"; toolName: string; callId: string; input: Record<string, unknown> }
|
|
77
|
+
| {
|
|
78
|
+
kind: "tool_call_end";
|
|
79
|
+
toolName: string;
|
|
80
|
+
callId: string;
|
|
81
|
+
success: boolean;
|
|
82
|
+
result?: unknown;
|
|
83
|
+
error?: string;
|
|
84
|
+
}
|
|
85
|
+
| { kind: "approval_request"; callId: string; toolName: string; input: Record<string, unknown> }
|
|
86
|
+
| { kind: "session_meta"; sessionId: string; title: string }
|
|
87
|
+
| {
|
|
88
|
+
kind: "usage";
|
|
89
|
+
promptTokens: number;
|
|
90
|
+
completionTokens: number;
|
|
91
|
+
totalTokens: number;
|
|
92
|
+
costUsd?: number;
|
|
93
|
+
}
|
|
94
|
+
| { kind: "todos"; items: Array<{ id: string; content: string; status: string }> }
|
|
95
|
+
| { kind: "error"; message: string }
|
|
96
|
+
| { kind: "completion" };
|
|
97
|
+
|
|
98
|
+
export interface BridgeClient {
|
|
99
|
+
url: string;
|
|
100
|
+
token: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function resolveBridge(): Promise<BridgeClient> {
|
|
104
|
+
if (window.__HARNESS_BRIDGE && window.__HARNESS_TOKEN) {
|
|
105
|
+
return { url: window.__HARNESS_BRIDGE, token: window.__HARNESS_TOKEN };
|
|
106
|
+
}
|
|
107
|
+
const url = import.meta.env.VITE_HARNESS_BRIDGE;
|
|
108
|
+
const token = import.meta.env.VITE_HARNESS_TOKEN;
|
|
109
|
+
if (url && token) return { url, token };
|
|
110
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
111
|
+
return invoke<BridgeClient>("bridge_info");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function request<T>(bridge: BridgeClient, path: string, init: RequestInit = {}): Promise<T> {
|
|
115
|
+
const headers = new Headers(init.headers);
|
|
116
|
+
headers.set("authorization", `Bearer ${bridge.token}`);
|
|
117
|
+
if (init.body && !headers.has("content-type")) headers.set("content-type", "application/json");
|
|
118
|
+
const res = await fetch(`${bridge.url}${path}`, { ...init, headers });
|
|
119
|
+
const type = res.headers.get("content-type") ?? "";
|
|
120
|
+
if (type.includes("text/event-stream")) {
|
|
121
|
+
throw new Error("Expected JSON.");
|
|
122
|
+
}
|
|
123
|
+
const body = (await res.json()) as T & { error?: string };
|
|
124
|
+
if (!res.ok) throw new Error(body.error ?? `Caelence agent returned ${res.status}.`);
|
|
125
|
+
return body;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function requestBlob(bridge: BridgeClient, path: string, init: RequestInit): Promise<Blob> {
|
|
129
|
+
const headers = new Headers(init.headers);
|
|
130
|
+
headers.set("authorization", `Bearer ${bridge.token}`);
|
|
131
|
+
if (init.body && !headers.has("content-type")) headers.set("content-type", "application/json");
|
|
132
|
+
const res = await fetch(`${bridge.url}${path}`, { ...init, headers });
|
|
133
|
+
const type = res.headers.get("content-type") ?? "";
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
if (type.includes("application/json")) {
|
|
136
|
+
const body = (await res.json()) as { error?: string };
|
|
137
|
+
throw new Error(body.error ?? `Caelence agent returned ${res.status}.`);
|
|
138
|
+
}
|
|
139
|
+
throw new Error(`Caelence agent returned ${res.status}.`);
|
|
140
|
+
}
|
|
141
|
+
return res.blob();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function getState(bridge: BridgeClient): Promise<DesktopState> {
|
|
145
|
+
return request(bridge, "/state");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function getSessions(bridge: BridgeClient): Promise<{ items: PickerItem[] }> {
|
|
149
|
+
return request(bridge, "/sessions");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function getModels(bridge: BridgeClient): Promise<{ items: PickerItem[] }> {
|
|
153
|
+
return request(bridge, "/models");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface DesktopSettings {
|
|
157
|
+
hasApiKey: boolean;
|
|
158
|
+
apiKeyHint: string;
|
|
159
|
+
mode: string;
|
|
160
|
+
modelId: string;
|
|
161
|
+
modelLabel: string;
|
|
162
|
+
hasGoogleOAuth: boolean;
|
|
163
|
+
googleOAuthHint: string;
|
|
164
|
+
hasMicrosoftOAuth: boolean;
|
|
165
|
+
microsoftOAuthHint: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface PublicIntegration {
|
|
169
|
+
id: string;
|
|
170
|
+
label: string;
|
|
171
|
+
description: string;
|
|
172
|
+
docsUrl: string;
|
|
173
|
+
logoUrl: string;
|
|
174
|
+
logoKind: "mask" | "image";
|
|
175
|
+
glyph: string;
|
|
176
|
+
connected: boolean;
|
|
177
|
+
auth?: "dcr" | "google" | "microsoft" | "desktop";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function getSettings(bridge: BridgeClient): Promise<DesktopSettings> {
|
|
181
|
+
return request(bridge, "/settings");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function getIntegrations(bridge: BridgeClient): Promise<{ items: PublicIntegration[] }> {
|
|
185
|
+
return request(bridge, "/integrations");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface PublicSkill {
|
|
189
|
+
id: string;
|
|
190
|
+
name: string;
|
|
191
|
+
description: string;
|
|
192
|
+
origin: "bundled" | "skills.sh";
|
|
193
|
+
source: string;
|
|
194
|
+
installs?: number;
|
|
195
|
+
status: "off" | "user" | "project";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export interface SkillsPage {
|
|
199
|
+
bundled: PublicSkill[];
|
|
200
|
+
popular: PublicSkill[];
|
|
201
|
+
loaded: PublicSkill[];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function getSkills(bridge: BridgeClient): Promise<SkillsPage> {
|
|
205
|
+
return request(bridge, "/skills");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function getPopularSkills(bridge: BridgeClient): Promise<{ popular: PublicSkill[] }> {
|
|
209
|
+
return request(bridge, "/skills/popular");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function searchSkills(bridge: BridgeClient, q: string): Promise<{ items: PublicSkill[] }> {
|
|
213
|
+
return request(bridge, `/skills/search?q=${encodeURIComponent(q)}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function addSkill(bridge: BridgeClient, id: string): Promise<{ ok: boolean } & SkillsPage> {
|
|
217
|
+
return request(bridge, "/skills/add", {
|
|
218
|
+
method: "POST",
|
|
219
|
+
body: JSON.stringify({ id }),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function removeSkill(
|
|
224
|
+
bridge: BridgeClient,
|
|
225
|
+
id: string,
|
|
226
|
+
): Promise<{ ok: boolean } & SkillsPage> {
|
|
227
|
+
return request(bridge, "/skills/remove", {
|
|
228
|
+
method: "POST",
|
|
229
|
+
body: JSON.stringify({ id }),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function connectIntegration(
|
|
234
|
+
bridge: BridgeClient,
|
|
235
|
+
connectorId: string,
|
|
236
|
+
): Promise<{ ok: boolean; items: PublicIntegration[] }> {
|
|
237
|
+
return request(bridge, "/integrations/connect", {
|
|
238
|
+
method: "POST",
|
|
239
|
+
body: JSON.stringify({ connectorId }),
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function disconnectIntegration(
|
|
244
|
+
bridge: BridgeClient,
|
|
245
|
+
connectorId: string,
|
|
246
|
+
): Promise<{ ok: boolean; items: PublicIntegration[] }> {
|
|
247
|
+
return request(bridge, "/integrations/disconnect", {
|
|
248
|
+
method: "POST",
|
|
249
|
+
body: JSON.stringify({ connectorId }),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function openUrl(bridge: BridgeClient, url: string): Promise<{ ok: boolean }> {
|
|
254
|
+
return request(bridge, "/open", {
|
|
255
|
+
method: "POST",
|
|
256
|
+
body: JSON.stringify({ url }),
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function embedPreview(bridge: BridgeClient, src: string): Promise<Blob> {
|
|
261
|
+
return requestBlob(bridge, "/embed", {
|
|
262
|
+
method: "POST",
|
|
263
|
+
body: JSON.stringify({ url: src }),
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export async function openExternal(url: string, bridge?: BridgeClient | null): Promise<void> {
|
|
268
|
+
try {
|
|
269
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
270
|
+
await invoke("open_url", { url });
|
|
271
|
+
return;
|
|
272
|
+
} catch {
|
|
273
|
+
// Browser preview, or a binary that does not yet expose open_url.
|
|
274
|
+
}
|
|
275
|
+
if (bridge) {
|
|
276
|
+
await openUrl(bridge, url);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
const opened = window.open(url, "_blank", "noopener,noreferrer");
|
|
280
|
+
if (!opened) throw new Error("Could not open the link.");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function openPath(
|
|
284
|
+
bridge: BridgeClient,
|
|
285
|
+
path: string,
|
|
286
|
+
reveal = false,
|
|
287
|
+
): Promise<{ ok: boolean }> {
|
|
288
|
+
return request(bridge, "/open-path", {
|
|
289
|
+
method: "POST",
|
|
290
|
+
body: JSON.stringify({ path, reveal }),
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export async function openLocalFile(
|
|
295
|
+
path: string,
|
|
296
|
+
reveal = false,
|
|
297
|
+
bridge?: BridgeClient | null,
|
|
298
|
+
): Promise<void> {
|
|
299
|
+
try {
|
|
300
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
301
|
+
await invoke("open_path", { path, reveal });
|
|
302
|
+
return;
|
|
303
|
+
} catch {
|
|
304
|
+
// Browser preview, or a binary that does not yet expose open_path.
|
|
305
|
+
}
|
|
306
|
+
if (bridge) {
|
|
307
|
+
await openPath(bridge, path, reveal);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
throw new Error("Could not open the file.");
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export function saveSettings(
|
|
314
|
+
bridge: BridgeClient,
|
|
315
|
+
body: {
|
|
316
|
+
apiKey?: string;
|
|
317
|
+
mode?: string;
|
|
318
|
+
modelId?: string;
|
|
319
|
+
googleClientId?: string;
|
|
320
|
+
googleClientSecret?: string;
|
|
321
|
+
microsoftClientId?: string;
|
|
322
|
+
microsoftClientSecret?: string;
|
|
323
|
+
},
|
|
324
|
+
): Promise<
|
|
325
|
+
{
|
|
326
|
+
ok: boolean;
|
|
327
|
+
state: DesktopState;
|
|
328
|
+
} & DesktopSettings
|
|
329
|
+
> {
|
|
330
|
+
return request(bridge, "/settings", { method: "POST", body: JSON.stringify(body) });
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function getSlash(bridge: BridgeClient, q: string): Promise<{ items: SlashCommand[] }> {
|
|
334
|
+
return request(bridge, `/slash?q=${encodeURIComponent(q)}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function postSlash(
|
|
338
|
+
bridge: BridgeClient,
|
|
339
|
+
raw: string,
|
|
340
|
+
): Promise<{ outcome: SlashOutcome; state: DesktopState }> {
|
|
341
|
+
return request(bridge, "/slash", { method: "POST", body: JSON.stringify({ raw }) });
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function postPicker(
|
|
345
|
+
bridge: BridgeClient,
|
|
346
|
+
picker: SlashPickerKind,
|
|
347
|
+
id: string,
|
|
348
|
+
): Promise<{
|
|
349
|
+
ok: boolean;
|
|
350
|
+
text?: string;
|
|
351
|
+
state: DesktopState;
|
|
352
|
+
messages?: ChatTranscriptLine[];
|
|
353
|
+
}> {
|
|
354
|
+
return request(bridge, "/picker", { method: "POST", body: JSON.stringify({ picker, id }) });
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function postSession(
|
|
358
|
+
bridge: BridgeClient,
|
|
359
|
+
body: { id: string } | { clear: true },
|
|
360
|
+
): Promise<{
|
|
361
|
+
ok: boolean;
|
|
362
|
+
state: DesktopState;
|
|
363
|
+
messages?: ChatTranscriptLine[];
|
|
364
|
+
}> {
|
|
365
|
+
return request(bridge, "/session", { method: "POST", body: JSON.stringify(body) });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function renameSession(
|
|
369
|
+
bridge: BridgeClient,
|
|
370
|
+
id: string,
|
|
371
|
+
title: string,
|
|
372
|
+
): Promise<{ ok: boolean; title: string; state: DesktopState }> {
|
|
373
|
+
return request(bridge, "/session", { method: "POST", body: JSON.stringify({ id, title }) });
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function deleteSession(
|
|
377
|
+
bridge: BridgeClient,
|
|
378
|
+
id: string,
|
|
379
|
+
): Promise<{ ok: boolean; cleared: boolean; state: DesktopState }> {
|
|
380
|
+
return request(bridge, "/session", {
|
|
381
|
+
method: "POST",
|
|
382
|
+
body: JSON.stringify({ id, delete: true }),
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function approve(
|
|
387
|
+
bridge: BridgeClient,
|
|
388
|
+
body: { callId: string; decision: "yes" | "no" | "always"; toolName?: string },
|
|
389
|
+
): Promise<{ ok: boolean }> {
|
|
390
|
+
return request(bridge, "/approve", { method: "POST", body: JSON.stringify(body) });
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function abortTurn(bridge: BridgeClient): Promise<{ ok: boolean }> {
|
|
394
|
+
return request(bridge, "/abort", { method: "POST" });
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function transcribe(
|
|
398
|
+
bridge: BridgeClient,
|
|
399
|
+
body: { data: string; format: string },
|
|
400
|
+
): Promise<{ text: string }> {
|
|
401
|
+
return request(bridge, "/transcribe", { method: "POST", body: JSON.stringify(body) });
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export async function micStart(): Promise<void> {
|
|
405
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
406
|
+
await invoke("mic_start");
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export async function micStop(): Promise<{ data: string; format: string }> {
|
|
410
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
411
|
+
return invoke("mic_stop");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export async function filesFromDroppedPaths(paths: string[]): Promise<File[]> {
|
|
415
|
+
const { invoke } = await import("@tauri-apps/api/core");
|
|
416
|
+
const files: File[] = [];
|
|
417
|
+
for (const path of paths) {
|
|
418
|
+
const trimmed = path.trim();
|
|
419
|
+
if (!trimmed) continue;
|
|
420
|
+
try {
|
|
421
|
+
const item = await invoke<{ name: string; mime: string; data: string }>("read_drop_file", {
|
|
422
|
+
path: trimmed,
|
|
423
|
+
});
|
|
424
|
+
const raw = atob(item.data);
|
|
425
|
+
const bytes = Uint8Array.from(raw, (ch) => ch.codePointAt(0) ?? 0);
|
|
426
|
+
files.push(new File([bytes], item.name, { type: item.mime }));
|
|
427
|
+
} catch {
|
|
428
|
+
// skip unreadable paths
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
return files;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export interface TurnAttachmentPayload {
|
|
435
|
+
name: string;
|
|
436
|
+
mime: string;
|
|
437
|
+
data: string;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export async function startTurn(
|
|
441
|
+
bridge: BridgeClient,
|
|
442
|
+
message: string,
|
|
443
|
+
onEvent?: (event: AgentEvent) => void,
|
|
444
|
+
signal?: AbortSignal,
|
|
445
|
+
editUserTurn?: number,
|
|
446
|
+
attachments?: TurnAttachmentPayload[],
|
|
447
|
+
): Promise<{ outcome?: SlashOutcome; state?: DesktopState } | void> {
|
|
448
|
+
const res = await fetch(`${bridge.url}/turn`, {
|
|
449
|
+
method: "POST",
|
|
450
|
+
headers: {
|
|
451
|
+
authorization: `Bearer ${bridge.token}`,
|
|
452
|
+
"content-type": "application/json",
|
|
453
|
+
},
|
|
454
|
+
body: JSON.stringify({
|
|
455
|
+
message,
|
|
456
|
+
...(editUserTurn !== undefined ? { editUserTurn } : {}),
|
|
457
|
+
...(attachments && attachments.length > 0 ? { attachments } : {}),
|
|
458
|
+
}),
|
|
459
|
+
signal,
|
|
460
|
+
});
|
|
461
|
+
const type = res.headers.get("content-type") ?? "";
|
|
462
|
+
if (!res.ok) {
|
|
463
|
+
const body = (await res.json()) as { error?: string };
|
|
464
|
+
throw new Error(body.error ?? `Caelence agent returned ${res.status}.`);
|
|
465
|
+
}
|
|
466
|
+
if (!type.includes("text/event-stream")) {
|
|
467
|
+
return (await res.json()) as { outcome?: SlashOutcome; state?: DesktopState };
|
|
468
|
+
}
|
|
469
|
+
if (!res.body) return;
|
|
470
|
+
await readSse(res.body, onEvent);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function readSse(
|
|
474
|
+
body: ReadableStream<Uint8Array>,
|
|
475
|
+
onEvent?: (event: AgentEvent) => void,
|
|
476
|
+
): Promise<void> {
|
|
477
|
+
const reader = body.getReader();
|
|
478
|
+
const decoder = new TextDecoder();
|
|
479
|
+
let buffer = "";
|
|
480
|
+
while (true) {
|
|
481
|
+
const { done, value } = await reader.read();
|
|
482
|
+
if (done) break;
|
|
483
|
+
buffer += decoder.decode(value, { stream: true });
|
|
484
|
+
const chunks = buffer.split("\n\n");
|
|
485
|
+
buffer = chunks.pop() ?? "";
|
|
486
|
+
for (const chunk of chunks) {
|
|
487
|
+
const line = chunk.split("\n").find((row) => row.startsWith("data:"));
|
|
488
|
+
if (!line) continue;
|
|
489
|
+
const raw = line.slice(5).trim();
|
|
490
|
+
if (!raw) continue;
|
|
491
|
+
onEvent?.(JSON.parse(raw) as AgentEvent);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export function isAbortError(err: unknown): boolean {
|
|
497
|
+
if (!err || typeof err !== "object") return false;
|
|
498
|
+
const name = (err as { name?: unknown }).name;
|
|
499
|
+
if (name === "AbortError" || name === "CancelledError") return true;
|
|
500
|
+
if (err instanceof Error) return /aborted|The operation was aborted/i.test(err.message);
|
|
501
|
+
if (typeof err === "string") return /aborted|The operation was aborted/i.test(err);
|
|
502
|
+
return false;
|
|
503
|
+
}
|