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.

fix(typescript-sdk): accept option_order and noul labels on a question

thread3d5c1a56395d6f
diff --git a/sdk/typescript/src/validation.ts b/sdk/typescript/src/validation.ts
index 5aefc312..9a0989e0 100644
--- a/sdk/typescript/src/validation.ts
+++ b/sdk/typescript/src/validation.ts
@@ -25,6 +25,21 @@ export function validateJson(value: unknown, path = 'request', ancestors = new S
ancestors.delete(value);
}
+/**
+ * The fields a question may carry. `type`, `instructions` and `criteria` are the schema; the
+ * other two are documented extensions the server honours (`laya/agent.py::_check_question`) and
+ * the Python client passes through unchanged ("Question extensions such as `option_order` pass
+ * through unchanged", `sdk/python/README.md`):
+ *
+ * * `option_order` -- a permutation of the option indices, controlling positional option bias;
+ * * `labels` -- custom wording for a `noul` question's true/false options.
+ *
+ * They were missing from this whitelist, so both were rejected as "unknown question field" and
+ * the request never left the process, even though the server accepts them. The set stays closed
+ * otherwise, so a typo (`instruction`) is still caught here rather than by a 422.
+ */
+const QUESTION_FIELDS = new Set(['type', 'instructions', 'criteria', 'option_order', 'labels']);
+
export function validateQuestions(questions: unknown): void {
if (!isRecord(questions) || Object.keys(questions).length === 0) {
throw new LayaValidationError('questions must be an object containing at least one question');
@@ -34,7 +49,7 @@ export function validateQuestions(questions: unknown): void {
if (!isRecord(q)) invalid('expected a question object');
const question = q as Record<string, unknown>;
if (!Object.hasOwn(question, 'instructions')) invalid('instructions is required');
- if (Object.keys(question).some(key => !['type', 'instructions', 'criteria'].includes(key))) {
+ if (Object.keys(question).some(key => !QUESTION_FIELDS.has(key))) {
invalid('unknown question field');
}
const criteria = question.criteria;
diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjs
index 1706cbe6..42ff8e06 100644
--- a/sdk/typescript/test/client.test.mjs
+++ b/sdk/typescript/test/client.test.mjs
@@ -476,3 +476,49 @@ test('presets are fresh and custom email categories are copied', () => {
assert.equal(categories.finance.description, 'money');
assert.ok(emailQuestions().category.criteria.billing);
});
+
+test('accepts the question extensions the server accepts (option_order, noul labels)', async () => {
+ // The server honours both (laya/agent.py::_check_question) and the Python client passes them
+ // through unchanged. The client's closed whitelist rejected them as "unknown question field"
+ // before the request went out, so option ordering and custom noul wording were unreachable
+ // from this SDK.
+ const extended = {
+ team: {
+ type: 'choice',
+ instructions: 'Which team?',
+ criteria: { billing: null, support: null },
+ option_order: [1, 0],
+ },
+ flag: {
+ type: 'noul',
+ instructions: 'Flag it?',
+ labels: { false: 'nope', true: 'yep' },
+ },
+ };
+ let sent = null;
+ const client = new Laya({
+ fetch: async (_url, init) => {
+ sent = JSON.parse(init.body);
+ return json({
+ model: 'laya-rl-agent',
+ routing: route,
+ usage: { input_tokens: 3, output_tokens: 0 },
+ answers: {
+ team: { type: 'choice', choice: 'support', confidence: 0.9, probabilities: { support: 0.9, billing: 0.1 } },
+ flag: { type: 'noul', noul: 0.8, confidence: 0.8 },
+ },
+ });
+ },
+ });
+ const result = await client.predict('hello', extended);
+ assert.deepEqual(sent.questions, extended, 'the extensions reach the wire unchanged');
+ assert.equal(result.answers.team.choice, 'support');
+});
+
+test('still refuses a question field that is neither schema nor a documented extension', async () => {
+ const client = new Laya({ fetch: async () => json(prediction) });
+ await assert.rejects(
+ client.predict('hello', { refund: { type: 'noul', instructions: 'Refund?', instruction: 'typo' } }),
+ (error) => error instanceof LayaValidationError && /unknown question field/.test(error.message),
+ );
+});