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.

feat(sdk): type the whole usage report /v1/systemone answers with

Aashish2a8c96223f45
diff --git a/sdk/typescript/src/response.ts b/sdk/typescript/src/response.ts
index d6eb8468..68baf021 100644
--- a/sdk/typescript/src/response.ts
+++ b/sdk/typescript/src/response.ts
@@ -6,6 +6,7 @@ function expect(condition: unknown, field: string): asserts condition {
if (!condition) throw new LayaResponseError(`Invalid Laya response: ${field}`);
}
const number = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value);
+const count = (value: unknown): value is number => Number.isInteger(value);
const probability = (value: unknown) => number(value) && value >= 0 && value <= 1;
const model = (value: unknown) => typeof value === 'string' && ['english', 'multilingual', 'typed-decisions'].includes(value);
@@ -34,9 +35,38 @@ export function validateHealth(value: unknown): void {
}
}
+export function validateUsage(value: unknown): void {
+ // The two token counts are what Jev decodes, so they are the only required fields: a
+ // self-hosted deployment predating the truncation report (#174) answers with those two and is
+ // a valid prediction. `Usage` types the fields the current server always sends. Each of the
+ // rest is checked whenever the server does send it, and a half-report is refused rather than
+ // passed through, because these are the only fields that make a truncated answer visible.
+ expect(isRecord(value), 'usage');
+ expect(Number.isInteger(value.input_tokens) && Number.isInteger(value.output_tokens), 'usage');
+ if (value.state_tokens !== undefined) expect(Number.isInteger(value.state_tokens), 'usage.state_tokens');
+ if (value.state_tokens_dropped !== undefined) {
+ expect(Number.isInteger(value.state_tokens_dropped), 'usage.state_tokens_dropped');
+ }
+ if (value.truncated !== undefined) expect(typeof value.truncated === 'boolean', 'usage.truncated');
+ if (value.truncated_questions !== undefined) {
+ expect(Array.isArray(value.truncated_questions) &&
+ value.truncated_questions.every((qid: unknown) => typeof qid === 'string'), 'usage.truncated_questions');
+ }
+ if (value.options === undefined) return;
+ expect(isRecord(value.options), 'usage.options');
+ for (const collapse of Object.values(value.options)) {
+ expect(isRecord(collapse), 'usage.options');
+ expect(count(collapse.total) && count(collapse.distinct) && collapse.distinct <= collapse.total,
+ 'usage.options');
+ expect(Object.hasOwn(collapse, 'tokens_per_option') &&
+ (collapse.tokens_per_option === null || count(collapse.tokens_per_option)),
+ 'usage.options.tokens_per_option');
+ }
+}
+
export function validatePrediction(value: unknown, questions: Questions): void {
expect(isRecord(value) && typeof value.model === 'string' && isRecord(value.answers), 'prediction');
- expect(isRecord(value.usage) && Number.isInteger(value.usage.input_tokens) && Number.isInteger(value.usage.output_tokens), 'usage');
+ validateUsage(value.usage);
if (value.routing !== undefined) validateRoute(value.routing);
for (const [id, question] of Object.entries(questions)) {
const answer = value.answers[id];
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index 5a0b62a3..7e418b50 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -97,10 +97,43 @@ export interface RouteDecision {
detection: LanguageDetection | null;
workflow: string | null;
}
+/** One question whose options no longer have a token span each after the head budget (#538). */
+export interface OptionCollapse {
+ /** Options the question defines, not the option markers that reached the sequence. */
+ total: number;
+ /** Of those, the ones still carrying a span of their own. */
+ distinct: number;
+ /** Tokens the budget allowed each option, or null when none was capped. */
+ tokens_per_option: number | null;
+}
+/** What the forward pass was built from, as `/v1/systemone` reports it.
+ *
+ * How much of a state the model reads is a token budget, not a character count, and the budget
+ * moves with `max_len`, `head_max_len` and each question's own option prompt (#174). A truncated
+ * answer is still an answer, so these keys are the only place the cut is visible: nothing in
+ * `answers` changes when evidence is dropped.
+ */
+export interface Usage {
+ /** Non-pad tokens of the state's rows, one row per question, so it grows with the questions. */
+ input_tokens: number;
+ /** Always 0: the head answers in one pass and generates nothing. */
+ output_tokens: number;
+ /** Tokens the whole serialized state needs. */
+ state_tokens: number;
+ /** Tokens of it at least one question did not get: the worst case over the questions, since
+ * each leaves the state a different room. */
+ state_tokens_dropped: number;
+ /** Whether that worst case dropped anything. */
+ truncated: boolean;
+ /** The ids whose own window was cut, empty when none was. */
+ truncated_questions: string[];
+ /** Present only when some question's options were collapsed, keyed by question id. */
+ options?: Record<string, OptionCollapse>;
+}
export interface Prediction<Q extends Questions = Questions> {
model: string;
answers: Answers<Q>;
- usage: { input_tokens: number; output_tokens: number };
+ usage: Usage;
/** Optional routing metadata returned by Laya. */
routing?: RouteDecision;
}