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): run process-wide default hooks once per Router.predict (#435 parity)
divit aggarwald20613edaa79
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.tsindex f5bc8e69..40249342 100644--- a/laya-ts/src/agent.ts+++ b/laya-ts/src/agent.ts@@ -20,6 +20,7 @@ import {PredictContext,aggregateUsage,composeHooks,+ defaultsAlreadyRan,dispatch,normaliseHooks,type HookArg,@@ -360,7 +361,9 @@ export class Agent extends HookRegistry {questions: Record<string, QuestionDef>,opts: PredictOptions,): Promise<SystemOneResult[]> {- const active = composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);+ const active = defaultsAlreadyRan(opts)+ ? [...this.hooks, ...normaliseHooks(opts.hooks, opts.onPredictStart, opts.onPredictEnd)]+ : composeHooks(this.hooks, opts.hooks, opts.onPredictStart, opts.onPredictEnd);const raiseErrors = opts.hooksRaise ?? this.hooksRaise;const ctx = new PredictContext({states,diff --git a/laya-ts/src/hooks.ts b/laya-ts/src/hooks.tsindex 8d5428ac..a8a1deea 100644--- a/laya-ts/src/hooks.ts+++ b/laya-ts/src/hooks.ts@@ -220,6 +220,18 @@ export function clearDefaultHooks(): void {defaultHooksList.length = 0;}+const defaultsRan = new WeakSet<object>();++/** `Router.predict` marks the options of its Agent call: the Router already ran the defaults for this request. */+export function markDefaultsRan(opts: object): void {+ defaultsRan.add(opts);+}++/** Whether `opts` was marked by `markDefaultsRan`, so the Agent must not run the defaults a second time. */+export function defaultsAlreadyRan(opts: object): boolean {+ return defaultsRan.has(opts);+}+/*** Effective hook list for one call: defaults, then installed, then per-call hooks.*diff --git a/laya-ts/src/router.ts b/laya-ts/src/router.tsindex 0f09b078..c8fee255 100644--- a/laya-ts/src/router.ts+++ b/laya-ts/src/router.ts@@ -7,6 +7,7 @@ import {aggregateUsage,composeHooks,dispatch,+ markDefaultsRan,normaliseHooks,type HookArg,type PredictHook,@@ -504,10 +505,12 @@ export class Router extends HookRegistry {// explicit lang="en" can select an "en" override.const detected = decision.detection?.language;const effectiveLang = opts.lang ?? (detected && detected !== "en" ? detected : null);+ const agentOpts = { lang: effectiveLang };+ markDefaultsRan(agentOpts);const result = (await agent.systemOne(ctx.states[0],ctx.questions as Record<string, QuestionDef>,- { lang: effectiveLang },+ agentOpts,)) as RoutedResult;result["routing"] = { ...decision };ctx.results = [result as unknown as Record<string, unknown>];diff --git a/laya-ts/tests/hooks.test.ts b/laya-ts/tests/hooks.test.tsindex 6021a58c..86b3591b 100644--- a/laya-ts/tests/hooks.test.ts+++ b/laya-ts/tests/hooks.test.ts@@ -377,6 +377,40 @@ describe("BaseHook and process-wide default hooks (py #276 parity)", () => {}});+ it("defaults/fire once per Router.predict, not again inside the Agent", async () => {+ const events: string[] = [];+ const tag = (ctx: PredictContext) => (ctx.router ? "router" : "agent");+ setDefaultHooks({+ onPredictStart: (ctx: PredictContext) => void events.push(`start:${tag(ctx)}`),+ onPredictEnd: (ctx: PredictContext) => void events.push(`end:${tag(ctx)}`),+ });+ try {+ const router = new Router();+ router.attach("english", makeAgent() as never);+ await router.predict("s0", QUESTIONS, { model: "english" });+ expect(events).toEqual(["start:router", "end:router"]);+ } finally {+ clearDefaultHooks();+ }+ });++ it("defaults/still fire for a direct Agent call made while a Router request is in flight", async () => {+ const events: string[] = [];+ setDefaultHooks({ onPredictEnd: (ctx: PredictContext) => void events.push(ctx.router ? "router" : "agent") });+ try {+ const router = new Router();+ router.attach("english", makeAgent() as never);+ await Promise.all([+ router.predict("s0", QUESTIONS, { model: "english" }),+ makeAgent().predict("s1", QUESTIONS),+ ]);+ expect(events.filter((e) => e === "router")).toHaveLength(1);+ expect(events.filter((e) => e === "agent")).toHaveLength(1);+ } finally {+ clearDefaultHooks();+ }+ });+it("defaults/cover the router lifecycle (onLoad/onEvict)", async () => {const events: [string, string | undefined][] = [];class LifeDefaults extends BaseHook {