@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
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
// The chat's file manager: the session's directory as a tree (folders
|
|
2
|
+
// fold, files open), an editor for text files (save with ⌘S), a preview
|
|
3
|
+
// for pictures, download for anything, upload (picker or drop) and
|
|
4
|
+
// delete. Modelled on the harness app's sidebar + editor pane, in one
|
|
5
|
+
// panel beside the transcript.
|
|
6
|
+
import { buildZip } from "./index.js";
|
|
7
|
+
import { h, I, modalConfirm, modalInput, downloadBlob, renderMD } from "./dom.js";
|
|
8
|
+
const IMAGE_RE = /\.(png|jpe?g|gif|webp|svg|bmp|avif)$/i;
|
|
9
|
+
const basename = (p) => p.slice(p.lastIndexOf("/") + 1);
|
|
10
|
+
const dirname = (p) => p.slice(0, p.lastIndexOf("/")) || "/";
|
|
11
|
+
function buildTree(files) {
|
|
12
|
+
const root = { name: "", path: "/", folder: true, children: [] };
|
|
13
|
+
const folders = new Map([["/", root]]);
|
|
14
|
+
const folderOf = (path) => {
|
|
15
|
+
let f = folders.get(path);
|
|
16
|
+
if (f)
|
|
17
|
+
return f;
|
|
18
|
+
const parent = folderOf(dirname(path));
|
|
19
|
+
f = { name: basename(path), path, folder: true, children: [] };
|
|
20
|
+
parent.children.push(f);
|
|
21
|
+
folders.set(path, f);
|
|
22
|
+
return f;
|
|
23
|
+
};
|
|
24
|
+
for (const s of files)
|
|
25
|
+
folderOf(dirname(s.path)).children.push({ name: basename(s.path), path: s.path, folder: false, stat: s, children: [] });
|
|
26
|
+
const sort = (n) => { n.children.sort((a, b) => Number(b.folder) - Number(a.folder) || a.name.localeCompare(b.name)); n.children.forEach(sort); };
|
|
27
|
+
sort(root);
|
|
28
|
+
return root;
|
|
29
|
+
}
|
|
30
|
+
/** A free path for `name` in `dir` (`-2`, `-3`… when taken). */
|
|
31
|
+
export async function freePath(ws, dir, name) {
|
|
32
|
+
const base = dir === "/" ? "" : dir;
|
|
33
|
+
const dot = name.lastIndexOf(".");
|
|
34
|
+
const stem = dot > 0 ? name.slice(0, dot) : name, ext = dot > 0 ? name.slice(dot) : "";
|
|
35
|
+
for (let i = 1; i < 1000; i++) {
|
|
36
|
+
const p = `${base}/${i === 1 ? name : `${stem}-${i}${ext}`}`;
|
|
37
|
+
if (!(await ws.exists(p)))
|
|
38
|
+
return p;
|
|
39
|
+
}
|
|
40
|
+
return `${base}/${Date.now()}-${name}`;
|
|
41
|
+
}
|
|
42
|
+
export function filesPanel(opts) {
|
|
43
|
+
const { t, root, notify, editorHost } = opts;
|
|
44
|
+
let ws = null;
|
|
45
|
+
let visible = false;
|
|
46
|
+
let active = null;
|
|
47
|
+
let draft = null;
|
|
48
|
+
let loaded = null;
|
|
49
|
+
let previewURL = null;
|
|
50
|
+
let selectedDir = "/";
|
|
51
|
+
const collapsed = new Set(["/.agent"]);
|
|
52
|
+
let offChange = null;
|
|
53
|
+
let gen = 0;
|
|
54
|
+
let lastFiles = null;
|
|
55
|
+
const MAX_EDIT = 2 * 1024 * 1024;
|
|
56
|
+
let refreshTimer = null;
|
|
57
|
+
/** Many writes in a burst (a skills zip unpacking, a folder delete) redraw once. */
|
|
58
|
+
const scheduleRefresh = () => { if (refreshTimer)
|
|
59
|
+
return; refreshTimer = setTimeout(() => { refreshTimer = null; void refresh(); }, 150); };
|
|
60
|
+
const changedPaths = new Set();
|
|
61
|
+
let disposed = false;
|
|
62
|
+
const el = h("div", "ra-fp");
|
|
63
|
+
el.setAttribute("role", "complementary");
|
|
64
|
+
el.style.display = "none";
|
|
65
|
+
const head = h("div", "ra-fph");
|
|
66
|
+
const title = h("span", "ra-fpt", t.files);
|
|
67
|
+
const ibtn = (icon, label, onClick) => { const b = h("button", "ra-ib"); b.type = "button"; b.innerHTML = icon; b.title = label; b.setAttribute("aria-label", label); b.addEventListener("click", onClick); return b; };
|
|
68
|
+
const fileInput = h("input");
|
|
69
|
+
fileInput.type = "file";
|
|
70
|
+
fileInput.multiple = true;
|
|
71
|
+
fileInput.style.display = "none";
|
|
72
|
+
const uploadBtn = ibtn(I.upload(), t.upload, () => fileInput.click());
|
|
73
|
+
const zipBtn = ibtn(I.download(), t.downloadZip, () => void downloadFolder("/"));
|
|
74
|
+
const newBtn = ibtn(I.filePlus(14), t.newFile, () => void newFile());
|
|
75
|
+
const refreshBtn = ibtn(I.refresh(), t.refresh, () => void refresh());
|
|
76
|
+
const closeBtn = ibtn(I.x(14), t.close, () => void close());
|
|
77
|
+
head.append(title, h("span", "ra-sp"), zipBtn, uploadBtn, newBtn, refreshBtn, closeBtn);
|
|
78
|
+
const tree = h("div", "ra-tree");
|
|
79
|
+
const editor = h("div", "ra-ed");
|
|
80
|
+
editor.style.display = "none";
|
|
81
|
+
const edHead = h("div", "ra-edh");
|
|
82
|
+
const edPath = h("span", "ra-edp");
|
|
83
|
+
const edRO = h("span", "ra-edro");
|
|
84
|
+
edRO.innerHTML = I.lock(11) + `<span>${t.readOnly}</span>`;
|
|
85
|
+
edRO.style.display = "none";
|
|
86
|
+
const edActs = h("div", "ra-eda");
|
|
87
|
+
const saveBtn = h("button", "ra-btn xs solid");
|
|
88
|
+
saveBtn.type = "button";
|
|
89
|
+
saveBtn.innerHTML = I.save() + t.save;
|
|
90
|
+
saveBtn.style.display = "none";
|
|
91
|
+
saveBtn.addEventListener("click", () => void save());
|
|
92
|
+
// Word wrap (remembered) and a Markdown preview rendered by the same renderer as the chat's replies.
|
|
93
|
+
const WRAP_KEY = "__runapps_agent_files:wrap";
|
|
94
|
+
let wrap = (() => { try {
|
|
95
|
+
return localStorage.getItem(WRAP_KEY) !== "0";
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return true;
|
|
99
|
+
} })();
|
|
100
|
+
let previewing = false;
|
|
101
|
+
const wrapBtn = ibtn(I.wrap(), t.wrap, () => { wrap = !wrap; try {
|
|
102
|
+
localStorage.setItem(WRAP_KEY, wrap ? "1" : "0");
|
|
103
|
+
}
|
|
104
|
+
catch { /* private mode */ } applyWrap(); });
|
|
105
|
+
const mdBtn = ibtn(I.eye(), t.preview, () => { previewing = !previewing; applyView(); });
|
|
106
|
+
const md = h("div", "ra-md ra-edmd");
|
|
107
|
+
md.style.display = "none";
|
|
108
|
+
md.addEventListener("click", (e) => {
|
|
109
|
+
const a = e.target.closest("a[href]");
|
|
110
|
+
const href = a?.getAttribute("href") ?? "";
|
|
111
|
+
if (!a || !href.startsWith("/") || href.startsWith("//") || !ws)
|
|
112
|
+
return;
|
|
113
|
+
e.preventDefault();
|
|
114
|
+
const target = ws;
|
|
115
|
+
void target.exists(href).then((ok) => { if (ok && target === ws)
|
|
116
|
+
void openFile(href); });
|
|
117
|
+
});
|
|
118
|
+
const applyWrap = () => { ta.classList.toggle("ra-wrap", wrap); wrapBtn.classList.toggle("on", wrap); };
|
|
119
|
+
const isMarkdown = (path) => !!path && /\.(md|markdown|mdx)$/i.test(path);
|
|
120
|
+
const applyView = () => {
|
|
121
|
+
const text = loaded !== null;
|
|
122
|
+
mdBtn.style.display = text && isMarkdown(active) ? "" : "none";
|
|
123
|
+
if (!text || !isMarkdown(active))
|
|
124
|
+
previewing = false;
|
|
125
|
+
mdBtn.innerHTML = previewing ? I.pen() : I.eye();
|
|
126
|
+
mdBtn.title = previewing ? t.edit : t.preview;
|
|
127
|
+
mdBtn.setAttribute("aria-label", mdBtn.title);
|
|
128
|
+
mdBtn.classList.toggle("on", previewing);
|
|
129
|
+
if (previewing)
|
|
130
|
+
renderMD(md, draft ?? ta.value, false);
|
|
131
|
+
md.style.display = previewing ? "" : "none";
|
|
132
|
+
ta.style.display = text && !previewing ? "" : "none";
|
|
133
|
+
};
|
|
134
|
+
const dlBtn = ibtn(I.download(), t.download, () => { if (active)
|
|
135
|
+
void download(active); });
|
|
136
|
+
const delBtn = ibtn(I.trash(), t.delete, () => { if (active)
|
|
137
|
+
void remove(active); });
|
|
138
|
+
const edClose = ibtn(I.x(14), t.close, () => void closeFile());
|
|
139
|
+
edActs.append(saveBtn, wrapBtn, mdBtn, dlBtn, delBtn, edClose);
|
|
140
|
+
edHead.append(edPath, edRO, edActs);
|
|
141
|
+
const edBody = h("div", "ra-edb");
|
|
142
|
+
const ta = h("textarea", "ra-edta");
|
|
143
|
+
ta.spellcheck = false;
|
|
144
|
+
ta.setAttribute("aria-label", t.files);
|
|
145
|
+
const preview = h("div", "ra-edpv");
|
|
146
|
+
edBody.append(ta, md, preview);
|
|
147
|
+
applyWrap();
|
|
148
|
+
editor.append(edHead, edBody);
|
|
149
|
+
const drop = h("div", "ra-fpdrop", t.upload);
|
|
150
|
+
el.append(head, tree, fileInput, drop);
|
|
151
|
+
editorHost.append(editor);
|
|
152
|
+
/** A read-only file: marked in the header, no download or delete, the text cannot be changed. */
|
|
153
|
+
const setRO = (ro) => { edRO.style.display = ro ? "" : "none"; delBtn.style.display = dlBtn.style.display = ro ? "none" : ""; };
|
|
154
|
+
const setDraft = (v) => { draft = v; saveBtn.style.display = v === null ? "none" : ""; };
|
|
155
|
+
ta.addEventListener("input", () => setDraft(ta.value === loaded ? null : ta.value));
|
|
156
|
+
ta.addEventListener("keydown", (e) => {
|
|
157
|
+
if ((e.metaKey || e.ctrlKey) && e.key === "s") {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
void save();
|
|
160
|
+
}
|
|
161
|
+
if (e.key === "Tab" && !e.shiftKey) {
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
const s = ta.selectionStart;
|
|
164
|
+
ta.setRangeText(" ", s, ta.selectionEnd, "end");
|
|
165
|
+
ta.dispatchEvent(new Event("input"));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
const confirmDiscard = async () => draft === null || modalConfirm(root, t.discard, "", { ok: t.ok, cancel: t.cancel, danger: true });
|
|
169
|
+
/** Read-only: a file the workspace refuses to write, a folder whose files are all such (the generated skills folder). */
|
|
170
|
+
const isRO = (path, folder) => {
|
|
171
|
+
if (!ws)
|
|
172
|
+
return false;
|
|
173
|
+
if (!folder)
|
|
174
|
+
return ws.isReadOnly(path);
|
|
175
|
+
const under = (lastFiles ?? []).filter((f) => f.path.startsWith(path + "/"));
|
|
176
|
+
return under.length > 0 && under.every((f) => ws.isReadOnly(f.path));
|
|
177
|
+
};
|
|
178
|
+
/** Where uploads and new files go: the selected folder, or the top when that folder is read-only. */
|
|
179
|
+
const writableDir = () => (selectedDir !== "/" && isRO(selectedDir, true) ? "/" : selectedDir);
|
|
180
|
+
const render = (node, depth, frag, parentRO = false) => {
|
|
181
|
+
for (const c of node.children) {
|
|
182
|
+
const row = h("div", "ra-fr" + (c.path === active ? " ra-cur" : ""));
|
|
183
|
+
row.style.paddingLeft = `${depth * 14 + 6}px`;
|
|
184
|
+
row.dataset.path = c.path;
|
|
185
|
+
const open = c.folder && !collapsed.has(c.path);
|
|
186
|
+
const chev = h("span", "ra-frc");
|
|
187
|
+
chev.innerHTML = c.folder ? (open ? I.chevDown() : I.chev(12)) : "";
|
|
188
|
+
const icon = h("span", "ra-fri");
|
|
189
|
+
icon.innerHTML = c.folder ? (open ? I.folderOpen() : I.folder()) : I.file();
|
|
190
|
+
const name = h("span", "ra-frn", c.name);
|
|
191
|
+
name.title = c.path;
|
|
192
|
+
row.append(chev, icon, name);
|
|
193
|
+
if (changedPaths.has(c.path)) {
|
|
194
|
+
const dot = h("span", "ra-dot");
|
|
195
|
+
dot.title = t.changedByAgent;
|
|
196
|
+
row.append(dot);
|
|
197
|
+
}
|
|
198
|
+
const ro = parentRO || isRO(c.path, c.folder);
|
|
199
|
+
if (ro) {
|
|
200
|
+
// Read-only (generated): nothing to download or delete; the top of it says why.
|
|
201
|
+
row.classList.add("ra-ro");
|
|
202
|
+
row.title = `${c.path} · ${t.readOnly}`;
|
|
203
|
+
if (!parentRO) {
|
|
204
|
+
const lk = h("span", "ra-frl");
|
|
205
|
+
lk.innerHTML = I.lock();
|
|
206
|
+
lk.title = t.readOnly;
|
|
207
|
+
row.append(lk);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const acts = h("span", "ra-fra");
|
|
212
|
+
acts.append(c.folder ? ibtn(I.download(13), t.downloadZip, () => void downloadFolder(c.path)) : ibtn(I.download(13), t.download, () => void download(c.path)));
|
|
213
|
+
acts.append(ibtn(I.trash(12), t.delete, () => void remove(c.path)));
|
|
214
|
+
row.append(acts);
|
|
215
|
+
}
|
|
216
|
+
row.addEventListener("click", (e) => {
|
|
217
|
+
if (e.target.closest(".ra-fra"))
|
|
218
|
+
return;
|
|
219
|
+
if (c.folder) {
|
|
220
|
+
selectedDir = c.path;
|
|
221
|
+
if (!collapsed.delete(c.path))
|
|
222
|
+
collapsed.add(c.path);
|
|
223
|
+
draw();
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
selectedDir = dirname(c.path);
|
|
227
|
+
void openFile(c.path);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
frag.append(row);
|
|
231
|
+
if (c.folder && open)
|
|
232
|
+
render(c, depth + 1, frag, ro);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
/** Draws the tree from the last listing (folding and highlighting need no new listing). */
|
|
236
|
+
const draw = () => {
|
|
237
|
+
if (!lastFiles)
|
|
238
|
+
return;
|
|
239
|
+
const frag = document.createDocumentFragment();
|
|
240
|
+
if (!lastFiles.length)
|
|
241
|
+
frag.append(h("div", "ra-fpe", t.noFiles));
|
|
242
|
+
else
|
|
243
|
+
render(buildTree(lastFiles), 0, frag);
|
|
244
|
+
tree.replaceChildren(frag);
|
|
245
|
+
};
|
|
246
|
+
/** Lists the directory again; an answer that arrives after a newer request (or another workspace) is dropped. */
|
|
247
|
+
const refresh = async () => {
|
|
248
|
+
if (!ws || !visible || disposed)
|
|
249
|
+
return;
|
|
250
|
+
const mine = ++gen, target = ws;
|
|
251
|
+
try {
|
|
252
|
+
const files = await target.list("/");
|
|
253
|
+
if (mine !== gen || target !== ws || disposed)
|
|
254
|
+
return;
|
|
255
|
+
lastFiles = files;
|
|
256
|
+
draw();
|
|
257
|
+
}
|
|
258
|
+
catch (err) {
|
|
259
|
+
if (mine !== gen || disposed)
|
|
260
|
+
return;
|
|
261
|
+
tree.replaceChildren(h("div", "ra-fpe", err.message));
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
const showEditor = (on) => { editor.style.display = on ? "" : "none"; root.classList.toggle("ra-editing", on); };
|
|
265
|
+
const openFile = async (path) => {
|
|
266
|
+
if (!ws)
|
|
267
|
+
return;
|
|
268
|
+
if (path !== active && !(await confirmDiscard()))
|
|
269
|
+
return;
|
|
270
|
+
active = path;
|
|
271
|
+
changedPaths.delete(path);
|
|
272
|
+
setDraft(null);
|
|
273
|
+
edPath.textContent = path;
|
|
274
|
+
if (previewURL) {
|
|
275
|
+
URL.revokeObjectURL(previewURL);
|
|
276
|
+
previewURL = null;
|
|
277
|
+
}
|
|
278
|
+
preview.replaceChildren();
|
|
279
|
+
showEditor(true);
|
|
280
|
+
try {
|
|
281
|
+
const r = await ws.read(path);
|
|
282
|
+
if (active !== path)
|
|
283
|
+
return;
|
|
284
|
+
const image = IMAGE_RE.test(path) || /^image\//.test(r.contentType ?? "");
|
|
285
|
+
let text = null;
|
|
286
|
+
if (!image) {
|
|
287
|
+
if (typeof r.data === "string")
|
|
288
|
+
text = r.data;
|
|
289
|
+
else {
|
|
290
|
+
try {
|
|
291
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(r.data);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
text = null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// A PDF opens in the browser's own viewer (the agent reads its text through pdf.js).
|
|
299
|
+
if (/\.pdf$/i.test(path) || r.contentType === "application/pdf") {
|
|
300
|
+
loaded = null;
|
|
301
|
+
ta.style.display = "none";
|
|
302
|
+
md.style.display = "none";
|
|
303
|
+
mdBtn.style.display = "none";
|
|
304
|
+
preview.style.display = "";
|
|
305
|
+
previewURL = URL.createObjectURL(new Blob([r.data], { type: "application/pdf" }));
|
|
306
|
+
const frame = h("iframe", "ra-edpdf");
|
|
307
|
+
frame.src = previewURL;
|
|
308
|
+
frame.title = basename(path);
|
|
309
|
+
wrapBtn.style.display = "none";
|
|
310
|
+
preview.replaceChildren(frame);
|
|
311
|
+
setRO(ws.isReadOnly(path));
|
|
312
|
+
draw();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
wrapBtn.style.display = "";
|
|
316
|
+
if (text !== null && r.size > MAX_EDIT)
|
|
317
|
+
text = null;
|
|
318
|
+
const readOnly = ws.isReadOnly(path);
|
|
319
|
+
setRO(readOnly);
|
|
320
|
+
if (text !== null) {
|
|
321
|
+
loaded = text;
|
|
322
|
+
ta.value = text;
|
|
323
|
+
preview.style.display = "none";
|
|
324
|
+
ta.readOnly = readOnly;
|
|
325
|
+
ta.title = readOnly ? t.readOnly : "";
|
|
326
|
+
applyView();
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
loaded = null;
|
|
330
|
+
ta.style.display = "none";
|
|
331
|
+
md.style.display = "none";
|
|
332
|
+
mdBtn.style.display = "none";
|
|
333
|
+
preview.style.display = "";
|
|
334
|
+
if (image) {
|
|
335
|
+
const blob = new Blob([r.data], { type: r.contentType || "image/*" });
|
|
336
|
+
previewURL = URL.createObjectURL(blob);
|
|
337
|
+
const img = h("img");
|
|
338
|
+
img.src = previewURL;
|
|
339
|
+
img.alt = basename(path);
|
|
340
|
+
preview.append(img);
|
|
341
|
+
}
|
|
342
|
+
else
|
|
343
|
+
preview.append(h("div", "ra-fpe", r.size > MAX_EDIT && !image ? t.tooBig : t.noPreview));
|
|
344
|
+
preview.append(h("div", "ra-fpe", `${r.size} B`));
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
loaded = null;
|
|
349
|
+
ta.style.display = "none";
|
|
350
|
+
md.style.display = "none";
|
|
351
|
+
mdBtn.style.display = "none";
|
|
352
|
+
preview.style.display = "";
|
|
353
|
+
preview.replaceChildren(h("div", "ra-fpe", err.message));
|
|
354
|
+
}
|
|
355
|
+
draw();
|
|
356
|
+
};
|
|
357
|
+
const closeFile = async () => {
|
|
358
|
+
if (!active)
|
|
359
|
+
return true;
|
|
360
|
+
if (!(await confirmDiscard()))
|
|
361
|
+
return false;
|
|
362
|
+
active = null;
|
|
363
|
+
loaded = null;
|
|
364
|
+
setDraft(null);
|
|
365
|
+
if (previewURL) {
|
|
366
|
+
URL.revokeObjectURL(previewURL);
|
|
367
|
+
previewURL = null;
|
|
368
|
+
}
|
|
369
|
+
showEditor(false);
|
|
370
|
+
await refresh();
|
|
371
|
+
return true;
|
|
372
|
+
};
|
|
373
|
+
const save = async () => {
|
|
374
|
+
if (!ws || !active || draft === null)
|
|
375
|
+
return;
|
|
376
|
+
const path = active, text = draft, target = ws;
|
|
377
|
+
try {
|
|
378
|
+
// Changed under the editor since it was opened (the agent, another tab): ask before overwriting.
|
|
379
|
+
const now = await target.readText(path).catch(() => null);
|
|
380
|
+
if (now !== null && loaded !== null && now !== loaded && !(await modalConfirm(root, t.conflict, path, { ok: t.save, cancel: t.cancel, danger: true })))
|
|
381
|
+
return;
|
|
382
|
+
await target.write(path, text);
|
|
383
|
+
if (active !== path || target !== ws)
|
|
384
|
+
return;
|
|
385
|
+
loaded = text;
|
|
386
|
+
// Typing that went on while saving is still unsaved.
|
|
387
|
+
setDraft(ta.value === text ? null : ta.value);
|
|
388
|
+
notify(`${t.saved}: ${path}`);
|
|
389
|
+
opts.onChange?.();
|
|
390
|
+
}
|
|
391
|
+
catch (err) {
|
|
392
|
+
notify(err.message, "error");
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
const download = async (path) => {
|
|
396
|
+
if (!ws)
|
|
397
|
+
return;
|
|
398
|
+
try {
|
|
399
|
+
const r = await ws.read(path);
|
|
400
|
+
downloadBlob(basename(path), r.data, r.contentType);
|
|
401
|
+
}
|
|
402
|
+
catch (err) {
|
|
403
|
+
notify(err.message, "error");
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
/** A folder (or the whole directory) as a zip, built in the browser. */
|
|
407
|
+
const downloadFolder = async (path) => {
|
|
408
|
+
if (!ws)
|
|
409
|
+
return;
|
|
410
|
+
try {
|
|
411
|
+
const prefix = path === "/" ? "/" : path + "/";
|
|
412
|
+
const files = (await ws.list(path)).filter((f) => f.path.startsWith(prefix));
|
|
413
|
+
if (!files.length) {
|
|
414
|
+
notify(t.noFiles, "warning");
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
notify(`${t.zipping} ${files.length}…`);
|
|
418
|
+
const entries = [];
|
|
419
|
+
for (const f of files) {
|
|
420
|
+
const r = await ws.read(f.path);
|
|
421
|
+
entries.push({ path: f.path.slice(prefix.length), data: r.data, mtime: f.mtime });
|
|
422
|
+
}
|
|
423
|
+
const zip = await buildZip(entries);
|
|
424
|
+
downloadBlob(`${path === "/" ? (ws.id || "directory") : basename(path)}.zip`, zip, "application/zip");
|
|
425
|
+
}
|
|
426
|
+
catch (err) {
|
|
427
|
+
notify(err.message, "error");
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
const remove = async (path) => {
|
|
431
|
+
if (!ws)
|
|
432
|
+
return;
|
|
433
|
+
if (!(await modalConfirm(root, t.delete, t.confirmDeleteFile.replace("{path}", path), { ok: t.delete, cancel: t.cancel, danger: true })))
|
|
434
|
+
return;
|
|
435
|
+
const failed = [];
|
|
436
|
+
try {
|
|
437
|
+
const targets = (await ws.isFolder(path)) ? (await ws.list(path)).map((f) => f.path).filter((p) => p !== path) : [path];
|
|
438
|
+
for (const p of targets) {
|
|
439
|
+
if (ws.isReadOnly(p)) {
|
|
440
|
+
failed.push(p);
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
try {
|
|
444
|
+
await ws.delete(p);
|
|
445
|
+
}
|
|
446
|
+
catch {
|
|
447
|
+
failed.push(p);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
catch (err) {
|
|
452
|
+
notify(err.message, "error");
|
|
453
|
+
}
|
|
454
|
+
if (active && (active === path || active.startsWith(path + "/")) && !(await ws.exists(active))) {
|
|
455
|
+
active = null;
|
|
456
|
+
loaded = null;
|
|
457
|
+
setDraft(null);
|
|
458
|
+
showEditor(false);
|
|
459
|
+
}
|
|
460
|
+
if (failed.length)
|
|
461
|
+
notify(`${t.readOnly}: ${failed.slice(0, 3).join(", ")}${failed.length > 3 ? ` +${failed.length - 3}` : ""}`, "warning");
|
|
462
|
+
opts.onChange?.();
|
|
463
|
+
await refresh();
|
|
464
|
+
};
|
|
465
|
+
const newFile = async () => {
|
|
466
|
+
if (!ws)
|
|
467
|
+
return;
|
|
468
|
+
const dir = writableDir();
|
|
469
|
+
const base = dir === "/" ? "/" : dir + "/";
|
|
470
|
+
const p = await modalInput(root, t.newFilePath, { ok: t.ok, cancel: t.cancel, initial: `${base}untitled.md` });
|
|
471
|
+
if (!p)
|
|
472
|
+
return;
|
|
473
|
+
const path = "/" + p.replace(/^\/+/, "");
|
|
474
|
+
try {
|
|
475
|
+
if (!(await ws.exists(path)))
|
|
476
|
+
await ws.write(path, "");
|
|
477
|
+
opts.onChange?.();
|
|
478
|
+
await refresh();
|
|
479
|
+
await openFile(path);
|
|
480
|
+
ta.focus();
|
|
481
|
+
}
|
|
482
|
+
catch (err) {
|
|
483
|
+
notify(err.message, "error");
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
const upload = async (list) => {
|
|
487
|
+
if (!ws || !list?.length)
|
|
488
|
+
return;
|
|
489
|
+
let last = null;
|
|
490
|
+
for (const file of Array.from(list)) {
|
|
491
|
+
try {
|
|
492
|
+
const path = await freePath(ws, writableDir(), basename(file.name.replace(/\\/g, "/")) || "file");
|
|
493
|
+
await ws.write(path, new Uint8Array(await file.arrayBuffer()), file.type || undefined);
|
|
494
|
+
last = path;
|
|
495
|
+
}
|
|
496
|
+
catch (err) {
|
|
497
|
+
notify(err.message, "error");
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
opts.onChange?.();
|
|
501
|
+
await refresh();
|
|
502
|
+
if (last && list.length === 1)
|
|
503
|
+
await openFile(last);
|
|
504
|
+
};
|
|
505
|
+
fileInput.addEventListener("change", () => { const picked = Array.from(fileInput.files ?? []); fileInput.value = ""; void upload(picked); });
|
|
506
|
+
let dragDepth = 0;
|
|
507
|
+
el.addEventListener("dragenter", (e) => { if (!e.dataTransfer?.types.includes("Files"))
|
|
508
|
+
return; e.preventDefault(); e.stopPropagation(); dragDepth++; el.dataset.drop = "1"; });
|
|
509
|
+
el.addEventListener("dragover", (e) => { if (el.dataset.drop) {
|
|
510
|
+
e.preventDefault();
|
|
511
|
+
e.stopPropagation();
|
|
512
|
+
e.dataTransfer.dropEffect = "copy";
|
|
513
|
+
} });
|
|
514
|
+
el.addEventListener("dragleave", (e) => { if (el.dataset.drop) {
|
|
515
|
+
e.stopPropagation();
|
|
516
|
+
if (--dragDepth <= 0) {
|
|
517
|
+
dragDepth = 0;
|
|
518
|
+
delete el.dataset.drop;
|
|
519
|
+
}
|
|
520
|
+
} });
|
|
521
|
+
el.addEventListener("drop", (e) => { if (!el.dataset.drop)
|
|
522
|
+
return; e.preventDefault(); e.stopPropagation(); dragDepth = 0; delete el.dataset.drop; void upload(e.dataTransfer?.files); });
|
|
523
|
+
const open = async (path) => {
|
|
524
|
+
visible = true;
|
|
525
|
+
el.style.display = "";
|
|
526
|
+
root.classList.add("ra-files-open");
|
|
527
|
+
opts.onShow?.(true);
|
|
528
|
+
await refresh();
|
|
529
|
+
if (path)
|
|
530
|
+
await openFile(path);
|
|
531
|
+
};
|
|
532
|
+
/** Hides the tree and closes the open file with it (unsaved changes are asked about; declining keeps both open). */
|
|
533
|
+
const close = async () => {
|
|
534
|
+
if (!(await closeFile()))
|
|
535
|
+
return;
|
|
536
|
+
visible = false;
|
|
537
|
+
el.style.display = "none";
|
|
538
|
+
root.classList.remove("ra-files-open");
|
|
539
|
+
opts.onShow?.(false);
|
|
540
|
+
};
|
|
541
|
+
return {
|
|
542
|
+
el,
|
|
543
|
+
get visible() { return visible; },
|
|
544
|
+
open,
|
|
545
|
+
close,
|
|
546
|
+
toggle: () => (visible ? close() : open()),
|
|
547
|
+
setWorkspace(next) {
|
|
548
|
+
if (next === ws)
|
|
549
|
+
return;
|
|
550
|
+
// An unsaved edit is written to the file it belongs to before the panel moves on, not dropped.
|
|
551
|
+
if (ws && active && draft !== null) {
|
|
552
|
+
const [old, path, text] = [ws, active, draft];
|
|
553
|
+
void old.write(path, text).then(() => notify(`${t.autoSaved}: ${path}`), (err) => notify(err.message, "error"));
|
|
554
|
+
}
|
|
555
|
+
offChange?.();
|
|
556
|
+
offChange = null;
|
|
557
|
+
ws = next;
|
|
558
|
+
if (next)
|
|
559
|
+
offChange = next.onChange((c) => {
|
|
560
|
+
if (!visible)
|
|
561
|
+
return;
|
|
562
|
+
scheduleRefresh();
|
|
563
|
+
if (c.path !== active || draft !== null)
|
|
564
|
+
return;
|
|
565
|
+
// The open file changed under the editor: reload it, or close it when it is gone (unless the user has unsaved edits).
|
|
566
|
+
if (c.kind === "write")
|
|
567
|
+
void openFile(c.path);
|
|
568
|
+
else {
|
|
569
|
+
active = null;
|
|
570
|
+
loaded = null;
|
|
571
|
+
showEditor(false);
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
if (previewURL) {
|
|
575
|
+
URL.revokeObjectURL(previewURL);
|
|
576
|
+
previewURL = null;
|
|
577
|
+
}
|
|
578
|
+
lastFiles = null;
|
|
579
|
+
active = null;
|
|
580
|
+
loaded = null;
|
|
581
|
+
setDraft(null);
|
|
582
|
+
showEditor(false);
|
|
583
|
+
changedPaths.clear();
|
|
584
|
+
selectedDir = "/";
|
|
585
|
+
if (!next) {
|
|
586
|
+
visible = false;
|
|
587
|
+
el.style.display = "none";
|
|
588
|
+
root.classList.remove("ra-files-open");
|
|
589
|
+
}
|
|
590
|
+
else
|
|
591
|
+
void refresh();
|
|
592
|
+
},
|
|
593
|
+
refresh,
|
|
594
|
+
// The agent touched a path: mark it (onChange already redraws and reloads the open file).
|
|
595
|
+
changed(path) {
|
|
596
|
+
changedPaths.add(path);
|
|
597
|
+
draw();
|
|
598
|
+
},
|
|
599
|
+
download,
|
|
600
|
+
downloadFolder,
|
|
601
|
+
dispose() {
|
|
602
|
+
disposed = true;
|
|
603
|
+
offChange?.();
|
|
604
|
+
if (refreshTimer)
|
|
605
|
+
clearTimeout(refreshTimer);
|
|
606
|
+
if (previewURL)
|
|
607
|
+
URL.revokeObjectURL(previewURL);
|
|
608
|
+
el.remove();
|
|
609
|
+
editor.remove();
|
|
610
|
+
root.classList.remove("ra-editing");
|
|
611
|
+
},
|
|
612
|
+
};
|
|
613
|
+
}
|