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
fix(ts): a score legend echoed the caller's own types, unlike the Python backends
PerryLink32a3d41aea9b
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.tsindex f5bc8e69..97f49d7c 100644--- a/laya-ts/src/agent.ts+++ b/laya-ts/src/agent.ts@@ -6,6 +6,7 @@ import {collateItems,confidenceFromProbs,answerConfidence,+ renderCriterion,renderOptions,sequenceWithState,serializeState,@@ -487,7 +488,15 @@ export class Agent extends HookRegistry {answers[qid] = {type: "score",score: r4(exp),- legend: Object.fromEntries((q.crit as unknown[]).map((c, i) => [String(i), c])),+ // `renderCriterion`, not the raw `c`: a legend maps an index to the TEXT of a level, and+ // the keys are already strings. A numeric scale written directly used to come back with+ // the caller's own JSON types (`{"0": 1}`), so a client that reads a level as a string+ // had to handle a number, a boolean and null as well -- and `{"0": null}` is not even+ // parseable by a Jev client (#302). Same change as the Python `agent.py` and+ // `onnx_agent.py` legends, so the two runtimes return the same value types.+ legend: Object.fromEntries(+ (q.crit as unknown[]).map((c, i) => [String(i), renderCriterion(c)]),+ ),probabilities: Object.fromEntries(p.map((v, i) => [String(i), r4(v)])),confidence: r4(confidenceFromProbs(p)),answer_confidence: ansConf,diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.tsindex 6e4ec30e..ae5b9da8 100644--- a/laya-ts/src/common.ts+++ b/laya-ts/src/common.ts@@ -38,7 +38,14 @@ export function serializeState(state: unknown): string {if (typeof state === "string") return state;return pyJson(state) ?? String(state);}-function renderCriterion(v: unknown): string {+/** The text of a criterion: the string itself, else the JSON the model was shown.+ *+ * Exported because `agent.ts` needs it for a `score` answer's `legend`, which maps an index to the+ * text of that level. Putting the raw value in made the response's JSON types depend on the+ * caller's input -- a numeric scale came back as `{"0": 1}`, a boolean as `{"0": true}` and a null+ * as `{"0": null}`, which a Jev client refuses to parse (#302).+ */+export function renderCriterion(v: unknown): string {return typeof v === "string" ? v : pyJson(v) ?? String(v);}export function renderOptions(q: InternalQ): string[] {diff --git a/laya-ts/tests/legend-parity.test.ts b/laya-ts/tests/legend-parity.test.tsnew file mode 100644index 00000000..45bacd82--- /dev/null+++ b/laya-ts/tests/legend-parity.test.ts@@ -0,0 +1,93 @@+import { describe, expect, it } from "vitest";+import { Agent } from "../src/agent.js";+import { renderCriterion, renderOptions } from "../src/common.js";++/**+ * A `score` answer's `legend` maps a level index to the TEXT of that level, and its keys are+ * already strings. Its values used to be the caller's own JSON types, so a numeric scale came back+ * as `{"0": 1}`, a boolean as `{"0": true}` and a null as `{"0": null}` -- which a Jev client+ * refuses to parse (#302). The Python side was fixed the same way in `laya/agent.py` and+ * `laya/onnx_agent.py`, so all three runtimes have to agree on the value types.+ *+ * The Python numbers these are pinned against, produced by `render_criterion` in `laya/common.py`:+ *+ * render_criterion(1) -> '1'+ * render_criterion(1.5) -> '1.5'+ * render_criterion(True) -> 'true'+ * render_criterion(None) -> 'null'+ * render_criterion({'a': 1}) -> '{"a": 1}'+ * render_criterion([1, 2]) -> '[1, 2]'+ */++const fakeProvider = (n: number) => ({+ async runEncoder(_b: any) { return { lastHidden: [[1, 0], [0, 1]] }; },+ async runHead(_h: any) {+ const row = Array.from({ length: n }, (_, i) => (i === 0 ? 2 : 0));+ return { logits: [row], act: [[3, 0]] };+ },+});++/** The legend for one score question, through the public API. */+async function legendFor(criteria: unknown[]): Promise<Record<string, unknown>> {+ const agent = new Agent({ provider: fakeProvider(criteria.length) } as any);+ const res: any = await agent.systemOne("s", {+ q: { type: "score", instructions: "q?", criteria },+ });+ return res.answers.q.legend;+}++describe("score legend value types", () => {+ it("renders numeric levels as strings, not numbers", async () => {+ const legend = await legendFor([1, 2, 3]);+ expect(legend).toEqual({ "0": "1", "1": "2", "2": "3" });+ for (const v of Object.values(legend)) expect(typeof v).toBe("string");+ });++ it("renders float levels as strings", async () => {+ expect(await legendFor([1.5, 2.5])).toEqual({ "0": "1.5", "1": "2.5" });+ });++ it("renders boolean levels with JSON spelling, matching Python", async () => {+ // Python's `render_criterion(True)` is 'true', not 'True' -- this is the cross-runtime pin+ expect(await legendFor([true, false])).toEqual({ "0": "true", "1": "false" });+ });++ it("rejects a null level rather than rendering it, matching the Python guard", async () => {+ // A null score level never reaches the legend: both runtimes refuse it up front, so+ // `{"0": null}` (#302) cannot be produced on either. This pins the TS half of that guard.+ await expect(legendFor([null])).rejects.toThrow(/score level 0 is null/);+ await expect(legendFor(["low", null])).rejects.toThrow(/score level 1 is null/);+ });++ it("renders a structured level as the JSON the model was shown, not a repr", async () => {+ expect(await legendFor([{ a: 1 }])).toEqual({ "0": '{"a": 1}' });+ expect(await legendFor([[1, 2]])).toEqual({ "0": "[1, 2]" });+ });++ it("keeps string levels unchanged", async () => {+ expect(await legendFor(["low", "high"])).toEqual({ "0": "low", "1": "high" });+ });++ it("agrees with renderCriterion, which is what the option text already used", async () => {+ // the point of the fix: the legend value and the option text are the same rendering.+ // `null` is absent because it is rejected before this point (see the test above).+ for (const level of [1, 1.5, true, false, "low"]) {+ const legend = await legendFor([level]);+ const optionText = renderOptions({ t: "score", ins: "q?", crit: [level] } as any)[0];+ expect(legend["0"]).toBe(renderCriterion(level));+ expect(optionText).toBe(`level 0: ${renderCriterion(level)}`);+ }+ });++ it("does not change the probabilities keys or the score itself", async () => {+ const agent = new Agent({ provider: fakeProvider(3) } as any);+ const res: any = await agent.systemOne("s", {+ q: { type: "score", instructions: "q?", criteria: [1, 2, 3] },+ });+ // the legend is stringified, the probability keys were already strings, and the score is a+ // number either way -- so only the legend changed+ expect(Object.keys(res.answers.q.probabilities)).toEqual(["0", "1", "2"]);+ expect(typeof res.answers.q.score).toBe("number");+ expect(typeof res.answers.q.confidence).toBe("number");+ });+});