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.

Merge pull request #821 from aashish254/fix-sdk-mixed-segment

Nandakishor03a408f9e43e
diff --cc sdk/typescript/test/client.test.mjs
index d4a16aa6,37ad3def..cb367708
--- a/sdk/typescript/test/client.test.mjs
+++ b/sdk/typescript/test/client.test.mjs
@@@ -224,52 -224,44 +224,90 @@@ test('prediction validation rejects mal
}
});
+// The key set is what a live `/v1/systemone` answer carries: both agents build these six keys for
+// any non-empty question set, and `options` joins them when the head budget leaves some question's
+// options sharing a token span (#538). The counts are chosen to exercise a cut state, which a
+// checkpoint that small cannot produce, and both reported `tokens_per_option` shapes.
+const truncationReport = { input_tokens: 43, output_tokens: 0, state_tokens: 120,
+ state_tokens_dropped: 76, truncated: true, truncated_questions: ['refund'] };
+
+test('the truncation report reaches the caller verbatim and is validated when sent', async () => {
+ const reported = structuredClone(prediction);
+ reported.usage = truncationReport;
+ const client = new Laya({ fetch: async () => json(reported) });
+ assert.deepEqual((await client.predict('Hello', questions)).usage, truncationReport);
+
+ const collapsed = structuredClone(reported);
+ collapsed.usage.options = { refund: { total: 4, distinct: 1, tokens_per_option: null },
+ team: { total: 2, distinct: 1, tokens_per_option: 4 } };
+ const collapseClient = new Laya({ fetch: async () => json(collapsed) });
+ assert.deepEqual((await collapseClient.predict('Hello', questions)).usage.options,
+ collapsed.usage.options);
+
+ // a server answering only the two keys Jev decodes is still a valid prediction
+ const legacy = structuredClone(prediction);
+ const legacyClient = new Laya({ fetch: async () => json(legacy) });
+ assert.deepEqual((await legacyClient.predict('Hello', questions)).usage,
+ { input_tokens: 10, output_tokens: 0 });
+
+ // but a half-report is refused rather than passed through as a budget fact
+ for (const mutate of [
+ u => { delete u.input_tokens; },
+ u => { u.state_tokens = '120'; },
+ u => { u.state_tokens_dropped = 76.5; },
+ u => { u.truncated = 'true'; },
+ u => { u.truncated_questions = 'refund'; },
+ u => { u.truncated_questions = [1]; },
+ u => { u.options = []; },
+ u => { u.options = { refund: { total: 4, distinct: 1 } }; },
+ u => { u.options = { refund: { total: 4, distinct: 5, tokens_per_option: 4 } }; },
+ u => { u.options = { refund: { total: 4, distinct: 1, tokens_per_option: '4' } }; },
+ ]) {
+ const bad = structuredClone(reported);
+ mutate(bad.usage);
+ await assert.rejects(new Laya({ fetch: async () => json(bad) }).predict('Hello', questions),
+ LayaResponseError, JSON.stringify(bad.usage));
+ }
+});
+
+ // 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 --cc sdk/typescript/test/types.ts
index d0dd4d5a,d4e410d2..6569dd6d
--- a/sdk/typescript/test/types.ts
+++ b/sdk/typescript/test/types.ts
@@@ -1,4 -1,5 +1,5 @@@
- import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer, type Usage } from 'laya-client';
+ import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer,
- type LanguageDetection } from 'laya-client';
++ type Usage, type LanguageDetection } from 'laya-client';
const client = new Laya();
const questions = defineQuestions({
@@@ -16,21 -17,16 +17,27 @@@ const noul: number = result.answers.ref
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 stateTokens: number = result.usage.state_tokens;
+const stateTokensDropped: number = result.usage.state_tokens_dropped;
+const truncated: boolean = result.usage.truncated;
+const truncatedQuestions: string[] = result.usage.truncated_questions;
+const collapsedSpans: number | null | undefined = result.usage.options?.refund?.tokens_per_option;
+ 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 truncation report is a boolean flag, not a count or a string.
+const truncatedText: string = result.usage.truncated;
+const answeredUsage: Usage = { input_tokens: 43, output_tokens: 0, state_tokens: 12,
+ state_tokens_dropped: 0, truncated: false, truncated_questions: [] };
+// @ts-expect-error `windows` is Agent.predict_long's own key; /v1/systemone never answers with it.
+const windowedUsage: Usage = { ...answeredUsage, windows: 1 };
+ // @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.
@@@ -70,5 -66,4 +77,5 @@@ if (answer?.type === 'choice')
void [dynamicLabel, dynamicProbability];
}
void [label, arrayLabel, probability, score, noul, inlineLabel, intent, category, routingModel,
- actionProbability, noulConfidence, mixedSegment, undetermined];
+ actionProbability, noulConfidence, stateTokens, stateTokensDropped, truncated, truncatedQuestions,
- collapsedSpans, answeredUsage];
++ collapsedSpans, answeredUsage, mixedSegment, undetermined];