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(laya-ts): forward per-call hooks through decideBatch on a Router
thread3d8e95aeee3a83
diff --git a/laya-ts/src/structured.ts b/laya-ts/src/structured.tsindex f1b2f2fe..c62c3cdc 100644--- a/laya-ts/src/structured.ts+++ b/laya-ts/src/structured.ts@@ -19,6 +19,7 @@* with a `toJSONSchema()` method is accepted.*/import type { QuestionDef } from "./agent.js";+import type { PredictBatchOptions } from "./router.js";import { checkMinConfidence, flagLowConfidence } from "./common.js";import type { MinConfidence } from "./common.js";@@ -459,9 +460,26 @@ export async function decideBatch(const isRouter = typeof r.routeBatch === "function";let results: any[];if (isRouter) {- const requests = states.map((s) => ({ state: s, questions: qs, ...predictOpts }));+ // `Router.predictBatch` reads the routing overrides off each REQUEST and takes the hook+ // controls as its third argument. Spreading everything into the request objects (what this+ // path used to do) put `hooks`/`onPredictStart`/`onPredictEnd`/`hooksRaise` where+ // `predictBatch` never looks, so every per-call hook -- the tracing/metrics/audit hooks this+ // option exists for -- silently observed nothing on the Router path while the Agent path+ // fired them. Python's `decide_batch` makes the same split: `_ROUTER_REQUEST_KEYS` onto each+ // request, everything else as call keywords.+ const requestKeys = ["model", "task", "lang", "langGuess", "lang_guess", "maxLen", "headMaxLen"];+ const perRequest: Record<string, unknown> = {};+ const callOpts: PredictBatchOptions = {};+ for (const [key, value] of Object.entries(predictOpts)) {+ if (requestKeys.includes(key)) {+ perRequest[key] = value;+ } else if (key === "hooks" || key === "onPredictStart" || key === "onPredictEnd" || key === "hooksRaise") {+ (callOpts as Record<string, unknown>)[key] = value;+ }+ }+ const requests = states.map((s) => ({ state: s, questions: qs, ...perRequest }));const batchSize = (predictOpts.batchSize ?? null) as number | null;- results = await predictBatchFn.call(runner, requests, batchSize);+ results = await predictBatchFn.call(runner, requests, batchSize, callOpts);} else {results = await predictBatchFn.call(runner, states, qs as Record<string, QuestionDef>, predictOpts);}diff --git a/laya-ts/tests/structured.test.ts b/laya-ts/tests/structured.test.tsindex ed415a6e..72652dac 100644--- a/laya-ts/tests/structured.test.ts+++ b/laya-ts/tests/structured.test.ts@@ -546,3 +546,59 @@ describe("structured/decideBatch", () => {});++// `DecideBatchOptions` documents that anything it does not read itself is forwarded to the+// runner, hooks included. On a Router-like runner the call used to spread those options into the+// request objects and pass no third argument, so `Router.predictBatch` -- which reads hooks from+// its options argument, not from each request -- never ran them: tracing and metrics hooks+// observed nothing on the batch path while firing on the single-request and Agent paths.+describe("structured/decideBatch Router hook forwarding", () => {+ const provider = () => ({+ async runEncoder(b: any) {+ const n = b?.inputIds?.length ?? 1;+ return { lastHidden: Array.from({ length: n }, () => [[1, 0], [0, 1]]) };+ },+ async runHead(_h: any, batch?: any) {+ const n = batch?.inputIds?.length ?? 1;+ return {+ logits: Array.from({ length: n }, () => [2, 0, 1, 0]),+ act: Array.from({ length: n }, () => [3, 0]),+ };+ },+ });++ const routerWithAgent = () =>+ new Router({ default: "english", loader: () => new Agent({ provider: provider() } as any) } as any);++ it("fires per-call hooks through decide() and decideBatch() on a Router", async () => {+ const fired: string[] = [];+ const hook = {+ onPredictStart(ctx: any) {+ fired.push(String(ctx.states[0]));+ },+ };+ await decide(routerWithAgent(), "single", SCHEMA, { hooks: [hook] });+ expect(fired).toEqual(["single"]);++ fired.length = 0;+ await decideBatch(routerWithAgent(), ["s1", "s2"], SCHEMA, { hooks: [hook] });+ expect(fired).toEqual(["s1", "s2"]);+ });++ it("forwards the plain-callable form too, and does not put hooks on the request objects", async () => {+ const fired: string[] = [];+ const router = routerWithAgent();+ const calls: any[] = [];+ // Record what reaches the router's own predictBatch, then run the real one.+ const realBatch = router.predictBatch.bind(router);+ (router as any).predictBatch = (requests: any[], batchSize: any, opts: any) => {+ calls.push({ requests, batchSize, opts });+ return realBatch(requests, batchSize, opts);+ };+ await decideBatch(router, ["s1"], SCHEMA, { onPredictStart: (ctx) => fired.push(String(ctx.states[0])) });+ expect(fired).toEqual(["s1"]);+ expect(calls).toHaveLength(1);+ expect("onPredictStart" in calls[0].requests[0]).toBe(false);+ expect(typeof calls[0].opts.onPredictStart).toBe("function");+ });+});