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): reject a null or empty instructions, as Python does
Bruce-Yii5826d90fb5e5
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.tsindex 647c5c0c..ac72f545 100644--- a/laya-ts/src/agent.ts+++ b/laya-ts/src/agent.ts@@ -157,6 +157,9 @@ function qidStr(qid: string): string {}export function checkQuestion(qid: string, qdef: unknown): void {+ if (typeof qid !== "string" || !qid.trim()) {+ throw new Error(`question id must be a non-empty string, got ${qidStr(qid)}`);+ }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}`);@@ -171,6 +174,36 @@ export function checkQuestion(qid: string, qdef: unknown): void {if (!("instructions" in q)) {throw new Error(`question ${qidStr(qid)}: no 'instructions'; add the text the model should answer`);}+ // `instructions` is the text the model is asked, so a null or empty one is not a weak question+ // but a question with nothing to answer. Only the key's presence was checked, and `toInternal`+ // then serialised whatever it found, so a null reached the model as the literal prompt "null",+ // an empty dict as "{}", and a blank string stayed blank -- the caller's error answered+ // silently. Python's `Agent._check_question` rejects all of these; these are the same three+ // rejections in the same order.+ const ins = q["instructions"];+ if (ins === null || ins === undefined) {+ throw new Error(+ `question ${qidStr(qid)}: 'instructions' must not be None; add the text the model should answer`,+ );+ }+ if (typeof ins === "string" && !ins.trim()) {+ throw new Error(+ `question ${qidStr(qid)}: 'instructions' must not be empty; add the text the model should answer`,+ );+ }+ // An object here is a list or a dict, and both are empty exactly when they own no keys.+ if (typeof ins === "object" && Object.keys(ins as object).length === 0) {+ throw new Error(+ `question ${qidStr(qid)}: 'instructions' must not be empty; add the text the model should answer`,+ );+ }+ // Python allows str, dict, list, int and float. A bool is an int there, so it passes as well;+ // a function or a symbol is nothing either side can turn into a question.+ if (!["string", "object", "number", "bigint", "boolean"].includes(typeof ins)) {+ throw new Error(+ `question ${qidStr(qid)}: 'instructions' must be a string, dict, or list, got ${typeof ins}`,+ );+ }const crit = q["criteria"];if (t === "choice") {if (typeof crit !== "object" || crit === null) {diff --git a/laya-ts/tests/question-instructions.test.ts b/laya-ts/tests/question-instructions.test.tsnew file mode 100644index 00000000..dbf97c8c--- /dev/null+++ b/laya-ts/tests/question-instructions.test.ts@@ -0,0 +1,88 @@+// A question's `instructions` is the text the model is asked, so a null or empty one is not a+// weak question, it is a question with nothing to answer. Python's `Agent._check_question`+// rejects null, a blank string, and an empty list or dict; the TypeScript port only checked that+// the key was present, and `toInternal` then serialised whatever it found, so `null` reached the+// model as the literal prompt "null" and `{}` as "{}".+import { describe, expect, it } from "vitest";+import { Agent, checkQuestion } from "../src/agent.js";++const fakeProvider = () => ({+ 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 CHOICE = { type: "choice", criteria: { x: "yes", y: "no" } };++// Python: agent.py _check_question, the three rejections in a row.+const REJECTED: Array<[string, unknown]> = [+ ["null", null],+ ["undefined", undefined],+ ["an empty string", ""],+ ["a blank string", " "],+ ["an empty list", []],+ ["an empty dict", {}],+];++describe("checkQuestion instructions", () => {+ it.each(REJECTED)("rejects %s", (_label, ins) => {+ expect(() => checkQuestion("q", { ...CHOICE, instructions: ins })).toThrow(+ /'instructions' must not be (None|empty)/,+ );+ });++ it("names the question and what to fix", () => {+ expect(() => checkQuestion("q", { ...CHOICE, instructions: null })).toThrow(+ 'question "q": \'instructions\' must not be None',+ );+ });++ it("accepts instructions that can be answered", () => {+ for (const ins of ["Is it urgent?", " padded ", 42, { k: "v" }, ["a", "b"]]) {+ expect(() => checkQuestion("q", { ...CHOICE, instructions: ins })).not.toThrow();+ }+ });++ it("still reports a missing key as missing", () => {+ expect(() => checkQuestion("q", { ...CHOICE })).toThrow("no 'instructions'");+ });+});++describe("checkQuestion id", () => {+ it("rejects a blank question id, as Python does", () => {+ for (const qid of ["", " "]) {+ expect(() => checkQuestion(qid, { ...CHOICE, instructions: "q?" })).toThrow(+ "question id must be a non-empty string",+ );+ }+ });+});++describe("Agent", () => {+ it("rejects an unanswerable question instead of prompting with it", async () => {+ const a = new Agent({ provider: fakeProvider() } as any);+ for (const method of ["predict", "systemOne"] as const) {+ for (const [_label, ins] of REJECTED) {+ await expect(+ a[method]("hello", { q: { ...CHOICE, instructions: ins } } as any),+ ).rejects.toThrow('question "q": \'instructions\' must not be');+ }+ }+ });++ it("still answers a question whose instructions are real", async () => {+ const a = new Agent({ provider: fakeProvider() } as any);+ const r: any = await a.systemOne("hi", {+ q: { ...CHOICE, instructions: "Is it urgent?" },+ });+ expect(r.answers.q.choice).toBe("x");+ });+});