@useavalon/caelence-agent 0.2.2-canary.5 → 0.2.2-canary.7
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/desktop/src/App.tsx +226 -74
- package/desktop/src/BubbleClip.tsx +58 -0
- package/desktop/src/PipelineTrack.tsx +19 -0
- package/desktop/src/PlatformPane.tsx +98 -0
- package/desktop/src/ReplySources.tsx +267 -0
- package/desktop/src/ShimmerPhrase.tsx +71 -0
- package/desktop/src/SkillsPanel.tsx +0 -7
- package/desktop/src/api.ts +24 -1
- package/desktop/src/app.css +906 -47
- package/desktop/src/clip.test.ts +16 -0
- package/desktop/src/clip.ts +26 -0
- package/desktop/src/pipeline.test.ts +86 -0
- package/desktop/src/pipeline.ts +131 -0
- package/desktop/src/platform.test.ts +30 -0
- package/desktop/src/platform.ts +76 -0
- package/desktop/src/preview-render.test.ts +79 -0
- package/desktop/src/preview-render.ts +254 -0
- package/desktop/src/queue.test.ts +18 -1
- package/desktop/src/queue.ts +8 -0
- package/desktop/src/reply-sources.test.ts +73 -0
- package/desktop/src/reply-sources.ts +116 -0
- package/desktop/src/rich-text.test.ts +26 -0
- package/desktop/src/rich-text.ts +51 -2
- package/desktop/src/stream.test.ts +41 -2
- package/desktop/src/stream.ts +22 -3
- package/desktop/src/styles/primitives.css +6 -3
- package/desktop/src/thought.test.ts +65 -18
- package/desktop/src/thought.ts +41 -11
- package/desktop/src/tool-label.test.ts +12 -0
- package/desktop/src/tool-label.ts +28 -0
- package/package.json +2 -2
- package/src/cli/models.ts +3 -1
- package/src/cli/openrouter-live.test.ts +29 -0
- package/src/cli/openrouter-live.ts +9 -1
- package/src/core/events.ts +1 -0
- package/src/core/run-turn.ts +7 -0
- package/src/core/session.ts +3 -0
- package/src/desktop/bridge.test.ts +51 -0
- package/src/desktop/bridge.ts +84 -6
- package/src/desktop/preview-kind.test.ts +20 -0
- package/src/desktop/preview-kind.ts +29 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { looksLikeWebUrl } from "./rich-text.ts";
|
|
2
|
+
|
|
3
|
+
const VOID_TAGS = new Set([
|
|
4
|
+
"area",
|
|
5
|
+
"br",
|
|
6
|
+
"col",
|
|
7
|
+
"embed",
|
|
8
|
+
"hr",
|
|
9
|
+
"img",
|
|
10
|
+
"input",
|
|
11
|
+
"source",
|
|
12
|
+
"track",
|
|
13
|
+
"wbr",
|
|
14
|
+
]);
|
|
15
|
+
const INLINE_TAGS = new Set([
|
|
16
|
+
"a",
|
|
17
|
+
"abbr",
|
|
18
|
+
"b",
|
|
19
|
+
"br",
|
|
20
|
+
"code",
|
|
21
|
+
"em",
|
|
22
|
+
"i",
|
|
23
|
+
"span",
|
|
24
|
+
"strong",
|
|
25
|
+
"sub",
|
|
26
|
+
"sup",
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
export function escapeHtml(value: string): string {
|
|
30
|
+
return value
|
|
31
|
+
.replaceAll("&", "&")
|
|
32
|
+
.replaceAll("<", "<")
|
|
33
|
+
.replaceAll(">", ">")
|
|
34
|
+
.replaceAll('"', """);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolvePreviewAsset(fromFile: string, href: string): string | undefined {
|
|
38
|
+
const raw = href.trim();
|
|
39
|
+
if (!raw || raw.startsWith("#") || raw.startsWith("data:") || raw.startsWith("mailto:")) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (looksLikeWebUrl(raw) || /^https?:\/\//i.test(raw)) return undefined;
|
|
43
|
+
const file = fromFile.replaceAll("\\", "/").replace(/^\.\//, "");
|
|
44
|
+
const slash = file.lastIndexOf("/");
|
|
45
|
+
const dir = slash >= 0 ? file.slice(0, slash) : "";
|
|
46
|
+
const joined = raw.startsWith("/")
|
|
47
|
+
? raw.replace(/^\/+/, "")
|
|
48
|
+
: [dir, raw.replace(/^\.\//, "")].filter(Boolean).join("/");
|
|
49
|
+
const parts: string[] = [];
|
|
50
|
+
for (const part of joined.split("/")) {
|
|
51
|
+
if (!part || part === ".") continue;
|
|
52
|
+
if (part === "..") {
|
|
53
|
+
if (parts.length === 0) return undefined;
|
|
54
|
+
parts.pop();
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
parts.push(part);
|
|
58
|
+
}
|
|
59
|
+
return parts.join("/") || undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function rewritePreviewAssets(
|
|
63
|
+
html: string,
|
|
64
|
+
fromFile: string,
|
|
65
|
+
hrefFor: (rel: string) => string,
|
|
66
|
+
): string {
|
|
67
|
+
return html.replace(/(src|srcset)="([^"]*)"/gi, (all, attr: string, value: string) => {
|
|
68
|
+
if (attr.toLowerCase() === "srcset") {
|
|
69
|
+
const next = value
|
|
70
|
+
.split(",")
|
|
71
|
+
.map((part) => {
|
|
72
|
+
const trimmed = part.trim();
|
|
73
|
+
if (!trimmed) return trimmed;
|
|
74
|
+
const bits = trimmed.split(/\s+/);
|
|
75
|
+
const url = bits[0] ?? "";
|
|
76
|
+
const desc = bits.slice(1).join(" ");
|
|
77
|
+
const rel = resolvePreviewAsset(fromFile, url);
|
|
78
|
+
if (!rel) return trimmed;
|
|
79
|
+
return desc ? `${hrefFor(rel)} ${desc}` : hrefFor(rel);
|
|
80
|
+
})
|
|
81
|
+
.join(", ");
|
|
82
|
+
return `${attr}="${next}"`;
|
|
83
|
+
}
|
|
84
|
+
const rel = resolvePreviewAsset(fromFile, value);
|
|
85
|
+
if (!rel) return all;
|
|
86
|
+
return `${attr}="${hrefFor(rel)}"`;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function sanitizePreviewHtml(html: string): string {
|
|
91
|
+
return html
|
|
92
|
+
.replace(/<script\b[\s\S]*?<\/script>/gi, "")
|
|
93
|
+
.replace(/<style\b[\s\S]*?<\/style>/gi, "")
|
|
94
|
+
.replace(/<(iframe|object|embed|link|meta)\b[\s\S]*?>/gi, "")
|
|
95
|
+
.replace(/\s+on[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "")
|
|
96
|
+
.replace(/\s(href|src|srcset|xlink:href)\s*=\s*(['"])\s*javascript:[\s\S]*?\2/gi, "");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function renderMarkdown(src: string): string {
|
|
100
|
+
const fences: string[] = [];
|
|
101
|
+
const htmlBlocks: string[] = [];
|
|
102
|
+
let text = src.replace(/\r\n/g, "\n");
|
|
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
|
+
});
|
|
108
|
+
text = extractHtmlBlocks(text, htmlBlocks);
|
|
109
|
+
return text
|
|
110
|
+
.split(/\n{2,}/)
|
|
111
|
+
.map((block) => {
|
|
112
|
+
const trimmed = block.trim();
|
|
113
|
+
if (!trimmed) return "";
|
|
114
|
+
const fence = /^%%FENCE(\d+)%%$/.exec(trimmed);
|
|
115
|
+
if (fence) return fences[Number(fence[1])] ?? "";
|
|
116
|
+
const html = /^%%HTML(\d+)%%$/.exec(trimmed);
|
|
117
|
+
if (html) return htmlBlocks[Number(html[1])] ?? "";
|
|
118
|
+
return renderBlock(trimmed);
|
|
119
|
+
})
|
|
120
|
+
.filter(Boolean)
|
|
121
|
+
.join("\n");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function renderPreviewHtml(text: string, kind: "markdown" | "html"): string {
|
|
125
|
+
return sanitizePreviewHtml(kind === "html" ? text : renderMarkdown(text));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function extractHtmlBlocks(src: string, store: string[]): string {
|
|
129
|
+
const lines = src.split("\n");
|
|
130
|
+
const out: string[] = [];
|
|
131
|
+
let i = 0;
|
|
132
|
+
while (i < lines.length) {
|
|
133
|
+
const line = lines[i] ?? "";
|
|
134
|
+
const start = /^\s*<([a-zA-Z][\w-]*)\b/.exec(line);
|
|
135
|
+
const tag = start?.[1]?.toLowerCase();
|
|
136
|
+
if (tag && !INLINE_TAGS.has(tag)) {
|
|
137
|
+
const chunk: string[] = [];
|
|
138
|
+
if (
|
|
139
|
+
VOID_TAGS.has(tag) ||
|
|
140
|
+
/\/>\s*$/.test(line) ||
|
|
141
|
+
new RegExp(`</${tag}\\s*>`, "i").test(line)
|
|
142
|
+
) {
|
|
143
|
+
chunk.push(line);
|
|
144
|
+
i += 1;
|
|
145
|
+
} else {
|
|
146
|
+
let depth = 1;
|
|
147
|
+
chunk.push(line);
|
|
148
|
+
i += 1;
|
|
149
|
+
const openRe = new RegExp(`<${tag}\\b`, "gi");
|
|
150
|
+
const closeRe = new RegExp(`</${tag}\\s*>`, "gi");
|
|
151
|
+
while (i < lines.length && depth > 0) {
|
|
152
|
+
const next = lines[i] ?? "";
|
|
153
|
+
depth += (next.match(openRe) ?? []).length;
|
|
154
|
+
depth -= (next.match(closeRe) ?? []).length;
|
|
155
|
+
chunk.push(next);
|
|
156
|
+
i += 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const id = store.length;
|
|
160
|
+
store.push(chunk.join("\n"));
|
|
161
|
+
out.push("", `%%HTML${id}%%`, "");
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
out.push(line);
|
|
165
|
+
i += 1;
|
|
166
|
+
}
|
|
167
|
+
return out.join("\n");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function renderBlock(block: string): string {
|
|
171
|
+
if (/^#{1,6}\s/.test(block)) {
|
|
172
|
+
const match = /^(#{1,6})\s+([\s\S]+)$/.exec(block);
|
|
173
|
+
if (match) {
|
|
174
|
+
const level = match[1]?.length ?? 1;
|
|
175
|
+
return `<h${level}>${renderInline(match[2] ?? "")}</h${level}>`;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (/^[-*_]{3,}$/.test(block)) return "<hr />";
|
|
179
|
+
if (
|
|
180
|
+
/^>\s?/m.test(block) &&
|
|
181
|
+
block.split("\n").every((line) => /^>\s?/.test(line) || !line.trim())
|
|
182
|
+
) {
|
|
183
|
+
const body = block
|
|
184
|
+
.split("\n")
|
|
185
|
+
.map((line) => line.replace(/^>\s?/, ""))
|
|
186
|
+
.join("\n");
|
|
187
|
+
return `<blockquote>${renderBlock(body)}</blockquote>`;
|
|
188
|
+
}
|
|
189
|
+
if (/^[-*]\s+/m.test(block) && block.split("\n").every((line) => /^[-*]\s+/.test(line))) {
|
|
190
|
+
const items = block
|
|
191
|
+
.split("\n")
|
|
192
|
+
.map((line) => `<li>${renderInline(line.replace(/^[-*]\s+/, ""))}</li>`)
|
|
193
|
+
.join("");
|
|
194
|
+
return `<ul>${items}</ul>`;
|
|
195
|
+
}
|
|
196
|
+
if (/^\d+\.\s+/m.test(block) && block.split("\n").every((line) => /^\d+\.\s+/.test(line))) {
|
|
197
|
+
const items = block
|
|
198
|
+
.split("\n")
|
|
199
|
+
.map((line) => `<li>${renderInline(line.replace(/^\d+\.\s+/, ""))}</li>`)
|
|
200
|
+
.join("");
|
|
201
|
+
return `<ol>${items}</ol>`;
|
|
202
|
+
}
|
|
203
|
+
if (isTableBlock(block)) return renderTable(block);
|
|
204
|
+
return `<p>${renderInline(block.replaceAll("\n", " "))}</p>`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function isTableBlock(block: string): boolean {
|
|
208
|
+
const lines = block.split("\n").filter((line) => line.trim());
|
|
209
|
+
if (lines.length < 2) return false;
|
|
210
|
+
return lines.every((line) => line.includes("|"));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function renderTable(block: string): string {
|
|
214
|
+
const lines = block.split("\n").filter((line) => line.trim());
|
|
215
|
+
const cells = (line: string) =>
|
|
216
|
+
line
|
|
217
|
+
.replace(/^\||\|$/g, "")
|
|
218
|
+
.split("|")
|
|
219
|
+
.map((cell) => cell.trim());
|
|
220
|
+
const head = cells(lines[0] ?? "");
|
|
221
|
+
const align = cells(lines[1] ?? "");
|
|
222
|
+
const isSep = align.every((cell) => /^:?-+:?$/.test(cell));
|
|
223
|
+
const bodyLines = isSep ? lines.slice(2) : lines.slice(1);
|
|
224
|
+
const th = head.map((cell) => `<th>${renderInline(cell)}</th>`).join("");
|
|
225
|
+
const rows = bodyLines
|
|
226
|
+
.map(
|
|
227
|
+
(line) =>
|
|
228
|
+
`<tr>${cells(line)
|
|
229
|
+
.map((cell) => `<td>${renderInline(cell)}</td>`)
|
|
230
|
+
.join("")}</tr>`,
|
|
231
|
+
)
|
|
232
|
+
.join("");
|
|
233
|
+
return `<table><thead><tr>${th}</tr></thead><tbody>${rows}</tbody></table>`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function renderInline(src: string): string {
|
|
237
|
+
const codes: string[] = [];
|
|
238
|
+
let text = escapeHtml(src);
|
|
239
|
+
text = text.replace(/`([^`]+)`/g, (_all, code: string) => {
|
|
240
|
+
const id = codes.length;
|
|
241
|
+
codes.push(`<code>${code}</code>`);
|
|
242
|
+
return `%%CODE${id}%%`;
|
|
243
|
+
});
|
|
244
|
+
text = text.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (_all, alt: string, href: string) => {
|
|
245
|
+
return `<img src="${href}" alt="${alt}" />`;
|
|
246
|
+
});
|
|
247
|
+
text = text.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_all, label: string, href: string) => {
|
|
248
|
+
return `<a href="${href}">${label}</a>`;
|
|
249
|
+
});
|
|
250
|
+
text = text.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
|
251
|
+
text = text.replace(/__([^_]+)__/g, "<strong>$1</strong>");
|
|
252
|
+
text = text.replace(/(^|[^\w*])\*([^*\n]+)\*(?!\*)/g, "$1<em>$2</em>");
|
|
253
|
+
return text.replace(/%%CODE(\d+)%%/g, (_all, id: string) => codes[Number(id)] ?? "");
|
|
254
|
+
}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
enqueueMessage,
|
|
4
|
+
promoteQueued,
|
|
5
|
+
removeQueued,
|
|
6
|
+
setQueuedText,
|
|
7
|
+
shiftQueue,
|
|
8
|
+
updateQueued,
|
|
9
|
+
} from "./queue.ts";
|
|
3
10
|
|
|
4
11
|
describe("message queue", () => {
|
|
5
12
|
test("enqueues trimmed text and drops empty", () => {
|
|
@@ -34,4 +41,14 @@ describe("message queue", () => {
|
|
|
34
41
|
expect(shifted.next?.text).toBe("one edited");
|
|
35
42
|
expect(shifted.rest).toEqual([]);
|
|
36
43
|
});
|
|
44
|
+
|
|
45
|
+
test("promoteQueued moves an item to the front", () => {
|
|
46
|
+
const queue = [
|
|
47
|
+
{ id: "q-1", text: "one" },
|
|
48
|
+
{ id: "q-2", text: "two" },
|
|
49
|
+
{ id: "q-3", text: "three" },
|
|
50
|
+
];
|
|
51
|
+
expect(promoteQueued(queue, "q-3").map((item) => item.id)).toEqual(["q-3", "q-1", "q-2"]);
|
|
52
|
+
expect(promoteQueued(queue, "q-1")).toEqual(queue);
|
|
53
|
+
});
|
|
37
54
|
});
|
package/desktop/src/queue.ts
CHANGED
|
@@ -38,3 +38,11 @@ export function shiftQueue(queue: QueuedMessage[]): {
|
|
|
38
38
|
const [next, ...rest] = queue;
|
|
39
39
|
return next ? { next, rest } : { rest };
|
|
40
40
|
}
|
|
41
|
+
|
|
42
|
+
export function promoteQueued(queue: QueuedMessage[], id: string): QueuedMessage[] {
|
|
43
|
+
const index = queue.findIndex((item) => item.id === id);
|
|
44
|
+
if (index <= 0) return queue;
|
|
45
|
+
const item = queue[index];
|
|
46
|
+
if (!item) return queue;
|
|
47
|
+
return [item, ...queue.filter((row) => row.id !== id)];
|
|
48
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { replySourcesForAssistant } from "./reply-sources.ts";
|
|
3
|
+
import type { StreamLine } from "./stream.ts";
|
|
4
|
+
|
|
5
|
+
function tool(
|
|
6
|
+
partial: Partial<Extract<StreamLine, { type: "tool" }>> &
|
|
7
|
+
Pick<Extract<StreamLine, { type: "tool" }>, "key" | "name" | "label" | "status">,
|
|
8
|
+
): Extract<StreamLine, { type: "tool" }> {
|
|
9
|
+
return {
|
|
10
|
+
type: "tool",
|
|
11
|
+
callId: partial.key,
|
|
12
|
+
count: 1,
|
|
13
|
+
preview: "",
|
|
14
|
+
...partial,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("reply sources", () => {
|
|
19
|
+
test("collects edits and search hits from the current turn", () => {
|
|
20
|
+
const lines: StreamLine[] = [
|
|
21
|
+
{ key: "u-0", type: "user", text: "fix readme", userTurnIndex: 0 },
|
|
22
|
+
tool({
|
|
23
|
+
key: "t1",
|
|
24
|
+
name: "grep",
|
|
25
|
+
label: "Searching files",
|
|
26
|
+
status: "ok",
|
|
27
|
+
output: JSON.stringify({
|
|
28
|
+
pattern: "harness",
|
|
29
|
+
hits: [
|
|
30
|
+
{ path: "README.md", line: 1, text: "harness" },
|
|
31
|
+
{ path: "src/runtime.ts", line: 2, text: "harness" },
|
|
32
|
+
],
|
|
33
|
+
}),
|
|
34
|
+
}),
|
|
35
|
+
tool({
|
|
36
|
+
key: "t2",
|
|
37
|
+
name: "edit_file",
|
|
38
|
+
label: "Editing README.md",
|
|
39
|
+
status: "ok",
|
|
40
|
+
preview: "README.md",
|
|
41
|
+
}),
|
|
42
|
+
{ key: "a-1", type: "assistant", text: "updated" },
|
|
43
|
+
];
|
|
44
|
+
expect(replySourcesForAssistant(lines, "a-1")).toEqual([
|
|
45
|
+
{ kind: "edit", path: "README.md", label: "README.md" },
|
|
46
|
+
{ kind: "search", path: "src/runtime.ts", label: "src/runtime.ts" },
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("ignores earlier turns and failed tools", () => {
|
|
51
|
+
const lines: StreamLine[] = [
|
|
52
|
+
{ key: "u-0", type: "user", text: "old", userTurnIndex: 0 },
|
|
53
|
+
tool({
|
|
54
|
+
key: "old",
|
|
55
|
+
name: "write_file",
|
|
56
|
+
label: "Writing leftover.ts",
|
|
57
|
+
status: "ok",
|
|
58
|
+
preview: "leftover.ts",
|
|
59
|
+
}),
|
|
60
|
+
{ key: "a-0", type: "assistant", text: "done" },
|
|
61
|
+
{ key: "u-1", type: "user", text: "new", userTurnIndex: 1 },
|
|
62
|
+
tool({
|
|
63
|
+
key: "fail",
|
|
64
|
+
name: "write_file",
|
|
65
|
+
label: "Writing no.ts",
|
|
66
|
+
status: "fail",
|
|
67
|
+
preview: "no.ts",
|
|
68
|
+
}),
|
|
69
|
+
{ key: "a-1", type: "assistant", text: "could not" },
|
|
70
|
+
];
|
|
71
|
+
expect(replySourcesForAssistant(lines, "a-1")).toEqual([]);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { StreamLine } from "./stream";
|
|
2
|
+
|
|
3
|
+
export type ReplySourceKind = "edit" | "read" | "search" | "web";
|
|
4
|
+
|
|
5
|
+
export interface ReplySource {
|
|
6
|
+
kind: ReplySourceKind;
|
|
7
|
+
path: string;
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const LABEL_FILE = /^(?:Reading|Writing|Editing)\s+(\S+)/u;
|
|
12
|
+
const WRITE_TOOLS = new Set(["write_file", "edit_file"]);
|
|
13
|
+
const READ_TOOLS = new Set(["read_file"]);
|
|
14
|
+
const SEARCH_TOOLS = new Set(["grep", "glob"]);
|
|
15
|
+
const SOURCE_CAP = 12;
|
|
16
|
+
|
|
17
|
+
function looksLikePath(value: string): boolean {
|
|
18
|
+
if (!value || value.length > 240) return false;
|
|
19
|
+
if (value.startsWith("{") || value.startsWith("[")) return false;
|
|
20
|
+
return /[./]/.test(value) || /\.[a-z0-9]{1,8}$/i.test(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseJson(text: string): unknown {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(text);
|
|
26
|
+
} catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function urlsFromText(text: string): string[] {
|
|
32
|
+
return [...text.matchAll(/https?:\/\/[^\s"'<>]+/gi)].map((match) => match[0]);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function pathsFromOutput(name: string, output?: string): string[] {
|
|
36
|
+
if (!output) return [];
|
|
37
|
+
const data = parseJson(output);
|
|
38
|
+
if (!data || typeof data !== "object") return urlsFromText(output);
|
|
39
|
+
const rec = data as Record<string, unknown>;
|
|
40
|
+
if (name === "glob" && Array.isArray(rec.matches)) {
|
|
41
|
+
return rec.matches.filter((item): item is string => typeof item === "string");
|
|
42
|
+
}
|
|
43
|
+
if (name === "grep" && Array.isArray(rec.hits)) {
|
|
44
|
+
const out: string[] = [];
|
|
45
|
+
for (const hit of rec.hits) {
|
|
46
|
+
if (hit && typeof hit === "object" && typeof (hit as { path?: unknown }).path === "string") {
|
|
47
|
+
out.push((hit as { path: string }).path);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
if (typeof rec.path === "string") return [rec.path];
|
|
53
|
+
return urlsFromText(output);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function kindForTool(name: string): ReplySourceKind | undefined {
|
|
57
|
+
if (WRITE_TOOLS.has(name)) return "edit";
|
|
58
|
+
if (READ_TOOLS.has(name)) return "read";
|
|
59
|
+
if (SEARCH_TOOLS.has(name)) return "search";
|
|
60
|
+
if (/search|firecrawl/i.test(name)) return "web";
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function sourceLabel(kind: ReplySourceKind, path: string): string {
|
|
65
|
+
if (kind === "web") return path.replace(/^https?:\/\//, "");
|
|
66
|
+
return path;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function replySourcesForAssistant(lines: StreamLine[], assistantKey: string): ReplySource[] {
|
|
70
|
+
const end = lines.findIndex((line) => line.key === assistantKey);
|
|
71
|
+
if (end < 0) return [];
|
|
72
|
+
let start = 0;
|
|
73
|
+
for (let i = end - 1; i >= 0; i--) {
|
|
74
|
+
if (lines[i]?.type === "user") {
|
|
75
|
+
start = i + 1;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const byPath = new Map<string, ReplySource>();
|
|
80
|
+
for (let i = start; i < end; i++) {
|
|
81
|
+
const line = lines[i];
|
|
82
|
+
if (line?.type !== "tool" || line.status !== "ok") continue;
|
|
83
|
+
const kind = kindForTool(line.name);
|
|
84
|
+
if (!kind) continue;
|
|
85
|
+
const fromLabel = LABEL_FILE.exec(line.label)?.[1];
|
|
86
|
+
const fromPreview = looksLikePath(line.preview) ? line.preview : undefined;
|
|
87
|
+
const extra = pathsFromOutput(line.name, line.output);
|
|
88
|
+
const paths =
|
|
89
|
+
kind === "search" || kind === "web"
|
|
90
|
+
? extra.length > 0
|
|
91
|
+
? extra
|
|
92
|
+
: fromLabel
|
|
93
|
+
? [fromLabel]
|
|
94
|
+
: fromPreview
|
|
95
|
+
? [fromPreview]
|
|
96
|
+
: []
|
|
97
|
+
: [fromLabel ?? extra[0] ?? fromPreview].filter(
|
|
98
|
+
(item): item is string => typeof item === "string" && item.length > 0,
|
|
99
|
+
);
|
|
100
|
+
for (const path of paths) {
|
|
101
|
+
const current = byPath.get(path);
|
|
102
|
+
if (current && sourceRank(current.kind) <= sourceRank(kind)) continue;
|
|
103
|
+
byPath.set(path, { kind, path, label: sourceLabel(kind, path) });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return [...byPath.values()]
|
|
107
|
+
.sort((a, b) => sourceRank(a.kind) - sourceRank(b.kind))
|
|
108
|
+
.slice(0, SOURCE_CAP);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sourceRank(kind: ReplySourceKind): number {
|
|
112
|
+
if (kind === "edit") return 0;
|
|
113
|
+
if (kind === "read") return 1;
|
|
114
|
+
if (kind === "search") return 2;
|
|
115
|
+
return 3;
|
|
116
|
+
}
|
|
@@ -76,6 +76,32 @@ describe("splitRichText", () => {
|
|
|
76
76
|
]);
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
+
test("keeps Notion list copy and scheme-less URLs as links, not files", () => {
|
|
80
|
+
const listed = splitRichText(
|
|
81
|
+
"Pages:\n1. https://www.notion.so/workspace/Notes-abc123\n2. www.notion.so/Second-page\n3. notion.so/Third",
|
|
82
|
+
);
|
|
83
|
+
expect(listed.filter((part) => part.type === "file")).toEqual([]);
|
|
84
|
+
const links = listed.filter((part) => part.type === "link");
|
|
85
|
+
expect(links.map((part) => part.href)).toEqual([
|
|
86
|
+
"https://www.notion.so/workspace/Notes-abc123",
|
|
87
|
+
"https://www.notion.so/Second-page",
|
|
88
|
+
"https://notion.so/Third",
|
|
89
|
+
]);
|
|
90
|
+
expect(
|
|
91
|
+
splitRichText("1. www.notion.so/workspace/Notes-abc123").some((part) => part.type === "file"),
|
|
92
|
+
).toBe(false);
|
|
93
|
+
expect(
|
|
94
|
+
splitRichText("1. notion.so/workspace/Notes-abc123").some((part) => part.type === "file"),
|
|
95
|
+
).toBe(false);
|
|
96
|
+
const prose = splitRichText(
|
|
97
|
+
"Main page: CPH MARATHON 24\nURL: app.notion.com/p/53e6c2a14dfe45e5beb210a33cc8c3c4\n- Workout Schedule (planned training calendar)\n- RUNS (planned/logged runs)\n- Completed Runs",
|
|
98
|
+
);
|
|
99
|
+
expect(prose.filter((part) => part.type === "file")).toEqual([]);
|
|
100
|
+
expect(prose.some((part) => part.type === "link" && part.href.includes("53e6c2a14dfe"))).toBe(
|
|
101
|
+
true,
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
79
105
|
test("keeps an explicit home path clickable", () => {
|
|
80
106
|
const parts = splitRichText("Open ~/Documents/brief.md when you can.");
|
|
81
107
|
expect(parts.some((part) => part.type === "file" && part.path === "~/Documents/brief.md")).toBe(
|
package/desktop/src/rich-text.ts
CHANGED
|
@@ -6,6 +6,8 @@ export type RichPart =
|
|
|
6
6
|
|
|
7
7
|
const MD_LINK = /\[([^\]]+)\]\(((?:https?:\/\/|file:\/\/|~\/|\/)[^)\s]+)\)/gi;
|
|
8
8
|
const BARE_URL = /https?:\/\/[^\s<>"'\])]+/gi;
|
|
9
|
+
const WEB_REF =
|
|
10
|
+
/(?:^|[\s(])((?:www\.)[^\s<>"'\])]+|[a-z0-9][a-z0-9.-]*\.[a-z]{2,}\/[^\s<>"'\])]+)/gi;
|
|
9
11
|
const DIR_REF =
|
|
10
12
|
/(?:^|[\s(])((?:~|\/(?:Users|home|Volumes|private|tmp|opt|var|Applications|Library))[^\s):,]*)/g;
|
|
11
13
|
|
|
@@ -61,9 +63,30 @@ export function joinLocalPath(dir: string, name: string): string {
|
|
|
61
63
|
return `${dir.replace(/\/+$/, "")}/${name}`;
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
export function looksLikeWebUrl(name: string): boolean {
|
|
67
|
+
const value = name.trim();
|
|
68
|
+
if (!value) return false;
|
|
69
|
+
if (/^https?:\/\//i.test(value)) return true;
|
|
70
|
+
if (/^www\./i.test(value)) return true;
|
|
71
|
+
return /^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}(?:\/|\?|#)/i.test(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function webHref(value: string): string {
|
|
75
|
+
const trimmed = value.trim();
|
|
76
|
+
if (/^https?:\/\//i.test(trimmed)) return trimmed;
|
|
77
|
+
return `https://${trimmed.replace(/^\/+/, "")}`;
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
export function looksLikeFilename(name: string): boolean {
|
|
65
81
|
if (!name || name.length > 240) return false;
|
|
66
|
-
if (
|
|
82
|
+
if (looksLikeWebUrl(name)) return false;
|
|
83
|
+
if (isLocalFileHref(name) || name.startsWith("./") || name.startsWith("../")) return true;
|
|
84
|
+
if (name.includes("/")) {
|
|
85
|
+
if (/\s/.test(name)) return false;
|
|
86
|
+
if (!/^[.A-Za-z0-9_~-]+(?:\/[.A-Za-z0-9_~-]+)+$/.test(name)) return false;
|
|
87
|
+
const last = name.split("/").pop() ?? "";
|
|
88
|
+
return /\.[A-Za-z0-9]{1,10}$/.test(last);
|
|
89
|
+
}
|
|
67
90
|
return /\.[A-Za-z0-9]{1,10}$/.test(name);
|
|
68
91
|
}
|
|
69
92
|
|
|
@@ -98,6 +121,15 @@ export function splitRichText(text: string): RichPart[] {
|
|
|
98
121
|
});
|
|
99
122
|
}
|
|
100
123
|
|
|
124
|
+
for (const hit of collectWebRefs(text)) {
|
|
125
|
+
if (overlaps(taken, hit.start, hit.end)) continue;
|
|
126
|
+
taken.push({
|
|
127
|
+
start: hit.start,
|
|
128
|
+
end: hit.end,
|
|
129
|
+
part: { type: "link", href: hit.href, label: hit.label },
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
101
133
|
for (const hit of collectLocalFiles(text)) {
|
|
102
134
|
if (overlaps(taken, hit.start, hit.end)) continue;
|
|
103
135
|
taken.push({
|
|
@@ -135,6 +167,23 @@ function overlaps(
|
|
|
135
167
|
return taken.some((item) => start < item.end && end > item.start);
|
|
136
168
|
}
|
|
137
169
|
|
|
170
|
+
function collectWebRefs(
|
|
171
|
+
text: string,
|
|
172
|
+
): Array<{ start: number; end: number; href: string; label: string }> {
|
|
173
|
+
const hits: Array<{ start: number; end: number; href: string; label: string }> = [];
|
|
174
|
+
for (const match of text.matchAll(WEB_REF)) {
|
|
175
|
+
const raw = match[1];
|
|
176
|
+
if (!raw || match.index === undefined) continue;
|
|
177
|
+
if (/^https?:\/\//i.test(raw)) continue;
|
|
178
|
+
const start = match.index + match[0].length - raw.length;
|
|
179
|
+
const token = trimTrailingPunct(raw);
|
|
180
|
+
if (!looksLikeWebUrl(token)) continue;
|
|
181
|
+
const href = webHref(token);
|
|
182
|
+
hits.push({ start, end: start + token.length, href, label: linkLabel(href) });
|
|
183
|
+
}
|
|
184
|
+
return hits;
|
|
185
|
+
}
|
|
186
|
+
|
|
138
187
|
function collectLocalFiles(
|
|
139
188
|
text: string,
|
|
140
189
|
): Array<{ start: number; end: number; path: string; label: string }> {
|
|
@@ -146,7 +195,7 @@ function collectLocalFiles(
|
|
|
146
195
|
if (!raw || match.index === undefined) continue;
|
|
147
196
|
const start = match.index + match[0].length - raw.length;
|
|
148
197
|
const path = trimTrailingPunct(raw).replace(/\/+$/, "") || raw;
|
|
149
|
-
if (path.length < 2) continue;
|
|
198
|
+
if (path.length < 2 || looksLikeWebUrl(path)) continue;
|
|
150
199
|
dirs.push({ start, path });
|
|
151
200
|
hits.push({ start, end: start + raw.length, path, label: path });
|
|
152
201
|
}
|
|
@@ -53,6 +53,28 @@ describe("applyEvent", () => {
|
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
test("coalesces back-to-back identical tool starts", () => {
|
|
56
|
+
const first = applyEvent([], {
|
|
57
|
+
kind: "tool_call_start",
|
|
58
|
+
toolName: "grep",
|
|
59
|
+
callId: "a",
|
|
60
|
+
input: { pattern: "one" },
|
|
61
|
+
});
|
|
62
|
+
const second = applyEvent(first, {
|
|
63
|
+
kind: "tool_call_start",
|
|
64
|
+
toolName: "grep",
|
|
65
|
+
callId: "b",
|
|
66
|
+
input: { pattern: "two" },
|
|
67
|
+
});
|
|
68
|
+
expect(second).toHaveLength(1);
|
|
69
|
+
expect(second[0]).toMatchObject({
|
|
70
|
+
type: "tool",
|
|
71
|
+
label: "Searching files",
|
|
72
|
+
count: 2,
|
|
73
|
+
callId: "b",
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("keeps each integration tool as its own row", () => {
|
|
56
78
|
const first = applyEvent([], {
|
|
57
79
|
kind: "tool_call_start",
|
|
58
80
|
toolName: "notion__notion_fetch",
|
|
@@ -65,8 +87,25 @@ describe("applyEvent", () => {
|
|
|
65
87
|
callId: "b",
|
|
66
88
|
input: { id: "2" },
|
|
67
89
|
});
|
|
68
|
-
expect(second).toHaveLength(
|
|
69
|
-
expect(second
|
|
90
|
+
expect(second).toHaveLength(2);
|
|
91
|
+
expect(second.map((line) => line.type === "tool" && line.callId)).toEqual(["a", "b"]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("stores tool output on tool_call_end", () => {
|
|
95
|
+
const started = applyEvent([], {
|
|
96
|
+
kind: "tool_call_start",
|
|
97
|
+
toolName: "notion__notion_fetch",
|
|
98
|
+
callId: "a",
|
|
99
|
+
input: { id: "1" },
|
|
100
|
+
});
|
|
101
|
+
const ended = applyEvent(started, {
|
|
102
|
+
kind: "tool_call_end",
|
|
103
|
+
toolName: "notion__notion_fetch",
|
|
104
|
+
callId: "a",
|
|
105
|
+
success: true,
|
|
106
|
+
output: '{"title":"Notes"}',
|
|
107
|
+
});
|
|
108
|
+
expect(ended[0]).toMatchObject({ type: "tool", status: "ok", output: '{"title":"Notes"}' });
|
|
70
109
|
});
|
|
71
110
|
|
|
72
111
|
test("appends reasoning deltas onto a thought line", () => {
|