Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(laya-ts): take the noul enum shortcut only for two distinct booleans

thread3da1eb8f6f3277
diff --git a/laya-ts/src/structured.ts b/laya-ts/src/structured.ts
index f1b2f2fe..8949d8f0 100644
--- a/laya-ts/src/structured.ts
+++ b/laya-ts/src/structured.ts
@@ -145,7 +145,14 @@ function enumField(path: string, name: string, values: unknown[], description: u
throw new SchemaError(`${path}: ${values.length} options exceeds MAX_OPTIONS=${MAX_OPTIONS}`);
}
if (values.length === 0) throw new SchemaError(`${path}: 'enum' must not be empty`);
- if (values.every((v) => typeof v === "boolean")) return noulField(name, description);
+ // Python's `_enum_field` takes the noul shortcut only for exactly two DISTINCT booleans
+ // (`all(isinstance(v, bool) ...) and len(set(values)) == 2`). Without the distinctness test a
+ // one-value enum became a noul here and a one-option choice there -- a different question type
+ // and a different projected value type (boolean vs "true") from the documented mapping -- and
+ // `[true, true]` became a noul instead of the duplicate-label SchemaError both SDKs promise.
+ if (values.length === 2 && values.every((v) => typeof v === "boolean") && values[0] !== values[1]) {
+ return noulField(name, description);
+ }
const options: [string, unknown][] = values.map((v) => [v == null ? "null" : String(v), v]);
if (new Set(options.map(([label]) => label)).size !== options.length) {
throw new SchemaError(`${path}: enum values produce duplicate choice labels`);
diff --git a/laya-ts/tests/structured.test.ts b/laya-ts/tests/structured.test.ts
index ed415a6e..4d9cd41b 100644
--- a/laya-ts/tests/structured.test.ts
+++ b/laya-ts/tests/structured.test.ts
@@ -408,6 +408,48 @@ describe("structured/answerConfidence", () => {
});
});
+describe("structured/boolean enums", () => {
+ // Python parity (laya/structured.py::_enum_field): the noul shortcut is exactly two distinct
+ // booleans. A single-value enum plans as a one-option choice there, and a duplicate enum raises
+ // the duplicate-label SchemaError -- both of which this side used to plan as a noul, changing
+ // the question type and the projected value type ("true" vs boolean).
+ it("plans a two-value boolean enum as a noul", () => {
+ const [f] = planFromJsonSchema({ type: "object", properties: { a: { enum: [true, false] } } });
+ expect(f.kind).toBe("noul");
+ expect(f.question.type).toBe("noul");
+ });
+
+ it("plans a single-value boolean enum as a one-option choice", () => {
+ const fields = planFromJsonSchema({ type: "object", properties: { a: { enum: [true] } } });
+ expect(fields[0].kind).toBe("choice");
+ expect(fields[0].question.type).toBe("choice");
+ expect(fields[0].question.criteria).toEqual({ true: null });
+ });
+
+ it("refuses a duplicate boolean enum instead of planning a noul", () => {
+ expect(() => planFromJsonSchema({ type: "object", properties: { a: { enum: [true, true] } } })).toThrow(
+ SchemaError,
+ );
+ expect(() => planFromJsonSchema({ type: "object", properties: { a: { enum: [false, false] } } })).toThrow(
+ /duplicate choice labels/,
+ );
+ });
+
+ it("projects the one-option choice back to the boolean value", async () => {
+ const runner = {
+ async predict() {
+ return {
+ model: "fake",
+ answers: { a: { type: "choice", choice: "true", confidence: 0.9, probabilities: { true: 0.9 } } },
+ };
+ },
+ };
+ expect(await decide(runner as any, "state", { type: "object", properties: { a: { enum: [true] } } })).toEqual({
+ a: true,
+ });
+ });
+});
+
describe("structured/decideBatch", () => {
const fakeBatchRunner = (answersPerState: Record<string, any>[]) => ({
calls: [] as any[],