v1.5.0
AtheraStudio Code
- (function () {
- "use strict";
-
- var A = window.Athera;
- var util = A.util;
- var ai = A.ai;
- var t = ai.t;
-
- var HISTORY_KEY = "ai.eval.history";
- var HISTORY_MAX = 20;
-
- var PAGE = "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<title>{title}</title>\n<link rel=\"stylesheet\" href=\"style.css\">\n</head>\n<body>\n{body}\n</body>\n</html>\n";
-
- function page(title, body) {
- return PAGE.replace("{title}", title).replace("{body}", body);
- }
-
- function has(re) {
- return function (c) { return re.test(c.all); };
- }
-
- function noLayout(types) {
- return function (c) {
- if (!c.layout) return null;
- return !c.layout.issues.some(function (i) { return types.indexOf(i.type) >= 0; });
- };
- }
-
- var TASKS = [
- {
- id: "landing",
- page: "index.html",
- seed: {},
- prompt: "Create a one-page landing page for a small local bakery called \"Pod Kłosem\" in {dir}: index.html and style.css only. Sections: header with logo text and navigation, hero with a call to action, three offer cards, opening hours, contact with address and phone, footer. Mobile-first CSS with custom properties, no frameworks, no external resources. Use realistic but short Polish texts.",
- checks: [
- ["files", function (c) { return !!c.files["index.html"] && !!c.files["style.css"]; }],
- ["viewport", has(/<meta[^>]+name=["']viewport/i)],
- ["one_h1", function (c) { return (c.files["index.html"] || "").split(/<h1[\s>]/i).length === 2; }],
- ["landmarks", has(/<header[\s>][\s\S]*<main[\s>][\s\S]*<footer[\s>]/i)],
- ["custom_props", has(/--[\w-]+\s*:/)],
- ["no_errors", function (c) { return !c.errors.length; }],
- ["mobile_layout", noLayout(["horizontal_overflow", "missing_viewport_meta"])]
- ]
- },
- {
- id: "nav",
- page: "index.html",
- seed: { "index.html": page("Nav", "<header>\n <a href=\"#\">Logo</a>\n <ul>\n <li><a href=\"#\">Home</a></li>\n <li><a href=\"#\">Offer</a></li>\n <li><a href=\"#\">About</a></li>\n <li><a href=\"#\">Contact</a></li>\n </ul>\n</header>\n<main><h1>Page</h1></main>"), "style.css": "header ul { display: flex; gap: 40px; list-style: none; }\n" },
- prompt: "Turn the header in {dir}/index.html into an accessible responsive navigation: on phones a menu button (with aria-expanded and aria-controls) shows and hides the links, on wider screens the links are inline. Use a <nav> element, vanilla JS in a small script and mobile-first CSS in {dir}/style.css. Keep it keyboard accessible.",
- checks: [
- ["nav_element", has(/<nav[\s>]/i)],
- ["menu_button", has(/<button[^>]*aria-expanded/i)],
- ["aria_controls", has(/aria-controls\s*=/i)],
- ["media_query", has(/@media/i)],
- ["no_errors", function (c) { return !c.errors.length; }],
- ["mobile_layout", noLayout(["horizontal_overflow", "small_tap_target", "empty_button"])]
- ]
- },
- {
- id: "php_form",
- page: null,
- seed: {},
- prompt: "Create {dir}/contact.php: a contact form (name, e-mail, message) that posts to itself, validates input on the server (required fields, valid e-mail, length limits), shows errors next to the fields, keeps entered values, protects against CSRF with a session token, escapes all output and on success shows a thank-you message (do not send real e-mail; put the sending in a clearly marked function). Plain PHP 8, no frameworks.",
- checks: [
- ["files", function (c) { return !!c.files["contact.php"]; }],
- ["csrf", has(/csrf|_token/i)],
- ["session", has(/session_start\s*\(/)],
- ["escaping", has(/htmlspecialchars\s*\(/)],
- ["email_check", has(/FILTER_VALIDATE_EMAIL/)],
- ["labels", has(/<label[^>]+for=/i)],
- ["security", function (c) { return !c.security.length; }],
- ["no_errors", function (c) { return !c.errors.length; }]
- ]
- },
- {
- id: "css_bug",
- page: "index.html",
- seed: { "index.html": page("Bug", "<main class=\"wrap\">\n <h1>Offer</h1>\n <div class=\"row\"><div class=\"box\">One</div><div class=\"box\">Two</div><div class=\"box\">Three</div></div>\n <img src=\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='900' height='300'/%3E\" alt=\"Banner\" width=\"900\" height=\"300\">\n</main>"), "style.css": ".wrap { width: 1100px; margin: 0 auto; }\n.row { display: flex; }\n.box { flex: 0 0 360px; padding: 20px; background: #eee; }\n" },
- prompt: "On phones the page in {dir} scrolls horizontally. Find the cause and fix it in {dir}/style.css with minimal changes, keeping the desktop layout.",
- checks: [
- ["no_fixed_width", function (c) { return !/\.wrap\s*\{[^}]*(?:^|[\s;{])width\s*:\s*1100px/.test(c.files["style.css"] || ""); }],
- ["no_errors", function (c) { return !c.errors.length; }],
- ["mobile_layout", noLayout(["horizontal_overflow", "overflow_source"])]
- ]
- },
- {
- id: "dark_mode",
- page: "index.html",
- seed: { "index.html": page("Dark", "<header><h1>Studio</h1></header>\n<main><p>Welcome to our studio.</p><a class=\"btn\" href=\"#\">Contact</a></main>"), "style.css": "body { background: #ffffff; color: #1a1a1a; font-family: system-ui, sans-serif; }\n.btn { background: #1d4ed8; color: #ffffff; padding: 10px 16px; border-radius: 6px; }\n" },
- prompt: "Add a dark mode to {dir}: move the colours in {dir}/style.css to CSS custom properties and switch them with prefers-color-scheme: dark. Keep good contrast in both themes.",
- checks: [
- ["custom_props", has(/--[\w-]+\s*:/)],
- ["color_scheme", has(/prefers-color-scheme\s*:\s*dark/i)],
- ["uses_vars", has(/var\(--/)],
- ["no_errors", function (c) { return !c.errors.length; }],
- ["contrast", noLayout(["low_contrast"])]
- ]
- },
- {
- id: "seo",
- page: "index.html",
- seed: { "index.html": "<html>\n<head>\n<title>Home</title>\n</head>\n<body>\n<h1>Plumber</h1>\n<h1>Services</h1>\n<p>We fix pipes in Kraków. <a href=\"services.html\">Click here</a></p>\n<img src=\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10'/%3E\">\n</body>\n</html>\n" },
- prompt: "Do an SEO pass on {dir}/index.html (a local plumber in Kraków): fix the document structure and add what a small business page needs. Do not invent facts like phone numbers; use clearly marked placeholders where needed.",
- checks: [
- ["doctype", has(/<!doctype html>/i)],
- ["lang", has(/<html[^>]+lang=/i)],
- ["description", has(/<meta[^>]+name=["']description/i)],
- ["viewport", has(/<meta[^>]+name=["']viewport/i)],
- ["og", has(/property=["']og:title/i)],
- ["one_h1", function (c) { return (c.files["index.html"] || "").split(/<h1[\s>]/i).length === 2; }],
- ["alt", has(/<img[^>]+alt=/i)],
- ["link_text", function (c) { return !/>\s*click here\s*</i.test(c.files["index.html"] || ""); }]
- ]
- },
- {
- id: "a11y",
- page: "index.html",
- seed: { "index.html": page("A11y", "<div class=\"top\"><div onclick=\"location='#'\">Menu</div></div>\n<h1>Newsletter</h1>\n<img src=\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10'/%3E\">\n<form><input type=\"email\" placeholder=\"E-mail\"><div class=\"send\" onclick=\"\">Send</div></form>\n<p class=\"note\">We never share your e-mail.</p>"), "style.css": ".note { color: #bbbbbb; }\n.send { display: inline-block; padding: 4px; background: #ddd; }\n" },
- prompt: "Do an accessibility pass (WCAG 2.2 AA) on {dir}: semantic elements instead of clickable divs, labels, alt text, contrast, focus styles and keyboard access. Change {dir}/index.html and {dir}/style.css.",
- checks: [
- ["label", has(/<label[\s>]/i)],
- ["button", has(/<button[\s>]/i)],
- ["alt", has(/<img[^>]+alt=/i)],
- ["focus", has(/:focus(-visible)?/i)],
- ["no_click_div", function (c) { return !/<div[^>]+onclick/i.test(c.files["index.html"] || ""); }],
- ["no_errors", function (c) { return !c.errors.length; }],
- ["page_checks", noLayout(["img_no_alt", "unlabelled_field", "low_contrast", "empty_button"])]
- ]
- },
- {
- id: "viewer_fix",
- page: "index.html",
- viewer: true,
- seed: { "index.html": page("Cards", "<main>\n <h1>Team</h1>\n <section class=\"grid\"><article>Ann</article><article>Bob</article><article>Eve</article></section>\n <p><a class=\"tiny\" href=\"#\">i</a></p>\n</main>"), "style.css": ".grid { display: grid; grid-template-columns: repeat(3, 300px); gap: 24px; }\n.tiny { display: inline-block; width: 12px; height: 12px; font-size: 8px; }\n" },
- prompt: "Open {dir}/index.html in the preview, set a phone viewport and use the preview tools (layout check, inspect) to find the layout problems, then fix them in {dir}/style.css and verify with the layout check again.",
- checks: [
- ["no_errors", function (c) { return !c.errors.length; }],
- ["mobile_layout", noLayout(["horizontal_overflow", "overflow_source", "small_tap_target"])]
- ]
- }
- ];
-
- var running = null;
-
- function configSummary() {
- var roles = {};
- ai.ROLES.forEach(function (r) {
- var c = ai.candidates(r)[0];
- roles[r] = c ? c.model : "-";
- });
- return { mode: ai.get("mode"), efficiency: ai.get("efficiency"), cheap: ai.get("cheap"), roles: roles };
- }
-
- function stamp() {
- var d = new Date();
- var p = function (n) { return n < 10 ? "0" + n : String(n); };
- return d.getFullYear() + p(d.getMonth() + 1) + p(d.getDate()) + "-" + p(d.getHours()) + p(d.getMinutes()) + p(d.getSeconds());
- }
-
- async function readDir(dir) {
- var files = {};
- try {
- for (var e of await ai.tools.io.list(dir)) {
- if (e.kind === "file") files[e.name] = await ai.tools.io.read(dir + "/" + e.name);
- }
- } catch (err) { }
- return files;
- }
-
- async function layoutOf(path) {
- if (!path || !A.preview || !ai.viewer) return null;
- var hadTab = !!A.tabs.byPath(path);
- try {
- var before = A.preview.buildId();
- await A.preview.openRoute(path);
- var end = Date.now() + 12000;
- while (Date.now() < end && !(A.preview.status().ready && A.preview.buildId() > before)) await new Promise(function (r) { setTimeout(r, 100); });
- if (!A.preview.status().ready) return null;
- A.preview.setViewport(ai.get("preview.mobile"), 740, t("ai.eval.title"));
- await new Promise(function (r) { setTimeout(r, 450); });
- var result = await ai.viewer.request("layout_check", {}, 10000);
- return result;
- } catch (e) {
- return null;
- } finally {
- A.preview.clearViewport();
- var opened = A.tabs.byPath(path);
- if (!hadTab && opened) await A.tabs.close(opened, true);
- }
- }
-
- async function runTask(task, root, onEvent, signal) {
- var dir = root + "/" + task.id;
- for (var name of Object.keys(task.seed)) await ai.tools.io.write(dir + "/" + name, task.seed[name]);
- var started = Date.now();
- var result = { id: task.id, cost: 0, known: true, calls: 0, model: "", ms: 0, checks: [], score: 0, error: null };
- var models = {};
- try {
- var r = await ai.agent.run({
- text: task.prompt.replace(/\{dir\}/g, dir),
- history: [],
- editorContext: false,
- preview: false,
- check: false,
- kind: "eval",
- signal: signal,
- autoApprovePrefix: dir + "/",
- onEvent: function (e) {
- if (e.type === "usage" && e.result) models[e.result.model] = true;
- onEvent(e);
- }
- });
- result.cost = r.cost;
- result.known = r.costKnown;
- result.calls = r.calls;
- } catch (e) {
- if (e.name === "AbortError") throw e;
- result.error = e.message;
- result.cost = e.partialCost || 0;
- }
- result.ms = Date.now() - started;
- result.model = Object.keys(models).join(", ");
- var files = await readDir(dir);
- var all = Object.keys(files).map(function (k) { return files[k]; }).join("\n");
- var quality = await ai.quality.check(Object.keys(files).map(function (k) { return dir + "/" + k; }));
- var ctx = {
- files: files,
- all: all,
- errors: quality.problems.filter(function (p) { return p.severity === "error" && !p.security; }),
- security: quality.problems.filter(function (p) { return p.security; }),
- layout: task.page && ai.allowed("preview") ? await layoutOf(dir + "/" + task.page) : null
- };
- var passed = 0, counted = 0;
- task.checks.forEach(function (c) {
- var v = null;
- try { v = c[1](ctx); } catch (e) { v = false; }
- result.checks.push({ id: c[0], ok: v });
- if (v === null) return;
- counted++;
- if (v) passed++;
- });
- result.score = counted ? Math.round((passed / counted) * 100) : 0;
- result.layoutIssues = ctx.layout ? ctx.layout.issues.length : null;
- return result;
- }
-
- async function run(ids, onProgress) {
- if (running) throw new Error(t("ai.eval.busy"));
- ai.assertNetwork();
- if (!ai.allowed("read") || !ai.allowed("write")) throw new Error(t("ai.eval.need_perms"));
- if (!A.fs.provider) throw new Error(t("ai.priv.no_folder"));
- if (!(await ai.private.ensure())) throw new Error(t("ai.tool.err_private_declined"));
- var ctrl = new AbortController();
- running = { ctrl: ctrl };
- var root = ".private/sandbox/eval-" + stamp();
- var report = { at: Date.now(), root: root, config: configSummary(), tasks: [], cost: 0, known: true, score: 0, aborted: false };
- try {
- for (var task of TASKS.filter(function (x) { return ids.indexOf(x.id) >= 0; })) {
- onProgress({ type: "task", id: task.id, report: report });
- var res = await runTask(task, root, function (e) { onProgress({ type: "event", id: task.id, event: e }); }, ctrl.signal);
- report.tasks.push(res);
- report.cost += res.cost;
- if (!res.known) report.known = false;
- onProgress({ type: "done", id: task.id, result: res, report: report });
- }
- } catch (e) {
- if (e.name !== "AbortError") throw e;
- report.aborted = true;
- } finally {
- running = null;
- if (A.explorer) {
- try { await A.explorer.refresh(null); } catch (e) { A.emit("files-changed"); }
- } else {
- A.emit("files-changed");
- }
- }
- report.score = report.tasks.length ? Math.round(report.tasks.reduce(function (n, x) { return n + x.score; }, 0) / report.tasks.length) : 0;
- var history = (await A.idb.get(HISTORY_KEY)) || [];
- history.push(report);
- while (history.length > HISTORY_MAX) history.shift();
- await A.idb.set(HISTORY_KEY, history);
- return report;
- }
-
- ai.eval = {
- TASKS: TASKS,
- run: run,
- stop: function () { if (running) running.ctrl.abort(); },
- busy: function () { return !!running; },
- history: async function () { return ((await A.idb.get(HISTORY_KEY)) || []).slice().reverse(); },
- clearHistory: function () { return A.idb.set(HISTORY_KEY, []); }
- };
-
- var ui = A.aiUi;
- var el = ui.el;
- var state = { selected: TASKS.map(function (x) { return x.id; }), log: "", report: null };
-
- function money(cost, known) {
- return ai.budget.format(cost) + (known === false ? " + ?" : "");
- }
-
- function configText(c) {
- return t("ai.eval.config_line", {
- mode: c.mode === "auto" ? t("ai.model.mode_auto") : ai.providerLabel(c.mode),
- efficiency: t("ai.eff." + c.efficiency),
- cheap: c.cheap ? " · " + t("ai.model.cheap") : "",
- models: ai.ROLES.map(function (r) { return t("ai.role." + r) + ": " + c.roles[r]; }).join(", ")
- });
- }
-
- function resultTable(report) {
- var rows = report.tasks.map(function (r) {
- var failed = r.checks.filter(function (c) { return c.ok === false; }).map(function (c) { return t("ai.eval.check_" + c.id); });
- var skipped = r.checks.some(function (c) { return c.ok === null; });
- return el("tr", {}, [
- el("td", {}, t("ai.eval.task_" + r.id)),
- el("td", { class: "ath-ai-eval-score is-" + (r.score >= 80 ? "good" : r.score >= 50 ? "mid" : "bad") }, r.score + "%"),
- el("td", {}, money(r.cost, r.known)),
- el("td", {}, Math.round(r.ms / 1000) + " s"),
- el("td", { class: "ath-ai-eval-model" }, r.model || "-"),
- el("td", { class: "ath-ai-eval-notes" }, (r.error ? r.error + " " : "") + (failed.length ? t("ai.eval.failed", { list: failed.join(", ") }) : t("ai.eval.all_passed")) + (skipped ? " " + t("ai.eval.skipped") : ""))
- ]);
- });
- rows.push(el("tr", { class: "is-total" }, [
- el("td", {}, t("ai.eval.total")),
- el("td", {}, (report.tasks.length ? Math.round(report.tasks.reduce(function (n, x) { return n + x.score; }, 0) / report.tasks.length) : 0) + "%"),
- el("td", {}, money(report.cost, report.known)),
- el("td", {}, ""), el("td", {}, ""), el("td", {}, report.aborted ? t("ai.eval.aborted") : "")
- ]));
- return el("div", { class: "ath-ai-table-wrap" }, el("table", { class: "orb-table ath-ai-eval-table" }, [
- el("thead", {}, el("tr", {}, ["task", "score", "cost", "time", "model", "notes"].map(function (k) { return el("th", {}, t("ai.eval.col_" + k)); }))),
- el("tbody", {}, rows)
- ]));
- }
-
- ui.section({
- id: "eval",
- order: 115,
- icon: "lightbulb",
- label: "ai.sec.eval",
- hidden: function () { return !ai.get("evalPanel"); },
- render: async function (box) {
- box.appendChild(ui.hint(t("ai.eval.intro")));
- box.appendChild(ui.alert("warning", null, t("ai.eval.warning")));
- box.appendChild(ui.heading(t("ai.eval.config")));
- box.appendChild(ui.hint(configText(configSummary())));
- if (!ai.allowed("preview")) box.appendChild(ui.hint(t("ai.eval.no_preview")));
- box.appendChild(ui.heading(t("ai.eval.tasks")));
- var list = el("div", { class: "ath-ai-eval-tasks" });
- TASKS.forEach(function (task) {
- var cb = el("input", { type: "checkbox" });
- cb.checked = state.selected.indexOf(task.id) >= 0;
- cb.addEventListener("change", function () {
- state.selected = cb.checked ? state.selected.concat([task.id]) : state.selected.filter(function (x) { return x !== task.id; });
- });
- list.appendChild(el("label", { class: "orb-checkbox" }, [cb, el("span", {}, t("ai.eval.task_" + task.id))]));
- });
- box.appendChild(list);
- var progress = el("p", { class: "ath-ai-eval-progress", "aria-live": "polite" }, state.log);
- var resultBox = el("div", {}, state.report ? resultTable(state.report) : null);
- var runBtn = ui.button(t(ai.eval.busy() ? "ai.eval.stop" : "ai.eval.run"), ai.eval.busy() ? "danger" : "primary", async function () {
- if (ai.eval.busy()) return ai.eval.stop();
- if (!state.selected.length) return A.ui.toast(t("ai.eval.none_selected"), "info");
- if (!ai.enabled() && !(await ai.enable())) return;
- var month = await ai.budget.month();
- var ok = await A.ui.confirm(t("ai.eval.title"), t("ai.eval.confirm", { n: state.selected.length, spent: ai.budget.format(month.cost), limit: ai.get("budget.monthly") > 0 ? ai.budget.format(ai.get("budget.monthly")) : "-" }), t("ai.eval.run"), false);
- if (!ok) return;
- state.report = null;
- resultBox.innerHTML = "";
- runBtn.querySelector("span").textContent = t("ai.eval.stop");
- runBtn.classList.replace("orb-btn-primary", "orb-btn-danger");
- try {
- var report = await ai.eval.run(state.selected.slice(), function (p) {
- if (p.type === "task") state.log = t("ai.eval.running", { task: t("ai.eval.task_" + p.id), n: p.report.tasks.length + 1, total: state.selected.length });
- if (p.type === "event" && p.event.type === "tool_run") state.log = t("ai.eval.running_tool", { task: t("ai.eval.task_" + p.id), tool: p.event.name });
- if (p.type === "done") {
- resultBox.innerHTML = "";
- resultBox.appendChild(resultTable(p.report));
- }
- progress.textContent = state.log;
- });
- state.report = report;
- state.log = t("ai.eval.finished", { score: report.score, cost: money(report.cost, report.known), dir: report.root });
- } catch (e) {
- state.log = e.message;
- }
- ui.refresh();
- }, ai.eval.busy() ? "x" : "power");
- box.appendChild(el("div", { class: "ath-ai-actions" }, [runBtn, ui.button(t("ai.eval.hide"), "ghost", function () {
- ai.set("evalPanel", false);
- A.aiUi.open("model");
- })]));
- box.appendChild(progress);
- box.appendChild(resultBox);
- var history = await ai.eval.history();
- box.appendChild(ui.heading(t("ai.eval.history")));
- if (!history.length) box.appendChild(el("p", { class: "ath-set-empty" }, t("ai.eval.history_empty")));
- else {
- box.appendChild(el("div", { class: "ath-ai-eval-history" }, history.map(function (h) {
- return el("div", { class: "ath-ai-eval-run" }, [
- el("strong", {}, h.score + "% · " + money(h.cost, h.known) + " · " + h.tasks.length + " " + t("ai.eval.tasks_short")),
- el("small", {}, new Date(h.at).toLocaleString(document.documentElement.lang || undefined) + " · " + configText(h.config))
- ]);
- })));
- box.appendChild(el("div", { class: "ath-ai-actions" }, [ui.button(t("ai.eval.clear"), "ghost", function () {
- ai.eval.clearHistory().then(function () { ui.refresh(); });
- }, "trash")]));
- }
- }
- });
- })();
-