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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
feat(examples): redesign the server.py web GUI as a two-pane playground
luiz-linkezio557ac320af20
diff --git a/README.md b/README.mdindex 0560d3e6..84521b87 100644--- a/README.md+++ b/README.md@@ -197,15 +197,16 @@ Routing alone never downloads a checkpoint, so it returns in milliseconds. `--pr## Try it locally: web GUI + JSON API`examples/server.py` is a self-contained FastAPI app for testing Laya without writing any code:-a request builder (or a raw-JSON paste box) that renders `choice`/`score`/`noul` answers as-0-100 bars, plus a plain JSON API (`/predict`, `/predict/batch`) for scripting against.+a two-pane playground (edit the request as a form or as JSON, run it with Ctrl+Enter, read each+answer's full distribution and calibrated confidence, copy it as curl or Python), plus a plain+JSON API (`/predict`, `/predict/batch`) for scripting against.```bashpip install "laya[serve]"python examples/server.py # http://127.0.0.1:8000```-Open `http://127.0.0.1:8000` in a browser for the builder UI, or hit it directly:+Open `http://127.0.0.1:8000` in a browser for the playground, or hit it directly:```bashcurl -s localhost:8000/predict -H 'content-type: application/json' -d '{diff --git a/examples/server.py b/examples/server.pyindex c401758d..ed842808 100644--- a/examples/server.py+++ b/examples/server.py@@ -28,8 +28,10 @@ Example:from __future__ import annotationsimport argparse+import base64import jsonimport os+import reimport timefrom contextlib import asynccontextmanagerfrom html import escape@@ -228,7 +230,7 @@ def qtypes() -> Dict[str, Any]:@app.get("/presets")def presets() -> Dict[str, Any]:- """The laya.presets workflows this demo's builder can load, state+questions included."""+ """The laya.presets workflows the playground can load, state+questions included."""return PRESETS@@ -294,367 +296,651 @@ def predict_batch(req: BatchRequest) -> Dict[str, Any]:# --------------------------------------------------------------------------- #-# GUI: same payload, rendered as 0-100 bars instead of JSON+# GUI: a request playground on `/`; POST /gui renders the same answers server-side# --------------------------------------------------------------------------- #-_CSS = """-:root, :root[data-theme="ocean"] { color-scheme: light;- --ground:#1f5f9e; --shell:#f7f5ef; --card:#fff; --ink:#122236; --mut:#6b7a8c;- --line:#e6e2d8; --bar:#e9e6de; --accent:#1f5f9e; --accent-soft:#a9bdd2;- --dark:#0d2740; --dark-ink:#eaf2fa; --dark-mut:#8fa9c2; --glow:#8fd6ff; --danger:#b3452f; }-/* third theme: midnight, for anyone who wants the room dark */-:root[data-theme="midnight"] { color-scheme: dark;- --ground:#071522; --shell:#111a24; --card:#18232f; --ink:#e9f0f7; --mut:#93a5b8;- --line:#26333f; --bar:#22303c; --accent:#4d9ae0; --accent-soft:#41556a;- --dark:#0a1826; --dark-ink:#eaf2fa; --dark-mut:#8fa9c2; --glow:#8fd6ff; --danger:#e08268; }-/* second theme: light paper, black pills, coral accent */-:root[data-theme="paper"] { color-scheme: light;- --ground:#e9e9e7; --shell:#f4f4f2; --card:#fff; --ink:#131313; --mut:#8b8b88;- --line:#e6e6e3; --bar:#eeeeeb; --accent:#e2573c; --accent-soft:#d5d5d1;- --dark:#141414; --dark-ink:#fafafa; --dark-mut:#a3a3a0; --glow:#22b573; --danger:#c0392b; }-:root[data-theme="paper"] .shell { box-shadow:none; border:1px solid var(--line) }-:root[data-theme="paper"] .dot { background:var(--glow) }-:root[data-theme="paper"] .dot::after { background:#fff }-:root[data-theme="paper"] h1 { letter-spacing:-.03em }-:root[data-theme="paper"] .row.win .fill { background:var(--accent) }--* { box-sizing:border-box }+_LIGHT = (+ "--bg:#ffffff; --chrome:#f5f6f8; --well:#f2f4f6; --card:#ffffff; --sunk:#e9edf1; --hover:#e8ecf0;"+ "--ink:#1a1e24; --ink-2:#3a424d; --mute:#5c6571; --faint:#646d79; --line:#e1e5ea; --line-2:#cdd3da;"+ "--brand:#2a78d6; --btn:#2470cc; --btn-hover:#1d62b8; --blue-ink:#1b62b5; --blue-soft:#e6effa;"+ "--focus:#2a78d6; --sel:#cbdcf3; --sel-a:rgba(42,120,214,.24); --dot:#c2c9d1; --bar:#8d97a3; --curline:#f1f5fa;"+ "--ok:#1c7547; --ok-soft:#e1f1e8; --warn:#8f5200; --warn-soft:#fbefd9; --bad:#b93228; --bad-soft:#fbe6e3;"+ "--sx-key:#1b5fb0; --sx-str:#8a4510; --sx-num:#a02d78; --sx-lit:#6a44b5; --sx-pun:#5f6874;"+ "--shadow:rgba(20,32,48,.16); --toast:#1f242c; --toast-ink:#f3f5f8;"+)+_DARK = (+ "--bg:#15181d; --chrome:#1b1f25; --well:#111418; --card:#1b1f25; --sunk:#262b33; --hover:#262b33;"+ "--ink:#e4e8ee; --ink-2:#c5ccd6; --mute:#9ba4b0; --faint:#8a939e; --line:#2a3038; --line-2:#3b434e;"+ "--brand:#3d88e0; --btn:#2470cc; --btn-hover:#2f7ed8; --blue-ink:#72aef2; --blue-soft:#1c2a3b;"+ "--focus:#5b9ded; --sel:#2b4466; --sel-a:rgba(91,157,237,.34); --dot:#3c434d; --bar:#707a87; --curline:#1a1f26;"+ "--ok:#5cc690; --ok-soft:#173226; --warn:#e5a94f; --warn-soft:#3a2c15; --bad:#f27d71; --bad-soft:#3d1f1c;"+ "--sx-key:#7fb6f5; --sx-str:#e3b47c; --sx-num:#ea93c8; --sx-lit:#b9a0f5; --sx-pun:#8a939e;"+ "--shadow:rgba(0,0,0,.5); --toast:#e4e8ee; --toast-ink:#15181d;"+)++_CSS = (+ f":root {{ color-scheme:light; {_LIGHT} }}\n"+ f"@media (prefers-color-scheme: dark) {{ :root:not([data-theme='light']) {{ color-scheme:dark; {_DARK} }} }}\n"+ f":root[data-theme='dark'] {{ color-scheme:dark; {_DARK} }}\n"+) + """+:root { --sans: Inter, "Adwaita Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,+ "Helvetica Neue", Arial, sans-serif;+ --mono: ui-monospace, "JetBrains Mono", "Cascadia Mono", SFMono-Regular, Menlo, Consolas,+ "Adwaita Mono", "DejaVu Sans Mono", "Liberation Mono", monospace; }+*, *::before, *::after { box-sizing:border-box }[hidden] { display:none !important }-body { margin:0; padding:28px 16px 48px; background:var(--ground); color:var(--ink);- font:15px/1.55 ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif }-.shell { max-width:900px; margin:0 auto; background:var(--shell); border-radius:0;- padding:22px 26px 30px; box-shadow:0 26px 60px rgba(4,20,38,.28) }-.bar { display:flex; align-items:center; gap:14px; padding-bottom:18px;- border-bottom:1px solid var(--line); margin-bottom:26px }-.brand { display:flex; align-items:center; gap:9px; font-weight:650; letter-spacing:-.01em }-.dot { width:26px; height:26px; border-radius:0; background:var(--dark);- display:inline-block; position:relative }-.dot::after { content:""; position:absolute; inset:9px; border-radius:0; background:var(--glow) }-.bar nav { margin-left:auto; display:flex; gap:18px; align-items:center; font-size:13px }-.theme { padding:5px 14px; font-size:12px }-.bar nav a { color:var(--mut); text-decoration:none }-.bar nav a:hover { color:var(--accent) }-h1 { font-size:34px; line-height:1.12; letter-spacing:-.025em; font-weight:680; margin:0 0 6px }-h1 .thin { color:var(--mut); font-weight:500 }-.sub { color:var(--mut); font-size:13.5px; margin-bottom:22px }-.sumline { display:flex; gap:14px; align-items:center; flex-wrap:wrap }-.sumline span { flex:1 1 320px }-.card { background:var(--card); border:1px solid var(--line); border-radius:0;- padding:18px 20px; margin-bottom:14px }-.q { font-weight:620; margin-bottom:2px }-.name { font-weight:620; margin-bottom:3px }-.line { display:flex; flex-wrap:wrap; align-items:baseline; gap:6px 14px; margin-bottom:6px }-.line .instr { margin:0; flex:0 1 auto }-.line .more-btn { margin:0 }-.verdict { color:var(--accent); font-weight:500; font-size:14.5px }-.verdict b { font-weight:660 }-.conf { color:var(--mut); font-size:12.5px; font-weight:500 }-.instr { color:var(--mut); font-size:13px; margin-bottom:14px }--/* dark summary panel, like the KPI tile */-.panel { background:var(--dark); color:var(--dark-ink); border-radius:0;- padding:18px 22px; margin-bottom:16px; display:flex; flex-wrap:wrap;- align-items:flex-end; gap:6px 26px }-.panel .k { font-size:11px; letter-spacing:.11em; text-transform:uppercase; color:var(--dark-mut) }-.panel .v { font-size:30px; font-weight:660; letter-spacing:-.02em; line-height:1.1 }-.panel .m { color:var(--dark-mut); font-size:13px; margin-left:auto; text-align:right }-.panel .m b { color:var(--glow); font-weight:600 }--/* per-field probability tables */-.chip { display:inline-block; background:var(--bar); border-radius:0; padding:4px 10px;- font:13px/1.45 ui-monospace,SFMono-Regular,Menlo,monospace; color:var(--ink) }-.chip.sm { padding:2px 8px; font-size:12px }-.twrap-outer { display:flex }-.twrap { border:1px solid var(--line); margin:14px 0 2px; max-width:100%;- overflow-x:auto }-.dist { width:auto; border-collapse:collapse; font-size:13px }-.dist th { text-align:left; white-space:nowrap; font-size:11.5px; letter-spacing:.06em; text-transform:lowercase;- color:var(--mut); font-weight:600; background:var(--bar); padding:9px 12px }-.dist td { padding:8px 16px 8px 12px; border-top:1px solid var(--line); vertical-align:middle }-.dist tr.win .chip { background:var(--accent); color:#fff }-.dist tr.win .num { font-weight:700; color:var(--ink) }-.meter { font:13px/1 ui-monospace,SFMono-Regular,Menlo,monospace; letter-spacing:1.5px;- white-space:nowrap }-.blocks { color:var(--accent) }-.dots { color:var(--accent-soft) }-.num { text-align:right; white-space:nowrap; color:var(--mut);- font:13px ui-monospace,SFMono-Regular,Menlo,monospace; font-variant-numeric:tabular-nums }-.legend { list-style:none; margin:14px 0 0; padding:0; font-size:13px }-.legend li { display:flex; flex-wrap:wrap; gap:8px; align-items:baseline; margin:7px 0 }-.letter { color:var(--mut); font:11.5px ui-monospace,Menlo,monospace; border:1px solid var(--line);- border-radius:0; padding:1px 6px }-.desc { color:var(--mut); flex:1 1 220px }-.meta { margin-top:13px; font:12px ui-monospace,Menlo,monospace; color:var(--mut) }-.more-btn { margin:8px 0 0; padding:5px 14px; font-size:12px }-.tags { margin-top:13px; font-size:12px; color:var(--mut) }-.tag { display:inline-block; background:var(--bar); border-radius:0;- padding:3px 11px; margin:2px 6px 0 0 }+html { -webkit-text-size-adjust:100% }+body { margin:0; background:var(--bg); color:var(--ink); font:13px/1.5 var(--sans);+ -webkit-font-smoothing:antialiased }+html:not(.js) .js { display:none !important }+a { color:var(--blue-ink); text-underline-offset:2px }+code, kbd, pre, .mono { font-family:var(--mono) }+button, input, select, textarea { font:inherit; color:inherit; letter-spacing:inherit }+:focus-visible { outline:2px solid var(--focus); outline-offset:1px }+::selection { background:var(--sel) }+::placeholder { color:var(--faint); opacity:1 }+.vh { position:absolute !important; width:1px; height:1px; overflow:hidden; clip:rect(0 0 0 0);+ white-space:nowrap }+.ic { width:16px; height:16px; flex:none; fill:none; stroke:currentColor; stroke-width:1.5;+ stroke-linecap:round; stroke-linejoin:round }+.grow { flex:1 1 auto }+.muted { color:var(--mute) }++/* top bar */+.top { display:flex; align-items:center; gap:8px; min-height:48px; padding:0 10px 0 14px;+ background:var(--chrome); border-bottom:1px solid var(--line); position:relative; z-index:5 }+.brand { display:flex; align-items:center; gap:7px; color:var(--ink); text-decoration:none;+ font-weight:650; font-size:14px; letter-spacing:-.01em }+.mark { width:22px; height:22px; color:var(--brand); flex:none }+.crumb { color:var(--faint); padding:0 2px }+.crumb-t { color:var(--ink-2); font-weight:500; font-size:inherit; line-height:inherit; margin:0 }+.links { display:flex; align-items:center; gap:2px; margin-left:auto }+.links a { color:var(--mute); text-decoration:none; padding:5px 8px; border-radius:5px; font-weight:500 }+.links a:hover, .links a[aria-current] { color:var(--ink); background:var(--hover) }+.top .vr { width:1px; height:20px; background:var(--line-2); margin:0 4px }/* controls */-select, input, textarea { background:var(--card); color:var(--ink);- border:1px solid var(--line); border-radius:0; padding:9px 12px;- font:13px ui-sans-serif,system-ui,sans-serif }-textarea { width:100%; font:12px/1.5 ui-monospace,SFMono-Regular,Menlo,monospace }-select:focus, input:focus, textarea:focus { outline:2px solid var(--accent); outline-offset:-1px }-button { font:14px ui-sans-serif,system-ui,sans-serif; cursor:pointer; border-radius:0;- border:1px solid transparent; padding:9px 18px;- background:var(--dark); color:var(--dark-ink) }-.tabs { display:flex; gap:8px; margin-bottom:18px }-.presets { display:flex; flex-wrap:wrap; gap:8px; margin:6px 0 18px }-.tab { background:var(--card); color:var(--mut); border:1px solid var(--line); font-size:13px;- padding:7px 16px; border-radius:0; display:inline-block; text-decoration:none;- line-height:1.4; font-family:inherit; cursor:pointer }-.tab:hover { color:var(--ink); border-color:var(--accent) }-.tab.on { background:var(--dark); border-color:var(--dark); color:var(--dark-ink) }-.ghost { background:var(--card); color:var(--accent); border:1.5px solid var(--accent);- width:auto; margin:4px 0 20px; padding:10px 22px; font-weight:620; font-size:14px }-.ghost:hover { background:var(--accent); color:#fff }-#addField { margin:6px 0 0; padding:8px 18px; font-size:13px }-.rm { background:transparent; color:var(--mut); border:1px solid var(--line);- padding:7px 12px; font-size:13px; flex:0 0 auto; border-radius:0}-.rm:hover { color:var(--danger); border-color:var(--danger) }-.send { font-size:15px; padding:11px 26px; margin-top:12px }-.send:hover { background:var(--accent) }-.kv { display:flex; gap:8px; margin-bottom:8px }-.kv input.k { flex:0 0 140px } .kv input.v { flex:1 1 auto; min-width:0 }-.qhead { display:flex; gap:8px; margin-bottom:9px }-.qhead input { flex:1 1 auto; min-width:0 } .qhead select { flex:0 0 auto }-.full { width:100%; margin-bottom:9px }-.foot { display:flex; gap:10px; align-items:center }-.foot .spacer { margin-left:auto; font-size:13px }-.head-row { display:flex; align-items:center; gap:10px; margin-bottom:4px }-.badge { font-size:11px; letter-spacing:.06em; text-transform:uppercase; border-radius:0;- padding:3px 10px; background:var(--bar); color:var(--mut) }-.badge.on { background:var(--accent); color:#fff }-.badge.ok { background:var(--glow); color:#06202e }-.kvlist { display:grid; grid-template-columns:auto 1fr; gap:6px 18px; font-size:13.5px;- margin-top:4px }-.kvlist dt { color:var(--mut) } .kvlist dd { margin:0; font-variant-numeric:tabular-nums }-.snip { position:relative }-.snip pre { margin:10px 0 0 }-.acts { display:flex; gap:8px; flex-wrap:wrap; margin-top:12px }-.acts a { text-decoration:none }-.err { color:var(--danger); font-size:13px; margin:10px 0 0; white-space:pre-wrap }-a { color:var(--accent) }-pre { background:var(--bar); border-radius:0; padding:12px; overflow-x:auto; font-size:12px }-@media (max-width:560px) { body { padding:12px 10px 28px } .shell { padding:18px 16px 24px;- border-radius:0} h1 { font-size:27px } .row { grid-template-columns:1fr; gap:3px }- .label, .pct { text-align:left } .kv, .qhead { flex-wrap:wrap } .kv input.k { flex:1 1 100% }- .panel .m { margin-left:0; text-align:left } }+.btn { display:inline-flex; align-items:center; justify-content:center; gap:6px; height:28px;+ padding:0 10px; border:1px solid transparent; border-radius:5px; background:transparent;+ color:var(--ink-2); cursor:pointer; font-size:12.5px; font-weight:500; white-space:nowrap;+ text-decoration:none; line-height:1 }+.btn:hover { background:var(--hover); color:var(--ink) }+.btn.line { border-color:var(--line-2); background:var(--bg) }+.btn.line:hover { background:var(--hover) }+.btn.primary { background:var(--btn); color:#fff; height:32px; padding:0 8px 0 12px; font-weight:600 }+.btn.primary:hover { background:var(--btn-hover) }+.btn:disabled, .btn[aria-disabled='true'] { opacity:.55; cursor:default; background:transparent }+.btn.primary:disabled { background:var(--btn); opacity:.7 }+.ibtn { width:28px; height:28px; display:inline-grid; place-items:center; border:0; border-radius:5px;+ background:transparent; color:var(--mute); cursor:pointer; flex:none; padding:0 }+.ibtn:hover { background:var(--hover); color:var(--ink) }+.ibtn:disabled { opacity:.4; cursor:default; background:transparent }+kbd { font:500 11px/16px var(--mono); padding:0 5px; border-radius:4px; border:1px solid var(--line-2);+ color:var(--mute); background:var(--bg); white-space:nowrap }+.btn.primary kbd { background:rgba(0,0,0,.2); border-color:rgba(255,255,255,.34); color:#fff }+.sel { position:relative; display:inline-flex; align-items:center }+.sel select { appearance:none; -webkit-appearance:none; height:28px; padding:0 26px 0 9px;+ border:1px solid var(--line-2); border-radius:5px; background:var(--bg); color:var(--ink);+ font-size:12.5px; cursor:pointer; max-width:100% }+.sel select:hover { border-color:var(--mute) }+.sel .ic { position:absolute; right:6px; pointer-events:none; color:var(--mute); width:14px; height:14px }+.theme-t .i-sun { display:none }+:root[data-theme='dark'] .theme-t .i-sun { display:block }+:root[data-theme='dark'] .theme-t .i-moon { display:none }+@media (prefers-color-scheme: dark) {+ :root:not([data-theme='light']) .theme-t .i-sun { display:block }+ :root:not([data-theme='light']) .theme-t .i-moon { display:none } }++/* badges */+.badge { display:inline-flex; align-items:center; gap:5px; font:600 11.5px/20px var(--sans);+ padding:0 7px; border-radius:4px; background:var(--sunk); color:var(--ink-2); white-space:nowrap }+.badge[data-s='ok'] { background:var(--ok-soft); color:var(--ok) }+.badge[data-s='running'] { background:var(--blue-soft); color:var(--blue-ink) }+.badge[data-s='error'] { background:var(--bad-soft); color:var(--bad) }+.badge[data-s='stale'] { background:var(--warn-soft); color:var(--warn) }+.tb { display:inline-block; font:500 11px/16px var(--mono); padding:0 5px; border-radius:3px;+ border:1px solid var(--line-2); color:var(--ink-2); margin-left:8px; vertical-align:1px;+ white-space:nowrap }++/* summary strip: what the router did; one row, or a 3-column table when narrow (never a lone cell) */+.stripw { container-type:inline-size; border-bottom:1px solid var(--line) }+.strip { display:flex; margin:0; overflow:hidden }+.strip > div { flex:none; padding:7px 14px 8px; min-width:0; box-shadow:1px 0 0 var(--line), 0 1px 0 var(--line) }+.strip > .why { flex:1 1 0; min-width:150px }+.strip dt { font:11.5px/1.5 var(--sans); color:var(--mute); white-space:nowrap }+.strip dd { margin:0; font-size:13px; color:var(--ink); overflow-wrap:anywhere }+.strip .mono dd { font:12.5px/19.5px var(--mono) }+@container (max-width: 719px) {+ .strip { display:grid; grid-template-columns:repeat(3, minmax(0,1fr)) }+ .strip > .why { grid-column:1 / -1; order:-1 } }+@container (max-width: 380px) { .strip { grid-template-columns:repeat(2, minmax(0,1fr)) } }++/* answers: one foldable row per question */+.alist { container-type:inline-size; container-name:alist }+.al-head { display:flex; justify-content:space-between; gap:12px; padding:6px 14px 6px 36px;+ font-size:11.5px; color:var(--mute); border-bottom:1px solid var(--line); background:var(--bg) }+.ans { border-bottom:1px solid var(--line) }+.ans > summary, .pv { display:grid; grid-template-columns:18px minmax(0,1fr) minmax(0,max-content);+ gap:2px 12px; padding:11px 14px 11px 10px; align-items:start }+.ans > summary { list-style:none; cursor:pointer }+.ans > summary::-webkit-details-marker { display:none }+.ans > summary:hover { background:var(--curline) }+.chev { margin-top:3px; color:var(--mute); transition:transform .12s ease }+.ans:not([open]) .chev { transform:rotate(-90deg) }+.a-id { min-width:0 }+.a-key { font:600 13px/20px var(--mono); color:var(--ink); overflow-wrap:anywhere }+.a-ins { display:block; color:var(--mute); font-size:12.5px; line-height:18px; margin-top:1px;+ overflow-wrap:anywhere }+.a-ins code, .a-meta code { font-size:.92em; padding:0 4px; border-radius:3px; background:var(--sunk);+ color:var(--ink-2); overflow-wrap:anywhere }+.a-v { text-align:right; max-width:min(44ch, 52cqi); min-width:0 }+.v-top { display:flex; justify-content:flex-end; align-items:baseline; gap:14px }+.v-label { min-width:0; font-size:16px; line-height:24px; font-weight:600; color:var(--ink);+ overflow-wrap:anywhere; letter-spacing:-.005em }+.v-label.mono { font-family:var(--mono); font-size:15px; letter-spacing:0 }+.v-pct { flex:none; min-width:6.5ch; font:600 18px/24px var(--mono); color:var(--ink); text-align:right;+ font-variant-numeric:tabular-nums; letter-spacing:-.02em }+.v-pct.unsure { color:var(--warn) }+.v-sub { display:block; font-size:12px; line-height:18px; color:var(--mute) }+.v-sub b { font-weight:600; color:var(--ink-2); font-variant-numeric:tabular-nums }+.unc { display:inline-flex; align-items:center; gap:4px; font-weight:500; color:var(--warn) }+.unc::before { content:""; width:6px; height:6px; border-radius:50%; border:1.5px solid currentColor }+.pv .a-v { color:var(--mute); font-size:12px; padding-top:1px }+.pv .dotm { width:6px; height:6px; border-radius:50%; background:var(--line-2); margin:7px 0 0 5px }+.a-body { padding:0 16px 14px 38px }+/* fixed tracks (container units, no content sizing), so bars and percentages line up across questions */+.dist { list-style:none; margin:2px 0 0; padding:0; display:grid;+ grid-template-columns:32cqi minmax(64px,1fr) 6.5ch; column-gap:12px }+.dr { display:grid; grid-column:1 / -1; grid-template-columns:subgrid; align-items:center; padding:3px 0 }+.dr-l { display:flex; font:12.5px/18px var(--mono); color:var(--ink-2); overflow-wrap:anywhere; min-width:0 }+.dr-l.prose { font-family:var(--sans); font-size:13px }+.dr-i { flex:none; min-width:2ch; margin-right:6px; color:var(--mute); font:11.5px/18px var(--mono) }+.dr.win .dr-l { color:var(--ink); font-weight:600 }+.bar { display:block; height:8px; border-radius:2px;+ background:radial-gradient(circle, var(--dot) 0 1px, transparent 1.5px) 0 50% / 5px 8px repeat-x }+.fill { display:block; height:100%; min-width:1px; border-radius:2px; background:var(--bar) }+.dr.win .fill { background:var(--brand) }+.dr-p { font:12px/18px var(--mono); text-align:right; color:var(--mute); font-variant-numeric:tabular-nums }+.dr.win .dr-p { color:var(--ink); font-weight:600 }+.dr-d { grid-column:1 / -1; font-size:12px; line-height:17px; color:var(--mute); padding:0 0 2px;+ overflow-wrap:anywhere }+@container alist (min-width: 700px) {+ .dist { grid-template-columns:26cqi minmax(80px,1fr) 6.5ch 28cqi }+ .dr-d { grid-column:auto; padding:0 } }+.scale { display:flex; align-items:center; gap:10px; margin:4px 0 8px; font:12px/18px var(--mono);+ color:var(--mute) }+.sc-track { position:relative; flex:1 1 auto; max-width:360px; height:8px; margin:0 4px;+ background:radial-gradient(circle, var(--dot) 0 1px, transparent 1.5px) 0 50% / 5px 8px repeat-x }+.sc-fill { position:absolute; left:0; top:3px; height:2px; background:var(--brand) }+.sc-tick { position:absolute; top:0; width:1px; height:8px; background:var(--line-2) }+.sc-mark { position:absolute; top:-2px; width:12px; height:12px; margin-left:-6px; border-radius:50%;+ background:var(--bg); border:2.5px solid var(--brand) }+.a-meta { display:flex; flex-wrap:wrap; align-items:center; gap:4px 16px; margin-top:8px; font-size:12px;+ color:var(--mute) }+.a-meta b { font:600 12px var(--mono); color:var(--ink-2) }+.a-meta > span { display:inline-flex; align-items:baseline; gap:5px }+.tipw { position:relative; display:inline-flex }+.tipt { display:inline-flex; align-items:baseline; gap:5px; padding:0; border:0; background:none;+ color:var(--mute); cursor:help; font-size:12px }+.tipt .ic { width:14px; height:14px; align-self:center }+.tipb { display:none; position:absolute; left:0; bottom:calc(100% + 6px); z-index:20; width:min(340px, 80vw);+ padding:9px 11px; border-radius:6px; background:var(--toast); color:var(--toast-ink);+ font-size:12px; line-height:1.45; box-shadow:0 6px 20px var(--shadow) }+.tipb::after { content:""; position:absolute; left:0; right:0; top:100%; height:7px } /* hover bridge */+.tipb code { background:rgba(127,127,127,.22); color:inherit }+.tipw.below .tipb { bottom:auto; top:calc(100% + 6px) }+.tipw.below .tipb::after { top:auto; bottom:100% }+.tipw:hover .tipb, .tipw:focus-within .tipb { display:block }+.tipw.tip-off .tipb { display:none }+.a-raw { margin:6px 0 0; padding:10px 12px; background:var(--chrome); border:1px solid var(--line);+ border-radius:5px; font-size:12px; overflow:auto }+@container alist (max-width: 520px) {+ .ans > summary, .pv { grid-template-columns:18px minmax(0,1fr) }+ .a-v { grid-column:2; text-align:left; max-width:none }+ .v-top { justify-content:flex-start }+ .a-body { padding-left:28px; padding-right:12px }+ .dist { grid-template-columns:minmax(0,1fr) 6.5ch }+ .dr-l { grid-column:1 } .dr-p { grid-column:2 } .bar { grid-column:1 / -1; order:3; margin:2px 0 1px }+ .dr-d { order:4 } }++/* documents: /models, /health, POST /gui */+.doc { max-width:1040px; margin:0 auto; padding:24px 20px 56px }+.doc-head { display:flex; flex-wrap:wrap; align-items:center; gap:8px 12px; margin:0 0 16px }+.doc h1 { font-size:20px; line-height:28px; font-weight:650; letter-spacing:-.015em; margin:0 }+.doc-sub { color:var(--mute) }+.doc p.lead { color:var(--ink-2); margin:-8px 0 20px; max-width:72ch }+.panel { border:1px solid var(--line); border-radius:7px; background:var(--bg); overflow:hidden }+.panel + .panel { margin-top:14px }+.panel > .ans:last-child { border-bottom:0 }+.ph { display:flex; flex-wrap:wrap; align-items:center; gap:6px 10px; padding:10px 14px;+ border-bottom:1px solid var(--line); background:var(--chrome) }+.ph h2 { margin:0; font:600 14px/22px var(--mono) }+.ph h2.t { font-family:var(--sans); font-size:13.5px }+.ph .repo { margin-left:auto; font:12px var(--mono); color:var(--mute) }+.pb { padding:12px 14px 14px }+.pb p { margin:0 0 10px; max-width:76ch; color:var(--ink-2) }+.chips { display:flex; flex-wrap:wrap; gap:6px; margin:0 0 12px; padding:0; list-style:none }+.chips li { font-size:12px; padding:1px 8px; border-radius:4px; border:1px solid var(--line-2); color:var(--ink-2) }+.chips li.muted { border-color:transparent; padding-left:0; color:var(--mute) }+.pb.two { display:grid; grid-template-columns:minmax(0,5fr) minmax(0,6fr); grid-template-rows:auto auto 1fr;+ gap:4px 20px; align-items:start }+.pb.two .snip { grid-column:2; grid-row:1 / span 3; margin:0 }+@media (max-width: 860px) { .pb.two { display:block } .pb.two .snip { margin:0 0 10px } }+.snip { position:relative; margin:0 0 10px }+.snip pre { margin:0; padding:10px 12px; background:var(--chrome); border:1px solid var(--line);+ border-radius:5px; font-size:12px; line-height:18px; overflow:auto; white-space:pre-wrap;+ overflow-wrap:anywhere }+.acts { display:flex; flex-wrap:wrap; gap:8px }+.kvt { display:grid; grid-template-columns:max-content minmax(0,1fr); margin:0 }+.kvt dt, .kvt dd { padding:7px 14px; border-bottom:1px solid var(--line); margin:0 }+.kvt dt { color:var(--mute) }+.kvt dd { font:12.5px/20px var(--mono) }+.kvt > :nth-last-child(-n+2) { border-bottom:0 }+.status { display:flex; align-items:center; gap:10px; font-size:20px; font-weight:650 }+.status::before { content:""; width:10px; height:10px; border-radius:50%; background:var(--ok) }+.status.wait::before { background:var(--warn) }+.eps { width:100%; border-collapse:collapse; font-size:12.5px }+.eps td { padding:7px 14px; border-bottom:1px solid var(--line); vertical-align:top }+.eps tr:last-child td { border-bottom:0 }+.eps td:first-child { font:600 11.5px/20px var(--mono); color:var(--mute); width:1%; white-space:nowrap }+.eps td:nth-child(2) { font:12.5px/20px var(--mono); white-space:nowrap; width:1%; padding-right:28px }+.eps td:last-child { color:var(--ink-2) }+.errbox { margin:14px; padding:12px 14px; border:1px solid var(--line-2); border-left:3px solid var(--bad);+ border-radius:5px; background:var(--bad-soft) }+.errbox h3 { margin:0 0 6px; font-size:13.5px; color:var(--bad) }+.errbox ul { margin:0; padding:0; list-style:none; font:12.5px/19px var(--mono); color:var(--ink) }+.errbox li { padding:2px 0; overflow-wrap:anywhere; white-space:pre-wrap }+.errbox p { margin:8px 0 0; color:var(--ink-2) }+.errloc { font:inherit; color:var(--blue-ink); background:none; border:0; padding:0; cursor:pointer;+ text-decoration:underline; text-underline-offset:2px }+.foot { display:flex; flex-wrap:wrap; gap:8px; margin-top:16px }+@media (max-width: 700px) {+ .top { flex-wrap:wrap; padding:6px 8px 6px 12px; gap:6px }+ .links a { padding:5px 6px }+ .doc { padding:16px 12px 40px } }+@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition:none !important;+ animation:none !important } }"""-_THEME_JS = r"""(function () {- const NAMES = ["ocean", "paper", "midnight"];- const LABEL = {ocean: "Ocean", paper: "Paper", midnight: "Midnight"};- const key = "laya.theme";- const get = () => { try { return localStorage.getItem(key); } catch (e) { return null; } };- const set = (t) => {- document.documentElement.dataset.theme = t;- try { localStorage.setItem(key, t); } catch (e) {}- const b = document.getElementById("theme");- if (b) b.textContent = LABEL[t] || t;- };- const q = new URLSearchParams(location.search).get("theme");- set(NAMES.includes(q) ? q : (get() || "ocean")); /* runs in <head>, before first paint */- window.addEventListener("DOMContentLoaded", () => {- const cur = document.documentElement.dataset.theme;- set(cur);- const b = document.getElementById("theme");- if (b) b.onclick = () => set(NAMES[(NAMES.indexOf(document.documentElement.dataset.theme) + 1) % NAMES.length]);- });+ var r = document.documentElement, t = null;+ var names = {light: "light", dark: "dark", ocean: "light", paper: "light", midnight: "dark"};+ var pick = function (k) { return k && Object.prototype.hasOwnProperty.call(names, k) ? names[k] : null; };+ r.classList.add("js");+ try { t = pick(localStorage.getItem("laya.theme")); } catch (e) {}+ var q = pick(new URLSearchParams(location.search).get("theme"));+ if (q || t) r.dataset.theme = q || t; /* runs in <head>, before first paint */})();"""-_MORE_JS = r"""-document.addEventListener("click", function (e) {- const all = e.target.closest("#expand-all");- if (all) {- const open = all.dataset.open !== "1";- for (const b of document.querySelectorAll(".more-btn"))- if ((b.dataset.open === "1") !== open) b.click();- all.dataset.open = open ? "1" : "0";- all.textContent = open ? "Collapse all" : "Expand all";- return;- }- const b = e.target.closest(".more-btn");- if (!b) return;- const card = b.closest(".card");- const open = b.dataset.open !== "1";- card.querySelectorAll(".more").forEach(el => { el.hidden = !open; });- b.dataset.open = open ? "1" : "0";- b.textContent = open ? "Hide details" : b.dataset.label;- b.classList.toggle("on", open);-});-"""---_SNIP_JS = r"""-window.addEventListener("DOMContentLoaded", () => {- const here = location.origin;+_PAGE_JS = r"""+(function () {+ const root = document.documentElement;+ const mq = matchMedia("(prefers-color-scheme: dark)");+ const dark = () => (root.dataset.theme || (mq.matches ? "dark" : "light")) === "dark";+ const syncTheme = () => {+ for (const b of document.querySelectorAll("[data-theme-toggle]")) {+ b.setAttribute("aria-pressed", String(dark()));+ b.title = dark() ? "Switch to light theme" : "Switch to dark theme";+ }+ };+ syncTheme();+ if (mq.addEventListener) mq.addEventListener("change", syncTheme);for (const pre of document.querySelectorAll(".snip pre"))- pre.textContent = pre.textContent.split("http://127.0.0.1:8000").join(here);- for (const b of document.querySelectorAll(".copy"))- b.onclick = () => {- const pre = b.closest(".card").querySelector(".snip pre");- navigator.clipboard.writeText(pre.textContent).then(- () => { b.textContent = "Copied"; setTimeout(() => (b.textContent = "Copy curl"), 1400); },- () => { b.textContent = "Copy failed"; });+ pre.textContent = pre.textContent.split("http://127.0.0.1:8000").join(location.origin);+ const rows = () => Array.from(document.querySelectorAll("details.ans"));+ const syncExpand = () => {+ const all = rows().every((d) => d.open);+ for (const b of document.querySelectorAll("[data-expand]")) b.textContent = all ? "Collapse all" : "Expand all";+ };+ const toggleAll = () => {+ const open = rows().some((d) => !d.open);+ rows().forEach((d) => { d.open = open; });+ syncExpand();+ };+ document.addEventListener("toggle", syncExpand, true);+ /* a tooltip stays inside the box that clips it, and flips under its button when there is no room above */+ const placeTip = (w) => {+ const tip = w.querySelector(".tipb");+ let box = w.parentElement;+ while (box && box !== document.body && getComputedStyle(box).overflow === "visible") box = box.parentElement;+ const r = w.getBoundingClientRect(), g = box && box !== document.body ? box.getBoundingClientRect() : null;+ const x = g ? g.left + box.clientLeft : 0; /* the client area, so a tooltip never runs under a scrollbar */+ const b = g ? {left: x, top: g.top + box.clientTop, right: x + box.clientWidth}+ : {left: 0, top: 0, right: document.documentElement.clientWidth};+ const lo = Math.max(0, b.left) + 8, hi = Math.min(innerWidth, b.right) - 8, width = Math.min(340, hi - lo);+ tip.style.width = width + "px";+ tip.style.left = Math.max(lo, Math.min(r.left, hi - width)) - r.left + "px";+ tip.style.display = "block";+ const tall = tip.offsetHeight;+ tip.style.display = "";+ w.classList.toggle("below", r.top - tall - 6 < Math.max(0, b.top));+ };+ const tipOf = (e) => e.target.closest && e.target.closest(".tipw");+ document.addEventListener("mouseover", (e) => {+ const w = tipOf(e);+ if (w && !w.contains(e.relatedTarget)) placeTip(w);+ });+ document.addEventListener("focusin", (e) => { const w = tipOf(e); if (w) placeTip(w); });+ document.addEventListener("scroll", () => { /* an open tooltip follows its button as the pane scrolls */+ for (const w of document.querySelectorAll(".tipw:hover, .tipw:focus-within")) placeTip(w);+ }, {capture: true, passive: true});+ document.addEventListener("keydown", (e) => { /* Esc hides an open tooltip until the pointer or focus leaves */+ if (e.key !== "Escape") return;+ for (const w of document.querySelectorAll(".tipw:hover, .tipw:focus-within")) {+ w.classList.add("tip-off");+ const on = () => {+ w.classList.remove("tip-off");+ w.removeEventListener("mouseleave", on);+ w.removeEventListener("focusout", on);+ };+ w.addEventListener("mouseleave", on);+ w.addEventListener("focusout", on);+ }+ });+ window.layaCopy = function (text, btn) {+ const done = (ok) => {+ if (!btn) return;+ const was = btn.dataset.label || btn.textContent;+ btn.dataset.label = was;+ btn.textContent = ok ? "Copied" : "Copy failed";+ setTimeout(() => { btn.textContent = was; }, 1400);};-});+ const fallback = () => {+ const ta = document.createElement("textarea");+ ta.value = text; ta.setAttribute("readonly", ""); ta.style.position = "fixed"; ta.style.opacity = "0";+ document.body.append(ta); ta.select();+ let ok = false;+ try { ok = document.execCommand("copy"); } catch (e) {}+ ta.remove(); done(ok);+ return ok;+ };+ if (navigator.clipboard && window.isSecureContext)+ return navigator.clipboard.writeText(text).then(() => { done(true); return true; }, fallback);+ return Promise.resolve(fallback());+ };+ document.addEventListener("click", (e) => {+ const t = e.target.closest("[data-theme-toggle]");+ if (t) {+ const next = dark() ? "light" : "dark";+ root.dataset.theme = next;+ try { localStorage.setItem("laya.theme", next); } catch (err) {}+ syncTheme();+ return;+ }+ const c = e.target.closest("[data-copy]"), src = c && document.getElementById(c.dataset.copy);+ if (src) { /* a painted <pre> holds one block per line, and textContent would join them with no newline */+ const lines = src.querySelector(":scope > .ln");+ window.layaCopy(lines ? Array.from(src.children, (d) => d.textContent).join("\n") : src.textContent, c);+ return;+ }+ if (e.target.closest("[data-expand]")) toggleAll();+ });+})();"""+# Laya's mark (assets/logo-mark-mono.svg): an open stroke that breaks into points and shrinks to one.+_MARK_DOTS = (+ (48.213, 42.609, 2.782, .929), (49.456, 33.958, 2.527, .894), (46.696, 26.535, 2.291, .852),+ (41.336, 21.918, 2.067, .803), (35.242, 20.694, 1.852, .749), (30.173, 22.422, 1.642, .691),+ (27.294, 25.889, 1.438, .628), (26.902, 29.558, 1.239, .562), (28.430, 32.075, 1.043, .493),+ (30.702, 32.690, 0.850, .420), (32.0, 32.0, 2.35, 1),+)+_MARK = (+ "<svg class='mark' viewBox='0 0 64 64' aria-hidden='true'>"+ "<path d='M 22.141 13.458 A 21.0 21.0 0 1 0 42.500 50.187' fill='none' stroke='currentColor' "+ "stroke-width='5' stroke-linecap='round' opacity='.95'/><g fill='currentColor'>"+ + "".join(+ f"<circle cx='{x}' cy='{y}' r='{r}' opacity='{o}' style='--i:{i}'/>"+ for i, (x, y, r, o) in enumerate(_MARK_DOTS)+ )+ + "</g></svg>"+)+_FAVICON = (+ "data:image/svg+xml,"+ + _MARK.replace("class='mark' ", "xmlns='http://www.w3.org/2000/svg' ")+ .replace("currentColor", "%232a78d6").replace("<", "%3C").replace(">", "%3E")+)-_NAV = (- "<div class='bar'><div class='brand'><span class='dot'></span>Laya</div>"- "<nav><a href='/'>New request</a><a href='/docs'>API</a>"- "<a href='/models'>Models</a><a href='/health'>Health</a>"- "<button type='button' class='tab theme' id='theme'>Theme</button></nav></div>"+_ICONS = {+ "x": "<path d='M4.5 4.5l7 7M11.5 4.5l-7 7'/>",+ "plus": "<path d='M8 3.5v9M3.5 8h9'/>",+ "dup": "<rect x='5.5' y='5.5' width='8' height='8' rx='1.5'/>"+ "<path d='M10.5 5.5v-2a1 1 0 0 0-1-1h-6a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2'/>",+ "up": "<path d='M4.5 9.5L8 6l3.5 3.5'/>",+ "down": "<path d='M4.5 6.5L8 10l3.5-3.5'/>",+ "chev": "<path d='M4.5 6.5L8 10l3.5-3.5'/>",+ "link": "<path d='M6.8 9.2l2.4-2.4M7.2 4.8l1-1a2.7 2.7 0 0 1 3.9 3.9l-1 1"+ "M8.8 11.2l-1 1a2.7 2.7 0 0 1-3.9-3.9l1-1'/>",+ "sun": "<circle cx='8' cy='8' r='2.8'/><path d='M8 1.8v1.4M8 12.8v1.4M1.8 8h1.4M12.8 8h1.4"+ "M3.6 3.6l1 1M11.4 11.4l1 1M3.6 12.4l1-1M11.4 4.6l1-1'/>",+ "moon": "<path d='M13.2 9.6A5.6 5.6 0 0 1 6.4 2.8a5.6 5.6 0 1 0 6.8 6.8z'/>",+ "cols": "<rect x='2' y='3' width='12' height='10' rx='1.5'/><path d='M8 3v10'/>",+ "rows": "<rect x='2' y='3' width='12' height='10' rx='1.5'/><path d='M2 8h12'/>",+ "info": "<circle cx='8' cy='8' r='6'/><path d='M8 7.3v3.7M8 5.1v.1'/>",+ "warn": "<path d='M8 2.6l5.9 10.4H2.1z'/><path d='M8 6.6v2.9M8 11.2v.1'/>",+ "check": "<path d='M3.5 8.5l3 3 6-7'/>",+ "braces": "<path d='M6 2.5c-1.4 0-2 .6-2 2v1.8c0 1-.5 1.7-1.5 1.7 1 0 1.5.7 1.5 1.7v1.8c0 1.4.6 2 2 2"+ "M10 2.5c1.4 0 2 .6 2 2v1.8c0 1 .5 1.7 1.5 1.7-1 0-1.5.7-1.5 1.7v1.8c0 1.4-.6 2-2 2'/>",+ "fields": "<path d='M2.5 4.5h3M7.5 4.5h6M2.5 8h3M7.5 8h6M2.5 11.5h3M7.5 11.5h6'/>",+ "form": "<rect x='2.5' y='2.5' width='11' height='4.5' rx='1'/>"+ "<rect x='2.5' y='9' width='11' height='4.5' rx='1'/>",+ "format": "<path d='M2.5 3.5h11M5 6.5h8.5M5 9.5h8.5M2.5 12.5h11'/>",+ "kbd": "<rect x='1.5' y='4' width='13' height='8.5' rx='1.5'/>"+ "<path d='M4 7h.01M6.5 7h.01M9 7h.01M11.5 7h.01M5 9.8h6'/>",+ "undo": "<path d='M3.5 6h6.2a3.3 3.3 0 0 1 0 6.6H6.5'/><path d='M6 3.3L3.3 6 6 8.7'/>",+ "expand": "<path d='M5 6l3-3 3 3M5 10l3 3 3-3'/>",+}+_SPRITE = (+ "<svg width='0' height='0' style='position:absolute' aria-hidden='true'>"+ + "".join(f"<symbol id='i-{name}' viewBox='0 0 16 16'>{d}</symbol>" for name, d in _ICONS.items())+ + "</svg>")-def _page(title: str, body: str) -> HTMLResponse:- return HTMLResponse(- f"<!doctype html><html><head><meta charset='utf-8'>"- f"<meta name='viewport' content='width=device-width,initial-scale=1'>"- f"<title>{escape(title)}</title><style>{_CSS}</style>"- f"<script>{_THEME_JS}</script></head>"- f"<body><div class='shell'>{_NAV}{body}</div>"- f"<script>{_MORE_JS}</script><script>{_SNIP_JS}</script></body></html>"+def _icon(name: str, cls: str = "") -> str:+ return f"<svg class='ic{' ' + cls if cls else ''}' aria-hidden='true'><use href='#i-{name}'/></svg>"+++def _topbar(current: str = "", middle: str = "", tools: str = "") -> str:+ """Brand, breadcrumb, page links and the theme toggle, shared by every page."""+ links = "".join(+ f"<a href='{href}'{' aria-current=page' if href == current else ''}>{label}</a>"+ for href, label in (("/models", "Models"), ("/health", "Health"), ("/docs", "API docs"))+ )+ return (+ f"<header class='top'><a class='brand' href='/'>{_MARK}<span>Laya</span></a>{middle}"+ f"<nav class='links' aria-label='Pages'>{links}</nav>{tools}"+ "<button type='button' class='ibtn theme-t js' data-theme-toggle aria-label='Dark theme'>"+ f"{_icon('moon', 'i-moon')}{_icon('sun', 'i-sun')}</button></header>")-#: how sure the top option has to be before the verdict drops its hedge-_HEDGES = (- (80.0, ""), # clear enough to state flatly- (70.0, "certainly is"),- (60.0, "mostly is"),- (52.0, "somehow is"),- (0.0, "trending to"),-)+def _crumb(label: str, tag: str = "span") -> str:+ return f"<span class='crumb' aria-hidden='true'>/</span><{tag} class='crumb-t'>{escape(label)}</{tag}>"-def _verdict(label: str, pct: float) -> str:- """The winning option, hedged by how far ahead of the field it is."""- hedge = next(word for floor, word in _HEDGES if pct >= floor)- return (- f"<span class='verdict'>{escape(hedge) + ' ' if hedge else ''}"- f"<b>{escape(label)}</b></span>"+def _html(title: str, body: str, css: str = "", script: str = "", body_attrs: str = "") -> HTMLResponse:+ return HTMLResponse(+ "<!doctype html><html lang='en'><head><meta charset='utf-8'>"+ "<meta name='viewport' content='width=device-width,initial-scale=1'>"+ f"<title>{escape(title)}</title><link rel='icon' href=\"{_FAVICON}\">"+ f"<script>{_THEME_JS}</script><style>{_CSS}{css}</style></head>"+ f"<body{body_attrs}>{_SPRITE}{body}<script>{_PAGE_JS}</script>{script}</body></html>")-_CELLS = 18 #: width of the ascii distribution meter-_BLOCK = "\u2588"-_DOT = "\u00b7"+def _page(title: str, crumb: str, body: str, current: str = "") -> HTMLResponse:+ return _html(title, _topbar(current, _crumb(crumb)) + f"<main class='doc'>{body}</main>")-def _dist(pct: float) -> str:- """A blocky meter: filled cells for the mass, dots for the rest."""- filled = int(round(max(0.0, min(100.0, pct)) / 100 * _CELLS))- blocks = _BLOCK * filled- dots = _DOT * (_CELLS - filled)- return f"<span class='blocks'>{blocks}</span><span class='dots'>{dots}</span>"+def _describe(value: Any) -> str:+ """A criterion's description as text: None or "" shows nothing, never the word None."""+ if value is None or value == "":+ return ""+ return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False)-def _criteria_legend(question: Dict[str, Any], rows: List[tuple]) -> str:- """The caller's own criteria, lettered A/B/C, next to each label."""- crit = question.get("criteria")- if isinstance(crit, dict):- described = {str(k): (str(v) if v is not None else "") for k, v in crit.items()}- elif isinstance(crit, list):- described = {str(v): "" for v in crit}- else:- return ""+def _pct(p: float) -> str:+ """A probability as a percent; a near-certainty never rounds up to 100%, nor a long shot down to 0%."""+ if 0 < p < 0.001:+ return "<0.1%"+ if 0.999 < p < 1:+ return ">99.9%"+ return f"{p * 100:.1f}%"- items = ""- for i, (label, _pct, _win) in enumerate(rows):- desc = described.get(label)- if desc is None:- continue- letter = chr(ord("A") + i)- items += (- f"<li><span class='letter'>{letter}</span>"- f"<code class='chip sm'>{escape(label)}</code>"- + (f"<span class='desc'>— {escape(desc)}</span>" if desc else "")- + "</li>"- )- return f"<ul class='legend'>{items}</ul>" if items else ""+def _ms(secs: float) -> str:+ return f"{secs * 1000:.0f} ms" if secs < 1 else f"{secs:.2f} s"-def _dist_table(rows: List[tuple], head: str) -> str:- body = "".join(- f"<tr class='{'win' if win else ''}'>"- f"<td><code class='chip sm'>{escape(label)}</code></td>"- f"<td class='meter'>{_dist(pct)}</td>"- f"<td class='num'>{pct:.1f}%</td></tr>"- for label, pct, win in rows- )- return (- "<div class='twrap-outer'><div class='twrap'><table class='dist'><thead><tr>"- f"<th>{escape(head)}</th><th>distribution</th><th>probability</th>"- f"</tr></thead><tbody>{body}</tbody></table></div></div>"- )+def _with_code(text: str) -> str:+ """Escaped text in which `backtick` spans, as laya's presets write field names, become <code>."""+ parts = text.split("`")+ if len(parts) < 3 or len(parts) % 2 == 0:+ return escape(text)+ return "".join(f"<code>{escape(p)}</code>" if i % 2 else escape(p) for i, p in enumerate(parts))-def _answer_card(name: str, ans: Dict[str, Any], question: Dict[str, Any]) -> str:- """A field chip, its question text, the verdict, and a foldable probability table."""- kind = ans.get("type", question.get("type", ""))- rows: List[tuple] = []- head = "choice"++def _level_key(k: Any) -> tuple:+ """Score levels arrive as "0", "1", ...; sort them as numbers."""+ try:+ return (0, float(k))+ except (TypeError, ValueError):+ return (1, 0.0)+++def _answer_dist(kind: str, ans: Dict[str, Any], question: Dict[str, Any]) -> tuple:+ """Rows (label, p, description, level) in display order, and the index of the answer."""+ probs = ans.get("probabilities")+ probs = probs if isinstance(probs, dict) else {}+ crit = question.get("criteria")+ crit = crit if isinstance(crit, dict) else {}if kind == "choice":- probs = ans.get("probabilities") or {}- winner = ans.get("choice")- rows = [(k, v * 100, k == winner) for k, v in sorted(probs.items(), key=lambda kv: -kv[1])]- elif kind == "score":- probs = ans.get("probabilities") or {}+ rows = sorted(+ ((str(k), float(v), _describe(crit.get(k)), None) for k, v in probs.items()), key=lambda r: -r[1]+ )+ win = next((i for i, r in enumerate(rows) if r[0] == str(ans.get("choice"))), 0)+ return rows, win+ if kind == "score":legend = ans.get("legend") or {}- top = max(probs, key=probs.get) if probs else None- rows = [(legend.get(str(k), str(k)), v * 100, k == top) for k, v in probs.items()]- head = "level"- elif kind == "noul":- p = float(ans.get("noul", 0.0))- rows = [("true", p * 100, p >= 0.5), ("false", (1 - p) * 100, p < 0.5)]- head = "answer"-- win = next(((lbl, pct) for lbl, pct, w in rows if w), None)- fold = win is not None and len(rows) > 1-- verdict = ""- if win:- conf = ans.get("confidence")- verdict = _verdict(*win)- if isinstance(conf, float):- verdict += f" <span class='conf'>({conf:.2f})</span>"-- toggle = (- "<button type='button' class='tab more-btn' data-label='Show details'>Show details</button>"- if fold- else ""+ rows = [+ (_describe(legend.get(str(k), k)) or str(k), float(probs[k]), "", str(k))+ for k in sorted(probs, key=_level_key)+ ]+ win = max(range(len(rows)), key=lambda i: rows[i][1]) if rows else 0+ return rows, win+ if kind == "noul" and isinstance(ans.get("noul"), (int, float)):+ p = float(ans["noul"])+ rows = [("true", p, _describe(crit.get("true")), None), ("false", 1 - p, _describe(crit.get("false")), None)]+ return rows, (0 if p >= 0.5 else 1)+ return [], 0+++_CERTAINTY_TIP = (+ "The `confidence` field is 1 minus the normalized entropy of the whole distribution: how peaked it is. "+ "It is not calibrated, so do not gate on it. `answer_confidence` is the probability of the "+ "reported answer, calibrated so that answers returned at 0.9 are right about 90% of the time."+)+_UNSURE = 0.6+++def _answer_row(name: str, ans: Any, question: Dict[str, Any], n: int) -> str:+ """One foldable row: key, type and instructions; the verdict; the whole distribution."""+ known = isinstance(ans, dict)+ kind = str((ans.get("type") if known else None) or question.get("type") or "")+ rows, win = _answer_dist(kind, ans, question) if known else ([], 0)+ head = (+ f"{_icon('chev', 'chev')}<span class='a-id'><span class='a-key'>{escape(name)}</span>"+ f"<span class='tb'>{escape(kind or '?')}</span>"+ f"<span class='a-ins'>{_with_code(str(question.get('instructions', '')))}</span></span>")+ if not rows:+ raw = escape(json.dumps(ans, indent=2, ensure_ascii=False))+ return (+ f"<details class='ans' open><summary>{head}<span class='a-v'><span class='v-sub'>raw answer</span>"+ f"</span></summary><div class='a-body'><pre class='a-raw'>{raw}</pre></div></details>"+ )- extras = []- for key in ("score", "noul", "confidence"):- if isinstance(ans.get(key), float):- extras.append(f"{key} {ans[key]:.3f}")- footer = f"<div class='meta'>{escape(' · '.join(extras))}</div>" if extras else ""- footer = footer.replace("&middot;", "·")-- if rows:- details = _dist_table(rows, head) + _criteria_legend(question, rows) + footer- else:- details = f"<pre>{escape(json.dumps(ans, indent=2))}</pre>"+ label = rows[win][0]+ calibrated = ans.get("answer_confidence")+ calibrated = float(calibrated) if isinstance(calibrated, (int, float)) else max(r[1] for r in rows)+ certainty = ans.get("confidence")+ score = ans.get("score") if kind == "score" and isinstance(ans.get("score"), (int, float)) else None+ top = len(rows) - 1+ unsure = calibrated < _UNSURE+ sub = []+ if score is not None:+ sub.append(f"score <b>{score:.2f}</b> on 0–{top}")+ if unsure:+ runner = max((r for i, r in enumerate(rows) if i != win), key=lambda r: r[1], default=None)+ sub.append(+ "<span class='unc' title='Calibrated confidence below 0.60'>uncertain</span>"+ + (f", runner-up <b>{escape(runner[0])}</b> {_pct(runner[1])}" if runner else "")+ )+ verdict = (+ f"<span class='a-v'><span class='v-top'><span class='v-label{'' if kind == 'score' else ' mono'}'>"+ f"{escape(label)}</span><span class='v-pct{' unsure' if unsure else ''}'>{_pct(calibrated)}</span></span>"+ + (f"<span class='v-sub'>{' · '.join(sub)}</span>" if sub else "")+ + "</span>"+ )+ scale = ""+ if score is not None and top > 0:+ at = max(0.0, min(1.0, score / top)) * 100+ ticks = "".join(f"<span class='sc-tick' style='left:{i / top * 100:.2f}%'></span>" for i in range(top + 1))+ scale = (+ f"<div class='scale' role='img' aria-label='Expected score {score:.2f} on a 0 to {top} scale'>"+ f"<span>0</span><span class='sc-track'>{ticks}<span class='sc-fill' style='width:{at:.2f}%'></span>"+ f"<span class='sc-mark' style='left:{at:.2f}%'></span></span><span>{top}</span></div>"+ )+ items = "".join(+ f"<li class='dr{' win' if i == win else ''}'>"+ f"<span class='dr-l{' prose' if kind == 'score' else ''}'>"+ + (f"<span class='dr-i'>{escape(level)}</span>" if level is not None else "")+ + f"{escape(lbl)}</span><span class='bar' aria-hidden='true'>"+ f"<span class='fill' style='width:{max(0.0, min(1.0, pr)) * 100:.2f}%'></span></span>"+ f"<span class='dr-p'>{_pct(pr)}</span>"+ + (f"<span class='dr-d'>{escape(desc)}</span>" if desc else "")+ + "</li>"+ for i, (lbl, pr, desc, level) in enumerate(rows)+ )+ meta = f"<span><code>answer_confidence</code> <b>{calibrated:.4f}</b> calibrated</span>"+ if isinstance(certainty, (int, float)) and abs(float(certainty) - calibrated) > 5e-5:+ meta += (+ f"<span class='tipw'><button type='button' class='tipt' aria-describedby='tip-{n}'>"+ f"<code>confidence</code> <b>{float(certainty):.4f}</b> entropy, not calibrated {_icon('info')}</button>"+ f"<span class='tipb' role='tooltip' id='tip-{n}'>{_with_code(_CERTAINTY_TIP)}</span></span>"+ )return (- f"<div class='card'>"- f"<div class='line'><code class='chip'>{escape(name)}</code>{verdict}{toggle}</div>"- f"<div class='instr'>{escape(question.get('instructions', ''))}</div>"- f"<div class='details{' more' if fold else ''}'{' hidden' if fold else ''}>{details}</div>"- f"</div>"+ f"<details class='ans' open><summary>{head}{verdict}</summary><div class='a-body'>{scale}"+ f"<ol class='dist'>{items}</ol><div class='a-meta'>{meta}</div></div></details>")+def _strip(res: Dict[str, Any], timing: str) -> str:+ """What the router did: checkpoint, why, what it detected, tokens and time."""+ routing = res.get("routing") or {}+ det = routing.get("detection") or {}+ usage = res.get("usage") or {}+ cells = [+ ("Checkpoint", routing.get("model"), "mono"),+ ("Reason", routing.get("reason"), "why"),+ ("Detected", " / ".join(str(x) for x in (det.get("language"), det.get("script")) if x), "mono"),+ ("Engine", res.get("model"), "mono"),+ ("Input tokens", usage.get("input_tokens"), "mono"),+ ]+ if timing:+ cells.append(("Server time", timing, "mono"))+ return "<div class='stripw'><dl class='strip'>" + "".join(+ f"<div class='{cls}'><dt>{k}</dt><dd>{escape(str(v))}</dd></div>"+ for k, v, cls in cells+ if v not in (None, "")+ ) + "</dl></div>"++_EXAMPLE_STATE = json.dumps({"from": "[email protected]",@@ -742,217 +1028,2117 @@ PRESETS: Dict[str, Dict[str, Any]] = _build_presets()_PRESETS_JSON = json.dumps(PRESETS)-_BUILDER_JS = r"""-const QTYPES = ["choice", "score", "noul"];-const HINT = {- choice: "one option per line, as label: description",- score: "one level per line, lowest first",- noul: ""-};+_PLAYGROUND_CSS = """+body.pg { display:flex; flex-direction:column; min-height:100vh }+.top .preset select { font-weight:500; max-width:260px }+.edited { font-size:12px; color:var(--mute); font-style:italic }+.work { display:flex; flex-direction:column; flex:1 1 auto }+.pane { display:flex; flex-direction:column; min-width:0; background:var(--bg) }+.sec { display:flex; flex-direction:column; min-height:0; background:var(--bg) }+.sec-head, .res-head { display:flex; align-items:center; gap:6px; height:40px; padding:0 8px 0 14px; flex:none;+ background:var(--chrome); border-bottom:1px solid var(--line) }+#secQs .sec-head { border-top:1px solid var(--line) }+.sec-head { container-type:inline-size }+@container (max-width: 440px) { /* Format keeps its name but not its label, so the section count still shows */+ .sec-head .fmt-t { position:absolute; width:1px; height:1px; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap }+ .sec-head .seg .ic { display:none } }+.sec-head h2, .res-head h2 { margin:0 4px 0 0; font-size:13px; font-weight:600 }+.sec-sub { color:var(--mute); font-size:12px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;+ min-width:0 }+.sec-tools { margin-left:auto; display:flex; align-items:center; gap:4px; flex:none }+.sec-body { flex:1 1 auto; min-height:0; display:flex; flex-direction:column; background:var(--bg) }+.sec-note { display:flex; gap:7px; align-items:flex-start; padding:7px 14px; font-size:12px; color:var(--ink-2);+ background:var(--blue-soft); border-bottom:1px solid var(--line); flex:none }+.sec-note.warn { background:var(--warn-soft); color:var(--ink) }+.sec-note .ic { margin-top:1px; color:var(--blue-ink) }+.sec-note.warn .ic { color:var(--warn) }+.sec-note button { font:inherit; color:var(--blue-ink); background:none; border:0; padding:0; cursor:pointer;+ text-decoration:underline; text-underline-offset:2px }+.seg { display:inline-flex; padding:2px; border-radius:6px; background:var(--sunk); gap:2px; flex:none }+.seg button { display:inline-flex; align-items:center; gap:5px; height:24px; padding:0 8px; border:0;+ border-radius:4px; background:transparent; color:var(--mute); font-size:12px; font-weight:500;+ cursor:pointer; white-space:nowrap }+.seg button:hover { color:var(--ink) }+.seg button[aria-pressed='true'] { background:var(--bg); color:var(--ink); box-shadow:0 0 0 1px var(--line-2) }+.seg .ic { width:14px; height:14px }+.probs { display:inline-flex; align-items:center; gap:5px; height:26px; padding:0 7px; border:0;+ border-radius:5px; background:transparent; color:var(--mute); font:500 12px var(--mono); cursor:pointer }+.probs:hover { background:var(--hover); color:var(--ink) }+.probs.has { color:var(--bad); background:var(--bad-soft) }+.probs .ic { width:15px; height:15px }++/* fields: the state as name/value rows */+.kv { display:grid; grid-template-columns:minmax(92px, 28%) minmax(0,1fr) 32px; border-bottom:1px solid var(--line);+ align-items:start }+.kv-k, .kv-v { border:0; background:transparent; padding:8px 10px; min-height:36px; margin:0; border-radius:0 }+.kv-k { font:12.5px/20px var(--mono); color:var(--sx-key); padding-left:14px; min-width:0 }+.kv-v { font:13px/20px var(--sans); resize:none; overflow:hidden; display:block; width:100%;+ box-shadow:inset 1px 0 var(--line) }+.kv .ibtn { margin:4px 2px 0 0 }+/* a transparent outline still shows in forced-colors mode, where box-shadow is dropped */+.kv-k:focus-visible, .kv-v:focus-visible, .qc input:focus-visible, .qc textarea:focus-visible {+ outline:2px solid transparent; outline-offset:-2px; box-shadow:inset 0 0 0 2px var(--focus);+ background:var(--curline) }+[aria-invalid='true'] { box-shadow:inset 0 -2px 0 var(--bad) !important }+[aria-invalid='true']:focus-visible { box-shadow:inset 0 0 0 2px var(--focus), inset 0 -4px 0 var(--bad) !important }+.add { align-self:flex-start; margin:8px 10px 10px; color:var(--mute) }+.add .ic { width:14px; height:14px }+textarea.grow { field-sizing:content }++/* questions form */+.qlist { display:flex; flex-direction:column; gap:10px; padding:10px; background:var(--well); flex:1 0 auto }+.qc { border:1px solid var(--line); border-radius:7px; background:var(--card); container-type:inline-size }+.qc-head { display:flex; flex-wrap:wrap; align-items:center; gap:6px 8px; padding:6px 6px 6px 8px;+ border-bottom:1px solid var(--line) }+.qc-key { flex:1 1 120px; min-width:0; height:28px; padding:0 6px; border:1px solid transparent; border-radius:4px;+ background:transparent; font:600 13px var(--mono); color:var(--ink) }+.qc-key:hover { border-color:var(--line-2) }+.qc-tools { display:flex; align-items:center; gap:0 }+.qc-tools .ibtn { width:26px; height:26px }+.qc-ins { display:block; width:100%; margin:0; padding:8px 12px; border:0; border-bottom:1px solid var(--line);+ background:transparent; resize:none; overflow:hidden; font:13px/20px var(--sans); min-height:37px }+.crit { padding:8px 12px 10px }+.crit-h { display:flex; justify-content:space-between; gap:8px; font-size:11.5px; color:var(--mute); margin:0 0 5px }+.rows { border:1px solid var(--line); border-radius:5px; overflow:hidden }+.opt { display:grid; grid-template-columns:minmax(80px, 34%) minmax(0,1fr) 28px; align-items:start;+ border-top:1px solid var(--line) }+.lvl { display:grid; grid-template-columns:30px minmax(0,1fr) auto; align-items:start;+ border-top:1px solid var(--line) }+.opt:first-child, .lvl:first-child { border-top:0 }+.opt input, .opt textarea, .lvl textarea { border:0; background:transparent; margin:0; padding:5px 8px;+ min-height:30px; font:12.5px/20px var(--sans); resize:none; overflow:hidden; width:100%; display:block }+.opt input { font-family:var(--mono); color:var(--sx-key); border-right:1px solid var(--line); align-self:stretch }+.lvl-i { font:11.5px/30px var(--mono); color:var(--mute); text-align:center; border-right:1px solid var(--line);+ align-self:stretch }+.lvl-t { display:flex; padding:2px 2px 0 0 }+.opt .ibtn, .lvl .ibtn { width:24px; height:24px; margin-top:3px }+.crit .add { margin:6px 0 0 }+.noul-h { margin:0; font-size:12px; color:var(--mute) }+.tf-l { font:12.5px/20px var(--mono); color:var(--sx-key); padding:5px 8px; border-right:1px solid var(--line);+ align-self:stretch }+.opt.tf { grid-template-columns:minmax(80px, 34%) minmax(0,1fr) }+.qc-note { margin:0; padding:0 12px 10px; font-size:12px; color:var(--mute) }+.qc-note code { font-size:11.5px }++/* code editor: a transparent textarea over a highlighted, line-numbered copy of its text */+.cx { position:relative; display:flex; flex-direction:column; flex:1 0 auto; background:var(--bg);+ font:12.5px/20px var(--mono); --lh:20px; --gw:2ch }+.cx-body { position:relative; flex:1 0 auto;+ background:linear-gradient(to right, var(--chrome) calc(var(--gw) + 18px), var(--line) 0,+ var(--line) calc(var(--gw) + 19px), transparent 0) }+.cx-hl, .cx-ta { margin:0; padding:8px 14px 28px calc(var(--gw) + 31px); font:inherit; letter-spacing:0;+ tab-size:2; white-space:pre-wrap; overflow-wrap:anywhere; word-break:normal;+ font-variant-ligatures:none; font-kerning:none; text-rendering:optimizeSpeed }+.cx-hl { counter-reset:ln; color:var(--ink); pointer-events:none; min-height:100% }+.cx-ta { position:absolute; inset:0; width:100%; height:100%; border:0; outline:0; resize:none; overflow:hidden;+ background:transparent; color:transparent; -webkit-text-fill-color:transparent; caret-color:var(--ink) }+.cx-ta::selection { background:var(--sel-a); -webkit-text-fill-color:transparent }+.cx-ta::placeholder { -webkit-text-fill-color:var(--faint); color:var(--faint) }+.cx-ta:focus-visible { outline:2px solid transparent; outline-offset:-2px }+.cx:focus-within .cx-body { box-shadow:inset 0 0 0 2px var(--focus) }+/* how to leave the editor, shown for a moment on focus; it takes no room and never catches the pointer */+.cx-hint { position:sticky; top:0; height:0; z-index:2; display:flex; justify-content:flex-end; align-items:flex-start;+ pointer-events:none }+.cx-hint span { margin:6px 8px 0 0; padding:0 7px; border:1px solid var(--line-2); border-radius:4px;+ background:var(--chrome); color:var(--mute); font:11.5px/20px var(--sans); white-space:nowrap;+ opacity:0; transition:opacity .3s ease }+.cx.hinting .cx-hint span { opacity:1 }+.ln { position:relative; min-height:var(--lh); counter-increment:ln }+.ln::before { content:counter(ln); position:absolute; left:calc(-1 * (var(--gw) + 31px)); width:calc(var(--gw) + 10px);+ text-align:right; color:var(--faint); font-size:11.5px }+.ln.cur { background:var(--curline) }+.ln.cur::before { color:var(--ink) }+.ln.bad::before { color:var(--bad); font-weight:700 }+.ln.warn::before { color:var(--warn); font-weight:700 }+.ln.bad::after, .ln.warn::after { content:""; position:absolute; top:4px; left:calc(-1 * (var(--gw) + 31px) + 3px);+ width:3px; height:12px; border-radius:2px; background:var(--bad) }+.ln.warn::after { background:var(--warn) }+.ln.flash { animation:flash 1.1s ease-out }+@keyframes flash { from { background:var(--sel) } to { background:transparent } }+.cx .k { color:var(--sx-key) } .cx .s { color:var(--sx-str) } .cx .n { color:var(--sx-num) }+.cx .l { color:var(--sx-lit); font-weight:600 } .cx .p, .cx .b { color:var(--sx-pun) } .cx .x { color:var(--bad) }+.cx .err { text-decoration:wavy underline var(--bad); text-decoration-skip-ink:none; background:var(--bad-soft) }+.cx .eol::after { content:""; display:inline-block; width:7px; height:14px; margin-left:1px; vertical-align:-2px;+ background:var(--bad-soft); border-bottom:2px solid var(--bad) }+.cx-msg { position:sticky; bottom:0; display:flex; align-items:center; gap:8px; width:100%; padding:6px 14px;+ border:0; border-top:1px solid var(--bad); background:var(--bad-soft); color:var(--ink);+ font:12px/18px var(--sans);+ text-align:left; cursor:pointer; z-index:1 }+.cx-msg .ic { color:var(--bad) }+.cx-msg b { font:600 12px var(--mono); color:var(--bad); white-space:nowrap }+.cx.ro .cx-hl { pointer-events:auto; user-select:text; padding-bottom:12px }+.cx.ro .cx-body { flex:0 0 auto }++/* run bar */+.runbar { display:flex; flex-wrap:wrap; align-items:center; gap:8px; min-height:52px; padding:8px 10px;+ background:var(--chrome); border-top:1px solid var(--line); flex:none; container-type:inline-size }+.runbar .lang { width:72px; height:28px; padding:0 8px; border:1px solid var(--line-2); border-radius:5px;+ background:var(--bg); font:12.5px var(--mono) }+.runmsg { flex:1 1 auto; margin-left:auto; font-size:12px; color:var(--mute); text-align:right; min-width:0 }+.runmsg.bad { color:var(--bad) }+.runmsg button { font:inherit; color:inherit; background:none; border:0; padding:0; cursor:pointer;+ text-decoration:underline; text-underline-offset:2px }+.runbar .right { display:flex; align-items:center; gap:6px; margin-left:auto }++/* response */+.res { container-type:inline-size }+.res-head { gap:8px }+.res-hint { color:var(--mute); font-size:12px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;+ min-width:0 }+.tabs { display:flex; align-self:stretch; margin-left:auto; flex:none }+.tab { padding:0 10px; border:0; border-bottom:2px solid transparent; background:transparent; color:var(--mute);+ font-size:12.5px; font-weight:500; cursor:pointer; margin-bottom:-1px }+.tab:hover { color:var(--ink) }+.tab[aria-selected='true'] { color:var(--ink); border-bottom-color:var(--brand) }+.progress { height:2px; flex:none; background:linear-gradient(90deg, transparent, var(--brand), transparent)+ 0 0 / 35% 100% no-repeat var(--blue-soft); animation:slide 1s linear infinite }+@keyframes slide { from { background-position:-40% 0 } to { background-position:140% 0 } }+.res-body { flex:1 1 auto; min-height:0; overflow:auto }+#viewAnswers.stale .alist { filter:grayscale(1) }+#viewAnswers.stale .fill { opacity:.55 }+.al-head { position:sticky; top:0; z-index:2 }+.pv-intro { margin:0; padding:12px 14px; color:var(--mute); font-size:12.5px; border-bottom:1px solid var(--line) }+.pv-empty { padding:28px 16px; color:var(--mute); text-align:center }+.codeview { padding:14px; display:flex; flex-direction:column; gap:14px }+.cblock { border:1px solid var(--line); border-radius:6px; overflow:hidden }+.cblock-h { display:flex; align-items:center; gap:8px; padding:4px 6px 4px 12px; background:var(--chrome);+ border-bottom:1px solid var(--line); font-size:12px; color:var(--mute) }+.cblock-h b { color:var(--ink); font-weight:600 }+.cblock-h .btn { margin-left:auto; height:24px }+.cblock pre { margin:0; padding:10px 12px; font:12px/18px var(--mono); overflow:auto; white-space:pre-wrap;+ overflow-wrap:anywhere; background:var(--bg) }+.jsonview { display:flex; flex-direction:column; min-height:100% }+.jsonview .cblock-h { border-radius:0; position:sticky; top:0; z-index:2 }+.mark circle { transform-box:fill-box; transform-origin:center }+body.busy .top .mark circle { animation:gather 1.2s ease-in-out infinite; animation-delay:calc(var(--i) * 70ms) }+@keyframes gather { 0%, 100% { opacity:.25; transform:scale(.6) } 45% { opacity:1; transform:scale(1.15) } }++/* popovers and toasts */+.pop { position:fixed; z-index:50; min-width:240px; max-width:min(460px, calc(100vw - 16px)); max-height:60vh;+ overflow:auto; padding:6px; background:var(--bg); border:1px solid var(--line-2); border-radius:8px;+ box-shadow:0 10px 30px var(--shadow) }+.pop h3 { margin:4px 8px 6px; font-size:12px; font-weight:600; color:var(--mute) }+.pop ul { list-style:none; margin:0; padding:0 }+.pitem { display:grid; grid-template-columns:16px minmax(0,1fr); gap:2px 8px; width:100%; padding:6px 8px;+ border:0; border-radius:5px; background:transparent; text-align:left; cursor:pointer; font-size:12.5px;+ color:var(--ink) }+.pitem:hover, .pitem:focus-visible { background:var(--hover) }+.pitem .ic { color:var(--bad); margin-top:2px }+.pitem small { grid-column:2; font:11.5px var(--mono); color:var(--mute); overflow-wrap:anywhere }+.pnone { display:flex; gap:8px; align-items:center; padding:8px; color:var(--ok); font-size:12.5px }+.keys { display:grid; grid-template-columns:max-content 1fr; gap:6px 14px; margin:4px 8px 8px; font-size:12.5px }+.keys dt { text-align:right } .keys dd { margin:0; color:var(--ink-2) }+.pop input { width:100%; height:30px; padding:0 8px; border:1px solid var(--line-2); border-radius:5px;+ font:12px var(--mono); background:var(--chrome) }+.toast { position:fixed; left:50%; bottom:20px; transform:translateX(-50%); z-index:60; display:flex; gap:12px;+ align-items:center; max-width:calc(100vw - 24px); padding:8px 8px 8px 14px; border-radius:7px;+ background:var(--toast); color:var(--toast-ink); font-size:12.5px; box-shadow:0 8px 24px var(--shadow) }+.toast button { height:26px; padding:0 9px; border:1px solid currentColor; border-radius:5px; background:transparent;+ color:inherit; font:600 12px var(--sans); cursor:pointer; opacity:.9 }+.toast:not(:has(button)) { padding-right:14px }+.toast .tx { width:26px; padding:0; border:0; display:grid; place-items:center }+body.pg .toast { bottom:104px } /* clear of the run bar */+body.pg[data-layout] .toast.up { top:12px; bottom:auto } /* off a focused control it would cover */++/* dividers */+.vsplit, .hsplit { display:none; position:relative; z-index:3; flex:none; background:var(--line) }+.vsplit::before, .hsplit::before { content:""; position:absolute }+.vsplit::before { inset:0 -4px }+.hsplit::before { inset:-4px 0 }+.vsplit:hover, .hsplit:hover, .vsplit.drag, .hsplit.drag, .vsplit:focus-visible, .hsplit:focus-visible {+ background:var(--brand) }+.vsplit:focus-visible, .hsplit:focus-visible { outline:1px solid transparent; box-shadow:0 0 0 1px var(--focus) }++/* noscript form */+.nojs { max-width:880px; margin:0 auto; padding:24px 16px 48px; display:grid; gap:8px }+.nojs p { margin:0 }+.nojs label { font-weight:600; margin-top:6px }+.nojs textarea, .nojs input, .nojs select { width:100%; padding:8px 10px; border:1px solid var(--line-2);+ border-radius:5px; background:var(--bg); font:12.5px/1.5 var(--mono) }+.nojs .row { display:flex; flex-wrap:wrap; gap:12px; align-items:end }+.nojs .row label { flex:1 1 180px; display:grid; gap:4px }++/* side by side on wide screens: a fixed-height workspace, each pane scrolling on its own, neither under 390px */+@media (min-width: 1100px) {+ body.pg[data-layout='split'] { height:100vh; height:100dvh; overflow:hidden }+ body.pg[data-layout='split'] .toast { bottom:calc(var(--runbar-h, 52px) + 16px) }+ [data-layout='split'] .work { display:grid; flex:1 1 0; min-height:0;+ grid-template-columns:clamp(390px, var(--split, 50%), calc(100% - 391px)) 1px minmax(0, 1fr) }+ [data-layout='split'] .pane { min-height:0; overflow:hidden }+ [data-layout='split'] .vsplit { display:block; cursor:col-resize }+ [data-layout='split'] .hsplit { display:block; height:1px; cursor:row-resize }+ [data-layout='split'] #secState { flex:0 1 auto; max-height:50%; min-height:96px }+ [data-layout='split'] .req.sized #secState { flex:0 0 calc((100% - 52px) * var(--sh)); max-height:none }+ [data-layout='split'] #secQs { flex:1 1 0 }+ [data-layout='split'] .sec-body { overflow:auto }+ [data-layout='split'] #secQs .sec-head { border-top:0 }+ [data-layout='stack'] .work { max-width:1180px; width:100%; margin:0 auto; border-inline:1px solid var(--line) } }+@media (max-width: 1099px) { #layoutBtn { display:none } }+#layoutBtn .i-rows, #layoutBtn[aria-pressed='true'] .i-cols { display:none }+#layoutBtn[aria-pressed='true'] .i-rows { display:block }+/* stacked: document flow, the run bar stays reachable while the request is on screen, and never covers focus */+html { scroll-padding-bottom:96px }+.runbar { position:sticky; bottom:0; z-index:4 }+.res { border-top:1px solid var(--line); min-height:60vh }+@media (min-width: 1100px) {+ [data-layout='split'] .runbar { position:static }+ [data-layout='split'] .res { border-top:0; min-height:0 } }+@container (max-width: 470px) {+ .runbar .kbd-help, .runbar .rv-t { display:none }+ .runbar .sel { flex:1 1 150px } .runbar .sel select { width:100% } .runmsg { flex-grow:0 } }+@container (max-width: 480px) { /* a narrow card: an option's label gets the whole row, its description goes under */+ .qc-head .seg { order:3 }+ .opt:not(.tf) { grid-template-columns:minmax(0,1fr) 28px }+ .opt:not(.tf) input { grid-column:1 / -1; border-right:0 } }+@media (max-width: 700px) {+ .top .preset { order:5; flex:1 1 60% }+ .top .preset select { width:100%; max-width:none }+ .top #share { order:6 } .top .edited { order:7 }+ .top .crumb, .top span.crumb-t { display:none }+ .top h1.crumb-t { position:absolute; width:1px; height:1px; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap }+ .btn.primary kbd { display:none }+ .res-head { flex-wrap:wrap; height:auto; padding-top:6px; row-gap:0 }+ .res-hint { order:5; flex-basis:100%; padding-bottom:6px } }+@media (forced-colors: active) {+ .seg button[aria-pressed='true'] { forced-color-adjust:none; background:Highlight; color:HighlightText }+ .vsplit, .hsplit { forced-color-adjust:none; background:CanvasText }+ .vsplit:focus-visible, .hsplit:focus-visible { background:Highlight; box-shadow:0 0 0 1px Highlight } }+"""+++_PLAYGROUND_JS = r"""+(function () {+"use strict";++/* ---------- DOM helpers: text only ever goes in through textContent / append ---------- */const $ = (sel, root) => (root || document).querySelector(sel);-const mk = (tag, cls, props) => Object.assign(- Object.assign(document.createElement(tag), props || {}), cls ? {className: cls} : {});--/* ---------- state fields ---------- */-function addField(k, v) {- const row = mk("div", "kv");- const ik = mk("input", "k", {value: k || "", placeholder: "field"});- const iv = mk("input", "v", {value: v || "", placeholder: "value"});- const rm = mk("button", "rm", {type: "button", textContent: "\u00d7", title: "remove"});- rm.onclick = () => row.remove();- row.append(ik, iv, rm);- $("#fields").append(row);-}--/* ---------- questions ---------- */-function critToText(q) {- /* v == null (missing/None) round-trips as a bare label -- textToCrit reads a line with no- ":" back as an empty description, matching the server's None. Without this, JS string- concatenation turns a real null into the literal text "null". */- if (q.type === "choice" && q.criteria)- return Object.entries(q.criteria).map(([k, v]) => v == null || v === "" ? k : k + ": " + v).join("\n");- if (q.type === "score" && q.criteria) return (q.criteria || []).join("\n");- return "";+const PROPS = new Set(["value", "checked", "disabled", "readOnly", "hidden", "open", "rows"]);+function h(tag, props, ...kids) {+ const el = document.createElement(tag);+ for (const k in props || {}) {+ const v = props[k];+ if (v == null || v === false) continue;+ if (k === "class") el.className = v;+ else if (k === "text") el.textContent = v;+ else if (typeof v === "function") el.addEventListener(k.slice(2), v);+ else if (PROPS.has(k)) el[k] = v;+ else el.setAttribute(k, v === true ? "" : String(v));+ }+ for (const c of kids.flat(Infinity)) if (c != null && c !== false) el.append(c);+ return el;}-function textToCrit(type, text) {- const lines = text.split("\n").map(x => x.trim()).filter(Boolean);- if (!lines.length) return null;- if (type === "choice") {- const o = {};- for (const l of lines) {- const i = l.indexOf(":");- if (i < 0) o[l] = ""; else o[l.slice(0, i).trim()] = l.slice(i + 1).trim();+function icon(name, cls) {+ const NS = "http://www.w3.org/2000/svg";+ const svg = document.createElementNS(NS, "svg"), use = document.createElementNS(NS, "use");+ svg.setAttribute("class", "ic" + (cls ? " " + cls : ""));+ svg.setAttribute("aria-hidden", "true");+ use.setAttribute("href", "#i-" + name);+ svg.append(use);+ return svg;+}+const iconBtn = (name, label, onclick) =>+ h("button", {type: "button", class: "ibtn", "aria-label": label, title: label, onclick}, icon(name));+const store = {+ get(k) {+ try { const v = localStorage.getItem(k); return v == null ? null : JSON.parse(v); } catch (e) { return null; }+ },+ set(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch (e) { /* storage off: still works */ } },+};+const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);+const isMap = (x) => !!x && typeof x === "object" && !Array.isArray(x);+/* defineProperty, so a "__proto__" key stays an ordinary key */+const own = (o, k, v) => Object.defineProperty(o, k, {value: v, enumerable: true, writable: true, configurable: true});+const plural = (n, w) => n + " " + w + (n === 1 ? "" : "s");+const quote = (s) => "\u201c" + s + "\u201d";+const orList = (xs) => (xs.length > 1 ? xs.slice(0, -1).join(", ") + " or " + xs[xs.length - 1] : xs.join(""));+/* an Enter that confirms an IME composition belongs to the IME */+const plainEnter = (e) => e.key === "Enter" && !e.isComposing && e.keyCode !== 229 && !e.shiftKey && !e.ctrlKey+ && !e.metaKey && !e.altKey;+const onEnter = (el, fn) => el.addEventListener("keydown", (e) => { if (plainEnter(e)) { e.preventDefault(); fn(); } });+const smooth = matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth";++/* ---------- JSON with positions: exact error locations, duplicate keys, key offsets ---------- */+function parseJSON(src) {+ let i = 0;+ const n = src.length;+ const fail = (msg, at) => { const e = new Error(msg); e.at = at == null ? i : at; throw e; };+ const ws = () => { while (i < n && " \n\t\r".includes(src[i])) i++; };+ const NUM = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y;+ const ESC = {'"': '"', "\\": "\\", "/": "/", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t"};+ function value() {+ ws();+ if (i >= n) fail(n ? "Unexpected end of JSON; expected a value" : "Empty; expected a JSON value");+ const c = src[i];+ if (c === "{") return object();+ if (c === "[") return array();+ if (c === '"') return string();+ if (c === "-" || (c >= "0" && c <= "9")) {+ NUM.lastIndex = i;+ const m = NUM.exec(src);+ if (!m) fail("Invalid number");+ const s = i;+ i += m[0].length;+ return {t: "num", v: Number(m[0]), raw: m[0], s, e: i};}+ for (const [w, v] of [["true", true], ["false", false], ["null", null]])+ if (src.startsWith(w, i)) { const s = i; i += w.length; return {t: "lit", v, s, e: i}; }+ if (c === "'") fail("Strings need double quotes, not single quotes");+ fail("Unexpected " + JSON.stringify(c) + "; expected a value");+ }+ function object() {+ const s = i++, entries = [];+ ws();+ if (src[i] === "}") { i++; return {t: "obj", entries, s, e: i}; }+ for (;;) {+ ws();+ if (src[i] !== '"')+ fail(src[i] === "}" ? "Trailing comma before '}'" : "Expected a property name in double quotes");+ const k = string();+ ws();+ if (src[i] !== ":") fail("Expected ':' after the property name");+ i++;+ entries.push({k: k.v, ks: k.s, ke: k.e, v: value()});+ ws();+ if (src[i] === ",") { i++; continue; }+ if (src[i] === "}") { i++; return {t: "obj", entries, s, e: i}; }+ fail(i >= n ? "Unexpected end of JSON; missing '}'" : "Expected ',' or '}' after the value");+ }+ }+ function array() {+ const s = i++, items = [];+ ws();+ if (src[i] === "]") { i++; return {t: "arr", items, s, e: i}; }+ for (;;) {+ ws();+ if (src[i] === "]") fail("Trailing comma before ']'");+ items.push(value());+ ws();+ if (src[i] === ",") { i++; continue; }+ if (src[i] === "]") { i++; return {t: "arr", items, s, e: i}; }+ fail(i >= n ? "Unexpected end of JSON; missing ']'" : "Expected ',' or ']' after the item");+ }+ }+ function string() {+ const s = i++;+ let out = "", run = i;+ for (;;) {+ if (i >= n) fail("Unterminated string", s);+ const c = src[i];+ if (c === '"') { out += src.slice(run, i); i++; return {t: "str", v: out, s, e: i}; }+ if (c === "\n") fail("Line break inside a string; close the quote or write \\n");+ if (c < " ") fail("Control character inside a string; escape it");+ if (c === "\\") {+ out += src.slice(run, i);+ const d = src[i + 1], hex = src.slice(i + 2, i + 6);+ if (d !== undefined && has(ESC, d)) { out += ESC[d]; i += 2; }+ else if (d === "u" && /^[0-9a-fA-F]{4}$/.test(hex)) { out += String.fromCharCode(parseInt(hex, 16)); i += 6; }+ else fail("Invalid escape sequence");+ run = i;+ continue;+ }+ i++;+ }+ }+ const ast = value();+ ws();+ if (i < n) fail("Unexpected text after the end of the JSON value");+ return ast;+}+function toValue(node) {+ if (node.t === "obj") {+ const o = {};+ for (const en of node.entries) own(o, en.k, toValue(en.v));return o;}- if (type === "score") return lines;+ return node.t === "arr" ? node.items.map(toValue) : node.v;+}+function tryParse(text) {+ try { const ast = parseJSON(text); return {ast, value: toValue(ast)}; }+ catch (e) { return {error: {msg: e.message, at: e.at}}; }+}+function lastEntry(node, key) {+ if (!node || node.t !== "obj") return null;+ for (let j = node.entries.length - 1; j >= 0; j--) if (node.entries[j].k === key) return node.entries[j];return null;}-function addQuestion(name, q) {- q = q || {type: "noul", instructions: ""};- const card = mk("div", "card qcard");- const head = mk("div", "qhead");- const nm = mk("input", "", {value: name || "", placeholder: "question name, e.g. urgency"});- const ty = mk("select");- for (const t of QTYPES) ty.append(mk("option", "", {value: t, textContent: t}));- ty.value = q.type || "noul";- const rm = mk("button", "rm", {type: "button", textContent: "\u00d7", title: "remove"});- rm.onclick = () => card.remove();- head.append(nm, ty, rm);-- const ins = mk("input", "full", {value: q.instructions || "", placeholder: "instructions"});- const crit = mk("textarea", "crit", {rows: 4, value: critToText(q)});- const hint = mk("div", "instr");- const sync = () => {- const t = ty.value;- crit.hidden = hint.hidden = (t === "noul");- crit.placeholder = hint.textContent = HINT[t];+/* Pretty-print from the tree, so duplicate keys and number spellings survive Format. */+function fmt(node, ind) {+ ind = ind || 0;+ const pad = " ".repeat(ind + 1), end = "\n" + " ".repeat(ind);+ const list = (open, close, parts) => (parts.length ? open + "\n" + parts.join(",\n") + end + close : open + close);+ if (node.t === "obj")+ return list("{", "}", node.entries.map((en) => pad + JSON.stringify(en.k) + ": " + fmt(en.v, ind + 1)));+ if (node.t === "arr") return list("[", "]", node.items.map((it) => pad + fmt(it, ind + 1)));+ return node.t === "num" ? node.raw : JSON.stringify(node.v);+}+const pretty = (v) => JSON.stringify(v, null, 2);+function lineCol(text, at) {+ const before = text.slice(0, at).split("\n");+ return {line: before.length, col: before[before.length - 1].length + 1};+}+const lineOf = (text, at) => text.slice(0, at).split("\n").length - 1;+const KINDS = {obj: "an object", arr: "a list", str: "text", num: "a number"};+const kindOf = (node) => KINDS[node.t] || (node.v === null ? "null" : "true/false");++/* ---------- highlighting: one element per line, so wrapped lines keep their number ---------- */+const TOK = /"(?:[^"\\]|\\.)*"?|[{}[\]]|[,:]|\s+|[^\s"{}[\],:]+/g; /* strings, brackets, punctuation, space, words */+function tokClass(t, rest) {+ const c = t[0];+ if (c === '"') return /^\s*:/.test(rest) ? "k" : "s";+ if (c === "-" || (c >= "0" && c <= "9")) return "n";+ if (t === "true" || t === "false" || t === "null") return "l";+ if ("{}[]".includes(c)) return "b";+ if (c === "," || c === ":") return "p";+ return /\s/.test(c) ? "" : "x";+}+const span = (text, cls) => h("span", {class: cls, text});+function paint(pre, text, marks) {+ const frag = document.createDocumentFragment();+ const lines = text.split("\n");+ const errAt = marks && marks.errAt != null ? marks.errAt : -1;+ const warn = (marks && marks.warn) || new Set();+ let off = 0;+ lines.forEach((line, li) => {+ const div = h("div", {class: "ln"});+ const end = off + line.length, errHere = errAt >= off && errAt <= end;+ if (errHere) div.classList.add("bad");+ else if (warn.has(li)) div.classList.add("warn");+ TOK.lastIndex = 0;+ for (let m; (m = TOK.exec(line));) {+ const t = m[0], cls = tokClass(t, line.slice(TOK.lastIndex)), at = off + m.index;+ if (errHere && errAt >= at && errAt < at + t.length) {+ const k = errAt - at;+ if (k) div.append(span(t.slice(0, k), cls));+ div.append(span(t.slice(k, k + 1), (cls ? cls + " " : "") + "err"));+ if (k + 1 < t.length) div.append(span(t.slice(k + 1), cls));+ } else div.append(cls ? span(t, cls) : t);+ }+ if (errHere && errAt === end) div.append(span("", "eol"));+ frag.append(div);+ off = end + 1;+ });+ pre.replaceChildren(frag);+ pre.parentNode.parentNode.style.setProperty("--gw", Math.max(2, String(lines.length).length) + "ch");+}++/* ---------- the code editor: a transparent textarea over the highlighted copy ---------- */+function insertText(ta, text) {+ ta.focus({preventScroll: true});+ let ok = false; /* execCommand keeps the browser's undo stack; setRangeText is the fallback */+ try { ok = document.execCommand("insertText", false, text); } catch (e) { ok = false; }+ if (!ok) {+ ta.setRangeText(text, ta.selectionStart, ta.selectionEnd, "end");+ ta.dispatchEvent(new Event("input", {bubbles: true}));+ }+}+function CodeEditor(opts) {+ const pre = h("pre", {class: "cx-hl", "aria-hidden": "true"});+ const msg = h("button", {type: "button", class: "cx-msg", id: "cxm-" + opts.id, hidden: true});+ const hint = h("span", {id: "cxh-" + opts.id, text: "Tab indents \u00b7 Esc, then Tab to leave"});+ const ta = h("textarea", {class: "cx-ta", id: opts.id, spellcheck: "false", autocapitalize: "off",+ autocomplete: "off", autocorrect: "off", wrap: "soft", placeholder: opts.placeholder,+ "aria-label": opts.label, "aria-describedby": hint.id + " " + msg.id});+ const root = h("div", {class: "cx"}, h("div", {class: "cx-hint"}, hint), h("div", {class: "cx-body"}, pre, ta), msg);+ const ed = {root, ta, marks: {}, cur: -1, escaped: false, painted: false};+ ed.paint = () => {+ paint(pre, ta.value, ed.marks);+ ed.painted = true;+ ed.cur = -1;+ ed.caret();+ ta.scrollTop = 0;};- ty.onchange = sync;- sync();-- card.append(head, ins, crit, hint);- card._read = () => {- const key = nm.value.trim();- if (!key) return null;- const out = {type: ty.value, instructions: ins.value.trim()};- const c = textToCrit(ty.value, crit.value);- if (c) out.criteria = c;- return [key, out];+ ed.caret = () => { /* current-line highlight */+ const li = document.activeElement === ta ? lineOf(ta.value, ta.selectionStart) : -1;+ if (li === ed.cur) return;+ if (pre.children[ed.cur]) pre.children[ed.cur].classList.remove("cur");+ if (pre.children[li]) pre.children[li].classList.add("cur");+ ed.cur = li;};- $("#questions").append(card);+ ed.get = () => ta.value;+ ed.set = (text) => { ta.value = text; ed.paint(); };+ ed.setMarks = (marks, err) => {+ ed.marks = marks;+ ed.paint();+ msg.hidden = !err;+ msg.replaceChildren();+ if (!err) return;+ const lc = lineCol(ta.value, err.at);+ msg.append(icon("warn"), h("b", {text: "Ln " + lc.line + ", Col " + lc.col}), h("span", {text: err.msg}));+ msg.onclick = () => ed.select(err.at, err.at);+ };+ ed.select = (s, e) => {+ ta.focus({preventScroll: true});+ ta.setSelectionRange(s, e);+ const row = pre.children[lineOf(ta.value, s)];+ if (row) {+ row.scrollIntoView({block: "center", behavior: smooth});+ row.classList.remove("flash");+ void row.offsetWidth;+ row.classList.add("flash");+ }+ ed.caret();+ };+ ed.format = () => {+ const r = tryParse(ta.value);+ if (r.error) { ed.select(r.error.at, r.error.at); return; }+ const next = fmt(r.ast) + "\n";+ if (next === ta.value) return;+ ta.focus();+ ta.select();+ insertText(ta, next);+ ta.setSelectionRange(0, 0);+ };+ /* The textarea is as tall as its text, so the browser's own PageDown would jump to the end: move one screen. */+ ed.page = (dir, extend) => {+ const v = ta.value, rows = pre.children, sc = ta.closest(".sec-body");+ const pane = sc && getComputedStyle(sc).overflowY !== "visible" ? sc : null;+ const dy = dir * Math.max(20, (pane ? pane.clientHeight : innerHeight) - 60);+ const back = ta.selectionDirection === "backward";+ const head = back ? ta.selectionStart : ta.selectionEnd, anchor = back ? ta.selectionEnd : ta.selectionStart;+ let li = lineOf(v, head);+ if (!rows[li]) return;+ const y = rows[li].offsetTop + dy, col = head - (v.lastIndexOf("\n", head - 1) + 1);+ const off = (j) => Math.abs(rows[j].offsetTop - y); /* the nearest line, so PageUp undoes PageDown */+ if (dir > 0) while (li < rows.length - 1 && off(li + 1) <= off(li)) li++;+ else while (li > 0 && off(li - 1) <= off(li)) li--;+ let start = 0;+ for (let k = 0; k < li; k++) start = v.indexOf("\n", start) + 1;+ const end = v.indexOf("\n", start), at = start + Math.min(col, (end < 0 ? v.length : end) - start);+ if (extend) ta.setSelectionRange(Math.min(anchor, at), Math.max(anchor, at), at < anchor ? "backward" : "forward");+ else ta.setSelectionRange(at, at);+ (pane || window).scrollBy(0, dy);+ rows[li].scrollIntoView({block: "nearest"});+ ed.caret();+ };+ let hintTimer = 0;+ ta.addEventListener("input", () => { ed.painted = false; opts.onInput(); if (!ed.painted) ed.paint(); });+ ta.addEventListener("scroll", () => { if (ta.scrollTop) ta.scrollTop = 0; });+ ta.addEventListener("focus", () => {+ ed.caret();+ root.classList.add("hinting");+ clearTimeout(hintTimer);+ hintTimer = setTimeout(() => root.classList.remove("hinting"), 4000);+ });+ ta.addEventListener("blur", () => { ed.escaped = false; root.classList.remove("hinting"); ed.caret(); });+ ta.addEventListener("pointerdown", () => { ed.escaped = false; });+ ta.addEventListener("keydown", (e) => {+ if (e.isComposing || e.keyCode === 229) return;+ if (e.key === "Shift" || e.key === "Control" || e.key === "Alt" || e.key === "Meta") return; /* Esc, Shift+Tab */+ if (e.key === "Escape") { ed.escaped = true; return; }+ if (e.key === "Tab" && !e.ctrlKey && !e.altKey && !e.metaKey) {+ if (ed.escaped) { ed.escaped = false; return; } /* Esc, then Tab: leave the editor */+ e.preventDefault();+ indent(ta, e.shiftKey);+ return;+ }+ ed.escaped = false;+ if ((e.key === "PageDown" || e.key === "PageUp") && !e.ctrlKey && !e.altKey && !e.metaKey) {+ e.preventDefault();+ ed.page(e.key === "PageDown" ? 1 : -1, e.shiftKey);+ return;+ }+ if (plainEnter(e)) { e.preventDefault(); newline(ta); }+ else if (e.altKey && e.shiftKey && e.code === "KeyF") { e.preventDefault(); ed.format(); }+ });+ return ed;+}+function indent(ta, out) {+ const v = ta.value, s = ta.selectionStart, e = ta.selectionEnd;+ if (!out && s === e) { insertText(ta, " "); return; }+ const ls = v.lastIndexOf("\n", s - 1) + 1;+ let le = v.indexOf("\n", e > s && v[e - 1] === "\n" ? e - 1 : e);+ if (le < 0) le = v.length;+ const block = v.slice(ls, le);+ let first = 0, total = 0;+ const next = block.split("\n").map((l, j) => {+ const d = out ? -(l.startsWith(" ") ? 2 : l.startsWith(" ") ? 1 : 0) : 2;+ if (j === 0) first = d;+ total += d;+ return d < 0 ? l.slice(-d) : " " + l;+ }).join("\n");+ if (next === block) return;+ ta.setSelectionRange(ls, le);+ insertText(ta, next);+ ta.setSelectionRange(Math.max(ls, s + first), Math.max(ls, e + total));+}+function newline(ta) { /* keep the indentation; one level deeper after an opening bracket */+ const v = ta.value, s = ta.selectionStart, e = ta.selectionEnd;+ const ls = v.lastIndexOf("\n", s - 1) + 1;+ const ind = /^[ \t]*/.exec(v.slice(ls, s))[0];+ const before = v.slice(ls, s).trimEnd().slice(-1);+ const open = before === "{" || before === "[";+ if (open && v[e] === (before === "{" ? "}" : "]")) {+ insertText(ta, "\n" + ind + " \n" + ind);+ const p = s + ind.length + 3;+ ta.setSelectionRange(p, p);+ } else insertText(ta, "\n" + ind + (open ? " " : ""));+}++/* ---------- boot data and state ---------- */+const BASIC = ["choice", "score", "noul"];+const FORM_TYPES = BASIC.filter((t) => QTYPES.includes(t));+const TYPES = FORM_TYPES.concat(QTYPES.filter((t) => !BASIC.includes(t)));+const MOD = /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent) ? "\u2318" : "Ctrl";+const DRAFT_KEY = "laya.playground.draft", UI_KEY = "laya.playground.ui";+const CERTAINTY_TIP = $("#certaintyTip").textContent;+const SOURCES = Object.create(null); /* no inherited keys, so ?preset=constructor is just unknown */+SOURCES.example = {label: "Billing email (example)", req: EXAMPLE};+for (const [k, p] of Object.entries(PRESETS))+ SOURCES[k] = {label: p.label, req: {state: p.state, questions: p.questions}};++const S = {+ stateMode: "fields", qMode: "form", fields: [], qs: [], base: null, last: null, running: false,+ problems: [], body: null, qValue: null, view: "answers", notes: {state: null, questions: null},+};+const work = $("#work"), reqPane = $("#req"), secState = $("#secState");+const stateBody = $("#stateBody"), qsBody = $("#qsBody"), modelSel = $("#model"), langIn = $("#lang");+const runBtn = $("#run"), revertBtn = $("#revert"), runMsg = $("#runMsg"), presetSel = $("#preset");+const badge = $("#status"), resHint = $("#resHint"), progress = $("#progress"), resBody = $("#resBody");+const viewAnswers = $("#viewAnswers"), viewJson = $("#viewJson"), viewCode = $("#viewCode");+const expandBtn = $("#expandAll");++const stateEd = CodeEditor({id: "stateJson", label: "State as JSON", onInput: () => changed(),+ placeholder: '{"field": "value"} or "plain text"'});+const qEd = CodeEditor({id: "qJson", label: "Questions as JSON", onInput: () => changed(),+ placeholder: '{"key": {"type": "noul", "instructions": "..."}}'});++/* ---------- state: fields <-> JSON ---------- */+function fieldsBlocker(ast) {+ if (ast.t === "str") return "State is plain text, so it is edited as JSON (a quoted string).";+ if (ast.t === "arr") return "State is a list, so it is edited as JSON.";+ if (ast.t !== "obj") return "State must be text, an object or a list.";+ const bad = ast.entries.find((en) => en.v.t !== "str");+ return bad ? "Field " + quote(bad.k) + " holds " + kindOf(bad.v) + ", which only JSON can edit." : "";+}+const filledFields = () => S.fields.filter((f) => f.k !== "" || f.v !== "");+const fieldsText = () => fmt({t: "obj", entries: filledFields().map((f) => ({k: f.k, v: {t: "str", v: f.v}}))});+function renderFields(focusIdx) {+ const add = h("button", {type: "button", class: "btn add", onclick: () => {+ S.fields.push({k: "", v: ""});+ renderFields(S.fields.length - 1);+ changed();+ }}, icon("plus"), "Add field");+ stateBody.replaceChildren(h("div", {class: "kv-list"}, S.fields.map(fieldRow)), add);+ stateBody.querySelectorAll("textarea.grow").forEach(grow);+ if (focusIdx != null) (S.fields[focusIdx] ? S.fields[focusIdx].el : add).focus();+}+function fieldRow(f) {+ const k = h("input", {class: "kv-k", value: f.k, placeholder: "name", "aria-label": "Field name",+ spellcheck: "false", autocomplete: "off"});+ const v = h("textarea", {class: "kv-v grow", rows: 1, value: f.v, placeholder: "value", "aria-label": "Field value"});+ k.addEventListener("input", () => { f.k = k.value; changed(); });+ v.addEventListener("input", () => { f.v = v.value; grow(v); changed(); });+ onEnter(k, () => v.focus());+ f.el = k;+ const rm = iconBtn("x", "Remove field", () => {+ const i = S.fields.indexOf(f);+ S.fields.splice(i, 1);+ renderFields(S.fields.length ? Math.min(i, S.fields.length - 1) : -1);+ changed();+ });+ return h("div", {class: "kv"}, k, v, rm);+}++/* ---------- questions: form <-> JSON ---------- */+let qSeq = 0;+function formBlocker(ast) {+ if (ast.t !== "obj") return "Questions must be a JSON object to use the form.";+ for (const en of ast.entries) {+ const name = quote(en.k), v = en.v;+ if (v.t !== "obj") return name + " is " + kindOf(v) + ", not a question object.";+ const t = lastEntry(v, "type"), ins = lastEntry(v, "instructions"), c = lastEntry(v, "criteria");+ if (!t || t.v.t !== "str" || !FORM_TYPES.includes(t.v.v))+ return name + " needs a type (" + orList(FORM_TYPES) + ") before the form can show it.";+ if (ins && ins.v.t !== "str") return "The instructions of " + name + " are not text.";+ if (!c || (c.v.t === "lit" && c.v.v === null)) continue;+ const textOrNull = (x) => x.t === "str" || (x.t === "lit" && x.v === null);+ const labels = c.v.t === "arr" && c.v.items.every((x) => x.t === "str"); /* a list of labels is fine too */+ if (t.v.v === "choice" && !(c.v.t === "obj" ? c.v.entries.every((o) => textOrNull(o.v)) : labels))+ return "The options of " + name + " are not all label: text pairs.";+ if (t.v.v === "score" && !(c.v.t === "arr" && c.v.items.every((x) => x.t === "str")))+ return "The levels of " + name + " are not all text.";+ const tf = t.v.v === "noul" && c.v.t === "obj" ? c.v.entries.map((o) => o.k) : [];+ if (t.v.v === "noul" && !(tf.length && tf.every((k) => k === "true" || k === "false") &&+ new Set(tf).size === tf.length && c.v.entries.every((o) => textOrNull(o.v))))+ return "The criteria of " + name + " are not true/false descriptions, which only JSON can edit.";+ }+ return "";+}+function qFromAst(en) {+ const v = en.v, c = lastEntry(v, "criteria"), ins = lastEntry(v, "instructions");+ const q = {id: ++qSeq, key: en.k, type: lastEntry(v, "type").v.v, ins: ins ? ins.v.v : "",+ opts: [], levels: [], tf: null, extra: []};+ const pairs = (obj) =>+ obj.entries.map((o) => ({label: o.k, desc: o.v.v == null ? "" : o.v.v, wasNull: o.v.v === null}));+ if (c && q.type === "choice" && c.v.t === "obj") q.opts = pairs(c.v);+ if (c && q.type === "choice" && c.v.t === "arr") { /* a list of labels stays a list until one gets a description */+ q.opts = c.v.items.map((x) => ({label: x.v, desc: "", wasNull: true}));+ q.optList = true;+ }+ if (c && q.type === "noul" && c.v.t === "obj") q.tf = pairs(c.v); /* what true and false mean */+ if (c && q.type === "score" && c.v.t === "arr") q.levels = c.v.items.map((x) => x.v);+ const seen = new Set(["type", "instructions", "criteria"]);+ for (const e of v.entries) if (!seen.has(e.k)) { seen.add(e.k); q.extra.push([e.k, toValue(lastEntry(v, e.k).v)]); }+ return q;+}+const filledOpts = (q) => q.opts.filter((x) => x.label !== "" || x.desc !== "");+const optValue = (x) => (x.desc === "" && x.wasNull ? null : x.desc);+function qObject(q) {+ const o = {type: q.type, instructions: q.ins};+ const opts = filledOpts(q), levels = q.levels.filter((x) => x !== "");+ const asList = q.optList && opts.every((x) => x.desc === "");+ if (q.type === "choice" && opts.length && asList) o.criteria = opts.map((x) => x.label);+ else if (q.type === "choice" && opts.length) {+ o.criteria = {};+ for (const x of opts) own(o.criteria, x.label, optValue(x));+ }+ if (q.type === "score" && levels.length) o.criteria = levels;+ if (q.type === "noul" && q.tf) {+ o.criteria = {};+ for (const x of q.tf) own(o.criteria, x.label, optValue(x));+ }+ for (const [k, v] of q.extra) if (!has(o, k)) own(o, k, v);+ return o;+}+/* Serialize through a syntax tree so repeated keys or option labels stay visible, as they would in JSON. */+function qNode(q) {+ const node = parseJSON(JSON.stringify(qObject(q)));+ const c = lastEntry(node, "criteria");+ if (c && q.type === "choice" && c.v.t === "obj") {+ const lit = (v) => (v === null ? {t: "lit", v} : {t: "str", v});+ c.v = {t: "obj", entries: filledOpts(q).map((x) => ({k: x.label, v: lit(optValue(x))}))};+ }+ return node;+}+const qText = () => fmt({t: "obj", entries: S.qs.map((q) => ({k: q.key, v: qNode(q)}))});+function uniqueKey(base) {+ const keys = new Set(S.qs.map((q) => q.key));+ let key = base;+ for (let n = 2; keys.has(key); n++) key = base + "_" + n;+ return key;+}+function renderQuestions(focus) {+ const add = h("button", {type: "button", class: "btn line add", onclick: addQuestion}, icon("plus"), "Add question");+ qsBody.replaceChildren(h("div", {class: "qlist"}, S.qs.map(qCard), add));+ qsBody.querySelectorAll("textarea.grow").forEach(grow);+ if (focus) focus();+}+function addQuestion() {+ const q = {id: ++qSeq, key: uniqueKey("new_question"), type: "noul", ins: "", opts: [], levels: [], tf: null,+ extra: []};+ S.qs.push(q);+ renderQuestions(() => { q.el.key.focus(); q.el.key.select(); q.el.card.scrollIntoView({block: "nearest"}); });+ changed();+}+function qCard(q, at) {+ const key = h("input", {class: "qc-key", value: q.key, placeholder: "key", "aria-label": "Question key",+ spellcheck: "false", autocomplete: "off"});+ const ins = h("textarea", {class: "qc-ins grow", rows: 1, value: q.ins, "aria-label": "Instructions",+ placeholder: "Instructions: what should be decided about the state?"});+ key.addEventListener("input", () => { q.key = key.value; changed(); });+ ins.addEventListener("input", () => { q.ins = ins.value; grow(ins); changed(); });+ onEnter(key, () => ins.focus());+ const segBtns = FORM_TYPES.map((t) =>+ h("button", {type: "button", "aria-pressed": String(q.type === t), onclick: () => setType(q, t)}, t));+ const move = (d) => {+ const i = S.qs.indexOf(q), j = i + d;+ S.qs.splice(i, 1);+ S.qs.splice(j, 0, q);+ renderQuestions(() => {+ const b = q.el.card.querySelector(d < 0 ? "[data-mv=up]" : "[data-mv=down]");+ (b.disabled ? q.el.key : b).focus();+ });+ changed();+ };+ const up = iconBtn("up", "Move up", () => move(-1)), down = iconBtn("down", "Move down", () => move(1));+ up.dataset.mv = "up";+ down.dataset.mv = "down";+ up.disabled = at === 0;+ down.disabled = at === S.qs.length - 1;+ const dup = iconBtn("dup", "Duplicate question", () => {+ const copy = JSON.parse(JSON.stringify({type: q.type, ins: q.ins, opts: q.opts, optList: q.optList,+ levels: q.levels, tf: q.tf, extra: q.extra}));+ Object.assign(copy, {id: ++qSeq, key: uniqueKey((q.key || "question") + "_copy")});+ S.qs.splice(S.qs.indexOf(q) + 1, 0, copy);+ renderQuestions(() => { copy.el.key.focus(); copy.el.key.select(); });+ changed();+ });+ const rm = iconBtn("x", "Remove question", () => {+ const before = snap(), i = S.qs.indexOf(q);+ S.qs.splice(i, 1);+ renderQuestions(() => { const next = S.qs[Math.min(i, S.qs.length - 1)]; if (next) next.el.key.focus(); });+ changed();+ toast("Removed " + (q.key ? quote(q.key) : "the question") + ".", "Undo", () => {+ restore(before);+ const back = S.qs[i];+ return back && back.el && back.el.key;+ });+ });+ const crit = h("div", {class: "crit"});+ const card = h("div", {class: "qc"}, h("div", {class: "qc-head"}, key,+ h("div", {class: "seg", role: "group", "aria-label": "Answer type"}, segBtns),+ h("div", {class: "qc-tools"}, up, down, dup, rm)), ins, crit);+ if (q.extra.length) {+ const names = q.extra.map(([k], j) => [j ? ", " : "", h("code", {text: k})]);+ card.append(h("p", {class: "qc-note"}, "Also sends ", names, " (edit in JSON)."));+ }+ q.el = {card, key, ins, crit, segBtns};+ renderCrit(q);+ return card;+}+function setType(q, t) {+ if (q.type === t) return;+ if (t === "score" && !q.levels.some(Boolean)) q.levels = q.opts.map((o) => o.label).filter(Boolean);+ if (t === "choice" && !q.opts.some((o) => o.label))+ q.opts = q.levels.filter(Boolean).map((l) => ({label: l, desc: ""}));+ q.type = t;+ q.el.segBtns.forEach((b) => b.setAttribute("aria-pressed", String(b.textContent === t)));+ renderCrit(q);+ changed();+}+function renderCrit(q) {+ const box = q.el.crit;+ q.el.rows = [];+ if (q.type === "noul") {+ if (!q.tf) {+ const describe = h("button", {type: "button", class: "btn add", onclick: () => {+ q.tf = [{label: "true", desc: ""}, {label: "false", desc: ""}];+ renderCrit(q);+ q.el.rows[0][0].focus();+ changed();+ }}, icon("plus"), "Describe true and false");+ const text = "A yes/no question, answered with the probability of true.";+ box.replaceChildren(h("p", {class: "noul-h", text}), describe);+ q.el.addCrit = describe;+ return;+ }+ const rows = h("div", {class: "rows"}, q.tf.map((it) => {+ const desc = h("textarea", {class: "grow", rows: 1, value: it.desc, "aria-label": "What " + it.label + " means",+ placeholder: it.label === "true" ? "yes, the statement holds" : "no, the statement does not hold"});+ desc.addEventListener("input", () => { it.desc = desc.value; grow(desc); changed(); });+ q.el.rows.push([desc]);+ return h("div", {class: "opt tf"}, h("span", {class: "tf-l", text: it.label}), desc);+ }));+ const drop = h("button", {type: "button", class: "btn add", onclick: () => {+ q.tf = null;+ renderCrit(q);+ q.el.addCrit.focus();+ changed();+ }}, icon("x"), "Remove descriptions");+ q.el.addCrit = drop;+ box.replaceChildren(h("div", {class: "crit-h"}, h("span", {text: "What true and false mean"}),+ h("span", {text: "optional"})), rows, drop);+ box.querySelectorAll("textarea.grow").forEach(grow);+ return;+ }+ const choice = q.type === "choice", items = choice ? q.opts : q.levels;+ const redraw = (focusAt) => { renderCrit(q); if (q.el.rows[focusAt]) q.el.rows[focusAt][0].focus(); changed(); };+ const insertAfter = (i) => { items.splice(i + 1, 0, choice ? {label: "", desc: ""} : ""); redraw(i + 1); };+ const rows = h("div", {class: "rows"});+ items.forEach((it, i) => {+ const remove = () => { items.splice(i, 1); redraw(Math.max(0, i - 1)); };+ if (choice) {+ const lab = h("input", {value: it.label, placeholder: "label", "aria-label": "Option label",+ spellcheck: "false", autocomplete: "off"});+ const desc = h("textarea", {class: "grow", rows: 1, value: it.desc, placeholder: "description (optional)",+ "aria-label": "Option description"});+ lab.addEventListener("input", () => { it.label = lab.value; changed(); });+ desc.addEventListener("input", () => { it.desc = desc.value; grow(desc); changed(); });+ onEnter(lab, () => desc.focus());+ onEnter(desc, () => insertAfter(i));+ q.el.rows.push([lab, desc]);+ rows.append(h("div", {class: "opt"}, lab, desc, iconBtn("x", "Remove option", remove)));+ } else {+ const txt = h("textarea", {class: "grow", rows: 1, value: it, "aria-label": "Level " + i,+ placeholder: i === 0 ? "lowest level" : "next level"});+ txt.addEventListener("input", () => { q.levels[i] = txt.value; grow(txt); changed(); });+ onEnter(txt, () => insertAfter(i));+ const swap = (d) => { [items[i], items[i + d]] = [items[i + d], items[i]]; redraw(i + d); };+ const up = iconBtn("up", "Move level up", () => swap(-1)), dn = iconBtn("down", "Move level down", () => swap(1));+ up.disabled = i === 0;+ dn.disabled = i === items.length - 1;+ q.el.rows.push([txt]);+ rows.append(h("div", {class: "lvl"}, h("span", {class: "lvl-i", text: String(i)}), txt,+ h("span", {class: "lvl-t"}, up, dn, iconBtn("x", "Remove level", remove))));+ }+ });+ q.el.addCrit = h("button", {type: "button", class: "btn add", onclick: () => insertAfter(items.length - 1)},+ icon("plus"), choice ? "Add option" : "Add level");+ const head = h("div", {class: "crit-h"}, h("span", {text: choice ? "Options" : "Levels, lowest first"}),+ h("span", {text: choice ? "label \u2192 description" : "index \u2192 level"}));+ box.replaceChildren(head, ...(items.length ? [rows] : []), q.el.addCrit);+ box.querySelectorAll("textarea.grow").forEach(grow);+}++/* ---------- auto-growing textareas (CSS field-sizing where supported) ---------- */+const FIELD_SIZING = window.CSS && CSS.supports && CSS.supports("field-sizing", "content");+function grow(ta) {+ if (FIELD_SIZING || !ta.isConnected) return;+ const sc = ta.closest(".sec-body"), keep = sc ? sc.scrollTop : 0;+ ta.style.height = "auto";+ ta.style.height = ta.scrollHeight + "px";+ if (sc) sc.scrollTop = keep;+}+if (!FIELD_SIZING && window.ResizeObserver)+ new ResizeObserver(() => document.querySelectorAll("textarea.grow").forEach(grow)).observe(reqPane);++/* ---------- modes ---------- */+function setNote(sec, text, kind, action, cause) {+ S.notes[sec] = text ? {text, kind, cause} : null;+ const el = sec === "state" ? $("#stateNote") : $("#qNote");+ el.hidden = !text;+ el.className = "sec-note" + (kind === "warn" ? " warn" : "");+ el.replaceChildren();+ if (!text) return;+ const btn = action && h("button", {type: "button", onclick: action.run, text: action.label});+ el.append(icon(kind === "warn" ? "warn" : "info"), h("span", null, text, btn ? [" ", btn] : null));+}+function syncModeButtons() {+ for (const b of document.querySelectorAll("[data-mode]")) {+ const [sec, mode] = b.dataset.mode.split(":");+ b.setAttribute("aria-pressed", String((sec === "state" ? S.stateMode : S.qMode) === mode));+ }+ $("#fmtState").hidden = S.stateMode !== "json";+ $("#fmtQs").hidden = S.qMode !== "json";+}+function mount() {+ if (S.stateMode === "json") { stateBody.replaceChildren(stateEd.root); stateEd.paint(); } else renderFields();+ if (S.qMode === "json") { qsBody.replaceChildren(qEd.root); qEd.paint(); } else renderQuestions();+ syncModeButtons();+}+function parseNote(sec, ed, err) {+ const lc = lineCol(ed.get(), err.at);+ setNote(sec, "The JSON has an error at line " + lc.line + ", column " + lc.col + ": fix it before switching.",+ "warn", {label: "Show me", run: () => ed.select(err.at, err.at)}, "parse");+}+/* Leaving JSON mode needs JSON the form can show; otherwise say why and keep every character typed. */+function setMode(sec, mode) {+ const isState = sec === "state", ed = isState ? stateEd : qEd;+ if ((isState ? S.stateMode : S.qMode) === mode) return;+ if (mode === "json") ed.set((isState ? fieldsText() : qText()) + "\n");+ else {+ const r = tryParse(ed.get());+ if (r.error) { parseNote(sec, ed, r.error); return; }+ const why = isState ? fieldsBlocker(r.ast) : formBlocker(r.ast);+ if (why) { setNote(sec, why, "warn", null, "shape"); return; }+ if (isState) S.fields = r.ast.entries.map((en) => ({k: en.k, v: en.v.v}));+ else S.qs = r.ast.entries.map(qFromAst);+ }+ if (isState) S.stateMode = mode; else S.qMode = mode;+ store.set(UI_KEY, Object.assign(store.get(UI_KEY) || {}, isState ? {stateMode: mode} : {qMode: mode}));+ setNote(sec, null);+ mount();+ changed();+}++/* ---------- analysis: one pass gives the request body and every problem, with its location ---------- */+const edLoc = (ed, s, e) => ({ed, at: [s, e == null ? s : e]});+function parseProblem(sec, ed, err) {+ const where = sec + ", line " + lineCol(ed.get(), err.at).line;+ return {sec, msg: err.msg, loc: edLoc(ed, err.at), where, parse: err};+}+function analyzeState() {+ const P = [], add = (msg, loc) => P.push({sec: "state", msg, loc, where: "state"});+ const twice = (k) => "Field " + quote(k) + " appears twice; only the last one is sent";+ if (S.stateMode === "fields") {+ const seen = new Set(), value = {}, rows = filledFields();+ if (!rows.length) add("State is empty; add a field", {el: S.fields[0] ? S.fields[0].el : $(".add", stateBody)});+ rows.forEach((f, i) => {+ if (!f.k.trim()) add("Field " + (i + 1) + " has no name", {el: f.el});+ else if (seen.has(f.k)) add(twice(f.k), {el: f.el});+ seen.add(f.k);+ own(value, f.k, f.v);+ });+ return {problems: P, ok: true, value, blocker: ""};+ }+ const r = tryParse(stateEd.get());+ if (r.error) return {problems: [parseProblem("state", stateEd, r.error)], ok: false};+ const a = r.ast, whole = edLoc(stateEd, a.s, a.e);+ if (a.t === "lit" || a.t === "num") add("State must be text, an object or a list, not " + kindOf(a), whole);+ else if (!(a.t === "str" ? a.v : (a.entries || a.items).length)) add("State is empty", whole);+ const seen = new Set();+ for (const en of a.t === "obj" ? a.entries : []) {+ if (seen.has(en.k)) add(twice(en.k), edLoc(stateEd, en.ks, en.ke));+ seen.add(en.k);+ }+ return {problems: P, ok: true, value: r.value, blocker: fieldsBlocker(a)};+}+/* Both editors reduce a question to the same record, so one checker serves the form and the JSON. */+function recordsFromForm() {+ return S.qs.map((q) => {+ const choice = q.type === "choice", el = q.el || {rows: []};+ const items = choice ? q.opts.map((o) => [o.label, o.label === "" && o.desc === ""])+ : q.levels.map((l) => [l, l === ""]);+ const filled = items.map(([text, empty], j) => ({text, empty, loc: {el: el.rows[j] && el.rows[j][0]}}))+ .filter((l) => !l.empty);+ return {key: q.key, keyLoc: {el: el.key}, isObj: true, type: q.type, typeLoc: {el: el.key}, ins: q.ins,+ insLoc: {el: el.ins}, crit: {kind: q.type === "noul" || !filled.length ? "none" : choice ? "obj" : "arr",+ labels: filled, loc: {el: el.addCrit}}};+ });+}+function recordsFromAst(a) {+ return a.entries.map((en) => {+ const v = en.v;+ const rec = {key: en.k, keyLoc: edLoc(qEd, en.ks, en.ke), isObj: v.t === "obj", qLoc: edLoc(qEd, v.s, v.e)};+ if (!rec.isObj) return rec;+ const t = lastEntry(v, "type"), ins = lastEntry(v, "instructions"), c = lastEntry(v, "criteria");+ rec.type = t ? (t.v.t === "str" ? t.v.v : fmt(t.v)) : null;+ rec.typeLoc = t ? edLoc(qEd, t.v.s, t.v.e) : rec.keyLoc;+ rec.ins = ins && ins.v.t === "str" ? ins.v.v : "";+ rec.insLoc = ins ? edLoc(qEd, ins.v.s, ins.v.e) : rec.keyLoc;+ const cv = c && c.v, loc = cv ? edLoc(qEd, cv.s, cv.e) : rec.keyLoc;+ if (!cv || (cv.t === "lit" && cv.v === null)) rec.crit = {kind: "none", loc};+ else if (cv.t === "obj")+ rec.crit = {kind: cv.entries.length ? "obj" : "none", loc,+ labels: cv.entries.map((o) => ({text: o.k, loc: edLoc(qEd, o.ks, o.ke)}))};+ else if (cv.t === "arr")+ rec.crit = {kind: cv.items.length ? "arr" : "none", loc,+ labels: cv.items.map((x) => ({text: x.t === "str" ? x.v : null, loc: edLoc(qEd, x.s, x.e)}))};+ else rec.crit = {kind: "other", loc};+ return rec;+ });+}+function checkQuestions(recs, rootLoc) {+ const P = [], add = (msg, loc, where) => P.push({sec: "questions", msg, loc, where: where || "questions"});+ if (!recs.length) add("Add at least one question", rootLoc);+ const count = new Set(recs.map((r) => r.key)).size, over = recs[LIMITS.questions];+ if (count > LIMITS.questions)+ add(count + " questions is over the server's limit of " + LIMITS.questions + "; split the request",+ over ? over.keyLoc : rootLoc);+ const seen = new Set(), types = orList(TYPES);+ for (const r of recs) {+ const name = r.key.trim() ? quote(r.key) : "A question";+ const where = "questions \u2192 " + (r.key || "\u2205");+ const tw = where + " \u2192 type", cw = where + " \u2192 criteria";+ const c = r.crit || {kind: "none"};+ if (!r.key.trim()) add("A question has an empty key", r.keyLoc, where);+ else if (seen.has(r.key)) add("Key " + name + " is used twice; only the last one is sent", r.keyLoc, where);+ seen.add(r.key);+ if (!r.isObj) { add(name + " must be an object with a type and instructions", r.qLoc, where); continue; }+ if (r.type == null) add(name + " has no type; use " + types, r.typeLoc, tw);+ else if (!QTYPES.includes(r.type))+ add(name + " has an unknown type " + quote(r.type) + "; use " + types, r.typeLoc, tw);+ if (!r.ins.trim()) add(name + " has no instructions", r.insLoc, where + " \u2192 instructions");+ /* laya takes a choice's options as label -> description, or as a list of labels */+ const choice = r.type === "choice", want = choice ? ["obj", "arr"] : r.type === "score" ? ["arr"] : null;+ if (!want) continue;+ if (c.kind === "none") add(name + " is a " + r.type + " with no " + (choice ? "options" : "levels"), c.loc, cw);+ else if (!want.includes(c.kind))+ add(choice ? "The options of " + name + " must be an object of label \u2192 description, or a list of labels"+ : "The levels of " + name + " must be a list, lowest first", c.loc, cw);+ else {+ const labels = new Set();+ for (const l of c.labels) {+ if (l.text === null) { if (choice) add(name + " has an option that is not text", l.loc, cw); continue; }+ if (!l.text.trim()) add(name + (choice ? " has an option with no label" : " has an empty level"), l.loc, cw);+ else if (choice && labels.has(l.text)) add(name + " lists the option " + quote(l.text) + " twice", l.loc, cw);+ labels.add(l.text);+ }+ }+ }+ return P;+}+function analyzeQuestions() {+ if (S.qMode === "form") {+ const value = {};+ for (const q of S.qs) own(value, q.key, qObject(q));+ const problems = checkQuestions(recordsFromForm(), {el: $(".qlist > .add", qsBody)});+ return {problems, ok: true, value, blocker: ""};+ }+ const r = tryParse(qEd.get());+ if (r.error) return {ok: false, problems: [parseProblem("questions", qEd, r.error)]};+ const whole = edLoc(qEd, r.ast.s, r.ast.e);+ const msg = "Questions must be an object that maps each key to a question";+ const problems = r.ast.t === "obj" ? checkQuestions(recordsFromAst(r.ast), whole)+ : [{sec: "questions", msg, loc: whole, where: "questions"}];+ return {ok: true, value: r.value, blocker: formBlocker(r.ast), problems};+}+function markEditor(problems, ed) {+ const warn = new Set(), parse = problems.find((p) => p.parse);+ for (const p of problems) if (!p.parse && p.loc && p.loc.ed === ed) warn.add(lineOf(ed.get(), p.loc.at[0]));+ ed.setMarks({errAt: parse ? parse.parse.at : null, warn}, parse && parse.parse);+}+/* In JSON mode, say quietly why the form can't show this JSON; a refusal note stays, kept up to date, while a+ cause of its kind does. */+function syncBlocker(sec, res, ed) {+ const note = S.notes[sec], warn = note && note.kind === "warn";+ if (warn && note.cause === "parse" && !res.ok) { parseNote(sec, ed, res.problems[0].parse); return; }+ if (warn && note.cause === "shape" && res.ok && res.blocker) {+ if (note.text !== res.blocker) setNote(sec, res.blocker, "warn", null, "shape");+ return;+ }+ if (res.ok && res.blocker) { if (!note || note.text !== res.blocker) setNote(sec, res.blocker, "info"); }+ else if (note) setNote(sec, null);}-/* ---------- builder <-> payload ---------- */-function collect() {- const state = {};- for (const row of document.querySelectorAll("#fields .kv")) {- const k = $(".k", row).value.trim();- if (k) state[k] = $(".v", row).value;+/* ---------- refresh after every edit ---------- */+let saveTimer = 0, lastPreview = "";+function changed() {+ const st = analyzeState(), qs = analyzeQuestions();+ S.problems = st.problems.concat(qs.problems);+ S.body = st.ok && qs.ok ? body(st.value, qs.value) : null;+ if (qs.ok) S.qValue = qs.value;+ if (S.stateMode === "json") { markEditor(st.problems, stateEd); syncBlocker("state", st, stateEd); }+ if (S.qMode === "json") { markEditor(qs.problems, qEd); syncBlocker("questions", qs, qEd); }+ for (const el of reqPane.querySelectorAll("[aria-invalid]")) el.removeAttribute("aria-invalid");+ for (const p of S.problems)+ if (p.loc && p.loc.el && p.loc.el.matches("input, textarea")) p.loc.el.setAttribute("aria-invalid", "true");+ counters(st, qs);+ const modified = !S.base || canon(S.body) !== canon(S.base.body);+ $("#edited").hidden = !modified;+ revertBtn.disabled = !modified;+ if (!S.running) { runMsg.className = "runmsg"; runMsg.replaceChildren(); }+ if (toastBody != null && canon(S.body) !== toastBody) hideToast(); /* an undo is only offered until the next edit */+ status();+ const pv = S.qValue ? JSON.stringify(S.qValue) : "";+ if (pv !== lastPreview && !S.last) { lastPreview = pv; renderPreview(); }+ if (S.view === "code") renderCode();+ clearTimeout(saveTimer);+ saveTimer = setTimeout(saveDraft, 250);+}+function body(state, questions) {+ const b = {state, questions}, model = modelSel.value, lang = langIn.value.trim();+ if (model) b.model = model;+ if (lang) b.lang = lang;+ return b;+}+/* Key order inside a question is not meaningful, nor is "criteria": null (the server's default); everything else+ (question order, option order) is. */+function canon(b) {+ if (!b) return "";+ const kept = (q) => Object.keys(q).filter((k) => !(k === "criteria" && q[k] === null));+ const sorted = (q) => (isMap(q) ? kept(q).sort().map((k) => [k, q[k]]) : q);+ const qs = isMap(b.questions) ? Object.entries(b.questions).map(([k, q]) => [k, sorted(q)]) : b.questions;+ return JSON.stringify([b.state, qs, b.model || "", b.lang || ""]);+}+function counters(st, qs) {+ const rows = [["#probState", st.problems.length, "State"], ["#probQs", qs.problems.length, "Questions"]];+ for (const [id, n, sec] of rows) {+ const b = $(id);+ b.classList.toggle("has", n > 0);+ b.querySelector("span").textContent = String(n);+ b.setAttribute("aria-label", sec + ": " + plural(n, "problem"));+ b.title = n ? plural(n, "problem") + " (F8 jumps to the next one)" : "No problems";}- const questions = {};- for (const card of document.querySelectorAll(".qcard")) {- const kv = card._read();- if (kv) questions[kv[0]] = kv[1];+ $("#probState").hidden = !st.problems.length;+ const v = st.value;+ $("#stateSub").textContent = S.stateMode === "fields" ? plural(filledFields().length, "field")+ : !st.ok ? "invalid JSON" : typeof v === "string" ? "text" : Array.isArray(v) ? "list" : "object";+ const qn = S.qMode === "form" ? S.qs.length : qs.ok && isMap(qs.value) ? Object.keys(qs.value).length : null;+ $("#qSub").textContent = qn != null ? plural(qn, "question") : qs.ok ? "not an object" : "invalid JSON";+}+function jump(p) {+ closePop();+ if (!p || !p.loc) return;+ if (p.loc.ed) { p.loc.ed.select(p.loc.at[0], p.loc.at[1]); return; }+ const el = p.loc.el;+ if (!el || !el.isConnected) return;+ el.focus({preventScroll: true});+ el.scrollIntoView({block: "center", behavior: smooth});+}+let probCursor = -1;+function nextProblem(back) {+ if (!S.problems.length) { toast("No problems in the request."); return; }+ probCursor = (probCursor + (back ? -1 : 1) + S.problems.length) % S.problems.length;+ jump(S.problems[probCursor]);+}++/* ---------- popovers & toasts ---------- */+/* A popover sits at the end of <body> (it is position:fixed), so Tab out of it goes back to where it opened from. */+let pop = null;+function closePop(restoreFocus) {+ if (!pop) return;+ const {el, anchor, back} = pop;+ pop = null;+ el.remove();+ anchor.setAttribute("aria-expanded", "false");+ document.removeEventListener("pointerdown", outside, true);+ document.removeEventListener("focusin", outsideFocus, true);+ if (restoreFocus) (back && back.isConnected && back !== document.body ? back : anchor).focus();+}+function outside(e) { if (pop && !pop.el.contains(e.target) && !pop.anchor.contains(e.target)) closePop(); }+function outsideFocus(e) { if (pop && !pop.el.contains(e.target) && e.target !== pop.anchor) closePop(); }+function openPop(anchor, content, label) {+ if (pop && pop.anchor === anchor) { closePop(true); return; }+ closePop();+ const back = document.activeElement;+ const el = h("div", {class: "pop", role: "dialog", "aria-label": label}, content);+ document.body.append(el);+ const r = anchor.getBoundingClientRect(), w = el.offsetWidth, ht = el.offsetHeight;+ const top = r.bottom + 6 + ht > innerHeight - 8 && r.top - ht - 6 > 8 ? r.top - ht - 6 : r.bottom + 6;+ el.style.top = Math.max(8, top) + "px";+ el.style.left = Math.min(Math.max(8, r.right - w), innerWidth - w - 8) + "px";+ anchor.setAttribute("aria-expanded", "true");+ pop = {el, anchor, back};+ el.addEventListener("keydown", (e) => { /* Tab past either end: close, then carry on from the trigger */+ const f = el.querySelectorAll("button, input");+ if (e.key !== "Tab" || !f.length || document.activeElement !== f[e.shiftKey ? 0 : f.length - 1]) return;+ closePop(false);+ anchor.focus();+ if (e.shiftKey) e.preventDefault();+ });+ document.addEventListener("pointerdown", outside, true);+ document.addEventListener("focusin", outsideFocus, true);+ const first = el.querySelector("button, input");+ if (first) first.focus();+}+function openProblems(sec, anchor) {+ const list = S.problems.filter((p) => !sec || p.sec === sec);+ const title = list.length ? plural(list.length, "problem") + " in " + (sec || "the request") : "No problems";+ const items = list.map((p) => h("li", null, h("button", {type: "button", class: "pitem", onclick: () => jump(p)},+ icon("warn"), h("span", {text: p.msg}), h("small", {text: p.where}))));+ const ready = "Nothing to fix here; the " + (sec || "request") + (sec === "questions" ? " are" : " is")+ + " ready to run.";+ const none = h("div", {class: "pnone"}, icon("check"), ready);+ openPop(anchor, [h("h3", {text: title}), list.length ? h("ul", null, items) : none], "Problems");+}+/* One live region, always rendered, speaks for the toast and the run status; clearing it first lets a repeated+ message be heard again. */+let announceTimer = 0;+function announce(text) {+ const el = $("#announce");+ clearTimeout(announceTimer);+ el.textContent = "";+ announceTimer = setTimeout(() => { el.textContent = text; }, 80);+}+/* A toast with an action (Undo) has no time limit: it stays until it is used or dismissed, the request changes+ again, or another toast replaces it. Focus inside it goes back where it came from when it closes. */+let toastTimer = 0, toastBack = null, toastBody = null;+function hideToast(focusTo) {+ const el = $("#toast"), inside = el.contains(document.activeElement);+ clearTimeout(toastTimer);+ toastBody = null;+ el.hidden = true;+ el.classList.remove("up");+ if (!inside) return;+ for (const x of [focusTo, toastBack, runBtn]) { /* the first one that can take focus: Revert may be disabled */+ if (!x || !x.isConnected || x === document.body || el.contains(x)) continue;+ x.focus();+ if (document.activeElement === x) return;+ }+}+function toast(text, actionLabel, action) {+ const el = $("#toast"), active = document.activeElement;+ if (active && active !== document.body && !el.contains(active)) toastBack = active;+ el.replaceChildren(h("span", {text}));+ el.classList.remove("up");+ clearTimeout(toastTimer);+ if (actionLabel) {+ const act = () => { toastBody = null; hideToast(action()); };+ el.append(h("button", {type: "button", text: actionLabel, onclick: act}),+ h("button", {type: "button", class: "tx", "aria-label": "Dismiss", title: "Dismiss", onclick: () => hideToast()},+ icon("x")));+ toastBody = canon(S.body);+ } else {+ toastBody = null;+ const arm = () => { toastTimer = setTimeout(() => (el.matches(":hover") ? arm() : hideToast()), 3200); };+ arm();}- const payload = {state, questions};- const m = $("#model").value;- if (m) payload.model = m;- return payload;-}-function fill(p) {- $("#fields").innerHTML = "";- $("#questions").innerHTML = "";- const st = p.state;- if (typeof st === "string") addField("body", st);- else for (const [k, v] of Object.entries(st || {})) addField(k, typeof v === "string" ? v : JSON.stringify(v));- for (const [k, q] of Object.entries(p.questions || {})) addQuestion(k, q);- const forced = new URLSearchParams(location.search).get("model");- if (forced || p.model) $("#model").value = forced || p.model;-}--/* ---------- tabs ---------- */-function show(tab, err, skipSync) {- const cur = document.body.dataset.tab; /* undefined on first paint */- if (!skipSync) {- if (cur === "build" && tab === "raw") $("#raw").value = JSON.stringify(collect(), null, 2);- if (cur === "raw" && tab === "build") {- try { fill(JSON.parse($("#raw").value)); }- catch (e) { $("#err").textContent = "Raw JSON is not valid: " + e.message; return; }+ el.hidden = false;+ announce(text);+ requestAnimationFrame(clearOfToast);+}+/* A toast can stay up while the user tabs on, so whatever has keyboard focus is scrolled clear of it; when nothing+ can scroll that far, the toast moves to the top of the window. */+function clearOfToast() {+ const el = $("#toast"), t = document.activeElement;+ if (el.hidden || !t || t === document.body || el.contains(t) || !t.matches(":focus-visible")) return;+ const under = () => {+ const a = t.getBoundingClientRect(), b = el.getBoundingClientRect();+ return a.right > b.left && a.left < b.right && a.bottom > b.top && a.top < b.bottom ? a.bottom - b.top + 8 : 0;+ };+ if (!under() || t.getBoundingClientRect().height > innerHeight / 2) return; /* an editor never fits clear */+ el.classList.remove("up");+ const was = [];+ for (let sc = t.parentElement, d = under(); d && sc; sc = sc.parentElement, d = under())+ if (sc === document.documentElement || /auto|scroll/.test(getComputedStyle(sc).overflowY)) {+ was.push([sc, sc.scrollTop]);+ sc.scrollTop += d;}+ if (!under()) return;+ for (const [sc, top] of was) sc.scrollTop = top; /* scrolling did not help: leave the page where it was */+ el.classList.add("up");+}+document.addEventListener("focusin", () => requestAnimationFrame(clearOfToast));+if (window.ResizeObserver) /* the side-by-side toast sits clear of the run bar, one row or two */+ new ResizeObserver(() => document.body.style.setProperty("--runbar-h", $(".runbar").offsetHeight + "px"))+ .observe($(".runbar"));++/* ---------- loading requests: presets, drafts, share links ---------- */+function snap() {+ return {+ stateMode: S.stateMode, qMode: S.qMode, model: modelSel.value, lang: langIn.value,+ stateText: S.stateMode === "json" ? stateEd.get() : fieldsText(), qText: S.qMode === "json" ? qEd.get() : qText(),+ };+}+function restore(sn) {+ const st = tryParse(sn.stateText || ""), qp = tryParse(sn.qText || "");+ S.stateMode = sn.stateMode !== "json" && !st.error && !fieldsBlocker(st.ast) ? "fields" : "json";+ S.qMode = sn.qMode !== "json" && !qp.error && !formBlocker(qp.ast) ? "form" : "json";+ if (S.stateMode === "fields") S.fields = st.ast.entries.map((en) => ({k: en.k, v: en.v.v}));+ else stateEd.set(sn.stateText || "");+ if (S.qMode === "form") S.qs = qp.ast.entries.map(qFromAst);+ else qEd.set(sn.qText || "");+ setModel(sn.model || "");+ langIn.value = sn.lang || "";+ setNote("state", null);+ setNote("questions", null);+ mount();+ changed();+}+function snapFromReq(req) {+ const text = (v) => pretty(v === undefined ? {} : v) + "\n", pref = store.get(UI_KEY) || {};+ return {stateMode: pref.stateMode || "fields", qMode: pref.qMode || "json", /* the last editors picked */+ stateText: text(req.state), qText: text(req.questions),+ model: typeof req.model === "string" ? req.model : "", lang: typeof req.lang === "string" ? req.lang : ""};+}+function setModel(m) {+ if (m && !Array.from(modelSel.options).some((o) => o.value === m))+ modelSel.append(h("option", {value: m}, m + " (unknown)"));+ modelSel.value = m;+}+function setBase(name, label, sn) {+ S.base = {name, label, snap: sn};+ restore(sn);+ S.base.body = S.body;+ if (!Array.from(presetSel.options).some((o) => o.value === name)) presetSel.append(h("option", {value: name}, label));+ presetSel.value = name;+ changed();+}+function loadReq(name, label, req, withUndo) {+ const prev = snap(), prevBase = S.base, dirty = S.base && canon(S.body) !== canon(S.base.body);+ setBase(name, label, snapFromReq(req));+ clearResult();+ if (name === "shared") SOURCES.shared = {label, req}; /* the picker can load it again */+ if (withUndo && dirty)+ toast("Loaded " + quote(label) + ".", "Undo", () => {+ S.base = prevBase;+ presetSel.value = prevBase.name;+ restore(prev);+ });+}+function loadSource(name, withUndo) {+ const src = SOURCES[name];+ if (src) loadReq(name, src.label, src.req, withUndo);+}+/* #r=<base64url JSON> is a shared request: undefined when there is none, null when it can't be read */+function readHash() {+ const m = /^#r=(.*)$/.exec(location.hash);+ if (!m) return undefined;+ history.replaceState(null, "", location.pathname + location.search); /* a reload now keeps later edits */+ try { /* "=" padding (as Python's urlsafe_b64encode writes it) is optional */+ const req = JSON.parse(b64urlDecode(decodeURIComponent(m[1]).replace(/=+$/, "")));+ return isMap(req) && "state" in req && "questions" in req ? req : null;+ } catch (e) {+ return null;}- $("#pane-build").hidden = tab !== "build";- $("#pane-raw").hidden = tab !== "raw";- for (const b of document.querySelectorAll(".tab")) b.classList.toggle("on", b.dataset.t === tab);- $("#err").textContent = err || "";- document.body.dataset.tab = tab;-}--/* ---------- submit ---------- */-function send() {- let p;- if (document.body.dataset.tab === "raw") {- try { p = JSON.parse($("#raw").value); }- catch (e) { $("#err").textContent = "Raw JSON is not valid: " + e.message; return; }- } else p = collect();-- const emptyState = !p.state || (typeof p.state === "object" && !Object.keys(p.state).length);- if (emptyState) { $("#err").textContent = "Add at least one state field."; return; }- if (!p.questions || !Object.keys(p.questions).length) {- $("#err").textContent = "Add at least one question."; return;+}+function saveDraft() {+ const base = S.base && {name: S.base.name, label: S.base.label, snap: S.base.snap};+ store.set(DRAFT_KEY, {v: 1, snap: snap(), base});+}+function b64urlEncode(str) {+ const bytes = new TextEncoder().encode(str);+ let bin = "";+ for (let j = 0; j < bytes.length; j += 0x8000) bin += String.fromCharCode.apply(null, bytes.subarray(j, j + 0x8000));+ return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");+}+function b64urlDecode(s) {+ const bin = atob(s.replace(/-/g, "+").replace(/_/g, "/") + "===".slice((s.length + 3) % 4));+ return new TextDecoder("utf-8", {fatal: true}).decode(Uint8Array.from(bin, (c) => c.charCodeAt(0)));+}+function shareLink(e) {+ if (!S.body) { toast("Fix the JSON error before copying a link."); return; }+ const url = location.origin + location.pathname + "#r=" + b64urlEncode(JSON.stringify(S.body)), btn = e.currentTarget;+ window.layaCopy(url, null).then((ok) => {+ const kb = Math.max(1, Math.round(url.length / 1024));+ if (ok) { toast("Link copied. It carries the whole request (" + kb + " KB)."); return; }+ const input = h("input", {value: url, readOnly: true, "aria-label": "Share link"});+ openPop(btn, [h("h3", {text: "Copy this link"}), input], "Share link");+ input.select();+ });+}++/* ---------- running ---------- */+const fmtMs = (ms) => (ms < 1000 ? Math.round(ms) + " ms" : (ms / 1000).toFixed(2) + " s");+let spokenRun = null, spokenStale = null;+function status() {+ const L = S.last, count = (o, w) => plural(Object.keys(o || {}).length, w);+ const same = L && S.body && canon(S.body) === L.canon;+ let s = "preview", hint = S.qValue ? "Run the request to answer " + count(S.qValue, "question") : "";+ if (S.running) [s, hint] = ["running", "Waiting for the server\u2026"];+ else if (L && !same) [s, hint] = ["stale", "The request changed since this run"];+ else if (L && L.ok) [s, hint] = ["ok", count(L.data.answers, "answer") + " in " + fmtMs(L.ms) + " round trip"];+ else if (L) [s, hint] = ["error", L.net ? "Network error" : L.odd ? "Unexpected response" : "HTTP " + L.status];+ badge.dataset.s = s;+ badge.textContent = {preview: "Preview", running: "Running", ok: "OK", error: "Error", stale: "Stale"}[s];+ resHint.textContent = hint;+ viewAnswers.classList.toggle("stale", s === "stale");+ /* Each run is announced once, and so is the first edit after it; an edit undone back to the run is silent. */+ if ((s === "ok" || s === "error") && spokenRun !== L) {+ spokenRun = L; /* errors in the Answers view speak for themselves (role=alert) */+ if (s === "ok") announce("Run finished: " + hint + ".");+ else if (S.view !== "answers") announce("The run failed: " + hint + ".");+ } else if (s === "stale" && spokenStale !== L) {+ spokenStale = L;+ announce("The request changed since the last run.");+ }+}+function busy(on) {+ S.running = on;+ runBtn.disabled = on;+ $(".run-t", runBtn).textContent = on ? "Running\u2026" : "Run request";+ document.body.classList.toggle("busy", on);+ progress.hidden = !on;+ if (on) resBody.setAttribute("aria-busy", "true"); else resBody.removeAttribute("aria-busy");+ status();+}+async function run() {+ if (S.running) return;+ changed();+ if (S.problems.length) { /* blocked: say so, and put the caret on the first problem */+ runMsg.className = "runmsg bad";+ const text = "Fix " + plural(S.problems.length, "problem") + " to run";+ runMsg.replaceChildren(h("button", {type: "button", "aria-haspopup": "dialog", "aria-expanded": "false", text,+ onclick: (e) => openProblems(null, e.currentTarget)}));+ probCursor = 0;+ jump(S.problems[0]);+ return;+ }+ const last = {sent: JSON.stringify(S.body), canon: canon(S.body), body: S.body, ok: false}, t0 = performance.now();+ busy(true);+ try {+ const res = await fetch("/predict", {method: "POST", body: last.sent,+ headers: {"content-type": "application/json", accept: "application/json"}});+ last.status = res.status;+ last.text = await res.text();+ try { last.data = JSON.parse(last.text); } catch (e) { last.data = null; }+ last.ok = res.ok && isMap(last.data) && isMap(last.data.answers);+ last.odd = res.ok && !last.ok; /* a 2xx that is not a laya response */+ } catch (e) {+ last.net = e;+ }+ last.ms = performance.now() - t0;+ S.last = last;+ busy(false);+ renderAnswersView();+ if (S.view === "json") renderJson();+ if (document.body.dataset.layout !== "split" || innerWidth < 1100) /* stacked: bring the answers into view */+ $("#res").scrollIntoView({block: "start", behavior: smooth});+}+function clearResult() {+ S.last = null;+ lastPreview = "";+ renderPreview();+ if (S.view === "json") renderJson();+ status();+}++/* ---------- the response pane ---------- */+/* a near-certainty never rounds up to 100%, nor a long shot down to 0% */+const pct = (p) => (p > 0 && p < 0.001 ? "<0.1%" : p > 0.999 && p < 1 ? ">99.9%" : (p * 100).toFixed(1) + "%");+const UNSURE = 0.6;+const clamp01 = (x) => Math.max(0, Math.min(1, x));+const describe = (v) => (v == null || v === "" ? "" : typeof v === "string" ? v : JSON.stringify(v));+const empty = (text) => h("p", {class: "pv-empty", text});+const listHead = (right) =>+ h("div", {class: "al-head"}, h("span", {text: "Key & instructions"}), h("span", {text: right}));+function withCode(text) { /* `backtick` spans, as laya's presets write field names, become <code> */+ const parts = text.split("`");+ if (parts.length < 3 || parts.length % 2 === 0) return [text];+ return parts.map((part, j) => (j % 2 ? h("code", {text: part}) : part));+}+const idCell = (key, type, ins) => h("span", {class: "a-id"}, h("span", {class: "a-key", text: key}),+ h("span", {class: "tb", text: type || "?"}), h("span", {class: "a-ins"}, withCode(ins)));+function renderAnswersView() {+ expandBtn.hidden = true;+ if (!S.last) return renderPreview();+ if (!S.last.ok) return renderError();+ const res = S.last.data, qs = isMap(S.last.body.questions) ? S.last.body.questions : {};+ const obj = (x) => (isMap(x) ? x : {});+ const routing = obj(res.routing), det = obj(routing.detection), usage = obj(res.usage);+ const cells = [+ ["Checkpoint", routing.model, "mono"], ["Reason", routing.reason, "why"],+ ["Detected", [det.language, det.script].filter(Boolean).join(" / "), "mono"], ["Engine", res.model, "mono"],+ ["Input tokens", usage.input_tokens, "mono"], ["Round trip", fmtMs(S.last.ms), "mono"],+ ].filter((c) => c[1] != null && c[1] !== "");+ const strip = h("div", {class: "stripw"}, h("dl", {class: "strip"}, cells.map(([k, v, cls]) =>+ h("div", {class: cls}, h("dt", {text: k}), h("dd", {text: String(v)})))));+ const list = h("div", {class: "alist"}, listHead("Answer & calibrated confidence"));+ Object.entries(res.answers).forEach(([key, ans], n) =>+ list.append(answerRow(key, ans, has(qs, key) && isMap(qs[key]) ? qs[key] : {}, n)));+ viewAnswers.replaceChildren(strip, list);+ expandBtn.hidden = S.view !== "answers" || !list.querySelector("details");+ syncExpand();+}+function distOf(kind, ans, q) {+ const probs = ans.probabilities && typeof ans.probabilities === "object" ? ans.probabilities : {};+ if (kind === "choice") {+ const crit = isMap(q.criteria) ? q.criteria : {};+ const rows = Object.keys(probs).map((k) =>+ ({label: k, p: +probs[k] || 0, desc: describe(has(crit, k) ? crit[k] : null)}));+ rows.sort((a, b) => b.p - a.p);+ return {rows, win: Math.max(0, rows.findIndex((r) => r.label === String(ans.choice)))};}- $("#h_state").value = JSON.stringify(p.state);- $("#h_questions").value = JSON.stringify(p.questions);- $("#h_model").value = p.model || "";- $("#f").submit();-}--window.addEventListener("DOMContentLoaded", () => {- fill(EXAMPLE);- $("#addField").onclick = () => addField("", "");- $("#addQ").onclick = () => addQuestion("", null);- $("#send").onclick = send;- /* .tabs .tab only: the nav's theme pill is also a .tab and must keep its own handler */- for (const b of document.querySelectorAll(".tabs .tab")) b.onclick = () => show(b.dataset.t);- for (const b of document.querySelectorAll(".preset-btn")) {- /* skipSync=true: switch to the build tab without show()'s raw-textarea sync first,- which would otherwise immediately overwrite the preset with stale/unrelated raw JSON. */- b.onclick = () => { show("build", null, true); fill(PRESETS[b.dataset.preset]); };+ if (kind === "score") {+ const legend = ans.legend && typeof ans.legend === "object" ? ans.legend : {};+ const rows = Object.keys(probs).sort((a, b) => a - b)+ .map((k) => ({label: describe(has(legend, k) ? legend[k] : null) || k, p: +probs[k] || 0, level: k}));+ return {rows, win: rows.reduce((w, r, j) => (r.p > rows[w].p ? j : w), 0)};}- show("build");+ if (kind === "noul" && typeof ans.noul === "number") {+ const crit = isMap(q.criteria) ? q.criteria : {}, d = (k) => describe(has(crit, k) ? crit[k] : null);+ return {rows: [{label: "true", p: ans.noul, desc: d("true")}, {label: "false", p: 1 - ans.noul, desc: d("false")}],+ win: ans.noul >= 0.5 ? 0 : 1};+ }+ return {rows: [], win: 0};+}+function scale(score, top) { /* the expected score marked on the 0..top ordinal scale */+ const at = clamp01(score / top) * 100 + "%", track = h("span", {class: "sc-track"});+ for (let j = 0; j <= top; j++) {+ const tick = h("span", {class: "sc-tick"});+ tick.style.left = (j / top) * 100 + "%";+ track.append(tick);+ }+ const fill = h("span", {class: "sc-fill"}), mark = h("span", {class: "sc-mark"});+ fill.style.width = at;+ mark.style.left = at;+ track.append(fill, mark);+ const label = "Expected score " + score.toFixed(2) + " on a 0 to " + top + " scale";+ return h("div", {class: "scale", role: "img", "aria-label": label},+ h("span", {text: "0"}), track, h("span", {text: String(top)}));+}+function answerRow(key, ans, q, n) {+ const known = isMap(ans), kind = String((known && ans.type) || q.type || "");+ const {rows, win} = known ? distOf(kind, ans, q) : {rows: [], win: 0};+ const head = [icon("chev", "chev"), idCell(key, kind, String(q.instructions || ""))];+ if (!rows.length) /* a type this page does not know yet, or no answer at all: show what came back */+ return h("details", {class: "ans", open: true},+ h("summary", null, head, h("span", {class: "a-v"}, h("span", {class: "v-sub", text: "raw answer"}))),+ h("div", {class: "a-body"}, h("pre", {class: "a-raw", text: pretty(ans === undefined ? null : ans)})));+ const top = rows[win], maxLevel = rows.length - 1;+ const calibrated = typeof ans.answer_confidence === "number" ? ans.answer_confidence+ : Math.max(...rows.map((r) => r.p));+ const score = kind === "score" && typeof ans.score === "number" ? ans.score : null;+ const unsure = calibrated < UNSURE, sub = [];+ if (score != null) sub.push(["score ", h("b", {text: score.toFixed(2)}), " on 0\u2013" + maxLevel]);+ if (unsure) {+ const next = rows.filter((r, j) => j !== win).sort((a, b) => b.p - a.p)[0];+ sub.push([h("span", {class: "unc", title: "Calibrated confidence below 0.60", text: "uncertain"}),+ next ? [", runner-up ", h("b", {text: next.label}), " " + pct(next.p)] : null]);+ }+ const verdict = h("span", {class: "a-v"}, h("span", {class: "v-top"},+ h("span", {class: "v-label" + (kind === "score" ? "" : " mono"), text: top.label}),+ h("span", {class: "v-pct" + (unsure ? " unsure" : ""), text: pct(calibrated)})),+ sub.length ? h("span", {class: "v-sub"}, sub.map((x, j) => (j ? [" \u00b7 ", x] : x))) : null);+ const dist = h("ol", {class: "dist"}, rows.map((r, j) => {+ const fill = h("span", {class: "fill"});+ fill.style.width = clamp01(r.p) * 100 + "%";+ const level = r.level != null ? h("span", {class: "dr-i", text: r.level}) : null;+ return h("li", {class: "dr" + (j === win ? " win" : "")},+ h("span", {class: "dr-l" + (kind === "score" ? " prose" : "")}, level, r.label),+ h("span", {class: "bar", "aria-hidden": "true"}, fill), h("span", {class: "dr-p", text: pct(r.p)}),+ r.desc ? h("span", {class: "dr-d", text: r.desc}) : null);+ }));+ const meta = h("div", {class: "a-meta"},+ h("span", null, h("code", {text: "answer_confidence"}), h("b", {text: calibrated.toFixed(4)}), "calibrated"));+ if (typeof ans.confidence === "number" && Math.abs(ans.confidence - calibrated) > 5e-5)+ meta.append(h("span", {class: "tipw"},+ h("button", {type: "button", class: "tipt", "aria-describedby": "tip-" + n},+ h("code", {text: "confidence"}), h("b", {text: ans.confidence.toFixed(4)}), "entropy, not calibrated",+ icon("info")),+ h("span", {class: "tipb", role: "tooltip", id: "tip-" + n}, withCode(CERTAINTY_TIP))));+ return h("details", {class: "ans", open: true}, h("summary", null, head, verdict),+ h("div", {class: "a-body"}, score != null && maxLevel > 0 ? scale(score, maxLevel) : null, dist, meta));+}+function renderPreview() {+ expandBtn.hidden = true;+ const list = previewList();+ if (!list) {+ viewAnswers.replaceChildren(empty("Add a question to the request and its answer will show up here."));+ return;+ }+ const intro = h("p", {class: "pv-intro"}, "Nothing has run yet. Press ", h("kbd", {text: MOD + "+\u21b5"}),+ " to answer these questions about the state.");+ viewAnswers.replaceChildren(intro, list);+}+function previewList() {+ const qs = isMap(S.qValue) ? S.qValue : {};+ if (!Object.keys(qs).length) return null;+ const rows = Object.entries(qs).map(([k, q]) => {+ q = q && typeof q === "object" ? q : {};+ const c = q.criteria;+ const what = q.type === "noul" ? "true / false"+ : q.type === "choice" ? (isMap(c) ? plural(Object.keys(c).length, "option")+ : Array.isArray(c) && c.length ? plural(c.length, "option") : "no options yet")+ : q.type === "score" ? (Array.isArray(c) ? plural(c.length, "level") : "no levels yet") : "\u2014";+ return h("div", {class: "pv"}, h("span", {class: "dotm"}),+ idCell(k, String(q.type || ""), String(q.instructions || "")), h("span", {class: "a-v", text: what}));+ });+ return h("div", {class: "alist"}, listHead("Possible answers"), rows);+}+/* One line per 422 problem. A union field (state, states \u2192 i, questions \u2192 k \u2192 criteria) fails once per+ branch ("state \u2192 str", "state \u2192 list[any]"); those fold into one line, like POST /gui does. */+const BRANCH = /^(str|int|float|bool|none|dict\[.*\]|list\[.*\])$/, VALID = "Input should be a valid ";+function detailLines(detail) {+ if (!Array.isArray(detail)) return detail == null ? [] : [{loc: [], msg: describe(detail)}];+ const errs = detail.map((d) => ({+ loc: Array.isArray(d && d.loc) ? d.loc.filter((x, j) => !(j === 0 && x === "body")).map(String) : [],+ msg: d && d.msg ? String(d.msg).replace(/^Value error, /, "") : JSON.stringify(d),+ }));+ const union = (loc) => (loc.length === 2 && loc[0] === "state") || (loc.length === 3 && loc[0] === "states")+ || (loc.length === 4 && loc[0] === "questions" && loc[2] === "criteria");+ const branchy = (loc) => union(loc) && BRANCH.test(loc[loc.length - 1]);+ const parent = (loc) => JSON.stringify(loc.slice(0, -1));+ const count = {};+ for (const e of errs) if (branchy(e.loc)) count[parent(e.loc)] = (count[parent(e.loc)] || 0) + 1;+ const groups = new Map();+ for (const e of errs) {+ const loc = branchy(e.loc) && count[parent(e.loc)] > 1 ? e.loc.slice(0, -1) : e.loc, id = JSON.stringify(loc);+ if (!groups.has(id)) groups.set(id, {loc, msgs: []});+ groups.get(id).msgs.push(e.msg);+ }+ return Array.from(groups.values(), ({loc, msgs}) => {+ const kinds = msgs.filter((m) => m.startsWith(VALID)).map((m) => m.slice(VALID.length));+ const msg = msgs.length > 1 && kinds.length === msgs.length ? VALID + orList(kinds)+ : Array.from(new Set(msgs)).join("; ");+ return {loc, msg};+ });+}+function locate(path) { /* a 422 location such as ["questions", "x", "criteria"] -> where to put the caret */+ if (path[0] === "model") return {el: modelSel};+ if (path[0] === "lang") return {el: langIn};+ if (path[0] === "state") return S.stateMode === "json" ? edLoc(stateEd, 0) : {el: S.fields[0] && S.fields[0].el};+ if (path[0] !== "questions") return null;+ if (S.qMode === "form") {+ const q = S.qs.filter((x) => x.key === path[1]).pop();+ if (!q) return null;+ const crit = q.el.rows[0] ? q.el.rows[0][0] : q.el.addCrit;+ return {el: path[2] === "instructions" ? q.el.ins : path[2] === "criteria" ? crit : q.el.key};+ }+ const r = tryParse(qEd.get());+ if (r.error) return null;+ let node = r.ast, at = [node.s, node.s];+ for (const part of path.slice(1)) {+ const en = lastEntry(node, part);+ if (en) { at = [en.ks, en.ke]; node = en.v; }+ else if (node.t === "arr" && node.items[+part]) { node = node.items[+part]; at = [node.s, node.e]; }+ else break;+ }+ return edLoc(qEd, at[0], at[1]);+}+function renderError() {+ const L = S.last, box = h("div", {class: "errbox", role: "alert"});+ if (L.net) {+ const why = "Could not reach " + location.origin + "/predict (" + (L.net.message || String(L.net))+ + "). Check that the server is still running, then run the request again.";+ box.append(h("h3", {text: "The server did not answer"}), h("p", {text: why}));+ showError(box);+ return;+ }+ const detail = isMap(L.data) && "detail" in L.data && !L.odd ? L.data.detail : L.odd ? null : L.text;+ const lines = detailLines(detail);+ if (!lines.length) lines.push({loc: [], msg: String(L.text || "").slice(0, 600) || "No details."});+ const title = L.odd ? "Unexpected response (HTTP " + L.status + ")"+ : L.status === 422 ? "The server rejected the request (HTTP 422)"+ : L.status === 503 ? "The server is still loading (HTTP 503)"+ : L.status === 413 ? "The request is too large (HTTP 413)"+ : L.status >= 500 ? "The prediction failed on the server (HTTP " + L.status + ")"+ : "Request failed (HTTP " + L.status + ")";+ const ul = h("ul", null, lines.map((ln) => {+ const where = ln.loc.length && locate(ln.loc), label = ln.loc.join(" \u2192 ");+ const go = () => jump({loc: where});+ const loc = where ? h("button", {type: "button", class: "errloc", text: label, onclick: go}) : label;+ return h("li", null, ln.loc.length ? [loc, ": "] : null, ln.msg);+ }));+ const help = L.odd ? "The server answered, but not with laya's answers object. The JSON view shows the whole body."+ : L.status === 422 ? "Fix the parts above and run again; locations are clickable."+ : L.status === 503 ? "Its checkpoints are not ready yet. Wait a moment, then run the request again."+ : L.status === 413 ? "The server takes up to " + LIMITS.questions + " questions and a state of up to "+ + LIMITS.stateChars.toLocaleString("en-US") + " characters. Split the request or shorten the state."+ : "Nothing was answered. The server log has the full trace.";+ box.append(h("h3", {text: title}), ul, h("p", {text: help}));+ showError(box);+}+function showError(box) { /* the questions stay listed under the error, so the pane never goes blank */+ const list = previewList();+ viewAnswers.replaceChildren(box, ...(list ? [list] : []));+}+function toPy(v, ind) {+ const pad = " ".repeat(ind + 1), end = ",\n" + " ".repeat(ind);+ if (v === null || typeof v === "boolean") return {null: "None", true: "True", false: "False"}[String(v)];+ if (typeof v !== "object") return typeof v === "number" ? String(v) : JSON.stringify(v);+ const parts = Array.isArray(v) ? v.map((x) => pad + toPy(x, ind + 1))+ : Object.keys(v).map((k) => pad + JSON.stringify(k) + ": " + toPy(v[k], ind + 1));+ const [open, close] = Array.isArray(v) ? ["[", "]"] : ["{", "}"];+ return parts.length ? open + "\n" + parts.join(",\n") + end + close : open + close;+}+function codeBlock(title, sub, text, id) {+ return h("div", {class: "cblock"}, h("div", {class: "cblock-h"}, h("b", {text: title}), h("span", {text: sub}),+ h("button", {type: "button", class: "btn line", "data-copy": id}, "Copy")), h("pre", {id, text}));+}+function renderCode() {+ if (!S.body) {+ viewCode.replaceChildren(empty("Fix the JSON error in the request to get code for it."));+ return;+ }+ const url = location.origin + "/predict";+ const curl = "curl -s " + url + " \\\n -H 'content-type: application/json' \\\n -d '"+ + pretty(S.body).replace(/'/g, "'\\''") + "'";+ const py = "import requests\n\npayload = " + toPy(S.body, 0) + "\n\nresp = requests.post(" + JSON.stringify(url)+ + ", json=payload, timeout=120)\nresp.raise_for_status()\nfor key, answer in resp.json()[\"answers\"].items():\n"+ + " print(key, answer)\n";+ viewCode.replaceChildren(h("div", {class: "codeview"},+ codeBlock("curl", "the current request", curl, "code-curl"), codeBlock("Python", "requests", py, "code-py")));+}+function renderJson() {+ const L = S.last;+ if (!L) {+ viewJson.replaceChildren(empty("Run the request to see the raw JSON response."));+ return;+ }+ const pre = h("pre", {class: "cx-hl", id: "resJson"});+ const cx = h("div", {class: "cx ro"}, h("div", {class: "cx-body"}, pre));+ paint(pre, L.data != null ? pretty(L.data) : L.text || "", null);+ const size = (new Blob([L.text || ""]).size / 1024).toFixed(1) + " KB";+ const sub = L.net ? "network error" : "HTTP " + L.status + ", " + size + ", " + fmtMs(L.ms);+ viewJson.replaceChildren(h("div", {class: "jsonview"}, h("div", {class: "cblock-h"}, h("b", {text: "Response body"}),+ h("span", {text: sub}), h("button", {type: "button", class: "btn line", "data-copy": "resJson"}, "Copy")), cx));+}+function setView(v) {+ S.view = v;+ for (const t of document.querySelectorAll("[role=tab]")) {+ t.setAttribute("aria-selected", String(t.dataset.view === v));+ t.tabIndex = t.dataset.view === v ? 0 : -1;+ }+ viewAnswers.hidden = v !== "answers";+ viewJson.hidden = v !== "json";+ viewCode.hidden = v !== "code";+ expandBtn.hidden = v !== "answers" || !viewAnswers.querySelector("details.ans");+ if (v === "json") renderJson();+ if (v === "code") renderCode();+ store.set(UI_KEY, Object.assign(store.get(UI_KEY) || {}, {view: v}));+}+function syncExpand() {+ const allOpen = Array.from(viewAnswers.querySelectorAll("details.ans")).every((d) => d.open);+ expandBtn.setAttribute("aria-label", allOpen ? "Collapse all answers" : "Expand all answers");+ expandBtn.title = allOpen ? "Collapse all" : "Expand all";+}++/* ---------- layout: side by side or stacked, with draggable dividers ---------- */+const ui = store.get(UI_KEY) || {};+const clamp = (x, lo, hi, d) => (typeof x === "number" && isFinite(x) ? Math.max(lo, Math.min(hi, x)) : d);+S.layout = ui.layout === "stack" ? "stack" : "split";+S.split = clamp(ui.split, 0.3, 0.7, 0.5);+S.sh = clamp(ui.sh, 0.15, 0.75, null); /* null: the state section fits its content */+const editorHeight = () => reqPane.getBoundingClientRect().height - $(".runbar", reqPane).offsetHeight - 1;+/* the CSS keeps both panes at least 390px wide; the divider's range says the same */+function splitBounds() {+ const w = work.getBoundingClientRect().width, lo = Math.max(0.3, 390 / w), hi = Math.min(0.7, 1 - 391 / w);+ return w >= 1100 && lo < hi ? [lo, hi] : [0.3, 0.7];+}+const stateShare = () => (S.sh != null ? S.sh : secState.offsetHeight / (editorHeight() || 1));+function applyLayout() {+ document.body.dataset.layout = S.layout;+ work.style.setProperty("--split", (S.split * 100).toFixed(2) + "%");+ reqPane.classList.toggle("sized", S.sh != null);+ if (S.sh != null) reqPane.style.setProperty("--sh", S.sh.toFixed(4));+ const lb = $("#layoutBtn");+ lb.setAttribute("aria-pressed", String(S.layout === "stack"));+ lb.title = S.layout === "stack" ? "Show request and response side by side" : "Stack the response under the request";+ const [lo, hi] = splitBounds(), vs = $("#vsplit");+ vs.setAttribute("aria-valuemin", String(Math.round(lo * 100)));+ vs.setAttribute("aria-valuemax", String(Math.round(hi * 100)));+ vs.setAttribute("aria-valuenow", String(Math.round(clamp(S.split, lo, hi, 0.5) * 100)));+ $("#hsplit").setAttribute("aria-valuenow", String(Math.round(stateShare() * 100)));+}+function saveUi() {+ store.set(UI_KEY, Object.assign(store.get(UI_KEY) || {}, {layout: S.layout, split: S.split, sh: S.sh}));+}+function splitter(handle, measure, get, set, reset) {+ const done = () => { applyLayout(); saveUi(); };+ handle.addEventListener("pointerdown", (e) => {+ if (e.button !== 0) return;+ e.preventDefault();+ handle.setPointerCapture(e.pointerId);+ handle.classList.add("drag");+ const move = (ev) => { set(measure(ev)); applyLayout(); };+ const up = () => {+ handle.classList.remove("drag");+ handle.removeEventListener("pointermove", move);+ handle.removeEventListener("pointerup", up);+ handle.removeEventListener("pointercancel", up);+ saveUi();+ };+ handle.addEventListener("pointermove", move);+ handle.addEventListener("pointerup", up);+ handle.addEventListener("pointercancel", up);+ });+ handle.addEventListener("keydown", (e) => {+ const step = e.shiftKey ? 0.1 : 0.02;+ const d = {ArrowLeft: -step, ArrowUp: -step, ArrowRight: step, ArrowDown: step, Home: -1, End: 1}[e.key];+ if (d != null) set(get() + d);+ else if (e.key === "Enter") reset();+ else return;+ e.preventDefault();+ done();+ });+ handle.addEventListener("dblclick", () => { reset(); done(); });+}+splitter($("#vsplit"), (e) => { const r = work.getBoundingClientRect(); return (e.clientX - r.left) / r.width; },+ () => clamp(S.split, ...splitBounds(), 0.5), (v) => { S.split = clamp(v, ...splitBounds(), 0.5); },+ () => { S.split = 0.5; });+splitter($("#hsplit"), (e) => (e.clientY - reqPane.getBoundingClientRect().top) / editorHeight(),+ stateShare, (v) => { S.sh = clamp(v, 0.15, 0.75, 0.36); }, () => { S.sh = null; });++/* ---------- wiring ---------- */+for (const b of document.querySelectorAll("[data-mode]"))+ b.addEventListener("click", () => setMode(...b.dataset.mode.split(":")));+$("#fmtState").addEventListener("click", () => stateEd.format());+$("#fmtQs").addEventListener("click", () => qEd.format());+$("#probState").addEventListener("click", (e) => openProblems("state", e.currentTarget));+$("#probQs").addEventListener("click", (e) => openProblems("questions", e.currentTarget));+modelSel.addEventListener("change", changed);+langIn.addEventListener("input", changed);+runBtn.addEventListener("click", run);+revertBtn.addEventListener("click", () => {+ const prev = snap(), had = document.activeElement === revertBtn;+ restore(Object.assign({}, S.base.snap, {stateMode: S.stateMode, qMode: S.qMode}));+ /* Revert has just disabled itself, so focus moves next door to Run instead of dropping to the page */+ if (had && revertBtn.disabled) runBtn.focus({preventScroll: true});+ toast("Reverted to " + quote(S.base.label) + ".", "Undo", () => restore(prev));});+/* Arrow keys on a closed <select> fire change on every press, so an edited request is only replaced when asked. */+presetSel.addEventListener("change", () => {+ const name = presetSel.value, src = SOURCES[name];+ const dirty = S.base && canon(S.body) !== canon(S.base.body);+ if (!src || (dirty && !confirm("Replace your edited request with " + quote(src.label) + "?"))) {+ presetSel.value = S.base ? S.base.name : "example";+ return;+ }+ loadSource(name, false);+});+$("#share").addEventListener("click", shareLink);+$("#layoutBtn").addEventListener("click", () => {+ S.layout = S.layout === "split" ? "stack" : "split";+ applyLayout();+ saveUi();+});+expandBtn.addEventListener("click", () => {+ const rows = viewAnswers.querySelectorAll("details.ans"), open = Array.from(rows).some((d) => !d.open);+ rows.forEach((d) => { d.open = open; });+ syncExpand();+});+viewAnswers.addEventListener("toggle", syncExpand, true);+const tabs = Array.from(document.querySelectorAll("[role=tab]"));+tabs.forEach((t, j) => {+ t.addEventListener("click", () => setView(t.dataset.view));+ t.addEventListener("keydown", (e) => {+ const d = {ArrowRight: 1, ArrowLeft: -1}[e.key];+ if (!d) return;+ e.preventDefault();+ const next = tabs[(j + d + tabs.length) % tabs.length];+ next.focus();+ setView(next.dataset.view);+ });+});+const SHORTCUTS = [+ [MOD + "+\u21b5", "Run the request, from anywhere"], ["F8 / Shift+F8", "Jump to the next / previous problem"],+ ["Tab / Shift+Tab", "Indent / outdent lines in a JSON editor"],+ ["Esc, then Tab / Shift+Tab", "Leave a JSON editor, forward / back"],+ ["Shift+Alt+F", "Format the JSON you are editing"], ["\u21b5", "In a form: next field, or a new option / level"],+ ["?", "Show these shortcuts"],+];+$("#keysBtn").addEventListener("click", (e) => openPop(e.currentTarget, [h("h3", {text: "Keyboard shortcuts"}),+ h("dl", {class: "keys"}, SHORTCUTS.map(([k, d]) => [h("dt", null, h("kbd", {text: k})), h("dd", {text: d})]))],+ "Keyboard shortcuts"));+document.addEventListener("keydown", (e) => {+ if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {+ if (e.target.closest && e.target.closest("a[href]")) return; /* keep the browser's "open in a new tab" */+ e.preventDefault();+ run();+ return;+ }+ if (e.key === "F8") { e.preventDefault(); nextProblem(e.shiftKey); return; }+ if (e.key === "Escape" && pop) { e.preventDefault(); closePop(true); return; }+ if (e.key === "Escape" && $("#toast").contains(e.target)) { hideToast(); return; }+ const typing = e.target.closest && e.target.closest("input, textarea, select, [contenteditable]");+ if (e.key === "?" && !typing && !e.ctrlKey && !e.metaKey && !e.altKey) { e.preventDefault(); $("#keysBtn").click(); }+});+document.addEventListener("selectionchange", () => { stateEd.caret(); qEd.caret(); });+addEventListener("resize", () => closePop());+resBody.addEventListener("scroll", () => closePop(), {passive: true});+$("#runKbd").textContent = MOD + "+\u21b5";++/* ---------- boot: a share link, else ?preset=, else the saved draft, else the example ---------- */+applyLayout();+const params = new URLSearchParams(location.search), shared = readHash();+let booted = false, fromDraft = false;+if (shared) { loadReq("shared", "Shared request", shared, false); booted = true; }+addEventListener("hashchange", () => { /* a link pasted into a tab that is already open */+ const req = readHash();+ if (req) loadReq("shared", "Shared request", req, true);+ else if (req === null) toast("The link does not hold a readable request.");+});+if (!booted && SOURCES[params.get("preset")]) { loadSource(params.get("preset"), false); booted = true; }+const draft = booted ? null : store.get(DRAFT_KEY);+if (draft && draft.snap && typeof draft.snap.stateText === "string" && typeof draft.snap.qText === "string") {+ const b = draft.base && draft.base.snap ? draft.base+ : {name: "example", label: SOURCES.example.label, snap: snapFromReq(EXAMPLE)};+ setBase(b.name, b.label, b.snap);+ if (b.name === "shared") /* the picker can load it again */+ try {+ SOURCES.shared = {label: b.label, req: {state: JSON.parse(b.snap.stateText), questions: JSON.parse(b.snap.qText),+ model: b.snap.model, lang: b.snap.lang}};+ } catch (e) {+ presetSel.querySelector("option[value='shared']").hidden = true; /* a hand-edited draft: nothing to load */+ }+ restore(draft.snap);+ booted = fromDraft = true;+}+if (!booted) loadSource("example", false);+if (shared === null)+ toast("The link does not hold a readable request; showing " + (fromDraft ? "your last draft" : quote(S.base.label))+ + " instead.");+if (params.has("model")) { /* from the Models page: part of what was loaded, not an edit */+ setModel(params.get("model"));+ changed();+ if (!fromDraft) {+ S.base.snap = Object.assign({}, S.base.snap, {model: params.get("model")});+ S.base.body = S.body;+ changed();+ }+}+if (params.has("preset") || params.has("model")) {+ params.delete("preset");+ params.delete("model");+ history.replaceState(null, "", location.pathname + (params.toString() ? "?" + params : ""));+}+setView(ui.view === "json" || ui.view === "code" ? ui.view : "answers");+})();"""[email protected]("/", response_class=HTMLResponse)-def index() -> HTMLResponse:- """Default page: build a request with form elements, or paste raw JSON."""- example = json.dumps(- {"state": json.loads(_EXAMPLE_STATE), "questions": json.loads(_EXAMPLE_QUESTIONS)}+def _script_json(value: Any) -> str:+ """JSON for an inline <script>: `<` is escaped so no string can close the tag."""+ return json.dumps(value).replace("<", "\\u003c")+++def _mode_seg(sec: str, label: str, modes: tuple) -> str:+ return f"<div class='seg' role='group' aria-label='{label}'>" + "".join(+ f"<button type='button' data-mode='{sec}:{mode}' aria-pressed='{str(i == 0).lower()}'>"+ f"{_icon(ico)}{text}</button>"+ for i, (mode, ico, text) in enumerate(modes)+ ) + "</div>"+++def _sec_head(title: str, sub_id: str, fmt_id: str, prob_id: str, seg: str) -> str:+ return (+ f"<div class='sec-head'><h2>{title}</h2><span class='sec-sub' id='{sub_id}'></span><div class='sec-tools'>"+ f"<button type='button' class='btn' id='{fmt_id}' hidden title='Format JSON (Shift+Alt+F)'>"+ f"{_icon('format')}<span class='fmt-t'>Format</span></button>{seg}"+ f"<button type='button' class='probs' id='{prob_id}' aria-haspopup='dialog' aria-expanded='false'>"+ f"{_icon('info')}<span>0</span></button></div></div>")- options = "".join(f"<option value='{escape(m)}'>{escape(m)}</option>" for m in sorted(MODELS))- preset_btns = "".join(- f"<button type='button' class='tab preset-btn' data-preset='{escape(key)}'>"- f"{escape(p['label'])}</button>"++[email protected]("/", response_class=HTMLResponse)+def index(request: Request) -> HTMLResponse:+ """The playground: edit a request on the left, run it, read the answers on the right."""+ example = {"state": json.loads(_EXAMPLE_STATE), "questions": json.loads(_EXAMPLE_QUESTIONS)}+ presets = "".join(+ f"<option value='{escape(key)}' data-preset='{escape(key)}'>{escape(p['label'])}</option>"for key, p in PRESETS.items())- return _page(- "Laya",- "<h1>Ask a state anything.<br><span class='thin'>Every question, one pass.</span></h1>"- "<div class='sub'>Fill in the request below or paste raw JSON, then send it to "- "<code>/gui</code> to see each answer as a 0–100 bar.</div>"- "<div class='sub'><span class='instr' style='margin:0 8px 0 0'>Or start from a built-in "- "workflow preset (<a href='/presets'>raw JSON</a>):</span>"- f"<div class='presets'>{preset_btns}</div></div>"- "<div class='tabs'>"- "<button type='button' class='tab on' data-t='build'>Builder</button>"- "<button type='button' class='tab' data-t='raw'>Raw JSON</button></div>"- "<form id='f' method='post' action='/gui'>"- "<input type='hidden' name='state' id='h_state'>"- "<input type='hidden' name='questions' id='h_questions'>"- "<input type='hidden' name='model' id='h_model'>"- "<div id='pane-build'>"- "<div class='card'><div class='q'>State</div>"- "<div class='instr'>The record to classify — any fields you like.</div>"- "<div id='fields'></div>"- "<button type='button' class='ghost' id='addField'>+ Add field</button></div>"- "<div class='q'>Questions</div>"- "<div class='instr'>choice = pick one label · score = ordered levels · "- "noul = yes/no probability</div>"- "<div id='questions'></div>"- "<button type='button' class='ghost' id='addQ'>+ Add question</button>"- "</div>"- "<div id='pane-raw' hidden><div class='card'><div class='q'>Raw JSON</div>"- "<div class='instr'>{\"state\": …, \"questions\": {…}, "- "\"model\": optional}</div>"- "<textarea id='raw' rows='22'></textarea></div></div>"- "<div class='card'>"- "<div class='q'>Model <span class='instr'>(optional)</span></div>"- f"<select id='model'><option value=''>auto-route by language</option>{options}</select>"- "<div><button type='button' class='send' id='send'>Send</button></div>"- "<div class='err' id='err'></div></div></form>"- f"<script>const EXAMPLE = {example}; const PRESETS = {_PRESETS_JSON};</script>"- f"<script>{_BUILDER_JS}</script>",+ models = "".join(f"<option value='{escape(m)}'>{escape(m)}</option>" for m in sorted(MODELS))+ middle = (+ _crumb("Playground", "h1")+ + "<span class='crumb js' aria-hidden='true'>/</span><span class='sel preset js'>"+ "<select id='preset' aria-label='Load a preset'><option value='example'>Billing email (example)</option>"+ f"{presets}</select>{_icon('chev')}</span><span class='edited js' id='edited' hidden>edited</span>"+ "<button type='button' class='btn js' id='share' title='Copy a link that opens this exact request'>"+ f"{_icon('link')}Copy link</button>"+ )+ layout = (+ "<button type='button' class='ibtn js' id='layoutBtn' aria-label='Stacked layout' aria-pressed='false'>"+ f"{_icon('cols', 'i-cols')}{_icon('rows', 'i-rows')}</button>"+ )+ state_head = _sec_head("State", "stateSub", "fmtState", "probState", _mode_seg(+ "state", "State editor", (("fields", "fields", "Fields"), ("json", "braces", "JSON"))))+ q_head = _sec_head("Questions", "qSub", "fmtQs", "probQs", _mode_seg(+ "questions", "Questions editor", (("form", "form", "Form"), ("json", "braces", "JSON"))))+ tabs = "".join(+ f"<button type='button' class='tab' role='tab' id='tab-{v}' data-view='{v}' aria-controls='view{v.title()}' "+ f"aria-selected='{str(v == 'answers').lower()}'>{label}</button>"+ for v, label in (("answers", "Answers"), ("json", "JSON"), ("code", "Code"))+ )+ work = (+ "<main class='work js' id='work'><section class='pane req' id='req' aria-label='Request'>"+ f"<div class='sec' id='secState'>{state_head}<div class='sec-note' id='stateNote' hidden></div>"+ "<div class='sec-body' id='stateBody'></div></div>"+ "<div class='hsplit' id='hsplit' role='separator' tabindex='0' aria-orientation='horizontal' "+ "aria-label='Resize state and questions' aria-valuemin='15' aria-valuemax='75'></div>"+ f"<div class='sec' id='secQs'>{q_head}<div class='sec-note' id='qNote' hidden></div>"+ "<div class='sec-body' id='qsBody'></div></div>"+ "<div class='runbar'><span class='sel'><select id='model' aria-label='Model' "+ "title='Leave on auto-route and the server picks a checkpoint by language'>"+ f"<option value=''>Auto-route by language</option>{models}</select>{_icon('chev')}</span>"+ "<input class='lang' id='lang' placeholder='lang' maxlength='16' autocomplete='off' spellcheck='false' "+ "aria-label='Language hint, an ISO code such as en' "+ "title='ISO language hint such as en or pt; skips detection'>"+ "<span class='runmsg' id='runMsg' role='status' aria-live='polite'></span><div class='right'>"+ "<button type='button' class='ibtn kbd-help' id='keysBtn' aria-label='Keyboard shortcuts' "+ f"aria-haspopup='dialog' aria-expanded='false'>{_icon('kbd')}</button>"+ "<button type='button' class='btn' id='revert' disabled aria-label='Revert' "+ "title='Go back to the request as it was loaded'>"+ f"{_icon('undo')}<span class='rv-t'>Revert</span></button>"+ "<button type='button' class='btn primary' id='run'><span class='run-t'>Run request</span>"+ "<kbd id='runKbd'>Ctrl+↵</kbd></button></div></div></section>"+ "<div class='vsplit' id='vsplit' role='separator' tabindex='0' aria-orientation='vertical' "+ "aria-label='Resize request and response' aria-valuemin='30' aria-valuemax='70'></div>"+ "<section class='pane res' id='res' aria-label='Response'><div class='res-head'><h2>Response</h2>"+ "<span class='badge' id='status' data-s='preview'>Preview</span><span class='res-hint' id='resHint'></span>"+ f"<div class='tabs' role='tablist' aria-label='Response views'>{tabs}</div>"+ "<button type='button' class='ibtn' id='expandAll' hidden aria-label='Collapse all answers'>"+ f"{_icon('expand')}</button></div><div class='progress' id='progress' hidden></div>"+ "<div class='res-body' id='resBody'><div id='viewAnswers' role='tabpanel' aria-labelledby='tab-answers'></div>"+ "<div id='viewJson' role='tabpanel' aria-labelledby='tab-json' hidden></div>"+ "<div id='viewCode' role='tabpanel' aria-labelledby='tab-code' hidden></div></div></section></main>"+ "<div class='toast' id='toast' hidden></div>"+ "<div class='vh' id='announce' role='status' aria-live='polite'></div>"+ f"<span id='certaintyTip' hidden>{escape(_CERTAINTY_TIP)}</span>"+ )+ picked = PRESETS.get(request.query_params.get("preset") or "")+ nj_state = json.dumps(picked["state"], indent=2, ensure_ascii=False) if picked else _EXAMPLE_STATE+ nj_qs = json.dumps(picked["questions"], indent=2, ensure_ascii=False) if picked else _EXAMPLE_QUESTIONS+ nj_links = ", ".join(+ f"<a href='/?preset={escape(key)}'>{escape(label)}</a>"+ for key, label in [("example", "Billing email (example)")] + [(k, p["label"]) for k, p in PRESETS.items()]+ )+ nojs = (+ "<noscript><form class='nojs' method='post' action='/gui'>"+ "<p>JavaScript is off, so this is the plain form: edit the JSON and the server renders the answers.</p>"+ f"<p class='muted'>Start from a preset: {nj_links}.</p>"+ f"<label for='nj-state'>State</label><textarea id='nj-state' name='state' rows='7'>{escape(nj_state)}"+ f"</textarea><label for='nj-q'>Questions</label><textarea id='nj-q' name='questions' rows='18'>"+ f"{escape(nj_qs)}</textarea><div class='row'><label>Model <select name='model'>"+ f"<option value=''>Auto-route by language</option>{models}</select></label>"+ "<label>Language hint <input name='lang' placeholder='e.g. en'></label>"+ "<button type='submit' class='btn primary'>Run request</button></div></form></noscript>"+ )+ presets_js = _PRESETS_JSON.replace("<", "\\u003c")+ boot = (+ f"<script>const EXAMPLE = {_script_json(example)}; const PRESETS = {presets_js};"+ f" const QTYPES = {_script_json(sorted(getattr(laya, 'QTYPES', {}) or {}) or ['choice', 'score', 'noul'])};"+ f" const LIMITS = {_script_json({'questions': MAX_QUESTIONS, 'stateChars': MAX_STATE_CHARS})};"+ "</script>"+ )+ return _html(+ "Laya playground",+ _topbar("", middle, layout) + work + nojs,+ css=_PLAYGROUND_CSS,+ script=boot + f"<script>{_PLAYGROUND_JS}</script>",+ body_attrs=" class='pg' data-layout='split'",)@@ -992,7 +3178,7 @@ def _snippet(model: Optional[str]) -> str:payload["model"] = modelbody = json.dumps(payload, indent=2)return (- "<div class='snip'><pre>"+ f"<div class='snip'><pre id='snip-{escape(model or 'auto')}'>"+ escape(f"curl -s {_HOST}/predict \\\n -H 'content-type: application/json' \\\n -d '{body}'")+ "</pre></div>")@@ -1004,35 +3190,46 @@ def _models_page(payload: Dict[str, Any]) -> HTMLResponse:for name in payload["allowed"]:repo, rev = (payload["models"].get(name) or [None, None])[:2]blurb, picks = _MODEL_DOC.get(name, ("", []))- chips = "".join(f"<span class='tag'>{escape(p)}</span>" for p in picks)+ chips = "".join(f"<li>{escape(p)}</li>" for p in picks)cards += (- "<div class='card'>"- f"<div class='head-row'><div class='q'>{escape(name)}</div>"- + (f"<span class='badge on'>default</span>" if name == default else "")- + f"<span class='badge'>{escape(str(repo or ''))}"- + (f" · {escape(str(rev))}" if rev else "")- + "</span></div>"- f"<div class='instr'>{blurb}</div>"- f"<div class='tags' style='margin:0 0 4px'>{chips}</div>"+ f"<section class='panel'><div class='ph'><h2>{escape(name)}</h2>"+ + ("<span class='badge' data-s='ok'>default</span>" if name == default else "")+ + f"<span class='repo'>{escape(str(repo or ''))}{escape(' @ ' + str(rev)) if rev else ''}</span></div>"+ f"<div class='pb two'><p>{escape(blurb)}</p>"+ _snippet(name)+ + (f"<ul class='chips' aria-label='Picked when'><li class='muted'>Picked when</li>{chips}</ul>"+ if chips else "")+ "<div class='acts'>"- f"<a class='tab' href='/?model={escape(name)}'>Open in builder</a>"- "<button type='button' class='tab copy'>Copy curl</button>"- "</div></div>"+ f"<a class='btn line' href='/?model={escape(name)}'>Open in playground</a>"+ f"<button type='button' class='btn line js' data-copy='snip-{escape(name)}'>Copy curl</button>"+ "</div></div></section>")return _page("Laya models",- "<h1>Models.<br><span class='thin'>Three checkpoints, one router.</span></h1>"- "<div class='sub'>Leave <code>model</code> out and the router picks by language. "- "Send it to pin a checkpoint; anything else is rejected with a 422.</div>"+ "Models",+ "<div class='doc-head'><h1>Models</h1><span class='doc-sub'>Three checkpoints, one router</span></div>"+ "<p class='lead'>Leave <code>model</code> out and the router picks a checkpoint by language. "+ "Send it to pin one; any other value is rejected with a 422.</p>"+ cards- + "<div class='card foot'>"- "<button type='button' class='tab' onclick=\"location.href='/'\">New request</button>"- "<span class='spacer'><a href='/models' >Raw JSON</a> · "- "<a href='/docs'>API docs</a></span></div>",+ + "<div class='foot'><a class='btn line' href='/'>Open the playground</a>"+ "<a class='btn' href='/docs'>API docs</a></div>",+ current="/models",)+_ENDPOINTS = (+ ("GET", "/", "The playground", "/"),+ ("POST", "/predict", "Answer every question about one state", ""),+ ("POST", "/predict/batch", "The same questions over up to 64 states", ""),+ ("POST", "/gui", "Form or JSON in, rendered answers out; no JavaScript needed", ""),+ ("GET", "/models", "Checkpoints and when the router picks each", "/models"),+ ("GET", "/presets", "Built-in workflow presets, state and questions included", "/presets"),+ ("GET", "/qtypes", "Question types this laya build answers", "/qtypes"),+ ("GET", "/health", "This page, or JSON for API clients", ""),+ ("GET", "/docs", "Interactive OpenAPI docs", "/docs"),+)++def _health_page(payload: Dict[str, Any]) -> HTMLResponse:cfg = payload.get("config", {})ok = payload.get("status") == "ok"@@ -1040,42 +3237,95 @@ def _health_page(payload: Dict[str, Any]) -> HTMLResponse:f"<dt>{escape(k)}</dt><dd>{escape(str(v if v is not None else 'auto'))}</dd>"for k, v in cfg.items())- links = "".join(- f"<a class='tab' href='{escape(href)}'>{escape(label)}</a>"- for href, label in (- ("/", "Builder"), ("/models", "Models"), ("/docs", "API docs"),- ("/qtypes", "Question types"), ("/health", "Raw JSON"),- )+ eps = "".join(+ f"<tr><td>{method}</td><td>"+ + (f"<a href='{href}'>{escape(path)}</a>" if href else escape(path))+ + f"</td><td>{escape(what)}</td></tr>"+ for method, path, what, href in _ENDPOINTS)return _page("Laya health",- "<h1>Health.</h1>"- "<div class='sub'>Router state and the settings this process was started with.</div>"- "<div class='panel'><div><div class='k'>Status</div>"- f"<div class='v'>{'ready' if ok else 'loading'}</div></div>"- f"<div class='m'>checkpoints are {'preloaded' if cfg.get('preload') else 'loaded on demand'}"- f"<br>up to <b>{escape(str(cfg.get('max_loaded', 1)))}</b> kept in memory</div></div>"- f"<div class='card'><div class='q'>Configuration</div>"- "<div class='instr'>Override with flags (<code>--device</code>, <code>--no-preload</code>) "- "or env vars (<code>LAYA_DEVICE</code>, <code>LAYA_PRELOAD</code>).</div>"- f"<dl class='kvlist'>{rows}</dl></div>"- f"<div class='card'><div class='q'>Endpoints</div>"- "<div class='instr'>Every GET page here also answers JSON when you ask for it "- "(<code>Accept: application/json</code>).</div>"- f"<div class='acts'>{links}</div></div>"- "<div class='card foot'>"- "<button type='button' class='tab' onclick='location.reload()'>Refresh</button>"- "<button type='button' class='tab' onclick=\"location.href='/'\">New request</button>"- "</div>",+ "Health",+ "<div class='doc-head'><h1>Health</h1></div>"+ "<p class='lead'>Router state and the settings this process was started with.</p>"+ f"<section class='panel'><div class='pb'><div class='status{'' if ok else ' wait'}'>"+ f"{'Ready' if ok else 'Loading'}</div>"+ f"<p class='muted' style='margin:4px 0 0'>Checkpoints are "+ f"{'preloaded' if cfg.get('preload') else 'loaded on demand'}; up to "+ f"{escape(str(cfg.get('max_loaded', 1)))} kept in memory.</p></div></section>"+ f"<section class='panel'><div class='ph'><h2 class='t'>Configuration</h2></div><dl class='kvt'>{rows}</dl>"+ "<div class='pb muted' style='border-top:1px solid var(--line)'>Override with flags "+ "(<code>--device</code>, <code>--no-preload</code>, <code>--max-loaded</code>) or env vars "+ "(<code>LAYA_DEVICE</code>, <code>LAYA_PRELOAD</code>, <code>LAYA_MAX_LOADED</code>).</div></section>"+ "<section class='panel'><div class='ph'><h2 class='t'>Endpoints</h2>"+ "<span class='repo'><code>/models</code> and <code>/health</code> answer JSON to "+ "<code>Accept: application/json</code></span></div>"+ f"<table class='eps'>{eps}</table></section>"+ "<div class='foot'><button type='button' class='btn line js' onclick='location.reload()'>Refresh</button>"+ "<a class='btn line' href='/'>Open the playground</a></div>",+ current="/health",)@app.get("/gui", response_class=RedirectResponse)def gui_form() -> RedirectResponse:- """The GUI's entry point is the builder page."""+ """Redirects to the playground at /."""return RedirectResponse("/", status_code=307)+_UNION_BRANCH = re.compile(r"(str|int|float|bool|none|dict\[.*\]|list\[.*\])")+_VALID = "Input should be a valid "+++def _union_branch(loc: List[str]) -> bool:+ """Is this the error of one branch of a union field (state, states -> i, questions -> k -> criteria)?++ Only there: under `questions` the last part is a key the user chose, and may well be `str`.+ """+ union = (len(loc) == 2 and loc[0] == "state") or (len(loc) == 3 and loc[0] == "states") or (+ len(loc) == 4 and loc[0] == "questions" and loc[2] == "criteria")+ return union and _UNION_BRANCH.fullmatch(loc[-1]) is not None+++def _error_lines(exc: Exception) -> List[str]:+ """One readable line per problem: `questions -> x -> criteria: message`.++ A union field fails once per branch (`state -> str`, `state -> list[any]`); those fold into one line.+ """+ if isinstance(exc, PydanticValidationError):+ errs = [([str(p) for p in e.get("loc", ())], str(e.get("msg", "")).replace("Value error, ", "", 1))+ for e in exc.errors()]+ branchy = [tuple(loc[:-1]) for loc, _ in errs if _union_branch(loc)]+ grouped: Dict[str, List[str]] = {}+ for loc, msg in errs:+ if _union_branch(loc) and branchy.count(tuple(loc[:-1])) > 1:+ loc = loc[:-1]+ grouped.setdefault(" \u2192 ".join(loc), []).append(msg)+ lines = []+ for where, msgs in grouped.items():+ kinds = [m[len(_VALID):] for m in msgs if m.startswith(_VALID)]+ if len(msgs) > 1 and len(kinds) == len(msgs):+ msg = _VALID + ", ".join(kinds[:-1]) + " or " + kinds[-1]+ else:+ msg = "; ".join(dict.fromkeys(msgs))+ lines.append(f"{where}: {msg}")+ return lines+ if isinstance(exc, json.JSONDecodeError):+ return [f"Invalid JSON: {exc.msg} at line {exc.lineno}, column {exc.colno}"]+ return [str(exc)]+++def _gui_error(title: str, lines: List[str], hint: str) -> HTMLResponse:+ items = "".join(f"<li>{escape(line)}</li>" for line in lines)+ return _page(+ "Laya - error",+ "Results",+ f"<div class='doc-head'><h1>{escape(title)}</h1><span class='badge' data-s='error'>Error</span></div>"+ f"<div class='panel'><div class='errbox' role='alert'><ul>{items}</ul><p>{escape(hint)}</p></div></div>"+ "<div class='foot'><a class='btn line' href='/'>Open the playground</a></div>",+ )++@app.post("/gui", response_class=HTMLResponse)async def gui_predict(request: Request) -> HTMLResponse:ctype = (request.headers.get("content-type") or "").split(";")[0].strip()@@ -1092,9 +3342,14 @@ async def gui_predict(request: Request) -> HTMLResponse:}req = PredictRequest.model_validate(raw)except (json.JSONDecodeError, PydanticValidationError, ValueError) as exc:- return _page("Laya - error", f"<h1>Bad request.</h1><pre>{escape(str(exc))}</pre>"- "<p><a href='/gui'>back</a></p>")+ return _gui_error("Bad request", _error_lines(exc), "Fix the request and send it again.")+ try:+ _check_request_limits(req.state, req.questions)+ except HTTPException as exc:+ return _gui_error("Request too large", [str(exc.detail)],+ f"The server takes up to {MAX_QUESTIONS} questions and a state of up to "+ f"{MAX_STATE_CHARS:,} characters. Split the request or shorten the state.")questions = _questions(req.questions)try:started = time.perf_counter()@@ -1104,45 +3359,32 @@ async def gui_predict(request: Request) -> HTMLResponse:)res["_elapsed"] = time.perf_counter() - startedexcept Exception as exc:- return _page("Laya - error", f"<h1>Prediction failed.</h1>"- f"<pre>{escape(f'{type(exc).__name__}: {exc}')}</pre>"- "<p><a href='/gui'>back</a></p>")-- routing = res.get("routing", {}) or {}- cards = "".join(- _answer_card(name, ans, questions.get(name, {}))- for name, ans in (res.get("answers") or {}).items()- )- n = len(res.get("answers") or {})+ return _gui_error("Prediction failed", [f"{type(exc).__name__}: {exc}"],+ "Nothing was answered. The server log has the full trace.")++ answers = res.get("answers") or {}+ n = len(answers)secs = res.pop("_elapsed", None)- usage = res.get("usage") or {}- side = " · ".join(- escape(str(x))- for x in (- res.get("model"),- f"{usage['input_tokens']} input tokens" if usage.get("input_tokens") else None,- )- if x- )- panel = (- "<div class='panel'><div><div class='k'>Routed to</div>"- f"<div class='v'>{escape(str(routing.get('model', 'default')))}</div></div>"- f"<div class='m'><b>{n}</b> question{'s' if n != 1 else ''} answered<br>"- f"{escape(str(routing.get('reason', '')))}<br>{side}</div></div>"+ rows = "".join(+ _answer_row(name, ans, questions.get(name, {}), i)+ for i, (name, ans) in enumerate(answers.items()))+ sent: Dict[str, Any] = {"state": req.state, "questions": questions}+ sent.update({k: v for k, v in (("model", req.model), ("lang", req.lang)) if v})+ share = base64.urlsafe_b64encode(json.dumps(sent, ensure_ascii=False).encode()).decode().rstrip("=")return _page("Laya results",- f"<h1>Answers.</h1>"- f"<div class='sub sumline'><span>Scored {n} field{'s' if n != 1 else ''} in one pass"- + (f" in {secs:.2f}s" if isinstance(secs, float) else "")- + ". Every field is answered from the same state, independently of the others.</span>"- "<button type='button' class='tab' id='expand-all'>Expand all</button></div>"- f"{panel}{cards}"- "<div class='card foot'>"- "<button type='button' class='tab' onclick='history.back()'>← Back</button>"- "<button type='button' class='tab' onclick=\"location.href='/'\">New request</button>"- "<span class='spacer'><a href='/docs'>API docs</a></span>"- "</div>",+ "Results",+ "<div class='doc-head'><h1>Answers</h1><span class='badge' data-s='ok'>OK</span>"+ f"<span class='doc-sub'>{n} question{'s' if n != 1 else ''} answered in one pass</span>"+ "<span class='grow'></span>"+ "<button type='button' class='btn line js' data-expand>Collapse all</button>"+ f"<a class='btn line' href='/#r={share}'>Edit in playground</a></div>"+ f"<div class='panel'>{_strip(res, _ms(secs) if isinstance(secs, float) else '')}"+ "<div class='alist'><div class='al-head'><span>Key & instructions</span>"+ f"<span>Answer & calibrated confidence</span></div>{rows}</div></div>"+ "<div class='foot'><a class='btn line' href='/'>Open the playground</a>"+ "<a class='btn' href='/docs'>API docs</a></div>",)diff --git a/tests/test_server_example.py b/tests/test_server_example.pyindex 9bb6c7e0..3fea79d9 100644--- a/tests/test_server_example.py+++ b/tests/test_server_example.py@@ -7,8 +7,10 @@ socket, no subprocess.Run: python3 tests/test_server_example.py"""+import base64import jsonimport os+import reimport sysos.environ.setdefault("TOKENIZERS_PARALLELISM", "false")@@ -32,6 +34,11 @@ def ok(name, cond, detail=""):print(" %s %s%s" % ("PASS" if cond else "FAIL", name, (" " + detail) if detail else ""), flush=True)+def errs(r):+ """The error lines of a /gui error page, for a short failure message."""+ return " | ".join(re.findall(r"<li>(.*?)</li>", r.text)) or r.text[-300:]++def main():try:from fastapi.testclient import TestClient@@ -52,6 +59,7 @@ def main():r = client.get("/models")ok("GET /models -> 200", r.status_code == 200, str(r.status_code))+ ok("GET /models stays JSON for API clients", set(r.json()) == {"default", "allowed", "models"}, r.text[:200])r = client.get("/presets")ok("GET /presets -> 200", r.status_code == 200, str(r.status_code))@@ -64,10 +72,27 @@ def main():)r = client.get("/")- ok("GET / (builder page) -> 200", r.status_code == 200, str(r.status_code))+ ok("GET / (playground page) -> 200", r.status_code == 200, str(r.status_code))ok("GET / is html", "<html" in r.text.lower())- ok("GET / embeds PRESETS for the builder JS", "const PRESETS = " in r.text)- ok("GET / has a preset button per workflow", r.text.count("data-preset=") == len(expected_presets))+ ok("GET / embeds PRESETS for the playground JS", "const PRESETS = " in r.text)+ ok("GET / has a preset option per workflow", r.text.count("data-preset=") == len(expected_presets))+ ok("GET / sets the theme in <head>, before first paint", r.text.index("laya.theme") < r.text.index("<body"))+ ok("GET / names the page in an h1", "<h1 class='crumb-t'>Playground</h1>" in r.text)+ ok("GET / keeps a no-JavaScript form that posts to /gui",+ "<noscript><form class='nojs' method='post' action='/gui'>" in r.text)+ ok("GET / loads nothing from the network",+ not re.search(r"""(src|href)=["']?(https?:)?//|url\(\s*["']?(https?:)?//""", r.text))+ r = client.get("/?preset=guard")+ ok("GET /?preset=guard prefills the no-JavaScript form", ""jailbreak"" in r.text)+ r = client.get("/gui", follow_redirects=False)+ ok("GET /gui redirects to the playground", r.status_code == 307 and r.headers.get("location") == "/",+ str(r.status_code))++ r = client.get("/models", headers={"accept": "text/html"})+ ok("GET /models (browser) renders every checkpoint",+ all(f"href='/?model={m}'" in r.text for m in server.MODELS), r.text[:200])+ r = client.get("/health", headers={"accept": "text/html"})+ ok("GET /health (browser) renders the status page", "<h1>Health</h1>" in r.text and "Ready" in r.text)payload = {"state": "We were billed twice for March. Please refund it today.",@@ -101,18 +126,43 @@ def main():"type": "score", "instructions": "?", "criteria": ["low", None, "high"]}}})ok("POST /predict rejects a null score level as 422", r.status_code == 422, str(r.status_code) + " " + r.text[:200])- # /gui is the form-post path the builder JS uses -- this is what caught the+ # /gui is the form-post path the no-JavaScript fallback uses -- this is what caught the# missing python-multipart dependency during manual verification.- r = client.post(- "/gui",- data={- "state": '{"body": "We were billed twice for March. Please refund it."}',- "questions": '{"refund_requested": {"type": "noul", "instructions": "Does the user explicitly request a refund?"}}',- "model": "",- },- )+ form = {+ "state": '{"body": "We were billed twice for March. Please refund it."}',+ "questions": '{"refund_requested": {"type": "noul", "instructions": "Does the user explicitly request a refund?"}}',+ "model": "",+ }+ r = client.post("/gui", data=form)ok("POST /gui (form) -> 200", r.status_code == 200, str(r.status_code) + " " + r.text[:200])ok("POST /gui is html", "<html" in r.text.lower())+ ok("POST /gui renders one answer row with a winning bar",+ r.text.count("<details class='ans'") == 1 and r.text.count("class='dr win'") == 1)+ link = re.search(r"href='/#r=([A-Za-z0-9_-]+)'", r.text)+ shared = json.loads(base64.urlsafe_b64decode(link.group(1) + "=" * (-len(link.group(1)) % 4))) if link else {}+ ok("POST /gui links back to the playground with the same request",+ shared == {"state": json.loads(form["state"]), "questions": json.loads(form["questions"])}, str(shared))+ ok("POST /gui links to a fresh playground too", "href='/'>Open the playground</a>" in r.text)++ r = client.post("/gui", data={"state": '"hi"', "questions": '{"x": {"type": "choice", "instructions": "?"}}'})+ ok("POST /gui explains a validation error per location",+ r.status_code == 200 and "questions \u2192 x" in r.text and "criteria" in r.text, errs(r))+ r = client.post("/gui", data={"state": '"hi"', "questions": '{"x": {"type": "noul", "instructions": "?"}}',+ "model": "gpt-5"})+ ok("POST /gui drops pydantic's 'Value error, ' prefix",+ "model: unknown model" in r.text and "Value error," not in r.text, errs(r))+ r = client.post("/gui", data={"state": "5", "questions": '{"x": {"type": "choice", "instructions": "?", '+ '"criteria": "billing"}}'})+ ok("POST /gui folds a union's per-branch errors into one line",+ "state: Input should be a valid string, dictionary or list" in r.text+ and "criteria: Input should be a valid dictionary or list" in r.text and "list[any]" not in r.text,+ errs(r))+ r = client.post("/gui", data={"state": '"hi"', "questions": '{"int": {"type": "choice", "instructions": "a"}, '+ '"bool": {"type": "choice", "instructions": "b"}}'})+ ok("POST /gui keeps question keys named like union branches apart",+ "questions \u2192 int" in r.text and "questions \u2192 bool" in r.text, errs(r))+ r = client.post("/gui", data={"state": "{not json", "questions": "{}"})+ ok("POST /gui explains invalid JSON", r.status_code == 200 and "Invalid JSON" in r.text, errs(r))# laya.guard_questions()'s "topic" criteria uses None as a placeholder for "no# description" ({"coding": None, ...}) -- this is what caught two bugs: the@@ -128,8 +178,44 @@ def main():},)ok("POST /gui with guard preset (None criteria) -> 200", r.status_code == 200, str(r.status_code) + " " + r.text[:300])- ok("POST /gui with guard preset renders real answers", "<h1>Answers." in r.text, r.text[:300])- ok("POST /gui with guard preset does not leak literal 'None'", "— None" not in r.text)+ ok("POST /gui with guard preset renders real answers",+ r.text.count("<details class='ans'") == len(guard["questions"]), r.text[:300])+ ok("POST /gui with guard preset does not leak literal 'None'",+ ">None<" not in r.text and ">null<" not in r.text and "— None" not in r.text)+ ok("POST /gui renders `backtick` spans in instructions as code", "<code>prompt</code>" in r.text)+ ok("POST /gui reports server time in the playground's units",+ re.search(r"Server time</dt><dd>(\d+ ms|\d+\.\d\d s)</dd>", r.text) is not None)++ # Every user string on the server-rendered page is escaped, keys and criteria included.+ evil = "<img src=x onerror=alert(1)>"+ hostile = {evil: {"type": "choice", "instructions": "</script><script>alert(2)</script> `" + evil + "`",+ "criteria": {evil: evil, "other": None}}}+ r = client.post("/gui", data={"state": json.dumps({"body": evil}), "questions": json.dumps(hostile)})+ ok("POST /gui escapes hostile keys, instructions and criteria",+ r.status_code == 200 and evil not in r.text and "<script>alert" not in r.text+ and "<img src=x onerror=alert(1)>" in r.text, str(r.status_code))++ # The no-JS path refuses what /predict refuses, and the page knows the limits for its own check.+ many = {"q%d" % i: {"type": "noul", "instructions": "?"} for i in range(server.MAX_QUESTIONS + 1)}+ r = client.post("/gui", data={"state": json.dumps({"body": "hi"}), "questions": json.dumps(many)})+ ok("POST /gui refuses more questions than /predict takes", r.status_code == 200+ and "Request too large" in r.text and "too many questions" in r.text, r.text[:300])+ r = client.get("/")+ ok("GET / tells the playground the server's request limits",+ "const LIMITS = " + json.dumps({"questions": server.MAX_QUESTIONS, "stateChars": server.MAX_STATE_CHARS})+ in r.text)++ ok("_pct never rounds a near-certainty to 100% or a long shot to 0%",+ (server._pct(0.0004), server._pct(0.9996), server._pct(0.5), server._pct(0.0), server._pct(1.0))+ == ("<0.1%", ">99.9%", "50.0%", "0.0%", "100.0%"))+ ans = {"type": "choice", "choice": "a", "probabilities": {"a": 0.9999, "b": 0.0001}, "answer_confidence": 0.9999,+ "confidence": 0.9986}+ row = server._answer_row("x", ans, {"type": "choice", "instructions": "?"}, 0)+ ok("_answer_row prints laya's 4 decimals, so a near-certainty does not read 1.000",+ "<b>0.9999</b> calibrated" in row and "<b>0.9986</b>" in row, row)+ ok("_answer_row's tooltip shows field names as code", "<code>answer_confidence</code> is the probability" in row)+ row = server._answer_row("x", None, {"type": "choice", "instructions": "?"}, 0)+ ok("_answer_row shows a non-object answer as raw JSON instead of failing", "class='a-raw'>null</pre>" in row, row)print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))if FAIL: