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 usage contract at the type and the wire

Aashishc6d28d53e87f
diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjs
index c862f012..d4a16aa6 100644
--- a/sdk/typescript/test/client.test.mjs
+++ b/sdk/typescript/test/client.test.mjs
@@ -224,6 +224,52 @@ test('prediction validation rejects malformed required fields and optional exten
}
});
+// 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));
+ }
+});
+
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..d0dd4d5a 100644
--- a/sdk/typescript/test/types.ts
+++ b/sdk/typescript/test/types.ts
@@ -1,4 +1,4 @@
-import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer } from 'laya-client';
+import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer, type Usage } from 'laya-client';
const client = new Laya();
const questions = defineQuestions({
@@ -16,10 +16,21 @@ 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 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;
// @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 /v1/systemone does not expose standalone routing.
client.route('hello');
// @ts-expect-error task must be a string.
@@ -58,4 +69,6 @@ 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, stateTokens, stateTokensDropped, truncated, truncatedQuestions,
+ collapsedSpans, answeredUsage];