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(ts): add laya-ts TypeScript inference package (#243)
NLG Sakibec8409e54294
diff --git a/.gitignore b/.gitignoreindex db05504c..79806eb9 100644--- a/.gitignore+++ b/.gitignore@@ -37,6 +37,21 @@ session-*.md*-session.md.claude-session*+# laya-ts: exported weights (regenerate via laya-ts/scripts/export_onnx.py)+model-*/+*.onnx+*.onnx.data++# laya-ts: build output + deps+laya-ts/dist/+node_modules/++# uv lockfile (repo pins via pip/venv, not uv)+uv.lock++# agent process docs (specs/plans) - not shipped+docs/superpowers/+# credentials - never commit.env.env.*diff --git a/laya-ts/README.md b/laya-ts/README.mdnew file mode 100644index 00000000..60a59ee6--- /dev/null+++ b/laya-ts/README.md@@ -0,0 +1,61 @@+# laya-ts++TypeScript inference for Laya (`Agent.predict`, `Router`, `lang`, `email`, `presets`, `shortlist`) on Node and the browser via split ONNX (`encoder.onnx` + `head.onnx`). ESM-only (`"type": "module"`); no CJS build — import from ESM or bundle.++## Export weights (once per checkpoint)++```bash+python laya-ts/scripts/export_onnx.py --model-dir <ckpt> --out-dir ./model+# writes encoder.onnx, head.onnx + copies tokenizer.json, rl_agent_config.json+# verifies torch vs ONNX match within 1e-4 (skip with --no-verify)+```++## Node (CPU/CUDA)++```ts+import { Agent, Router } from "laya-ts";++const agent = await Agent.load("./model"); // local dir, or ("convaiinnovations/laya", { subfolder: "multilingual" })+const router = new Router();+router.attach("english", agent);+const out = await router.predict({ body: "charged twice, refund please" }, {+ intent: { type: "choice", instructions: "What does the customer want?", criteria: { refund: "money back", other: "anything else" } },+});+console.log(out.answers.intent);+```++CUDA: `Agent.load("./model", { device: "cuda" })` (falls back to CPU with a warning).++## Browser (WebGPU → WASM fallback)++```ts+import { Agent } from "laya-ts";++const agent = await Agent.load("https://example.com/models/laya"); // serves encoder.onnx, head.onnx, tokenizer.json, rl_agent_config.json+const out = await agent.predict("charged twice", {+ d: { type: "choice", instructions: "pick", criteria: { refund: "money back", other: "rest" } },+});+```++`onnxruntime-node` / `onnxruntime-web` are optional peer deps, imported lazily behind the provider you use.++## Shortlist (many labels)++```ts+import { shortlistChoice, predictShortlist, embedFnFromAgent } from "laya-ts";++const keep = await shortlistChoice(state, bigCriteriaDict, embedFn, 20);+const out = await predictShortlist(agent, state, questions, embedFn, 20);+// out.shortlist[qid] = { labels, scores, k, n, passthrough }+// embedFnFromAgent(agent) mean-pools the loaded encoder; a dedicated bi-encoder usually shortlists better.+```++## Example (repo root)++```bash+node laya-ts/examples/try-ml.mjs # needs ./model-ml from the export step+```++## Packaging++ponytail: CJS/browser-field dual build + tsconfig tests-include deferred — Task 7 verified ESM-only; CJS needs second tsc config + export-map change, untested. Add when a CJS consumer or browser-field swap is requested.diff --git a/laya-ts/examples/try-ml.mjs b/laya-ts/examples/try-ml.mjsnew file mode 100644index 00000000..cc4ee6f7--- /dev/null+++ b/laya-ts/examples/try-ml.mjs@@ -0,0 +1,36 @@+import { Agent, Router } from "../dist/index.js";++const agent = await Agent.load("./model-ml"); // run as: node laya-ts/examples/try-ml.mjs (repo root)+console.log("loaded:", agent.cfg.encoder ?? "multilingual");++// Direct predict (Hindi -> multilingual weights)+const direct = await agent.predict(+ { body: "मुझसे दो बार शुल्क लिया गया, कृपया पैसे वापस करें।" },+ {+ department: {+ type: "choice",+ instructions: "Which department should handle this request?",+ criteria: {+ billing: "invoices, payments, refunds",+ technical: "bugs, outages, system errors",+ sales: "pricing, new contracts",+ other: "everything else",+ },+ },+ churn_risk: { type: "noul", instructions: "Does the user threaten to cancel or leave?" },+ }+);+console.log(JSON.stringify(direct, null, 2));++// Router path (auto language detect + routing metadata)+const router = new Router();+router.attach("multilingual", agent);+const routed = await router.predict({ body: "Der Kunde wurde zweimal belastet" }, {+ department: {+ type: "choice",+ instructions: "Which department should handle this request?",+ criteria: { billing: "invoices, payments, refunds", technical: "bugs, outages", other: "rest" },+ },+});+console.log("routing:", JSON.stringify(routed.routing));+console.log("answer:", JSON.stringify(routed.answers.department));diff --git a/laya-ts/package-lock.json b/laya-ts/package-lock.jsonnew file mode 100644index 00000000..75bf12f3--- /dev/null+++ b/laya-ts/package-lock.json@@ -0,0 +1,1881 @@+{+ "name": "laya-ts",+ "version": "0.1.0",+ "lockfileVersion": 3,+ "requires": true,+ "packages": {+ "": {+ "name": "laya-ts",+ "version": "0.1.0",+ "devDependencies": {+ "typescript": "^5.6.0",+ "vitest": "^2.1.0"+ },+ "optionalDependencies": {+ "onnxruntime-node": "^1.20.0",+ "onnxruntime-web": "^1.20.0"+ }+ },+ "node_modules/@esbuild/aix-ppc64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz",+ "integrity": "sha512-1SDgH6ZSPTlggy1yI6+Dbkiz8xzpHJEVAlF/AM1tHPLsf5STom9rwtjE4hKAF20FfXXNTFqEYXyJNWh1GiZedQ==",+ "cpu": [+ "ppc64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "aix"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/android-arm": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.21.5.tgz",+ "integrity": "sha512-vCPvzSjpPHEi1siZdlvAlsPxXl7WbOVUBBAowWug4rJHb68Ox8KualB+1ocNvT5fjv6wpkX6o/iEpbDrf68zcg==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/android-arm64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.21.5.tgz",+ "integrity": "sha512-c0uX9VAUBQ7dTDCjq+wdyGLowMdtR/GoC2U5IYk/7D1H1JYC0qseD7+11iMP2mRLN9RcCMRcjC4YMclCzGwS/A==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/android-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.21.5.tgz",+ "integrity": "sha512-D7aPRUUNHRBwHxzxRvp856rjUHRFW1SdQATKXH2hqA0kAZb1hKmi02OpYRacl0TxIGz/ZmXWlbZgjwWYaCakTA==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/darwin-arm64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.21.5.tgz",+ "integrity": "sha512-DwqXqZyuk5AiWWf3UfLiRDJ5EDd49zg6O9wclZ7kUMv2WRFr4HKjXp/5t8JZ11QbQfUS6/cRCKGwYhtNAY88kQ==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/darwin-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.21.5.tgz",+ "integrity": "sha512-se/JjF8NlmKVG4kNIuyWMV/22ZaerB+qaSi5MdrXtd6R08kvs2qCN4C09miupktDitvh8jRFflwGFBQcxZRjbw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/freebsd-arm64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.21.5.tgz",+ "integrity": "sha512-5JcRxxRDUJLX8JXp/wcBCy3pENnCgBR9bN6JsY4OmhfUtIHe3ZW0mawA7+RDAcMLrMIZaf03NlQiX9DGyB8h4g==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/freebsd-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.21.5.tgz",+ "integrity": "sha512-J95kNBj1zkbMXtHVH29bBriQygMXqoVQOQYA+ISs0/2l3T9/kj42ow2mpqerRBxDJnmkUDCaQT/dfNXWX/ZZCQ==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-arm": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.21.5.tgz",+ "integrity": "sha512-bPb5AHZtbeNGjCKVZ9UGqGwo8EUu4cLq68E95A53KlxAPRmUyYv2D6F0uUI65XisGOL1hBP5mTronbgo+0bFcA==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-arm64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.21.5.tgz",+ "integrity": "sha512-ibKvmyYzKsBeX8d8I7MH/TMfWDXBF3db4qM6sy+7re0YXya+K1cem3on9XgdT2EQGMu4hQyZhan7TeQ8XkGp4Q==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-ia32": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.21.5.tgz",+ "integrity": "sha512-YvjXDqLRqPDl2dvRODYmmhz4rPeVKYvppfGYKSNGdyZkA01046pLWyRKKI3ax8fbJoK5QbxblURkwK/MWY18Tg==",+ "cpu": [+ "ia32"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-loong64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.21.5.tgz",+ "integrity": "sha512-uHf1BmMG8qEvzdrzAqg2SIG/02+4/DHB6a9Kbya0XDvwDEKCoC8ZRWI5JJvNdUjtciBGFQ5PuBlpEOXQj+JQSg==",+ "cpu": [+ "loong64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-mips64el": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.21.5.tgz",+ "integrity": "sha512-IajOmO+KJK23bj52dFSNCMsz1QP1DqM6cwLUv3W1QwyxkyIWecfafnI555fvSGqEKwjMXVLokcV5ygHW5b3Jbg==",+ "cpu": [+ "mips64el"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-ppc64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.21.5.tgz",+ "integrity": "sha512-1hHV/Z4OEfMwpLO8rp7CvlhBDnjsC3CttJXIhBi+5Aj5r+MBvy4egg7wCbe//hSsT+RvDAG7s81tAvpL2XAE4w==",+ "cpu": [+ "ppc64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-riscv64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.21.5.tgz",+ "integrity": "sha512-2HdXDMd9GMgTGrPWnJzP2ALSokE/0O5HhTUvWIbD3YdjME8JwvSCnNGBnTThKGEB91OZhzrJ4qIIxk/SBmyDDA==",+ "cpu": [+ "riscv64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-s390x": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.21.5.tgz",+ "integrity": "sha512-zus5sxzqBJD3eXxwvjN1yQkRepANgxE9lgOW2qLnmr8ikMTphkjgXu1HR01K4FJg8h1kEEDAqDcZQtbrRnB41A==",+ "cpu": [+ "s390x"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/linux-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.21.5.tgz",+ "integrity": "sha512-1rYdTpyv03iycF1+BhzrzQJCdOuAOtaqHTWJZCWvijKD2N5Xu0TtVC8/+1faWqcP9iBCWOmjmhoH94dH82BxPQ==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/netbsd-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.21.5.tgz",+ "integrity": "sha512-Woi2MXzXjMULccIwMnLciyZH4nCIMpWQAs049KEeMvOcNADVxo0UBIQPfSmxB3CWKedngg7sWZdLvLczpe0tLg==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "netbsd"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/openbsd-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.21.5.tgz",+ "integrity": "sha512-HLNNw99xsvx12lFBUwoT8EVCsSvRNDVxNpjZ7bPn947b8gJPzeHWyNVhFsaerc0n3TsbOINvRP2byTZ5LKezow==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "openbsd"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/sunos-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.21.5.tgz",+ "integrity": "sha512-6+gjmFpfy0BHU5Tpptkuh8+uw3mnrvgs+dSPQXQOv3ekbordwnzTVEb4qnIvQcYXq6gzkyTnoZ9dZG+D4garKg==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "sunos"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/win32-arm64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.21.5.tgz",+ "integrity": "sha512-Z0gOTd75VvXqyq7nsl93zwahcTROgqvuAcYDUr+vOv8uHhNSKROyU961kgtCD1e95IqPKSQKH7tBTslnS3tA8A==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/win32-ia32": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.21.5.tgz",+ "integrity": "sha512-SWXFF1CL2RVNMaVs+BBClwtfZSvDgtL//G/smwAc5oVK/UPu2Gu9tIaRgFmYFFKrmg3SyAjSrElf0TiJ1v8fYA==",+ "cpu": [+ "ia32"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@esbuild/win32-x64": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.21.5.tgz",+ "integrity": "sha512-tQd/1efJuzPC6rCFwEvLtci/xNFcTZknmXs98FYDfGE4wP9ClFV98nyKrzJKVPMhdDnjzLhdUyMX4PsQAPjwIw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/@jridgewell/sourcemap-codec": {+ "version": "1.6.0",+ "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.6.0.tgz",+ "integrity": "sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/@napi-rs/lzma-linux-x64-gnu": {+ "version": "1.5.1",+ "resolved": "https://registry.npmjs.org/@napi-rs/lzma-linux-x64-gnu/-/lzma-linux-x64-gnu-1.5.1.tgz",+ "integrity": "sha512-oTXEIha4SsuXdTA4Iyskj0kpdx2yVXdhd75c2v3xGrHFfVMsbhTPZU/nMPL4sWKo4pBHm3aucLaqGlF696dTyQ==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": "^22.20 || ^24.12 || >=25"+ }+ },+ "node_modules/@protobufjs/aspromise": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz",+ "integrity": "sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/base64": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@protobufjs/base64/-/base64-1.1.2.tgz",+ "integrity": "sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/codegen": {+ "version": "2.0.5",+ "resolved": "https://registry.npmjs.org/@protobufjs/codegen/-/codegen-2.0.5.tgz",+ "integrity": "sha512-zgXFLzW3Ap33e6d0Wlj4MGIm6Ce8O89n/apUaGNB/jx+hw+ruWEp7EwGUshdLKVRCxZW12fp9r40E1mQrf/34g==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/eventemitter": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/@protobufjs/eventemitter/-/eventemitter-1.1.1.tgz",+ "integrity": "sha512-vW1GmwMZNnL+gMRaovlh9yZX74kc+TTU3FObkkurpMaRtBfLP3ldjS9KQWlwZgraRE0+dheEEoAxdzcJQ8eXZg==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/fetch": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/@protobufjs/fetch/-/fetch-1.1.1.tgz",+ "integrity": "sha512-GpptLrs57adMSuHi3VNj0mAF8dwh36LMaYF6XyJ6JMWlVsc+t42tm1HSEDmOs3A8fC9yyeisgLhsTVQokOZ0zw==",+ "license": "BSD-3-Clause",+ "optional": true,+ "dependencies": {+ "@protobufjs/aspromise": "^1.1.1"+ }+ },+ "node_modules/@protobufjs/float": {+ "version": "1.0.2",+ "resolved": "https://registry.npmjs.org/@protobufjs/float/-/float-1.0.2.tgz",+ "integrity": "sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/path": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@protobufjs/path/-/path-1.1.2.tgz",+ "integrity": "sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/pool": {+ "version": "1.1.0",+ "resolved": "https://registry.npmjs.org/@protobufjs/pool/-/pool-1.1.0.tgz",+ "integrity": "sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@protobufjs/utf8": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.2.tgz",+ "integrity": "sha512-b1UQwcEZ4yCnMCD8DAL1VlbvBJE9/IX4FTIp7BG1xYpf29SLazLSrqUkj4w7Y5y7cCVP6E5tcqqcI0xemPkHug==",+ "license": "BSD-3-Clause",+ "optional": true+ },+ "node_modules/@rollup/rollup-android-arm-eabi": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.63.4.tgz",+ "integrity": "sha512-I+BSHzTAhKN2n7ZwGZsegGcZjDpLqFOMAtJz/u6uFGe0pUFbq56dEHjqJV/ZUdRJtNXNxA+hREUatZBvMR3Oiw==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ]+ },+ "node_modules/@rollup/rollup-android-arm64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.63.4.tgz",+ "integrity": "sha512-pu3BdjS2LtEzRu2elmGzS3fIeWSZy4BMDIaLNwjorO76+k2d0LMluijhsDx3KQyQBQ/lLUZCQA9/s6csvUfuhw==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ]+ },+ "node_modules/@rollup/rollup-darwin-arm64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.63.4.tgz",+ "integrity": "sha512-xfSrj9MHnWK9GaSqT9U0ImHtH/N8WZlHLx4cZHiuLcqs640hvZ3hLPd5UR2AZS57FaE8HrRUSpltbZdWRxHiDA==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ]+ },+ "node_modules/@rollup/rollup-darwin-x64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.63.4.tgz",+ "integrity": "sha512-bqU99PLJb/dqb3S0GIMdeuyAEETSUgZBoqXYd3Sd+WCsV+MmPhnN6JrotWyir31+QgH7EvvE5/mwGJlEoci8Fw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ]+ },+ "node_modules/@rollup/rollup-freebsd-arm64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.63.4.tgz",+ "integrity": "sha512-JinsFZ5G40oXQb+sUuiA5x689vhr6dDYK0H0NL+rwKdL6CqnmYN8PE4ZwfRSoIjrCxqTQG/SLfTtSvHeGxoVlw==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ]+ },+ "node_modules/@rollup/rollup-freebsd-x64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.63.4.tgz",+ "integrity": "sha512-GAdA4UxpiNm27cLHr2GqXBpAD0x9FqwYBY7/YSP0Ss0/PNi4k8gbviqpIpYbVSRBaS2ZcegXEzgTQMbRNCwxCw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ]+ },+ "node_modules/@rollup/rollup-linux-arm-gnueabihf": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.63.4.tgz",+ "integrity": "sha512-qDd6NoA1znaLjp4jR5U/KWCdLAKDJNB8W9ChbbDaKbo0xA+Atln5HK6LFCZ4oJQpemtRZA288DCirFRjrspptw==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-arm-musleabihf": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.63.4.tgz",+ "integrity": "sha512-WtB5Tz5KTNINb8ZA+8sQ7bmjuS1JrRT7YverYIhUGdWWDlpzVWmIwuZE+jidkEXUn1l0zrEkaIMa8dHF3NGcsA==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-arm64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.63.4.tgz",+ "integrity": "sha512-VcQ3L1tjnkKzWjryAVaFhHEWcqOfICX9uxVVoDzm2t0DpgKRHd2zOpVrJc0xsWeBZcBFyYROCIBdyR/fS174pg==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-arm64-musl": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.63.4.tgz",+ "integrity": "sha512-6+ZQX6P5s0cMDN2Ypb8Lbm2+/sZYmZjdaYny992ujUU9UKi/4CWoJWsl1pNvjWJHNHGK51m+jKGLlh1ylb2ifQ==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-loong64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-gnu/-/rollup-linux-loong64-gnu-4.63.4.tgz",+ "integrity": "sha512-D72ZnvkFkBXOfzMMQLcwfPLyGkKb7HZ9/mf97B7v6/P5Lbv4oFOtSY/uHbS8lH6uKUOxoKiuokdb50XZSzzbJw==",+ "cpu": [+ "loong64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-loong64-musl": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-musl/-/rollup-linux-loong64-musl-4.63.4.tgz",+ "integrity": "sha512-piU6BxeqA3O9KSu3kRCIQQtNqFFaTu21SEV4FwaRZowpnj3bLaWPZHw+xFqCs0XlJ+aOH3PTRWGoglH+mKA/OA==",+ "cpu": [+ "loong64"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-ppc64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-gnu/-/rollup-linux-ppc64-gnu-4.63.4.tgz",+ "integrity": "sha512-/5PGpHwqt2EEEOUs1XwzubE/ucr0dWDQ+to3zqi4Ds7EWpwtQ79wXc4JBoxqj/OwpawTsKWzJxHfSuBOq3DrWA==",+ "cpu": [+ "ppc64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-ppc64-musl": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-musl/-/rollup-linux-ppc64-musl-4.63.4.tgz",+ "integrity": "sha512-cX3beZDLWt7G2oJF+nhChiT+qtaihs+S2xi7ziGmVB+2pwPng6D0Ed0HmElQOgv2UsUmSJJLGwpBao/3TDx3VA==",+ "cpu": [+ "ppc64"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-riscv64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.63.4.tgz",+ "integrity": "sha512-1uz2mGWHyptR7DgHHrlbdRAjXK7v7elGZ9lMja910/RP+ZYbX6xAmCiU9UZSX4hqmgtHMv6lr5l3kq1HIOpcag==",+ "cpu": [+ "riscv64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-riscv64-musl": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.63.4.tgz",+ "integrity": "sha512-nLS8topojxyz7SRpKR2IODRpQ0XPZ+xaOXvT3+hqK/Uy8Lo5HFgkkIBiIrCu5tL5YqzTvgovGw55PwpahTAGig==",+ "cpu": [+ "riscv64"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-s390x-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.63.4.tgz",+ "integrity": "sha512-gs7DRKotr3l3q+jGPQBjH0ng1FjlEDm5ueQrkw5JtQvtLyEIcLASqAEaor56BhkKRzk+IcQzrcanBdb/bBQn8g==",+ "cpu": [+ "s390x"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-x64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.63.4.tgz",+ "integrity": "sha512-791ET7W17NnScOZM7h4dX5hYspxE28htPFsb1awY/NRR8+PRNkS53e475rDdxXXDrP+kwnCcNWg9CX5ztn/Aqw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-linux-x64-musl": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.63.4.tgz",+ "integrity": "sha512-iwZQRcmj7g88g3tzefIrQY7qvmuA/cfYwhrDtTBhsmukO4U2huVO5W+86XacUMRvdSFVAc6kZUZy21JaRwiB9w==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@rollup/rollup-openbsd-x64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-openbsd-x64/-/rollup-openbsd-x64-4.63.4.tgz",+ "integrity": "sha512-dVHFp9gRWrdTpnqQuGfCwd7hOQDatK1VCP2iWhLY/cGrOQs/ucFzJ6A5SRqbXX12ZDI8EUuejSM5kwg+ja7Png==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "openbsd"+ ]+ },+ "node_modules/@rollup/rollup-openharmony-arm64": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-openharmony-arm64/-/rollup-openharmony-arm64-4.63.4.tgz",+ "integrity": "sha512-t3NlauOW6gxZVVFcBEnO62Cb4wbyDFL416gTg1uFI/2tgqYQlf69FbSE115Ajre9I+c26Lk4mcmdFUsS/DGifQ==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "openharmony"+ ]+ },+ "node_modules/@rollup/rollup-win32-arm64-msvc": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.63.4.tgz",+ "integrity": "sha512-xWuIaSye5FWZF8+UYtVEcHtRJDN5kN9Kfgxx3Kq8XIov9KSKbc1fiqQCm90SKrgQbUXZelbnUhnlUJmfSE7P9A==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ]+ },+ "node_modules/@rollup/rollup-win32-ia32-msvc": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.63.4.tgz",+ "integrity": "sha512-9ALJJUOg/ZflMJepVo2PlgsGxSaxN7SQ4Z8GoZfVlarWr6r3rkHUNsd/zAio7p4YMtChSMXPionxej4Hkf6CXQ==",+ "cpu": [+ "ia32"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ]+ },+ "node_modules/@rollup/rollup-win32-x64-gnu": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-gnu/-/rollup-win32-x64-gnu-4.63.4.tgz",+ "integrity": "sha512-blj9z5qx/Pv4WU0W1NMFDB97e0JH5ed+aZGywW8WCvp/NhWX/4PFAq5uu6Q0AebNn+Vo6KzUYDT++JzTT5ojlQ==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ]+ },+ "node_modules/@rollup/rollup-win32-x64-msvc": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.63.4.tgz",+ "integrity": "sha512-Erx822VRBwLa124shbj+wNXe//BOgMEctDV0m1aqTQdNO1S69DgNUCFKC1RCeZfixs1J31l6igk1ziyXErbigQ==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ]+ },+ "node_modules/@types/estree": {+ "version": "1.0.9",+ "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",+ "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/@types/node": {+ "version": "26.6.2",+ "resolved": "https://registry.npmjs.org/@types/node/-/node-26.6.2.tgz",+ "integrity": "sha512-X1P21scMv4zGKLYqjdGjaKa7COa0RKVYYZZN/NfvLQ1JegxFhdhpZG/Lyn8AXx6CDUavKAd11v6BvfpkDByK8g==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "undici-types": "~8.9.0"+ }+ },+ "node_modules/@vitest/expect": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-2.1.9.tgz",+ "integrity": "sha512-UJCIkTBenHeKT1TTlKMJWy1laZewsRIzYighyYiJKZreqtdxSos/S1t+ktRMQWu2CKqaarrkeszJx1cgC5tGZw==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/spy": "2.1.9",+ "@vitest/utils": "2.1.9",+ "chai": "^5.1.2",+ "tinyrainbow": "^1.2.0"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/@vitest/mocker": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-2.1.9.tgz",+ "integrity": "sha512-tVL6uJgoUdi6icpxmdrn5YNo3g3Dxv+IHJBr0GXHaEdTcw3F+cPKnsXFhli6nO+f/6SDKPHEK1UN+k+TQv0Ehg==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/spy": "2.1.9",+ "estree-walker": "^3.0.3",+ "magic-string": "^0.30.12"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ },+ "peerDependencies": {+ "msw": "^2.4.9",+ "vite": "^5.0.0"+ },+ "peerDependenciesMeta": {+ "msw": {+ "optional": true+ },+ "vite": {+ "optional": true+ }+ }+ },+ "node_modules/@vitest/pretty-format": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-2.1.9.tgz",+ "integrity": "sha512-KhRIdGV2U9HOUzxfiHmY8IFHTdqtOhIzCpd8WRdJiE7D/HUcZVD0EgQCVjm+Q9gkUXWgBvMmTtZgIG48wq7sOQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "tinyrainbow": "^1.2.0"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/@vitest/runner": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-2.1.9.tgz",+ "integrity": "sha512-ZXSSqTFIrzduD63btIfEyOmNcBmQvgOVsPNPe0jYtESiXkhd8u2erDLnMxmGrDCwHCCHE7hxwRDCT3pt0esT4g==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/utils": "2.1.9",+ "pathe": "^1.1.2"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/@vitest/snapshot": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-2.1.9.tgz",+ "integrity": "sha512-oBO82rEjsxLNJincVhLhaxxZdEtV0EFHMK5Kmx5sJ6H9L183dHECjiefOAdnqpIgT5eZwT04PoggUnW88vOBNQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/pretty-format": "2.1.9",+ "magic-string": "^0.30.12",+ "pathe": "^1.1.2"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/@vitest/spy": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-2.1.9.tgz",+ "integrity": "sha512-E1B35FwzXXTs9FHNK6bDszs7mtydNi5MIfUWpceJ8Xbfb1gBMscAnwLbEu+B44ed6W3XjL9/ehLPHR1fkf1KLQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "tinyspy": "^3.0.2"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/@vitest/utils": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-2.1.9.tgz",+ "integrity": "sha512-v0psaMSkNJ3A2NMrUEHFRzJtDPFn+/VWZ5WxImB21T9fjucJRmS7xCS3ppEnARb9y11OAzaD+P2Ps+b+BGX5iQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/pretty-format": "2.1.9",+ "loupe": "^3.1.2",+ "tinyrainbow": "^1.2.0"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/adm-zip": {+ "version": "0.6.1",+ "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.6.1.tgz",+ "integrity": "sha512-Xwrja8nx9e5o2N1my4DsKCeKpdrnACyr1wtbPxBDgGzKzKyE9kRtBFA8mWldI+RVlD7CBZNWY/wQ2+ydwOR6kQ==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">=14.0"+ }+ },+ "node_modules/assertion-error": {+ "version": "2.0.1",+ "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz",+ "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/cac": {+ "version": "6.7.14",+ "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz",+ "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=8"+ }+ },+ "node_modules/chai": {+ "version": "5.3.3",+ "resolved": "https://registry.npmjs.org/chai/-/chai-5.3.3.tgz",+ "integrity": "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "assertion-error": "^2.0.1",+ "check-error": "^2.1.1",+ "deep-eql": "^5.0.1",+ "loupe": "^3.1.0",+ "pathval": "^2.0.0"+ },+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/check-error": {+ "version": "2.1.3",+ "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.3.tgz",+ "integrity": "sha512-PAJdDJusoxnwm1VwW07VWwUN1sl7smmC3OKggvndJFadxxDRyFJBX/ggnu/KE4kQAB7a3Dp8f/YXC1FlUprWmA==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">= 16"+ }+ },+ "node_modules/debug": {+ "version": "4.4.3",+ "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz",+ "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "ms": "^2.1.3"+ },+ "engines": {+ "node": ">=6.0"+ },+ "peerDependenciesMeta": {+ "supports-color": {+ "optional": true+ }+ }+ },+ "node_modules/deep-eql": {+ "version": "5.0.2",+ "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz",+ "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=6"+ }+ },+ "node_modules/define-data-property": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz",+ "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "es-define-property": "^1.0.0",+ "es-errors": "^1.3.0",+ "gopd": "^1.0.1"+ },+ "engines": {+ "node": ">= 0.4"+ },+ "funding": {+ "url": "https://github.com/sponsors/ljharb"+ }+ },+ "node_modules/define-properties": {+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.2.1.tgz",+ "integrity": "sha512-8QmQKqEASLd5nx0U1B1okLElbUuuttJ/AnYmRXbbbGDWh6uS208EjD4Xqq/I9wK7u0v6O08XhTWnt5XtEbR6Dg==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "define-data-property": "^1.0.1",+ "has-property-descriptors": "^1.0.0",+ "object-keys": "^1.1.1"+ },+ "engines": {+ "node": ">= 0.4"+ },+ "funding": {+ "url": "https://github.com/sponsors/ljharb"+ }+ },+ "node_modules/es-define-property": {+ "version": "1.0.1",+ "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",+ "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">= 0.4"+ }+ },+ "node_modules/es-errors": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",+ "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">= 0.4"+ }+ },+ "node_modules/es-module-lexer": {+ "version": "1.7.0",+ "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.7.0.tgz",+ "integrity": "sha512-jEQoCwk8hyb2AZziIOLhDqpm5+2ww5uIE6lkO/6jcOCusfk6LhMHpXXfBLXTZ7Ydyt0j4VoUQv6uGNYbdW+kBA==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/esbuild": {+ "version": "0.21.5",+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz",+ "integrity": "sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==",+ "dev": true,+ "hasInstallScript": true,+ "license": "MIT",+ "bin": {+ "esbuild": "bin/esbuild"+ },+ "engines": {+ "node": ">=12"+ },+ "optionalDependencies": {+ "@esbuild/aix-ppc64": "0.21.5",+ "@esbuild/android-arm": "0.21.5",+ "@esbuild/android-arm64": "0.21.5",+ "@esbuild/android-x64": "0.21.5",+ "@esbuild/darwin-arm64": "0.21.5",+ "@esbuild/darwin-x64": "0.21.5",+ "@esbuild/freebsd-arm64": "0.21.5",+ "@esbuild/freebsd-x64": "0.21.5",+ "@esbuild/linux-arm": "0.21.5",+ "@esbuild/linux-arm64": "0.21.5",+ "@esbuild/linux-ia32": "0.21.5",+ "@esbuild/linux-loong64": "0.21.5",+ "@esbuild/linux-mips64el": "0.21.5",+ "@esbuild/linux-ppc64": "0.21.5",+ "@esbuild/linux-riscv64": "0.21.5",+ "@esbuild/linux-s390x": "0.21.5",+ "@esbuild/linux-x64": "0.21.5",+ "@esbuild/netbsd-x64": "0.21.5",+ "@esbuild/openbsd-x64": "0.21.5",+ "@esbuild/sunos-x64": "0.21.5",+ "@esbuild/win32-arm64": "0.21.5",+ "@esbuild/win32-ia32": "0.21.5",+ "@esbuild/win32-x64": "0.21.5"+ }+ },+ "node_modules/escape-string-regexp": {+ "version": "4.0.0",+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",+ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">=10"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/estree-walker": {+ "version": "3.0.3",+ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz",+ "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@types/estree": "^1.0.0"+ }+ },+ "node_modules/expect-type": {+ "version": "1.4.0",+ "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",+ "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==",+ "dev": true,+ "license": "Apache-2.0",+ "engines": {+ "node": ">=12.0.0"+ }+ },+ "node_modules/flatbuffers": {+ "version": "25.9.23",+ "resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-25.9.23.tgz",+ "integrity": "sha512-MI1qs7Lo4Syw0EOzUl0xjs2lsoeqFku44KpngfIduHBYvzm8h2+7K8YMQh1JtVVVrUvhLpNwqVi4DERegUJhPQ==",+ "license": "Apache-2.0",+ "optional": true+ },+ "node_modules/fsevents": {+ "version": "2.3.3",+ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",+ "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",+ "dev": true,+ "hasInstallScript": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": "^8.16.0 || ^10.6.0 || >=11.0.0"+ }+ },+ "node_modules/global-agent": {+ "version": "4.1.3",+ "resolved": "https://registry.npmjs.org/global-agent/-/global-agent-4.1.3.tgz",+ "integrity": "sha512-KUJEViiuFT3I97t+GYMikLPJS2Lfo/S2F+DQuBWzuzaMPnvt5yyZePzArx36fBzpGTxZjIpDbXLeySLgh+k76g==",+ "license": "BSD-3-Clause",+ "optional": true,+ "dependencies": {+ "globalthis": "^1.0.2",+ "matcher": "^4.0.0",+ "semver": "^7.3.5",+ "serialize-error": "^8.1.0"+ },+ "engines": {+ "node": ">=10.0"+ }+ },+ "node_modules/globalthis": {+ "version": "1.0.4",+ "resolved": "https://registry.npmjs.org/globalthis/-/globalthis-1.0.4.tgz",+ "integrity": "sha512-DpLKbNU4WylpxJykQujfCcwYWiV/Jhm50Goo0wrVILAv5jOr9d+H+UR3PhSCD2rCCEIg0uc+G+muBTwD54JhDQ==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "define-properties": "^1.2.1",+ "gopd": "^1.0.1"+ },+ "engines": {+ "node": ">= 0.4"+ },+ "funding": {+ "url": "https://github.com/sponsors/ljharb"+ }+ },+ "node_modules/gopd": {+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",+ "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">= 0.4"+ },+ "funding": {+ "url": "https://github.com/sponsors/ljharb"+ }+ },+ "node_modules/guid-typescript": {+ "version": "1.0.9",+ "resolved": "https://registry.npmjs.org/guid-typescript/-/guid-typescript-1.0.9.tgz",+ "integrity": "sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==",+ "license": "ISC",+ "optional": true+ },+ "node_modules/has-property-descriptors": {+ "version": "1.0.2",+ "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz",+ "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "es-define-property": "^1.0.0"+ },+ "funding": {+ "url": "https://github.com/sponsors/ljharb"+ }+ },+ "node_modules/long": {+ "version": "5.3.2",+ "resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz",+ "integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==",+ "license": "Apache-2.0",+ "optional": true+ },+ "node_modules/loupe": {+ "version": "3.2.1",+ "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.2.1.tgz",+ "integrity": "sha512-CdzqowRJCeLU72bHvWqwRBBlLcMEtIvGrlvef74kMnV2AolS9Y8xUv1I0U/MNAWMhBlKIoyuEgoJ0t/bbwHbLQ==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/magic-string": {+ "version": "0.30.21",+ "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",+ "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@jridgewell/sourcemap-codec": "^1.5.5"+ }+ },+ "node_modules/matcher": {+ "version": "4.0.0",+ "resolved": "https://registry.npmjs.org/matcher/-/matcher-4.0.0.tgz",+ "integrity": "sha512-S6x5wmcDmsDRRU/c2dkccDwQPXoFczc5+HpQ2lON8pnvHlnvHAHj5WlLVvw6n6vNyHuVugYrFohYxbS+pvFpKQ==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "escape-string-regexp": "^4.0.0"+ },+ "engines": {+ "node": ">=10"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/ms": {+ "version": "2.1.3",+ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",+ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/nanoid": {+ "version": "3.3.19",+ "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.19.tgz",+ "integrity": "sha512-Y2tUNy4ouw6tq5oDSKeQYGOyhkUBhNOcGV/02KC+6kd9eDGqdZd++mjMiIDilrBYvjEnCYvVtsuHCuP+okSfug==",+ "dev": true,+ "funding": [+ {+ "type": "github",+ "url": "https://github.com/sponsors/ai"+ }+ ],+ "license": "MIT",+ "bin": {+ "nanoid": "bin/nanoid.cjs"+ },+ "engines": {+ "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"+ }+ },+ "node_modules/object-keys": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",+ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==",+ "license": "MIT",+ "optional": true,+ "engines": {+ "node": ">= 0.4"+ }+ },+ "node_modules/onnxruntime-common": {+ "version": "1.30.0",+ "resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.30.0.tgz",+ "integrity": "sha512-7fdVWjAID1dVhH/G8qK3APARunV4VkBFoCQAP7qp4Wkab0mrorvmc+sqiT+mKXOzDqdjN5j+/Z9nb4gzNPWcyA==",+ "license": "MIT",+ "optional": true+ },+ "node_modules/onnxruntime-node": {+ "version": "1.30.0",+ "resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.30.0.tgz",+ "integrity": "sha512-twhs1C2C/BFkz1yc5OY0KIU2GUq6DURO7hD4bx5Q2Qy3nAMJwRXW8xU3NVczE29VA9lolLOYepoD8fjTGOfIqw==",+ "hasInstallScript": true,+ "license": "MIT",+ "optional": true,+ "os": [+ "win32",+ "darwin",+ "linux"+ ],+ "dependencies": {+ "adm-zip": "^0.6.0",+ "global-agent": "^4.1.3",+ "onnxruntime-common": "1.30.0"+ }+ },+ "node_modules/onnxruntime-web": {+ "version": "1.30.0",+ "resolved": "https://registry.npmjs.org/onnxruntime-web/-/onnxruntime-web-1.30.0.tgz",+ "integrity": "sha512-q0y+JrrtukXSzsBWEMccVfqX25LRmosXHF+CaRJmg8pZClzcV7svNc4rKY3jL02Vb7QmRMDs1SigqR4CXAfKYQ==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "flatbuffers": "^25.1.24",+ "guid-typescript": "^1.0.9",+ "long": "^5.2.3",+ "onnxruntime-common": "1.30.0",+ "platform": "^1.3.6",+ "protobufjs": "^7.2.4"+ }+ },+ "node_modules/pathe": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz",+ "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/pathval": {+ "version": "2.0.1",+ "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.1.tgz",+ "integrity": "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">= 14.16"+ }+ },+ "node_modules/picocolors": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",+ "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==",+ "dev": true,+ "license": "ISC"+ },+ "node_modules/platform": {+ "version": "1.3.6",+ "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz",+ "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==",+ "license": "MIT",+ "optional": true+ },+ "node_modules/postcss": {+ "version": "8.5.28",+ "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.28.tgz",+ "integrity": "sha512-RRuzqDtt5Y9h3quz5hWhK+TPnsmVs6WwSU6LkJMeY4HstUEDuYTG8UJSdawMRzmzAtV+KEoG8N3Qg2qLy5vM/A==",+ "dev": true,+ "funding": [+ {+ "type": "opencollective",+ "url": "https://opencollective.com/postcss/"+ },+ {+ "type": "tidelift",+ "url": "https://tidelift.com/funding/github/npm/postcss"+ },+ {+ "type": "github",+ "url": "https://github.com/sponsors/ai"+ }+ ],+ "license": "MIT",+ "dependencies": {+ "nanoid": "^3.3.18",+ "picocolors": "^1.1.1",+ "source-map-js": "^1.2.1"+ },+ "engines": {+ "node": "^10 || ^12 || >=14"+ }+ },+ "node_modules/protobufjs": {+ "version": "7.6.6",+ "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.6.6.tgz",+ "integrity": "sha512-dYDWdjSl5RNb7SgPxGQcRU+GtvP7s2fpkrY0r432PcOIaZ0/rBcxEZnQN67iJhFuQiVw754JDoPruPCNdGsbjg==",+ "hasInstallScript": true,+ "license": "BSD-3-Clause",+ "optional": true,+ "dependencies": {+ "@protobufjs/aspromise": "^1.1.2",+ "@protobufjs/base64": "^1.1.2",+ "@protobufjs/codegen": "^2.0.5",+ "@protobufjs/eventemitter": "^1.1.1",+ "@protobufjs/fetch": "^1.1.1",+ "@protobufjs/float": "^1.0.2",+ "@protobufjs/path": "^1.1.2",+ "@protobufjs/pool": "^1.1.0",+ "@protobufjs/utf8": "^1.1.1",+ "@types/node": ">=13.7.0",+ "long": "^5.3.2"+ },+ "engines": {+ "node": ">=12.0.0"+ }+ },+ "node_modules/rollup": {+ "version": "4.63.4",+ "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.63.4.tgz",+ "integrity": "sha512-4U0liVayNIoLp3GFl1FcI8561WepLnZ1rqfraGh7S9B3Ur5F9S283y8Futii7RUU2C/97tOBmBy7nYvhoiOpbQ==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@types/estree": "1.0.9"+ },+ "bin": {+ "rollup": "dist/bin/rollup"+ },+ "engines": {+ "node": ">=18.0.0",+ "npm": ">=8.0.0"+ },+ "optionalDependencies": {+ "@napi-rs/lzma-linux-x64-gnu": "1.5.1",+ "@rollup/rollup-android-arm-eabi": "4.63.4",+ "@rollup/rollup-android-arm64": "4.63.4",+ "@rollup/rollup-darwin-arm64": "4.63.4",+ "@rollup/rollup-darwin-x64": "4.63.4",+ "@rollup/rollup-freebsd-arm64": "4.63.4",+ "@rollup/rollup-freebsd-x64": "4.63.4",+ "@rollup/rollup-linux-arm-gnueabihf": "4.63.4",+ "@rollup/rollup-linux-arm-musleabihf": "4.63.4",+ "@rollup/rollup-linux-arm64-gnu": "4.63.4",+ "@rollup/rollup-linux-arm64-musl": "4.63.4",+ "@rollup/rollup-linux-loong64-gnu": "4.63.4",+ "@rollup/rollup-linux-loong64-musl": "4.63.4",+ "@rollup/rollup-linux-ppc64-gnu": "4.63.4",+ "@rollup/rollup-linux-ppc64-musl": "4.63.4",+ "@rollup/rollup-linux-riscv64-gnu": "4.63.4",+ "@rollup/rollup-linux-riscv64-musl": "4.63.4",+ "@rollup/rollup-linux-s390x-gnu": "4.63.4",+ "@rollup/rollup-linux-x64-gnu": "4.63.4",+ "@rollup/rollup-linux-x64-musl": "4.63.4",+ "@rollup/rollup-openbsd-x64": "4.63.4",+ "@rollup/rollup-openharmony-arm64": "4.63.4",+ "@rollup/rollup-win32-arm64-msvc": "4.63.4",+ "@rollup/rollup-win32-ia32-msvc": "4.63.4",+ "@rollup/rollup-win32-x64-gnu": "4.63.4",+ "@rollup/rollup-win32-x64-msvc": "4.63.4",+ "fsevents": "~2.3.2"+ }+ },+ "node_modules/semver": {+ "version": "7.8.5",+ "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz",+ "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==",+ "license": "ISC",+ "optional": true,+ "bin": {+ "semver": "bin/semver.js"+ },+ "engines": {+ "node": ">=10"+ }+ },+ "node_modules/serialize-error": {+ "version": "8.1.0",+ "resolved": "https://registry.npmjs.org/serialize-error/-/serialize-error-8.1.0.tgz",+ "integrity": "sha512-3NnuWfM6vBYoy5gZFvHiYsVbafvI9vZv/+jlIigFn4oP4zjNPK3LhcY0xSCgeb1a5L8jO71Mit9LlNoi2UfDDQ==",+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "type-fest": "^0.20.2"+ },+ "engines": {+ "node": ">=10"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/siginfo": {+ "version": "2.0.0",+ "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz",+ "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==",+ "dev": true,+ "license": "ISC"+ },+ "node_modules/source-map-js": {+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz",+ "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==",+ "dev": true,+ "license": "BSD-3-Clause",+ "engines": {+ "node": ">=0.10.0"+ }+ },+ "node_modules/stackback": {+ "version": "0.0.2",+ "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz",+ "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/std-env": {+ "version": "3.10.0",+ "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.10.0.tgz",+ "integrity": "sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/tinybench": {+ "version": "2.9.0",+ "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz",+ "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/tinyexec": {+ "version": "0.3.2",+ "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-0.3.2.tgz",+ "integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/tinypool": {+ "version": "1.1.1",+ "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-1.1.1.tgz",+ "integrity": "sha512-Zba82s87IFq9A9XmjiX5uZA/ARWDrB03OHlq+Vw1fSdt0I+4/Kutwy8BP4Y/y/aORMo61FQ0vIb5j44vSo5Pkg==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": "^18.0.0 || >=20.0.0"+ }+ },+ "node_modules/tinyrainbow": {+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-1.2.0.tgz",+ "integrity": "sha512-weEDEq7Z5eTHPDh4xjX789+fHfF+P8boiFB+0vbWzpbnbsEr/GRaohi/uMKxg8RZMXnl1ItAi/IUHWMsjDV7kQ==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=14.0.0"+ }+ },+ "node_modules/tinyspy": {+ "version": "3.0.2",+ "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-3.0.2.tgz",+ "integrity": "sha512-n1cw8k1k0x4pgA2+9XrOkFydTerNcJ1zWCO5Nn9scWHTD+5tp8dghT2x1uduQePZTZgd3Tupf+x9BxJjeJi77Q==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=14.0.0"+ }+ },+ "node_modules/type-fest": {+ "version": "0.20.2",+ "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz",+ "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==",+ "license": "(MIT OR CC0-1.0)",+ "optional": true,+ "engines": {+ "node": ">=10"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"+ }+ },+ "node_modules/typescript": {+ "version": "5.9.3",+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",+ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",+ "dev": true,+ "license": "Apache-2.0",+ "bin": {+ "tsc": "bin/tsc",+ "tsserver": "bin/tsserver"+ },+ "engines": {+ "node": ">=14.17"+ }+ },+ "node_modules/undici-types": {+ "version": "8.9.0",+ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.9.0.tgz",+ "integrity": "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg==",+ "license": "MIT",+ "optional": true+ },+ "node_modules/vite": {+ "version": "5.4.21",+ "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.21.tgz",+ "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "esbuild": "^0.21.3",+ "postcss": "^8.4.43",+ "rollup": "^4.20.0"+ },+ "bin": {+ "vite": "bin/vite.js"+ },+ "engines": {+ "node": "^18.0.0 || >=20.0.0"+ },+ "funding": {+ "url": "https://github.com/vitejs/vite?sponsor=1"+ },+ "optionalDependencies": {+ "fsevents": "~2.3.3"+ },+ "peerDependencies": {+ "@types/node": "^18.0.0 || >=20.0.0",+ "less": "*",+ "lightningcss": "^1.21.0",+ "sass": "*",+ "sass-embedded": "*",+ "stylus": "*",+ "sugarss": "*",+ "terser": "^5.4.0"+ },+ "peerDependenciesMeta": {+ "@types/node": {+ "optional": true+ },+ "less": {+ "optional": true+ },+ "lightningcss": {+ "optional": true+ },+ "sass": {+ "optional": true+ },+ "sass-embedded": {+ "optional": true+ },+ "stylus": {+ "optional": true+ },+ "sugarss": {+ "optional": true+ },+ "terser": {+ "optional": true+ }+ }+ },+ "node_modules/vite-node": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-2.1.9.tgz",+ "integrity": "sha512-AM9aQ/IPrW/6ENLQg3AGY4K1N2TGZdR5e4gu/MmmR2xR3Ll1+dib+nook92g4TV3PXVyeyxdWwtaCAiUL0hMxA==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "cac": "^6.7.14",+ "debug": "^4.3.7",+ "es-module-lexer": "^1.5.4",+ "pathe": "^1.1.2",+ "vite": "^5.0.0"+ },+ "bin": {+ "vite-node": "vite-node.mjs"+ },+ "engines": {+ "node": "^18.0.0 || >=20.0.0"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ }+ },+ "node_modules/vitest": {+ "version": "2.1.9",+ "resolved": "https://registry.npmjs.org/vitest/-/vitest-2.1.9.tgz",+ "integrity": "sha512-MSmPM9REYqDGBI8439mA4mWhV5sKmDlBKWIYbA3lRb2PTHACE0mgKwA8yQ2xq9vxDTuk4iPrECBAEW2aoFXY0Q==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@vitest/expect": "2.1.9",+ "@vitest/mocker": "2.1.9",+ "@vitest/pretty-format": "^2.1.9",+ "@vitest/runner": "2.1.9",+ "@vitest/snapshot": "2.1.9",+ "@vitest/spy": "2.1.9",+ "@vitest/utils": "2.1.9",+ "chai": "^5.1.2",+ "debug": "^4.3.7",+ "expect-type": "^1.1.0",+ "magic-string": "^0.30.12",+ "pathe": "^1.1.2",+ "std-env": "^3.8.0",+ "tinybench": "^2.9.0",+ "tinyexec": "^0.3.1",+ "tinypool": "^1.0.1",+ "tinyrainbow": "^1.2.0",+ "vite": "^5.0.0",+ "vite-node": "2.1.9",+ "why-is-node-running": "^2.3.0"+ },+ "bin": {+ "vitest": "vitest.mjs"+ },+ "engines": {+ "node": "^18.0.0 || >=20.0.0"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"+ },+ "peerDependencies": {+ "@edge-runtime/vm": "*",+ "@types/node": "^18.0.0 || >=20.0.0",+ "@vitest/browser": "2.1.9",+ "@vitest/ui": "2.1.9",+ "happy-dom": "*",+ "jsdom": "*"+ },+ "peerDependenciesMeta": {+ "@edge-runtime/vm": {+ "optional": true+ },+ "@types/node": {+ "optional": true+ },+ "@vitest/browser": {+ "optional": true+ },+ "@vitest/ui": {+ "optional": true+ },+ "happy-dom": {+ "optional": true+ },+ "jsdom": {+ "optional": true+ }+ }+ },+ "node_modules/why-is-node-running": {+ "version": "2.3.0",+ "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz",+ "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "siginfo": "^2.0.0",+ "stackback": "0.0.2"+ },+ "bin": {+ "why-is-node-running": "cli.js"+ },+ "engines": {+ "node": ">=8"+ }+ }+ }+}diff --git a/laya-ts/package.json b/laya-ts/package.jsonnew file mode 100644index 00000000..42e04ca4--- /dev/null+++ b/laya-ts/package.json@@ -0,0 +1,13 @@+{+ "name": "laya-ts",+ "version": "0.1.0",+ "type": "module",+ "main": "./dist/index.js",+ "module": "./dist/index.js",+ "types": "./dist/index.d.ts",+ "exports": { ".": { "import": "./dist/index.js", "types": "./dist/index.d.ts" } },+ "scripts": { "build": "tsc -p tsconfig.json", "test": "vitest" },+ "files": ["dist", "scripts"],+ "devDependencies": { "typescript": "^5.6.0", "vitest": "^2.1.0" },+ "optionalDependencies": { "onnxruntime-node": "^1.20.0", "onnxruntime-web": "^1.20.0" }+}diff --git a/laya-ts/scripts/export_onnx.py b/laya-ts/scripts/export_onnx.pynew file mode 100644index 00000000..b1b2b9d2--- /dev/null+++ b/laya-ts/scripts/export_onnx.py@@ -0,0 +1,250 @@+"""Export a Laya checkpoint to split ONNX (encoder.onnx + head.onnx).++Source of truth stays ``model.safetensors``; this script produces the two ONNX+files the ``laya-ts`` Node/Web providers load. Run once per checkpoint:+ python laya-ts/scripts/export_onnx.py --repo convaiinnovations/laya --out-dir ./model+ python laya-ts/scripts/export_onnx.py --repo convaiinnovations/laya --subfolder multilingual --out-dir ./model-ml+ python laya-ts/scripts/export_onnx.py --model-dir <local ckpt> --out-dir ./model++With ``--repo`` the checkpoint is downloaded from Hugging Face (safetensors ++tokenizer + encoder) into the HF cache first, then converted. With+``--model-dir`` an already-downloaded directory is used directly.++The encoder graph takes ``(input_ids, attention_mask)`` and returns+``last_hidden_state``. The head graph takes ``(hidden_states, marker_pos,+marker_mask, qtype)`` and returns ``(logits, act_logits)``. Verification runs+a torch forward at ``--seq-len`` and at ``--verify-len`` (default 512, past the+local_attention=128 sliding window) and requires the ONNX outputs to match+within 1e-4 at both lengths before anything is written.+"""+import argparse+import json+import os+import shutil++try:+ from laya.common import build_model+except ImportError: # --help must work without torch installed+ build_model = None++DEFAULT_REPO = "convaiinnovations/laya"+++def _download(repo, subfolder=None, token=None):+ """Download a checkpoint from HF into the HF cache; return the checkpoint dir."""+ # Windows without Developer Mode can't symlink the HF cache; the hub prints+ # a long warning and falls back to copies. Silence it, copies still work.+ os.environ.setdefault("HF_HUB_DISABLE_SYMLINKS_WARNING", "1")+ try:+ from huggingface_hub import snapshot_download+ except ImportError:+ raise SystemExit("download needs huggingface_hub: pip install huggingface_hub")+ prefix = ("%s/" % subfolder) if subfolder else ""+ allow = [prefix + name for name in (+ "rl_agent_config.json", "model.safetensors", "config.json",+ "tokenizer.json", "tokenizer/*", "encoder/*",+ )]+ root = snapshot_download(repo, allow_patterns=allow,+ token=token or os.environ.get("HF_TOKEN"))+ path = os.path.join(root, subfolder) if subfolder else root+ if not os.path.isdir(path):+ raise SystemExit("subfolder %r not found in %r" % (subfolder, repo))+ return path+++def _ensure_tokenizer_json(model_dir):+ """Copies tokenizer/tokenizer.json up to the checkpoint root when needed."""+ dest = os.path.join(model_dir, "tokenizer.json")+ if not os.path.exists(dest):+ src = os.path.join(model_dir, "tokenizer", "tokenizer.json")+ if os.path.exists(src):+ shutil.copy(src, dest)+++def parse_args(argv=None):+ p = argparse.ArgumentParser(description=__doc__)+ src = p.add_mutually_exclusive_group(required=True)+ src.add_argument("--repo", help="HF repo to download first (default %s with --model-dir absent)." % DEFAULT_REPO)+ src.add_argument("--model-dir", help="Local checkpoint dir with rl_agent_config.json (+ tokenizer.json).")+ p.add_argument("--subfolder", default=None, help="Subfolder inside the repo (e.g. multilingual, typed-decisions).")+ p.add_argument("--token", default=None, help="HF token (or HF_TOKEN env).")+ p.add_argument("--out-dir", required=True, help="Output dir for encoder.onnx, head.onnx, tokenizer.json, rl_agent_config.json.")+ p.add_argument("--opset", type=int, default=18, help="ONNX opset (default 18; the exporter implements 18 natively and a downgrade to 17 emits invalid Split nodes).")+ p.add_argument("--seq-len", type=int, default=16, help="Dummy sequence length for tracing/verification (default 16).")+ p.add_argument("--verify-len", type=int, default=512, help="Second length for the torch-vs-ONNX 1e-4 check (default 512; exercises sliding-window layers, local_attention=128).")+ p.add_argument("--no-verify", action="store_true", help="Skip the torch-vs-ONNX 1e-4 check.")+ return p.parse_args(argv)+++def main(argv=None):+ args = parse_args(argv)+ if build_model is None:+ raise SystemExit("export needs torch + laya installed: pip install torch transformers onnx onnxruntime")+ model_dir = args.model_dir or _download(args.repo or DEFAULT_REPO, args.subfolder, args.token)+ _ensure_tokenizer_json(model_dir)+ import torch++ class _EncoderOnly(torch.nn.Module):+ def __init__(self, model):+ super().__init__()+ self.encoder = model.encoder++ def forward(self, input_ids, attention_mask):+ return self.encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state++ class _HeadOnly(torch.nn.Module):+ """Decision head tail: type_emb + transformer head + scorer + act_head."""++ def __init__(self, model):+ super().__init__()+ self.head = model.head+ self.type_emb = model.type_emb+ self.scorer = model.scorer+ self.act_head = model.act_head++ def forward(self, hidden_states, marker_pos, marker_mask, qtype, attention_mask):+ h = hidden_states + self.type_emb(qtype.squeeze(-1))[:, None, :]+ if self.head is not None:+ # Same as DecisionModel.forward: padding must not be attended.+ # Without this, batch mates of unequal length corrupt each+ # other's markers through the head transformer.+ pad = attention_mask == 0+ for layer in self.head.layers:+ h = layer(h, src_key_padding_mask=pad)+ idx = marker_pos.clamp(min=0)[:, :, None].expand(-1, -1, h.size(-1))+ m = torch.gather(h, 1, idx)+ logits = self.scorer(m).squeeze(-1).float()+ logits = logits.masked_fill(~marker_mask, -1e4)+ p = torch.softmax(logits.detach(), -1)+ k = marker_mask.sum(-1).clamp(min=2).float()+ ent = -(p * torch.log(p.clamp_min(1e-9))).sum(-1) / torch.log(k)+ top2 = p.topk(2, -1).values+ feats = torch.stack([top2[:, 0], top2[:, 0] - top2[:, 1], ent, k / 255.0], -1)+ act_logits = self.act_head(torch.cat([h[:, 0].float(), feats], -1))+ return logits, act_logits++ with open(os.path.join(model_dir, "rl_agent_config.json"), encoding="utf-8") as f:+ cfg = json.load(f)+ # The encoder arch lives in <ckpt>/encoder/config.json (same as Agent:+ # enc_dir = model_dir/"encoder"); the checkpoint root itself has no+ # config.json, so passing model_dir here makes AutoConfig fail with+ # "Unrecognized model ... no model_type key".+ enc_dir = os.path.join(model_dir, "encoder")+ model = build_model(cfg, encoder_dir=enc_dir if os.path.exists(enc_dir) else None,+ pretrained=False)+ try:+ from safetensors.torch import load_file as _load_safetensors++ for name in ("model.safetensors", "pytorch_model.bin"):+ path = os.path.join(model_dir, name)+ if os.path.exists(path):+ state = _load_safetensors(path) if name.endswith(".safetensors") else torch.load(path, map_location="cpu")+ model.load_state_dict(state, strict=False)+ break+ except ImportError:+ pass+ model.eval().float()++ # Batch must stay symbolic: tracing with batch=1 lets dynamo bake batch=1+ # into a head reshape (batch=2 then fails with "204 by 408" broadcast).+ # Explicit Dims + batch=2 dummies keep it dynamic; verify covers 1 and 2.+ batch_dim = torch.export.Dim("batch", min=1, max=64)+ seq_dim = torch.export.Dim("seq", min=1, max=8192)+ markers_dim = torch.export.Dim("markers", min=1, max=256)+ enc_shapes = ({0: batch_dim, 1: seq_dim}, {0: batch_dim, 1: seq_dim})+ head_shapes = ({0: batch_dim, 1: seq_dim}, {0: batch_dim, 1: markers_dim},+ {0: batch_dim, 1: markers_dim}, {0: batch_dim},+ {0: batch_dim, 1: seq_dim})++ seq = int(args.seq_len)+ input_ids = torch.ones(2, seq, dtype=torch.long)+ attention_mask = torch.ones(2, seq, dtype=torch.long)+ ref_hidden, ref_logits, ref_act, marker_pos, marker_mask, qtype, ref_att = _run_ref(model, _HeadOnly(model).eval(), seq, batch=2)++ os.makedirs(args.out_dir, exist_ok=True)+ enc_path = os.path.join(args.out_dir, "encoder.onnx")+ head_path = os.path.join(args.out_dir, "head.onnx")++ torch.onnx.export(+ _EncoderOnly(model).eval(),+ (input_ids, attention_mask),+ enc_path,+ input_names=["input_ids", "attention_mask"],+ output_names=["last_hidden_state"],+ dynamic_axes={"input_ids": {0: "batch", 1: "seq"}, "attention_mask": {0: "batch", 1: "seq"}, "last_hidden_state": {0: "batch", 1: "seq"}},+ dynamic_shapes=enc_shapes,+ opset_version=args.opset,+ )+ torch.onnx.export(+ _HeadOnly(model).eval(),+ (ref_hidden, marker_pos, marker_mask, qtype, ref_att),+ head_path,+ input_names=["hidden_states", "marker_pos", "marker_mask", "qtype", "attention_mask"],+ output_names=["logits", "act_logits"],+ dynamic_axes={"hidden_states": {0: "batch", 1: "seq"}, "marker_pos": {0: "batch", 1: "markers"}, "marker_mask": {0: "batch", 1: "markers"}, "qtype": {0: "batch"}},+ dynamic_shapes=head_shapes,+ opset_version=args.opset,+ )++ if not args.no_verify:+ _verify(enc_path, head_path, input_ids, attention_mask, marker_pos, marker_mask, qtype, ref_att, ref_logits, ref_act)+ b1 = _run_ref(model, _HeadOnly(model).eval(), seq, batch=1)+ _verify(enc_path, head_path, torch.ones(1, seq, dtype=torch.long),+ torch.ones(1, seq, dtype=torch.long), *b1[3:6], b1[6], b1[1], b1[2])+ if int(args.verify_len) != seq:+ v_hidden, v_logits, v_act, v_pos, v_mask, v_qtype, v_att2 = _run_ref(model, _HeadOnly(model).eval(), int(args.verify_len), batch=2)+ v_ids = torch.ones(2, int(args.verify_len), dtype=torch.long)+ v_att = torch.ones(2, int(args.verify_len), dtype=torch.long)+ _verify(enc_path, head_path, v_ids, v_att, v_pos, v_mask, v_qtype, v_att2, v_logits, v_act)++ for name in ("tokenizer.json", "rl_agent_config.json"):+ src = os.path.join(model_dir, name)+ if os.path.exists(src):+ shutil.copy(src, os.path.join(args.out_dir, name))+ print("wrote %s and %s" % (enc_path, head_path))+++def _run_ref(model, head, seq, batch=1):+ import torch++ input_ids = torch.ones(batch, seq, dtype=torch.long)+ attention_mask = torch.ones(batch, seq, dtype=torch.long)+ with torch.inference_mode():+ ref_hidden = model.encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state+ marker_pos = torch.tensor([[1, 2]] * batch, dtype=torch.long)+ marker_mask = torch.tensor([[True, True]] * batch)+ qtype = torch.tensor([[0]] * batch, dtype=torch.long)+ ref_logits, ref_act = head(ref_hidden, marker_pos, marker_mask, qtype, attention_mask)+ return ref_hidden, ref_logits, ref_act, marker_pos, marker_mask, qtype, attention_mask+++def _verify(enc_path, head_path, input_ids, attention_mask, marker_pos, marker_mask, qtype, head_att, ref_logits, ref_act):+ import numpy as np++ try:+ import onnxruntime as ort+ except ImportError:+ raise SystemExit("export --no-verify not given but onnxruntime is missing: pip install onnxruntime")+ enc = ort.InferenceSession(enc_path, providers=["CPUExecutionProvider"])+ head = ort.InferenceSession(head_path, providers=["CPUExecutionProvider"])+ (hidden,) = enc.run(None, {"input_ids": input_ids.numpy(), "attention_mask": attention_mask.numpy()})+ logits, act = head.run(None, {"hidden_states": hidden.astype(np.float32), "marker_pos": marker_pos.numpy(), "marker_mask": marker_mask.numpy(), "qtype": qtype.numpy(), "attention_mask": head_att.numpy()})+ diff = float(np.abs(logits - ref_logits.detach().numpy()).max())+ if diff > 1e-4:+ raise SystemExit("verification failed: logits max abs diff %.2e > 1e-4" % diff)+ # act_logits are consumed only through softmax (rounded to 4dp downstream),+ # and dynamo decompositions add ~5e-4 raw-logit noise that softmax washes+ # out. Compare the probabilities TS actually reads instead of raw logits.+ got_p = np.exp(act - act.max(axis=-1, keepdims=True))+ got_p /= got_p.sum(axis=-1, keepdims=True)+ ref = ref_act.detach().numpy()+ ref_p = np.exp(ref - ref.max(axis=-1, keepdims=True))+ ref_p /= ref_p.sum(axis=-1, keepdims=True)+ pdiff = float(np.abs(got_p - ref_p).max())+ if pdiff > 1e-4:+ raise SystemExit("verification failed: act_probs max abs diff %.2e > 1e-4" % pdiff)+ print("verify ok: torch vs onnx match within 1e-4 (logits %.1e, act_probs %.1e)" % (diff, pdiff))+++if __name__ == "__main__":+ main()diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.tsnew file mode 100644index 00000000..f45bdb36--- /dev/null+++ b/laya-ts/src/agent.ts@@ -0,0 +1,372 @@+import {+ TEMP_MAX,+ TEMP_MIN,+ buildSequence,+ clampTemperature,+ collateItems,+ confidenceFromProbs,+ renderOptions,+ softmax,+ tempBucket,+} from "./common.js";+import type { Batch, SessionProvider } from "./providers.js";+import { encodeWithData, parseTokenizerJson, type TokenizerLike } from "./tokenizer.js";++export const QTYPES: Record<string, number> = { choice: 0, score: 1, noul: 2 };++export interface QuestionDef {+ type: string;+ instructions?: unknown;+ criteria?: unknown;+ [k: string]: unknown;+}++export interface ActionInfo {+ act_probability: number;+}++export interface ChoiceAnswer {+ type: "choice";+ choice: string;+ probabilities: Record<string, number>;+ confidence: number;+ action: ActionInfo;+}++export interface ScoreAnswer {+ type: "score";+ score: number;+ legend: Record<string, unknown>;+ probabilities: Record<string, number>;+ confidence: number;+ action: ActionInfo;+}++export interface NoulAnswer {+ type: "noul";+ noul: number;+ confidence: number;+ action: ActionInfo;+}++export type SystemAnswer = ChoiceAnswer | ScoreAnswer | NoulAnswer;++export interface SystemUsage {+ input_tokens: number;+ output_tokens: number;+}++export interface SystemOneResult {+ model: string;+ answers: Record<string, SystemAnswer>;+ usage: SystemUsage;+}++export interface AgentCfg {+ max_len?: number;+ head_max_len?: number;+ temperature?: unknown;+ temperature_by_options?: Record<string, unknown>;+ [k: string]: unknown;+}++export interface AgentOptions {+ provider: SessionProvider;+ tok?: TokenizerLike;+ cfg?: AgentCfg;+ max_len?: number;+ head_max_len?: number;+ temperature?: unknown;+ temperature_by_options?: Record<string, unknown>;+}++function qidStr(qid: string): string {+ return JSON.stringify(qid);+}++export function checkQuestion(qid: string, qdef: unknown): void {+ if (typeof qdef !== "object" || qdef === null || Array.isArray(qdef)) {+ const got = Array.isArray(qdef) ? "list" : qdef === null ? "NoneType" : typeof qdef;+ throw new Error(`question ${qidStr(qid)}: definition must be a dict, got ${got}`);+ }+ const q = qdef as Record<string, unknown>;+ const t = q["type"];+ if (t !== "choice" && t !== "score" && t !== "noul") {+ throw new Error(+ `question ${qidStr(qid)}: unknown type ${JSON.stringify(t)}; use one of ${JSON.stringify(sorted(QTYPES))}`,+ );+ }+ if (!("instructions" in q)) {+ throw new Error(`question ${qidStr(qid)}: no 'instructions'; add the text the model should answer`);+ }+ const crit = q["criteria"];+ if (t === "choice") {+ if (typeof crit !== "object" || crit === null) {+ throw new Error(+ `question ${qidStr(qid)}: a choice question takes 'criteria' as a dict of label -> description, or a list of labels`,+ );+ }+ if (Object.keys(crit as object).length === 0) {+ throw new Error(`question ${qidStr(qid)}: a choice question needs at least one criterion`);+ }+ } else if (t === "score") {+ if (!Array.isArray(crit)) {+ throw new Error(+ `question ${qidStr(qid)}: a score question takes 'criteria' as a list of level descriptions, index 0 first`,+ );+ }+ if (crit.length === 0) {+ throw new Error(`question ${qidStr(qid)}: a score question needs at least one level`);+ }+ } else if (crit !== undefined && crit !== null && (typeof crit !== "object" || Array.isArray(crit))) {+ throw new Error(+ `question ${qidStr(qid)}: a noul question takes 'criteria' as a dict with optional 'true'/'false' descriptions, or omits it`,+ );+ }+}++function sorted(o: Record<string, number>): string[] {+ return Object.keys(o).sort();+}++export function toInternal(qdef: QuestionDef): { t: "choice" | "score" | "noul"; ins: string; crit: unknown } {+ const t = qdef["type"] as "choice" | "score" | "noul";+ let crit: unknown = qdef["criteria"];+ if (t === "choice" && Array.isArray(crit)) {+ crit = Object.fromEntries(crit.map((c: unknown) => [c as string, null]));+ } else if (t === "noul" && crit !== null && crit !== undefined && typeof crit === "object" && !Array.isArray(crit)) {+ crit = Object.fromEntries(Object.entries(crit as Record<string, unknown>).map(([k, v]) => [String(k).toLowerCase(), v]));+ }+ let ins: unknown = qdef["instructions"];+ if (typeof ins !== "string") ins = JSON.stringify(ins);+ return { t, ins: ins as string, crit };+}++export function defaultTokenizer(): TokenizerLike {+ return {+ clsId: 101,+ sepId: 102,+ maskId: 103,+ padId: 0,+ maskToken: "[MASK]",+ encode(text: string): number[] {+ return text+ .split(/\s+/)+ .filter(Boolean)+ .map((w, i) => 1000 + ((w.length * 31 + i * 7) % 20000));+ },+ };+}++function tokenizerFromHF(tokenizerJson: unknown): TokenizerLike | null {+ const data = parseTokenizerJson(tokenizerJson);+ if (!data) return null;+ return {+ clsId: data.ids.cls,+ sepId: data.ids.sep,+ maskId: data.ids.mask,+ padId: data.ids.pad,+ maskToken: data.maskToken,+ encode: (text: string) => encodeWithData(data, text),+ };+}++/** Fallback when tokenizer.json is absent: ids from rl_agent_config, if present. */+function tokenizerFromConfig(cfg: AgentCfg): TokenizerLike | null {+ const c = cfg as Record<string, unknown>;+ const num = (v: unknown): number | null =>+ typeof v === "number" && Number.isFinite(v) ? v : null;+ const cls = num(c["cls_token_id"]) ?? num(c["cls_id"]);+ const sep = num(c["sep_token_id"]) ?? num(c["sep_id"]);+ const mask = num(c["mask_token_id"]) ?? num(c["mask_id"]);+ const pad = num(c["pad_token_id"]) ?? num(c["pad_id"]);+ if (cls === null && sep === null && mask === null && pad === null) return null;+ const d = defaultTokenizer();+ return {+ clsId: cls ?? d.clsId,+ sepId: sep ?? d.sepId,+ maskId: mask ?? d.maskId,+ padId: pad ?? d.padId,+ maskToken: typeof c["mask_token"] === "string" ? (c["mask_token"] as string) : d.maskToken,+ encode: d.encode,+ };+}++const r4 = (v: number): number => Math.round(v * 1e4) / 1e4;++export class Agent {+ cfg: AgentCfg;+ provider: SessionProvider;+ tok: TokenizerLike;+ maxLen: number;+ headMaxLen: number;+ temperatureRaw: unknown;+ temperatureByOptionsRaw: Record<string, unknown>;+ temperature: number[];+ temperatureByOptions: Record<string, number>;++ constructor(opts: AgentOptions) {+ if (!opts || !opts.provider) throw new Error("Agent needs a provider");+ this.provider = opts.provider;+ const cfg = { ...(opts.cfg ?? {}) } as AgentCfg;+ if (opts.max_len !== undefined) cfg.max_len = opts.max_len;+ if (opts.head_max_len !== undefined) cfg.head_max_len = opts.head_max_len;+ if (opts.temperature !== undefined) cfg.temperature = opts.temperature;+ if (opts.temperature_by_options !== undefined) cfg.temperature_by_options = opts.temperature_by_options;+ this.cfg = cfg;+ this.maxLen = Number(cfg.max_len ?? 512);+ this.headMaxLen = Number(cfg.head_max_len ?? 192);+ this.tok = opts.tok ?? defaultTokenizer();+ const raw = (cfg.temperature ?? [1.0, 1.0, 1.0]) as unknown;+ this.temperatureRaw = raw;+ const rawList = Array.isArray(raw) ? raw : [raw, raw, raw];+ this.temperature = [0, 1, 2].map((i) => clampTemperature(rawList[i] ?? 1.0));+ this.temperatureByOptionsRaw = (cfg.temperature_by_options ?? {}) as Record<string, unknown>;+ this.temperatureByOptions = Object.fromEntries(+ Object.entries(this.temperatureByOptionsRaw).map(([k, v]) => [k, clampTemperature(v)]),+ );+ const entries: Array<[string, unknown, number]> = [+ ...Object.entries(this.temperatureByOptionsRaw).map(+ ([k, v]) => [k, v, this.temperatureByOptions[k]] as [string, unknown, number],+ ),+ ...[0, 1, 2].map(+ (i) => [`temperature[${i}]`, rawList[i] ?? 1.0, this.temperature[i]] as [string, unknown, number],+ ),+ ];+ const rejected: string[] = [];+ for (const [name, rawV, applied] of entries) {+ try {+ if (Number(rawV) === applied) continue;+ } catch {+ /* invalid entries already have a neutral fallback; still report */+ }+ rejected.push(`${name}=${JSON.stringify(rawV) ?? String(rawV)} -> ${applied}`);+ }+ if (rejected.length > 0) {+ console.warn(+ `laya: this checkpoint ships invalid temperatures or values outside [${TEMP_MIN}, ${TEMP_MAX}]; ` ++ `using ${rejected.join(", ")}. Treat confidence from the affected entries as uncalibrated.`,+ );+ }+ }++ async systemOne(state: unknown, questions: Record<string, QuestionDef>): Promise<SystemOneResult> {+ const ids = Object.keys(questions ?? {});+ if (ids.length === 0) {+ return { model: "laya-rl-agent", answers: {}, usage: { input_tokens: 0, output_tokens: 0 } };+ }+ const items: { ids: number[]; markers: number[]; qtype: number }[] = [];+ const internals: { t: "choice" | "score" | "noul"; ins: string; crit: unknown }[] = [];+ for (const qid of ids) {+ checkQuestion(qid, questions[qid]);+ const q = toInternal(questions[qid]);+ internals.push(q);+ const { ids: seq, markers } = buildSequence(this.tok, state, q, this.maxLen, this.headMaxLen);+ if (markers.length !== renderOptions(q).length) {+ throw new Error(`question ${qidStr(qid)} options exceed head_max_len=${this.headMaxLen}`);+ }+ items.push({ ids: seq, markers, qtype: QTYPES[q.t] });+ }+ const collated = collateItems([items], this.tok.padId);+ if (!collated) throw new Error("no items to collate");+ const batch: Batch = collated;+ const nTokens = batch.attentionMask.flat().reduce((a, b) => a + b, 0);+ const { lastHidden } = await this.provider.runEncoder(batch);+ const { logits, act } = await this.provider.runHead(lastHidden, batch);++ const answers: Record<string, SystemAnswer> = {};+ for (let r = 0; r < ids.length; r++) {+ const qid = ids[r];+ const q = internals[r];+ const k = items[r].markers.length;+ const qt = QTYPES[q.t];+ const bucket = tempBucket(qt, k);+ const scale = this.temperatureByOptions[bucket] ?? this.temperature[qt] ?? 1.0;+ const z = (logits[r] as number[]).slice(0, k).map((v) => v / scale);+ const p = softmax(z);+ const actRow = (act[r] as number[]) ?? [1, 0];+ const actP = softmax(actRow.slice(0, Math.max(2, actRow.length)));+ const ext = { act_probability: r4(actP[0]) };+ if (q.t === "choice") {+ const keys = Object.keys(q.crit as Record<string, unknown>);+ let best = 0;+ for (let i = 1; i < p.length; i++) if (p[i] > p[best]) best = i;+ answers[qid] = {+ type: "choice",+ choice: keys[best],+ probabilities: Object.fromEntries(keys.map((kk, i) => [kk, r4(p[i] ?? 0)])),+ confidence: r4(confidenceFromProbs(p)),+ action: ext,+ };+ } else if (q.t === "score") {+ const exp = p.reduce((a, v, i) => a + i * v, 0);+ answers[qid] = {+ type: "score",+ score: r4(exp),+ legend: Object.fromEntries((q.crit as unknown[]).map((c, i) => [String(i), c])),+ probabilities: Object.fromEntries(p.map((v, i) => [String(i), r4(v)])),+ confidence: r4(confidenceFromProbs(p)),+ action: ext,+ };+ } else {+ const pt = p[1] ?? 0;+ answers[qid] = {+ type: "noul",+ noul: r4(pt),+ confidence: r4(Math.max(pt, 1 - pt)),+ action: ext,+ };+ }+ }+ return { model: "laya-rl-agent", answers, usage: { input_tokens: nTokens, output_tokens: 0 } };+ }++ async predict(state: unknown, questions: Record<string, QuestionDef>): Promise<SystemOneResult> {+ return this.systemOne(state, questions);+ }++ static async load(+ modelDirOrRepo: string,+ opts?: {+ device?: string;+ subfolder?: string | null;+ localDir?: string;+ token?: string | null;+ numThreads?: number;+ },+ ): Promise<Agent> {+ const sub = opts?.subfolder ?? null;+ const isBrowser =+ typeof (globalThis as unknown as { window?: unknown }).window !== "undefined";+ let cfg: AgentCfg = {};+ let tokenizerJson: unknown | null = null;+ let dir = opts?.localDir ?? modelDirOrRepo;+ let provider: SessionProvider;+ if (isBrowser) {+ const { loadWebBundle, createWebProvider } = await import("./providers.js");+ const bundle = await loadWebBundle(modelDirOrRepo, { subfolder: sub });+ cfg = bundle.cfg;+ tokenizerJson = bundle.tokenizerJson;+ dir = bundle.dir;+ provider = await createWebProvider(dir, { numThreads: opts?.numThreads });+ } else {+ const { loadNodeBundle, createNodeProvider } = await import("./providers.js");+ const bundle = await loadNodeBundle(modelDirOrRepo, {+ subfolder: sub,+ localDir: opts?.localDir,+ token: opts?.token,+ });+ cfg = bundle.cfg;+ tokenizerJson = bundle.tokenizerJson;+ dir = bundle.dir;+ provider = await createNodeProvider(dir, { device: opts?.device, numThreads: opts?.numThreads });+ }+ let tok: TokenizerLike | null = null;+ try {+ tok = tokenizerJson ? tokenizerFromHF(tokenizerJson) : tokenizerFromConfig(cfg);+ } catch {+ tok = null;+ }+ return new Agent({ provider, tok: tok ?? defaultTokenizer(), cfg });+ }+}diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.tsnew file mode 100644index 00000000..0d9331fc--- /dev/null+++ b/laya-ts/src/common.ts@@ -0,0 +1,139 @@+import type { TokenizerLike } from "./tokenizer.js";+export type QType = "choice" | "score" | "noul";+export interface InternalQ { t: QType; ins: string; crit: unknown }+/** Python `json.dumps(v, ensure_ascii=False)` replica: separators (", ", ": "),+unicode raw, unknown types fall back to undefined (caller applies str()). */+function pyJson(v: unknown): string | undefined {+ if (v === null) return "null";+ if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") return JSON.stringify(v);+ if (Array.isArray(v)) return `[${v.map((x) => pyJson(x) ?? "null").join(", ")}]`;+ if (typeof v === "object") {+ const proto = Object.getPrototypeOf(v);+ if (proto !== Object.prototype && proto !== null) return undefined;+ const parts: string[] = [];+ for (const [k, x] of Object.entries(v as Record<string, unknown>)) {+ const s = pyJson(x);+ if (s !== undefined) parts.push(`${JSON.stringify(k)}: ${s}`);+ }+ return `{${parts.join(", ")}}`;+ }+ return undefined;+}+export function serializeState(state: unknown): string {+ if (typeof state === "string") return state;+ return pyJson(state) ?? String(state);+}+function renderCriterion(v: unknown): string {+ return typeof v === "string" ? v : pyJson(v) ?? String(v);+}+export function renderOptions(q: InternalQ): string[] {+ if (q.t === "choice") {+ const crit = q.crit as Record<string, unknown>;+ return Object.entries(crit).map(([k, v]) =>+ v === null || v === undefined || v === "" ? k : `${k}: ${renderCriterion(v)}`);+ }+ if (q.t === "score") {+ return (q.crit as unknown[]).map((c, i) => `level ${i}: ${renderCriterion(c)}`);+ }+ const crit = (q.crit ?? {}) as Record<string, unknown>;+ const f = crit["false"], t = crit["true"];+ return [+ "false: " + (f !== null && f !== undefined && f !== "" ? renderCriterion(f) : "no, the statement does not hold"),+ "true: " + (t !== null && t !== undefined && t !== "" ? renderCriterion(t) : "yes, the statement holds"),+ ];+}+export function buildSequence(tok: TokenizerLike, state: unknown, q: InternalQ,+ maxLen = 512, headMaxLen = 192, optionOrder?: number[], truncateLeft = false): { ids: number[]; markers: number[] } {+ const maskTok = tok.maskToken;+ const opts = renderOptions(q);+ const order = optionOrder ?? opts.map((_, i) => i);+ const ins = String(q.ins).split(maskTok).join(" ");+ let headIds = tok.encode(`${q.t} question: ${ins}`);+ let optIds = order.map((i) =>+ [tok.maskId, ...tok.encode(" " + opts[i].split(maskTok).join(" ")).slice(0, 48)]);+ let budget = headMaxLen - optIds.reduce((a, o) => a + o.length, 0);+ if (budget < 16) {+ const per = Math.max(4, Math.floor((headMaxLen - 16) / Math.max(1, optIds.length)));+ optIds = optIds.map((o) => o.slice(0, per));+ budget = headMaxLen - optIds.reduce((a, o) => a + o.length, 0);+ }+ headIds = headIds.slice(0, Math.max(8, budget));+ let ids = [tok.clsId, ...headIds, tok.sepId];+ const markers: number[] = [];+ for (const o of optIds) { markers.push(ids.length); ids.push(...o); }+ ids.push(tok.sepId);+ const room = Math.max(0, maxLen - ids.length - 1);+ const stAll = tok.encode(serializeState(state).split(maskTok).join(" "));+ const st = truncateLeft ? stAll.slice(-room) : stAll.slice(0, room);+ ids = [...ids, ...st, tok.sepId].slice(0, maxLen);+ return { ids, markers: markers.filter((m) => m < maxLen) };+}+export function softmax(z: number[]): number[] {+ const m = Math.max(...z);+ const e = z.map((v) => Math.exp(v - m));+ const s = e.reduce((a, b) => a + b, 0);+ return e.map((v) => v / s);+}+export function confidenceFromProbs(p: number[]): number {+ const k = p.length;+ if (k < 2) return 1.0;+ const ent = -p.reduce((a, v) => a + v * Math.log(Math.max(v, 1e-12)), 0);+ return Math.min(1, Math.max(0, 1 - ent / Math.log(k)));+}+export const TEMP_MIN = 0.5, TEMP_MAX = 5.0;+export function clampTemperature(t: unknown): number {+ const f = typeof t === "number" ? t : Number(t);+ if (!Number.isFinite(f)) return 1.0;+ return Math.min(TEMP_MAX, Math.max(TEMP_MIN, f));+}+export function tempBucket(qtype: number, k: number): string {+ const size = k <= 2 ? "2" : k <= 5 ? "3-5" : k <= 10 ? "6-10" : "11+";+ return `${["choice", "score", "noul"][qtype]}:${size}`;+}+export interface CollateItem {+ ids: number[];+ markers: number[];+ qtype: number;+ label?: number;+ target?: number[];+ [k: string]: unknown;+}+export interface CollatedBatch {+ inputIds: number[][];+ attentionMask: number[][];+ markerPos: number[][];+ markerMask: boolean[][];+ qtype: number[];+ label: number[];+ meta: Record<string, unknown>[];+ target?: number[][];+}+/** TS parity of py `collate_items(batch, pad_id)`: batch = list of groups. */+export function collateItems(batch: CollateItem[][], padId: number): CollatedBatch | null {+ const items = (batch ?? []).flat();+ if (items.length === 0) return null;+ const L = Math.max(...items.map((it) => it.ids.length));+ const K = Math.max(...items.map((it) => it.markers.length));+ const hasTarget = items.some((it) => "target" in it);+ const inputIds = items.map((it) => [...it.ids, ...Array(L - it.ids.length).fill(padId)]);+ const attentionMask = items.map((it) => [...Array(it.ids.length).fill(1), ...Array(L - it.ids.length).fill(0)]);+ const markerPos = items.map((it) => [...it.markers, ...Array(K - it.markers.length).fill(0)]);+ const markerMask = items.map((it) => [...it.markers.map(() => true), ...Array(K - it.markers.length).fill(false)]);+ const qtype = items.map((it) => it.qtype);+ const label = items.map((it) => (typeof it.label === "number" ? it.label : -1));+ const meta = items.map((it) => {+ const m: Record<string, unknown> = {};+ for (const k of Object.keys(it)) {+ if (k !== "ids" && k !== "markers" && k !== "target") m[k] = (it as Record<string, unknown>)[k];+ }+ return m;+ });+ const out: CollatedBatch = { inputIds, attentionMask, markerPos, markerMask, qtype, label, meta };+ if (hasTarget) {+ out.target = items.map((it) => {+ const t = Array.isArray(it.target) ? (it.target as number[]) : [];+ return [...t, ...Array(K - t.length).fill(0)];+ });+ }+ return out;+}diff --git a/laya-ts/src/email.ts b/laya-ts/src/email.tsnew file mode 100644index 00000000..d142ea77--- /dev/null+++ b/laya-ts/src/email.ts@@ -0,0 +1,134 @@+const QUOTE_HEADERS: RegExp[] = [+ /^\s*On .{0,300}wrote:\s*$/i,+ /^\s*Em (?=.*\d).{0,300}escreveu:\s*$/i,+ /^\s*El (?=.*\d).{0,300}escribi[óo]:\s*$/i,+ /^\s*-{2,}\s*(Original|Forwarded) Message\s*-{2,}/i,+ /^\s*-{2,}\s*(Mensagem (original|encaminhada)|Mensaje (original|reenviado))\s*-{2,}/i,+ /^\s*_{8,}\s*$/,+ /^\s*From:\s.+$/i,+ /^\s*De:\s.*[@<]/i,+];+const ATTRIBUTION_TAIL = /^.{0,120}\S@\S+\s+(wrote|escreveu|escribi[óo]):\s*$/i;+const ATTRIBUTION_HEAD = /^\s*(On|Em|El) (?=.*\d)/i;+const HEADER_FROM_NAME = /^\s*De:\s+\S/i;+const HEADER_NEXT = /^\s*(Enviad[oa]( em| el)?:\s|(Data|Fecha):\s.*\d{4})/i;+const SIGNATURE_MARKERS: RegExp[] = [+ /^\s*--\s*$/,+ /^\s*(best|kind|warm|many thanks|thanks|thank you|regards|cheers|sincerely)[\w ,!.]*$/i,+ /^\s*sent from my (iphone|android|mobile|ipad)/i,+ /^\s*(atenciosamente|att|abraços?|abs|um abraço|cordialmente|grat[oa]|(muito )?obrigad[oa]s?( desde já| pela atenção)?|(com os melhores )?cumprimentos|saudações|(un )?saludos?( cordiales)?|atentamente|(muchas )?gracias( de antemano)?)[\s,!.]*$/i,+];+const DEVICE =+ "iphone|ipad|android|ios|celular|telemóvel|móvil|galaxy|smartphone|samsung|tablet|" ++ "outlook|yahoo|mail|e-?mail|gmail|windows";+const DEVICE_FOOTER = new RegExp(+ "^\\s*((enviad[oa] (do|pelo|pela|via|desde|a partir do)( meu| minha| mi)?|sent from( my)?)" ++ ` (${DEVICE})( (${DEVICE}|para|for|no|na|\\d+))*|(obter o|get) outlook (para|for) (ios|android))[\\s.!]*$`,+ "i",+);+const DISCLAIMER = new RegExp(+ "(confidential|intended (solely )?for the (use of the )?(named )?(addressee|recipient)|" ++ "if you (have )?received this (e-?mail|message) in error|" ++ "\\b(esta|este) (mensagem|e-?mail|mensaje|correo)\\b[^.]{0,80}(confidencia|sigilos|privilegiad)|" ++ "\\b(uso exclusivo|exclusivamente|únicamente|unicamente)\\b[^.]{0,30}" ++ "(destinatári|destinatari|pessoa|persona|entidade|entidad)|" ++ "\\b(recebeu|recebido|receber) (esta|este) (mensagem|e-?mail)\\b[^.]{0,20} por (engano|erro)|" ++ "\\b(ha recibido|recibió|recibe) (este|esta) (mensaje|correo)\\b[^.]{0,20} por error|" ++ "\\bantes de imprimir\\b[^.]{0,100}(meio ambiente|medio ambiente|natureza|planeta|realmente necess)|" ++ "\\b(meio|medio) ambiente\\b[^.]{0,30}antes de imprimir)",+ "i",+);+const SENTENCE = /(?<=[.!?])\s+/;++function startsNewSentence(line: string): boolean {+ for (const ch of line) {+ if (/\p{L}/u.test(ch)) return ch === ch.toUpperCase() && ch !== ch.toLowerCase();+ }+ return false;+}++function splitFusedLines(sentence: string): string[] {+ if (!sentence.includes("\n")) return [sentence];+ const pieces: string[] = [];+ let buf = "";+ for (const line of sentence.split("\n").map((ln) => ln.trim())) {+ if (!line) continue;+ if (buf && startsNewSentence(line)) {+ pieces.push(buf);+ buf = line;+ } else {+ buf = buf ? buf + " " + line : line;+ }+ }+ if (buf) pieces.push(buf);+ return pieces;+}++function stripDisclaimer(paragraph: string): string {+ if (!DISCLAIMER.test(paragraph)) return paragraph;+ const parts = paragraph.split(SENTENCE).map((p) => p.trim()).filter(Boolean);+ const pieces: string[] = [];+ for (const p of parts) {+ if (DISCLAIMER.test(p)) pieces.push(...splitFusedLines(p));+ else pieces.push(p);+ }+ return pieces.filter((p) => !DISCLAIMER.test(p)).join(" ");+}++export function cleanEmailBody(body: string, maxChars = 3000): string {+ const text = (body ?? "").replace(/\r\n?/g, "\n").replace(/\\n/g, "\n");+ const lines: string[] = [];+ const src = text.split("\n");+ for (let i = 0; i < src.length; i++) {+ const line = src[i];+ if (QUOTE_HEADERS.some((p) => p.test(line)) && lines.length) break;+ if (lines.length && HEADER_FROM_NAME.test(line) && i + 1 < src.length && HEADER_NEXT.test(src[i + 1])) break;+ if (ATTRIBUTION_TAIL.test(line) && lines.length) {+ if (ATTRIBUTION_HEAD.test(lines[lines.length - 1])) lines.pop();+ break;+ }+ if (line.replace(/^\s+/, "").startsWith(">")) continue;+ lines.push(line.replace(/\s+$/, ""));+ }+ let cut = lines.length;+ const start = Math.max(1, Math.min(Math.floor(lines.length * 0.6), lines.length - 8));+ for (let i = start; i < lines.length; i++) {+ const n = lines[i].trim().length;+ if (+ (n <= 40 && SIGNATURE_MARKERS.some((p) => p.test(lines[i]))) ||+ (n <= 60 && DEVICE_FOOTER.test(lines[i]))+ ) {+ cut = i;+ break;+ }+ }+ const kept = lines.slice(0, cut);+ const paragraphs = kept+ .join("\n")+ .split(/\n\s*\n/)+ .map(stripDisclaimer);+ const out = paragraphs+ .map((p) => p.trim())+ .filter(Boolean)+ .join("\n\n")+ .replace(/[ \t]+/g, " ");+ return out.slice(0, maxChars);+}++export function emailState(+ subject: string,+ body: string,+ sender?: string | null,+ clean = true,+ extra: Record<string, unknown> = {},+): Record<string, unknown> {+ const state: Record<string, unknown> = {+ subject: (subject ?? "").trim(),+ body: clean ? cleanEmailBody(body ?? "") : (body ?? ""),+ };+ if (sender) state["from"] = sender;+ for (const [k, v] of Object.entries(extra ?? {})) {+ if (v !== null && v !== undefined) state[k] = v;+ }+ return state;+}diff --git a/laya-ts/src/index.ts b/laya-ts/src/index.tsnew file mode 100644index 00000000..178cb90a--- /dev/null+++ b/laya-ts/src/index.ts@@ -0,0 +1,39 @@+export const VERSION = "0.1.0";+export { Agent, checkQuestion, toInternal, defaultTokenizer } from "./agent.js";+export type {+ QuestionDef,+ SystemOneResult,+ ChoiceAnswer,+ ScoreAnswer,+ NoulAnswer,+ SystemAnswer,+ SystemUsage,+ ActionInfo,+ AgentCfg,+ AgentOptions,+} from "./agent.js";+export { createNodeProvider, createWebProvider, feed, feedHead, loadNodeBundle, loadWebBundle } from "./providers.js";+export type { Batch, SessionProvider, ProviderOptions, NodeBundle, WebBundle } from "./providers.js";+export { Router, normaliseName, DEFAULT_MODELS } from "./router.js";+export type { RoutedResult, RouteDecision, ModelName, ModelSpec } from "./router.js";+export { shortlistChoice, predictShortlist, embedFnFromAgent, DEFAULT_SHORTLIST_K } from "./shortlist.js";+export type { EmbedFn, ShortlistMeta } from "./shortlist.js";+export { analyse, isEnglish, guessLatinLanguage, detectScript } from "./lang.js";+export type { AnalyseResult, LatinProfile } from "./lang.js";+export { cleanEmailBody, emailState } from "./email.js";+export { triageQuestions, emailQuestions, guardQuestions, moderationQuestions, routerQuestions } from "./presets.js";+export {+ renderOptions,+ serializeState,+ buildSequence,+ softmax,+ confidenceFromProbs,+ clampTemperature,+ tempBucket,+ collateItems,+ TEMP_MIN,+ TEMP_MAX,+} from "./common.js";+export type { QType, InternalQ, CollateItem, CollatedBatch } from "./common.js";+export { bpeEncode, metaspaceEncode, encodeWithData, parseTokenizerJson, loadTokenizerJson, CHECKPOINT_IDS, SPECIAL_ALIASES, METASPACE_REPLACEMENT } from "./tokenizer.js";+export type { TokenizerLike, TokenizerData, TokenizerIds, PreTokenizerKind } from "./tokenizer.js";diff --git a/laya-ts/src/lang.ts b/laya-ts/src/lang.tsnew file mode 100644index 00000000..64744f35--- /dev/null+++ b/laya-ts/src/lang.ts@@ -0,0 +1,269 @@+type ScriptRanges = Array<[string, Array<[number, number]>]>;++const SCRIPT_RANGES: ScriptRanges = [+ ["greek", [[0x0370, 0x03ff], [0x1f00, 0x1fff]]],+ ["cyrillic", [[0x0400, 0x052f], [0x2de0, 0x2dff], [0xa640, 0xa69f]]],+ ["armenian", [[0x0530, 0x058f]]],+ ["hebrew", [[0x0590, 0x05ff]]],+ ["arabic", [[0x0600, 0x06ff], [0x0750, 0x077f], [0x08a0, 0x08ff], [0xfb50, 0xfdff], [0xfe70, 0xfeff]]],+ ["devanagari", [[0x0900, 0x097f], [0xa8e0, 0xa8ff]]],+ ["bengali", [[0x0980, 0x09ff]]],+ ["gurmukhi", [[0x0a00, 0x0a7f]]],+ ["gujarati", [[0x0a80, 0x0aff]]],+ ["oriya", [[0x0b00, 0x0b7f]]],+ ["tamil", [[0x0b80, 0x0bff]]],+ ["telugu", [[0x0c00, 0x0c7f]]],+ ["kannada", [[0x0c80, 0x0cff]]],+ ["malayalam", [[0x0d00, 0x0d7f]]],+ ["sinhala", [[0x0d80, 0x0dff]]],+ ["thai", [[0x0e00, 0x0e7f]]],+ ["lao", [[0x0e80, 0x0eff]]],+ ["tibetan", [[0x0f00, 0x0fff]]],+ ["myanmar", [[0x1000, 0x109f]]],+ ["georgian", [[0x10a0, 0x10ff]]],+ ["ethiopic", [[0x1200, 0x137f]]],+ ["khmer", [[0x1780, 0x17ff]]],+ ["hangul", [[0x1100, 0x11ff], [0x3130, 0x318f], [0xac00, 0xd7af]]],+ ["kana", [[0x3040, 0x309f], [0x30a0, 0x30ff], [0x31f0, 0x31ff]]],+ ["han", [[0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xf900, 0xfaff]]],+];++const STOP: Record<string, Set<string>> = {+ en: new Set(["the", "and", "is", "are", "was", "were", "to", "of", "in", "for", "with", "that",+ "this", "it", "you", "have", "has", "not", "but", "on", "at", "be", "as", "from",+ "will", "can", "would", "there", "their", "what", "which", "please", "we", "i"]),+ fr: new Set(["le", "la", "les", "des", "une", "est", "pour", "dans", "que", "qui", "avec", "sur",+ "pas", "plus", "nous", "vous", "être", "cette", "mais", "sont", "ont", "aux", "ce",+ "et", "du", "au", "ou", "je", "tu", "il", "elle", "ils", "elles", "mon", "ton",+ "ma", "ta", "sa", "mes", "tes", "ses", "ces", "deux", "trois", "très", "bien",+ "tout", "tous", "toute", "fait", "veux", "veut", "peux", "peut", "dois", "doit",+ "merci", "bonjour", "jour", "jours", "mois", "fois", "quand", "comment", "pourquoi",+ "alors", "donc"]),+ de: new Set(["der", "die", "das", "und", "ist", "ein", "eine", "den", "dem", "nicht", "mit", "für",+ "auf", "von", "zu", "sich", "auch", "werden", "wurde", "haben", "sind", "oder", "aber"]),+ es: new Set(["el", "los", "las", "que", "por", "con", "para", "una", "es", "se", "del", "como",+ "pero", "son", "está", "este", "esta", "todo", "más", "muy", "hay", "sus",+ "la", "un", "y", "al", "lo", "le", "les", "su", "mi", "tu", "nos",+ "ni", "dos", "tres", "fue", "fueron", "ser", "tiene", "tienen", "tengo", "puede",+ "pueden", "quiero", "necesito", "hemos", "han", "sobre", "entre", "cuando", "donde",+ "porque", "aunque", "también", "ya", "eso", "esto", "esa", "ese", "nada", "algo",+ "aquí", "hoy", "gracias"]),+ pt: new Set(["os", "as", "que", "em", "um", "uma", "para", "com", "não", "é", "se", "do", "da",+ "dos", "das", "mas", "são", "está", "este", "esta", "muito", "pelo", "pela",+ "o", "e", "na", "nas", "nos", "ao", "aos", "por", "foi", "era", "ser", "sou",+ "tem", "tenho", "pode", "podem", "quero", "preciso", "eu", "meu", "minha", "seu",+ "sua", "isso", "isto", "aqui", "ali", "como", "quando", "onde", "porque", "mais",+ "já", "ainda", "agora", "hoje", "ontem", "dois", "três", "tudo", "nada", "obrigado",+ "olá",+ "você", "vocês", "voce", "voces", "vc", "vcs", "nao", "sao", "ja", "até", "tá", "pra",+ "gostaria", "obrigada", "também", "tambem", "estou", "estamos", "meus", "minhas",+ "nosso", "nossa", "consigo", "cadê", "boa", "tarde", "noite",+ "depois", "antes", "então", "entao", "ninguém", "ninguem", "alguém", "alguem", "nenhum",+ "nenhuma", "estava", "ficou", "fiz", "deu"]),+ it: new Set(["il", "lo", "gli", "che", "di", "per", "con", "non", "è", "si", "del", "della", "sono",+ "questo", "questa", "anche", "come", "più", "sono", "nella", "alla",+ "la", "le", "un", "uno", "una", "e", "ed", "o", "da", "su", "tra", "fra", "mi",+ "ci", "ne", "ho", "hai", "ha", "abbiamo", "avete", "hanno", "era", "stato", "stata",+ "devo", "deve", "devono", "voglio", "vorrei", "mio", "mia", "tuo", "sua", "quando",+ "dove", "perche", "molto", "poco", "sempre", "mai", "già", "ancora", "adesso", "oggi",+ "ieri", "grazie", "ciao", "scusa",+ "nel", "nell", "negli", "sul", "sulla", "sulle", "dal", "dalla", "dallo", "dagli", "dei",+ "delle", "dello", "degli", "agli", "alle", "col"]),+ nl: new Set(["het", "een", "van", "is", "op", "te", "dat", "niet", "met", "voor", "zijn", "aan",+ "door", "maar", "ook", "worden", "deze", "naar", "wordt"]),+ ro: new Set(["și", "să", "este", "sunt", "care", "pentru", "din", "dar", "după", "până", "fără",+ "ale", "lui", "în", "fost", "acum", "vreau", "trebuie", "foarte", "acest", "această",+ "acesta", "aceasta", "mi", "ți", "vă", "nu"]),+};++const NON_EN_DIACRITICS = new Set(+ ("àâäãáåçéèêëíìîïñóòôöõøúùûüýÿßæœ" ++ "ăâîșțşţ" ++ "ąćęłńśźż" ++ "čďěňřšťůž" ++ "őű" ++ "ğı" ++ "āēģīķļņūž" ++ "đ").split(""),+);++export const NON_EN_DIACRITIC_RATE = 0.02;++const SHARED_WORDS: Set<string> = (() => {+ const counts = new Map<string, number>();+ for (const words of Object.values(STOP)) {+ for (const w of words) counts.set(w, (counts.get(w) ?? 0) + 1);+ }+ const out = new Set<string>();+ for (const [w, n] of counts) if (n > 1) out.add(w);+ return out;+})();++const WORD_RE = /[^\W\d_]+/gu;+const IDENTIFIER_RE = /[\p{L}\p{N}_-]*(?:[.@][\p{L}\p{N}_-]+)+/gu;+const IS_ALPHA_RE = /\p{L}/u;++function isLatinCp(cp: number): boolean {+ return cp < 0x0250 || (0x1e00 <= cp && cp <= 0x1eff) || (0xff21 <= cp && cp <= 0xff3a) || (0xff41 <= cp && cp <= 0xff5a);+}++export function stateText(state: unknown, maxChars = 4000): string {+ const parts: string[] = [];+ const walk = (v: unknown, d: number): void => {+ if (d > 6 || v == null) return;+ if (typeof v === "string") { parts.push(v); return; }+ if (Array.isArray(v)) { for (const x of v) walk(x, d + 1); return; }+ if (typeof v === "object") { for (const x of Object.values(v as object)) walk(x, d + 1); }+ };+ walk(state, 0);+ return parts.join(" ").slice(0, maxChars);+}++export function detectScript(text: string): string {+ const counts = new Map<string, number>();+ let latin = 0;+ for (const ch of text) {+ if (!IS_ALPHA_RE.test(ch)) continue;+ const cp = ch.codePointAt(0)!;+ if (isLatinCp(cp)) { latin += 1; continue; }+ let found: string | null = null;+ for (const [name, ranges] of SCRIPT_RANGES) {+ if (ranges.some(([lo, hi]) => lo <= cp && cp <= hi)) { found = name; break; }+ }+ counts.set(found ?? "other", (counts.get(found ?? "other") ?? 0) + 1);+ }+ counts.set("latin", latin);+ let total = 0;+ for (const v of counts.values()) total += v;+ if (total === 0) return "unknown";+ let best = "latin";+ let bestN = -1;+ for (const [k, v] of counts) {+ if (v > bestN) { bestN = v; best = k; }+ }+ return best;+}++export function scriptProfile(text: string): Record<string, number> {+ const counts = new Map<string, number>([["latin", 0]]);+ for (const ch of text) {+ if (!IS_ALPHA_RE.test(ch)) continue;+ const cp = ch.codePointAt(0)!;+ if (isLatinCp(cp)) { counts.set("latin", (counts.get("latin") ?? 0) + 1); continue; }+ let found: string | null = null;+ for (const [name, ranges] of SCRIPT_RANGES) {+ if (ranges.some(([lo, hi]) => lo <= cp && cp <= hi)) { found = name; break; }+ }+ const key = found ?? "other";+ counts.set(key, (counts.get(key) ?? 0) + 1);+ }+ let total = 0;+ for (const v of counts.values()) total += v;+ if (!total) return {};+ const out: Record<string, number> = {};+ for (const [k, v] of counts) {+ if (v) out[k] = v / total;+ }+ return out;+}++export interface LatinProfile {+ language: string | null;+ englishHits: number;+ diacriticRate: number;+ looksNonEnglish: boolean;+}++export function latinProfile(text: string): LatinProfile {+ const stripped = text.replace(IDENTIFIER_RE, " ");+ const rawWords = stripped.match(WORD_RE) ?? [];+ const words = rawWords.map((w) => w.toLowerCase());+ const lowered = text.toLowerCase();+ let diac = 0;+ for (const ch of lowered) {+ if (NON_EN_DIACRITICS.has(ch)) diac += 1;+ }+ const diacRate = diac / Math.max(1, lowered.length);+ const nonEnglish = diacRate >= NON_EN_DIACRITIC_RATE;+ if (words.length < 4) {+ return { language: null, englishHits: 0, diacriticRate: diacRate, looksNonEnglish: nonEnglish };+ }+ const scores: Record<string, number> = {};+ for (const [lg, sw] of Object.entries(STOP)) {+ let s = 0;+ for (const w of words) if (sw.has(w)) s += 1;+ scores[lg] = s;+ }+ const en = scores["en"] ?? 0;+ const wordSet = new Set(words);+ let bestLg: string | null = null;+ let best = 0;+ for (const [lg, s] of Object.entries(scores)) {+ if (lg === "en") continue;+ const hasEvidence = [...wordSet].some((w) => STOP[lg].has(w) && !SHARED_WORDS.has(w));+ if (!hasEvidence) continue;+ if (s > best) { best = s; bestLg = lg; }+ }+ let lang: string | null = null;+ if (bestLg && best >= Math.max(2, en + 2)) {+ lang = bestLg;+ } else if (bestLg && nonEnglish && best >= Math.max(2, en)) {+ lang = bestLg;+ } else if (en && !nonEnglish) {+ lang = "en";+ }+ return { language: lang, englishHits: en, diacriticRate: diacRate, looksNonEnglish: nonEnglish };+}++export function guessLatinLanguage(text: string): string | null {+ return latinProfile(text).language;+}++export interface AnalyseResult {+ script: string;+ scriptProfile: Record<string, number>;+ language: string | null;+ isEnglish: boolean;+ languageUndecided: boolean;+ diacriticRate: number;+ nonLatinFraction: number;+}++function round4(x: number): number {+ return Math.round(x * 10000) / 10000;+}++export function analyse(state: unknown): AnalyseResult {+ const text = stateText(state);+ const prof = scriptProfile(text);+ const script = detectScript(text);+ const nonLatin = prof && Object.keys(prof).length ? round4(1.0 - (prof["latin"] ?? 0.0)) : 0.0;+ if (script === "unknown") {+ return {+ script: "unknown", scriptProfile: prof, language: null,+ isEnglish: true, languageUndecided: true, diacriticRate: 0.0,+ nonLatinFraction: 0.0,+ };+ }+ if (script !== "latin") {+ return {+ script, scriptProfile: prof, language: null,+ isEnglish: false, languageUndecided: true, diacriticRate: 0.0,+ nonLatinFraction: nonLatin,+ };+ }+ const profLat = latinProfile(text);+ const lang = profLat.language;+ const undecided = lang === null;+ const english = lang === "en" || (undecided && !profLat.looksNonEnglish);+ return {+ script: "latin", scriptProfile: prof, language: lang,+ isEnglish: english, languageUndecided: undecided,+ diacriticRate: round4(profLat.diacriticRate),+ nonLatinFraction: nonLatin,+ };+}++export function isEnglish(state: unknown): boolean {+ return analyse(state).isEnglish;+}diff --git a/laya-ts/src/presets.ts b/laya-ts/src/presets.tsnew file mode 100644index 00000000..0acd426b--- /dev/null+++ b/laya-ts/src/presets.ts@@ -0,0 +1,183 @@+export function triageQuestions(): Record<string, unknown> {+ return {+ intent: {+ type: "choice",+ instructions: "What does the customer want in `message`?",+ criteria: {+ refund: "money returned or a duplicate charge reversed",+ technical_help: "a bug, outage or integration problem",+ billing_question: "a question about an invoice, plan or payment method",+ information: "general information, pricing or how-to",+ cancellation: "wants to cancel or downgrade",+ other: "none of the other options fits",+ },+ },+ is_urgent: {+ type: "noul",+ instructions: "Does `message` communicate time pressure or a deadline?",+ },+ frustration: {+ type: "score",+ instructions: "How frustrated does the customer sound in `message`?",+ criteria: [+ "calm and neutral",+ "concerned but civil",+ "clearly annoyed",+ "very angry or using strong language",+ ],+ },+ refund_requested: {+ type: "noul",+ instructions: "Does the customer ask for money back?",+ },+ churn_risk: {+ type: "noul",+ instructions: "Does `message` suggest the customer may leave for a competitor or cancel?",+ },+ };+}++export function emailQuestions(categories?: Record<string, string>): Record<string, unknown> {+ const cats: Record<string, string> = categories ?? {+ billing: "invoices, payments, refunds",+ technical: "bugs, outages, integrations",+ sales: "pricing, demos, new purchases",+ security: "phishing, scams, account compromise",+ hr: "hiring, leave, payroll",+ other: "none of the above",+ };+ return {+ category: {+ type: "choice",+ instructions: "Which team should handle the email in `body`?",+ criteria: cats,+ },+ is_spam: {+ type: "noul",+ instructions: "Is this email unsolicited spam or bulk marketing?",+ },+ is_phishing: {+ type: "noul",+ instructions:+ "Is this email a phishing or scam attempt to steal money, credentials, or personal data?",+ criteria: { true: "phishing, scam, or fraud", false: "a legitimate email" },+ },+ urgency: {+ type: "score",+ instructions: "How urgent is the request in `body`?",+ criteria: ["no time pressure", "needs attention soon", "blocking issue or hard deadline"],+ },+ needs_reply: {+ type: "noul",+ instructions: "Does the sender expect a reply?",+ },+ };+}++export function guardQuestions(): Record<string, unknown> {+ return {+ jailbreak: {+ type: "noul",+ instructions:+ "Does `prompt` try to make an AI assistant ignore its rules, policies or system instructions?",+ },+ prompt_injection: {+ type: "noul",+ instructions:+ "Does `prompt` contain instructions aimed at the AI system rather than a genuine user request?",+ },+ sensitive_data: {+ type: "noul",+ instructions: "Does `prompt` contain credentials, personal data or other sensitive information?",+ },+ harm_severity: {+ type: "score",+ instructions: "How much harm would complying with `prompt` cause?",+ criteria: [+ "none: ordinary request",+ "minor: mildly inappropriate",+ "serious: unsafe advice or abuse",+ "severe: dangerous or illegal",+ ],+ },+ topic: {+ type: "choice",+ instructions: "What is `prompt` about?",+ criteria: {+ product_support: null,+ coding: null,+ general_knowledge: null,+ personal_advice: null,+ security_testing: null,+ other: null,+ },+ },+ };+}++export function moderationQuestions(): Record<string, unknown> {+ return {+ toxic: {+ type: "noul",+ instructions:+ "Is `post` toxic: rude, disrespectful or likely to make someone leave the discussion?",+ },+ harassment: {+ type: "noul",+ instructions: "Does `post` target or harass a specific person?",+ },+ threat: {+ type: "noul",+ instructions: "Does `post` threaten violence, harm or intimidation?",+ },+ spam: {+ type: "noul",+ instructions: "Is `post` spam or advertising?",+ },+ severity: {+ type: "score",+ instructions: "How severe is any rule-breaking in `post`?",+ criteria: [+ "no rule-breaking: ordinary on-topic post",+ "mild: rude tone or off-topic, no target",+ "clear violation: insults, harassment or spam aimed at someone",+ "severe: threats, hate speech or calls for violence",+ ],+ },+ };+}++export function routerQuestions(): Record<string, unknown> {+ return {+ difficulty: {+ type: "score",+ instructions: "How hard is `request` for a language model?",+ criteria: [+ "trivial: a lookup or one-liner",+ "easy: short answer, no reasoning",+ "moderate: several steps",+ "hard: long multi-step reasoning or specialist knowledge",+ ],+ },+ domain: {+ type: "choice",+ instructions: "What domain does `request` belong to?",+ criteria: {+ code: "software engineering, programming, refactoring, architecture, debugging",+ math_or_logic: "mathematics, logic puzzles, proofs, complex calculation",+ writing: "creative writing, essays, emails, blog posts, copywriting",+ factual_lookup: "facts, definitions, trivia, history",+ data_analysis: "statistics, SQL, data manipulation, metrics",+ chitchat: "casual conversation, greetings, small talk",+ },+ },+ needs_tools: {+ type: "noul",+ instructions: "Does answering `request` require external tools, search or private data?",+ },+ is_sensitive: {+ type: "noul",+ instructions: "Does `request` involve money, legal, medical or safety consequences?",+ },+ };+}diff --git a/laya-ts/src/providers.ts b/laya-ts/src/providers.tsnew file mode 100644index 00000000..91084b08--- /dev/null+++ b/laya-ts/src/providers.ts@@ -0,0 +1,422 @@+/** ONNX session shim: Node (onnxruntime-node) + browser (onnxruntime-web).+ * Lazy imports only — unit tests with a fake provider never touch onnxruntime. */++export interface Batch {+ inputIds: number[][];+ attentionMask: number[][];+ markerPos: number[][];+ markerMask: boolean[][];+ qtype: number[];+}++export interface SessionProvider {+ runEncoder(batch: Batch): Promise<{ lastHidden: number[][][] }>;+ runHead(hidden: number[][][] | unknown, batch: Batch): Promise<{ logits: number[][]; act: number[][] }>;+}++function toNested(data: ArrayLike<number | bigint | boolean>, dims: number[]): any {+ const flat = Array.from(data as any, (v: any) => (typeof v === "bigint" ? Number(v) : v));+ if (dims.length === 0) return flat[0];+ const rec = (d: number, off: number): any => {+ if (d === dims.length - 1) return flat.slice(off, off + dims[d]);+ const step = dims.slice(d + 1).reduce((a, b) => a * b, 1);+ const out: any[] = [];+ for (let i = 0; i < dims[d]; i++) out.push(rec(d + 1, off + i * step));+ return out;+ };+ return rec(0, 0);+}++function i64(ort: any, arr: number[] | number[][], dims: number[]): any {+ const flat = (arr as any).flat(Infinity).map((v: number) => BigInt(Math.trunc(v)));+ return new ort.Tensor("int64", BigInt64Array.from(flat), dims);+}++/** Encoder feeds: input_ids + attention_mask (int64). */+export function feed(ort: any, b: Batch): Record<string, any> {+ const n = b.inputIds.length;+ const L = Math.max(1, ...b.inputIds.map((r) => r.length));+ return {+ input_ids: i64(ort, b.inputIds, [n, L]),+ attention_mask: i64(ort, b.attentionMask, [n, L]),+ };+}++/** Head feeds: encoder hidden + marker_pos/mask + qtype. */+export function feedHead(ort: any, hidden: number[][][] | any, b: Batch): Record<string, any> {+ const n = b.markerPos.length;+ const k = Math.max(1, ...b.markerPos.map((r) => r.length));+ const H = new ort.Tensor(+ "float32",+ Float32Array.from((hidden as any).flat(Infinity).map(Number)),+ [(hidden as any).length ?? n, (hidden as any)[0]?.length ?? 1, (hidden as any)[0]?.[0]?.length ?? 1],+ );+ const S = (hidden as any)[0]?.length ?? 1;+ // Pad/trim mask rows to S so the mask always matches hidden_states even+ // if a caller passes unpadded rows.+ const maskRows = b.attentionMask.map((r) => {+ const row = r.slice(0, S);+ while (row.length < S) row.push(0);+ return row;+ });+ return {+ hidden_states: H,+ marker_pos: i64(ort, b.markerPos, [n, k]),+ marker_mask: new ort.Tensor(+ "bool",+ Uint8Array.from((b.markerMask as any).flat(Infinity).map((v: any) => (v ? 1 : 0))),+ [n, k],+ ),+ qtype: i64(ort, b.qtype.map((v) => [v]), [n, 1]),+ // Padding mask for the head transformer (py DecisionModel.forward).+ // Without it, batch mates of unequal length corrupt each other's markers.+ attention_mask: i64(ort, maskRows, [n, S]),+ };+}++function pickOutput(out: Record<string, any>, names: string[]): any {+ for (const n of names) if (out[n] !== undefined) return out[n];+ const vals = Object.values(out);+ return vals[0];+}++export interface ProviderOptions {+ device?: string;+ numThreads?: number;+}++function applyNumThreads(ort: any, numThreads?: number): void {+ try {+ const raw =+ numThreads ??+ (typeof process !== "undefined" ? Number((process as any).env?.["LAYA_THREADS"]) : NaN);+ if (Number.isFinite(raw) && (raw as number) > 0 && ort?.env) {+ ort.env.numThreads = Math.trunc(raw as number);+ }+ } catch {+ /* best-effort only */+ }+}++function isOomError(e: unknown): boolean {+ const m = String((e as any)?.message ?? e).toLowerCase();+ return m.includes("memory") || m.includes("cuda") || m.includes("out of memory") || m.includes("oom");+}++/** Online-first fetch: try network, cache on success, fall back to CacheStorage. */+async function fetchArrayBuffer(url: string): Promise<ArrayBuffer> {+ const g = globalThis as unknown as { caches?: any };+ let cache: any = null;+ let hit: any = null;+ try {+ if (g.caches && typeof g.caches.open === "function") {+ try {+ cache = await g.caches.open("laya-ts");+ try {+ hit = await cache.match(url);+ } catch {+ hit = null;+ }+ } catch {+ cache = null;+ }+ }+ } catch {+ cache = null;+ }+ if (cache) {+ try {+ const res = await fetch(url);+ if (res.ok) {+ try {+ await cache.put(url, res.clone());+ } catch {+ /* cache full/blocked: still return network bytes */+ }+ return await res.arrayBuffer();+ }+ } catch (e) {+ if (hit) {+ try {+ return await hit.arrayBuffer();+ } catch {+ /* fall through to throw original */+ }+ }+ throw e;+ }+ if (hit) {+ try {+ return await hit.arrayBuffer();+ } catch {+ /* fall through to direct error below */+ }+ }+ throw new Error(`fetch failed for ${url}`);+ }+ const res = await fetch(url);+ if (!res.ok) throw new Error(`fetch failed for ${url}: ${res.status}`);+ return await res.arrayBuffer();+}++async function fetchJson(url: string): Promise<unknown> {+ const buf = await fetchArrayBuffer(url);+ return JSON.parse(new TextDecoder().decode(buf));+}++export interface NodeBundle {+ dir: string;+ cfg: any;+ tokenizerJson: unknown | null;+}++export async function loadNodeBundle(+ modelDirOrRepo: string,+ opts?: { subfolder?: string | null; localDir?: string; token?: string | null },+): Promise<NodeBundle> {+ const fs: typeof import("node:fs/promises") = await import("node:fs/promises");+ const path: typeof import("node:path") = await import("node:path");+ const os: typeof import("node:os") = await import("node:os");+ const sub = opts?.subfolder ?? null;+ let dir = opts?.localDir ?? modelDirOrRepo;+ try {+ const st = await fs.stat(sub ? path.join(dir, sub) : dir);+ if (st.isDirectory()) dir = sub ? path.join(dir, sub) : dir;+ else dir = path.dirname(dir);+ } catch {+ const cache = path.join(+ os.homedir(),+ ".cache",+ "laya-ts",+ "hf",+ modelDirOrRepo.replace(/\//g, "__"),+ sub ?? "root",+ );+ await fs.mkdir(cache, { recursive: true });+ const token =+ opts?.token ?? (typeof process !== "undefined" ? (process as any).env?.["HF_TOKEN"] : undefined);+ for (const f of ["rl_agent_config.json", "tokenizer.json", "encoder.onnx", "head.onnx"]) {+ try {+ await fs.stat(path.join(cache, f));+ } catch {+ const url = `https://huggingface.co/${modelDirOrRepo}/resolve/main/${sub ? sub + "/" : ""}${f}`;+ const res = await fetch(url, token ? { headers: { Authorization: `Bearer ${token}` } } : undefined);+ if (!res.ok) {+ if (f === "rl_agent_config.json") {+ throw new Error(+ `Incompatible model: ${JSON.stringify(modelDirOrRepo)} does not contain 'rl_agent_config.json'.`,+ );+ }+ continue;+ }+ await fs.writeFile(path.join(cache, f), new Uint8Array(await res.arrayBuffer()));+ }+ }+ dir = cache;+ }+ let cfg: any = {};+ try {+ cfg = JSON.parse(await fs.readFile(path.join(dir, "rl_agent_config.json"), "utf8"));+ } catch {+ throw new Error(+ `Incompatible model: ${JSON.stringify(modelDirOrRepo)} does not contain 'rl_agent_config.json'.`,+ );+ }+ let tokenizerJson: unknown | null = null;+ try {+ tokenizerJson = JSON.parse(await fs.readFile(path.join(dir, "tokenizer.json"), "utf8"));+ } catch {+ tokenizerJson = null;+ }+ return { dir, cfg, tokenizerJson };+}++export interface WebBundle {+ dir: string;+ cfg: any;+ tokenizerJson: unknown | null;+}++function baseUrlFor(repoOrUrl: string, subfolder?: string | null): string {+ const sub = subfolder ? `/${subfolder.replace(/^\/+|\/+$/g, "")}` : "";+ if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(repoOrUrl)) {+ return `${repoOrUrl.replace(/\/+$/, "")}${sub}`;+ }+ return `https://huggingface.co/${repoOrUrl}/resolve/main${sub}`;+}++export async function loadWebBundle(+ repoOrUrl: string,+ opts?: { subfolder?: string | null },+): Promise<WebBundle> {+ const base = baseUrlFor(repoOrUrl, opts?.subfolder ?? null);+ let cfg: any;+ try {+ cfg = await fetchJson(`${base}/rl_agent_config.json`);+ } catch {+ throw new Error(`Incompatible model: ${JSON.stringify(repoOrUrl)} does not contain 'rl_agent_config.json'.`);+ }+ let tokenizerJson: unknown | null = null;+ try {+ tokenizerJson = await fetchJson(`${base}/tokenizer.json`);+ } catch {+ tokenizerJson = null;+ }+ return { dir: base, cfg, tokenizerJson };+}++export async function createNodeProvider(+ modelDir: string,+ opts?: ProviderOptions,+): Promise<SessionProvider> {+ const spec = "onnxruntime-" + "node";+ const ort: any = await import(/* @vite-ignore */ spec);+ applyNumThreads(ort, opts?.numThreads);+ try {+ const fs: typeof import("node:fs/promises") = await import("node:fs/promises");+ const path: typeof import("node:path") = await import("node:path");+ for (const f of ["encoder.onnx", "head.onnx"]) {+ const p = path.join(modelDir, f);+ try {+ await fs.stat(p);+ } catch {+ throw new Error(`Incompatible model: '${f}' not found in ${JSON.stringify(modelDir)} (expected ${p}).`);+ }+ }+ } catch (e) {+ if (e instanceof Error && e.message.includes("not found in")) throw e;+ }+ const dev = String(opts?.device ?? "cpu").toLowerCase();+ const want = dev === "cuda" ? "cuda" : dev === "dml" ? "dml" : "cpu";+ const make = async (ep: string) => {+ const e = await ort.InferenceSession.create(`${modelDir}/encoder.onnx`, {+ executionProviders: [ep],+ });+ const h = await ort.InferenceSession.create(`${modelDir}/head.onnx`, {+ executionProviders: ["cpu"],+ });+ return { e, h };+ };+ let enc: any;+ let head: any;+ let activeEP = want;+ try {+ ({ e: enc, h: head } = await make(want));+ } catch (e) {+ if (want !== "cpu") {+ console.warn(`Warning: ${want.toUpperCase()} requested but not available. Falling back to CPU.`);+ ({ e: enc, h: head } = await make("cpu"));+ activeEP = "cpu";+ } else {+ throw e;+ }+ }+ let cpuEnc: any = null;+ let cpuHead: any = null;+ const ensureCpu = async () => {+ if (!cpuEnc) {+ cpuEnc = await ort.InferenceSession.create(`${modelDir}/encoder.onnx`, {+ executionProviders: ["cpu"],+ });+ cpuHead = await ort.InferenceSession.create(`${modelDir}/head.onnx`, {+ executionProviders: ["cpu"],+ });+ }+ return { cpuEnc, cpuHead };+ };+ const runWithCpuFallback = async <T>(fn: (e: any, h: any) => Promise<T>): Promise<T> => {+ try {+ return await fn(enc, head);+ } catch (e) {+ if (activeEP !== "cpu" && isOomError(e)) {+ console.warn("Warning: GPU memory exceeded during inference. Falling back to CPU...");+ const { cpuEnc: ce, cpuHead: ch } = await ensureCpu();+ enc = ce;+ head = ch;+ activeEP = "cpu";+ return await fn(enc, head);+ }+ if (isOomError(e)) {+ throw new Error(`${(e as Error).message} (GPU out of memory; try device: "cpu")`);+ }+ throw e;+ }+ };+ return {+ runEncoder: async (b) =>+ runWithCpuFallback(async (e) => {+ const out = await e.run(feed(ort, b));+ const t = pickOutput(out, ["last_hidden_state", "lastHidden", "hidden_states"]);+ return { lastHidden: toNested(t.data, t.dims) };+ }),+ runHead: async (h, b) =>+ runWithCpuFallback(async (_e, hd) => {+ const out = await hd.run(feedHead(ort, h, b));+ const vals = Object.values(out) as any[];+ const lt = pickOutput(out, ["logits"]);+ const at = pickOutput(out, ["act_logits", "act"]) ?? vals[1] ?? vals[0];+ return { logits: toNested(lt.data, lt.dims), act: toNested(at.data, at.dims) };+ }),+ };+}++export async function createWebProvider(+ modelUrl: string,+ opts?: ProviderOptions,+): Promise<SessionProvider> {+ const spec = "onnxruntime-" + "web";+ const ort: any = await import(/* @vite-ignore */ spec);+ applyNumThreads(ort, opts?.numThreads);+ const base = modelUrl.replace(/\/+$/, "");+ const encUrl = `${base}/encoder.onnx`;+ const headUrl = `${base}/head.onnx`;+ let encBuf: ArrayBuffer;+ try {+ encBuf = await fetchArrayBuffer(encUrl);+ } catch {+ throw new Error(`Incompatible model: 'encoder.onnx' not found (expected ${encUrl}).`);+ }+ let headBuf: ArrayBuffer;+ try {+ headBuf = await fetchArrayBuffer(headUrl);+ } catch {+ throw new Error(`Incompatible model: 'head.onnx' not found (expected ${headUrl}).`);+ }+ let enc: any;+ try {+ enc = await ort.InferenceSession.create(new Uint8Array(encBuf), {+ executionProviders: ["webgpu", "wasm"],+ });+ } catch (e) {+ enc = await ort.InferenceSession.create(new Uint8Array(encBuf), {+ executionProviders: ["wasm"],+ });+ }+ const head = await ort.InferenceSession.create(new Uint8Array(headBuf), {+ executionProviders: ["wasm"],+ });+ return {+ runEncoder: async (b) => {+ try {+ const out = await enc.run(feed(ort, b));+ const t = pickOutput(out, ["last_hidden_state", "lastHidden", "hidden_states"]);+ return { lastHidden: toNested(t.data, t.dims) };+ } catch (e) {+ if (isOomError(e)) throw new Error(`${(e as Error).message} (WebGPU out of memory; WASM fallback already active)`);+ throw e;+ }+ },+ runHead: async (h, b) => {+ try {+ const out = await head.run(feedHead(ort, h, b));+ const vals = Object.values(out) as any[];+ const lt = pickOutput(out, ["logits"]);+ const at = pickOutput(out, ["act_logits", "act"]) ?? vals[1] ?? vals[0];+ return { logits: toNested(lt.data, lt.dims), act: toNested(at.data, at.dims) };+ } catch (e) {+ if (isOomError(e)) throw new Error(`${(e as Error).message} (out of memory; try fewer questions per call)`);+ throw e;+ }+ },+ };+}diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.tsnew file mode 100644index 00000000..ee38b483--- /dev/null+++ b/laya-ts/src/router.ts@@ -0,0 +1,385 @@+import { analyse, type AnalyseResult } from "./lang.js";+import type { QuestionDef, SystemOneResult } from "./agent.js";++export const BUNDLE_REPO = "convaiinnovations/laya";++export interface ModelSpec {+ repo: string;+ subfolder: string | null;+}++export const DEFAULT_MODELS: Record<string, ModelSpec> = {+ english: { repo: BUNDLE_REPO, subfolder: null },+ multilingual: { repo: BUNDLE_REPO, subfolder: "multilingual" },+ "typed-decisions": { repo: BUNDLE_REPO, subfolder: "typed-decisions" },+};++export const STANDALONE_MODELS: Record<string, string> = {+ english: "convaiinnovations/laya",+ multilingual: "convaiinnovations/laya-multilingual",+ "typed-decisions": "convaiinnovations/laya-typed-decisions",+};++export type ModelName = "english" | "multilingual" | "typed-decisions";++const ALIASES: Record<string, ModelName> = {+ en: "english",+ laya: "english",+ default: "english",+ multi: "multilingual",+ ml: "multilingual",+ "laya-multilingual": "multilingual",+ typed: "typed-decisions",+ typed_decisions: "typed-decisions",+ "laya-typed-decisions": "typed-decisions",+ decisions: "typed-decisions",+};++export function normaliseName(name: string): ModelName {+ const raw = String(name).trim().toLowerCase();+ const key = ALIASES[raw] ?? raw;+ if (!(key in DEFAULT_MODELS)) {+ throw new Error(+ `unknown model ${JSON.stringify(name)}; choose one of ${JSON.stringify(+ Object.keys(DEFAULT_MODELS).sort(),+ )} (or an alias: ${JSON.stringify(Object.keys(ALIASES).sort())})`,+ );+ }+ return key as ModelName;+}++const TYPED_DECISION_WORKFLOWS: Record<string, Set<string>> = {+ agent_trace_observability: new Set(["action", "needs_review", "outcome", "risk", "urgency"]),+ customer_service: new Set(["action", "category", "churn_risk", "needs_human", "urgency"]),+ invoice_processing: new Set(["discrepancy_severity", "disposition", "duplicate", "matches_order", "urgency"]),+ security_incidents: new Set(["credential_compromise", "disposition", "severity", "true_positive", "urgency"]),+};++export function matchTypedDecisionsWorkflow(+ questions: Record<string, unknown> | null | undefined,+): string | null {+ const ids = new Set(Object.keys(questions ?? {}));+ for (const [wf, sig] of Object.entries(TYPED_DECISION_WORKFLOWS)) {+ if (ids.size !== sig.size) continue;+ let same = true;+ for (const id of sig) {+ if (!ids.has(id)) {+ same = false;+ break;+ }+ }+ if (same) return wf;+ }+ return null;+}++const ENGLISH_SUBTAGS = new Set(["en", "eng", "english"]);++export function englishFromCode(value: unknown): boolean | null {+ if (value === null || value === undefined) return null;+ let code = String(value).trim().toLowerCase();+ if (!code) return null;+ code = code.split(".", 1)[0]; // en_US.UTF-8 -> en_US+ const primary = code.replace(/_/g, "-").split("-", 1)[0]; // en_US -> en+ if (!primary) return null;+ return ENGLISH_SUBTAGS.has(primary);+}++/** Parity alias for the Python `_english_from_code` name. */+export const _englishFromCode = englishFromCode;++export interface RouteDecision {+ model: ModelName;+ repo: string;+ reason: string;+ detection: AnalyseResult | null;+ workflow: string | null;+}++export type RoutedResult = SystemOneResult & { routing: RouteDecision };++export type LangGuess = string | null | undefined | ((state: unknown) => unknown);++export type AgentLoader = (name: ModelName, spec: ModelSpec) => unknown | Promise<unknown>;++export interface RouterOptions {+ models?: Record<string, string | ModelSpec | [string, string | null]>;+ device?: string | null;+ token?: string | null;+ maxLoaded?: number;+ max_loaded?: number;+ default?: string;+ autoTaskDetection?: boolean;+ auto_task_detection?: boolean;+ standaloneRepos?: boolean;+ standalone_repos?: boolean;+ preload?: boolean | string[];+ langGuess?: LangGuess;+ lang_guess?: LangGuess;+ loader?: AgentLoader;+}++export interface RouteOptions {+ model?: string | null;+ task?: string | null;+ lang?: string | null;+ langGuess?: LangGuess;+ lang_guess?: LangGuess;+}++function toSpec(spec: string | ModelSpec | [string, string | null]): ModelSpec {+ if (typeof spec === "string") return { repo: spec, subfolder: null };+ if (Array.isArray(spec)) {+ const [repo, sub] = [...spec, null].slice(0, 2) as [string, string | null];+ return { repo, subfolder: sub ?? null };+ }+ return { repo: spec.repo, subfolder: spec.subfolder ?? null };+}++function repoStr(spec: ModelSpec): string {+ return spec.subfolder ? `${spec.repo}/${spec.subfolder}` : spec.repo;+}++export class Router {+ models: Record<string, ModelSpec>;+ device: string | null;+ token: string | null | undefined;+ maxLoaded: number;+ default: ModelName;+ autoTaskDetection: boolean;+ langGuess: LangGuess;+ loader: AgentLoader | null;+ _agents: Map<string, unknown> = new Map();+ _order: string[] = []; // least-recently-used first++ constructor(opts: RouterOptions = {}) {+ const base: Record<string, string | ModelSpec> = opts.standaloneRepos ?? opts.standalone_repos+ ? { ...STANDALONE_MODELS }+ : Object.fromEntries(Object.entries(DEFAULT_MODELS).map(([k, v]) => [k, { ...v }]));+ this.models = Object.fromEntries(Object.entries(base).map(([k, v]) => [k, toSpec(v)]));+ if (opts.models) {+ for (const [k, v] of Object.entries(opts.models)) {+ this.models[normaliseName(k)] = toSpec(v);+ }+ }+ this.device = opts.device ?? null;+ this.token = opts.token ?? (typeof process !== "undefined" ? process.env?.["HF_TOKEN"] : undefined);+ this.maxLoaded = Math.max(1, Math.trunc(Number(opts.maxLoaded ?? opts.max_loaded ?? 2)));+ this.default = normaliseName(opts.default ?? "english");+ this.autoTaskDetection = Boolean(opts.autoTaskDetection ?? opts.auto_task_detection ?? false);+ this.langGuess = opts.langGuess ?? opts.lang_guess ?? null;+ this.loader = opts.loader ?? null;+ if (opts.preload === true) {+ void this.preload();+ } else if (Array.isArray(opts.preload)) {+ void this.preload(opts.preload);+ }+ }++ async load(name: string): Promise<unknown> {+ const key = normaliseName(name);+ if (this._agents.has(key)) {+ this._touch(key);+ return this._agents.get(key);+ }+ let agent: unknown;+ if (this.loader) {+ agent = await this.loader(key, this.models[key]);+ } else {+ const { Agent } = await import("./agent.js");+ const spec = this.models[key];+ agent = await (Agent as unknown as {+ load(repo: string, opts?: Record<string, unknown>): Promise<unknown>;+ }).load(spec.repo, {+ subfolder: spec.subfolder,+ device: this.device ?? undefined,+ token: this.token ?? undefined,+ });+ }+ this._agents.set(key, agent);+ this._order.push(key);+ this._evict();+ return agent;+ }++ _touch(key: string): void {+ const i = this._order.indexOf(key);+ if (i !== -1) this._order.splice(i, 1);+ this._order.push(key);+ }++ _evict(): void {+ while (this._order.length > this.maxLoaded) {+ const victim = this._order.shift()!;+ this._agents.delete(victim);+ }+ // Keep the two views consistent.+ if (this._order.length < this._agents.size) {+ for (const k of [...this._agents.keys()]) {+ if (!this._order.includes(k)) this._agents.delete(k);+ }+ }+ }++ attach(name: string, agent: unknown): unknown {+ const key = normaliseName(name);+ this._agents.set(key, agent);+ this._touch(key);+ this.maxLoaded = Math.max(this.maxLoaded, this._agents.size);+ return agent;+ }++ async preload(names?: string[]): Promise<this> {+ const keys = (names ?? Object.keys(this.models)).map((n) => normaliseName(n));+ this.maxLoaded = Math.max(this.maxLoaded, new Set([...keys, ...this._agents.keys()]).size);+ for (const n of keys) {+ if (!this._agents.has(n)) await this.load(n);+ }+ return this;+ }++ unload(name?: string | null): void {+ if (name === null || name === undefined) {+ this._agents.clear();+ this._order = [];+ } else {+ const key = normaliseName(name);+ this._agents.delete(key);+ const i = this._order.indexOf(key);+ if (i !== -1) this._order.splice(i, 1);+ }+ }++ get loaded(): string[] {+ return [...this._order];+ }++ _resolveHint(hint: LangGuess, state: unknown): boolean | null {+ if (hint === null || hint === undefined) return null;+ const value = typeof hint === "function" ? (hint as (s: unknown) => unknown)(state) : hint;+ return englishFromCode(value);+ }++ route(+ state: unknown,+ questions: Record<string, unknown> | null = null,+ opts: RouteOptions = {},+ ): RouteDecision {+ const { model = null, task = null, lang = null } = opts;+ const langGuessOpt = opts.langGuess ?? opts.lang_guess ?? null;++ if (model !== null && model !== undefined) {+ const key = normaliseName(model);+ return {+ model: key,+ repo: repoStr(this.models[key]),+ reason: `explicit model=${JSON.stringify(model)}`,+ detection: null,+ workflow: null,+ };+ }++ if (task !== null && task !== undefined) {+ const t = String(task).toLowerCase().replace(/-/g, "_") === "typed_decisions" ? "typed-decisions" : task;+ const key = normaliseName(t);+ return {+ model: key,+ repo: repoStr(this.models[key]),+ reason: `explicit task=${JSON.stringify(task)}`,+ detection: null,+ workflow: null,+ };+ }++ const workflow = matchTypedDecisionsWorkflow(questions ?? {});+ if (workflow && this.autoTaskDetection) {+ return {+ model: "typed-decisions",+ repo: repoStr(this.models["typed-decisions"]),+ reason: `question ids match the ${JSON.stringify(workflow)} typed-decisions workflow`,+ detection: null,+ workflow,+ };+ }++ if (lang !== null && lang !== undefined) {+ const key: ModelName = englishFromCode(lang) ? "english" : "multilingual";+ return {+ model: key,+ repo: repoStr(this.models[key]),+ reason: `explicit lang=${JSON.stringify(lang)}`,+ detection: null,+ workflow,+ };+ }++ const hints: Array<[string, LangGuess]> = [+ ["lang_guess", langGuessOpt],+ ["Router(lang_guess=...)", this.langGuess],+ ];+ for (const [source, hint] of hints) {+ const resolved = this._resolveHint(hint, state);+ if (resolved !== null && resolved !== undefined) {+ const key: ModelName = resolved ? "english" : "multilingual";+ return {+ model: key,+ repo: repoStr(this.models[key]),+ reason: `${source}: the caller identified this as ${resolved ? "English" : "non-English"} text`,+ detection: null,+ workflow,+ };+ }+ }++ const det = analyse(state);+ let key: ModelName;+ let reason: string;+ if (det.script === "unknown") {+ key = this.default;+ reason = `no letters detected in state; using default (${key})`;+ } else if (det.script !== "latin") {+ key = "multilingual";+ reason =+ `non-Latin script (${det.script}, ${Math.round(100 * det.nonLatinFraction)}% of letters); ` ++ "the English checkpoint cannot read it";+ } else if (!det.isEnglish) {+ key = "multilingual";+ if (det.language) {+ reason = `Latin script but language looks like ${JSON.stringify(det.language)}, not English`;+ } else {+ reason =+ `Latin script, language not identified but ${Math.round(100 * det.diacriticRate)}% ` ++ "non-English letters; not safe for the English checkpoint";+ }+ } else if (det.languageUndecided) {+ key = this.default;+ reason = `Latin script, language not identified and no non-English letters; using default (${key})`;+ } else {+ key = "english";+ reason = "English Latin text";+ }+ return { model: key, repo: repoStr(this.models[key]), reason, detection: det, workflow };+ }++ async predict(+ state: unknown,+ questions: Record<string, QuestionDef>,+ opts: RouteOptions = {},+ ): Promise<RoutedResult> {+ const decision = this.route(state, questions, opts);+ const agent = (await this.load(decision.model)) as {+ systemOne(s: unknown, q: Record<string, QuestionDef>): Promise<SystemOneResult>;+ };+ const result = (await agent.systemOne(state, questions)) as RoutedResult;+ result["routing"] = { ...decision };+ return result;+ }++ async systemOne(+ state: unknown,+ questions: Record<string, QuestionDef>,+ opts: RouteOptions = {},+ ): Promise<RoutedResult> {+ return this.predict(state, questions, opts);+ }+}diff --git a/laya-ts/src/shortlist.ts b/laya-ts/src/shortlist.tsnew file mode 100644index 00000000..89b8d0f0--- /dev/null+++ b/laya-ts/src/shortlist.ts@@ -0,0 +1,218 @@+import { renderOptions, serializeState } from "./common.js";+import type { Batch } from "./providers.js";+import type { QuestionDef, SystemOneResult } from "./agent.js";++export const DEFAULT_SHORTLIST_K = 20;++export type EmbedFn = (texts: string[]) => Promise<number[][]> | number[][];++export interface ShortlistMeta {+ labels: string[];+ scores: number[] | null;+ k: number;+ n: number;+ passthrough: boolean;+}++function checkK(k: unknown): number {+ if (typeof k !== "number" || !Number.isInteger(k) || k < 1) {+ throw new Error(`k must be a positive integer, got ${JSON.stringify(k)}`);+ }+ return k;+}++function criteriaItems(criteria: unknown): [string, unknown][] {+ let items: [string, unknown][];+ if (Array.isArray(criteria)) {+ items = (criteria as unknown[]).map((item) => [String(item), null] as [string, unknown]);+ } else if (typeof criteria === "object" && criteria !== null) {+ items = Object.entries(criteria as Record<string, unknown>);+ } else {+ throw new TypeError(+ `choice criteria must be a dict or list, got ${Array.isArray(criteria) ? "list" : criteria === null ? "NoneType" : typeof criteria}`,+ );+ }+ if (items.length === 0) throw new Error("choice criteria must contain at least one option");+ const seen = new Set<string>();+ for (const [key] of items) {+ if (seen.has(key)) throw new Error(`choice criteria label ${JSON.stringify(key)} is duplicated`);+ seen.add(key);+ }+ return items;+}++function optionTexts(items: [string, unknown][]): string[] {+ const crit: Record<string, unknown> = Object.fromEntries(items);+ const rendered = renderOptions({ t: "choice", ins: "", crit });+ if (rendered.length !== items.length) throw new Error("could not render every choice option");+ return rendered.map((p) => (typeof p === "string" ? p : String(p)));+}++function queryText(state: unknown, instructions?: unknown): string {+ const body = serializeState(state);+ if (instructions === null || instructions === undefined || instructions === "") return body;+ if (typeof instructions !== "string") return `${JSON.stringify(instructions)}\n${body}`;+ return `${instructions}\n${body}`;+}++function clean(v: number): number {+ return typeof v !== "number" || !Number.isFinite(v) ? 0 : v;+}++async function embeddings(embedFn: unknown, texts: string[]): Promise<number[][]> {+ if (typeof embedFn !== "function") throw new TypeError("embed_fn must be callable");+ const raw = await (embedFn as EmbedFn)([...texts]);+ const arr = raw as unknown;+ if (!Array.isArray(arr) || (arr as unknown[]).length !== texts.length) {+ throw new Error(`embed_fn must return an array of shape (${texts.length}, dim), got ${Array.isArray(arr) ? `(${(arr as unknown[]).length}, ?)` : typeof arr}`);+ }+ const rows = (arr as unknown[]).map((row) => {+ if (!Array.isArray(row) || row.length < 1) {+ throw new Error(`embed_fn must return an array of shape (${texts.length}, dim), got row ${JSON.stringify(row)?.slice(0, 60)}`);+ }+ return (row as unknown[]).map((v) => clean(Number(v)));+ });+ return rows;+}++function cosine(query: number[], docs: number[][]): number[] {+ const qn = Math.sqrt(query.reduce((a, v) => a + v * v, 0));+ if (qn === 0 || docs.length === 0) return docs.map(() => 0);+ return docs.map((d) => {+ const dn = Math.sqrt(d.reduce((a, v) => a + v * v, 0));+ if (dn === 0) return 0;+ const dot = d.reduce((a, v, i) => a + v * (query[i] ?? 0), 0);+ return Math.min(1, Math.max(-1, dot / (dn * qn)));+ });+}++async function rank(+ state: unknown,+ criteria: unknown,+ embedFn: EmbedFn,+ k: number,+ instructions?: unknown,+): Promise<{ labels: string[]; scores: number[] | null; passthrough: boolean; n: number }> {+ const checked = checkK(k);+ const items = criteriaItems(criteria);+ const n = items.length;+ const keys = items.map(([key]) => key);+ if (checked >= n) return { labels: [...keys], scores: null, passthrough: true, n };+ const q = queryText(state, instructions);+ const matrix = await embeddings(embedFn, [q, ...optionTexts(items)]);+ const sims = cosine(matrix[0], matrix.slice(1));+ const order = sims+ .map((s, i) => i)+ .sort((a, b) => sims[b] - sims[a]);+ const top = order.slice(0, checked);+ return { labels: top.map((i) => keys[i]), scores: top.map((i) => sims[i]), passthrough: false, n };+}++function subsetCriteria(criteria: unknown, labels: string[]): unknown {+ if (Array.isArray(criteria)) return [...labels];+ return Object.fromEntries(labels.map((l) => [l, (criteria as Record<string, unknown>)[l]]));+}++/** Return the top-k choice labels for state. Skips embed_fn when k >= n. */+export async function shortlistChoice(+ state: unknown,+ criteria: unknown,+ embedFn: EmbedFn,+ k: number = DEFAULT_SHORTLIST_K,+ instructions?: unknown,+): Promise<string[]> {+ const { labels } = await rank(state, criteria, embedFn, k, instructions);+ return labels;+}++/** Shortlist each choice question, then call predict/systemOne once. */+export async function predictShortlist(+ agent: unknown,+ state: unknown,+ questions: Record<string, QuestionDef>,+ embedFn: EmbedFn,+ k: number = DEFAULT_SHORTLIST_K,+ predictKwargs: Record<string, unknown> = {},+): Promise<SystemOneResult & { shortlist: Record<string, ShortlistMeta> }> {+ if (typeof questions !== "object" || questions === null || Array.isArray(questions)) {+ throw new TypeError("questions must be a dict of question id -> definition");+ }+ const checked = checkK(k);+ const reduced: Record<string, unknown> = {};+ const meta: Record<string, ShortlistMeta> = {};+ for (const [qid, qdef] of Object.entries(questions)) {+ if (typeof qdef !== "object" || qdef === null || Array.isArray(qdef) || (qdef as Record<string, unknown>)["type"] !== "choice") {+ reduced[qid] = qdef;+ continue;+ }+ if (!("criteria" in (qdef as Record<string, unknown>))) {+ throw new Error(`question ${JSON.stringify(qid)} is a choice but has no criteria`);+ }+ const q = qdef as Record<string, unknown>;+ const { labels, scores, passthrough, n } = await rank(state, q["criteria"], embedFn, checked, q["instructions"]);+ meta[qid] = { labels: [...labels], scores, k: checked, n, passthrough };+ if (passthrough) {+ reduced[qid] = qdef;+ continue;+ }+ reduced[qid] = { ...q, criteria: subsetCriteria(q["criteria"], labels) };+ }+ const a = agent as Record<string, unknown>;+ const fn = a?.["predict"] ?? a?.["systemOne"] ?? a?.["system_one"];+ if (typeof fn !== "function") throw new TypeError("agent must provide predict or system_one");+ const extra = Object.keys(predictKwargs ?? {}).length ? [predictKwargs] : [];+ const result = await (fn as (...args: unknown[]) => Promise<unknown>).call(agent, state, reduced, ...extra);+ if (typeof result !== "object" || result === null || Array.isArray(result)) {+ throw new TypeError("predict/system_one must return a dict");+ }+ return { ...(result as SystemOneResult), shortlist: meta };+}++/** Mean-pool the agent's provider encoder. Throws honestly without encoder access. */+export function embedFnFromAgent(agent: unknown, maxLength = 512, batchSize = 32): EmbedFn {+ if (!Number.isInteger(maxLength) || (maxLength as number) < 1) {+ throw new Error(`max_length must be a positive integer, got ${JSON.stringify(maxLength)}`);+ }+ if (!Number.isInteger(batchSize) || (batchSize as number) < 1) {+ throw new Error(`batch_size must be a positive integer, got ${JSON.stringify(batchSize)}`);+ }+ const a = agent as { tok?: { encode(t: string): number[]; padId: number }; provider?: { runEncoder(b: Batch): Promise<{ lastHidden: number[][][] }> } };+ if (!a?.provider || typeof a.provider.runEncoder !== "function" || !a?.tok || typeof a.tok.encode !== "function") {+ throw new Error("embedFnFromAgent needs encoder access: agent.provider.runEncoder + agent.tok.encode required");+ }+ const tok = a.tok;+ const provider = a.provider;+ return async (texts: string[]): Promise<number[][]> => {+ const rows = (texts ?? []).map((t) => (t === null || t === undefined ? "" : String(t)));+ if (rows.length === 0) return [];+ const out: number[][] = [];+ for (let s = 0; s < rows.length; s += batchSize) {+ const chunk = rows.slice(s, s + batchSize);+ const ids = chunk.map((t) => tok.encode(t).slice(0, maxLength));+ const L = Math.max(1, ...ids.map((r) => r.length));+ const pad = tok.padId ?? 0;+ const batch: Batch = {+ inputIds: ids.map((r) => [...r, ...Array(L - r.length).fill(pad)]),+ attentionMask: ids.map((r) => [...Array(r.length).fill(1), ...Array(L - r.length).fill(0)]),+ markerPos: ids.map(() => [0]),+ markerMask: ids.map(() => [true]),+ qtype: ids.map(() => 0),+ };+ const { lastHidden } = await provider.runEncoder(batch);+ for (let i = 0; i < chunk.length; i++) {+ const mask = batch.attentionMask[i];+ const denom = Math.max(1, mask.reduce((x, y) => x + y, 0));+ const dim = lastHidden[i]?.[0] ? (lastHidden[i][0] as number[]).length : 0;+ const pooled = new Array(dim).fill(0);+ for (let j = 0; j < mask.length; j++) {+ if (!mask[j]) continue;+ const hv = lastHidden[i]?.[j] as number[] | undefined;+ if (!hv) continue;+ for (let h = 0; h < dim; h++) pooled[h] += Number(hv[h] ?? 0);+ }+ out.push(pooled.map((v) => v / denom));+ }+ }+ return out;+ };+}diff --git a/laya-ts/src/tokenizer.ts b/laya-ts/src/tokenizer.tsnew file mode 100644index 00000000..8fc67bbb--- /dev/null+++ b/laya-ts/src/tokenizer.ts@@ -0,0 +1,223 @@+export interface TokenizerLike {+ readonly clsId: number; readonly sepId: number;+ readonly maskId: number; readonly padId: number;+ readonly maskToken: string;+ encode(text: string): number[];+}++/** Special ids of the laya ModernBERT checkpoint (HF added_tokens). */+export const CHECKPOINT_IDS = { cls: 50281, sep: 50282, mask: 50284, pad: 50283, unk: 50280 } as const;++/** Alias lookup order per special: ModernBERT `[X]` names first, Gemma `<x>` names after. */+export const SPECIAL_ALIASES = {+ cls: ["[CLS]", "<bos>", "<s>"],+ sep: ["[SEP]", "<eos>", "</s>"],+ pad: ["[PAD]", "<pad>"],+ mask: ["[MASK]", "<mask>"],+ unk: ["[UNK]", "<unk>"],+} as const;++export interface TokenizerIds { cls: number; sep: number; mask: number; pad: number; unk: number }+export type PreTokenizerKind = "metaspace" | "bytelevel";+export interface TokenizerData {+ vocab: Map<string, number>;+ merges: Map<string, number>;+ ids: TokenizerIds;+ kind: PreTokenizerKind;+ maskToken: string;+ /** Normalizer Replace rules (pattern -> content) applied in order before pre-tokenizing. */+ replaces: Array<[string, string]>;+}++/** Metaspace word-boundary marker (HF SentencePiece-style replacement for ' '). */+export const METASPACE_REPLACEMENT = "▁";++/** GPT-2 byte<->unicode table (same mapping as HF ByteLevel pre-tokenizer). */+function byteUnicodeMaps(): { b2u: Map<number, string>; u2b: Map<string, number> } {+ const b2u = new Map<number, string>();+ const u2b = new Map<string, number>();+ const extra = (n: number): number => (n < 0x100 ? n + 0x100 : n);+ const ranges: Array<[number, number]> = [[0x21, 0x7e], [0xa1, 0xac], [0xae, 0xff]];+ let k = 0;+ const inRange = (b: number): boolean => ranges.some(([lo, hi]) => b >= lo && b <= hi);+ for (let b = 0; b < 256; b++) {+ const cp = inRange(b) ? b : extra(k++);+ b2u.set(b, String.fromCodePoint(cp));+ u2b.set(String.fromCodePoint(cp), b);+ }+ return { b2u, u2b };+}++let cached: { b2u: Map<number, string>; u2b: Map<string, number> } | null = null;+function maps(): { b2u: Map<number, string>; u2b: Map<string, number> } {+ if (!cached) cached = byteUnicodeMaps();+ return cached;+}++/** GPT-2 pre-tokenizer split (HF ByteLevel use_regex=true). */+const GPT2_SPLIT = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;++/** Merge one word's chars greedily by merge rank (first occurrence per step). */+function bpeWord(chars: string[], rank: Map<string, number>): string[] {+ let word = chars.slice();+ if (word.length <= 1) return word;+ for (;;) {+ let best = Infinity, idx = -1;+ for (let i = 0; i < word.length - 1; i++) {+ const r = rank.get(word[i] + " " + word[i + 1]);+ if (r !== undefined && r < best) { best = r; idx = i; }+ }+ if (idx < 0) return word;+ word = [...word.slice(0, idx), word[idx] + word[idx + 1], ...word.slice(idx + 2)];+ }+}++/** Byte-level BPE encode: NFC-normalize, NO lowercasing, GPT-2 byte map + rank-order merges. */+export function bpeEncode(vocab: Map<string, number>, merges: Map<string, number>, text: string): number[] {+ const { b2u } = maps();+ const unkId = vocab.get("[UNK]") ?? CHECKPOINT_IDS.unk;+ const out: number[] = [];+ const enc = new TextEncoder();+ const parts = text.normalize("NFC").match(GPT2_SPLIT);+ if (!parts) return out;+ for (const piece of parts) {+ const chars: string[] = [];+ for (const b of enc.encode(piece)) chars.push(b2u.get(b) ?? "");+ for (const tok of bpeWord(chars, merges)) out.push(vocab.get(tok) ?? unkId);+ }+ return out;+}++/** Metaspace (SentencePiece-style) BPE encode: unicode chars, NO byte map, NO lowercasing.+ * Normalizer replaces run first, then one marker is ensured at text start, then the text+ * is cut into words at each marker (marker kept as word prefix) with maximal `\n` runs+ * as their own pieces. Each piece is BPE-merged; leftover unknown pieces map to unk. */+export function metaspaceEncode(+ vocab: Map<string, number>,+ merges: Map<string, number>,+ text: string,+ unkId?: number,+ replaces: ReadonlyArray<readonly [string, string]> = [[" ", METASPACE_REPLACEMENT]],+): number[] {+ const unk = unkId ?? vocab.get("<unk>") ?? vocab.get("[UNK]") ?? CHECKPOINT_IDS.unk;+ if (!text) return [];+ let t = text;+ for (const [from, to] of replaces) t = t.split(from).join(to);+ const out: number[] = [];+ const push = (piece: string): void => {+ for (const tok of bpeWord(Array.from(piece), merges)) out.push(vocab.get(tok) ?? unk);+ };+ for (const seg of t.split(/(\n+)/)) {+ if (!seg) continue;+ if (seg[0] === "\n") {+ push(seg);+ } else {+ const w = seg.startsWith(METASPACE_REPLACEMENT) ? seg : METASPACE_REPLACEMENT + seg;+ for (const chunk of w.split(METASPACE_REPLACEMENT).slice(1)) {+ push(chunk ? METASPACE_REPLACEMENT + chunk : METASPACE_REPLACEMENT);+ }+ }+ }+ return out;+}++/** Dispatch to the Metaspace or GPT-2/ByteLevel encoder based on the parsed pre-tokenizer. */+export function encodeWithData(data: TokenizerData, text: string): number[] {+ return data.kind === "metaspace"+ ? metaspaceEncode(data.vocab, data.merges, text, data.ids.unk, data.replaces)+ : bpeEncode(data.vocab, data.merges, text);+}++function childNodes(node: unknown): unknown[] {+ if (!node || typeof node !== "object") return [];+ const o = node as Record<string, unknown>;+ const out: unknown[] = [];+ for (const k of ["normalizers", "pre_tokenizers", "decoders"]) {+ const v = o[k];+ if (Array.isArray(v)) out.push(...v);+ }+ return out;+}++/** True when a normalizer/pre-tokenizer/decoder node (or nested Sequence member) has a type. */+function hasNodeType(node: unknown, want: string): boolean {+ if (!node || typeof node !== "object") return false;+ if ((node as Record<string, unknown>)["type"] === want) return true;+ return childNodes(node).some((c) => hasNodeType(c, want));+}++/** Collect normalizer Replace rules ({pattern: {String}, content}) in order. */+function collectReplaces(node: unknown, out: Array<[string, string]>): void {+ if (!node || typeof node !== "object") return;+ const o = node as Record<string, unknown>;+ if (o["type"] === "Replace") {+ const pat = o["pattern"] as Record<string, unknown> | undefined;+ const from = pat?.["String"];+ const to = o["content"];+ if (typeof from === "string" && typeof to === "string") out.push([from, to]);+ }+ for (const c of childNodes(node)) collectReplaces(c, out);+}++/** Parse an HF tokenizer.json ({model vocab/merges, normalizer, pre_tokenizer, added_tokens}). */+export function parseTokenizerJson(raw: unknown): TokenizerData | null {+ try {+ const r = raw as {+ model?: { vocab?: Record<string, number>; merges?: Array<string | [string, string]> };+ normalizer?: unknown;+ pre_tokenizer?: unknown;+ added_tokens?: Array<{ id?: number; content?: string }>;+ };+ const vocabObj = r?.model?.vocab;+ if (!vocabObj || typeof vocabObj !== "object") return null;+ const vocab = new Map(Object.entries(vocabObj));+ const merges = new Map<string, number>();+ for (const [i, m] of (r.model?.merges ?? []).entries()) {+ const pair = typeof m === "string" ? m.split(" ") : m;+ if (pair.length >= 2) merges.set(pair[0] + " " + pair[1], i);+ }+ const added = new Map<string, number>();+ for (const t of r.added_tokens ?? []) {+ if (typeof t?.content === "string" && typeof t?.id === "number") added.set(t.content, t.id);+ }+ const pick = (aliases: readonly string[], fb: number): { id: number; token: string } => {+ for (const a of aliases) {+ const v = added.get(a) ?? vocab.get(a);+ if (v !== undefined) return { id: v, token: a };+ }+ return { id: fb, token: aliases[0] };+ };+ const cls = pick(SPECIAL_ALIASES.cls, CHECKPOINT_IDS.cls);+ const sep = pick(SPECIAL_ALIASES.sep, CHECKPOINT_IDS.sep);+ const mask = pick(SPECIAL_ALIASES.mask, CHECKPOINT_IDS.mask);+ const pad = pick(SPECIAL_ALIASES.pad, CHECKPOINT_IDS.pad);+ const unk = pick(SPECIAL_ALIASES.unk, CHECKPOINT_IDS.unk);+ const kind: PreTokenizerKind = hasNodeType(r?.pre_tokenizer, "Metaspace") ? "metaspace" : "bytelevel";+ const replaces: Array<[string, string]> = [];+ collectReplaces(r?.normalizer, replaces);+ if (kind === "metaspace" && replaces.length === 0) replaces.push([" ", METASPACE_REPLACEMENT]);+ return {+ vocab, merges,+ ids: { cls: cls.id, sep: sep.id, mask: mask.id, pad: pad.id, unk: unk.id },+ kind,+ maskToken: mask.token,+ replaces,+ };+ } catch {+ return null;+ }+}++/** Load an HF tokenizer.json from a local path (node) or URL into vocab/merges/ids. */+export async function loadTokenizerJson(pathOrUrl: string): Promise<TokenizerData | null> {+ let raw: unknown;+ if (/^https?:\/\//.test(pathOrUrl)) {+ const res = await fetch(pathOrUrl);+ if (!res.ok) return null;+ raw = JSON.parse(new TextDecoder().decode(await res.arrayBuffer()));+ } else {+ const fs: typeof import("node:fs/promises") = await import("node:fs/promises");+ raw = JSON.parse(await fs.readFile(pathOrUrl, "utf8"));+ }+ return parseTokenizerJson(raw);+}diff --git a/laya-ts/tests/agent.test.ts b/laya-ts/tests/agent.test.tsnew file mode 100644index 00000000..76c810fd--- /dev/null+++ b/laya-ts/tests/agent.test.ts@@ -0,0 +1,24 @@+import { describe, expect, it } from "vitest";+import { Agent } from "../src/agent.js";+const fakeProvider = () => ({+ async runEncoder(_b: any) { return { lastHidden: [[1, 0], [0, 1]] }; },+ async runHead(_h: any) { return { logits: [[2, 0]], act: [[3, 0]] }; },+});+describe("agent", () => {+ it("empty questions skip forward pass", async () => {+ const a = new Agent({ provider: fakeProvider() } as any);+ expect(await a.systemOne("hi", {})).toEqual(+ expect.objectContaining({ answers: {} }));+ });+ it("choice picks argmax with temp + confidence", async () => {+ const a = new Agent({ provider: fakeProvider() } as any);+ const r: any = await a.systemOne("hi", {+ d: { type: "choice", instructions: "q?", criteria: { x: "yes", y: "no" } } });+ expect(r.answers.d.choice).toBe("x");+ expect(r.usage.input_tokens).toBeGreaterThan(0);+ });+ it("rejects bad question with qid", async () => {+ const a = new Agent({ provider: fakeProvider() } as any);+ await expect(a.systemOne("hi", { q: { type: "nope" } as any })).rejects.toThrow('question "q"');+ });+});diff --git a/laya-ts/tests/bpe.test.ts b/laya-ts/tests/bpe.test.tsnew file mode 100644index 00000000..93b0972f--- /dev/null+++ b/laya-ts/tests/bpe.test.ts@@ -0,0 +1,79 @@+// laya-ts/tests/bpe.test.ts — byte-level BPE parity (checkpoint tokenizer is GPT-2-style BPE).+// Fixture source: hand-verified against the checkpoint tokenizer.json+// (transformers NOT installed, so no AutoTokenizer fixture; merges[0..3] ranks used below:+// rank 0, 1, 2, 3 for the pairs (G,G), (G,t), (G,a), (h,e) where G=U+0120;+// plus ("a","b")=112, ("t","h")=149 from the same merges list).+import { describe, expect, it } from "vitest";+import { bpeEncode, parseTokenizerJson, CHECKPOINT_IDS } from "../src/tokenizer.js";++const G = String.fromCharCode(0x120); // GPT-2 byte-level space (U+0120)++describe("bpeEncode", () => {+ it("does not lowercase: 'Hello' keeps cap H encoding", () => {+ const vocab = new Map([["H", 1], ["h", 2], ["e", 3], ["l", 4], ["o", 5]]);+ expect(bpeEncode(vocab, new Map(), "Hello")).toEqual([1, 3, 4, 4, 5]);+ expect(bpeEncode(vocab, new Map(), "hello")).toEqual([2, 3, 4, 4, 5]);+ });+ it("NFC-normalizes (composed == decomposed)", () => {+ const vocab = new Map([["c", 1], ["a", 2], ["f", 3], ["Ã", 4], ["©", 5]]);+ expect(bpeEncode(vocab, new Map(), "café")).toEqual(bpeEncode(vocab, new Map(), "café"));+ });+ it("applies merges in rank order (real merges[1] before merges[3])", () => {+ const vocab = new Map([[G, 10], ["t", 20], ["h", 30], ["e", 40], [G + "t", 50], ["he", 60]]);+ const merges = new Map([[G + " t", 1], ["h e", 3]]);+ // " the" -> [G,t,h,e] -> merge (G,t) r1 -> [Gt,h,e] -> merge (h,e) r3 -> [Gt,he]+ expect(bpeEncode(vocab, merges, " the")).toEqual([50, 60]);+ });+ it("lower rank wins between competing pairs", () => {+ const vocab = new Map([["a", 1], ["b", 2], ["c", 3], ["bc", 4], ["ab", 5]]);+ const merges = new Map([["a b", 112], ["b c", 5]]);+ // [a,b,c]: (b,c) r5 beats (a,b) r112 -> [a,bc]+ expect(bpeEncode(vocab, merges, "abc")).toEqual([1, 4]);+ });+ it("'a'+'b' merges per merges list (rank 112)", () => {+ const vocab = new Map([["a", 1], ["b", 2], ["ab", 3]]);+ expect(bpeEncode(vocab, new Map([["a b", 112]]), "ab")).toEqual([3]);+ });+ it("unknown byte -> UNK 50280", () => {+ const vocab = new Map([["[UNK]", 50280], ["a", 1], ["b", 2]]);+ expect(bpeEncode(vocab, new Map(), "axb")).toEqual([1, 50280, 2]);+ });+ it("deterministic on 'hello world', matches hand fixture", () => {+ const vocab = new Map([+ ["h", 1], ["e", 2], ["l", 3], ["o", 4], ["w", 5],+ ["r", 6], ["d", 7], [G, 8], ["he", 9], ["ll", 10],+ ]);+ const merges = new Map([["h e", 0], ["l l", 1]]);+ // "hello" -> [h,e,l,l,o] -> [he,l,l,o] -> [he,ll,o] = [9,10,4]+ // " world" -> [G,w,o,r,l,d], no merges apply = [8,5,4,6,3,7]+ const expected = [9, 10, 4, 8, 5, 4, 6, 3, 7];+ expect(bpeEncode(vocab, merges, "hello world")).toEqual(expected);+ expect(bpeEncode(vocab, merges, "hello world")).toEqual(expected);+ });+});++describe("parseTokenizerJson", () => {+ const hf = {+ model: { vocab: { hello: 1, "[UNK]": 50280 }, merges: [["h", "e"], ["a", "b"]] },+ added_tokens: [+ { id: 50280, content: "[UNK]" },+ { id: 50281, content: "[CLS]" },+ { id: 50282, content: "[SEP]" },+ { id: 50283, content: "[PAD]" },+ { id: 50284, content: "[MASK]" },+ ],+ };+ it("maps specials from added_tokens", () => {+ const data = parseTokenizerJson(hf)!;+ expect(data.ids).toEqual({ cls: 50281, sep: 50282, mask: 50284, pad: 50283, unk: 50280 });+ expect(data.merges.get("h e")).toBe(0);+ expect(data.merges.get("a b")).toBe(1);+ });+ it("checkpoint ids match ground truth", () => {+ expect(CHECKPOINT_IDS).toEqual({ cls: 50281, sep: 50282, mask: 50284, pad: 50283, unk: 50280 });+ });+ it("returns null on garbage", () => {+ expect(parseTokenizerJson(null)).toBeNull();+ expect(parseTokenizerJson({})).toBeNull();+ });+});diff --git a/laya-ts/tests/email-presets.test.ts b/laya-ts/tests/email-presets.test.tsnew file mode 100644index 00000000..793df497--- /dev/null+++ b/laya-ts/tests/email-presets.test.ts@@ -0,0 +1,24 @@+import { describe, expect, it } from "vitest";+import { cleanEmailBody } from "../src/email.js";+import { triageQuestions, guardQuestions } from "../src/presets.js";+describe("email+presets", () => {+ it("cuts quoted history", () => {+ const out = cleanEmailBody("Refund please\n\nOn Mon, Bob wrote:\nold text");+ expect(out).toContain("Refund please"); expect(out).not.toContain("old text");+ });+ it("triage preset has 5 questions", () => {+ expect(Object.keys(triageQuestions()).sort()).toEqual(+ ["churn_risk", "frustration", "intent", "is_urgent", "refund_requested"]);+ });+ it("cuts device footer", () => {+ const out = cleanEmailBody("Please refund my order\n\nSent from my iPhone");+ expect(out).toContain("Please refund my order");+ expect(out).not.toContain("iPhone");+ });+ it("guard preset has jailbreak and harm_severity", () => {+ const g = guardQuestions() as Record<string, any>;+ expect(g.jailbreak.type).toBe("noul");+ expect(g.harm_severity.type).toBe("score");+ expect(g.harm_severity.criteria.length).toBe(4);+ });+});diff --git a/laya-ts/tests/lang.test.ts b/laya-ts/tests/lang.test.tsnew file mode 100644index 00000000..c15f0a7f--- /dev/null+++ b/laya-ts/tests/lang.test.ts@@ -0,0 +1,17 @@+// laya-ts/tests/lang.test.ts+import { describe, expect, it } from "vitest";+import { analyse, detectScript, isEnglish } from "../src/lang.js";+describe("lang", () => {+ it("detects devanagari as non-latin", () => {+ expect(detectScript("मुझसे दो बार शुल्क लिया गया")).toBe("devanagari");+ });+ it("routes english latin to english", () => {+ expect(analyse("Please refund the duplicate charge").isEnglish).toBe(true);+ });+ it("routes german latin to non-english", () => {+ expect(isEnglish("Der Kunde wurde zweimal belastet")).toBe(false);+ });+ it("unknown (no letters) is english + undecided", () => {+ expect(analyse("123 !!!").script).toBe("unknown");+ });+});diff --git a/laya-ts/tests/parity-sequence.test.ts b/laya-ts/tests/parity-sequence.test.tsnew file mode 100644index 00000000..6ef24927--- /dev/null+++ b/laya-ts/tests/parity-sequence.test.ts@@ -0,0 +1,80 @@+// laya-ts/tests/parity-sequence.test.ts+import { describe, expect, it } from "vitest";+import { renderOptions, serializeState, buildSequence, collateItems } from "../src/common.js";+describe("common parity", () => {+ it("renders choice with JSON criteria", () => {+ expect(renderOptions({ t: "choice", ins: "x", crit: { a: "yes", b: null } }))+ .toEqual(["a: yes", "b"]);+ });+ it("renders noul defaults", () => {+ expect(renderOptions({ t: "noul", ins: "x", crit: null })[0].startsWith("false: ")).toBe(true);+ });+ it("builds [CLS] head [SEP] markers [SEP] state [SEP]", () => {+ const tok = { clsId: 101, sepId: 102, maskId: 103, maskToken: "[MASK]", encode(s: string): number[] {+ return s.split(/\s+/).filter(Boolean).map((_, i) => 1000 + i); } };+ const { ids, markers } = buildSequence(tok as any,+ "hi", { t: "choice", ins: "pick", crit: { a: "x", b: "y" } }, 64, 32);+ expect(ids[0]).toBe(101);+ expect(markers.length).toBe(2);+ expect(ids[ids.length - 1]).toBe(102);+ });+ it("serializes dict state as JSON", () => {+ expect(serializeState({ body: "x" })).toBe('{"body": "x"}');+ });+ it("truncateLeft keeps tail of state (py parity)", () => {+ const tok = {+ clsId: 101, sepId: 102, maskId: 103, maskToken: "[MASK]",+ encode(s: string): number[] { return s.split(/\s+/).filter(Boolean).map((_, i) => 1000 + i); },+ } as any;+ const q = { t: "choice", ins: "pick", crit: { a: "x", b: "y" } } as any;+ const state = Array.from({ length: 20 }, (_, i) => `w${i}`).join(" ");+ const a = buildSequence(tok, state, q, 16, 8);+ const b = buildSequence(tok, state, q, 16, 8, undefined, true);+ expect(a.ids.length).toBeLessThanOrEqual(16);+ expect(b.ids.length).toBeLessThanOrEqual(16);+ expect(a.ids).not.toEqual(b.ids);+ // room=3: head keeps [1000,1001,1002], tail keeps [1017,1018,1019]+ expect(a.ids.slice(12, 15)).toEqual([1000, 1001, 1002]);+ expect(b.ids.slice(12, 15)).toEqual([1017, 1018, 1019]);+ });+ it("optionOrder reorders option blocks (py parity)", () => {+ const tok = {+ clsId: 101, sepId: 102, maskId: 103, maskToken: "[MASK]",+ encode(s: string): number[] {+ return s.split(/\s+/).filter(Boolean).map((w) => 2000 + (w.charCodeAt(0) % 500));+ },+ } as any;+ const q = { t: "choice", ins: "pick", crit: { a: "x", b: "y" } } as any;+ const dflt = buildSequence(tok, "hi", q, 64, 32);+ const rev = buildSequence(tok, "hi", q, 64, 32, [1, 0]);+ expect(rev.markers.length).toBe(2);+ expect(rev.ids).not.toEqual(dflt.ids);+ // swapping twice restores identity+ const back = buildSequence(tok, "hi", q, 64, 32, [0, 1]);+ expect(back.ids).toEqual(dflt.ids);+ });+ it("collateItems pads ids/att/mpos/mmask/qtype/label/meta (py parity)", () => {+ const batch = [[+ { ids: [1, 2, 3], markers: [1, 2], qtype: 0 },+ { ids: [4, 5], markers: [1], qtype: 1, label: 2 },+ ]];+ const out = collateItems(batch as any, 0)!;+ expect(out.inputIds).toEqual([[1, 2, 3], [4, 5, 0]]);+ expect(out.attentionMask).toEqual([[1, 1, 1], [1, 1, 0]]);+ expect(out.markerPos).toEqual([[1, 2], [1, 0]]);+ expect(out.markerMask).toEqual([[true, true], [true, false]]);+ expect(out.qtype).toEqual([0, 1]);+ expect(out.label).toEqual([-1, 2]);+ expect(out.meta.length).toBe(2);+ expect(out.meta[0]).toMatchObject({ qtype: 0 });+ expect("ids" in out.meta[0]).toBe(false);+ expect(out.target).toBeUndefined();+ });+ it("collateItems includes target when present and null on empty", () => {+ const batch = [[{ ids: [1], markers: [0, 1], qtype: 0, target: [0.2, 0.8] }]];+ const out = collateItems(batch as any, 0)!;+ expect(out.target).toEqual([[0.2, 0.8]]);+ expect(collateItems([], 0)).toBeNull();+ expect(collateItems([[]], 0)).toBeNull();+ });+});diff --git a/laya-ts/tests/router.test.ts b/laya-ts/tests/router.test.tsnew file mode 100644index 00000000..929c08ed--- /dev/null+++ b/laya-ts/tests/router.test.ts@@ -0,0 +1,18 @@+import { describe, expect, it } from "vitest";+import { Router, normaliseName } from "../src/router.js";+describe("router", () => {+ it("normalises aliases", () => { expect(normaliseName("en")).toBe("english"); });+ it("rejects unknown model", () => { expect(() => normaliseName("jev-1")).toThrow(); });+ it("routes hindi script to multilingual without loading", () => {+ const r = new Router();+ expect(r.route({ body: "मुझसे दो बार शुल्क लिया गया" }, {}).model).toBe("multilingual");+ expect(r.loaded).toEqual([]);+ });+ it("explicit model wins", () => {+ expect(new Router().route("hi", {}, { model: "typed-decisions" }).model).toBe("typed-decisions");+ });+ it("lang_guess callable routes", () => {+ const r = new Router();+ expect(r.route("text", {}, { langGuess: () => "ro" }).model).toBe("multilingual");+ });+});diff --git a/laya-ts/tests/scaffold.test.ts b/laya-ts/tests/scaffold.test.tsnew file mode 100644index 00000000..97c96344--- /dev/null+++ b/laya-ts/tests/scaffold.test.ts@@ -0,0 +1,5 @@+import { describe, expect, it } from "vitest";+import { VERSION } from "../src/index.js";+describe("scaffold", () => {+ it("exposes version", () => { expect(VERSION).toBe("0.1.0"); });+});diff --git a/laya-ts/tests/shortlist.test.ts b/laya-ts/tests/shortlist.test.tsnew file mode 100644index 00000000..945fb460--- /dev/null+++ b/laya-ts/tests/shortlist.test.ts@@ -0,0 +1,17 @@+import { describe, expect, it } from "vitest";+import { shortlistChoice } from "../src/shortlist.js";+const emb = async (texts: string[]) =>+ texts.map((t) => (t.includes("refund") || t.includes("billed") ? [1, 0] : [0, 1]));+describe("shortlist", () => {+ it("keeps top-k by cosine", async () => {+ const labels = await shortlistChoice({ text: "billed twice refund" },+ { card_arrival: "where is my card", transfer_fee: "fee on transfer", refund: "refund money" },+ emb as any, 1);+ expect(labels).toEqual(["refund"]);+ });+ it("passthrough when k >= n skips embed", async () => {+ let called = false;+ await shortlistChoice("x", { a: "1" }, (async (t: string[]) => { called = true; return t.map(() => [1]); }) as any, 5);+ expect(called).toBe(false);+ });+});diff --git a/laya-ts/tests/tokenizer-parity.test.ts b/laya-ts/tests/tokenizer-parity.test.tsnew file mode 100644index 00000000..8eeea468--- /dev/null+++ b/laya-ts/tests/tokenizer-parity.test.ts@@ -0,0 +1,79 @@+// laya-ts/tests/tokenizer-parity.test.ts — Gemma-style (Metaspace BPE) parity.+// Reference vectors from transformers AutoTokenizer on model-ml/tokenizer.json,+// add_special_tokens=False. Hardcoded so the suite passes on any machine; when the+// real model-ml/tokenizer.json is present it is loaded and checked byte-identical.+import { describe, expect, it } from "vitest";+import { existsSync, readFileSync } from "node:fs";+import { dirname, resolve } from "node:path";+import { fileURLToPath } from "node:url";+import { encodeWithData, metaspaceEncode, parseTokenizerJson } from "../src/tokenizer.js";++const EN = "choice question: Which department should handle this request?";+const EN_IDS = [6241, 2872, 235292, 12236, 9888, 1412, 6589, 736, 3853, 235336];+const HI = "मुझसे दो बार शुल्क लिया गया";+const HI_IDS = [39728, 238385, 20579, 51484, 99744, 144905, 228834, 158850, 44370];++const here = dirname(fileURLToPath(import.meta.url));+const mlPath = resolve(here, "../../model-ml/tokenizer.json");++describe("metaspaceEncode (synthetic)", () => {+ const vocab = new Map([+ ["▁hi", 10], ["▁", 11], ["h", 12], ["i", 13], ["<unk>", 3],+ ]);+ const joins = new Map([["▁ h", 0], ["▁h i", 1]]);+ it("prepends one marker, splits keeping marks, no lowercase", () => {+ expect(metaspaceEncode(vocab, joins, "hi")).toEqual([10]);+ expect(metaspaceEncode(vocab, joins, " hi")).toEqual([10]);+ expect(metaspaceEncode(vocab, joins, " hi")).toEqual([11, 10]);+ expect(metaspaceEncode(vocab, new Map(), "")).toEqual([]);+ expect(metaspaceEncode(vocab, joins, "HI")).toEqual([11, 3, 3]);+ });+ it("unknown piece -> unk id", () => {+ expect(metaspaceEncode(vocab, joins, "z")).toEqual([11, 3]);+ });+});++describe("parseTokenizerJson specials aliases", () => {+ it("resolves Gemma <bos>/<eos>/<pad>/<mask>/<unk> (cls=2 sep=1 pad=0 mask=4 unk=3)", () => {+ const data = parseTokenizerJson({+ model: { vocab: { "▁a": 10 }, merges: [] },+ normalizer: { type: "Replace", pattern: { String: " " }, content: "▁" },+ pre_tokenizer: { type: "Metaspace", replacement: "▁", prepend_scheme: "always", split: true },+ added_tokens: [+ { id: 0, content: "<pad>" }, { id: 1, content: "<eos>" }, { id: 2, content: "<bos>" },+ { id: 3, content: "<unk>" }, { id: 4, content: "<mask>" },+ ],+ })!;+ expect(data.ids).toEqual({ cls: 2, sep: 1, mask: 4, pad: 0, unk: 3 });+ expect(data.kind).toBe("metaspace");+ expect(data.maskToken).toBe("<mask>");+ expect(data.replaces).toEqual([[" ", "▁"]]);+ });+ it("keeps ModernBERT [CLS]/[SEP]/[PAD]/[MASK]/[UNK] on the ByteLevel path", () => {+ const data = parseTokenizerJson({+ model: { vocab: { hello: 1 }, merges: ["h e"] },+ pre_tokenizer: { type: "ByteLevel" },+ added_tokens: [+ { id: 50280, content: "[UNK]" }, { id: 50281, content: "[CLS]" },+ { id: 50282, content: "[SEP]" }, { id: 50283, content: "[PAD]" },+ { id: 50284, content: "[MASK]" },+ ],+ })!;+ expect(data.ids).toEqual({ cls: 50281, sep: 50282, mask: 50284, pad: 50283, unk: 50280 });+ expect(data.kind).toBe("bytelevel");+ expect(data.maskToken).toBe("[MASK]");+ });+});++describe("model-ml/tokenizer.json parity (gated on file presence)", () => {+ it("reproduces reference id-sequences byte-identical", () => {+ if (!existsSync(mlPath)) return;+ const raw = JSON.parse(readFileSync(mlPath, "utf8"));+ const data = parseTokenizerJson(raw)!;+ expect(data.kind).toBe("metaspace");+ expect(data.ids).toMatchObject({ cls: 2, sep: 1, mask: 4, pad: 0, unk: 3 });+ expect(data.maskToken).toBe("<mask>");+ expect(encodeWithData(data, EN)).toEqual(EN_IDS);+ expect(encodeWithData(data, HI)).toEqual(HI_IDS);+ });+});diff --git a/laya-ts/tsconfig.json b/laya-ts/tsconfig.jsonnew file mode 100644index 00000000..f773f4ee--- /dev/null+++ b/laya-ts/tsconfig.json@@ -0,0 +1,12 @@+{+ "compilerOptions": {+ "target": "ES2022",+ "module": "NodeNext",+ "moduleResolution": "NodeNext",+ "strict": true,+ "declaration": true,+ "outDir": "dist",+ "rootDir": "src"+ },+ "include": ["src"]+}