@useavalon/caelence-agent 0.2.2-canary.6 → 0.2.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 +11 -2
- package/brand/logo/wordmark-caelence-agent-dark.png +0 -0
- package/brand/logo/wordmark-caelence-agent.png +0 -0
- package/desktop/src/App.tsx +203 -78
- package/desktop/src/PipelineTrack.tsx +19 -0
- package/desktop/src/ReplySources.tsx +267 -0
- package/desktop/src/ShimmerPhrase.tsx +71 -0
- package/desktop/src/api.ts +24 -1
- package/desktop/src/pipeline.test.ts +86 -0
- package/desktop/src/pipeline.ts +131 -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/stream.test.ts +41 -2
- package/desktop/src/stream.ts +21 -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/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 +75 -0
- 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
|
+
}
|
|
@@ -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", () => {
|
package/desktop/src/stream.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AgentEvent } from "./api";
|
|
2
2
|
import type { AttachmentPreview } from "./composer-files";
|
|
3
|
+
import { isPlatformTool, toolEventOutput } from "./platform";
|
|
3
4
|
import { inputFromPreview, toolLabel } from "./tool-label";
|
|
4
5
|
|
|
5
6
|
export type ToolStatus = "running" | "ok" | "fail";
|
|
@@ -64,7 +65,8 @@ export function previewInput(input: Record<string, unknown>): string {
|
|
|
64
65
|
const pattern = typeof input.pattern === "string" ? input.pattern : undefined;
|
|
65
66
|
const prompt = typeof input.prompt === "string" ? input.prompt : undefined;
|
|
66
67
|
const label = typeof input.label === "string" ? input.label : undefined;
|
|
67
|
-
const
|
|
68
|
+
const name = typeof input.name === "string" ? input.name : undefined;
|
|
69
|
+
const raw = label ?? cmd ?? path ?? pattern ?? prompt ?? name ?? JSON.stringify(input);
|
|
68
70
|
return raw.length > 80 ? `${raw.slice(0, 79)}…` : raw;
|
|
69
71
|
}
|
|
70
72
|
|
|
@@ -87,7 +89,12 @@ export function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[]
|
|
|
87
89
|
case "tool_call_start": {
|
|
88
90
|
const label = toolLabel(event.toolName, event.input);
|
|
89
91
|
const last = lines[lines.length - 1];
|
|
90
|
-
if (
|
|
92
|
+
if (
|
|
93
|
+
last?.type === "tool" &&
|
|
94
|
+
last.status !== "fail" &&
|
|
95
|
+
last.label === label &&
|
|
96
|
+
!isPlatformTool(event.toolName)
|
|
97
|
+
) {
|
|
91
98
|
return [
|
|
92
99
|
...lines.slice(0, -1),
|
|
93
100
|
{
|
|
@@ -117,7 +124,16 @@ export function applyEvent(lines: StreamLine[], event: AgentEvent): StreamLine[]
|
|
|
117
124
|
case "tool_call_end":
|
|
118
125
|
return lines.map((line) =>
|
|
119
126
|
line.type === "tool" && line.callId === event.callId
|
|
120
|
-
? {
|
|
127
|
+
? {
|
|
128
|
+
...line,
|
|
129
|
+
status: event.success ? "ok" : "fail",
|
|
130
|
+
error: event.error,
|
|
131
|
+
...(event.output
|
|
132
|
+
? { output: event.output }
|
|
133
|
+
: event.result !== undefined
|
|
134
|
+
? { output: toolEventOutput(event.result) }
|
|
135
|
+
: {}),
|
|
136
|
+
}
|
|
121
137
|
: line,
|
|
122
138
|
);
|
|
123
139
|
case "session_meta":
|
|
@@ -164,6 +180,7 @@ export function linesFromTranscript(
|
|
|
164
180
|
status: ToolStatus;
|
|
165
181
|
preview: string;
|
|
166
182
|
error?: string;
|
|
183
|
+
output?: string;
|
|
167
184
|
}
|
|
168
185
|
>,
|
|
169
186
|
): StreamLine[] {
|
|
@@ -193,6 +210,7 @@ export function linesFromTranscript(
|
|
|
193
210
|
count: 1,
|
|
194
211
|
preview: message.preview,
|
|
195
212
|
error: message.error,
|
|
213
|
+
...(message.output ? { output: message.output } : {}),
|
|
196
214
|
};
|
|
197
215
|
});
|
|
198
216
|
}
|