Repository navigation
feat(ts): port structured decisions (decide) to laya-ts - #339
Merged
Merged
Conversation
2 tasks done
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Ports
laya/structured.py(#280) — schema-driven decisions viadecide()— tolaya-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"levelcriteria, boolean → noul. Same caps (
MAX_PROPERTIES/MAX_OPTIONS/MAX_SCORE_LEVELS)and the same path-naming
SchemaErrormessages for free strings, arrays, nested objectsand
$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 ofschema/opts.questions(sameerror message as Python), raw answers on the questions pass-through, remaining options
forwarded to
predict, andreturnDetails: trueyields aDecisionResultwithper-field confidence, probabilities (noul reports
{ false, true }), raw answers,usageandrouting.Agent.decideandRouter.decideconvenience methods, matching Python'sAgent.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) mirroringtests/test_structured.pycase 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,nullenum members, and anend-to-end
Agent.decideagainst a stub provider.npx tsc --noEmitclean; full suite 189/189.Notes
laya-serve) — that PR wiresthe HTTP surface in Python; this one brings the core
structuredmodule to the TS SDK.No file overlap.