WebOrbiton
v1.5.0

AtheraStudio Code

1,417 lines · 63.2 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 GEOMETRY_KEY = "ai.window";
  10. var MIN_W = 380;
  11. var MIN_H = 360;
  12. var COMPACT_W = 600;
  13. ​
  14. var sections = [];
  15. var win = null;
  16. var nav = null;
  17. var content = null;
  18. var chip = null;
  19. var current = null;
  20. var renderSeq = 0;
  21. var geom = A.store.get(GEOMETRY_KEY, null);
  22. var dock = { on: !!(geom && geom.docked), w: (geom && geom.dockW) || 420 };
  23. var reopen = !!(geom && geom.open);
  24. var lastSection = geom && geom.section || null;
  25. var wantsFocus = true;
  26. var dockBtn = null;
  27. ​
  28. function clamp(v, min, max) {
  29. return Math.max(min, Math.min(max, v));
  30. }
  31. ​
  32. function defaultGeometry() {
  33. var w = Math.min(680, window.innerWidth - 32);
  34. var h = Math.min(760, window.innerHeight - 96);
  35. return { x: window.innerWidth - w - 16, y: 56, w: w, h: h };
  36. }
  37. ​
  38. function fitGeometry(g) {
  39. var vw = window.innerWidth, vh = window.innerHeight;
  40. var minW = Math.min(MIN_W, vw - 16), minH = Math.min(MIN_H, vh - 16);
  41. var w = clamp(Math.round(g.w), minW, vw - 16);
  42. var h = clamp(Math.round(g.h), minH, vh - 16);
  43. return { x: clamp(Math.round(g.x), 8, vw - w - 8), y: clamp(Math.round(g.y), 8, vh - h - 8), w: w, h: h };
  44. }
  45. ​
  46. function applyGeometry(save) {
  47. geom = fitGeometry(geom || defaultGeometry());
  48. dock.w = clamp(Math.round(dock.w), 320, Math.max(320, Math.round(window.innerWidth * 0.6)));
  49. win.classList.toggle("is-docked", dock.on);
  50. if (dock.on) {
  51. win.style.left = "";
  52. win.style.top = "";
  53. win.style.height = "";
  54. win.style.width = dock.w + "px";
  55. document.documentElement.style.setProperty("--ath-ai-dock-w", dock.w + "px");
  56. } else {
  57. win.style.left = geom.x + "px";
  58. win.style.top = geom.y + "px";
  59. win.style.width = geom.w + "px";
  60. win.style.height = geom.h + "px";
  61. }
  62. win.classList.toggle("is-compact", (dock.on ? dock.w : geom.w) < COMPACT_W);
  63. syncDock();
  64. if (save) A.store.set(GEOMETRY_KEY, Object.assign({ section: current ? current.id : lastSection, docked: dock.on, dockW: dock.w, open: !!win && !win.hidden }, geom));
  65. }
  66. ​
  67. function syncDock() {
  68. var on = dock.on && !!win && !win.hidden;
  69. if (dockBtn) {
  70. var label = t(dock.on ? "ai.window.undock" : "ai.window.dock");
  71. dockBtn.title = label;
  72. dockBtn.setAttribute("aria-label", label);
  73. dockBtn.setAttribute("aria-pressed", dock.on ? "true" : "false");
  74. }
  75. if (document.body.classList.contains("ath-ai-docked") === on) return;
  76. document.body.classList.toggle("ath-ai-docked", on);
  77. if (A.editor && A.editor.view) A.editor.view.requestMeasure();
  78. }
  79. ​
  80. function el(tag, attrs, children) {
  81. return util.el(tag, attrs, children);
  82. }
  83. ​
  84. function iconButton(icon, label, onClick, cls) {
  85. return el("button", {
  86. type: "button",
  87. class: cls || "ath-icon-btn",
  88. title: label,
  89. "aria-label": label,
  90. html: A.icon(icon),
  91. onclick: onClick
  92. });
  93. }
  94. ​
  95. function button(label, kind, onClick, icon) {
  96. return el("button", {
  97. type: "button",
  98. class: "orb-btn orb-btn-sm orb-btn-" + (kind || "secondary"),
  99. html: (icon ? A.icon(icon) : "") + "<span>" + util.esc(label) + "</span>",
  100. onclick: onClick
  101. });
  102. }
  103. ​
  104. function alertBox(tone, title, text, extra) {
  105. return el("div", { class: "orb-alert orb-alert-" + tone + " ath-ai-alert" }, [
  106. el("div", { class: "orb-alert-body" }, [
  107. title ? el("div", { class: "orb-alert-title" }, title) : null,
  108. text ? el("div", { class: "orb-alert-text" }, text) : null
  109. ].concat(extra || []))
  110. ]);
  111. }
  112. ​
  113. function row(label, control, hint) {
  114. return el("div", { class: "ath-set-row ath-ai-row" }, [
  115. el("div", { class: "ath-set-label" }, [el("span", {}, label), hint ? el("small", {}, hint) : null]),
  116. el("div", { class: "ath-set-control" }, control)
  117. ]);
  118. }
  119. ​
  120. function heading(text) {
  121. return el("h3", { class: "ath-ai-subhead" }, text);
  122. }
  123. ​
  124. function hint(text) {
  125. return el("p", { class: "ath-set-hint" }, text);
  126. }
  127. ​
  128. function fail(e) {
  129. if (e && e.name === "AbortError") return;
  130. console.error(e);
  131. A.ui.toast(e && e.message ? e.message : String(e), "danger");
  132. }
  133. ​
  134. function bindSwitch(path, onChange) {
  135. var input = el("input", { type: "checkbox", "data-ai-path": path });
  136. input.checked = !!ai.get(path);
  137. input.addEventListener("change", function () {
  138. if (onChange) Promise.resolve(onChange(input.checked, input)).catch(fail);
  139. else ai.set(path, input.checked);
  140. });
  141. return el("label", { class: "orb-switch" }, [input, el("span", { class: "orb-switch-track" })]);
  142. }
  143. ​
  144. function bindSelect(path, options) {
  145. var value = String(ai.get(path));
  146. var select = el("select", { class: "orb-select", "data-ai-path": path }, options.map(function (o) {
  147. return el("option", { value: o[0], selected: String(o[0]) === value }, o[1]);
  148. }));
  149. select.addEventListener("change", function () { ai.set(path, select.value); });
  150. return select;
  151. }
  152. ​
  153. function bindNumber(path, step, suffix) {
  154. var lim = ai.limits(path) || [0, 1e9];
  155. var input = el("input", { type: "number", class: "orb-input ath-ai-number", min: lim[0], max: lim[1], step: step || 1, value: ai.get(path), "data-ai-path": path });
  156. input.addEventListener("change", function () {
  157. if (input.value === "" || !ai.set(path, Number(input.value))) input.value = ai.get(path);
  158. else input.value = ai.get(path);
  159. });
  160. return suffix ? el("div", { class: "ath-ai-unit" }, [input, el("span", {}, suffix)]) : input;
  161. }
  162. ​
  163. function bindText(path, attrs) {
  164. var input = el("input", Object.assign({ type: "text", class: "orb-input", spellcheck: "false", autocomplete: "off", value: ai.get(path), "data-ai-path": path }, attrs || {}));
  165. input.addEventListener("change", function () {
  166. if (!ai.set(path, input.value)) input.value = ai.get(path);
  167. });
  168. return input;
  169. }
  170. ​
  171. function bindArea(path, rows, max, mono) {
  172. var area = el("textarea", { class: "orb-textarea ath-ai-area" + (mono ? " orb-mono" : ""), rows: rows, spellcheck: mono ? "false" : "true", maxlength: max || null, "data-ai-path": path });
  173. area.value = ai.get(path);
  174. var counter = max ? el("div", { class: "ath-ai-counter" }) : null;
  175. function count() {
  176. if (counter) counter.textContent = t("ai.common.chars", { n: area.value.length, max: max });
  177. }
  178. var save = util.debounce(function () { ai.set(path, area.value); }, 400);
  179. area.addEventListener("input", function () { count(); save(); });
  180. area.addEventListener("blur", function () { ai.set(path, area.value); });
  181. count();
  182. return el("div", { class: "ath-ai-area-wrap" }, [area, counter]);
  183. }
  184. ​
  185. function bindRange(path, step, format) {
  186. var lim = ai.limits(path);
  187. var out = el("span", { class: "ath-range-value" }, format(ai.get(path)));
  188. var input = el("input", { type: "range", class: "orb-range", min: lim[0], max: lim[1], step: step, value: ai.get(path), "data-ai-path": path });
  189. input.addEventListener("input", function () {
  190. out.textContent = format(Number(input.value));
  191. ai.set(path, Number(input.value));
  192. });
  193. return el("div", { class: "ath-range" }, [input, out]);
  194. }
  195. ​
  196. function masterSwitch() {
  197. return bindSwitch("enabled", function (on, input) {
  198. if (!on) {
  199. ai.disable();
  200. return;
  201. }
  202. return ai.enable().then(function (ok) { input.checked = ok; });
  203. });
  204. }
  205. ​
  206. function permRow(perm) {
  207. return row(t("ai.perm." + perm), bindSwitch("perms." + perm), t("ai.perm." + perm + "_hint"));
  208. }
  209. ​
  210. function syncControls() {
  211. if (!win) return;
  212. win.querySelectorAll("[data-ai-path]").forEach(function (n) {
  213. var path = n.getAttribute("data-ai-path");
  214. var v = path === "enabled" ? ai.enabled() : ai.get(path);
  215. if (n.type === "checkbox") n.checked = !!v;
  216. else if (n !== document.activeElement) n.value = v == null ? "" : v;
  217. });
  218. }
  219. ​
  220. function updateChip() {
  221. if (!chip) return;
  222. ai.budget.month().then(function (m) {
  223. var limit = ai.get("budget.monthly");
  224. chip.textContent = limit > 0
  225. ? t("ai.chip.spend_limit", { spent: ai.budget.format(m.cost), limit: ai.budget.format(limit) })
  226. : t("ai.chip.spend", { spent: ai.budget.format(m.cost) });
  227. chip.classList.toggle("is-over", limit > 0 && m.cost >= limit * 0.9);
  228. });
  229. }
  230. ​
  231. function updateMenu() {
  232. var label = document.getElementById("ai-toggle-label");
  233. if (label) {
  234. label.setAttribute("data-ai-i18n", ai.enabled() ? "ai.menu.disable" : "ai.menu.enable");
  235. label.textContent = t(label.getAttribute("data-ai-i18n"));
  236. }
  237. document.body.classList.toggle("ath-ai-on", ai.enabled());
  238. }
  239. ​
  240. function renderNav() {
  241. if (!nav) return;
  242. nav.innerHTML = "";
  243. sections.filter(function (s) { return !s.hidden || !s.hidden(); }).forEach(function (s) {
  244. var label = t(s.label);
  245. var b = el("button", {
  246. type: "button",
  247. class: "ath-ai-nav-item" + (current && current.id === s.id ? " is-active" : ""),
  248. role: "tab",
  249. "aria-selected": current && current.id === s.id ? "true" : "false",
  250. title: label,
  251. html: A.icon(s.icon) + "<span>" + util.esc(label) + "</span>",
  252. onclick: function () { wantsFocus = true; show(s.id); }
  253. });
  254. nav.appendChild(b);
  255. });
  256. }
  257. ​
  258. function show(id) {
  259. var def = sections.find(function (s) { return s.id === id; }) || sections[0];
  260. if (!def) return;
  261. current = def;
  262. lastSection = def.id;
  263. renderNav();
  264. renderCurrent();
  265. applyGeometry(true);
  266. }
  267. ​
  268. function renderCurrent() {
  269. if (!content || !current) return;
  270. var seq = ++renderSeq;
  271. var scroll = content.scrollTop;
  272. var keep = content.getAttribute("data-section") === current.id;
  273. var full = !!current.full;
  274. var box = el("div", { class: full ? "ath-ai-section is-full" : "ath-ai-section ath-settings" });
  275. var head = full ? null : el("h2", { class: "ath-ai-section-title" }, t(current.label));
  276. Promise.resolve().then(function () { return current.render(box); }).then(function () {
  277. if (seq !== renderSeq) return;
  278. content.innerHTML = "";
  279. content.setAttribute("data-section", current.id);
  280. content.classList.toggle("is-full", full);
  281. if (head) content.appendChild(head);
  282. content.appendChild(box);
  283. content.scrollTop = keep ? scroll : 0;
  284. }).catch(function (e) {
  285. if (seq !== renderSeq) return;
  286. content.innerHTML = "";
  287. content.appendChild(alertBox("danger", t("ai.common.error"), e && e.message ? e.message : String(e)));
  288. });
  289. }
  290. ​
  291. function editingInside() {
  292. var a = document.activeElement;
  293. return !!content && content.contains(a) && /^(INPUT|TEXTAREA|SELECT)$/.test(a.tagName) && a.type !== "checkbox";
  294. }
  295. ​
  296. function startPointer(e, onMove, onEnd) {
  297. if (e.button !== 0) return;
  298. e.preventDefault();
  299. var target = e.currentTarget;
  300. target.setPointerCapture(e.pointerId);
  301. document.body.classList.add("ath-resizing");
  302. var sx = e.clientX, sy = e.clientY, start = Object.assign({ dockW: dock.w }, geom);
  303. function move(ev) { onMove(ev.clientX - sx, ev.clientY - sy, start); applyGeometry(false); }
  304. function up() {
  305. target.removeEventListener("pointermove", move);
  306. target.removeEventListener("pointerup", up);
  307. target.removeEventListener("pointercancel", up);
  308. document.body.classList.remove("ath-resizing");
  309. applyGeometry(true);
  310. if (onEnd) onEnd();
  311. }
  312. target.addEventListener("pointermove", move);
  313. target.addEventListener("pointerup", up);
  314. target.addEventListener("pointercancel", up);
  315. }
  316. ​
  317. function resizeHandle(dir) {
  318. var h = el("div", { class: "ath-ai-resize ath-ai-resize-" + dir, "aria-hidden": "true" });
  319. h.addEventListener("pointerdown", function (e) {
  320. startPointer(e, function (dx, dy, s) {
  321. var minW = Math.min(MIN_W, window.innerWidth - 16);
  322. var minH = Math.min(MIN_H, window.innerHeight - 16);
  323. if (dir.indexOf("e") >= 0) geom.w = Math.max(minW, s.w + dx);
  324. if (dock.on) {
  325. if (dir === "w") dock.w = s.dockW - dx;
  326. return;
  327. }
  328. if (dir.indexOf("w") >= 0) {
  329. var w = Math.max(minW, s.w - dx);
  330. geom.x = s.x + (s.w - w);
  331. geom.w = w;
  332. }
  333. if (dir.indexOf("s") >= 0) geom.h = Math.max(minH, s.h + dy);
  334. });
  335. });
  336. return h;
  337. }
  338. ​
  339. function build() {
  340. var title = el("span", { class: "ath-ai-title", id: "ath-ai-title" });
  341. chip = el("button", { type: "button", class: "ath-ai-chip", onclick: function () { show("model"); } });
  342. var head = el("header", { class: "ath-ai-head" }, [
  343. el("span", { class: "ath-ai-logo", html: A.icon("sparkles") }),
  344. title,
  345. chip,
  346. el("span", { class: "ath-ai-spacer" }),
  347. el("span", { class: "ath-ai-master", "data-ai-i18n-title": "ai.window.master" }, masterSwitch()),
  348. (dockBtn = iconButton("sidebar", t("ai.window.dock"), function () { aiUi.setDocked(!dock.on); })),
  349. iconButton("x", A.t("common.close"), function () { aiUi.close(); })
  350. ]);
  351. head.addEventListener("pointerdown", function (e) {
  352. if (dock.on || e.target.closest("button, input, label, select, a")) return;
  353. startPointer(e, function (dx, dy, s) { geom.x = s.x + dx; geom.y = s.y + dy; });
  354. });
  355. head.addEventListener("dblclick", function (e) {
  356. if (dock.on || e.target.closest("button, input, label, select, a")) return;
  357. geom = defaultGeometry();
  358. applyGeometry(true);
  359. });
  360. nav = el("nav", { class: "ath-ai-nav", role: "tablist", "data-ai-i18n-title": "ai.window.sections" });
  361. content = el("div", { class: "ath-ai-content", role: "tabpanel" });
  362. win = el("div", { class: "ath-ai-window", role: "dialog", "aria-modal": "false", "aria-labelledby": "ath-ai-title", tabindex: "-1", hidden: true }, [
  363. head,
  364. el("div", { class: "ath-ai-body" }, [nav, content]),
  365. resizeHandle("w"), resizeHandle("e"), resizeHandle("s"), resizeHandle("sw"), resizeHandle("se")
  366. ]);
  367. win.addEventListener("keydown", function (e) {
  368. if (e.key !== "Escape" || /^(INPUT|TEXTAREA|SELECT)$/.test(e.target.tagName)) return;
  369. e.preventDefault();
  370. e.stopPropagation();
  371. aiUi.close();
  372. });
  373. document.body.appendChild(win);
  374. updateTitle();
  375. }
  376. ​
  377. function updateTitle() {
  378. if (!win) return;
  379. win.querySelector(".ath-ai-title").textContent = ai.name();
  380. ai.applyLabels(win);
  381. }
  382. ​
  383. var aiUi = (A.aiUi = {
  384. section: function (def) {
  385. sections = sections.filter(function (s) { return s.id !== def.id; });
  386. sections.push(def);
  387. sections.sort(function (a, b) { return a.order - b.order; });
  388. if (win && !win.hidden) renderNav();
  389. },
  390. ​
  391. open: function (id, opts) {
  392. if (!win) build();
  393. var opening = win.hidden;
  394. win.hidden = false;
  395. if (opening) {
  396. applyGeometry(false);
  397. updateChip();
  398. }
  399. wantsFocus = !opts || opts.focus !== false;
  400. if (id || !current) show(id || lastSection || sections[0].id);
  401. else if (opening) renderCurrent();
  402. applyGeometry(true);
  403. if (!opts || opts.focus !== false) win.focus({ preventScroll: true });
  404. A.emit("ai-window", true);
  405. },
  406. ​
  407. close: function () {
  408. if (!win || win.hidden) return;
  409. win.hidden = true;
  410. applyGeometry(true);
  411. A.emit("ai-window", false);
  412. var focusTarget = A.tabs && A.tabs.active && A.tabs.active.kind === "text" ? A.editor.view : null;
  413. if (focusTarget) focusTarget.focus();
  414. },
  415. ​
  416. toggle: function () {
  417. if (win && !win.hidden && win.contains(document.activeElement)) aiUi.close();
  418. else aiUi.open();
  419. },
  420. ​
  421. isOpen: function () {
  422. return !!win && !win.hidden;
  423. },
  424. ​
  425. wantsFocus: function () {
  426. return wantsFocus;
  427. },
  428. ​
  429. isDocked: function () {
  430. return dock.on;
  431. },
  432. ​
  433. setDocked: function (on) {
  434. dock.on = !!on;
  435. if (!win) build();
  436. if (win.hidden) aiUi.open();
  437. else applyGeometry(true);
  438. applyGeometry(true);
  439. if (current && current.id === "overview" && !editingInside()) renderCurrent();
  440. },
  441. ​
  442. current: function () {
  443. return current ? current.id : null;
  444. },
  445. ​
  446. refresh: function () {
  447. if (aiUi.isOpen() && !editingInside()) renderCurrent();
  448. },
  449. ​
  450. el: el,
  451. row: row,
  452. heading: heading,
  453. hint: hint,
  454. alert: alertBox,
  455. button: button,
  456. iconButton: iconButton,
  457. bindSwitch: bindSwitch,
  458. bindSelect: bindSelect,
  459. bindNumber: bindNumber,
  460. bindText: bindText,
  461. bindArea: bindArea,
  462. bindRange: bindRange,
  463. permRow: permRow,
  464. fail: fail,
  465. ​
  466. init: function () {
  467. ai.applyLabels(document);
  468. updateMenu();
  469. window.addEventListener("resize", util.debounce(function () { if (aiUi.isOpen()) applyGeometry(true); }, 150));
  470. if (reopen) setTimeout(function () { aiUi.open(null, { focus: false }); }, 0);
  471. }
  472. });
  473. ​
  474. A.on("ai-config", function (path) {
  475. if (path === "enabled" || path === "consent") updateMenu();
  476. syncControls();
  477. if (/^budget\./.test(path)) updateChip();
  478. if (current && current.live && current.live(path) && !editingInside()) renderCurrent();
  479. });
  480. A.on("ai-usage", function () {
  481. updateChip();
  482. if (current && current.id === "model" && aiUi.isOpen()) refreshLive();
  483. });
  484. A.on("ai-keys", function () { if (current && (current.id === "keys" || current.id === "overview" || current.id === "network")) aiUi.refresh(); });
  485. A.on("ai-name", function () { updateTitle(); renderNav(); aiUi.refresh(); });
  486. A.on("lang", function () { updateMenu(); updateTitle(); renderNav(); if (aiUi.isOpen()) renderCurrent(); updateChip(); });
  487. ​
  488. var liveBox = null;
  489. ​
  490. function refreshLive() {
  491. if (!liveBox || !document.contains(liveBox)) return;
  492. renderLive(liveBox);
  493. }
  494. ​
  495. async function renderLive(box) {
  496. var m = await ai.budget.month();
  497. var blocked = await ai.budget.blocked();
  498. var limit = ai.get("budget.monthly");
  499. var s = ai.budget.session();
  500. box.innerHTML = "";
  501. var ratio = limit > 0 ? Math.min(1, m.cost / limit) : 0;
  502. box.appendChild(el("div", { class: "ath-ai-spend" }, [
  503. el("div", { class: "ath-ai-spend-main" }, [
  504. el("strong", {}, ai.budget.format(m.cost)),
  505. el("span", {}, limit > 0 ? t("ai.budget.of_limit", { limit: ai.budget.format(limit) }) : t("ai.budget.no_limit"))
  506. ]),
  507. limit > 0 ? el("div", { class: "orb-progress ath-ai-progress" + (ratio >= 0.9 ? " is-over" : "") }, [
  508. el("div", { class: "orb-progress-bar", style: "width:" + (ratio * 100).toFixed(1) + "%" })
  509. ]) : null,
  510. el("div", { class: "ath-ai-stats" }, [
  511. el("span", {}, t("ai.budget.requests", { n: m.requests })),
  512. el("span", {}, t("ai.budget.cache_rate", { rate: ai.budget.percent(ai.budget.cacheHitRate(m)) })),
  513. el("span", {}, t("ai.budget.searches", { n: m.searches })),
  514. el("span", {}, t("ai.budget.session", { cost: ai.budget.format(s.cost) }))
  515. ])
  516. ]));
  517. if (m.unpriced) box.appendChild(alertBox("warning", null, t("ai.budget.unpriced", { n: m.unpriced })));
  518. if (blocked) {
  519. box.appendChild(alertBox("danger", t("ai.budget.blocked_title"), t("ai.budget.blocked_text", { month: blocked.month }), [
  520. el("div", { class: "ath-ai-actions" }, [button(t("ai.budget.unblock"), "secondary", function () { ai.budget.unblock().catch(fail); })])
  521. ]));
  522. }
  523. box.appendChild(el("div", { class: "ath-ai-actions" }, [
  524. button(t("ai.budget.clear"), "ghost", async function () {
  525. if (await A.ui.confirm(t("ai.budget.clear"), t("ai.budget.clear_confirm"), A.t("common.delete"), true)) ai.budget.clear().catch(fail);
  526. }, "trash")
  527. ]));
  528. }
  529. ​
  530. function modelTable() {
  531. var rows = ai.models();
  532. var wrap = el("div", { class: "ath-ai-table-wrap" });
  533. var save = util.debounce(function () { ai.setModels(rows); }, 400);
  534. var roleOptions = [["", t("ai.models.role_none")]].concat(ai.ROLES.map(function (r) { return [r, t("ai.role." + r)]; }));
  535. ​
  536. function priceInput(r, f) {
  537. var input = el("input", {
  538. type: "number",
  539. class: "orb-input ath-ai-price",
  540. min: "0",
  541. step: "any",
  542. placeholder: "?",
  543. value: r[f] == null ? "" : r[f],
  544. "aria-label": t("ai.models.col_" + f)
  545. });
  546. input.addEventListener("change", function () {
  547. var n = input.value === "" ? null : Number(input.value);
  548. r[f] = n != null && isFinite(n) && n >= 0 ? n : null;
  549. input.value = r[f] == null ? "" : r[f];
  550. save();
  551. mark();
  552. });
  553. return input;
  554. }
  555. ​
  556. var tbody = el("tbody");
  557. ​
  558. function mark() {
  559. Array.prototype.forEach.call(tbody.children, function (tr, i) {
  560. var r = rows[i];
  561. var unpriced = !!r.model && (r.input == null || r.output == null);
  562. tr.classList.toggle("is-unpriced", unpriced);
  563. tr.title = unpriced ? t("ai.models.unpriced") : "";
  564. });
  565. }
  566. ​
  567. function draw() {
  568. tbody.innerHTML = "";
  569. rows.forEach(function (r, i) {
  570. var provider = el("select", { class: "orb-select ath-ai-cell-select", "aria-label": t("ai.models.col_provider") }, ai.PROVIDERS.map(function (p) {
  571. return el("option", { value: p, selected: p === r.provider }, ai.providerLabel(p));
  572. }));
  573. provider.addEventListener("change", function () { r.provider = provider.value; save(); });
  574. var model = el("input", { type: "text", class: "orb-input orb-mono ath-ai-model-id", value: r.model, spellcheck: "false", autocomplete: "off", placeholder: t("ai.models.model_ph"), "aria-label": t("ai.models.col_model") });
  575. model.addEventListener("change", function () { r.model = model.value.trim(); save(); mark(); });
  576. var role = el("select", { class: "orb-select ath-ai-cell-select", "aria-label": t("ai.models.col_role") }, roleOptions.map(function (o) {
  577. return el("option", { value: o[0], selected: o[0] === r.role }, o[1]);
  578. }));
  579. role.addEventListener("change", function () { r.role = role.value; save(); });
  580. var vision = el("input", { type: "checkbox", "aria-label": t("ai.models.col_vision") });
  581. vision.checked = !!r.vision;
  582. vision.addEventListener("change", function () { r.vision = vision.checked; save(); });
  583. var del = iconButton("trash", t("ai.models.remove"), function () {
  584. rows.splice(i, 1);
  585. ai.setModels(rows);
  586. draw();
  587. });
  588. tbody.appendChild(el("tr", {}, [
  589. el("td", {}, provider),
  590. el("td", {}, model),
  591. el("td", {}, role),
  592. el("td", { class: "ath-ai-center" }, el("label", { class: "orb-checkbox" }, vision)),
  593. el("td", {}, priceInput(r, "input")),
  594. el("td", {}, priceInput(r, "output")),
  595. el("td", {}, priceInput(r, "cacheRead")),
  596. el("td", {}, priceInput(r, "cacheWrite")),
  597. el("td", {}, priceInput(r, "search")),
  598. el("td", {}, del)
  599. ]));
  600. });
  601. mark();
  602. }
  603. ​
  604. var cols = ["provider", "model", "role", "vision", "input", "output", "cacheRead", "cacheWrite", "search", ""];
  605. var table = el("table", { class: "orb-table ath-ai-models" }, [
  606. el("thead", {}, el("tr", {}, cols.map(function (c) {
  607. return el("th", { title: c ? t("ai.models.col_" + c + "_hint") : null }, c ? t("ai.models.col_" + c) : "");
  608. }))),
  609. tbody
  610. ]);
  611. draw();
  612. wrap.appendChild(table);
  613. return el("div", {}, [
  614. wrap,
  615. el("div", { class: "ath-ai-actions" }, [
  616. button(t("ai.models.add"), "secondary", function () {
  617. rows.push(ai.blankModel(ai.get("mode") === "auto" ? "openai" : ai.get("mode")));
  618. ai.setModels(rows);
  619. draw();
  620. var last = tbody.lastElementChild && tbody.lastElementChild.querySelector(".ath-ai-model-id");
  621. if (last) last.focus();
  622. }, "plus"),
  623. button(t("ai.models.reset"), "ghost", async function () {
  624. if (!(await A.ui.confirm(t("ai.models.reset"), t("ai.models.reset_confirm"), t("ai.models.reset"), true))) return;
  625. ai.resetModels();
  626. rows = ai.models();
  627. draw();
  628. }, "undo")
  629. ])
  630. ]);
  631. }
  632. ​
  633. function routerProbe() {
  634. var input = el("textarea", { class: "orb-textarea ath-ai-area", rows: 2, placeholder: t("ai.router.probe_ph"), "aria-label": t("ai.router.probe") });
  635. var out = el("div", { class: "ath-ai-probe-out", "aria-live": "polite" });
  636. function run() {
  637. var parsed = ai.router.parseOverride(input.value);
  638. if (!parsed.text.trim()) { out.textContent = ""; return; }
  639. ai.router.decide({ text: parsed.text, contextTokens: ai.budget.tokens(parsed.text) + 3000 }, parsed.override).then(function (d) {
  640. var est = ai.router.estimate(d, { input: ai.budget.tokens(parsed.text) + 3000 });
  641. out.innerHTML = "";
  642. out.appendChild(el("div", {}, ai.router.describe(d)));
  643. out.appendChild(el("small", {}, t("ai.router.estimate", {
  644. cost: est.known ? ai.budget.format(est.cost) : t("ai.common.unknown"),
  645. effort: d.effort
  646. })));
  647. }).catch(function (e) {
  648. out.innerHTML = "";
  649. out.appendChild(el("div", { class: "is-error" }, e.message));
  650. });
  651. }
  652. input.addEventListener("input", util.debounce(run, 300));
  653. return el("div", { class: "ath-ai-probe-wrap" }, [
  654. heading(t("ai.router.probe")),
  655. hint(t("ai.router.probe_hint")),
  656. input,
  657. out
  658. ]);
  659. }
  660. ​
  661. function engineRow(id) {
  662. var engine = ai.search.ENGINES[id];
  663. var status = ai.keys.statusSync();
  664. var info = status.providers["search:" + id];
  665. var path = "search.engines." + id;
  666. var relayReady = !!ai.relayUrl();
  667. var reach = engine.direct ? t("ai.search.direct") : relayReady ? t("ai.search.via_relay") : t("ai.search.needs_relay");
  668. var input = el("input", { type: "password", class: "orb-input orb-mono", autocomplete: "off", spellcheck: "false", placeholder: t(info ? "ai.keys.replace_ph" : "ai.keys.paste_ph"), "aria-label": t("ai.keys.input_label", { provider: engine.label }) });
  669. function saveKey() {
  670. if (!input.value.trim()) return;
  671. ai.keys.set("search:" + id, input.value).then(function () {
  672. input.value = "";
  673. A.ui.toast(t("ai.keys.saved_toast", { provider: engine.label }), "success");
  674. }).catch(fail);
  675. }
  676. input.addEventListener("keydown", function (e) { if (e.key === "Enter") { e.preventDefault(); saveKey(); } });
  677. var fee = cfgFee(id);
  678. var feeInput = el("input", { type: "number", class: "orb-input ath-ai-number ath-ai-fee", min: "0", step: "any", placeholder: "?", value: fee == null ? "" : fee, "aria-label": t("ai.search.fee"), title: t("ai.search.fee_hint") });
  679. feeInput.addEventListener("change", function () {
  680. var v = feeInput.value === "" ? null : Number(feeInput.value);
  681. if (!ai.set(path + ".fee", v != null && isFinite(v) && v >= 0 ? v : null)) feeInput.value = cfgFee(id) == null ? "" : cfgFee(id);
  682. });
  683. var testBtn = info ? button(t("ai.keys.test"), "secondary", function () {
  684. if (!ai.enabled()) return A.ui.toast(t("ai.err.disabled"), "info");
  685. testBtn.disabled = true;
  686. ai.search.test(id).then(function (r) {
  687. A.ui.toast(t("ai.search.test_ok", { engine: engine.label, n: r.n, ms: r.ms }), "success");
  688. }).catch(fail).then(function () { testBtn.disabled = false; });
  689. }) : null;
  690. var toggle = bindSwitch(path + ".on");
  691. if (!engine.direct && !relayReady) toggle.querySelector("input").disabled = true;
  692. var controls = el("div", { class: "ath-ai-engine" }, [
  693. el("div", { class: "ath-ai-key-control" }, [
  694. input,
  695. button(A.t("common.save"), "primary", saveKey),
  696. testBtn,
  697. info ? iconButton("trash", t("ai.keys.remove", { provider: engine.label }), async function () {
  698. if (await A.ui.confirm(t("ai.keys.remove", { provider: engine.label }), t("ai.keys.remove_confirm", { provider: engine.label }), A.t("common.delete"), true)) {
  699. ai.keys.remove("search:" + id).catch(fail);
  700. }
  701. }) : null
  702. ]),
  703. el("label", { class: "ath-ai-fee-row" }, [el("span", {}, t("ai.search.fee")), feeInput, el("span", {}, "USD")])
  704. ]);
  705. var head = el("span", { class: "ath-ai-engine-head" }, [
  706. el("span", { class: "ath-ai-engine-name" }, [
  707. el("span", {}, engine.label),
  708. engine.direct ? null : el("span", { class: "orb-badge orb-tone-warning ath-ai-relay-badge", title: t("ai.relay.badge_title") }, t("ai.relay.badge"))
  709. ]),
  710. toggle
  711. ]);
  712. var desc = engine.host + " · " + reach + " · " + (info ? t("ai.keys.saved", { hint: info.hint }) : t("ai.keys.missing")) + (engine.scrape ? " · " + t("ai.search.scrape_note") : "");
  713. var r = row(head, controls, desc);
  714. r.classList.add("is-stacked", "ath-ai-engine-row");
  715. if (!engine.direct && !relayReady) r.classList.add("is-disabled");
  716. return r;
  717. }
  718. ​
  719. function cfgFee(id) {
  720. var v = ai.get("search.engines." + id + ".fee");
  721. return typeof v === "number" ? v : null;
  722. }
  723. ​
  724. function keyRow(p, status) {
  725. var info = status.providers[p];
  726. var input = el("input", {
  727. type: "password",
  728. class: "orb-input orb-mono",
  729. autocomplete: "off",
  730. spellcheck: "false",
  731. placeholder: t(info ? "ai.keys.replace_ph" : "ai.keys.paste_ph"),
  732. "aria-label": t("ai.keys.input_label", { provider: ai.providerLabel(p) })
  733. });
  734. function saveKey() {
  735. if (!input.value.trim()) return;
  736. ai.keys.set(p, input.value).then(function () {
  737. input.value = "";
  738. A.ui.toast(t("ai.keys.saved_toast", { provider: ai.providerLabel(p) }), "success");
  739. }).catch(fail);
  740. }
  741. input.addEventListener("keydown", function (e) { if (e.key === "Enter") { e.preventDefault(); saveKey(); } });
  742. var testBtn = info ? button(t("ai.keys.test"), "secondary", function () {
  743. if (!ai.enabled()) return A.ui.toast(t("ai.err.disabled"), "info");
  744. testBtn.disabled = true;
  745. ai.providers.test(p).then(function (r) {
  746. A.ui.toast(t("ai.keys.test_ok", {
  747. provider: ai.providerLabel(p),
  748. model: r.model,
  749. ms: r.ms,
  750. cost: r.costKnown ? ai.budget.format(r.cost) : t("ai.common.unknown")
  751. }), "success");
  752. }).catch(fail).then(function () { testBtn.disabled = false; });
  753. }) : null;
  754. var controls = el("div", { class: "ath-ai-key-control" }, [
  755. input,
  756. button(A.t("common.save"), "primary", saveKey),
  757. testBtn,
  758. info ? iconButton("trash", t("ai.keys.remove", { provider: ai.providerLabel(p) }), async function () {
  759. if (await A.ui.confirm(t("ai.keys.remove", { provider: ai.providerLabel(p) }), t("ai.keys.remove_confirm", { provider: ai.providerLabel(p) }), A.t("common.delete"), true)) {
  760. ai.keys.remove(p).catch(fail);
  761. }
  762. }) : null
  763. ]);
  764. var desc = t("ai.keys.goes_to", { host: ai.PROVIDER_HOSTS[p] }) + (info ? " " + t("ai.keys.saved", { hint: info.hint }) : " " + t("ai.keys.missing"));
  765. var r = row(ai.providerLabel(p), controls, desc);
  766. r.classList.add("is-stacked");
  767. return r;
  768. }
  769. ​
  770. function passwordPair(onSubmit, label) {
  771. var a = el("input", { type: "password", class: "orb-input", autocomplete: "new-password", placeholder: t("ai.keys.pass_new"), "aria-label": t("ai.keys.pass_new") });
  772. var b = el("input", { type: "password", class: "orb-input", autocomplete: "new-password", placeholder: t("ai.keys.pass_repeat"), "aria-label": t("ai.keys.pass_repeat") });
  773. var go = button(label, "primary", function () {
  774. if (a.value !== b.value) return A.ui.toast(t("ai.keys.pass_mismatch"), "warning");
  775. go.disabled = true;
  776. onSubmit(a.value).then(function () {
  777. a.value = b.value = "";
  778. }).catch(fail).then(function () { go.disabled = false; });
  779. }, "lock");
  780. return el("div", { class: "ath-ai-pass" }, [a, b, go]);
  781. }
  782. ​
  783. aiUi.section({
  784. id: "overview",
  785. order: 10,
  786. icon: "sparkles",
  787. label: "ai.sec.overview",
  788. live: function (path) { return path === "enabled" || path === "consent" || path === "relay.on"; },
  789. render: async function (box) {
  790. var on = ai.enabled();
  791. var status = await ai.keys.status();
  792. var keyCount = Object.keys(status.providers).length;
  793. var month = await ai.budget.month();
  794. var limit = ai.get("budget.monthly");
  795. box.appendChild(on
  796. ? alertBox("success", t("ai.ov.on_title"), t("ai.ov.on_text"))
  797. : alertBox("info", t("ai.ov.off_title"), t("ai.ov.off_text"), [
  798. el("div", { class: "ath-ai-actions" }, [button(t("ai.ov.enable"), "primary", function () { ai.enable().catch(fail); }, "power")])
  799. ]));
  800. box.appendChild(row(t("ai.ov.master"), masterSwitch(), t("ai.ov.master_hint")));
  801. ​
  802. var nameInput = el("input", { type: "text", class: "orb-input", maxlength: ai.NAME_MAX, value: ai.name(), placeholder: ai.DEFAULT_NAME, spellcheck: "false", autocomplete: "off" });
  803. nameInput.addEventListener("change", function () { nameInput.value = ai.setName(nameInput.value); });
  804. box.appendChild(row(t("ai.ov.name"), nameInput, t("ai.ov.name_hint", { max: ai.NAME_MAX })));
  805. ​
  806. var modeSelect = el("select", { class: "orb-select" }, [
  807. el("option", { value: "float", selected: !dock.on }, t("ai.window.mode_float")),
  808. el("option", { value: "dock", selected: dock.on }, t("ai.window.mode_dock"))
  809. ]);
  810. modeSelect.addEventListener("change", function () { aiUi.setDocked(modeSelect.value === "dock"); });
  811. box.appendChild(row(t("ai.window.mode"), modeSelect, t("ai.window.mode_hint")));
  812. ​
  813. box.appendChild(row(t("ai.ov.keys"), button(t("ai.ov.keys_manage"), "secondary", function () { show("keys"); }, "key"),
  814. status.locked ? t("ai.ov.keys_locked") : keyCount ? t("ai.ov.keys_count", { n: keyCount }) : t("ai.ov.keys_none")));
  815. box.appendChild(row(t("ai.ov.spend"), button(t("ai.ov.spend_manage"), "secondary", function () { show("model"); }, "coins"),
  816. limit > 0 ? t("ai.ov.spend_limit", { spent: ai.budget.format(month.cost), limit: ai.budget.format(limit) }) : t("ai.ov.spend_nolimit", { spent: ai.budget.format(month.cost) })));
  817. ​
  818. box.appendChild(row(t("ai.ov.relay"), button(t("ai.relay.setup"), "secondary", function () { show("relay"); }, "globe"),
  819. ai.relayUrl() ? t("ai.ov.relay_on", { url: ai.relayUrl() }) : t("ai.ov.relay_off")));
  820. ​
  821. box.appendChild(heading(t("ai.conv.head")));
  822. box.appendChild(row(t("ai.conv.sync"), bindSwitch("conversation.sync"), t("ai.conv.sync_hint")));
  823. box.appendChild(row(t("ai.conv.keep"), bindNumber("conversation.keep", 1), t("ai.conv.keep_hint")));
  824. box.appendChild(row(t("ai.conv.compact"), bindNumber("conversation.compactAt", 1000, t("ai.conv.chars")), t("ai.conv.compact_hint")));
  825. ​
  826. box.appendChild(heading(t("ai.ov.privacy_head")));
  827. if (ai.consented()) {
  828. var when = new Date(ai.consentDate());
  829. box.appendChild(row(t("ai.ov.consent"), button(t("ai.ov.consent_revoke"), "ghost", async function () {
  830. if (await A.ui.confirm(t("ai.ov.consent_revoke"), t("ai.ov.consent_revoke_confirm"), t("ai.ov.consent_revoke"), true)) ai.revokeConsent();
  831. }), t("ai.ov.consent_at", { date: isNaN(when) ? "" : when.toLocaleString(document.documentElement.lang || undefined) })));
  832. } else {
  833. box.appendChild(hint(t("ai.ov.consent_none")));
  834. }
  835. box.appendChild(hint(t("ai.ov.api_only")));
  836. }
  837. });
  838. ​
  839. aiUi.section({
  840. id: "instructions",
  841. order: 30,
  842. icon: "pencil",
  843. label: "ai.sec.instructions",
  844. render: async function (box) {
  845. box.appendChild(hint(t("ai.ins.intro")));
  846. box.appendChild(heading(t("ai.ins.global")));
  847. box.appendChild(bindArea("instructions", 10, ai.INSTRUCTIONS_MAX, false));
  848. box.appendChild(heading(t("ai.ins.project")));
  849. if (!A.fs.provider) {
  850. box.appendChild(hint(t("ai.priv.no_folder")));
  851. } else {
  852. var projectText = await ai.private.read("instructions.md");
  853. box.appendChild(hint(t(projectText != null ? "ai.ins.project_on" : "ai.ins.project_off")));
  854. if (projectText != null && projectText.trim()) {
  855. box.appendChild(el("pre", { class: "ath-ai-snippet orb-mono" }, projectText.trim().split("\n").slice(0, 12).join("\n")));
  856. }
  857. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t(projectText != null ? "ai.priv.open" : "ai.priv.create"), projectText != null ? "secondary" : "primary", function () {
  858. openPrivateFile("instructions.md").then(function () { aiUi.refresh(); }).catch(fail);
  859. }, projectText != null ? "file" : "filePlus")]));
  860. }
  861. box.appendChild(hint(t("ai.ins.precedence")));
  862. }
  863. });
  864. ​
  865. function privateEditor(name) {
  866. return ai.private.read(name).then(function (text) {
  867. var area = el("textarea", { class: "orb-textarea ath-ai-area orb-mono", rows: 18, spellcheck: "false" });
  868. area.value = text == null ? ai.private.template(name) : text;
  869. return A.ui.dialog({
  870. title: ai.private.path(name),
  871. size: "lg",
  872. body: el("div", {}, [hint(t("ai.priv.memory_editor")), area]),
  873. actions: [
  874. { label: A.t("common.cancel"), value: null, kind: "ghost" },
  875. { label: A.t("common.save"), value: "save", kind: "primary" }
  876. ]
  877. }).then(function (r) {
  878. if (r === "save") return ai.private.write(name, area.value).then(function () { A.ui.toast(t("ai.priv.saved", { name: name }), "success"); });
  879. });
  880. });
  881. }
  882. ​
  883. async function openPrivateFile(name) {
  884. if (ai.private.memoryMode()) return privateEditor(name);
  885. if (!(await ai.private.create(name))) return;
  886. await A.tabs.openPath(ai.private.path(name));
  887. }
  888. ​
  889. async function openPrivateDir(dir) {
  890. if (!(await ai.private.createDir(dir))) return;
  891. if (ai.private.memoryMode()) return A.ui.toast(t("ai.priv.memory_dir"), "info");
  892. if (document.body.classList.contains("ath-no-sidebar")) A.commands.toggleSidebar();
  893. await A.explorer.reveal(ai.private.path(dir));
  894. }
  895. ​
  896. aiUi.section({
  897. id: "private",
  898. order: 40,
  899. icon: "folder",
  900. label: "ai.sec.private",
  901. render: async function (box) {
  902. var st = await ai.private.status();
  903. if (!st.folder) {
  904. box.appendChild(alertBox("info", null, t("ai.priv.no_folder")));
  905. return;
  906. }
  907. box.appendChild(hint(t("ai.priv.intro")));
  908. if (st.memory) box.appendChild(alertBox("warning", t("ai.priv.memory_title"), t("ai.priv.memory_text")));
  909. ai.private.FILES.forEach(function (name) {
  910. var exists = st.files[name];
  911. var b = button(t(exists ? "ai.priv.open" : "ai.priv.create"), exists ? "secondary" : "primary", function () {
  912. openPrivateFile(name).then(function () { aiUi.refresh(); }).catch(fail);
  913. }, exists ? "file" : "filePlus");
  914. box.appendChild(row(name, b, t("ai.priv.desc_" + name.replace(/\.md$/, "").toLowerCase())));
  915. });
  916. box.appendChild(heading(t("ai.priv.folders")));
  917. ai.private.DIRS.forEach(function (dir) {
  918. box.appendChild(row(dir + "/", button(t("ai.priv.open_dir"), "secondary", function () {
  919. openPrivateDir(dir).then(function () { aiUi.refresh(); }).catch(fail);
  920. }, "folderOpen"), t("ai.priv.desc_" + dir)));
  921. });
  922. if (!st.memory) {
  923. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t("ai.priv.gitignore"), "ghost", function () {
  924. ai.private.addToGitignore().then(function (added) {
  925. A.ui.toast(t(added ? "ai.priv.gitignore_added" : "ai.priv.gitignore_present"), added ? "success" : "info");
  926. }).catch(fail);
  927. }, "shield")]));
  928. box.appendChild(hint(t("ai.priv.safety")));
  929. }
  930. }
  931. });
  932. ​
  933. aiUi.section({
  934. id: "changes",
  935. order: 45,
  936. icon: "undo",
  937. label: "ai.sec.changes",
  938. render: async function (box) {
  939. if (!A.fs.provider) {
  940. box.appendChild(alertBox("info", null, t("ai.priv.no_folder")));
  941. return;
  942. }
  943. var list = await ai.tools.snapshots();
  944. box.appendChild(hint(t(ai.get("snapshots") ? "ai.changes.intro" : "ai.changes.off")));
  945. if (!list.length) {
  946. box.appendChild(el("p", { class: "ath-set-empty" }, t("ai.changes.empty")));
  947. return;
  948. }
  949. var wrap = el("div", { class: "ath-ai-changes" });
  950. list.forEach(function (rec) {
  951. var when = new Date(rec.at).toLocaleString(document.documentElement.lang || undefined);
  952. var actions = el("div", { class: "ath-ai-change-actions" }, [
  953. rec.op === "move" ? null : iconButton("eye", t("ai.changes.show_diff"), function () {
  954. A.ui.dialog({
  955. title: t("ai.change.op_" + rec.op, { path: rec.path, to: rec.to || "" }),
  956. size: "lg",
  957. body: ai.tools.diffView(rec.before, rec.after),
  958. actions: [{ label: A.t("common.close"), value: null, kind: "secondary" }]
  959. });
  960. }),
  961. rec.undone ? null : iconButton("undo", t("ai.change.undo"), function () {
  962. ai.tools.undo(rec.id).then(function () { aiUi.refresh(); }).catch(fail);
  963. })
  964. ]);
  965. wrap.appendChild(el("div", { class: "ath-ai-change" + (rec.undone ? " is-undone" : "") }, [
  966. el("div", { class: "ath-ai-change-main" }, [
  967. el("span", { class: "ath-ai-change-op" }, t("ai.change.op_" + rec.op, { path: rec.path, to: rec.to || "" })),
  968. el("small", {}, when + (rec.undone ? " · " + t("ai.changes.undone_tag") : ""))
  969. ]),
  970. actions
  971. ]));
  972. });
  973. box.appendChild(wrap);
  974. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t("ai.changes.clear"), "ghost", async function () {
  975. if (await A.ui.confirm(t("ai.changes.clear"), t("ai.changes.clear_confirm"), A.t("common.delete"), true)) {
  976. ai.tools.clearSnapshots().then(function () { aiUi.refresh(); }).catch(fail);
  977. }
  978. }, "trash")]));
  979. }
  980. });
  981. ​
  982. aiUi.section({
  983. id: "recipes",
  984. order: 47,
  985. icon: "code",
  986. label: "ai.sec.recipes",
  987. render: async function (box) {
  988. box.appendChild(hint(t("ai.recipes.intro")));
  989. box.appendChild(heading(t("ai.recipes.builtin")));
  990. var builtin = el("div", { class: "ath-ai-cmd-list" });
  991. ai.commands.ORDER.forEach(function (name) {
  992. builtin.appendChild(el("div", { class: "ath-ai-cmd" }, [el("code", {}, "/" + name), el("span", {}, t("ai.cmd.desc_" + name))]));
  993. });
  994. box.appendChild(builtin);
  995. box.appendChild(heading(t("ai.recipes.own")));
  996. if (!A.fs.provider) {
  997. box.appendChild(hint(t("ai.priv.no_folder")));
  998. return;
  999. }
  1000. var recipes = await ai.commands.loadRecipes(true);
  1001. if (!recipes.length) box.appendChild(el("p", { class: "ath-set-empty" }, t("ai.recipes.empty")));
  1002. recipes.forEach(function (r) {
  1003. var rel = r.path.replace(/^\.private\//, "");
  1004. box.appendChild(row(el("code", {}, "/" + r.name), el("div", { class: "ath-ai-actions" }, [
  1005. button(t("ai.priv.open"), "secondary", function () { openPrivateFile(rel).catch(fail); }, "file"),
  1006. iconButton("trash", A.t("common.delete"), async function () {
  1007. if (!(await A.ui.confirm(A.t("common.delete"), t("ai.recipes.delete_confirm", { name: r.name }), A.t("common.delete"), true))) return;
  1008. await ai.tools.io.remove(r.path);
  1009. A.emit("files-changed");
  1010. aiUi.refresh();
  1011. })
  1012. ]), (r.description || "") + (r.tier ? " · " + t("ai.role." + r.tier) : "")));
  1013. });
  1014. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t("ai.recipes.new"), "primary", async function () {
  1015. var name = await A.ui.prompt(t("ai.recipes.new"), t("ai.recipes.name_label"), "", A.t("common.create"));
  1016. if (!name) return;
  1017. try {
  1018. var path = await ai.commands.createRecipe(name);
  1019. if (path) await openPrivateFile(path.replace(/^\.private\//, ""));
  1020. aiUi.refresh();
  1021. } catch (e) {
  1022. fail(e);
  1023. }
  1024. }, "plus")]));
  1025. box.appendChild(hint(t("ai.recipes.format")));
  1026. }
  1027. });
  1028. ​
  1029. A.on("ai-changes", function () { if (current && current.id === "changes") aiUi.refresh(); });
  1030. A.on("workspace", function () { if (current && (current.id === "private" || current.id === "changes")) aiUi.refresh(); });
  1031. ​
  1032. aiUi.section({
  1033. id: "keys",
  1034. order: 50,
  1035. icon: "key",
  1036. label: "ai.sec.keys",
  1037. render: async function (box) {
  1038. var status = await ai.keys.status();
  1039. if (!status.crypto) {
  1040. box.appendChild(alertBox("danger", t("ai.keys.no_crypto_title"), A.t("ai.keys.no_crypto")));
  1041. return;
  1042. }
  1043. if (status.locked) {
  1044. var pass = el("input", { type: "password", class: "orb-input", autocomplete: "current-password", placeholder: t("ai.keys.pass_enter"), "aria-label": t("ai.keys.pass_enter") });
  1045. var unlock = function () {
  1046. ai.keys.unlock(pass.value).then(function (ok) {
  1047. if (!ok) {
  1048. pass.value = "";
  1049. pass.focus();
  1050. A.ui.toast(t("ai.keys.pass_wrong"), "danger");
  1051. }
  1052. }).catch(fail);
  1053. };
  1054. pass.addEventListener("keydown", function (e) { if (e.key === "Enter") { e.preventDefault(); unlock(); } });
  1055. box.appendChild(alertBox("info", t("ai.keys.locked_title"), t("ai.keys.locked_text"), [
  1056. el("div", { class: "ath-ai-pass" }, [pass, button(t("ai.keys.unlock"), "primary", unlock, "lock")])
  1057. ]));
  1058. box.appendChild(hint(t("ai.keys.forgot_hint")));
  1059. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t("ai.keys.wipe"), "danger", async function () {
  1060. if (await A.ui.confirm(t("ai.keys.wipe"), t("ai.keys.wipe_confirm"), t("ai.keys.wipe"), true)) ai.keys.wipe().catch(fail);
  1061. }, "trash")]));
  1062. return;
  1063. }
  1064. box.appendChild(status.mode === "passphrase"
  1065. ? alertBox("success", t("ai.keys.pass_title"), t("ai.keys.pass_text"))
  1066. : alertBox("warning", t("ai.keys.device_title"), t("ai.keys.device_text")));
  1067. ai.PROVIDERS.forEach(function (p) { box.appendChild(keyRow(p, status)); });
  1068. box.appendChild(hint(t("ai.keys.only_provider")));
  1069. if (ai.relayUrl()) box.appendChild(alertBox("warning", null, t("ai.keys.via_relay")));
  1070. ​
  1071. box.appendChild(heading(t("ai.keys.pass_head")));
  1072. if (status.mode === "passphrase") {
  1073. box.appendChild(el("div", { class: "ath-ai-actions" }, [
  1074. button(t("ai.keys.lock_now"), "secondary", function () { ai.keys.lock(); }, "lock"),
  1075. button(t("ai.keys.pass_remove"), "ghost", async function () {
  1076. if (await A.ui.confirm(t("ai.keys.pass_remove"), t("ai.keys.pass_remove_confirm"), t("ai.keys.pass_remove"), true)) {
  1077. ai.keys.clearPassphrase().then(function () { A.ui.toast(t("ai.keys.pass_removed"), "success"); }).catch(fail);
  1078. }
  1079. })
  1080. ]));
  1081. box.appendChild(hint(t("ai.keys.pass_change")));
  1082. box.appendChild(passwordPair(function (v) {
  1083. return ai.keys.setPassphrase(v).then(function () { A.ui.toast(t("ai.keys.pass_set"), "success"); });
  1084. }, t("ai.keys.pass_change_btn")));
  1085. } else {
  1086. box.appendChild(hint(t("ai.keys.pass_intro", { n: ai.PASSPHRASE_MIN })));
  1087. box.appendChild(passwordPair(function (v) {
  1088. return ai.keys.setPassphrase(v).then(function () { A.ui.toast(t("ai.keys.pass_set"), "success"); });
  1089. }, t("ai.keys.pass_set_btn")));
  1090. }
  1091. }
  1092. });
  1093. ​
  1094. aiUi.section({
  1095. id: "model",
  1096. order: 60,
  1097. icon: "coins",
  1098. label: "ai.sec.model",
  1099. live: function (path) { return path === "budget.monthly"; },
  1100. render: function (box) {
  1101. box.appendChild(row(t("ai.model.mode"), bindSelect("mode", ai.MODES.map(function (m) {
  1102. return [m, m === "auto" ? t("ai.model.mode_auto") : ai.providerLabel(m)];
  1103. })), t("ai.model.mode_hint")));
  1104. box.appendChild(row(t("ai.model.efficiency"), bindSelect("efficiency", ai.EFFICIENCY.map(function (e) {
  1105. return [e, t("ai.eff." + e)];
  1106. })), t("ai.model.efficiency_hint")));
  1107. box.appendChild(row(t("ai.model.cheap"), bindSwitch("cheap"), t("ai.model.cheap_hint")));
  1108. ​
  1109. box.appendChild(heading(t("ai.budget.head")));
  1110. liveBox = el("div", { class: "ath-ai-live" });
  1111. box.appendChild(liveBox);
  1112. renderLive(liveBox).catch(fail);
  1113. box.appendChild(row(t("ai.budget.monthly"), bindNumber("budget.monthly", 0.5, "USD"), t("ai.budget.monthly_hint")));
  1114. box.appendChild(row(t("ai.budget.per_request"), bindNumber("budget.perRequest", 0.01, "USD"), t("ai.budget.per_request_hint")));
  1115. box.appendChild(row(t("ai.budget.iterations"), bindNumber("budget.iterations", 1), t("ai.budget.iterations_hint")));
  1116. box.appendChild(row(t("ai.budget.auto_reload"), bindSwitch("budget.autoReload"), t("ai.budget.auto_reload_hint")));
  1117. ​
  1118. box.appendChild(heading(t("ai.models.head")));
  1119. box.appendChild(hint(t("ai.models.intro")));
  1120. box.appendChild(modelTable());
  1121. box.appendChild(hint(t("ai.models.price_note")));
  1122. box.appendChild(routerProbe());
  1123. }
  1124. });
  1125. ​
  1126. aiUi.section({
  1127. id: "autocomplete",
  1128. order: 70,
  1129. icon: "type",
  1130. label: "ai.sec.autocomplete",
  1131. render: function (box) {
  1132. box.appendChild(permRow("ghost"));
  1133. box.appendChild(row(t("ai.ghost.provider"), bindSelect("ghost.provider", ai.PROVIDERS.map(function (p) { return [p, ai.providerLabel(p)]; }))));
  1134. box.appendChild(row(t("ai.ghost.model"), bindText("ghost.model", { class: "orb-input orb-mono" }), t("ai.ghost.model_hint")));
  1135. box.appendChild(row(t("ai.ghost.debounce"), bindRange("ghost.debounce", 25, function (v) { return t("ai.common.ms", { n: v }); }), t("ai.ghost.debounce_hint")));
  1136. box.appendChild(row(t("ai.ghost.max_tokens"), bindNumber("ghost.maxTokens", 8), t("ai.ghost.max_tokens_hint")));
  1137. box.appendChild(hint(t("ai.ghost.tab_hint")));
  1138. }
  1139. });
  1140. ​
  1141. aiUi.section({
  1142. id: "network",
  1143. order: 80,
  1144. icon: "globe",
  1145. label: "ai.sec.network",
  1146. live: function (path) { return path === "search.mode" || path === "relay.on" || /^search\.engines\.[a-z0-9]+\.on$/.test(path); },
  1147. render: async function (box) {
  1148. box.appendChild(permRow("web"));
  1149. box.appendChild(permRow("crawler"));
  1150. box.appendChild(alertBox("info", null, t("ai.net.cors")));
  1151. await ai.keys.status();
  1152. box.appendChild(heading(t("ai.search.head")));
  1153. box.appendChild(hint(t("ai.search.intro")));
  1154. box.appendChild(row(t("ai.search.mode"), bindSelect("search.mode", [
  1155. ["model", t("ai.search.mode_model")], ["api", t("ai.search.mode_api")], ["both", t("ai.search.mode_both")]
  1156. ]), t("ai.search.mode_hint")));
  1157. box.appendChild(row(t("ai.search.results"), bindNumber("search.results", 1), t("ai.search.results_hint")));
  1158. box.appendChild(row(t("ai.search.news"), bindSwitch("search.news"), t("ai.search.news_hint")));
  1159. box.appendChild(row(t("ai.search.safe"), bindSwitch("search.safe"), t("ai.search.safe_hint")));
  1160. if (!ai.relayUrl()) {
  1161. box.appendChild(alertBox("info", null, t("ai.relay.engines_note"), [
  1162. el("div", { class: "ath-ai-actions" }, [button(t("ai.relay.setup"), "secondary", function () { show("relay"); }, "globe")])
  1163. ]));
  1164. }
  1165. ai.search.ORDER.forEach(function (id) { box.appendChild(engineRow(id)); });
  1166. box.appendChild(hint(t("ai.search.keys_note")));
  1167. box.appendChild(alertBox("warning", null, t("ai.search.retired")));
  1168. box.appendChild(heading(t("ai.net.allow")));
  1169. box.appendChild(bindArea("network.allow", 4, 5000, true));
  1170. box.appendChild(hint(t("ai.net.allow_hint")));
  1171. box.appendChild(heading(t("ai.net.deny")));
  1172. box.appendChild(bindArea("network.deny", 4, 5000, true));
  1173. box.appendChild(hint(t("ai.net.deny_hint")));
  1174. }
  1175. });
  1176. ​
  1177. aiUi.section({
  1178. id: "preview",
  1179. order: 90,
  1180. icon: "eye",
  1181. label: "ai.sec.preview",
  1182. render: function (box) {
  1183. box.appendChild(permRow("preview"));
  1184. box.appendChild(permRow("screenshot"));
  1185. box.appendChild(row(t("ai.pv.approval"), bindSelect("preview.approval", [["ask", t("ai.approval.ask")], ["auto", t("ai.pv.auto")]]), t("ai.pv.approval_hint")));
  1186. box.appendChild(row(t("ai.pv.auto_fix"), bindSwitch("preview.autoFix"), t("ai.pv.auto_fix_hint")));
  1187. box.appendChild(row(t("ai.pv.max_fix"), bindNumber("preview.maxFix", 1), t("ai.pv.max_fix_hint")));
  1188. box.appendChild(heading(t("ai.pv.viewports")));
  1189. box.appendChild(row(t("ai.pv.mobile"), bindNumber("preview.mobile", 1, "px")));
  1190. box.appendChild(row(t("ai.pv.tablet"), bindNumber("preview.tablet", 1, "px")));
  1191. box.appendChild(row(t("ai.pv.desktop"), bindNumber("preview.desktop", 1, "px")));
  1192. box.appendChild(row(t("ai.pv.shot_width"), bindNumber("preview.shotWidth", 16, "px"), t("ai.pv.shot_width_hint")));
  1193. box.appendChild(alertBox("info", t("ai.pv.limits_title"), t("ai.pv.limits_text")));
  1194. box.appendChild(heading(t("ai.pv.tools_head")));
  1195. box.appendChild(hint(t("ai.pv.tools_hint")));
  1196. box.appendChild(el("div", { class: "ath-ai-actions" }, [
  1197. button(t("ai.viewer.pick"), "secondary", function () { A.commands.aiPick(); }, "target"),
  1198. button(t("ai.viewer.audit"), "secondary", function () { Promise.resolve(A.commands.aiAudit()).catch(fail); }, "layout")
  1199. ]));
  1200. }
  1201. });
  1202. ​
  1203. aiUi.section({
  1204. id: "permissions",
  1205. order: 100,
  1206. icon: "shield",
  1207. label: "ai.sec.permissions",
  1208. live: function (path) { return path === "enabled"; },
  1209. render: function (box) {
  1210. box.appendChild(ai.enabled()
  1211. ? alertBox("success", null, t("ai.perm.master_on"))
  1212. : alertBox("info", null, t("ai.perm.master_off")));
  1213. box.appendChild(row(t("ai.ov.master"), masterSwitch(), t("ai.ov.master_hint")));
  1214. box.appendChild(heading(t("ai.perm.head")));
  1215. ai.PERMS.forEach(function (p) { box.appendChild(permRow(p)); });
  1216. box.appendChild(heading(t("ai.approval.head")));
  1217. box.appendChild(row(t("ai.approval.mode"), bindSelect("approval", [["ask", t("ai.approval.ask")], ["auto", t("ai.approval.auto")]]), t("ai.approval.mode_hint")));
  1218. box.appendChild(row(t("ai.approval.diff"), bindSwitch("diff"), t("ai.approval.diff_hint")));
  1219. box.appendChild(row(t("ai.approval.snapshots"), bindSwitch("snapshots"), t("ai.approval.snapshots_hint")));
  1220. box.appendChild(hint(t("ai.approval.always")));
  1221. box.appendChild(heading(t("ai.priv.head")));
  1222. box.appendChild(row(t("ai.priv.index"), bindSwitch("privateIndex"), t("ai.priv.index_hint")));
  1223. }
  1224. });
  1225. ​
  1226. aiUi.section({
  1227. id: "exclusions",
  1228. order: 110,
  1229. icon: "ban",
  1230. label: "ai.sec.exclusions",
  1231. render: function (box) {
  1232. box.appendChild(hint(t("ai.exc.intro")));
  1233. box.appendChild(bindArea("exclusions", 12, 20000, true));
  1234. box.appendChild(el("div", { class: "ath-ai-actions" }, [button(t("ai.exc.reset"), "ghost", function () {
  1235. ai.set("exclusions", ai.DEFAULT_EXCLUSIONS);
  1236. renderCurrent();
  1237. }, "undo")]));
  1238. var probe = el("input", { type: "text", class: "orb-input orb-mono", spellcheck: "false", autocomplete: "off", placeholder: t("ai.exc.test_ph") });
  1239. var result = el("small", { class: "ath-ai-probe", "aria-live": "polite" });
  1240. probe.addEventListener("input", function () {
  1241. var p = probe.value.trim();
  1242. result.textContent = !p ? "" : ai.isExcluded(p) ? t("ai.exc.test_yes") : t("ai.exc.test_no");
  1243. result.classList.toggle("is-excluded", !!p && ai.isExcluded(p));
  1244. });
  1245. box.appendChild(row(t("ai.exc.test"), el("div", { class: "ath-ai-probe-box" }, [probe, result])));
  1246. box.appendChild(hint(ai.t("ai.exc.redact_link")));
  1247. }
  1248. });
  1249. ​
  1250. aiUi.section({
  1251. id: "relay",
  1252. order: 85,
  1253. icon: "globe",
  1254. label: "ai.sec.relay",
  1255. live: function (path) { return path === "relay.on" || path === "relay.hash"; },
  1256. render: async function (box) {
  1257. var R = ai.relay;
  1258. box.appendChild(hint(ai.t("ai.relay.intro")));
  1259. box.appendChild(el("ul", { class: "ath-ai-consent-list" }, [t("ai.relay.needs_search"), t("ai.relay.needs_pages"), ai.t("ai.relay.never_ai")].map(function (s) { return el("li", {}, s); })));
  1260. if (ai.relayUrl()) box.appendChild(alertBox("success", null, t("ai.relay.active", { url: ai.relayUrl() })));
  1261. ​
  1262. box.appendChild(heading(t("ai.relay.step_key")));
  1263. var hash = R.hash();
  1264. var hasKey = false;
  1265. try { hasKey = await R.hasKey(); } catch (e) { hasKey = false; }
  1266. var ready = hasKey && !!hash;
  1267. box.appendChild(row(t("ai.relay.key"), el("div", { class: "ath-ai-key-control" }, [
  1268. button(ready ? t("ai.relay.new_key") : t("ai.relay.create_key"), ready ? "ghost" : "primary", async function () {
  1269. if (ready && !(await A.ui.confirm(t("ai.relay.new_key"), t("ai.relay.new_key_confirm"), t("ai.relay.new_key"), true))) return;
  1270. await R.newKey();
  1271. A.ui.toast(t("ai.relay.key_created"), "success");
  1272. renderCurrent();
  1273. }, "key"),
  1274. ready ? iconButton("copy", t("ai.relay.copy_hash"), function () {
  1275. A.ui.copyText(hash).then(function () { A.ui.toast(t("ai.relay.hash_copied"), "success"); });
  1276. }) : null
  1277. ]), ready ? t("ai.relay.key_hint", { hash: hash.slice(0, 12) + "…" + hash.slice(-6) }) : t("ai.relay.key_none")));
  1278. ​
  1279. box.appendChild(heading(t("ai.relay.step_files")));
  1280. box.appendChild(row(t("ai.relay.pages"), bindSwitch("relay.pages"), t("ai.relay.pages_hint")));
  1281. box.appendChild(el("div", { class: "ath-ai-actions ath-ai-relay-files" }, [
  1282. R.canSave() ? button(t("ai.relay.save_folder"), "primary", async function () {
  1283. try {
  1284. var r = await R.saveToFolder();
  1285. A.ui.toast(t("ai.relay.saved", { folder: r.folder }), "success");
  1286. if (r.htaccess !== ".htaccess") A.ui.toast(t("ai.relay.saved_htaccess_txt"), "warning");
  1287. renderCurrent();
  1288. } catch (e) {
  1289. if (e && e.name !== "AbortError") fail(e);
  1290. }
  1291. }, "folder") : null,
  1292. button(t("ai.relay.download_php"), "secondary", function () { R.download("php").catch(fail); }, "download"),
  1293. button(t("ai.relay.download_htaccess"), "secondary", function () { R.download("htaccess").catch(fail); }, "download")
  1294. ]));
  1295. box.appendChild(hint(t("ai.relay.files_hint")));
  1296. var cmd = "php -S 127.0.0.1:8787 relay.php";
  1297. box.appendChild(el("div", { class: "ath-ai-relay-how" }, [
  1298. el("div", {}, [
  1299. el("h4", {}, t("ai.relay.how_host")),
  1300. el("ol", {}, [t("ai.relay.host_1", { folder: R.FOLDER }), t("ai.relay.host_2"), t("ai.relay.host_3", { folder: R.FOLDER })].map(function (s) { return el("li", {}, s); })),
  1301. el("p", { class: "ath-set-hint" }, t("ai.relay.host_req"))
  1302. ]),
  1303. el("div", {}, [
  1304. el("h4", {}, t("ai.relay.how_local")),
  1305. el("ol", {}, [
  1306. el("li", {}, [t("ai.relay.local_1"), el("div", { class: "ath-ai-relay-cmd" }, [el("code", {}, cmd), iconButton("copy", t("ai.relay.copy_cmd"), function () {
  1307. A.ui.copyText(cmd).then(function () { A.ui.toast(t("ai.chat.copied"), "success"); });
  1308. })])]),
  1309. el("li", {}, t("ai.relay.local_2"))
  1310. ]),
  1311. el("p", { class: "ath-set-hint" }, t("ai.relay.local_req"))
  1312. ])
  1313. ]));
  1314. ​
  1315. box.appendChild(heading(t("ai.relay.step_connect")));
  1316. var urlInput = bindText("relay.url", { type: "url", placeholder: "https://twojastrona.pl/" + R.FOLDER + "/relay.php", class: "orb-input orb-mono" });
  1317. urlInput.addEventListener("change", function () {
  1318. if (ai.get("relay.on") && !ai.validRelay(urlInput.value)) {
  1319. ai.set("relay.on", false);
  1320. A.ui.toast(t("ai.ov.relay_invalid"), "warning");
  1321. }
  1322. });
  1323. box.appendChild(row(t("ai.relay.url"), urlInput, t("ai.relay.url_hint")));
  1324. var result = el("p", { class: "ath-ai-relay-result", "aria-live": "polite" });
  1325. var testBtn = button(t("ai.relay.test"), "primary", async function () {
  1326. ai.set("relay.url", urlInput.value);
  1327. testBtn.disabled = true;
  1328. result.className = "ath-ai-relay-result";
  1329. result.textContent = t("ai.relay.testing");
  1330. try {
  1331. var r = await R.test(urlInput.value);
  1332. result.classList.add("is-ok");
  1333. result.textContent = t("ai.relay.test_ok", { php: r.php || "?", pages: r.pages ? t("ai.relay.pages_on") : t("ai.relay.pages_off") }) + (r.outdated ? " " + t("ai.relay.outdated") : "");
  1334. if (!ai.get("relay.on")) {
  1335. ai.set("relay.on", true);
  1336. A.ui.toast(t("ai.relay.enabled"), "success");
  1337. }
  1338. } catch (e) {
  1339. result.classList.add("is-error");
  1340. result.textContent = e.message;
  1341. } finally {
  1342. testBtn.disabled = false;
  1343. }
  1344. }, "check");
  1345. box.appendChild(row(t("ai.relay.check"), el("div", { class: "ath-ai-probe-box" }, [testBtn, result])));
  1346. box.appendChild(row(t("ai.relay.use"), bindSwitch("relay.on", function (v, input) {
  1347. if (v && !ai.validRelay(ai.get("relay.url"))) {
  1348. input.checked = false;
  1349. return A.ui.toast(t("ai.ov.relay_invalid"), "warning");
  1350. }
  1351. ai.set("relay.on", v);
  1352. }), t("ai.relay.use_hint")));
  1353. }
  1354. });
  1355. ​
  1356. aiUi.section({
  1357. id: "privacy",
  1358. order: 105,
  1359. icon: "shield",
  1360. label: "ai.sec.privacy",
  1361. render: function (box) {
  1362. var R = ai.redact;
  1363. box.appendChild(hint(ai.t("ai.redact.intro")));
  1364. box.appendChild(row(t("ai.redact.on"), bindSwitch("redact.on", function (on) {
  1365. ai.set("redact.on", on);
  1366. renderCurrent();
  1367. }), ai.t("ai.redact.on_hint")));
  1368. if (!ai.get("redact.on")) box.appendChild(alertBox("warning", null, ai.t("ai.redact.off_warning")));
  1369. box.appendChild(row(t("ai.redact.secrets"), bindSwitch("redact.secrets"), t("ai.redact.secrets_hint")));
  1370. box.appendChild(row(t("ai.redact.personal"), bindSwitch("redact.personal"), t("ai.redact.personal_hint")));
  1371. box.appendChild(hint(ai.t("ai.redact.images")));
  1372. ​
  1373. box.appendChild(heading(t("ai.redact.session_head")));
  1374. var items = R.session();
  1375. if (!items.length) box.appendChild(hint(t("ai.redact.session_empty")));
  1376. else {
  1377. box.appendChild(el("ul", { class: "ath-ai-redact-list" }, items.map(function (it) {
  1378. return el("li", {}, [
  1379. el("span", { class: "ath-ai-secret" }, "«" + it.id + (it.label ? ": " + it.label : "") + "»"),
  1380. el("code", {}, it.sample),
  1381. el("small", {}, it.where.join(", ")),
  1382. button(t("ai.redact.not_secret"), "ghost", function () {
  1383. R.allow(it.id);
  1384. A.ui.toast(t("ai.redact.allowed", { id: it.id }), "success");
  1385. renderCurrent();
  1386. })
  1387. ]);
  1388. })));
  1389. }
  1390. var allowed = R.allowedCount();
  1391. box.appendChild(row(t("ai.redact.exceptions"), allowed ? button(t("ai.redact.clear_exceptions"), "ghost", function () {
  1392. R.clearAllowed();
  1393. renderCurrent();
  1394. }, "undo") : el("span", { class: "orb-text-muted" }, t("ai.redact.no_exceptions")), t("ai.redact.exceptions_hint", { n: allowed })));
  1395. ​
  1396. box.appendChild(heading(t("ai.redact.test_head")));
  1397. var file = el("input", { type: "text", class: "orb-input orb-mono", spellcheck: "false", autocomplete: "off", placeholder: t("ai.redact.test_file_ph") });
  1398. var area = el("textarea", { class: "orb-textarea orb-mono ath-ai-area", rows: 6, spellcheck: "false", placeholder: t("ai.redact.test_ph") });
  1399. var out = el("div", { class: "ath-ai-redact-out", "aria-live": "polite" });
  1400. var run = util.debounce(function () {
  1401. out.textContent = "";
  1402. if (!area.value.trim()) return;
  1403. var r = R.test(area.value, file.value.trim());
  1404. out.appendChild(el("p", { class: "ath-ai-redact-sum" }, r.found.length ? t("ai.redact.test_found", { list: R.summary(r.found) }) : t("ai.redact.test_none")));
  1405. out.appendChild(el("pre", {}, r.parts.map(function (p) {
  1406. return p.kind ? el("mark", { class: "ath-ai-redact-hit", title: t("ai.redact.kind_" + p.kind) + (p.label ? ": " + p.label : "") }, p.text) : document.createTextNode(p.text);
  1407. })));
  1408. }, 200);
  1409. area.addEventListener("input", run);
  1410. file.addEventListener("input", run);
  1411. box.appendChild(row(t("ai.redact.test_file"), file));
  1412. box.appendChild(area);
  1413. box.appendChild(out);
  1414. }
  1415. });
  1416. })();
  1417. ​