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.

test(sdk): pin the detection segment at the type and on the wire

Aashish16a05c81631a
diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjs
index c862f012..37ad3def 100644
--- a/sdk/typescript/test/client.test.mjs
+++ b/sdk/typescript/test/client.test.mjs
@@ -224,6 +224,44 @@ test('prediction validation rejects malformed required fields and optional exten
}
});
+// A live `/v1/systemone` answer's `routing.detection`, verbatim, for a state whose subject and
+// notes are English but whose customer message is German. Every branch of `laya.lang.analyse()`
+// reports the same eight keys, which is what tests/test_router.py pins on the Python side;
+// `mixed_segment` is the one that names the segment that pulled the state off English (#384).
+const mixedDetection = {
+ script: 'latin', script_profile: { latin: 1.0 }, language: 'de', is_english: false,
+ language_undecided: false, diacritic_rate: 0.0083, non_latin_fraction: 0.0,
+ mixed_segment: 'Ich möchte meine Bestellung stornieren, danke',
+};
+
+test('routing detection reports the segment that pulled a mostly-English state off English', async () => {
+ const routed = structuredClone(prediction);
+ routed.routing = { ...route, detection: mixedDetection,
+ reason: 'Latin script, mostly English, but a line or field reads as \'de\'' };
+ const client = new Laya({ fetch: async () => json(routed) });
+ const result = await client.predict('Hello', questions);
+ assert.equal(result.routing.detection.mixed_segment, 'Ich möchte meine Bestellung stornieren, danke');
+
+ // null is the ordinary answer: the route read the state as one language
+ const single = structuredClone(routed);
+ single.routing.detection.mixed_segment = null;
+ const singleClient = new Laya({ fetch: async () => json(single) });
+ assert.equal((await singleClient.predict('Hello', questions)).routing.detection.mixed_segment, null);
+
+ // a deployment predating #384 sends no such key, and one display field must not fail the call
+ const older = structuredClone(routed);
+ delete older.routing.detection.mixed_segment;
+ const olderClient = new Laya({ fetch: async () => json(older) });
+ assert.equal((await olderClient.predict('Hello', questions)).routing.detection.language, 'de');
+
+ for (const segment of [7, false, ['Ich'], {}]) {
+ const bad = structuredClone(routed);
+ bad.routing.detection.mixed_segment = segment;
+ await assert.rejects(new Laya({ fetch: async () => json(bad) }).predict('Hello', questions),
+ LayaResponseError, JSON.stringify(segment));
+ }
+});
+
test('network errors preserve the cause', async () => {
const cause = new TypeError('fetch failed');
await assert.rejects(new Laya({ fetch: async () => { throw cause; } }).health(), error =>
diff --git a/sdk/typescript/test/types.ts b/sdk/typescript/test/types.ts
index 7bbd0aa8..d4e410d2 100644
--- a/sdk/typescript/test/types.ts
+++ b/sdk/typescript/test/types.ts
@@ -1,4 +1,5 @@
-import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer } from 'laya-client';
+import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer,
+ type LanguageDetection } from 'laya-client';
const client = new Laya();
const questions = defineQuestions({
@@ -16,10 +17,16 @@ const noul: number = result.answers.refund.noul;
const routingModel: string | undefined = result.routing?.model;
const actionProbability: number | undefined = result.answers.team.action?.act_probability;
const noulConfidence: number | undefined = result.answers.refund.confidence;
+const mixedSegment: string | null | undefined = result.routing?.detection?.mixed_segment;
// @ts-expect-error Routing metadata is optional.
result.routing.model;
// @ts-expect-error Action metadata is optional.
result.answers.team.action.act_probability;
+// @ts-expect-error The mixed segment names the text that was read, not a score.
+const mixedAsNumber: number = result.routing?.detection?.mixed_segment;
+// @ts-expect-error Every branch of analyse() reports the key, so the type does not make it optional.
+const undetermined: LanguageDetection = { script: 'latin', script_profile: { latin: 1.0 }, language: 'de',
+ is_english: false, language_undecided: false, diacritic_rate: 0.0083, non_latin_fraction: 0.0 };
// @ts-expect-error /v1/systemone does not expose standalone routing.
client.route('hello');
// @ts-expect-error task must be a string.
@@ -58,4 +65,5 @@ if (answer?.type === 'choice') {
const dynamicProbability: number | undefined = answer.probabilities.anyLabel;
void [dynamicLabel, dynamicProbability];
}
-void [label, arrayLabel, probability, score, noul, inlineLabel, intent, category, routingModel, actionProbability, noulConfidence];
+void [label, arrayLabel, probability, score, noul, inlineLabel, intent, category, routingModel,
+ actionProbability, noulConfidence, mixedSegment, undetermined];