@runappsai/agent 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +162 -2
- package/dist/agent-core.umd.js +266 -0
- package/dist/agent.d.ts +222 -0
- package/dist/agent.js +1331 -0
- package/dist/agent.umd.js +732 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +4 -0
- package/dist/chat-theme.d.ts +24 -0
- package/dist/chat-theme.js +101 -0
- package/dist/chat.d.ts +271 -0
- package/dist/chat.js +4097 -0
- package/dist/compaction.d.ts +48 -0
- package/dist/compaction.js +208 -0
- package/dist/completion.d.ts +29 -0
- package/dist/completion.js +88 -0
- package/dist/context.d.ts +44 -0
- package/dist/context.js +177 -0
- package/dist/debug-panel.d.ts +40 -0
- package/dist/debug-panel.js +262 -0
- package/dist/dialogs.d.ts +7 -0
- package/dist/dialogs.js +231 -0
- package/dist/directory-fs.d.ts +5 -0
- package/dist/directory-fs.js +118 -0
- package/dist/dom.d.ts +83 -0
- package/dist/dom.js +163 -0
- package/dist/edit.d.ts +22 -0
- package/dist/edit.js +133 -0
- package/dist/file-guard.d.ts +46 -0
- package/dist/file-guard.js +153 -0
- package/dist/file-tools.d.ts +16 -0
- package/dist/file-tools.js +339 -0
- package/dist/files-panel.d.ts +60 -0
- package/dist/files-panel.js +613 -0
- package/dist/index.cjs +4848 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +44 -0
- package/dist/json-schema.d.ts +3 -0
- package/dist/json-schema.js +141 -0
- package/dist/llm.d.ts +95 -0
- package/dist/llm.js +5 -0
- package/dist/markdown.d.ts +4 -0
- package/dist/markdown.js +265 -0
- package/dist/mcp.d.ts +82 -0
- package/dist/mcp.js +325 -0
- package/dist/node.cjs +402 -0
- package/dist/node.d.ts +78 -0
- package/dist/node.js +432 -0
- package/dist/pdf.d.ts +52 -0
- package/dist/pdf.js +77 -0
- package/dist/provider.d.ts +135 -0
- package/dist/provider.js +181 -0
- package/dist/sandbox.d.ts +33 -0
- package/dist/sandbox.js +219 -0
- package/dist/select.d.ts +30 -0
- package/dist/select.js +185 -0
- package/dist/skill-zips.d.ts +28 -0
- package/dist/skill-zips.js +123 -0
- package/dist/skills.d.ts +20 -0
- package/dist/skills.js +111 -0
- package/dist/templates.d.ts +24 -0
- package/dist/templates.js +136 -0
- package/dist/tool.d.ts +28 -0
- package/dist/tool.js +102 -0
- package/dist/types.d.ts +571 -0
- package/dist/types.js +7 -0
- package/dist/ui.cjs +6246 -0
- package/dist/ui.d.ts +6 -0
- package/dist/ui.js +11 -0
- package/dist/vlist.d.ts +80 -0
- package/dist/vlist.js +277 -0
- package/dist/workspace.d.ts +343 -0
- package/dist/workspace.js +1203 -0
- package/dist/zip.d.ts +26 -0
- package/dist/zip.js +204 -0
- package/docs/agent.md +228 -0
- package/docs/chat-ui.md +307 -0
- package/docs/node.md +135 -0
- package/docs/providers.md +176 -0
- package/docs/reference.md +143 -0
- package/docs/skills.md +131 -0
- package/docs/tools.md +254 -0
- package/docs/workspace.md +198 -0
- package/package.json +59 -4
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export declare function h<K extends keyof HTMLElementTagNameMap>(tag: K, cls?: string, text?: string): HTMLElementTagNameMap[K];
|
|
2
|
+
export declare const svg: (size: number, body: string, stroke?: number) => string;
|
|
3
|
+
export declare const I: {
|
|
4
|
+
tick: (s?: number) => string;
|
|
5
|
+
enter: (s?: number) => string;
|
|
6
|
+
lock: (s?: number) => string;
|
|
7
|
+
puzzle: (s?: number) => string;
|
|
8
|
+
spark: (s?: number) => string;
|
|
9
|
+
send: (s?: number) => string;
|
|
10
|
+
hist: (s?: number) => string;
|
|
11
|
+
eraser: (s?: number) => string;
|
|
12
|
+
pointer: (s?: number) => string;
|
|
13
|
+
plus: (s?: number) => string;
|
|
14
|
+
chev: (s?: number) => string;
|
|
15
|
+
brain: (s?: number) => string;
|
|
16
|
+
layers: (s?: number) => string;
|
|
17
|
+
check: (s?: number) => string;
|
|
18
|
+
xcircle: (s?: number) => string;
|
|
19
|
+
ban: (s?: number) => string;
|
|
20
|
+
square: (s?: number) => string;
|
|
21
|
+
corner: (s?: number) => string;
|
|
22
|
+
x: (s?: number) => string;
|
|
23
|
+
trash: (s?: number) => string;
|
|
24
|
+
clip: (s?: number) => string;
|
|
25
|
+
file: (s?: number) => string;
|
|
26
|
+
folder: (s?: number) => string;
|
|
27
|
+
folderOpen: (s?: number) => string;
|
|
28
|
+
upload: (s?: number) => string;
|
|
29
|
+
download: (s?: number) => string;
|
|
30
|
+
refresh: (s?: number) => string;
|
|
31
|
+
save: (s?: number) => string;
|
|
32
|
+
chevDown: (s?: number) => string;
|
|
33
|
+
checks: (s?: number) => string;
|
|
34
|
+
undo: (s?: number) => string;
|
|
35
|
+
gear: (s?: number) => string;
|
|
36
|
+
wrap: (s?: number) => string;
|
|
37
|
+
eye: (s?: number) => string;
|
|
38
|
+
pen: (s?: number) => string;
|
|
39
|
+
alert: (s?: number) => string;
|
|
40
|
+
arrowDown: (s?: number) => string;
|
|
41
|
+
bug: (s?: number) => string;
|
|
42
|
+
copy: (s?: number) => string;
|
|
43
|
+
fileText: (s?: number) => string;
|
|
44
|
+
filePlus: (s?: number) => string;
|
|
45
|
+
filePen: (s?: number) => string;
|
|
46
|
+
fileX: (s?: number) => string;
|
|
47
|
+
search: (s?: number) => string;
|
|
48
|
+
braces: (s?: number) => string;
|
|
49
|
+
message: (s?: number) => string;
|
|
50
|
+
wrench: (s?: number) => string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The chat's own dialog over `root`: title, message, optional extra
|
|
54
|
+
* content, Cancel / OK. Resolves true for OK; Escape and the backdrop
|
|
55
|
+
* cancel. `submitOnEnter` makes Enter (not while composing) press OK.
|
|
56
|
+
*/
|
|
57
|
+
export declare function modal(root: HTMLElement, o: {
|
|
58
|
+
title: string;
|
|
59
|
+
message?: string;
|
|
60
|
+
body?: HTMLElement[];
|
|
61
|
+
ok: string;
|
|
62
|
+
cancel: string;
|
|
63
|
+
danger?: boolean;
|
|
64
|
+
focus?: "ok" | "cancel" | HTMLElement;
|
|
65
|
+
submitOnEnter?: boolean;
|
|
66
|
+
}): Promise<boolean>;
|
|
67
|
+
/** A yes/no question of the chat's own (deleting a session). */
|
|
68
|
+
export declare function modalConfirm(root: HTMLElement, title: string, message: string, opts: {
|
|
69
|
+
ok: string;
|
|
70
|
+
cancel: string;
|
|
71
|
+
danger?: boolean;
|
|
72
|
+
}): Promise<boolean>;
|
|
73
|
+
/** A one-line question of the chat's own (a new file's path); null when cancelled or empty. */
|
|
74
|
+
export declare function modalInput(root: HTMLElement, title: string, opts: {
|
|
75
|
+
ok: string;
|
|
76
|
+
cancel: string;
|
|
77
|
+
initial?: string;
|
|
78
|
+
placeholder?: string;
|
|
79
|
+
}): Promise<string | null>;
|
|
80
|
+
/** Hands the browser a file to save. */
|
|
81
|
+
export declare function downloadBlob(name: string, data: string | Uint8Array, type?: string): void;
|
|
82
|
+
/** Markdown → DOM, with a header (language, copy) on each code block and a scroll wrapper on tables. */
|
|
83
|
+
export declare function renderMD(el: HTMLElement, md: string, streaming: boolean): void;
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// DOM helpers shared by the chat and its files panel: element factory,
|
|
2
|
+
// inline lucide icons, the Markdown → DOM renderer, and the chat's own
|
|
3
|
+
// dialogs (over the chat, not in the agent's question area).
|
|
4
|
+
import { renderMarkdown } from "./markdown.js";
|
|
5
|
+
export function h(tag, cls, text) {
|
|
6
|
+
const e = document.createElement(tag);
|
|
7
|
+
if (cls)
|
|
8
|
+
e.className = cls;
|
|
9
|
+
if (text !== undefined)
|
|
10
|
+
e.textContent = text;
|
|
11
|
+
return e;
|
|
12
|
+
}
|
|
13
|
+
export const svg = (size, body, stroke = 2) => `<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${stroke}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${body}</svg>`;
|
|
14
|
+
export const I = {
|
|
15
|
+
tick: (s = 14) => svg(s, '<path d="M20 6 9 17l-5-5"/>'),
|
|
16
|
+
enter: (s = 14) => svg(s, '<polyline points="9 10 4 15 9 20"/><path d="M20 4v7a4 4 0 0 1-4 4H4"/>'),
|
|
17
|
+
lock: (s = 12) => svg(s, '<rect width="18" height="11" x="3" y="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/>'),
|
|
18
|
+
puzzle: (s = 14) => svg(s, '<path d="M15.39 4.39a1 1 0 0 0 1.68-.474 2.5 2.5 0 1 1 3.014 3.015 1 1 0 0 0-.474 1.68l1.683 1.682a2.414 2.414 0 0 1 0 3.414L19.61 15.39a1 1 0 0 1-1.68-.474 2.5 2.5 0 1 0-3.014 3.015 1 1 0 0 1 .474 1.68l-1.683 1.682a2.414 2.414 0 0 1-3.414 0L8.61 19.61a1 1 0 0 0-1.68.474 2.5 2.5 0 1 1-3.014-3.015 1 1 0 0 0 .474-1.68l-1.683-1.682a2.414 2.414 0 0 1 0-3.414L4.39 8.61a1 1 0 0 1 1.68.474 2.5 2.5 0 1 0 3.014-3.015 1 1 0 0 1-.474-1.68l1.683-1.682a2.414 2.414 0 0 1 3.414 0z"/>'),
|
|
19
|
+
spark: (s = 14) => svg(s, '<path d="M9.9 2.1a1 1 0 0 1 1.9 0l1.3 4.1a4 4 0 0 0 2.6 2.6l4.1 1.3a1 1 0 0 1 0 1.9l-4.1 1.3a4 4 0 0 0-2.6 2.6l-1.3 4.1a1 1 0 0 1-1.9 0l-1.3-4.1a4 4 0 0 0-2.6-2.6L2.1 12a1 1 0 0 1 0-1.9l4.1-1.3a4 4 0 0 0 2.6-2.6z"/><path d="M20 3v4M22 5h-4"/>'),
|
|
20
|
+
send: (s = 16) => svg(s, '<path d="M14.5 9.5 22 2M22 2l-7 20-4-9-9-4z"/>'),
|
|
21
|
+
hist: (s = 14) => svg(s, '<path d="M3 12a9 9 0 1 0 3-6.7L3 8"/><path d="M3 3v5h5"/><path d="M12 7v5l3 3"/>'),
|
|
22
|
+
eraser: (s = 14) => svg(s, '<path d="M21 21H8a2 2 0 0 1-1.42-.587l-3.994-3.999a2 2 0 0 1 0-2.828l10-10a2 2 0 0 1 2.829 0l5.999 6a2 2 0 0 1 0 2.828L12.834 21"/><path d="m5.082 11.09 8.828 8.828"/>'),
|
|
23
|
+
pointer: (s = 14) => svg(s, '<path d="M4.037 4.688a.495.495 0 0 1 .651-.651l16 6.5a.5.5 0 0 1-.063.947l-6.124 1.58a2 2 0 0 0-1.438 1.435l-1.579 6.126a.5.5 0 0 1-.947.063z"/>'),
|
|
24
|
+
plus: (s = 14) => svg(s, '<path d="M12 5v14M5 12h14"/>'),
|
|
25
|
+
chev: (s = 14) => svg(s, '<path d="m9 18 6-6-6-6"/>'),
|
|
26
|
+
brain: (s = 13) => svg(s, '<path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"/><path d="M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"/><path d="M15 13a4.5 4.5 0 0 1-3-4 4.5 4.5 0 0 1-3 4"/><path d="M17.6 6.5a3 3 0 0 0 .4 1M6.4 6.5a3 3 0 0 1-.4 1M12 18v-5"/>'),
|
|
27
|
+
layers: (s = 14) => svg(s, '<path d="m12.8 2.2 8.5 4a1 1 0 0 1 0 1.6l-8.5 4a2 2 0 0 1-1.6 0l-8.5-4a1 1 0 0 1 0-1.6l8.5-4a2 2 0 0 1 1.6 0"/><path d="m2.4 11.9 8.8 4.1a2 2 0 0 0 1.6 0l8.8-4.1"/><path d="m2.4 16.9 8.8 4.1a2 2 0 0 0 1.6 0l8.8-4.1"/>'),
|
|
28
|
+
check: (s = 15) => svg(s, '<circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/>'),
|
|
29
|
+
xcircle: (s = 15) => svg(s, '<circle cx="12" cy="12" r="10"/><path d="m15 9-6 6M9 9l6 6"/>'),
|
|
30
|
+
ban: (s = 15) => svg(s, '<circle cx="12" cy="12" r="10"/><path d="m4.9 4.9 14.2 14.2"/>'),
|
|
31
|
+
square: (s = 12) => svg(s, '<rect x="3" y="3" width="18" height="18" rx="2"/>'),
|
|
32
|
+
corner: (s = 14) => svg(s, '<path d="M20 4v7a4 4 0 0 1-4 4H4"/><path d="m9 10-5 5 5 5"/>'),
|
|
33
|
+
x: (s = 12) => svg(s, '<path d="M18 6 6 18M6 6l12 12"/>'),
|
|
34
|
+
trash: (s = 13) => svg(s, '<path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>'),
|
|
35
|
+
clip: (s = 16) => svg(s, '<path d="m21.4 11.1-9.2 9.2a6 6 0 0 1-8.5-8.5l9.2-9.2a4 4 0 0 1 5.7 5.7l-9.2 9.2a2 2 0 0 1-2.8-2.8l8.5-8.5"/>'),
|
|
36
|
+
file: (s = 14) => svg(s, '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/>'),
|
|
37
|
+
folder: (s = 14) => svg(s, '<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>'),
|
|
38
|
+
folderOpen: (s = 14) => svg(s, '<path d="m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2"/>'),
|
|
39
|
+
upload: (s = 14) => svg(s, '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m17 8-5-5-5 5"/><path d="M12 3v12"/>'),
|
|
40
|
+
download: (s = 14) => svg(s, '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/><path d="M12 15V3"/>'),
|
|
41
|
+
refresh: (s = 13) => svg(s, '<path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16"/><path d="M16 16h5v5"/>'),
|
|
42
|
+
save: (s = 12) => svg(s, '<path d="M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7"/><path d="M7 3v4a1 1 0 0 0 1 1h7"/>'),
|
|
43
|
+
chevDown: (s = 12) => svg(s, '<path d="m6 9 6 6 6-6"/>'),
|
|
44
|
+
checks: (s = 13) => svg(s, '<path d="M18 6 7 17l-5-5"/><path d="m22 10-7.5 7.5L13 16"/>'),
|
|
45
|
+
undo: (s = 13) => svg(s, '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/>'),
|
|
46
|
+
gear: (s = 14) => svg(s, '<path d="M12.2 2h-.4a2 2 0 0 0-2 2v.2a2 2 0 0 1-1 1.7l-.4.3a2 2 0 0 1-2 0l-.2-.1a2 2 0 0 0-2.7.7l-.2.4a2 2 0 0 0 .7 2.7l.2.1a2 2 0 0 1 1 1.7v.6a2 2 0 0 1-1 1.8l-.2.1a2 2 0 0 0-.7 2.7l.2.4a2 2 0 0 0 2.7.7l.2-.1a2 2 0 0 1 2 0l.4.2a2 2 0 0 1 1 1.7V20a2 2 0 0 0 2 2h.4a2 2 0 0 0 2-2v-.2a2 2 0 0 1 1-1.7l.4-.2a2 2 0 0 1 2 0l.2.1a2 2 0 0 0 2.7-.7l.2-.4a2 2 0 0 0-.7-2.7l-.2-.1a2 2 0 0 1-1-1.8v-.5a2 2 0 0 1 1-1.7l.2-.1a2 2 0 0 0 .7-2.7l-.2-.4a2 2 0 0 0-2.7-.7l-.2.1a2 2 0 0 1-2 0l-.4-.3a2 2 0 0 1-1-1.7V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/>'),
|
|
47
|
+
wrap: (s = 14) => svg(s, '<path d="M3 6h18"/><path d="M3 12h15a3 3 0 1 1 0 6h-4"/><path d="m16 16-2 2 2 2"/><path d="M3 18h7"/>'),
|
|
48
|
+
eye: (s = 14) => svg(s, '<path d="M2.1 12.4a1 1 0 0 1 0-.8 10.5 10.5 0 0 1 19.8 0 1 1 0 0 1 0 .8 10.5 10.5 0 0 1-19.8 0"/><circle cx="12" cy="12" r="3"/>'),
|
|
49
|
+
pen: (s = 14) => svg(s, '<path d="M21.2 6.4a2 2 0 0 0 0-2.8l-.8-.8a2 2 0 0 0-2.8 0L4 16.4V20h3.6z"/><path d="m15 5 4 4"/>'),
|
|
50
|
+
alert: (s = 14) => svg(s, '<path d="m21.7 18-8-14a2 2 0 0 0-3.5 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.7-3"/><path d="M12 9v4"/><path d="M12 17h.01"/>'),
|
|
51
|
+
arrowDown: (s = 15) => svg(s, '<path d="M12 5v14"/><path d="m19 12-7 7-7-7"/>'),
|
|
52
|
+
bug: (s = 14) => svg(s, '<path d="m8 2 1.88 1.88"/><path d="M14.12 3.88 16 2"/><path d="M9 7.13v-1a3.003 3.003 0 1 1 6 0v1"/><path d="M12 20c-3.3 0-6-2.7-6-6v-3a4 4 0 0 1 4-4h4a4 4 0 0 1 4 4v3c0 3.3-2.7 6-6 6"/><path d="M12 20v-9"/><path d="M6.53 9C4.6 8.8 3 7.1 3 5"/><path d="M6 13H2"/><path d="M3 21c0-2.1 1.7-3.9 3.8-4"/><path d="M20.97 5c0 2.1-1.6 3.8-3.5 4"/><path d="M22 13h-4"/><path d="M17.2 17c2.1.1 3.8 1.9 3.8 4"/>'),
|
|
53
|
+
copy: (s = 14) => svg(s, '<rect width="14" height="14" x="8" y="8" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>'),
|
|
54
|
+
fileText: (s = 13) => svg(s, '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8M16 13H8M16 17H8"/>'),
|
|
55
|
+
filePlus: (s = 13) => svg(s, '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M9 15h6M12 18v-6"/>'),
|
|
56
|
+
filePen: (s = 13) => svg(s, '<path d="M12.5 22H18a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v9.5"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M13.4 20.3a1 1 0 0 0 .5-.3l6.9-6.9a1.4 1.4 0 0 0-2-2l-6.9 6.9a1 1 0 0 0-.3.5l-.4 1.8z"/>'),
|
|
57
|
+
fileX: (s = 13) => svg(s, '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="m14.5 12.5-5 5M9.5 12.5l5 5"/>'),
|
|
58
|
+
search: (s = 13) => svg(s, '<circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>'),
|
|
59
|
+
braces: (s = 13) => svg(s, '<path d="M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1"/><path d="M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1"/>'),
|
|
60
|
+
message: (s = 13) => svg(s, '<path d="M7.9 20A9 9 0 1 0 4 16.1L2 22Z"/>'),
|
|
61
|
+
wrench: (s = 13) => svg(s, '<path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>'),
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* The chat's own dialog over `root`: title, message, optional extra
|
|
65
|
+
* content, Cancel / OK. Resolves true for OK; Escape and the backdrop
|
|
66
|
+
* cancel. `submitOnEnter` makes Enter (not while composing) press OK.
|
|
67
|
+
*/
|
|
68
|
+
export function modal(root, o) {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
const overlay = h("div", "ra-modal");
|
|
71
|
+
const dlg = h("div", "ra-dlg");
|
|
72
|
+
dlg.setAttribute("role", "dialog");
|
|
73
|
+
dlg.setAttribute("aria-modal", "true");
|
|
74
|
+
dlg.setAttribute("aria-label", o.title);
|
|
75
|
+
dlg.append(h("div", "ra-dt", o.title));
|
|
76
|
+
if (o.message)
|
|
77
|
+
dlg.append(h("div", "ra-dm", o.message));
|
|
78
|
+
if (o.body)
|
|
79
|
+
dlg.append(...o.body);
|
|
80
|
+
const row = h("div", "ra-btns");
|
|
81
|
+
const done = (v) => { overlay.remove(); resolve(v); };
|
|
82
|
+
const cancel = h("button", "ra-btn", o.cancel);
|
|
83
|
+
cancel.type = "button";
|
|
84
|
+
cancel.addEventListener("click", () => done(false));
|
|
85
|
+
const ok = h("button", "ra-btn " + (o.danger ? "red" : "solid"), o.ok);
|
|
86
|
+
ok.type = "button";
|
|
87
|
+
ok.addEventListener("click", () => done(true));
|
|
88
|
+
row.append(cancel, ok);
|
|
89
|
+
dlg.append(row);
|
|
90
|
+
overlay.append(dlg);
|
|
91
|
+
overlay.addEventListener("click", (e) => { if (e.target === overlay)
|
|
92
|
+
done(false); });
|
|
93
|
+
overlay.addEventListener("keydown", (e) => {
|
|
94
|
+
if (e.key === "Escape") {
|
|
95
|
+
e.preventDefault();
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
done(false);
|
|
98
|
+
}
|
|
99
|
+
if (o.submitOnEnter && e.key === "Enter" && !e.isComposing && e.keyCode !== 229) {
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
done(true);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
root.append(overlay);
|
|
105
|
+
const f = o.focus === "ok" ? ok : o.focus instanceof HTMLElement ? o.focus : cancel;
|
|
106
|
+
queueMicrotask(() => { f.focus(); if (f instanceof HTMLInputElement)
|
|
107
|
+
f.select(); });
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** A yes/no question of the chat's own (deleting a session). */
|
|
111
|
+
export function modalConfirm(root, title, message, opts) {
|
|
112
|
+
return modal(root, { title, message, ...opts });
|
|
113
|
+
}
|
|
114
|
+
/** A one-line question of the chat's own (a new file's path); null when cancelled or empty. */
|
|
115
|
+
export async function modalInput(root, title, opts) {
|
|
116
|
+
const field = h("input", "ra-field");
|
|
117
|
+
field.type = "text";
|
|
118
|
+
field.value = opts.initial ?? "";
|
|
119
|
+
if (opts.placeholder)
|
|
120
|
+
field.placeholder = opts.placeholder;
|
|
121
|
+
const ok = await modal(root, { title, body: [field], ok: opts.ok, cancel: opts.cancel, focus: field, submitOnEnter: true });
|
|
122
|
+
return ok ? field.value.trim() || null : null;
|
|
123
|
+
}
|
|
124
|
+
/** Hands the browser a file to save. */
|
|
125
|
+
export function downloadBlob(name, data, type) {
|
|
126
|
+
const blob = new Blob([data], { type: type || "application/octet-stream" });
|
|
127
|
+
const url = URL.createObjectURL(blob);
|
|
128
|
+
const a = h("a");
|
|
129
|
+
a.href = url;
|
|
130
|
+
a.download = name;
|
|
131
|
+
document.body.append(a);
|
|
132
|
+
a.click();
|
|
133
|
+
a.remove();
|
|
134
|
+
setTimeout(() => URL.revokeObjectURL(url), 10_000);
|
|
135
|
+
}
|
|
136
|
+
/** Markdown → DOM, with a header (language, copy) on each code block and a scroll wrapper on tables. */
|
|
137
|
+
export function renderMD(el, md, streaming) {
|
|
138
|
+
el.innerHTML = renderMarkdown(md);
|
|
139
|
+
el.classList.toggle("ra-cursor", streaming);
|
|
140
|
+
for (const pre of Array.from(el.querySelectorAll("pre"))) {
|
|
141
|
+
const code = pre.querySelector("code");
|
|
142
|
+
const lang = code?.className.match(/language-([\w+#-]+)/)?.[1] ?? "";
|
|
143
|
+
const box = h("div", "ra-cb");
|
|
144
|
+
const head = h("div", "ra-cbh");
|
|
145
|
+
head.append(h("span", "", lang));
|
|
146
|
+
const copy = h("button", "ra-copy");
|
|
147
|
+
copy.type = "button";
|
|
148
|
+
copy.innerHTML = I.copy();
|
|
149
|
+
copy.title = /^zh/i.test(document.documentElement.lang || navigator.language || "") ? "复制" : "Copy";
|
|
150
|
+
copy.setAttribute("aria-label", copy.title);
|
|
151
|
+
copy.addEventListener("click", () => {
|
|
152
|
+
void navigator.clipboard?.writeText((code?.textContent ?? "").replace(/\n$/, "")).then(() => { copy.innerHTML = I.check(14); setTimeout(() => { copy.innerHTML = I.copy(); }, 1200); });
|
|
153
|
+
});
|
|
154
|
+
head.append(copy);
|
|
155
|
+
pre.replaceWith(box);
|
|
156
|
+
box.append(head, pre);
|
|
157
|
+
}
|
|
158
|
+
for (const table of Array.from(el.querySelectorAll("table"))) {
|
|
159
|
+
const w = h("div", "ra-tw");
|
|
160
|
+
table.replaceWith(w);
|
|
161
|
+
w.append(table);
|
|
162
|
+
}
|
|
163
|
+
}
|
package/dist/edit.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface TextEdit {
|
|
2
|
+
oldText: string;
|
|
3
|
+
newText: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The normalised text with, for each of its characters, the span of the
|
|
7
|
+
* original it came from — so a match found in the normalised text is
|
|
8
|
+
* spliced into the original, and the rest of the file keeps its exact
|
|
9
|
+
* characters (full-width punctuation, trailing spaces, quotes).
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeWithMap(text: string): {
|
|
12
|
+
text: string;
|
|
13
|
+
start: number[];
|
|
14
|
+
end: number[];
|
|
15
|
+
};
|
|
16
|
+
export declare function normalizeForFuzzyMatch(text: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Applies `edits` to `content`. Each oldText must occur exactly once and
|
|
19
|
+
* the matches must not overlap. Returns the new content; throws with
|
|
20
|
+
* pi's wording when an edit cannot be applied.
|
|
21
|
+
*/
|
|
22
|
+
export declare function applyEdits(path: string, content: string, edits: TextEdit[]): string;
|
package/dist/edit.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Exact-text file edits with pi's matching rules (github.com/earendil-
|
|
2
|
+
// works/pi, MIT): several disjoint replacements in one call, matched
|
|
3
|
+
// against the original text; an exact match first, else a normalised
|
|
4
|
+
// match (NFKC, trailing whitespace, smart quotes, dashes, odd spaces) so
|
|
5
|
+
// a model's slightly different quoting still lands; and its error
|
|
6
|
+
// messages, which tell the model what to fix.
|
|
7
|
+
function normalizeChar(ch) {
|
|
8
|
+
return ch
|
|
9
|
+
.normalize("NFKC")
|
|
10
|
+
.replace(/[\u2018\u2019\u201A\u201B]/g, "'")
|
|
11
|
+
.replace(/[\u201C\u201D\u201E\u201F]/g, '"')
|
|
12
|
+
.replace(/[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/g, "-")
|
|
13
|
+
.replace(/[\u00A0\u2002-\u200A\u202F\u205F\u3000]/g, " ");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The normalised text with, for each of its characters, the span of the
|
|
17
|
+
* original it came from — so a match found in the normalised text is
|
|
18
|
+
* spliced into the original, and the rest of the file keeps its exact
|
|
19
|
+
* characters (full-width punctuation, trailing spaces, quotes).
|
|
20
|
+
*/
|
|
21
|
+
export function normalizeWithMap(text) {
|
|
22
|
+
let out = "";
|
|
23
|
+
const start = [];
|
|
24
|
+
const end = [];
|
|
25
|
+
let pos = 0;
|
|
26
|
+
const lines = text.split("\n");
|
|
27
|
+
lines.forEach((line, li) => {
|
|
28
|
+
const kept = line.trimEnd();
|
|
29
|
+
let i = 0;
|
|
30
|
+
for (const ch of kept) {
|
|
31
|
+
const n = normalizeChar(ch);
|
|
32
|
+
for (let k = 0; k < n.length; k++) {
|
|
33
|
+
start.push(pos + i);
|
|
34
|
+
end.push(pos + i + ch.length);
|
|
35
|
+
}
|
|
36
|
+
out += n;
|
|
37
|
+
i += ch.length;
|
|
38
|
+
}
|
|
39
|
+
// trailing whitespace of the line produces nothing; the newline maps to itself
|
|
40
|
+
pos += line.length;
|
|
41
|
+
if (li < lines.length - 1) {
|
|
42
|
+
out += "\n";
|
|
43
|
+
start.push(pos);
|
|
44
|
+
end.push(pos + 1);
|
|
45
|
+
pos += 1;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return { text: out, start, end };
|
|
49
|
+
}
|
|
50
|
+
export function normalizeForFuzzyMatch(text) {
|
|
51
|
+
return normalizeWithMap(text).text;
|
|
52
|
+
}
|
|
53
|
+
function countOccurrences(content, needle) {
|
|
54
|
+
let n = 0;
|
|
55
|
+
let i = 0;
|
|
56
|
+
while ((i = content.indexOf(needle, i)) !== -1) {
|
|
57
|
+
n++;
|
|
58
|
+
i += needle.length;
|
|
59
|
+
}
|
|
60
|
+
return n;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Applies `edits` to `content`. Each oldText must occur exactly once and
|
|
64
|
+
* the matches must not overlap. Returns the new content; throws with
|
|
65
|
+
* pi's wording when an edit cannot be applied.
|
|
66
|
+
*/
|
|
67
|
+
export function applyEdits(path, content, edits) {
|
|
68
|
+
if (!edits.length)
|
|
69
|
+
throw new Error("Edit tool input is invalid. edits must contain at least one replacement.");
|
|
70
|
+
const many = edits.length > 1;
|
|
71
|
+
const bom = content.startsWith("\uFEFF") ? "\uFEFF" : "";
|
|
72
|
+
const crlf = /\r\n/.test(content);
|
|
73
|
+
const original = content.slice(bom.length).replace(/\r\n/g, "\n");
|
|
74
|
+
// Exact matching first; when any edit needs it, match all of them on the normalised text.
|
|
75
|
+
let haystack = original;
|
|
76
|
+
let map = null;
|
|
77
|
+
let needsFuzzy = false;
|
|
78
|
+
for (const e of edits) {
|
|
79
|
+
if (!e.oldText)
|
|
80
|
+
continue;
|
|
81
|
+
if (!original.includes(e.oldText.replace(/\r\n/g, "\n"))) {
|
|
82
|
+
needsFuzzy = true;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (needsFuzzy) {
|
|
87
|
+
const n = normalizeWithMap(original);
|
|
88
|
+
haystack = n.text;
|
|
89
|
+
map = n;
|
|
90
|
+
}
|
|
91
|
+
const spans = [];
|
|
92
|
+
edits.forEach((e, i) => {
|
|
93
|
+
const oldText = e.oldText.replace(/\r\n/g, "\n");
|
|
94
|
+
if (!oldText)
|
|
95
|
+
throw new Error(many ? `edits[${i}].oldText must not be empty in ${path}.` : `oldText must not be empty in ${path}.`);
|
|
96
|
+
const needle = needsFuzzy ? normalizeForFuzzyMatch(oldText) : oldText;
|
|
97
|
+
const count = countOccurrences(haystack, needle);
|
|
98
|
+
if (count === 0) {
|
|
99
|
+
throw new Error(many
|
|
100
|
+
? `Could not find edits[${i}] in ${path}. The oldText must match exactly including all whitespace and newlines.`
|
|
101
|
+
: `Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.`);
|
|
102
|
+
}
|
|
103
|
+
if (count > 1) {
|
|
104
|
+
throw new Error(many
|
|
105
|
+
? `Found ${count} occurrences of edits[${i}] in ${path}. Each oldText must be unique. Please provide more context to make it unique.`
|
|
106
|
+
: `Found ${count} occurrences of the text in ${path}. The text must be unique. Please provide more context to make it unique.`);
|
|
107
|
+
}
|
|
108
|
+
const at = haystack.indexOf(needle);
|
|
109
|
+
// Back to the original's offsets when matched on the normalised text.
|
|
110
|
+
const start = map ? map.start[at] : at;
|
|
111
|
+
const end = map ? map.end[at + needle.length - 1] : at + needle.length;
|
|
112
|
+
spans.push({ start, end, newText: e.newText.replace(/\r\n/g, "\n"), i });
|
|
113
|
+
});
|
|
114
|
+
spans.sort((a, b) => a.start - b.start);
|
|
115
|
+
for (let k = 1; k < spans.length; k++) {
|
|
116
|
+
if (spans[k].start < spans[k - 1].end) {
|
|
117
|
+
throw new Error(`edits[${spans[k - 1].i}] and edits[${spans[k].i}] overlap in ${path}. Merge them into one edit or target disjoint regions.`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
let out = "";
|
|
121
|
+
let pos = 0;
|
|
122
|
+
for (const s of spans) {
|
|
123
|
+
out += original.slice(pos, s.start) + s.newText;
|
|
124
|
+
pos = s.end;
|
|
125
|
+
}
|
|
126
|
+
out += original.slice(pos);
|
|
127
|
+
if (out === original) {
|
|
128
|
+
throw new Error(many
|
|
129
|
+
? `No changes made to ${path}. The replacements produced identical content.`
|
|
130
|
+
: `No changes made to ${path}. The replacement produced identical content. This might indicate an issue with special characters or the text not existing as expected.`);
|
|
131
|
+
}
|
|
132
|
+
return bom + (crlf ? out.replace(/\n/g, "\r\n") : out);
|
|
133
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type Workspace } from "./workspace.js";
|
|
2
|
+
/** An overwrite refused because the model read only lines 1..coveredTo of the file (write_file shows it the rest). */
|
|
3
|
+
export declare class PartialReadError extends Error {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly coveredTo: number;
|
|
6
|
+
constructor(message: string, path: string, coveredTo: number);
|
|
7
|
+
}
|
|
8
|
+
export interface FileGuard {
|
|
9
|
+
/** Whether it checks anything (`readBeforeWrite`). */
|
|
10
|
+
readonly enabled: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The model read `path`: lines `start`..`end` of `total` (all of it when
|
|
13
|
+
* left out). `text`, when it is the file's content as read, saves reading
|
|
14
|
+
* it again to fingerprint it. Resolves whether any of those lines had
|
|
15
|
+
* not been read before at this version (undefined when the guard is off).
|
|
16
|
+
*/
|
|
17
|
+
saw(path: string, opts?: {
|
|
18
|
+
start?: number;
|
|
19
|
+
end?: number;
|
|
20
|
+
total?: number;
|
|
21
|
+
text?: string;
|
|
22
|
+
}): Promise<boolean | undefined>;
|
|
23
|
+
/** The model wrote the whole file itself: it knows all of it. */
|
|
24
|
+
wrote(path: string): Promise<void>;
|
|
25
|
+
/** `to` is now a copy of `from` (copied or moved): what was read of `from` counts as read of `to`. */
|
|
26
|
+
copied(from: string, to: string): Promise<void>;
|
|
27
|
+
/** The model changed part of the file (an edit): what it had read stays read, at the new version. */
|
|
28
|
+
edited(path: string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Throws, with a message for the model, when `verb` must not happen:
|
|
31
|
+
* overwrite/edit of a file it has not read (or, for overwrite, read only
|
|
32
|
+
* in part), or any of them on a file changed since it read it. A file
|
|
33
|
+
* that does not exist passes; so does deleting one it never read.
|
|
34
|
+
*/
|
|
35
|
+
check(path: string, verb: "overwrite" | "edit" | "delete"): Promise<void>;
|
|
36
|
+
/** How much of the file as it is now the model has read: all of it, part of it, or none (never, or before it changed). */
|
|
37
|
+
known(path: string): Promise<"all" | "part" | "none">;
|
|
38
|
+
forget(path: string): void;
|
|
39
|
+
/** Forget every file but these (after compaction or a rewind: the ones whose contents the model still has). */
|
|
40
|
+
retain(paths: Iterable<string>): void;
|
|
41
|
+
/** Forget everything. */
|
|
42
|
+
clear(): void;
|
|
43
|
+
}
|
|
44
|
+
export declare function fileGuard(ws: Workspace, opts?: {
|
|
45
|
+
enabled?: boolean;
|
|
46
|
+
}): FileGuard;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Read before write: what the model has seen of each file, so that it does
|
|
2
|
+
// not overwrite, edit or delete a file from a stale or partial view — the
|
|
3
|
+
// user may edit files while it works. One guard per agent, shared by the
|
|
4
|
+
// file tools and run_js's `fs`; when the conversation is compacted or
|
|
5
|
+
// rewound it forgets the files whose contents the model no longer has.
|
|
6
|
+
import { decodeText } from "./workspace.js";
|
|
7
|
+
/** Content larger than this is versioned by time and size alone (not read to fingerprint it). */
|
|
8
|
+
const FINGERPRINT_MAX = 4 * 1024 * 1024;
|
|
9
|
+
/** FNV-1a over the text (or, for binary, the bytes): the same for a file stored as bytes or as its decoded text. */
|
|
10
|
+
function fingerprint(data) {
|
|
11
|
+
const text = typeof data === "string" ? data : decodeText(data);
|
|
12
|
+
let h = 0x811c9dc5;
|
|
13
|
+
if (text !== null)
|
|
14
|
+
for (let i = 0; i < text.length; i++) {
|
|
15
|
+
h ^= text.charCodeAt(i);
|
|
16
|
+
h = Math.imul(h, 0x01000193);
|
|
17
|
+
}
|
|
18
|
+
else
|
|
19
|
+
for (const b of data) {
|
|
20
|
+
h ^= b;
|
|
21
|
+
h = Math.imul(h, 0x01000193);
|
|
22
|
+
}
|
|
23
|
+
return (h >>> 0).toString(36);
|
|
24
|
+
}
|
|
25
|
+
/** Adds [start, end] to merged ranges; whether it covered any line they did not. */
|
|
26
|
+
function addRange(ranges, start, end) {
|
|
27
|
+
if (end < start)
|
|
28
|
+
return false;
|
|
29
|
+
const covered = ranges.some(([a, b]) => a <= start && b >= end);
|
|
30
|
+
ranges.push([start, end]);
|
|
31
|
+
ranges.sort((x, y) => x[0] - y[0]);
|
|
32
|
+
const merged = [];
|
|
33
|
+
for (const r of ranges) {
|
|
34
|
+
const last = merged[merged.length - 1];
|
|
35
|
+
if (last && r[0] <= last[1] + 1)
|
|
36
|
+
last[1] = Math.max(last[1], r[1]);
|
|
37
|
+
else
|
|
38
|
+
merged.push([r[0], r[1]]);
|
|
39
|
+
}
|
|
40
|
+
ranges.splice(0, ranges.length, ...merged);
|
|
41
|
+
return !covered;
|
|
42
|
+
}
|
|
43
|
+
/** An overwrite refused because the model read only lines 1..coveredTo of the file (write_file shows it the rest). */
|
|
44
|
+
export class PartialReadError extends Error {
|
|
45
|
+
path;
|
|
46
|
+
coveredTo;
|
|
47
|
+
constructor(message, path, coveredTo) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.path = path;
|
|
50
|
+
this.coveredTo = coveredTo;
|
|
51
|
+
this.name = "PartialReadError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function fileGuard(ws, opts = {}) {
|
|
55
|
+
const enabled = opts.enabled !== false;
|
|
56
|
+
const seen = new Map();
|
|
57
|
+
const versionOf = async (p, text) => {
|
|
58
|
+
const st = await ws.stat(p);
|
|
59
|
+
if (!st)
|
|
60
|
+
return null;
|
|
61
|
+
// Time and size, plus the content's fingerprint (a same-size change within one tick of a coarse clock still shows).
|
|
62
|
+
const fp = st.size > FINGERPRINT_MAX ? "" : fingerprint(text ?? (await ws.read(p)).data);
|
|
63
|
+
return `${st.mtime ?? ""}:${st.size}:${fp}`;
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
enabled,
|
|
67
|
+
async saw(p, o = {}) {
|
|
68
|
+
if (!enabled)
|
|
69
|
+
return undefined;
|
|
70
|
+
const version = await versionOf(p, o.text).catch(() => null);
|
|
71
|
+
if (!version) {
|
|
72
|
+
seen.delete(p);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
const start = o.start ?? 1;
|
|
76
|
+
const end = o.end ?? Infinity;
|
|
77
|
+
const total = o.total ?? Infinity;
|
|
78
|
+
const was = seen.get(p);
|
|
79
|
+
const same = !!was && was.version === version;
|
|
80
|
+
// Reading on from where it stopped extends what it has read; a new version starts over.
|
|
81
|
+
const coveredTo = same && start <= was.coveredTo + 1 ? Math.max(was.coveredTo, end) : start <= 1 ? end : 0;
|
|
82
|
+
const ranges = same ? was.ranges : [];
|
|
83
|
+
const fresh = addRange(ranges, start, end) || !same;
|
|
84
|
+
seen.set(p, { version, coveredTo, total, ranges });
|
|
85
|
+
return fresh;
|
|
86
|
+
},
|
|
87
|
+
async wrote(p) {
|
|
88
|
+
if (!enabled)
|
|
89
|
+
return;
|
|
90
|
+
const version = await versionOf(p).catch(() => null);
|
|
91
|
+
if (version)
|
|
92
|
+
seen.set(p, { version, coveredTo: Infinity, total: Infinity, ranges: [[1, Infinity]] });
|
|
93
|
+
else
|
|
94
|
+
seen.delete(p);
|
|
95
|
+
},
|
|
96
|
+
async copied(from, to) {
|
|
97
|
+
if (!enabled)
|
|
98
|
+
return;
|
|
99
|
+
const was = seen.get(from);
|
|
100
|
+
const version = was ? await versionOf(to).catch(() => null) : null;
|
|
101
|
+
if (was && version)
|
|
102
|
+
seen.set(to, { ...was, version, ranges: was.ranges.map(([a, b]) => [a, b]) });
|
|
103
|
+
else
|
|
104
|
+
seen.delete(to);
|
|
105
|
+
},
|
|
106
|
+
async edited(p) {
|
|
107
|
+
if (!enabled)
|
|
108
|
+
return;
|
|
109
|
+
const was = seen.get(p);
|
|
110
|
+
const version = await versionOf(p).catch(() => null);
|
|
111
|
+
// The lines moved: what was read stays read, but any later read shows the new text.
|
|
112
|
+
if (version && was)
|
|
113
|
+
seen.set(p, { ...was, version, total: Infinity, ranges: [] });
|
|
114
|
+
else
|
|
115
|
+
seen.delete(p);
|
|
116
|
+
},
|
|
117
|
+
async check(p, verb) {
|
|
118
|
+
if (!enabled)
|
|
119
|
+
return;
|
|
120
|
+
const now = await versionOf(p);
|
|
121
|
+
if (now === null)
|
|
122
|
+
return; // nothing there
|
|
123
|
+
const was = seen.get(p);
|
|
124
|
+
if (!was) {
|
|
125
|
+
if (verb === "delete")
|
|
126
|
+
return;
|
|
127
|
+
throw new Error(`${p} already exists and you have not read it in this conversation: read it with read_file first, then ${verb} it.`);
|
|
128
|
+
}
|
|
129
|
+
if (was.version !== now)
|
|
130
|
+
throw new Error(`${p} has changed since you last read it (the user or another process edited it): read it again with read_file, then ${verb === "delete" ? "decide whether to delete it" : "make your change on the current content"}.`);
|
|
131
|
+
if (verb === "overwrite" && was.coveredTo < was.total) {
|
|
132
|
+
throw new PartialReadError(`You have read only lines 1-${was.coveredTo} of ${p}: read the rest (read_file with offset=${was.coveredTo + 1}) before overwriting it, or change it with edit_file instead.`, p, was.coveredTo);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
async known(p) {
|
|
136
|
+
const was = seen.get(p);
|
|
137
|
+
if (!was)
|
|
138
|
+
return "none";
|
|
139
|
+
const now = await versionOf(p).catch(() => null);
|
|
140
|
+
if (now !== was.version)
|
|
141
|
+
return "none";
|
|
142
|
+
return was.coveredTo >= was.total ? "all" : was.coveredTo > 0 ? "part" : "none";
|
|
143
|
+
},
|
|
144
|
+
forget(p) { seen.delete(p); },
|
|
145
|
+
retain(paths) {
|
|
146
|
+
const keep = new Set(paths);
|
|
147
|
+
for (const p of [...seen.keys()])
|
|
148
|
+
if (!keep.has(p))
|
|
149
|
+
seen.delete(p);
|
|
150
|
+
},
|
|
151
|
+
clear() { seen.clear(); },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Tool } from "./types.js";
|
|
2
|
+
import { type Workspace } from "./workspace.js";
|
|
3
|
+
import { type FileGuard } from "./file-guard.js";
|
|
4
|
+
export interface FileToolsOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Refuse to overwrite or edit an existing file the model has not read in
|
|
7
|
+
* this conversation (or, to overwrite it, read only in part), or that
|
|
8
|
+
* changed since it last read it — by the user, the host or another
|
|
9
|
+
* process — and tell it to read the file again (default true). A new
|
|
10
|
+
* file can always be written.
|
|
11
|
+
*/
|
|
12
|
+
readBeforeWrite?: boolean;
|
|
13
|
+
/** The agent's guard, shared with run_js (made here when not given). */
|
|
14
|
+
guard?: FileGuard;
|
|
15
|
+
}
|
|
16
|
+
export declare function fileTools(ws: Workspace, opts?: FileToolsOptions): Tool[];
|