Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

feat(ts): browser triage demo with custom questions

nnlgsakiba202f82d9ec8
diff --git a/laya-ts/README.md b/laya-ts/README.md
index a64f9be2..48041672 100644
--- a/laya-ts/README.md
+++ b/laya-ts/README.md
@@ -138,6 +138,24 @@ node laya-ts/examples/try-ml.mjs # needs ./model-ml from the export step
node laya-ts/examples/snake.mjs --ticks 50 # autonomous snake demo, headless smoke (live TUI without --ticks)
```
+## Web demo (browser, WebGPU → WASM)
+
+No build step — serve the repo root over HTTP and open the page
+(`localhost` counts as a secure context for WebGPU):
+
+```bash
+python -m http.server 8000 # run at the repo root
+# open http://localhost:8000/laya-ts/examples/web/
+```
+
+The page loads `laya-ts/dist` (run `npm run build` inside `laya-ts/`
+first), pulls `onnxruntime-web` from a pinned CDN import map, and
+fetches the model from the URL in the box (default `../../model-ml`,
+i.e. the exported `./model-ml`). First load transfers ~1.3GB and is
+cached in CacheStorage afterwards; the encoder tries WebGPU and falls
+back to WASM automatically. Chrome/Edge for WebGPU, any modern
+browser for WASM.
+
## Packaging
ponytail: CJS/browser-field dual build + tsconfig tests-include deferred — Task 7 verified ESM-only; CJS needs second tsc config + export-map change, untested. Add when a CJS consumer or browser-field swap is requested.
diff --git a/laya-ts/examples/web/app.js b/laya-ts/examples/web/app.js
new file mode 100644
index 00000000..5c5c7319
--- /dev/null
+++ b/laya-ts/examples/web/app.js
@@ -0,0 +1,244 @@
+// Browser triage demo: Agent.load fetches the split ONNX model over HTTP,
+// createWebProvider tries WebGPU then falls back to WASM automatically.
+import { Agent, checkQuestion, triageQuestions } from "../../dist/index.js";
+
+const $ = (id) => document.getElementById(id);
+const pill = $("pill");
+const bar = $("bar");
+const loadMsg = $("loadmsg");
+const cardLoad = $("card-load");
+const cardRun = $("card-run");
+const cardQ = $("card-q");
+const runBtn = $("run");
+const outEl = $("out");
+const runMeta = $("runmeta");
+
+// ---- Custom questions ----
+let questions = triageQuestions();
+let qseq = 0;
+const CRIT_HINT = {
+ choice: "Criteria — one per line, <code>label: description</code>",
+ score: "Levels — one per line, best first",
+ noul: "Optional — <code>true: ...</code> / <code>false: ...</code> lines",
+};
+const CRIT_PLACEHOLDER = {
+ choice: "billing: invoices, payments, refunds\nother:",
+ score: "calm and neutral\nclearly annoyed",
+ noul: "true: yes, furious",
+};
+const CRIT_SAMPLE = {
+ choice: "billing: invoices, payments, refunds\nother:",
+ score: "calm and neutral\nclearly annoyed\nvery angry",
+ noul: "",
+};
+
+function renderQuestions() {
+ const list = $("qlist");
+ list.innerHTML = "";
+ const ids = Object.keys(questions);
+ if (ids.length === 0) list.innerHTML = `<p class="hint">No questions — add one below.</p>`;
+ for (const qid of ids) {
+ const q = questions[qid];
+ const item = document.createElement("div");
+ item.className = "qitem";
+ item.innerHTML = `<code>${qid}</code><span>${q.type}</span>`;
+ const del = document.createElement("button");
+ del.textContent = "remove";
+ del.onclick = () => {
+ delete questions[qid];
+ renderQuestions();
+ syncRunEnabled();
+ };
+ item.appendChild(del);
+ list.appendChild(item);
+ }
+}
+
+function syncRunEnabled() {
+ runBtn.disabled = !agent || Object.keys(questions).length === 0;
+}
+
+$("qtype").onchange = () => {
+ const t = $("qtype").value;
+ $("qcritlabel").innerHTML = CRIT_HINT[t];
+ $("qcrit").placeholder = CRIT_PLACEHOLDER[t];
+ if (!$("qcrit").value.trim()) $("qcrit").value = CRIT_SAMPLE[t];
+};
+
+function parseCriteria(type, raw) {
+ const lines = raw.split("\n").map((l) => l.trim()).filter(Boolean);
+ if (type === "score") return lines;
+ if (type === "noul") {
+ const crit = {};
+ for (const l of lines) {
+ const i = l.indexOf(":");
+ const k = (i === -1 ? l : l.slice(0, i)).trim().toLowerCase();
+ if (k !== "true" && k !== "false") throw new Error(`noul lines must start with true: or false:, got ${JSON.stringify(l)}`);
+ crit[k] = i === -1 ? "" : l.slice(i + 1).trim();
+ }
+ return crit;
+ }
+ const crit = {};
+ for (const l of lines) {
+ const i = l.indexOf(":");
+ const k = (i === -1 ? l : l.slice(0, i)).trim();
+ if (!k) throw new Error(`empty label in ${JSON.stringify(l)}`);
+ if (k in crit) throw new Error(`duplicate label ${JSON.stringify(k)}`);
+ crit[k] = i === -1 ? "" : l.slice(i + 1).trim();
+ }
+ return crit;
+}
+
+$("qadd").onclick = () => {
+ const errEl = $("qerr");
+ errEl.textContent = "";
+ try {
+ const type = $("qtype").value;
+ const ins = $("qins").value.trim();
+ if (!ins) throw new Error("instructions are required");
+ const qid = `custom${++qseq}`;
+ const qdef = { type, instructions: ins };
+ if (type !== "noul" || $("qcrit").value.trim()) qdef.criteria = parseCriteria(type, $("qcrit").value);
+ checkQuestion(qid, qdef);
+ questions[qid] = qdef;
+ $("qins").value = "";
+ $("qcrit").value = "";
+ renderQuestions();
+ syncRunEnabled();
+ } catch (e) {
+ errEl.textContent = e.message;
+ }
+};
+
+$("qpreset").onclick = () => {
+ questions = triageQuestions();
+ renderQuestions();
+ syncRunEnabled();
+ $("qerr").textContent = "";
+};
+
+let agent = null;
+let ticker = null;
+
+// Anything with a scheme goes as-is; dotted/absolute paths and bare
+// names resolve against the page (local-first: HF repos are owner/name).
+// Without this, `../model-ml` is misread as a HuggingFace repo ID.
+function resolveModelUrl(raw) {
+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(raw)) return raw;
+ if (raw.startsWith(".") || raw.startsWith("/") || !raw.includes("/")) {
+ return new URL(raw, document.baseURI).href;
+ }
+ return raw;
+}
+
+function setPill(state, text) {
+ pill.className = `pill${state === "idle" ? "" : ` ${state}`}`;
+ pill.textContent = text;
+}
+
+function startLoadingClock(t0) {
+ stopLoadingClock();
+ ticker = setInterval(() => {
+ loadMsg.textContent = `Fetching config → tokenizer → weights → sessions… ${((performance.now() - t0) / 1000).toFixed(0)}s elapsed`;
+ }, 500);
+}
+
+function stopLoadingClock() {
+ if (ticker !== null) {
+ clearInterval(ticker);
+ ticker = null;
+ }
+}
+
+function markReady(seconds, gpu) {
+ stopLoadingClock();
+ bar.hidden = true;
+ setPill("ready", "ready");
+ cardLoad.classList.remove("active");
+ cardLoad.classList.add("done");
+ cardRun.classList.remove("locked");
+ cardRun.classList.add("active");
+ loadMsg.textContent = `Ready in ${seconds}s. WebGPU ${gpu ? "available" : "unavailable — WASM fallback"}.`;
+ $("load").disabled = true;
+ $("model").disabled = true;
+ cardQ.classList.remove("locked");
+ cardQ.classList.add("active");
+ renderQuestions();
+ syncRunEnabled();
+ outEl.innerHTML = `<p class="hint">Type a message and hit Triage.</p>`;
+ $("text").focus();
+}
+
+function markError(msg) {
+ stopLoadingClock();
+ bar.hidden = true;
+ setPill("error", "error");
+ loadMsg.textContent = msg;
+ $("load").disabled = false;
+}
+
+$("load").onclick = async () => {
+ const raw = $("model").value.trim();
+ if (!raw) {
+ markError("Enter a model URL first.");
+ return;
+ }
+ const url = resolveModelUrl(raw);
+ setPill("loading", "loading");
+ bar.hidden = false;
+ $("load").disabled = true;
+ const t0 = performance.now();
+ startLoadingClock(t0);
+ try {
+ agent = await Agent.load(url);
+ markReady(((performance.now() - t0) / 1000).toFixed(1), !!navigator.gpu);
+ } catch (e) {
+ markError(`Load failed: ${e.message}`);
+ }
+};
+
+async function triage() {
+ if (!agent || runBtn.disabled) return;
+ const text = $("text").value.trim();
+ if (!text) {
+ runMeta.textContent = "Type a message first.";
+ return;
+ }
+ runBtn.disabled = true;
+ runMeta.textContent = "Running…";
+ outEl.innerHTML = "";
+ const t0 = performance.now();
+ try {
+ const r = await agent.predict({ body: text }, questions);
+ const ms = (performance.now() - t0).toFixed(0);
+ runMeta.textContent = `${ms}ms · ${r.usage.input_tokens} input tokens`;
+ for (const [qid, a] of Object.entries(r.answers)) {
+ const row = document.createElement("div");
+ row.className = "answer";
+ const head = a.type === "choice" ? `${qid}: <b>${a.choice}</b>`
+ : a.type === "score" ? `${qid}: <b>${a.score.toFixed(2)}</b>`
+ : `${qid}: <b>${a.noul.toFixed(2)}</b>`;
+ const conf = a.confidence ?? 0;
+ row.innerHTML = `${head} <span class="probs">(conf ${conf.toFixed(2)})</span>
+ <div class="conf"><div style="width:${Math.round(conf * 100)}%"></div></div>`;
+ if (a.probabilities) {
+ row.innerHTML += `<div class="probs">${Object.entries(a.probabilities)
+ .sort((x, y) => y[1] - x[1])
+ .slice(0, 4)
+ .map(([k, v]) => `${k} ${Number(v).toFixed(2)}`)
+ .join(" · ")}</div>`;
+ }
+ outEl.appendChild(row);
+ }
+ } catch (e) {
+ outEl.innerHTML = `<p class="hint">Failed: ${e.message}</p>`;
+ runMeta.textContent = "";
+ } finally {
+ runBtn.disabled = false;
+ }
+}
+
+runBtn.onclick = triage;
+$("text").addEventListener("keydown", (e) => {
+ if ((e.ctrlKey || e.metaKey) && e.key === "Enter") triage();
+});
diff --git a/laya-ts/examples/web/index.html b/laya-ts/examples/web/index.html
new file mode 100644
index 00000000..a8750427
--- /dev/null
+++ b/laya-ts/examples/web/index.html
@@ -0,0 +1,111 @@
+<!doctype html>
+<html lang="en">
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<title>laya-ts browser demo (WebGPU → WASM)</title>
+<style>
+ :root {
+ --bg: #f6f7f9; --card: #fff; --ink: #1c2333; --muted: #68738a;
+ --accent: #2563eb; --accent-ink: #fff; --ok: #15803d; --err: #b91c1c;
+ --line: #e4e8ef; --radius: 12px;
+ }
+ * { box-sizing: border-box; }
+ body { font: 15px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
+ background: var(--bg); color: var(--ink); margin: 0; padding: 2rem 1rem 4rem; }
+ main { max-width: 700px; margin: 0 auto; }
+ h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
+ .sub { color: var(--muted); margin: 0 0 1.5rem; }
+ .sub code, .card code { font-size: .85em; background: #eef1f6; padding: .1em .35em; border-radius: 6px; }
+ .card { background: var(--card); border: 1px solid var(--line);
+ border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem;
+ box-shadow: 0 1px 2px rgba(16, 24, 40, .05); }
+ .card h2 { font-size: 1rem; margin: 0 0 .75rem; display: flex; align-items: center; gap: .5rem; }
+ .step { width: 1.4rem; height: 1.4rem; border-radius: 50%; background: var(--line);
+ color: var(--muted); font-size: .8rem; display: inline-flex; align-items: center;
+ justify-content: center; font-weight: 700; flex: none; }
+ .card.active .step { background: var(--accent); color: var(--accent-ink); }
+ .card.done .step { background: var(--ok); color: #fff; }
+ .card.locked { opacity: .55; }
+ label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .35rem; }
+ input, textarea { width: 100%; font: inherit; color: var(--ink); padding: .55rem .65rem;
+ border: 1px solid var(--line); border-radius: 8px; background: #fff; }
+ input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
+ textarea { height: 4.5rem; resize: vertical; }
+ button { font: inherit; font-weight: 600; padding: .55rem 1.1rem; border: 0; border-radius: 8px;
+ background: var(--accent); color: var(--accent-ink); cursor: pointer; margin-top: .7rem; }
+ button:disabled { background: #b9c4d6; cursor: not-allowed; }
+ button:not(:disabled):hover { filter: brightness(1.08); }
+ .pill { display: inline-block; font-size: .8rem; font-weight: 700; padding: .15rem .6rem;
+ border-radius: 999px; background: var(--line); color: var(--muted); margin-left: auto; }
+ .pill.loading { background: #fef3c7; color: #92400e; }
+ .pill.ready { background: #dcfce7; color: var(--ok); }
+ .pill.error { background: #fee2e2; color: var(--err); }
+ .progress { height: .55rem; background: var(--line); border-radius: 999px; overflow: hidden; margin-top: .8rem; }
+ .progress > div { height: 100%; width: 30%; background: var(--accent); border-radius: 999px;
+ animation: slide 1.1s ease-in-out infinite; }
+ @keyframes slide { 0% { margin-left: -30%; } 100% { margin-left: 100%; } }
+ .meta { font-size: .85rem; color: var(--muted); margin-top: .5rem; }
+ .answer { border-top: 1px solid var(--line); padding: .6rem 0; }
+ .answer:first-child { border-top: 0; }
+ .conf { height: .55rem; background: var(--line); border-radius: 999px; margin: .25rem 0 .35rem; }
+ .conf > div { height: 100%; background: var(--accent); border-radius: 999px; }
+ .probs { font-size: .85rem; color: var(--muted); }
+ .hint { font-size: .85rem; color: var(--muted); }
+ .qitem { display: flex; align-items: center; gap: .5rem; border: 1px solid var(--line);
+ border-radius: 8px; padding: .4rem .6rem; margin-bottom: .4rem; font-size: .9rem; }
+ .qitem code { font-size: .8em; background: #eef1f6; padding: .1em .35em; border-radius: 6px; }
+ .qitem button { margin: 0 0 0 auto; padding: .2rem .6rem; background: #fee2e2; color: var(--err); }
+ kbd { background: #eef1f6; border: 1px solid var(--line); border-bottom-width: 2px;
+ border-radius: 6px; padding: 0 .35em; font-size: .85em; }
+</style>
+<h1>laya-ts in the browser</h1>
+<p class="sub">Multilingual checkpoint, fully client-side via
+<code>onnxruntime-web</code> (WebGPU, WASM fallback). No server inference.</p>
+<main>
+ <section class="card active" id="card-load">
+ <h2><span class="step">1</span> Load model <span class="pill" id="pill">idle</span></h2>
+ <label for="model">Model URL — serves <code>encoder.onnx</code>,
+ <code>head.onnx</code>, <code>tokenizer.json</code>, <code>rl_agent_config.json</code></label>
+ <input id="model" value="../../../model-ml">
+ <button id="load">Load model</button>
+ <div class="progress" id="bar" hidden><div></div></div>
+ <div class="meta" id="loadmsg">First load fetches ~1.3GB, then CacheStorage serves it.</div>
+ </section>
+
+ <section class="card locked" id="card-q">
+ <h2><span class="step">2</span> Questions</h2>
+ <div id="qlist"><p class="hint">Triage preset loaded. Add your own or remove these.</p></div>
+ <label for="qtype">Type</label>
+ <select id="qtype" style="font:inherit;padding:.5rem;border:1px solid var(--line);border-radius:8px;width:100%">
+ <option value="choice">choice — pick one label</option>
+ <option value="score">score — rate 0..n</option>
+ <option value="noul">noul — probability the statement holds</option>
+ </select>
+ <p></p>
+ <label for="qins">Instructions (what should the model answer?)</label>
+ <input id="qins" value="Which team should handle this request?">
+ <label for="qcrit" id="qcritlabel">Criteria — one per line, <code>label: description</code></label>
+ <textarea id="qcrit">billing: invoices, payments, refunds
+other:</textarea>
+ <button id="qadd">Add question</button>
+ <button id="qpreset" style="background:#eef1f6;color:var(--ink)">Reload triage preset</button>
+ <div class="meta" id="qerr"></div>
+ </section>
+
+ <section class="card locked" id="card-run">
+ <h2><span class="step">3</span> Triage a message</h2>
+ <label for="text">Customer message</label>
+ <textarea id="text">charged twice on my card, please refund the duplicate payment</textarea>
+ <button id="run" disabled>Triage</button>
+ <span class="hint"><kbd>Ctrl</kbd>+<kbd>Enter</kbd> to run</span>
+ <div id="out"><p class="hint">Load the model to unlock.</p></div>
+ <div class="meta" id="runmeta"></div>
+ </section>
+</main>
+<script type="importmap">
+{ "imports": {
+ "onnxruntime-web": "https://cdn.jsdelivr.net/npm/[email protected]/dist/ort.bundle.min.mjs"
+} }
+</script>
+<script type="module" src="./app.js"></script>
+</html>