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): forward per-call hook options through predictBatch
Cursor Agent21aac487339e
diff --git a/laya-ts/README.md b/laya-ts/README.mdindex b87b626a..713f7e56 100644--- a/laya-ts/README.md+++ b/laya-ts/README.md@@ -146,9 +146,14 @@ const results = await agent.predictBatch(states, questions, { batchSize: 32 });// in as few forward passes as possible. Results keep input order and carry `routing`.const decisions = router.routeBatch(requests); // validate + route, nothing loadsconst routed = await router.predictBatch(requests, 32); // == router.predictMany(...)-// Per-call hooks are composed like predict: installed hooks first, then this list.-// null and [] add nothing. The list runs once per request, including onRoute.-const traced = await router.predictBatch(requests, null, { hooks: [tracer] });+// Per-call hooks are composed like predict: installed hooks first, then this list,+// then onPredictStart / onPredictEnd. null and [] add nothing. hooksRaise overrides+// the Router's policy for this call. The list runs once per request, including onRoute.+const traced = await router.predictBatch(requests, null, {+ hooks: [tracer],+ onPredictEnd(ctx) { console.log(ctx.usage); },+ hooksRaise: false,+});// requests routed to the same checkpoint still split into separate batches when their// question schemas differ (order-sensitively), when per-request start hooks setdiff --git a/laya-ts/src/router.ts b/laya-ts/src/router.tsindex 567226fd..d3654153 100644--- a/laya-ts/src/router.ts+++ b/laya-ts/src/router.ts@@ -156,11 +156,16 @@ export interface RouteOptions {/*** Per-call options for `Router.predictBatch` / `predictMany` / `routeBatch`.- * `hooks` is composed the same way as on `predict`: installed hooks first, then this list.- * `null`, `undefined` and `[]` add nothing.+ * `hooks`, `onPredictStart` and `onPredictEnd` are composed the same way as on `predict`:+ * installed hooks first, then this call. `null`, `undefined` and `[]` add nothing.+ * `hooksRaise` overrides the Router's policy for this call, including `onRoute`.+ * The start and end callables apply to each request's predict lifecycle, not to routing.*/export interface PredictBatchOptions {hooks?: HookArg;+ onPredictStart?: PredictHook;+ onPredictEnd?: PredictHook;+ hooksRaise?: boolean;}/** One item of a Router.predictBatch/routeBatch batch: state, questions, route overrides. */@@ -721,6 +726,7 @@ export class Router extends HookRegistry {lang: request.lang ?? null,langGuess: request.langGuess ?? request.lang_guess ?? null,hooks: opts.hooks,+ hooksRaise: opts.hooksRaise,}),);}@@ -741,9 +747,10 @@ export class Router extends HookRegistry {* Router-level predict hooks run per request: each request gets its own PredictContext* carrying `decision`; `onPredictStart` may rewrite a request or `ctx.skip()` it, and* `onPredictEnd` runs once per started request even when the batch fails.- * `opts.hooks` are per-call hooks, appended after any installed on the Router, the same- * composition `predict` uses. `null` and `[]` add nothing. They apply to `onRoute` as well- * as to each request's predict lifecycle.+ * `opts.hooks`, `opts.onPredictStart` and `opts.onPredictEnd` are composed the way `predict`+ * composes them: installed hooks first, then this call. `null` and `[]` add nothing. `hooks`+ * also run `onRoute`. `opts.hooksRaise` overrides the Router's policy for this call, the way+ * `predict` uses `opts.hooksRaise ?? this.hooksRaise`.*/async predictBatch(requests: BatchRequest[],@@ -762,8 +769,10 @@ export class Router extends HookRegistry {}const results: (RoutedResult | null)[] = new Array(requests.length).fill(null);- const active = composeHooks(this.hooks, opts.hooks);- const raiseErrors = this.hooksRaise;+ // Same composition and raise policy as `predict`. `routeBatch` has already validated+ // `opts.hooks`; start/end are validated here, after routing and before any checkpoint loads.+ const active = composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);+ const raiseErrors = opts.hooksRaise ?? this.hooksRaise;for (const [modelName, indices] of groups) {const agent = (await this.load(modelName)) as {diff --git a/laya-ts/tests/router-batch.test.ts b/laya-ts/tests/router-batch.test.tsindex 22514fe3..8407d0eb 100644--- a/laya-ts/tests/router-batch.test.ts+++ b/laya-ts/tests/router-batch.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import { Agent, Router } from "../src/index.js";import type { BatchRequest } from "../src/index.js";@@ -248,6 +248,85 @@ describe("Router.routeBatch / predictBatch", () => {]);});+ it("runs per-call onPredictStart and onPredictEnd once per request", async () => {+ const events: string[] = [];+ const { router, calls } = makeRouter();+ const items = [req("one", { model: "english" }), req("two", { model: "english" })];+ const results = await router.predictBatch(items, null, {+ onPredictStart(ctx) {+ events.push(`start:${ctx.states[0]}`);+ },+ onPredictEnd(ctx) {+ events.push(`end:${ctx.states[0]}`);+ },+ });+ expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);+ expect(calls.length).toBe(1);+ // Starts run for every request in the checkpoint group, then ends, in start order.+ expect(events).toEqual(["start:one", "start:two", "end:one", "end:two"]);+ });++ it("hooksRaise false swallows a throwing per-call hook; the default still raises", async () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ try {+ const items = [req("one", { model: "english" }), req("two", { model: "english" })];+ const { router } = makeRouter();+ await expect(+ router.predictBatch(items, null, {+ onPredictStart() {+ throw new Error("hook failed");+ },+ }),+ ).rejects.toThrow("hook failed");++ const { router: quietByDefault } = makeRouter({ hooksRaise: false });+ const quiet = await quietByDefault.predictBatch(items, null, {+ onPredictStart() {+ throw new Error("installed policy");+ },+ });+ expect(quiet.map((r) => r.answers.seen)).toEqual(["one", "two"]);++ const seen: string[] = [];+ const { router: perCall } = makeRouter();+ const results = await perCall.predictBatch(items, null, {+ hooksRaise: false,+ onPredictStart(ctx) {+ seen.push(String(ctx.states[0]));+ throw new Error("per-call policy");+ },+ });+ expect(seen).toEqual(["one", "two"]);+ expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);+ expect(warn.mock.calls.some((args) => String(args[0]).includes("per-call policy"))).toBe(true);+ } finally {+ warn.mockRestore();+ }+ });++ it("predictMany forwards onPredictStart, onPredictEnd and hooksRaise", async () => {+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ try {+ const events: string[] = [];+ const { router } = makeRouter();+ const items = [req("one", { model: "english" }), req("two", { model: "english" })];+ const results = await router.predictMany(items, null, {+ hooksRaise: false,+ onPredictStart(ctx) {+ events.push(`start:${ctx.states[0]}`);+ if (ctx.states[0] === "one") throw new Error("start failed");+ },+ onPredictEnd(ctx) {+ events.push(`end:${ctx.states[0]}`);+ },+ });+ expect(results.map((r) => r.answers.seen)).toEqual(["one", "two"]);+ expect(events).toEqual(["start:one", "start:two", "end:one", "end:two"]);+ } finally {+ warn.mockRestore();+ }+ });+it("runs router hooks per request and short-circuits skipped requests", async () => {const events: string[] = [];const { router, calls } = makeRouter({