WebOrbiton
v1.5.0

AtheraStudio Code

1,229 lines · 52.1 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = window.Athera;
  5. var util = A.util;
  6. var ai = A.ai;
  7. var t = ai.t;
  8. ​
  9. var PRIVATE = ".private";
  10. var PRIVATE_FILES = ["Summary.md", "Conversation.md", "plan.md", "bugs.md", "instructions.md", "design.md"];
  11. var PRIVATE_DIRS = ["crawler", "sandbox", "recipes"];
  12. var TEXT_EXT = ["html", "htm", "php", "phtml", "inc", "css", "scss", "sass", "less", "js", "mjs", "cjs", "ts", "jsx", "tsx",
  13. "json", "jsonc", "md", "markdown", "txt", "xml", "svg", "sql", "csv", "tsv", "yml", "yaml", "ini", "conf", "twig", "tpl",
  14. "vue", "svelte", "webmanifest", "htaccess", "gitignore", "editorconfig", "example"];
  15. var TEXT_NAMES = [".htaccess", ".gitignore", ".editorconfig", ".user.ini", "robots.txt", "humans.txt", "LICENSE", "README"];
  16. var RESERVED = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\..*)?$/i;
  17. var READ_MAX = 60000;
  18. var READ_LINES = 600;
  19. var WRITE_MAX = 512 * 1024;
  20. var OVERWRITE_FREE = 8000;
  21. var LIST_MAX = 400;
  22. var SEARCH_MAX = 100;
  23. var SEARCH_FILES = 3000;
  24. var SEARCH_FILE_MAX = 1024 * 1024;
  25. var SNAP_MAX = 80;
  26. var SNAP_BYTES = 8 * 1024 * 1024;
  27. var DIFF_LIMIT = 1500;
  28. var FETCH_MAX = 40000;
  29. ​
  30. function toolError(key, params) {
  31. var e = new Error(t(key, params));
  32. e.tool = true;
  33. return e;
  34. }
  35. ​
  36. function provider() {
  37. if (!A.fs.provider) throw toolError("ai.tool.err_no_folder");
  38. return A.fs.provider;
  39. }
  40. ​
  41. function cleanPath(raw, allowRoot) {
  42. var p = String(raw == null ? "" : raw).trim().replace(/^\.\/+/, "").replace(/\/+$/, "");
  43. if (!p) {
  44. if (allowRoot) return "";
  45. throw toolError("ai.tool.err_path_empty");
  46. }
  47. if (p.length > 260) throw toolError("ai.tool.err_path_long");
  48. if (/^\//.test(p) || /^~/.test(p) || /^[a-z][a-z0-9+.-]*:/i.test(p)) throw toolError("ai.tool.err_path_absolute", { path: p });
  49. if (/[\u0000-\u001f<>:"|?*\\]/.test(p)) throw toolError("ai.tool.err_path_chars", { path: p });
  50. var segs = p.split("/");
  51. segs.forEach(function (s) {
  52. if (!s || s === "." || s === "..") throw toolError("ai.tool.err_path_dots", { path: p });
  53. if (RESERVED.test(s) || /[. ]$/.test(s)) throw toolError("ai.tool.err_path_reserved", { path: p });
  54. });
  55. return segs.join("/");
  56. }
  57. ​
  58. function isPrivate(path) {
  59. return path === PRIVATE || path.indexOf(PRIVATE + "/") === 0;
  60. }
  61. ​
  62. function inHidden(path) {
  63. return path.split("/").some(function (s) { return A.fs.HIDDEN_DIRS.indexOf(s) >= 0; });
  64. }
  65. ​
  66. function guardRead(raw) {
  67. var p = cleanPath(raw);
  68. if (ai.isExcluded(p)) throw toolError("ai.tool.err_excluded", { path: p });
  69. if (A.fs.isBinary(p)) throw toolError("ai.tool.err_binary", { path: p });
  70. return p;
  71. }
  72. ​
  73. function guardWrite(raw) {
  74. var p = cleanPath(raw);
  75. if (ai.isExcluded(p)) throw toolError("ai.tool.err_excluded", { path: p });
  76. if (inHidden(p)) throw toolError("ai.tool.err_hidden", { path: p });
  77. var name = util.basename(p);
  78. if (TEXT_NAMES.indexOf(name) < 0 && TEXT_EXT.indexOf(util.ext(name)) < 0) throw toolError("ai.tool.err_ext", { path: p });
  79. return p;
  80. }
  81. ​
  82. function useMemory(path) {
  83. return isPrivate(path) && !!A.fs.provider && A.fs.provider.kind === "memory";
  84. }
  85. ​
  86. var mem = null;
  87. ​
  88. async function memory() {
  89. var key = "ai.private." + (A.fs.provider ? A.fs.provider.name : "");
  90. if (!mem || mem.key !== key) mem = { key: key, files: (await A.idb.get(key)) || {} };
  91. return mem;
  92. }
  93. ​
  94. async function memorySave() {
  95. await A.idb.set(mem.key, mem.files);
  96. }
  97. ​
  98. var io = {
  99. kind: async function (path) {
  100. if (!path) return "dir";
  101. if (useMemory(path)) {
  102. var m = await memory();
  103. if (m.files[path] != null) return "file";
  104. return Object.keys(m.files).some(function (k) { return k.indexOf(path + "/") === 0; }) || path === PRIVATE ? "dir" : null;
  105. }
  106. var prov = provider();
  107. if (!(await prov.exists(path))) return null;
  108. try {
  109. await prov.readFile(path);
  110. return "file";
  111. } catch (e) {
  112. return "dir";
  113. }
  114. },
  115. ​
  116. exists: async function (path) {
  117. return (await io.kind(path)) != null;
  118. },
  119. ​
  120. read: async function (path) {
  121. var tab = A.tabs.byPath(path);
  122. if (tab && tab.kind === "text") return A.tabs.textOf(tab);
  123. if (useMemory(path)) {
  124. var m = await memory();
  125. if (m.files[path] == null) throw toolError("ai.tool.err_not_found", { path: path });
  126. return m.files[path];
  127. }
  128. try {
  129. return await provider().readText(path);
  130. } catch (e) {
  131. throw toolError("ai.tool.err_not_found", { path: path });
  132. }
  133. },
  134. ​
  135. write: async function (path, text) {
  136. if (useMemory(path)) {
  137. var m = await memory();
  138. m.files[path] = text;
  139. await memorySave();
  140. return;
  141. }
  142. await provider().write(path, text);
  143. },
  144. ​
  145. remove: async function (path) {
  146. if (useMemory(path)) {
  147. var m = await memory();
  148. Object.keys(m.files).forEach(function (k) { if (k === path || k.indexOf(path + "/") === 0) delete m.files[k]; });
  149. await memorySave();
  150. return;
  151. }
  152. await provider().remove(path);
  153. },
  154. ​
  155. list: async function (dir) {
  156. if (useMemory(dir || "") || (dir === PRIVATE && A.fs.provider && A.fs.provider.kind === "memory")) {
  157. var m = await memory();
  158. var prefix = dir ? dir + "/" : "";
  159. var seen = {};
  160. Object.keys(m.files).forEach(function (k) {
  161. if (k.indexOf(prefix) !== 0) return;
  162. var rest = k.slice(prefix.length).split("/");
  163. seen[rest[0]] = rest.length > 1 ? "dir" : "file";
  164. });
  165. return Object.keys(seen).sort().map(function (n) { return { name: n, kind: seen[n] }; });
  166. }
  167. var entries = await provider().list(dir || "");
  168. if (!dir && A.fs.provider.kind === "memory") {
  169. var mm = await memory();
  170. if (Object.keys(mm.files).length && !entries.some(function (e) { return e.name === PRIVATE; })) entries = entries.concat([{ name: PRIVATE, kind: "dir" }]);
  171. }
  172. return entries;
  173. },
  174. ​
  175. ensureDir: async function (dir) {
  176. if (!dir || useMemory(dir)) return;
  177. var prov = provider();
  178. if (await prov.exists(dir)) return;
  179. var parent = util.dirname(dir);
  180. if (parent) await io.ensureDir(parent);
  181. try { await prov.createDir(dir); } catch (e) { if (!(await prov.exists(dir))) throw e; }
  182. },
  183. ​
  184. move: async function (from, to, kind) {
  185. if (useMemory(from) || useMemory(to)) {
  186. var m = await memory();
  187. Object.keys(m.files).forEach(function (k) {
  188. if (k === from || k.indexOf(from + "/") === 0) {
  189. m.files[to + k.slice(from.length)] = m.files[k];
  190. delete m.files[k];
  191. }
  192. });
  193. await memorySave();
  194. return;
  195. }
  196. var prov = provider();
  197. var fromDir = util.dirname(from), toDir = util.dirname(to);
  198. var current = from;
  199. if (fromDir !== toDir) {
  200. await io.ensureDir(toDir);
  201. current = await prov.move(from, toDir, kind);
  202. }
  203. if (util.basename(current) !== util.basename(to)) current = await prov.rename(current, util.basename(to), kind);
  204. return current;
  205. }
  206. };
  207. ​
  208. function textOf(path) {
  209. return io.read(path);
  210. }
  211. ​
  212. function commonEdges(a, b) {
  213. var start = 0;
  214. var max = Math.min(a.length, b.length);
  215. while (start < max && a.charCodeAt(start) === b.charCodeAt(start)) start++;
  216. var end = 0;
  217. while (end < max - start && a.charCodeAt(a.length - 1 - end) === b.charCodeAt(b.length - 1 - end)) end++;
  218. return { from: start, toA: a.length - end, toB: b.length - end };
  219. }
  220. ​
  221. function replaceTabText(tab, text) {
  222. var current = A.tabs.textOf(tab);
  223. if (current === text) return;
  224. var e = commonEdges(current, text);
  225. var changes = { from: e.from, to: e.toA, insert: text.slice(e.from, e.toB) };
  226. if (tab === A.tabs.active) {
  227. A.editor.view.dispatch({ changes: changes, userEvent: "input.ai" });
  228. } else {
  229. tab.state = tab.state.update({ changes: changes }).state;
  230. tab.dirty = !tab.state.doc.eq(tab.savedDoc);
  231. }
  232. }
  233. ​
  234. async function applyText(path, text) {
  235. var tab = A.tabs.byPath(path);
  236. if (tab && tab.kind === "text") {
  237. var wasDirty = tab.dirty;
  238. replaceTabText(tab, text);
  239. if (wasDirty) {
  240. A.tabs.render();
  241. return { disk: false, tab: true };
  242. }
  243. await io.write(path, text);
  244. tab.savedDoc = A.tabs.stateOf(tab).doc;
  245. tab.dirty = false;
  246. A.tabs.render();
  247. A.emit("saved", tab);
  248. return { disk: true, tab: true };
  249. }
  250. await io.write(path, text);
  251. A.emit("saved", { path: path, name: util.basename(path) });
  252. return { disk: true, tab: false };
  253. }
  254. ​
  255. async function refreshTree(paths) {
  256. var dirs = {};
  257. paths.forEach(function (p) { if (p != null) dirs[util.dirname(p)] = true; });
  258. if (A.explorer && A.fs.provider) {
  259. for (var d of Object.keys(dirs)) {
  260. try { await A.explorer.refresh(d); } catch (e) { }
  261. }
  262. } else {
  263. A.emit("files-changed");
  264. }
  265. }
  266. ​
  267. function trimEdges(a, b) {
  268. var s = 0;
  269. while (s < a.length && s < b.length && a[s] === b[s]) s++;
  270. var e = 0;
  271. while (e < a.length - s && e < b.length - s && a[a.length - 1 - e] === b[b.length - 1 - e]) e++;
  272. return { s: s, e: e };
  273. }
  274. ​
  275. function myers(a, b) {
  276. var n = a.length, m = b.length;
  277. var v = { 1: 0 };
  278. var trace = [];
  279. for (var d = 0; d <= n + m; d++) {
  280. if (d > DIFF_LIMIT) return null;
  281. trace.push(Object.assign({}, v));
  282. for (var k = -d; k <= d; k += 2) {
  283. var x = k === -d || (k !== d && v[k - 1] < v[k + 1]) ? v[k + 1] : v[k - 1] + 1;
  284. var y = x - k;
  285. while (x < n && y < m && a[x] === b[y]) { x++; y++; }
  286. v[k] = x;
  287. if (x >= n && y >= m) return backtrack(trace, n, m);
  288. }
  289. }
  290. return null;
  291. }
  292. ​
  293. function backtrack(trace, n, m) {
  294. var x = n, y = m;
  295. var out = [];
  296. for (var d = trace.length - 1; d >= 0; d--) {
  297. var v = trace[d];
  298. var k = x - y;
  299. var prevK = k === -d || (k !== d && v[k - 1] < v[k + 1]) ? k + 1 : k - 1;
  300. var prevX = v[prevK];
  301. var prevY = prevX - prevK;
  302. while (x > prevX && y > prevY) { out.push({ t: " ", a: x - 1, b: y - 1 }); x--; y--; }
  303. if (d > 0) {
  304. if (x === prevX) out.push({ t: "+", b: y - 1 });
  305. else out.push({ t: "-", a: x - 1 });
  306. }
  307. x = prevX;
  308. y = prevY;
  309. }
  310. return out.reverse();
  311. }
  312. ​
  313. function diffLines(before, after) {
  314. var a = before == null ? [] : String(before).split("\n");
  315. var b = after == null ? [] : String(after).split("\n");
  316. var edge = trimEdges(a, b);
  317. var midA = a.slice(edge.s, a.length - edge.e);
  318. var midB = b.slice(edge.s, b.length - edge.e);
  319. var ops = myers(midA, midB);
  320. if (!ops) {
  321. ops = midA.map(function (l, i) { return { t: "-", a: i }; }).concat(midB.map(function (l, i) { return { t: "+", b: i }; }));
  322. }
  323. var out = [];
  324. for (var i = 0; i < edge.s; i++) out.push({ t: " ", text: a[i], la: i + 1, lb: i + 1 });
  325. ops.forEach(function (o) {
  326. if (o.t === " ") out.push({ t: " ", text: midA[o.a], la: edge.s + o.a + 1, lb: edge.s + o.b + 1 });
  327. else if (o.t === "-") out.push({ t: "-", text: midA[o.a], la: edge.s + o.a + 1 });
  328. else out.push({ t: "+", text: midB[o.b], lb: edge.s + o.b + 1 });
  329. });
  330. var offA = a.length - edge.e, offB = b.length - edge.e;
  331. for (var j = 0; j < edge.e; j++) out.push({ t: " ", text: a[offA + j], la: offA + j + 1, lb: offB + j + 1 });
  332. return out;
  333. }
  334. ​
  335. function hunks(lines, context) {
  336. context = context == null ? 3 : context;
  337. var result = [];
  338. var cur = null;
  339. var lastChange = -Infinity;
  340. lines.forEach(function (l, i) {
  341. if (l.t === " ") return;
  342. var start = Math.max(0, i - context);
  343. if (cur && start <= lastChange + context + 1) {
  344. for (var j = cur.end + 1; j <= i; j++) cur.lines.push(lines[j]);
  345. cur.end = i;
  346. } else {
  347. if (cur) result.push(cur);
  348. cur = { lines: lines.slice(start, i + 1), end: i };
  349. }
  350. lastChange = i;
  351. });
  352. if (cur) {
  353. var stop = Math.min(lines.length - 1, cur.end + context);
  354. for (var k = cur.end + 1; k <= stop; k++) cur.lines.push(lines[k]);
  355. result.push(cur);
  356. }
  357. return result;
  358. }
  359. ​
  360. function diffStats(lines) {
  361. var add = 0, del = 0;
  362. lines.forEach(function (l) { if (l.t === "+") add++; else if (l.t === "-") del++; });
  363. return { add: add, del: del };
  364. }
  365. ​
  366. function unified(before, after, maxLines) {
  367. var lines = diffLines(before, after);
  368. var out = [];
  369. hunks(lines).forEach(function (h) {
  370. var first = h.lines[0];
  371. out.push("@@ -" + (first.la || 0) + " +" + (first.lb || 0) + " @@");
  372. h.lines.forEach(function (l) { out.push(l.t + l.text); });
  373. });
  374. if (maxLines && out.length > maxLines) return out.slice(0, maxLines).join("\n") + "\n" + t("ai.tool.diff_more", { n: out.length - maxLines });
  375. return out.join("\n");
  376. }
  377. ​
  378. function diffView(before, after) {
  379. var lines = diffLines(before, after);
  380. var stats = diffStats(lines);
  381. var box = util.el("div", { class: "ath-ai-diff" });
  382. box.appendChild(util.el("div", { class: "ath-ai-diff-stats" }, [
  383. util.el("span", { class: "is-add" }, "+" + stats.add),
  384. util.el("span", { class: "is-del" }, "−" + stats.del)
  385. ]));
  386. var pre = util.el("div", { class: "ath-ai-diff-body orb-mono" });
  387. var shown = 0;
  388. hunks(lines).forEach(function (h, hi) {
  389. if (shown > 800) return;
  390. if (hi) pre.appendChild(util.el("div", { class: "ath-ai-diff-gap" }, "⋯"));
  391. h.lines.forEach(function (l) {
  392. shown++;
  393. if (shown > 800) return;
  394. pre.appendChild(util.el("div", { class: "ath-ai-diff-line" + (l.t === "+" ? " is-add" : l.t === "-" ? " is-del" : "") }, [
  395. util.el("span", { class: "ath-ai-diff-no" }, l.t === "+" ? "" : String(l.la || "")),
  396. util.el("span", { class: "ath-ai-diff-no" }, l.t === "-" ? "" : String(l.lb || "")),
  397. util.el("span", { class: "ath-ai-diff-text" }, (l.t === " " ? " " : l.t) + " " + l.text)
  398. ]));
  399. });
  400. });
  401. if (shown > 800) pre.appendChild(util.el("div", { class: "ath-ai-diff-gap" }, t("ai.tool.diff_more", { n: shown - 800 })));
  402. if (!lines.some(function (l) { return l.t !== " "; })) pre.appendChild(util.el("div", { class: "ath-ai-diff-gap" }, t("ai.tool.diff_none")));
  403. box.appendChild(pre);
  404. return box;
  405. }
  406. ​
  407. var snaps = { key: null, list: [] };
  408. ​
  409. function snapKey() {
  410. return "ai.snapshots." + (A.fs.provider ? A.fs.provider.name : "");
  411. }
  412. ​
  413. async function loadSnaps() {
  414. var key = snapKey();
  415. if (snaps.key !== key) {
  416. var stored = await A.idb.get(key);
  417. snaps = { key: key, list: Array.isArray(stored) ? stored : [] };
  418. }
  419. return snaps.list;
  420. }
  421. ​
  422. async function saveSnaps() {
  423. var list = snaps.list;
  424. while (list.length > SNAP_MAX) list.shift();
  425. var size = function () {
  426. return list.reduce(function (n, s) { return n + (s.before || "").length + (s.after || "").length; }, 0);
  427. };
  428. while (list.length > 1 && size() > SNAP_BYTES) list.shift();
  429. await A.idb.set(snaps.key, list);
  430. A.emit("ai-changes", list.length);
  431. }
  432. ​
  433. function opLabel(change) {
  434. return t("ai.change.op_" + change.op, { path: change.path, to: change.to || "" });
  435. }
  436. ​
  437. function needsApproval(change) {
  438. if (change.op === "delete" || change.op === "move") return true;
  439. if (change.private) return true;
  440. if (change.op === "overwrite" && change.large) return true;
  441. return ai.get("approval") !== "auto";
  442. }
  443. ​
  444. async function approve(change, reason) {
  445. var body = util.el("div", { class: "ath-ai-approve" }, [
  446. util.el("p", { class: "ath-dialog-text" }, opLabel(change)),
  447. reason ? util.el("p", { class: "ath-ai-approve-reason" }, reason) : null,
  448. change.private ? util.el("p", { class: "ath-set-hint" }, t("ai.change.private_note")) : null,
  449. change.op === "delete" ? util.el("p", { class: "ath-set-hint" }, t("ai.change.delete_note")) : null,
  450. change.op === "move" || change.op === "copy" ? null : diffView(change.before, change.after)
  451. ]);
  452. var answer = await A.ui.dialog({
  453. title: ai.t("ai.change.title"),
  454. body: body,
  455. size: "lg",
  456. actions: [
  457. { label: t("ai.change.reject"), value: false, kind: "ghost" },
  458. { label: t(change.op === "delete" ? "ai.change.approve_delete" : "ai.change.approve"), value: true, kind: change.op === "delete" ? "danger" : "primary" }
  459. ]
  460. });
  461. return answer === true;
  462. }
  463. ​
  464. async function commit(change, ctx) {
  465. var approved = true;
  466. var prefix = ctx && ctx.autoApprovePrefix;
  467. var inSandbox = !!prefix && change.path.indexOf(prefix) === 0 && (!change.to || change.to.indexOf(prefix) === 0) && change.op !== "delete" && change.op !== "move";
  468. if (!inSandbox && needsApproval(change)) approved = await approve(change, ctx && ctx.reason);
  469. if (!approved) return { applied: false };
  470. var record = {
  471. id: "s" + Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
  472. at: Date.now(),
  473. runId: ctx && ctx.runId || null,
  474. op: change.op,
  475. path: change.path,
  476. to: change.to || null,
  477. before: change.before,
  478. after: change.after,
  479. undone: false
  480. };
  481. var info = {};
  482. if (change.op === "create" || change.op === "edit" || change.op === "overwrite") {
  483. if (change.op === "create") await io.ensureDir(util.dirname(change.path));
  484. info = await applyText(change.path, change.after);
  485. if (change.op === "create") await refreshTree([change.path]);
  486. } else if (change.op === "copy") {
  487. await io.ensureDir(util.dirname(change.to));
  488. await io.write(change.to, change.after);
  489. A.emit("saved", { path: change.to, name: util.basename(change.to) });
  490. await refreshTree([change.to]);
  491. } else if (change.op === "delete") {
  492. await io.remove(change.path);
  493. A.tabs.removePath(change.path);
  494. await refreshTree([change.path]);
  495. } else if (change.op === "move") {
  496. await io.move(change.path, change.to, "file");
  497. A.tabs.renamePath(change.path, change.to);
  498. await refreshTree([change.path, change.to]);
  499. }
  500. if (ai.get("snapshots")) {
  501. await loadSnaps();
  502. snaps.list.push(record);
  503. await saveSnaps();
  504. }
  505. var event = { record: record, info: info, diff: ai.get("diff") && change.op !== "move" ? unified(change.before, change.after, 200) : null };
  506. A.emit("ai-change", event);
  507. return { applied: true, record: record, info: info };
  508. }
  509. ​
  510. async function undo(id) {
  511. await loadSnaps();
  512. var rec = snaps.list.find(function (s) { return s.id === id; });
  513. if (!rec || rec.undone) return false;
  514. var conflict = false;
  515. if (rec.op === "create" || rec.op === "edit" || rec.op === "overwrite") {
  516. var now = (await io.exists(rec.path)) ? await textOf(rec.path) : null;
  517. conflict = now !== rec.after;
  518. } else if (rec.op === "copy") {
  519. var copied = (await io.exists(rec.to)) ? await textOf(rec.to) : null;
  520. conflict = copied !== rec.after;
  521. } else if (rec.op === "delete") {
  522. conflict = await io.exists(rec.path);
  523. } else if (rec.op === "move") {
  524. conflict = (await io.exists(rec.path)) || !(await io.exists(rec.to));
  525. }
  526. if (conflict && !(await A.ui.confirm(t("ai.change.undo"), t("ai.change.undo_conflict", { path: rec.path }), t("ai.change.undo_anyway"), true))) return false;
  527. if (rec.op === "create") {
  528. if (await io.exists(rec.path)) {
  529. await io.remove(rec.path);
  530. A.tabs.removePath(rec.path);
  531. }
  532. await refreshTree([rec.path]);
  533. } else if (rec.op === "edit" || rec.op === "overwrite") {
  534. await applyText(rec.path, rec.before);
  535. } else if (rec.op === "copy") {
  536. if (await io.exists(rec.to)) await io.remove(rec.to);
  537. A.tabs.removePath(rec.to);
  538. await refreshTree([rec.to]);
  539. } else if (rec.op === "delete") {
  540. await io.ensureDir(util.dirname(rec.path));
  541. await io.write(rec.path, rec.before);
  542. await refreshTree([rec.path]);
  543. } else if (rec.op === "move") {
  544. if (await io.exists(rec.to)) {
  545. await io.move(rec.to, rec.path, "file");
  546. A.tabs.renamePath(rec.to, rec.path);
  547. }
  548. await refreshTree([rec.path, rec.to]);
  549. }
  550. rec.undone = true;
  551. await saveSnaps();
  552. A.ui.toast(t("ai.change.undone", { path: rec.path }), "success");
  553. return true;
  554. }
  555. ​
  556. async function undoRun(runId) {
  557. await loadSnaps();
  558. var list = snaps.list.filter(function (s) { return s.runId === runId && !s.undone; }).reverse();
  559. var n = 0;
  560. for (var rec of list) if (await undo(rec.id)) n++;
  561. return n;
  562. }
  563. ​
  564. var TEMPLATE_KEYS = {
  565. "Summary.md": "ai.priv.tpl_summary",
  566. "Conversation.md": "ai.priv.tpl_conversation",
  567. "plan.md": "ai.priv.tpl_plan",
  568. "bugs.md": "ai.priv.tpl_bugs",
  569. "instructions.md": "ai.priv.tpl_instructions",
  570. "design.md": "ai.priv.tpl_design"
  571. };
  572. ​
  573. var HTACCESS = "<IfModule mod_authz_core.c>\n Require all denied\n</IfModule>\n<IfModule !mod_authz_core.c>\n Deny from all\n</IfModule>\n";
  574. ​
  575. var priv = (ai.private = {
  576. DIR: PRIVATE,
  577. FILES: PRIVATE_FILES,
  578. DIRS: PRIVATE_DIRS,
  579. ​
  580. path: function (name) {
  581. return PRIVATE + "/" + name;
  582. },
  583. ​
  584. memoryMode: function () {
  585. return !!A.fs.provider && A.fs.provider.kind === "memory";
  586. },
  587. ​
  588. exists: async function (name) {
  589. if (!A.fs.provider) return false;
  590. return io.exists(priv.path(name));
  591. },
  592. ​
  593. read: async function (name) {
  594. if (!A.fs.provider) return null;
  595. var p = priv.path(name);
  596. if (!(await io.exists(p))) return null;
  597. return io.read(p);
  598. },
  599. ​
  600. write: async function (name, text) {
  601. if (!(await priv.ensure())) return false;
  602. var p = priv.path(name);
  603. await io.ensureDir(util.dirname(p));
  604. var existed = await io.exists(p);
  605. await applyText(p, text);
  606. if (!existed) await refreshTree([p]);
  607. return true;
  608. },
  609. ​
  610. template: function (name) {
  611. return TEMPLATE_KEYS[name] ? ai.t(TEMPLATE_KEYS[name]) + "\n" : "";
  612. },
  613. ​
  614. ensure: async function () {
  615. provider();
  616. if (priv.memoryMode()) return true;
  617. if (await io.exists(PRIVATE)) {
  618. if (!(await io.exists(PRIVATE + "/.htaccess"))) await io.write(PRIVATE + "/.htaccess", HTACCESS);
  619. return true;
  620. }
  621. var addIgnore = false;
  622. if (!ai.get("privateWarned")) {
  623. var choice = await A.ui.dialog({
  624. title: ai.t("ai.priv.warn_title"),
  625. body: util.el("div", {}, [
  626. util.el("p", { class: "ath-dialog-text" }, ai.t("ai.priv.warn_text")),
  627. util.el("p", { class: "ath-set-hint" }, ai.t("ai.priv.warn_htaccess"))
  628. ]),
  629. actions: [
  630. { label: A.t("common.cancel"), value: null, kind: "ghost" },
  631. { label: ai.t("ai.priv.create_only"), value: "create", kind: "secondary" },
  632. { label: ai.t("ai.priv.create_ignore"), value: "ignore", kind: "primary" }
  633. ]
  634. });
  635. if (!choice) return false;
  636. ai.set("privateWarned", true);
  637. addIgnore = choice === "ignore";
  638. }
  639. await io.ensureDir(PRIVATE);
  640. await io.write(PRIVATE + "/.htaccess", HTACCESS);
  641. if (addIgnore) await priv.addToGitignore();
  642. await refreshTree([PRIVATE, PRIVATE + "/.htaccess"]);
  643. return true;
  644. },
  645. ​
  646. addToGitignore: async function () {
  647. var current = (await io.exists(".gitignore")) ? await io.read(".gitignore") : "";
  648. if (/^\/?\.private\/?\s*$/m.test(current)) return false;
  649. await applyText(".gitignore", current + (current && !/\n$/.test(current) ? "\n" : "") + ".private/\n");
  650. await refreshTree([".gitignore"]);
  651. return true;
  652. },
  653. ​
  654. create: async function (name) {
  655. if (await priv.exists(name)) return true;
  656. return priv.write(name, priv.template(name));
  657. },
  658. ​
  659. createDir: async function (dir) {
  660. if (!(await priv.ensure())) return false;
  661. var p = priv.path(dir);
  662. if (priv.memoryMode()) return true;
  663. await io.ensureDir(p);
  664. await refreshTree([p + "/x"]);
  665. return true;
  666. },
  667. ​
  668. status: async function () {
  669. var out = { files: {}, dirs: {}, memory: priv.memoryMode(), folder: !!A.fs.provider };
  670. if (!A.fs.provider) return out;
  671. for (var f of PRIVATE_FILES) out.files[f] = await io.exists(priv.path(f));
  672. for (var d of PRIVATE_DIRS) out.dirs[d] = await io.exists(priv.path(d));
  673. return out;
  674. }
  675. });
  676. ​
  677. function numbered(text, start, end) {
  678. var lines = text.split("\n");
  679. var from = Math.max(1, start || 1);
  680. var to = Math.min(lines.length, end || lines.length);
  681. var width = String(to).length;
  682. var out = [];
  683. var chars = 0;
  684. var cut = false;
  685. for (var i = from; i <= to; i++) {
  686. var row = (new Array(width + 1).join(" ") + i).slice(-width) + "| " + lines[i - 1];
  687. chars += row.length + 1;
  688. if (chars > READ_MAX || i - from >= READ_LINES) { cut = true; to = i - 1; break; }
  689. out.push(row);
  690. }
  691. return { text: out.join("\n"), from: from, to: to, total: lines.length, cut: cut };
  692. }
  693. ​
  694. function nearby(text, old) {
  695. var lines = text.split("\n");
  696. var firstLine = String(old).split("\n").map(function (l) { return l.trim(); }).find(Boolean) || "";
  697. var best = -1, bestScore = 0;
  698. var words = (firstLine.toLowerCase().match(/[\p{L}\p{N}_-]{2,}/gu) || []).filter(function (w, i, all) { return all.indexOf(w) === i; });
  699. lines.forEach(function (l, i) {
  700. var lower = l.toLowerCase();
  701. var score = l.trim() === firstLine ? 100 : words.reduce(function (n, w) { return n + (lower.indexOf(w) >= 0 ? 1 : 0); }, 0);
  702. if (score > bestScore) { bestScore = score; best = i; }
  703. });
  704. if (best < 0) return null;
  705. return numbered(text, Math.max(1, best + 1 - 3), Math.min(lines.length, best + 1 + 6)).text;
  706. }
  707. ​
  708. function countOccurrences(text, needle) {
  709. if (!needle) return 0;
  710. var n = 0, i = 0;
  711. while ((i = text.indexOf(needle, i)) >= 0) { n++; i += needle.length; }
  712. return n;
  713. }
  714. ​
  715. function globRe(glob) {
  716. var src = String(glob).split("").map(function (c) {
  717. if (c === "*") return "[^/]*";
  718. if (c === "?") return "[^/]";
  719. return c.replace(/[.+^${}()|[\]\\]/g, "\\$&");
  720. }).join("").replace(/\[\^\/\]\*\[\^\/\]\*/g, ".*");
  721. return new RegExp((glob.indexOf("/") >= 0 ? "^" : "(?:^|/)") + src + "$", "i");
  722. }
  723. ​
  724. function outline(path, text) {
  725. var ext = util.ext(path);
  726. var lines = text.split("\n");
  727. var items = [];
  728. function add(i, label) { if (items.length < 60) items.push((i + 1) + ": " + label.trim().slice(0, 120)); }
  729. lines.forEach(function (line, i) {
  730. var m;
  731. if (/^(html?|php|phtml|inc|twig|tpl|svg|vue)$/.test(ext)) {
  732. if ((m = /<(h[1-6])[^>]*>([^<]{1,100})/i.exec(line))) add(i, m[1] + " " + m[2]);
  733. else if ((m = /<title>([^<]{1,100})/i.exec(line))) add(i, "title " + m[1]);
  734. else if ((m = /<(header|nav|main|section|article|aside|footer|form|dialog|table)\b[^>]*?(?:\b(?:id|class)="([^"]{1,60})")?/i.exec(line))) add(i, "<" + m[1] + (m[2] ? " " + m[2] : "") + ">");
  735. else if ((m = /\bid="([^"]{1,60})"/.exec(line))) add(i, "#" + m[1]);
  736. else if ((m = /<(?:script|link)\b[^>]*(?:src|href)="([^"]{1,100})"/i.exec(line))) add(i, "asset " + m[1]);
  737. if (ext !== "html" && ext !== "htm" && (m = /\b(function|class)\s+(\w+)/.exec(line))) add(i, m[1] + " " + m[2]);
  738. if ((m = /\b(require|include)(?:_once)?\s*\(?\s*['"]([^'"]+)/.exec(line))) add(i, m[1] + " " + m[2]);
  739. } else if (/^(css|scss|sass|less)$/.test(ext)) {
  740. if ((m = /^\s{0,2}(@media[^{]+|@supports[^{]+|@keyframes\s+\S+|:root|[^@\s][^{;]{0,80})\s*\{/.exec(line))) add(i, m[1]);
  741. } else if (/^(js|mjs|cjs|ts|jsx|tsx)$/.test(ext)) {
  742. if ((m = /\b(?:async\s+)?function\s*\*?\s*(\w+)/.exec(line))) add(i, "function " + m[1]);
  743. else if ((m = /\bclass\s+(\w+)/.exec(line))) add(i, "class " + m[1]);
  744. else if ((m = /^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?(?:\([^)]*\)|\w+)\s*=>/.exec(line))) add(i, "function " + m[1]);
  745. else if ((m = /addEventListener\(\s*['"](\w+)/.exec(line))) add(i, "on " + m[1]);
  746. } else if (/^(md|markdown)$/.test(ext)) {
  747. if ((m = /^(#{1,4})\s+(.+)/.exec(line))) add(i, m[1] + " " + m[2]);
  748. } else if (ext === "sql") {
  749. if ((m = /\b(create\s+table|create\s+view)\s+[`"]?(\w+)/i.exec(line))) add(i, m[1] + " " + m[2]);
  750. }
  751. });
  752. return items;
  753. }
  754. ​
  755. async function walk(dir, depth, out, includePrivate) {
  756. if (out.length >= LIST_MAX) return;
  757. var entries;
  758. try { entries = await io.list(dir); } catch (e) { return; }
  759. for (var e of entries) {
  760. if (out.length >= LIST_MAX) return;
  761. var p = util.join(dir, e.name);
  762. if (e.kind === "dir") {
  763. if (A.fs.HIDDEN_DIRS.indexOf(e.name) >= 0) continue;
  764. if (!dir && e.name === PRIVATE && !includePrivate) {
  765. out.push(p + "/ " + t("ai.tool.private_marker"));
  766. continue;
  767. }
  768. out.push(p + "/");
  769. if (depth > 1) await walk(p, depth - 1, out, includePrivate);
  770. } else {
  771. if (A.fs.HIDDEN_FILES.indexOf(e.name) >= 0) continue;
  772. out.push(p + (ai.isExcluded(p) ? " " + t("ai.tool.excluded_marker") : ""));
  773. }
  774. }
  775. }
  776. ​
  777. function wrapData(source, text) {
  778. return "[" + t("ai.tool.data_header", { source: source }) + "]\n" + text;
  779. }
  780. ​
  781. function hide(text, path) {
  782. return ai.redact ? ai.redact.apply(text, path).text : text;
  783. }
  784. ​
  785. function hostOf(url) {
  786. try {
  787. var u = new URL(url);
  788. if (!/^https?:$/.test(u.protocol)) return null;
  789. return u.hostname.toLowerCase();
  790. } catch (e) {
  791. return null;
  792. }
  793. }
  794. ​
  795. function domainAllowed(host) {
  796. var match = function (d) { return host === d || host.slice(-(d.length + 1)) === "." + d; };
  797. if (ai.domains("deny").some(match)) return false;
  798. var allow = ai.domains("allow");
  799. return !allow.length || allow.some(match);
  800. }
  801. ​
  802. function htmlToMarkdown(html, base) {
  803. var doc = new DOMParser().parseFromString(html, "text/html");
  804. doc.querySelectorAll("script, style, noscript, iframe, svg, nav, footer, header, form, button, template").forEach(function (n) { n.remove(); });
  805. var root = doc.querySelector("main, article, [role=main]") || doc.body || doc.documentElement;
  806. function abs(href) {
  807. try { return new URL(href, base).href; } catch (e) { return href; }
  808. }
  809. function walkNode(node, list) {
  810. if (node.nodeType === 3) return node.nodeValue.replace(/\s+/g, " ");
  811. if (node.nodeType !== 1) return "";
  812. var tag = node.tagName.toLowerCase();
  813. var inner = function () { return Array.prototype.map.call(node.childNodes, function (c) { return walkNode(c, list); }).join(""); };
  814. if (/^h[1-6]$/.test(tag)) return "\n\n" + new Array(Number(tag[1]) + 1).join("#") + " " + inner().trim() + "\n\n";
  815. if (tag === "p" || tag === "div" || tag === "section") return "\n\n" + inner().trim() + "\n\n";
  816. if (tag === "br") return "\n";
  817. if (tag === "pre") return "\n\n```\n" + node.textContent.replace(/\n+$/, "") + "\n```\n\n";
  818. if (tag === "code") return "`" + node.textContent + "`";
  819. if (tag === "strong" || tag === "b") return "**" + inner().trim() + "**";
  820. if (tag === "em" || tag === "i") return "*" + inner().trim() + "*";
  821. if (tag === "a") {
  822. var href = node.getAttribute("href");
  823. var label = inner().trim();
  824. return href && label ? "[" + label + "](" + abs(href) + ")" : label;
  825. }
  826. if (tag === "img") return node.getAttribute("alt") ? "![" + node.getAttribute("alt") + "]" : "";
  827. if (tag === "ul" || tag === "ol") {
  828. var ordered = tag === "ol";
  829. return "\n\n" + Array.prototype.filter.call(node.children, function (c) { return c.tagName === "LI"; }).map(function (li, i) {
  830. return (ordered ? (i + 1) + ". " : "- ") + walkNode(li, list).trim();
  831. }).join("\n") + "\n\n";
  832. }
  833. if (tag === "tr") return "| " + Array.prototype.map.call(node.children, function (c) { return walkNode(c, list).trim().replace(/\|/g, "\\|"); }).join(" | ") + " |\n";
  834. if (tag === "table") return "\n\n" + inner() + "\n\n";
  835. return inner();
  836. }
  837. return walkNode(root, []).replace(/[ \t]+\n/g, "\n").replace(/\n{3,}/g, "\n\n").trim();
  838. }
  839. ​
  840. function slug(text) {
  841. return String(text || "page").toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "")
  842. .replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "page";
  843. }
  844. ​
  845. var TOOLS = [
  846. {
  847. name: "list_files",
  848. perm: "read",
  849. description: "List files and folders in the open project. Use a small depth first and go deeper only where needed.",
  850. parameters: { type: "object", properties: {
  851. dir: { type: "string", description: "Folder relative to the project root, empty for the root." },
  852. depth: { type: "integer", description: "How many folder levels to list (1-6, default 2)." }
  853. } },
  854. run: async function (args) {
  855. var dir = cleanPath(args.dir, true);
  856. var depth = Math.max(1, Math.min(6, Number(args.depth) || 2));
  857. if (dir && (await io.kind(dir)) !== "dir") throw toolError("ai.tool.err_not_dir", { path: dir });
  858. var out = [];
  859. await walk(dir, depth, out, isPrivate(dir) || ai.get("privateIndex"));
  860. if (out.length >= LIST_MAX) out.push(t("ai.tool.list_cut", { n: LIST_MAX }));
  861. return out.length ? out.join("\n") : t("ai.tool.list_empty");
  862. }
  863. },
  864. {
  865. name: "read_file",
  866. perm: "read",
  867. description: "Read a text file. Lines are prefixed with their number and \"| \"; that prefix is not part of the file. Large files are cut: request a range with start_line/end_line. Prefer search_files or file_outline to find the right range first.",
  868. parameters: { type: "object", properties: {
  869. path: { type: "string", description: "File path relative to the project root." },
  870. start_line: { type: "integer", description: "First line to read (1-based)." },
  871. end_line: { type: "integer", description: "Last line to read (inclusive)." }
  872. }, required: ["path"] },
  873. run: async function (args) {
  874. var p = guardRead(args.path);
  875. var text = hide(await textOf(p), p);
  876. var r = numbered(text, Number(args.start_line) || 1, Number(args.end_line) || 0);
  877. var head = t("ai.tool.read_head", { path: p, from: r.from, to: r.to, total: r.total });
  878. var tail = r.cut || r.to < r.total ? "\n" + t("ai.tool.read_cut", { next: r.to + 1, total: r.total }) : "";
  879. return wrapData(p, head + "\n" + r.text + tail);
  880. }
  881. },
  882. {
  883. name: "file_outline",
  884. perm: "read",
  885. description: "Get a compact outline of a file (headings, ids, sections, functions, CSS selectors) with line numbers. Cheaper than reading the whole file.",
  886. parameters: { type: "object", properties: { path: { type: "string" } }, required: ["path"] },
  887. run: async function (args) {
  888. var p = guardRead(args.path);
  889. var text = await textOf(p);
  890. var items = outline(p, text).map(function (line) { return hide(line, p); });
  891. return wrapData(p, t("ai.tool.outline_head", { path: p, total: text.split("\n").length }) + "\n" + (items.length ? items.join("\n") : t("ai.tool.outline_empty")));
  892. }
  893. },
  894. {
  895. name: "search_files",
  896. perm: "read",
  897. description: "Search the text of project files. Returns matching lines as path:line: text. Use it to find where something is defined before reading.",
  898. parameters: { type: "object", properties: {
  899. query: { type: "string", description: "Text to find (case-insensitive) or a regular expression when regex is true." },
  900. regex: { type: "boolean" },
  901. glob: { type: "string", description: "Optional file name filter, e.g. *.css or pages/*.php." },
  902. max_results: { type: "integer" }
  903. }, required: ["query"] },
  904. run: async function (args) {
  905. var query = String(args.query || "");
  906. if (!query) throw toolError("ai.tool.err_query");
  907. var re;
  908. try {
  909. re = args.regex ? new RegExp(query, "i") : new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i");
  910. } catch (e) {
  911. throw toolError("ai.tool.err_regex", { message: e.message });
  912. }
  913. var filter = args.glob ? globRe(args.glob) : null;
  914. var max = Math.max(1, Math.min(SEARCH_MAX, Number(args.max_results) || 50));
  915. var files = (await A.fs.allFiles()).filter(function (p) {
  916. return !A.fs.isBinary(p) && !ai.isExcluded(p) && (!filter || filter.test(p));
  917. }).slice(0, SEARCH_FILES);
  918. var out = [];
  919. for (var p of files) {
  920. if (out.length >= max) break;
  921. var text;
  922. try {
  923. var tab = A.tabs.byPath(p);
  924. if (tab && tab.kind === "text") text = A.tabs.textOf(tab);
  925. else {
  926. var file = await provider().readFile(p);
  927. if (file.size > SEARCH_FILE_MAX) continue;
  928. text = await file.text();
  929. }
  930. } catch (e) { continue; }
  931. var lines = text.split("\n");
  932. for (var i = 0; i < lines.length && out.length < max; i++) {
  933. if (re.test(lines[i])) out.push(p + ":" + (i + 1) + ": " + hide(lines[i].trim(), p).slice(0, 200));
  934. }
  935. }
  936. if (!out.length) return t("ai.tool.search_none", { query: query });
  937. var joined = out.join("\n");
  938. return wrapData(t("ai.tool.search_source"), joined + (out.length >= max ? "\n" + t("ai.tool.search_cut", { n: max }) : ""));
  939. }
  940. },
  941. {
  942. name: "create_file",
  943. perm: "write",
  944. description: "Create a new text file. Fails if the file already exists (use edit_file for changes). Parent folders are created automatically.",
  945. parameters: { type: "object", properties: {
  946. path: { type: "string" },
  947. content: { type: "string" }
  948. }, required: ["path", "content"] },
  949. run: async function (args, ctx) {
  950. var p = guardWrite(args.path);
  951. var content = String(args.content == null ? "" : args.content);
  952. if (content.length > WRITE_MAX) throw toolError("ai.tool.err_too_big", { max: Math.round(WRITE_MAX / 1024) });
  953. if (await io.exists(p)) throw toolError("ai.tool.err_exists", { path: p });
  954. if (isPrivate(p) && !(await priv.ensure())) throw toolError("ai.tool.err_private_declined");
  955. var r = await commit({ op: "create", path: p, before: null, after: content, private: isPrivate(p) }, ctx);
  956. if (!r.applied) return { content: t("ai.tool.rejected", { path: p }), isError: true };
  957. return t("ai.tool.created", { path: p, lines: content.split("\n").length }) + memoryNote(p, r);
  958. }
  959. },
  960. {
  961. name: "edit_file",
  962. perm: "write",
  963. description: "Edit a file with exact text replacements. Each edit's \"old\" must appear exactly once in the current file (copy it exactly, without line-number prefixes, with enough surrounding lines to be unique). All edits are applied together or not at all. Prefer several small edits over rewriting.",
  964. parameters: { type: "object", properties: {
  965. path: { type: "string" },
  966. edits: { type: "array", items: { type: "object", properties: {
  967. old: { type: "string", description: "Exact current text to replace." },
  968. new: { type: "string", description: "Replacement text." }
  969. }, required: ["old", "new"] } }
  970. }, required: ["path", "edits"] },
  971. run: async function (args, ctx) {
  972. var p = guardWrite(args.path);
  973. if (!(await io.exists(p))) throw toolError("ai.tool.err_not_found", { path: p });
  974. var edits = Array.isArray(args.edits) ? args.edits : [];
  975. if (!edits.length) throw toolError("ai.tool.err_no_edits");
  976. var before = await textOf(p);
  977. var after = before;
  978. for (var i = 0; i < edits.length; i++) {
  979. var old = String(edits[i].old == null ? "" : edits[i].old);
  980. var rep = String(edits[i].new == null ? "" : edits[i].new);
  981. if (!old) throw toolError("ai.tool.err_old_empty", { n: i + 1 });
  982. var count = countOccurrences(after, old);
  983. if (count !== 1) {
  984. var near = ai.allowed("read") && count === 0 ? nearby(after, old) : null;
  985. if (near) near = hide(near, p);
  986. var key = count === 0 ? "ai.tool.err_old_missing" : "ai.tool.err_old_many";
  987. return { content: t(key, { n: i + 1, count: count, path: p }) + (near ? "\n" + t("ai.tool.nearby") + "\n" + near : ""), isError: true };
  988. }
  989. after = after.replace(old, function () { return rep; });
  990. }
  991. if (after === before) return t("ai.tool.no_change", { path: p });
  992. if (after.length > WRITE_MAX) throw toolError("ai.tool.err_too_big", { max: Math.round(WRITE_MAX / 1024) });
  993. var r = await commit({ op: "edit", path: p, before: before, after: after, private: isPrivate(p) }, ctx);
  994. if (!r.applied) return { content: t("ai.tool.rejected", { path: p }), isError: true };
  995. var s = diffStats(diffLines(before, after));
  996. return t("ai.tool.edited", { path: p, add: s.add, del: s.del }) + memoryNote(p, r);
  997. }
  998. },
  999. {
  1000. name: "overwrite_file",
  1001. perm: "write",
  1002. description: "Replace the whole content of an existing file. Only for small files (under about 8 KB) or when the user explicitly asked for a full rewrite (set user_requested). For other changes use edit_file.",
  1003. parameters: { type: "object", properties: {
  1004. path: { type: "string" },
  1005. content: { type: "string" },
  1006. user_requested: { type: "boolean", description: "True only if the user explicitly asked to rewrite the whole file." }
  1007. }, required: ["path", "content"] },
  1008. run: async function (args, ctx) {
  1009. var p = guardWrite(args.path);
  1010. if (!(await io.exists(p))) throw toolError("ai.tool.err_not_found", { path: p });
  1011. var before = await textOf(p);
  1012. var content = String(args.content == null ? "" : args.content);
  1013. if (content.length > WRITE_MAX) throw toolError("ai.tool.err_too_big", { max: Math.round(WRITE_MAX / 1024) });
  1014. var large = before.length > OVERWRITE_FREE;
  1015. if (large && !args.user_requested) return { content: t("ai.tool.err_overwrite_large", { path: p, size: Math.round(before.length / 1024) }), isError: true };
  1016. var r = await commit({ op: "overwrite", path: p, before: before, after: content, private: isPrivate(p), large: large }, ctx);
  1017. if (!r.applied) return { content: t("ai.tool.rejected", { path: p }), isError: true };
  1018. return t("ai.tool.overwritten", { path: p }) + memoryNote(p, r);
  1019. }
  1020. },
  1021. {
  1022. name: "delete_file",
  1023. perm: "write",
  1024. description: "Delete a file. The user always has to confirm.",
  1025. parameters: { type: "object", properties: { path: { type: "string" }, reason: { type: "string" } }, required: ["path"] },
  1026. run: async function (args, ctx) {
  1027. var p = guardWrite(args.path);
  1028. if ((await io.kind(p)) !== "file") throw toolError("ai.tool.err_not_found", { path: p });
  1029. var before = await textOf(p);
  1030. var r = await commit({ op: "delete", path: p, before: before, after: null, private: isPrivate(p) }, Object.assign({}, ctx, { reason: args.reason }));
  1031. if (!r.applied) return { content: t("ai.tool.rejected", { path: p }), isError: true };
  1032. return t("ai.tool.deleted", { path: p });
  1033. }
  1034. },
  1035. {
  1036. name: "move_file",
  1037. perm: "write",
  1038. description: "Move or rename a file. The user always has to confirm.",
  1039. parameters: { type: "object", properties: { from: { type: "string" }, to: { type: "string" }, reason: { type: "string" } }, required: ["from", "to"] },
  1040. run: async function (args, ctx) {
  1041. var from = guardWrite(args.from);
  1042. var to = guardWrite(args.to);
  1043. if ((await io.kind(from)) !== "file") throw toolError("ai.tool.err_not_found", { path: from });
  1044. if (await io.exists(to)) throw toolError("ai.tool.err_exists", { path: to });
  1045. var text = await textOf(from);
  1046. var r = await commit({ op: "move", path: from, to: to, before: text, after: text, private: isPrivate(from) || isPrivate(to) }, Object.assign({}, ctx, { reason: args.reason }));
  1047. if (!r.applied) return { content: t("ai.tool.rejected", { path: from }), isError: true };
  1048. return t("ai.tool.moved", { from: from, to: to });
  1049. }
  1050. },
  1051. {
  1052. name: "copy_file",
  1053. perm: "write",
  1054. description: "Copy a text file, for example into .private/sandbox/ to experiment without touching the original.",
  1055. parameters: { type: "object", properties: { from: { type: "string" }, to: { type: "string" } }, required: ["from", "to"] },
  1056. run: async function (args, ctx) {
  1057. var from = guardRead(args.from);
  1058. var to = guardWrite(args.to);
  1059. if ((await io.kind(from)) !== "file") throw toolError("ai.tool.err_not_found", { path: from });
  1060. if (await io.exists(to)) throw toolError("ai.tool.err_exists", { path: to });
  1061. if (isPrivate(to) && !(await priv.ensure())) throw toolError("ai.tool.err_private_declined");
  1062. var text = await textOf(from);
  1063. var r = await commit({ op: "copy", path: from, to: to, before: null, after: text, private: isPrivate(to) }, ctx);
  1064. if (!r.applied) return { content: t("ai.tool.rejected", { path: to }), isError: true };
  1065. return t("ai.tool.copied", { from: from, to: to });
  1066. }
  1067. },
  1068. {
  1069. name: "save_crawl",
  1070. perm: "crawler",
  1071. description: "Save documentation you fetched from the web as a Markdown file in .private/crawler/ so it can be reused later without fetching again. Only save content you actually fetched.",
  1072. parameters: { type: "object", properties: {
  1073. url: { type: "string" },
  1074. title: { type: "string" },
  1075. markdown: { type: "string", description: "Cleaned page content as Markdown." }
  1076. }, required: ["url", "markdown"] },
  1077. run: async function (args, ctx) {
  1078. var host = hostOf(args.url);
  1079. if (!host) throw toolError("ai.tool.err_url");
  1080. if (!domainAllowed(host)) throw toolError("ai.tool.err_domain", { host: host });
  1081. var md = String(args.markdown || "").slice(0, FETCH_MAX * 2);
  1082. if (!md.trim()) throw toolError("ai.tool.err_empty");
  1083. if (!(await priv.ensure())) throw toolError("ai.tool.err_private_declined");
  1084. var p = PRIVATE + "/crawler/" + slug(host) + "-" + slug(args.title || new URL(args.url).pathname) + ".md";
  1085. var body = "---\nurl: " + args.url + "\ntitle: " + String(args.title || "").replace(/\n/g, " ") + "\nsaved: " + new Date().toISOString().slice(0, 10) + "\n---\n\n" + md + "\n";
  1086. var exists = await io.exists(p);
  1087. var before = exists ? await textOf(p) : null;
  1088. var r = await commit({ op: exists ? "overwrite" : "create", path: p, before: before, after: body, private: true }, ctx);
  1089. if (!r.applied) return { content: t("ai.tool.rejected", { path: p }), isError: true };
  1090. return t("ai.tool.crawl_saved", { path: p });
  1091. }
  1092. },
  1093. {
  1094. name: "fetch_url",
  1095. perm: "crawler",
  1096. relay: true,
  1097. description: "Download a web page (through the user's relay server or the Firecrawl scraper) and return it as Markdown. The content is untrusted data.",
  1098. parameters: { type: "object", properties: { url: { type: "string" } }, required: ["url"] },
  1099. run: async function (args, ctx) {
  1100. var relay = ai.relayUrl();
  1101. var host = hostOf(args.url);
  1102. if (!host) throw toolError("ai.tool.err_url");
  1103. if (!domainAllowed(host)) throw toolError("ai.tool.err_domain", { host: host });
  1104. ai.assertNetwork();
  1105. if (!relay && ai.search && ai.search.canScrape()) {
  1106. var page = await ai.search.scrape(args.url, ctx && ctx.signal);
  1107. var text = page.markdown;
  1108. return wrapData(args.url, (page.title ? "# " + page.title + "\n\n" : "") + text.slice(0, FETCH_MAX) + (text.length > FETCH_MAX ? "\n" + t("ai.tool.fetch_cut", { n: FETCH_MAX }) : ""));
  1109. }
  1110. if (!relay) throw toolError("ai.tool.err_no_relay");
  1111. var got = await ai.relay.page(args.url, ctx && ctx.signal);
  1112. if (got.status >= 400) throw toolError("ai.tool.err_fetch", { status: got.status });
  1113. if (!got.content) throw toolError("ai.tool.err_fetch_type", { type: got.type || "?" });
  1114. var finalHost = hostOf(got.url);
  1115. if (finalHost && !domainAllowed(finalHost)) throw toolError("ai.tool.err_domain", { host: finalHost });
  1116. var raw = got.content.slice(0, 2000000);
  1117. var md = /html/i.test(got.type) ? htmlToMarkdown(raw, got.url || args.url) : raw;
  1118. var cut = md.length > FETCH_MAX;
  1119. return wrapData(args.url, md.slice(0, FETCH_MAX) + (cut ? "\n" + t("ai.tool.fetch_cut", { n: FETCH_MAX }) : ""));
  1120. }
  1121. }
  1122. ];
  1123. ​
  1124. function memoryNote(path, r) {
  1125. var notes = "";
  1126. if (r.info && r.info.tab && !r.info.disk) notes += " " + t("ai.tool.note_dirty_tab");
  1127. if (A.fs.provider && A.fs.provider.kind === "memory" && !isPrivate(path)) notes += " " + t("ai.tool.note_memory");
  1128. return notes;
  1129. }
  1130. ​
  1131. var RESTORES = { create_file: 1, edit_file: 1, overwrite_file: 1, search_files: 1 };
  1132. ​
  1133. var tools = (ai.tools = {
  1134. io: io,
  1135. cleanPath: cleanPath,
  1136. guardRead: guardRead,
  1137. guardWrite: guardWrite,
  1138. isPrivate: isPrivate,
  1139. outline: outline,
  1140. numbered: numbered,
  1141. diffLines: diffLines,
  1142. unified: unified,
  1143. diffView: diffView,
  1144. htmlToMarkdown: htmlToMarkdown,
  1145. commit: commit,
  1146. undo: undo,
  1147. undoRun: undoRun,
  1148. ​
  1149. register: function (def) {
  1150. var i = TOOLS.findIndex(function (x) { return x.name === def.name; });
  1151. if (i >= 0) TOOLS[i] = def;
  1152. else TOOLS.push(def);
  1153. },
  1154. ​
  1155. available: function () {
  1156. return TOOLS.filter(function (tool) {
  1157. if (!ai.allowed(tool.perm)) return false;
  1158. if (tool.perm === "write" && !ai.allowed("read")) return false;
  1159. if (tool.gate && !tool.gate()) return false;
  1160. var scraper = !!ai.search && ai.search.canScrape();
  1161. if (tool.relay && !ai.relayUrl() && !scraper) return false;
  1162. if (tool.name === "save_crawl" && !ai.allowed("web") && !ai.relayUrl() && !scraper) return false;
  1163. return true;
  1164. }).map(function (tool) {
  1165. return { name: tool.name, description: tool.description, parameters: tool.parameters };
  1166. });
  1167. },
  1168. ​
  1169. run: async function (call, ctx) {
  1170. var tool = TOOLS.find(function (x) { return x.name === call.name; });
  1171. if (!tool) return { content: t("ai.tool.err_unknown", { name: call.name }), isError: true };
  1172. if (!ai.allowed(tool.perm)) return { content: t("ai.tool.err_perm", { name: call.name }), isError: true };
  1173. if (call.invalid) return { content: t("ai.tool.err_json"), isError: true };
  1174. if (tool.folder !== false && !A.fs.provider && tool.perm !== "crawler") return { content: t("ai.tool.err_no_folder"), isError: true };
  1175. var input = call.input || {};
  1176. if (ai.redact && RESTORES[tool.name]) {
  1177. var restored = ai.redact.restoreArgs(input);
  1178. if (restored.missing.length) return { content: t("ai.redact.err_missing", { ids: restored.missing.join(", ") }), isError: true };
  1179. input = restored.input;
  1180. }
  1181. try {
  1182. var out = await tool.run(input, ctx || {});
  1183. return typeof out === "string" ? { content: out, isError: false } : out;
  1184. } catch (e) {
  1185. if (e.name === "AbortError") throw e;
  1186. if (!e.tool) console.error(e);
  1187. return { content: e.message || String(e), isError: true };
  1188. }
  1189. },
  1190. ​
  1191. snapshots: async function () {
  1192. return (await loadSnaps()).slice().reverse();
  1193. },
  1194. ​
  1195. clearSnapshots: async function () {
  1196. await loadSnaps();
  1197. snaps.list = [];
  1198. await saveSnaps();
  1199. },
  1200. ​
  1201. projectOutline: async function (maxChars) {
  1202. if (!A.fs.provider) return "";
  1203. maxChars = maxChars || 6000;
  1204. var files = (await A.fs.allFiles()).filter(function (p) { return !ai.isExcluded(p); });
  1205. var lines = [t("ai.tool.project_head", { name: A.fs.provider.name, n: files.length })];
  1206. files.slice(0, 250).forEach(function (p) { lines.push("- " + p); });
  1207. if (files.length > 250) lines.push(t("ai.tool.list_cut", { n: 250 }));
  1208. var key = files.filter(function (p) {
  1209. return /^(index|home|main|style|styles|app|script|functions|config)\.(html?|php|css|js)$/i.test(util.basename(p)) && p.split("/").length <= 2;
  1210. }).slice(0, 8);
  1211. var text = lines.join("\n");
  1212. for (var p of key) {
  1213. if (text.length > maxChars) break;
  1214. try {
  1215. var items = outline(p, await textOf(p)).slice(0, 25);
  1216. if (items.length) text += "\n\n" + t("ai.tool.outline_head", { path: p, total: "?" }) + "\n" + items.join("\n");
  1217. } catch (e) { }
  1218. }
  1219. return text.slice(0, maxChars);
  1220. }
  1221. });
  1222. ​
  1223. A.on("workspace", function () {
  1224. mem = null;
  1225. snaps = { key: null, list: [] };
  1226. A.emit("ai-changes", 0);
  1227. });
  1228. })();
  1229. ​