@useavalon/caelence-agent 0.2.3-canary.0 → 0.2.3-canary.2
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 +7 -0
- package/desktop/src/App.tsx +9 -2919
- package/desktop/src/BubbleClip.tsx +1 -1
- package/desktop/src/ComposerAttach.tsx +2 -2
- package/desktop/src/DeskBoot.tsx +28 -0
- package/desktop/src/DeskChrome.tsx +360 -0
- package/desktop/src/DeskComposer.tsx +311 -0
- package/desktop/src/DeskSidebar.tsx +191 -0
- package/desktop/src/FilesPanel.tsx +104 -36
- package/desktop/src/FloatNotice.test.ts +12 -0
- package/desktop/src/FloatNotice.tsx +5 -2
- package/desktop/src/IntegrationsPanel.tsx +273 -0
- package/desktop/src/McpPanel.tsx +8 -8
- package/desktop/src/MemoryPanel.tsx +138 -0
- package/desktop/src/QueuePanel.tsx +123 -0
- package/desktop/src/SessionsPanel.tsx +174 -0
- package/desktop/src/SettingsPanel.tsx +181 -0
- package/desktop/src/SkillsPanel.tsx +28 -12
- package/desktop/src/TranscriptViews.tsx +637 -0
- package/desktop/src/api.ts +36 -0
- package/desktop/src/app.css +159 -11
- package/desktop/src/composer-files.ts +2 -2
- package/desktop/src/desk-keys.ts +45 -0
- package/desktop/src/desk-prefs.ts +29 -0
- package/desktop/src/desk-types.ts +92 -0
- package/desktop/src/desktop-turn.ts +271 -0
- package/desktop/src/preview-render.ts +322 -36
- package/desktop/src/queue.ts +1 -1
- package/desktop/src/session-search.test.ts +20 -0
- package/desktop/src/session-search.ts +13 -0
- package/desktop/src/session-select.test.ts +23 -0
- package/desktop/src/session-select.ts +32 -0
- package/desktop/src/useDeskDictation.ts +123 -0
- package/desktop/src/useDeskFiles.ts +180 -0
- package/desktop/src/useDeskNotice.ts +20 -0
- package/desktop/src/useDeskPicker.ts +189 -0
- package/desktop/src/useDeskPrefs.ts +30 -0
- package/desktop/src/useDeskRuntime.ts +109 -0
- package/desktop/src/useDeskSessions.ts +195 -0
- package/desktop/src/useDeskSlash.ts +31 -0
- package/desktop/src/useDeskTurns.ts +264 -0
- package/desktop/src/useDesktopApp.ts +286 -0
- package/desktop/src/user-display.ts +2 -2
- package/package.json +2 -2
- package/src/cli/openrouter-live.ts +30 -21
- package/src/cli/slash-dispatch.test.ts +25 -0
- package/src/cli/slash-dispatch.ts +41 -0
- package/src/cli/slash.ts +1 -0
- package/src/config.ts +10 -1
- package/src/core/run-turn.test.ts +3 -0
- package/src/core/run-turn.ts +74 -41
- package/src/desktop/bridge.test.ts +43 -0
- package/src/desktop/bridge.ts +91 -19
- package/src/desktop/uploads.test.ts +6 -0
- package/src/desktop/uploads.ts +178 -66
- package/src/index.ts +13 -0
- package/src/integrations/remote-mcp.ts +51 -21
- package/src/memory/extract.test.ts +81 -0
- package/src/memory/extract.ts +88 -0
- package/src/memory/prefs.test.ts +53 -0
- package/src/memory/prefs.ts +47 -0
- package/src/memory/prompt.ts +24 -0
- package/src/memory/store.test.ts +191 -0
- package/src/memory/store.ts +382 -0
- package/src/runtime.test.ts +37 -0
- package/src/runtime.ts +29 -1
- package/src/tools/local.ts +86 -39
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { looksLikeWebUrl } from "./rich-text
|
|
1
|
+
import { looksLikeWebUrl } from "./rich-text";
|
|
2
2
|
|
|
3
3
|
const VOID_TAGS = new Set([
|
|
4
4
|
"area",
|
|
@@ -87,27 +87,23 @@ export function rewritePreviewAssets(
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
const JS_URL_ATTRS = ["href", "src", "srcset", "xlink:href"] as const;
|
|
91
|
+
|
|
90
92
|
export function sanitizePreviewHtml(html: string): string {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
let out = stripEnclosedTag(html, "script");
|
|
94
|
+
out = stripEnclosedTag(out, "style");
|
|
95
|
+
for (const tag of ["iframe", "object", "embed", "link", "meta"]) {
|
|
96
|
+
out = stripOpenTag(out, tag);
|
|
97
|
+
}
|
|
98
|
+
return stripJavascriptUrls(stripEventHandlers(out));
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
export function renderMarkdown(src: string): string {
|
|
100
102
|
const fences: string[] = [];
|
|
101
103
|
const htmlBlocks: string[] = [];
|
|
102
|
-
let text = src.
|
|
103
|
-
text = text.replace(/^```([\w-]*)\n([\s\S]*?)^```/gm, (_all, _lang: string, code: string) => {
|
|
104
|
-
const id = fences.length;
|
|
105
|
-
fences.push(`<pre><code>${escapeHtml(code.replace(/\n$/, ""))}</code></pre>`);
|
|
106
|
-
return `\n%%FENCE${id}%%\n`;
|
|
107
|
-
});
|
|
104
|
+
let text = extractFences(src.replaceAll("\r\n", "\n"), fences);
|
|
108
105
|
text = extractHtmlBlocks(text, htmlBlocks);
|
|
109
|
-
return text
|
|
110
|
-
.split(/\n{2,}/)
|
|
106
|
+
return splitBlocks(text)
|
|
111
107
|
.map((block) => {
|
|
112
108
|
const trimmed = block.trim();
|
|
113
109
|
if (!trimmed) return "";
|
|
@@ -135,23 +131,17 @@ function extractHtmlBlocks(src: string, store: string[]): string {
|
|
|
135
131
|
const tag = start?.[1]?.toLowerCase();
|
|
136
132
|
if (tag && !INLINE_TAGS.has(tag)) {
|
|
137
133
|
const chunk: string[] = [];
|
|
138
|
-
if (
|
|
139
|
-
VOID_TAGS.has(tag) ||
|
|
140
|
-
/\/>\s*$/.test(line) ||
|
|
141
|
-
new RegExp(`</${tag}\\s*>`, "i").test(line)
|
|
142
|
-
) {
|
|
134
|
+
if (VOID_TAGS.has(tag) || lineEndsWithSelfClose(line) || lineHasCloseTag(line, tag)) {
|
|
143
135
|
chunk.push(line);
|
|
144
136
|
i += 1;
|
|
145
137
|
} else {
|
|
146
138
|
let depth = 1;
|
|
147
139
|
chunk.push(line);
|
|
148
140
|
i += 1;
|
|
149
|
-
const openRe = new RegExp(`<${tag}\\b`, "gi");
|
|
150
|
-
const closeRe = new RegExp(`</${tag}\\s*>`, "gi");
|
|
151
141
|
while (i < lines.length && depth > 0) {
|
|
152
142
|
const next = lines[i] ?? "";
|
|
153
|
-
depth += (next
|
|
154
|
-
depth -= (next
|
|
143
|
+
depth += countTagOpens(next, tag);
|
|
144
|
+
depth -= countTagCloses(next, tag);
|
|
155
145
|
chunk.push(next);
|
|
156
146
|
i += 1;
|
|
157
147
|
}
|
|
@@ -168,12 +158,9 @@ function extractHtmlBlocks(src: string, store: string[]): string {
|
|
|
168
158
|
}
|
|
169
159
|
|
|
170
160
|
function renderBlock(block: string): string {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const level = match[1]?.length ?? 1;
|
|
175
|
-
return `<h${level}>${renderInline(match[2] ?? "")}</h${level}>`;
|
|
176
|
-
}
|
|
161
|
+
const heading = parseHeading(block);
|
|
162
|
+
if (heading) {
|
|
163
|
+
return `<h${heading.level}>${renderInline(heading.body)}</h${heading.level}>`;
|
|
177
164
|
}
|
|
178
165
|
if (/^[-*_]{3,}$/.test(block)) return "<hr />";
|
|
179
166
|
if (
|
|
@@ -241,14 +228,313 @@ function renderInline(src: string): string {
|
|
|
241
228
|
codes.push(`<code>${code}</code>`);
|
|
242
229
|
return `%%CODE${id}%%`;
|
|
243
230
|
});
|
|
244
|
-
text = text
|
|
245
|
-
|
|
246
|
-
});
|
|
247
|
-
text = text.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_all, label: string, href: string) => {
|
|
248
|
-
return `<a href="${href}">${label}</a>`;
|
|
249
|
-
});
|
|
231
|
+
text = rewriteMdMedia(text, true);
|
|
232
|
+
text = rewriteMdMedia(text, false);
|
|
250
233
|
text = text.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
|
251
234
|
text = text.replace(/__([^_]+)__/g, "<strong>$1</strong>");
|
|
252
235
|
text = text.replace(/(^|[^\w*])\*([^*\n]+)\*(?!\*)/g, "$1<em>$2</em>");
|
|
253
236
|
return text.replace(/%%CODE(\d+)%%/g, (_all, id: string) => codes[Number(id)] ?? "");
|
|
254
237
|
}
|
|
238
|
+
|
|
239
|
+
function isSpace(ch: string | undefined): boolean {
|
|
240
|
+
return ch === " " || ch === "\t" || ch === "\n" || ch === "\r";
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function codeAt(ch: string | undefined): number {
|
|
244
|
+
return ch?.codePointAt(0) ?? -1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isWordChar(ch: string | undefined): boolean {
|
|
248
|
+
const code = codeAt(ch);
|
|
249
|
+
return (
|
|
250
|
+
(code >= 48 && code <= 57) ||
|
|
251
|
+
(code >= 65 && code <= 90) ||
|
|
252
|
+
(code >= 97 && code <= 122) ||
|
|
253
|
+
code === 95
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function isAlpha(ch: string | undefined): boolean {
|
|
258
|
+
const code = codeAt(ch);
|
|
259
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function skipSpaces(text: string, i: number): number {
|
|
263
|
+
while (i < text.length && isSpace(text[i])) i += 1;
|
|
264
|
+
return i;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function skipQuoted(text: string, i: number, quote: string): number {
|
|
268
|
+
let p = i + 1;
|
|
269
|
+
while (p < text.length && text[p] !== quote) p += 1;
|
|
270
|
+
return p < text.length ? p + 1 : p;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function skipUnquotedValue(text: string, i: number): number {
|
|
274
|
+
let p = i;
|
|
275
|
+
while (p < text.length && !isSpace(text[p]) && text[p] !== ">") p += 1;
|
|
276
|
+
return p;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function findTagOpen(html: string, tag: string, from: number): number {
|
|
280
|
+
const needle = `<${tag}`;
|
|
281
|
+
const lower = html.toLowerCase();
|
|
282
|
+
let start = from;
|
|
283
|
+
while (start < html.length) {
|
|
284
|
+
const i = lower.indexOf(needle, start);
|
|
285
|
+
if (i < 0) return -1;
|
|
286
|
+
if (!isWordChar(html[i + needle.length])) return i;
|
|
287
|
+
start = i + 1;
|
|
288
|
+
}
|
|
289
|
+
return -1;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function stripEnclosedTag(html: string, tag: string): string {
|
|
293
|
+
let out = html;
|
|
294
|
+
while (true) {
|
|
295
|
+
const start = findTagOpen(out, tag, 0);
|
|
296
|
+
if (start < 0) return out;
|
|
297
|
+
const closeNeedle = `</${tag}`;
|
|
298
|
+
const close = out.toLowerCase().indexOf(closeNeedle, start + tag.length + 1);
|
|
299
|
+
if (close < 0) return out.slice(0, start);
|
|
300
|
+
const gt = out.indexOf(">", close + closeNeedle.length);
|
|
301
|
+
if (gt < 0) return out.slice(0, start);
|
|
302
|
+
out = out.slice(0, start) + out.slice(gt + 1);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function stripOpenTag(html: string, tag: string): string {
|
|
307
|
+
let out = html;
|
|
308
|
+
while (true) {
|
|
309
|
+
const start = findTagOpen(out, tag, 0);
|
|
310
|
+
if (start < 0) return out;
|
|
311
|
+
const gt = out.indexOf(">", start);
|
|
312
|
+
if (gt < 0) return out.slice(0, start);
|
|
313
|
+
out = out.slice(0, start) + out.slice(gt + 1);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function eventHandlerEnd(html: string, spaceStart: number): number | null {
|
|
318
|
+
const nameStart = skipSpaces(html, spaceStart);
|
|
319
|
+
const first = html[nameStart];
|
|
320
|
+
const second = html[nameStart + 1];
|
|
321
|
+
if ((first !== "o" && first !== "O") || (second !== "n" && second !== "N")) return null;
|
|
322
|
+
let k = nameStart + 2;
|
|
323
|
+
while (k < html.length && isAlpha(html[k])) k += 1;
|
|
324
|
+
if (k === nameStart + 2) return null;
|
|
325
|
+
let p = skipSpaces(html, k);
|
|
326
|
+
if (html[p] !== "=") return null;
|
|
327
|
+
p = skipSpaces(html, p + 1);
|
|
328
|
+
const quote = html[p];
|
|
329
|
+
if (quote === '"' || quote === "'") return skipQuoted(html, p, quote);
|
|
330
|
+
return skipUnquotedValue(html, p);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function stripEventHandlers(html: string): string {
|
|
334
|
+
let out = "";
|
|
335
|
+
let i = 0;
|
|
336
|
+
while (i < html.length) {
|
|
337
|
+
const end = isSpace(html[i]) ? eventHandlerEnd(html, i) : null;
|
|
338
|
+
if (end !== null) {
|
|
339
|
+
i = end;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
out += html[i];
|
|
343
|
+
i += 1;
|
|
344
|
+
}
|
|
345
|
+
return out;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function attrNameAt(html: string, afterSpace: number): string | undefined {
|
|
349
|
+
const rest = html.slice(afterSpace).toLowerCase();
|
|
350
|
+
return JS_URL_ATTRS.find((name) => {
|
|
351
|
+
if (!rest.startsWith(name)) return false;
|
|
352
|
+
const next = rest[name.length];
|
|
353
|
+
return !next || next === "=" || isSpace(next);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function javascriptUrlEnd(html: string, spaceStart: number): number | null {
|
|
358
|
+
const attr = attrNameAt(html, spaceStart + 1);
|
|
359
|
+
if (!attr) return null;
|
|
360
|
+
let p = skipSpaces(html, spaceStart + 1 + attr.length);
|
|
361
|
+
if (html[p] !== "=") return null;
|
|
362
|
+
p = skipSpaces(html, p + 1);
|
|
363
|
+
const quote = html[p];
|
|
364
|
+
if (quote !== '"' && quote !== "'") return null;
|
|
365
|
+
const valueStart = skipSpaces(html, p + 1);
|
|
366
|
+
if (html.slice(valueStart, valueStart + 11).toLowerCase() !== "javascript:") return null;
|
|
367
|
+
return skipQuoted(html, p, quote);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function stripJavascriptUrls(html: string): string {
|
|
371
|
+
let out = "";
|
|
372
|
+
let i = 0;
|
|
373
|
+
while (i < html.length) {
|
|
374
|
+
const end = isSpace(html[i]) ? javascriptUrlEnd(html, i) : null;
|
|
375
|
+
if (end !== null) {
|
|
376
|
+
i = end;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
out += html[i];
|
|
380
|
+
i += 1;
|
|
381
|
+
}
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function extractFences(src: string, fences: string[]): string {
|
|
386
|
+
const lines = src.split("\n");
|
|
387
|
+
const out: string[] = [];
|
|
388
|
+
let i = 0;
|
|
389
|
+
while (i < lines.length) {
|
|
390
|
+
const line = lines[i] ?? "";
|
|
391
|
+
if (line.startsWith("```")) {
|
|
392
|
+
const chunk: string[] = [];
|
|
393
|
+
let j = i + 1;
|
|
394
|
+
while (j < lines.length && !(lines[j] ?? "").startsWith("```")) {
|
|
395
|
+
chunk.push(lines[j] ?? "");
|
|
396
|
+
j += 1;
|
|
397
|
+
}
|
|
398
|
+
if (j < lines.length) {
|
|
399
|
+
const id = fences.length;
|
|
400
|
+
fences.push(`<pre><code>${escapeHtml(chunk.join("\n"))}</code></pre>`);
|
|
401
|
+
out.push("", `%%FENCE${id}%%`, "");
|
|
402
|
+
i = j + 1;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
out.push(line);
|
|
407
|
+
i += 1;
|
|
408
|
+
}
|
|
409
|
+
return out.join("\n");
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function splitBlocks(text: string): string[] {
|
|
413
|
+
const blocks: string[] = [];
|
|
414
|
+
let start = 0;
|
|
415
|
+
let i = 0;
|
|
416
|
+
while (i < text.length) {
|
|
417
|
+
if (text[i] === "\n" && text[i + 1] === "\n") {
|
|
418
|
+
blocks.push(text.slice(start, i));
|
|
419
|
+
while (text[i] === "\n") i += 1;
|
|
420
|
+
start = i;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
i += 1;
|
|
424
|
+
}
|
|
425
|
+
blocks.push(text.slice(start));
|
|
426
|
+
return blocks;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function lineEndsWithSelfClose(line: string): boolean {
|
|
430
|
+
let i = line.length - 1;
|
|
431
|
+
while (i >= 0 && isSpace(line[i])) i -= 1;
|
|
432
|
+
return i >= 1 && line[i] === ">" && line[i - 1] === "/";
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function lineHasCloseTag(line: string, tag: string): boolean {
|
|
436
|
+
const needle = `</${tag}`;
|
|
437
|
+
const lower = line.toLowerCase();
|
|
438
|
+
let from = 0;
|
|
439
|
+
while (from < line.length) {
|
|
440
|
+
const i = lower.indexOf(needle, from);
|
|
441
|
+
if (i < 0) return false;
|
|
442
|
+
let p = i + needle.length;
|
|
443
|
+
while (p < line.length && isSpace(line[p])) p += 1;
|
|
444
|
+
if (line[p] === ">") return true;
|
|
445
|
+
from = i + 1;
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function countTagOpens(line: string, tag: string): number {
|
|
451
|
+
const needle = `<${tag}`;
|
|
452
|
+
const lower = line.toLowerCase();
|
|
453
|
+
let n = 0;
|
|
454
|
+
let from = 0;
|
|
455
|
+
while (from < line.length) {
|
|
456
|
+
const i = lower.indexOf(needle, from);
|
|
457
|
+
if (i < 0) return n;
|
|
458
|
+
if (!isWordChar(line[i + needle.length])) n += 1;
|
|
459
|
+
from = i + 1;
|
|
460
|
+
}
|
|
461
|
+
return n;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function countTagCloses(line: string, tag: string): number {
|
|
465
|
+
const needle = `</${tag}`;
|
|
466
|
+
const lower = line.toLowerCase();
|
|
467
|
+
let n = 0;
|
|
468
|
+
let from = 0;
|
|
469
|
+
while (from < line.length) {
|
|
470
|
+
const i = lower.indexOf(needle, from);
|
|
471
|
+
if (i < 0) return n;
|
|
472
|
+
let p = i + needle.length;
|
|
473
|
+
while (p < line.length && isSpace(line[p])) p += 1;
|
|
474
|
+
if (line[p] === ">") n += 1;
|
|
475
|
+
from = i + 1;
|
|
476
|
+
}
|
|
477
|
+
return n;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function parseHeading(block: string): { level: number; body: string } | null {
|
|
481
|
+
let level = 0;
|
|
482
|
+
while (level < 6 && block[level] === "#") level += 1;
|
|
483
|
+
if (level === 0) return null;
|
|
484
|
+
let i = level;
|
|
485
|
+
if (!isSpace(block[i])) return null;
|
|
486
|
+
while (isSpace(block[i])) i += 1;
|
|
487
|
+
const body = block.slice(i);
|
|
488
|
+
if (!body) return null;
|
|
489
|
+
return { level, body };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function hrefEndAt(text: string, start: number): number | null {
|
|
493
|
+
let i = start;
|
|
494
|
+
while (i < text.length && text[i] !== ")") {
|
|
495
|
+
const ch = text[i];
|
|
496
|
+
if (ch === " " || ch === "\t" || ch === "\n") return null;
|
|
497
|
+
i += 1;
|
|
498
|
+
}
|
|
499
|
+
return text[i] === ")" ? i : null;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function mediaAt(
|
|
503
|
+
text: string,
|
|
504
|
+
start: number,
|
|
505
|
+
image: boolean,
|
|
506
|
+
): { html: string; next: number } | null {
|
|
507
|
+
const labelStart = start + (image ? 2 : 1);
|
|
508
|
+
const labelEnd = text.indexOf("]", labelStart);
|
|
509
|
+
if (labelEnd < 0 || text[labelEnd + 1] !== "(") return null;
|
|
510
|
+
const hrefEnd = hrefEndAt(text, labelEnd + 2);
|
|
511
|
+
if (hrefEnd === null) return null;
|
|
512
|
+
const label = text.slice(labelStart, labelEnd);
|
|
513
|
+
if (!image && !label) return null;
|
|
514
|
+
const href = text.slice(labelEnd + 2, hrefEnd);
|
|
515
|
+
const html = image ? `<img src="${href}" alt="${label}" />` : `<a href="${href}">${label}</a>`;
|
|
516
|
+
return { html, next: hrefEnd + 1 };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function rewriteMdMedia(text: string, image: boolean): string {
|
|
520
|
+
const marker = image ? "![" : "[";
|
|
521
|
+
let out = "";
|
|
522
|
+
let i = 0;
|
|
523
|
+
while (i < text.length) {
|
|
524
|
+
const start = text.indexOf(marker, i);
|
|
525
|
+
if (start < 0) {
|
|
526
|
+
out += text.slice(i);
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
out += text.slice(i, start);
|
|
530
|
+
const media = mediaAt(text, start, image);
|
|
531
|
+
if (!media) {
|
|
532
|
+
out += text[start];
|
|
533
|
+
i = start + 1;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
out += media.html;
|
|
537
|
+
i = media.next;
|
|
538
|
+
}
|
|
539
|
+
return out;
|
|
540
|
+
}
|
package/desktop/src/queue.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { sessionMatches, sortSessions } from "./session-search";
|
|
3
|
+
|
|
4
|
+
describe("sessionMatches", () => {
|
|
5
|
+
test("matches a title or hint", () => {
|
|
6
|
+
const item = { label: "Fix the sidebar", hint: "Yesterday" };
|
|
7
|
+
expect(sessionMatches(item, "")).toBe(true);
|
|
8
|
+
expect(sessionMatches(item, "side")).toBe(true);
|
|
9
|
+
expect(sessionMatches(item, "yester")).toBe(true);
|
|
10
|
+
expect(sessionMatches(item, "memory")).toBe(false);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("sortSessions", () => {
|
|
15
|
+
test("keeps newest-first or reverses to oldest", () => {
|
|
16
|
+
const items = ["new", "mid", "old"];
|
|
17
|
+
expect(sortSessions(items, "newest")).toEqual(["new", "mid", "old"]);
|
|
18
|
+
expect(sortSessions(items, "oldest")).toEqual(["old", "mid", "new"]);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type SessionSort = "newest" | "oldest";
|
|
2
|
+
|
|
3
|
+
export function sessionMatches(item: { label: string; hint?: string }, query: string): boolean {
|
|
4
|
+
const q = query.trim().toLowerCase();
|
|
5
|
+
if (!q) return true;
|
|
6
|
+
if (item.label.toLowerCase().includes(q)) return true;
|
|
7
|
+
return (item.hint ?? "").toLowerCase().includes(q);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Store list is newest-first. Oldest is that order reversed. */
|
|
11
|
+
export function sortSessions<T>(items: readonly T[], sort: SessionSort): T[] {
|
|
12
|
+
return sort === "newest" ? items.slice() : items.slice().reverse();
|
|
13
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { pruneSelected, toggleOne, toggleVisible } from "./session-select";
|
|
3
|
+
|
|
4
|
+
describe("session-select", () => {
|
|
5
|
+
test("toggleOne adds and removes", () => {
|
|
6
|
+
const added = toggleOne(new Set(), "a");
|
|
7
|
+
expect([...added]).toEqual(["a"]);
|
|
8
|
+
expect([...toggleOne(added, "a")]).toEqual([]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("toggleVisible selects and clears the visible set", () => {
|
|
12
|
+
const selected = toggleVisible(new Set(["keep"]), ["a", "b"], false);
|
|
13
|
+
expect([...selected].sort()).toEqual(["a", "b", "keep"]);
|
|
14
|
+
expect([...toggleVisible(selected, ["a", "b"], true)]).toEqual(["keep"]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("pruneSelected drops ids that left the list", () => {
|
|
18
|
+
const prev = new Set(["a", "gone"]);
|
|
19
|
+
const next = pruneSelected(prev, new Set(["a"]));
|
|
20
|
+
expect([...next]).toEqual(["a"]);
|
|
21
|
+
expect(pruneSelected(next, new Set(["a"]))).toBe(next);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function pruneSelected(
|
|
2
|
+
prev: ReadonlySet<string>,
|
|
3
|
+
live: ReadonlySet<string>,
|
|
4
|
+
): ReadonlySet<string> {
|
|
5
|
+
let changed = false;
|
|
6
|
+
const next = new Set<string>();
|
|
7
|
+
for (const id of prev) {
|
|
8
|
+
if (live.has(id)) next.add(id);
|
|
9
|
+
else changed = true;
|
|
10
|
+
}
|
|
11
|
+
return changed ? next : prev;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function toggleOne(prev: ReadonlySet<string>, id: string): ReadonlySet<string> {
|
|
15
|
+
const next = new Set(prev);
|
|
16
|
+
if (next.has(id)) next.delete(id);
|
|
17
|
+
else next.add(id);
|
|
18
|
+
return next;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function toggleVisible(
|
|
22
|
+
prev: ReadonlySet<string>,
|
|
23
|
+
visibleIds: readonly string[],
|
|
24
|
+
allSelected: boolean,
|
|
25
|
+
): ReadonlySet<string> {
|
|
26
|
+
const next = new Set(prev);
|
|
27
|
+
for (const id of visibleIds) {
|
|
28
|
+
if (allSelected) next.delete(id);
|
|
29
|
+
else next.add(id);
|
|
30
|
+
}
|
|
31
|
+
return next;
|
|
32
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { type MutableRefObject, type SetStateAction, useCallback, useRef, useState } from "react";
|
|
2
|
+
import { type BridgeClient, micStart, micStop, transcribe } from "./api";
|
|
3
|
+
import type { ShowNotice, UiStatus } from "./desk-types";
|
|
4
|
+
import {
|
|
5
|
+
audioFormatFromMime,
|
|
6
|
+
blobToBase64,
|
|
7
|
+
canUseWebMic,
|
|
8
|
+
dictationErrorMessage,
|
|
9
|
+
isMicPermissionDenied,
|
|
10
|
+
pickRecorderMime,
|
|
11
|
+
} from "./dictation";
|
|
12
|
+
|
|
13
|
+
export function useDeskDictation(input: {
|
|
14
|
+
bridge: BridgeClient | null;
|
|
15
|
+
status: UiStatus;
|
|
16
|
+
showNotice: ShowNotice;
|
|
17
|
+
noticeError: (err: unknown) => void;
|
|
18
|
+
setDraft: (value: SetStateAction<string>) => void;
|
|
19
|
+
inputRef: MutableRefObject<HTMLTextAreaElement | null>;
|
|
20
|
+
}) {
|
|
21
|
+
const [listening, setListening] = useState(false);
|
|
22
|
+
const recorderRef = useRef<MediaRecorder | null>(null);
|
|
23
|
+
const dictationModeRef = useRef<"web" | "native" | null>(null);
|
|
24
|
+
|
|
25
|
+
const applySpoken = useCallback(
|
|
26
|
+
async (data: string, format: string) => {
|
|
27
|
+
if (!input.bridge) return;
|
|
28
|
+
try {
|
|
29
|
+
const result = await transcribe(input.bridge, { data, format });
|
|
30
|
+
const spoken = result.text.trim();
|
|
31
|
+
if (!spoken) {
|
|
32
|
+
input.showNotice("Transcription returned no text.", "error");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
input.setDraft((current) => (current.trim() ? `${current.trim()} ${spoken}` : spoken));
|
|
36
|
+
input.inputRef.current?.focus();
|
|
37
|
+
} catch (err) {
|
|
38
|
+
input.noticeError(err);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
[input.bridge, input.inputRef, input.noticeError, input.setDraft, input.showNotice],
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const stopDictation = useCallback(() => {
|
|
45
|
+
const mode = dictationModeRef.current;
|
|
46
|
+
if (mode === "native") {
|
|
47
|
+
dictationModeRef.current = null;
|
|
48
|
+
setListening(false);
|
|
49
|
+
void (async () => {
|
|
50
|
+
try {
|
|
51
|
+
const clip = await micStop();
|
|
52
|
+
await applySpoken(clip.data, clip.format);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
input.noticeError(err);
|
|
55
|
+
}
|
|
56
|
+
})();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const recorder = recorderRef.current;
|
|
60
|
+
if (!recorder) {
|
|
61
|
+
setListening(false);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (recorder.state !== "inactive") recorder.stop();
|
|
65
|
+
}, [applySpoken, input.noticeError]);
|
|
66
|
+
|
|
67
|
+
const startDictation = useCallback(async () => {
|
|
68
|
+
if (!input.bridge || input.status !== "idle" || listening) return;
|
|
69
|
+
const startWeb = async () => {
|
|
70
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
71
|
+
const mime = pickRecorderMime();
|
|
72
|
+
const recorder = mime
|
|
73
|
+
? new MediaRecorder(stream, { mimeType: mime })
|
|
74
|
+
: new MediaRecorder(stream);
|
|
75
|
+
const chunks: Blob[] = [];
|
|
76
|
+
recorder.ondataavailable = (event) => {
|
|
77
|
+
if (event.data.size > 0) chunks.push(event.data);
|
|
78
|
+
};
|
|
79
|
+
recorder.onstop = () => {
|
|
80
|
+
for (const track of stream.getTracks()) track.stop();
|
|
81
|
+
recorderRef.current = null;
|
|
82
|
+
dictationModeRef.current = null;
|
|
83
|
+
setListening(false);
|
|
84
|
+
const blob = new Blob(chunks, { type: recorder.mimeType || "audio/webm" });
|
|
85
|
+
if (blob.size === 0) return;
|
|
86
|
+
void blobToBase64(blob).then((data) => applySpoken(data, audioFormatFromMime(blob.type)));
|
|
87
|
+
};
|
|
88
|
+
recorderRef.current = recorder;
|
|
89
|
+
dictationModeRef.current = "web";
|
|
90
|
+
recorder.start(250);
|
|
91
|
+
setListening(true);
|
|
92
|
+
};
|
|
93
|
+
if (canUseWebMic()) {
|
|
94
|
+
try {
|
|
95
|
+
await startWeb();
|
|
96
|
+
return;
|
|
97
|
+
} catch (err) {
|
|
98
|
+
if (isMicPermissionDenied(err)) {
|
|
99
|
+
input.showNotice(dictationErrorMessage(err), "error");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
await micStart();
|
|
106
|
+
dictationModeRef.current = "native";
|
|
107
|
+
setListening(true);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
input.showNotice(dictationErrorMessage(err), "error");
|
|
110
|
+
}
|
|
111
|
+
}, [applySpoken, input.bridge, input.showNotice, input.status, listening]);
|
|
112
|
+
|
|
113
|
+
const stopRecorder = useCallback(() => {
|
|
114
|
+
const recorder = recorderRef.current;
|
|
115
|
+
if (recorder && recorder.state !== "inactive") recorder.stop();
|
|
116
|
+
if (dictationModeRef.current === "native") {
|
|
117
|
+
void micStop().catch(() => undefined);
|
|
118
|
+
dictationModeRef.current = null;
|
|
119
|
+
}
|
|
120
|
+
}, []);
|
|
121
|
+
|
|
122
|
+
return { listening, applySpoken, startDictation, stopDictation, stopRecorder };
|
|
123
|
+
}
|