Repository navigation
fix(sdk): type the mixed_segment a routing detection answer carries - #821
Conversation
`laya.lang.analyse()` reports eight keys on every branch, and a live `/v1/systemone` answer was measured sending all eight, but `LanguageDetection` declared seven. So the field that names the line or which pulled a mostly-English state off the English checkpoint (NandhaKishorM#384) could not be read from TypeScript: a caller sees `is_english: false` and `language: de` with nothing pointing at the segment the router actually acted on, and the answer to "which of my fields is the non-English one?" was only in the free-text `reason`. `validateRoute` checks it when the server sends it, as a null or a string, so a deployment older than NandhaKishorM#384 stays usable while a malformed one cannot pass.
test/types.ts reads the field, refuses to type it as a number, and refuses a detection literal that leaves it out. client.test.mjs drives a witnessed detection through the client: the segment arrives, null is the ordinary answer, a server that sends no such key still predicts, and four malformed ones do not.
|
Merged for 0.3.24. One note on the merge, since #820 and #822 landed in the same pass and all three touch the same two test files: the conflict in The field itself is worth typing. Verified: SDK suite 26 tests passing, Thank you. |
What
sdk/typescript(laya-client) learns the eighth field of a routing detection report.src/types.ts:LanguageDetectiongainsmixed_segment: string | null.src/response.ts:validateRoute()acceptsnullor a string for it when the server sends it.test/types.ts: the field is readable and typed as text, not as a number, and a detection literalwithout it is a type error.
test/client.test.mjs: one wire test with the detection dict captured from a live answer.Why
laya.lang.analyse()reportsmixed_segmenton every branch (laya/lang.py:580,584,596,675) and itis the field that explains a decision: when a support ticket reads as English overall because the
stack trace is longer than the customer's sentence,
mixed_segmentnames the line that was Germanand sent the state to the multilingual checkpoint.
router.py:843-845puts it in the reason string,docs/http-api.md:191lists it among the eight documented keys,tests/test_router.py:99pins thekey set, and
laya-ts/src/lang.ts:373already types it asmixedSegment: string | null.laya-clientis the one reader left out: the interface stopped at seven fields, so a TypeScriptcaller touching
detection.mixed_segmentgetsTS2339: Property 'mixed_segment' does not exist on type 'LanguageDetection'and has to cast throughanyto read a field that is present whenever theserver reports a detection at all.
One deliberate choice: the type says required, the validator says checked-when-present. Every
branch of
analyse()reports the key, so the type must not let a caller forget it exists -- that iswhat makes the omission a compile error. The validator cannot require it, because a deployment that
predates that field would then fail every prediction; it follows the idiom already in this file for
validateHealth()'s withheld fields and the optionalusagefields.How it was verified
The detection dicts are not transcribed from the docs -- they were POSTed to the repo's own fixture
server (
tests/sdk_server_fixture.py, tiny random weights, no downloads) with nomodelso theRouter really routes. Every answer carries all eight keys, and the mostly-English state names its
German line:
{"diacritic_rate": 0.0083, "is_english": false, "language": "de", "language_undecided": false, "mixed_segment": "Ich möchte meine Bestellung stornieren, danke", "non_latin_fraction": 0.0, "script": "latin", "script_profile": {"latin": 1.0}}The integration test is the anchor: it deep-equals the SDK's parsed prediction against
agent.predict()from Python, so the field survives the real HTTP round-trip and matches core.Mutation check -- each arm breaks one claim,
git checkout --restores it, and every arm must gored for the named reason (
tscerror or assertion):test/types.ts(20,76): error TS2339: Property 'mixed_segment' does not exist on type 'LanguageDetection'.test/types.ts(20,7): error TS2322: Type 'number | undefined' is not assignable to type 'string | null | undefined'.test/types.ts(27,1): error TS2578: Unused '@ts-expect-error' directive.(the 7-key literal stops being an error)routing detection reports the segment that pulled a mostly-English state off Englishfails -- the rejection loopnullis no longer an accepted answer[7, false, ['Ich'], {}]are all accepted7/7 arms red, 0 survivors. Tree clean afterwards (
git status --porcelain-> empty, each armrestored with
git checkout --before the next).Checklist
main(branch is off4aa6761;git diff upstream/main...HEADis these 4 files only)npm test21/21,npm run test:integration1/1, mutation 7/7)docs/http-api.md:191already names all eight keys, and neither
sdk/typescript/README.mdnordocs/typescript-sdk.mdenumerates the client's detection fields. No Python changed, so
ruff/compileallare unaffected.Fixes #