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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(sdk): reject non-producible min_confidence bucket keys before the request
Bruce-Yii1917a4a0b8e3
diff --git a/sdk/typescript/src/buckets.ts b/sdk/typescript/src/buckets.tsnew file mode 100644index 00000000..aa712035--- /dev/null+++ b/sdk/typescript/src/buckets.ts@@ -0,0 +1,20 @@+/** The option types and option-count sizes core's `temp_bucket` can produce. The bucket-key type+ * and the runtime `BUCKET_KEY` check below are both derived from these, so the keys a+ * `min_confidence` map may name cannot drift from the ones the engine can answer with. */+const BUCKET_TYPES = ['choice', 'score', 'noul'] as const;+const BUCKET_SIZES = ['2', '3-5', '6-10', '11+'] as const;++/** One bucket key core can produce, e.g. `"choice:2"` or `"score:11+"`. */+export type MinConfidenceBucket = `${(typeof BUCKET_TYPES)[number]}:${(typeof BUCKET_SIZES)[number]}`;+/** A `MinConfidenceMap` key: a bucket core can produce, or `"default"` for the rest. */+export type MinConfidenceKey = MinConfidenceBucket | 'default';++const BUCKET_KEY = new RegExp(+ `^(${BUCKET_TYPES.join('|')}):(${BUCKET_SIZES.map(size => size.replace('+', '\\+')).join('|')})$`,+);++/** True for a key core's `temp_bucket` can produce, or `"default"`. Mirrors core's+ * `check_min_confidence`, so a gate the server would 422 is refused before the request. */+export function isMinConfidenceKey(key: string): key is MinConfidenceKey {+ return key === 'default' || BUCKET_KEY.test(key);+}diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.tsindex b5f14e69..c802b018 100644--- a/sdk/typescript/src/client.ts+++ b/sdk/typescript/src/client.ts@@ -3,6 +3,7 @@ import {LayaTimeoutError, LayaValidationError,} from './errors.js';import type { Health, Prediction, PredictOptions, Questions, RequestOptions, State } from './types.js';+import { isMinConfidenceKey } from './buckets.js';import { isRecord, validateJson, validateQuestions, validateTimeout } from './validation.js';import { validateHealth, validatePrediction } from './response.js';@@ -12,11 +13,14 @@ function isThreshold(value: unknown): value is number {}/** Mirror core's `check_min_confidence`: one threshold for every answer, or a non-empty- * per-bucket map whose values are all thresholds. Anything else is refused before the- * request goes out, so a caller cannot pay for inference with a gate the server would 422. */+ * per-bucket map whose keys core can produce and whose values are all thresholds. Anything else+ * is refused before the request goes out, so a caller cannot pay for inference with a gate the+ * server would 422. */function validateMinConfidence(value: unknown): asserts value is number | Record<string, number> {if (isThreshold(value)) return;- if (isRecord(value) && Object.keys(value).length > 0 && Object.values(value).every(isThreshold)) return;+ if (isRecord(value) && Object.keys(value).length > 0+ && Object.keys(value).every(isMinConfidenceKey)+ && Object.values(value).every(isThreshold)) return;throw new LayaValidationError('minConfidence must be a number between 0 and 1, or a non-empty map of bucket to threshold');}diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.tsindex 0e012d7a..4515e96f 100644--- a/sdk/typescript/src/types.ts+++ b/sdk/typescript/src/types.ts@@ -1,3 +1,7 @@+import type { MinConfidenceKey } from './buckets.js';++export type { MinConfidenceBucket, MinConfidenceKey } from './buckets.js';+/** Values that survive the JSON transport without being changed or discarded. */export type JsonValue = string | number | boolean | null | readonly JsonValue[] | { readonly [key: string]: JsonValue };export type State = string | readonly JsonValue[] | { readonly [key: string]: JsonValue } | null;@@ -27,8 +31,10 @@ export type ModelAlias = ModelName | 'en' | 'laya' | 'default' | 'multi' | 'ml'* spelling (`"choice:2"`, `"choice:3-5"`, `"score:6-10"`, `"noul:2"`, ...) plus an optional* `"default"` for buckets the map does not name. One threshold does not transfer across option* counts, so each bucket is gated at the level its own calibration earns; fit one with- * `laya.calibrate.fit_abstention_thresholds`. Every value is a threshold in `[0, 1]`. */-export type MinConfidenceMap = Record<string, number>;+ * `laya.calibrate.fit_abstention_thresholds`. Every value is a threshold in `[0, 1]`. Only keys+ * core can produce type-check, so a map like `{ "choice:2-5": 0.9 }` is refused here as well as+ * by the client's runtime validation (#1002). */+export type MinConfidenceMap = { [K in MinConfidenceKey]?: number };/** The abstention gate: one threshold for every answer, or a per-bucket map. */export type MinConfidence = number | MinConfidenceMap;diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjsindex fc4ae4ab..d67477f0 100644--- a/sdk/typescript/test/client.test.mjs+++ b/sdk/typescript/test/client.test.mjs@@ -115,6 +115,8 @@ test('invalid model and control values fail before sending a request', async (){ minConfidence: true }, { minConfidence: [] }, { minConfidence: {} },{ minConfidence: { 'choice:2': 1.5 } }, { minConfidence: { 'choice:2': 'high' } },{ minConfidence: { 'choice:2': NaN } },+ { minConfidence: { 'choice:2-5': 0.9 } }, { minConfidence: { 'foo:2': 0.9 } },+ { minConfidence: { 'choice:2': 0.9, 'score:1': 0.5 } },]) {await assert.rejects(client.predict('hello', questions, options), LayaValidationError, JSON.stringify(options));}diff --git a/sdk/typescript/test/types.ts b/sdk/typescript/test/types.tsindex 5931132c..078a0f2b 100644--- a/sdk/typescript/test/types.ts+++ b/sdk/typescript/test/types.ts@@ -1,5 +1,5 @@import { Laya, defineQuestions, emailQuestions, triageQuestions, type Questions, type Answer,- type Usage, type LanguageDetection, type ChoiceAnswer } from 'laya-client';+ type Usage, type LanguageDetection, type ChoiceAnswer, type MinConfidenceMap } from 'laya-client';const client = new Laya();const questions = defineQuestions({@@ -73,6 +73,13 @@ client.predict('hello', questions, { task: 'typed', lang: 'de', langGuess: 'fr',client.predict('hello', questions, { minConfidence: { 'choice:2': 0.9, default: 0.3 } });// @ts-expect-error A threshold map with a non-threshold value is not a gate.client.predict('hello', questions, { minConfidence: { 'choice:2': 'high' } });+// @ts-expect-error "choice:2-5" matches core's bucket pattern but is not a bucket it can produce.+client.predict('hello', questions, { minConfidence: { 'choice:2-5': 0.9 } });+// @ts-expect-error "foo:2" names no option type core can produce.+client.predict('hello', questions, { minConfidence: { 'foo:2': 0.9 } });+// A map assembled at run time is still accepted: Record<string, number> stays assignable.+const dynamicThresholds: MinConfidenceMap = Object.fromEntries([['choice:2', 0.4]]);+client.predict('hello', questions, { minConfidence: dynamicThresholds });new Laya({ model: 'english' });// @ts-expect-error A score answer has no choice field.result.answers.priority.choice;