Skip to content

feat(ts): port structured decisions (decide) to laya-ts - #339

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-ts-structured
Sep 24, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-ts-structured

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

Ports laya/structured.py (#280) — schema-driven decisions via decide() — to laya-ts.
The Python package can turn a JSON schema (or pydantic model) into Laya questions and typed
values; the TS port had no equivalent, so JS/TS users had to hand-build questions and
re-project answers themselves.

Mirrors the Python contract exactly:

  • questionsFromJsonSchema / planFromJsonSchema: enum → choice (string labels, null
    → "null", all-boolean enum → noul), bounded integer → score with "0".."N" level
    criteria, boolean → noul. Same caps (MAX_PROPERTIES/MAX_OPTIONS/MAX_SCORE_LEVELS)
    and the same path-naming SchemaError messages for free strings, arrays, nested objects
    and $ref.
  • answersToJson: choice label → original value (integer enums keep their type), score →
    argmax level (falling back to the rounded score when probabilities are absent), noul →
    boolean at the 0.5 threshold.
  • decide(runner, state, schema?, opts?): exactly one of schema/opts.questions (same
    error message as Python), raw answers on the questions pass-through, remaining options
    forwarded to predict, and returnDetails: true yields a DecisionResult with
    per-field confidence, probabilities (noul reports { false, true }), raw answers,
    usage and routing.
  • Agent.decide and Router.decide convenience methods, matching Python's
    Agent.decide/Router.decide.

TS idioms where the ecosystems differ: camelCase names, async (predict returns a Promise),
and instead of pydantic, any object exposing toJSONSchema() (e.g. z.toJSONSchema(Model)
for Zod 4) is accepted as the schema.

Tests

New laya-ts/tests/structured.test.ts (24 tests) mirroring tests/test_structured.py
case for case — mapping, projection (including the argmax-vs-raw-score case and
integer-enum type preservation), all ten rejection paths with their messages, decide
semantics (details, pass-through, kwarg forwarding, exactly-one validation) — plus
TS-specific coverage: the toJSONSchema() duck type, const, null enum members, and an
end-to-end Agent.decide against a stub provider.

npx tsc --noEmit clean; full suite 189/189.

Notes

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you for bringing schema-driven decisions to the TypeScript SDK. JS and TS users can now pass a JSON schema (or a Zod schema via toJSONSchema) and get typed values back, just like Python's decide(), and the tests cover the Python cases one for one, which made this easy to review. Merging.

@NandhaKishorM
NandhaKishorM merged commit d4cd60c into NandhaKishorM:main Sep 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants