From b9822bca93d6ec6450e3f062d7a39a4c88f63a12 Mon Sep 17 00:00:00 2001 From: logic10492 <15616843641@163.com> Date: Thu, 28 May 2026 12:44:30 +0800 Subject: [PATCH 1/2] fix(kosong): correct type inference when enum/const contradicts explicit type --- packages/kosong/src/providers/kimi-schema.ts | 30 ++++++++++++++----- .../kosong/test/providers/kimi-schema.test.ts | 20 +++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/packages/kosong/src/providers/kimi-schema.ts b/packages/kosong/src/providers/kimi-schema.ts index f603a144..53d15ea1 100644 --- a/packages/kosong/src/providers/kimi-schema.ts +++ b/packages/kosong/src/providers/kimi-schema.ts @@ -300,15 +300,29 @@ function normalizeProperty(node: unknown): void { return; } - if (!hasOwn(node, 'type') && !hasAnyKey(node, TYPE_COMPLETION_SKIP_KEYS)) { - const enumValues = node['enum']; - if (Array.isArray(enumValues) && enumValues.length > 0) { - node['type'] = inferTypeFromValues(enumValues); - } else if (hasOwn(node, 'const')) { - node['type'] = inferTypeFromValues([node['const']]); - } else { - node['type'] = inferTypeFromStructure(node); + const enumValues = node['enum']; + const hasEnum = Array.isArray(enumValues) && enumValues.length > 0; + const hasConst = hasOwn(node, 'const'); + + if (hasEnum || hasConst) { + if (!hasOwn(node, 'type') && !hasAnyKey(node, TYPE_COMPLETION_SKIP_KEYS)) { + node['type'] = hasEnum + ? inferTypeFromValues(enumValues) + : inferTypeFromValues([node['const']]); + } else if (hasOwn(node, 'type') && !hasAnyKey(node, TYPE_COMPLETION_SKIP_KEYS)) { + const inferredType = hasEnum + ? inferTypeFromValues(enumValues) + : inferTypeFromValues([node['const']]); + if (node['type'] !== inferredType) { + node['type'] = inferredType; + } } + recurseSchema(node); + return; + } + + if (!hasOwn(node, 'type') && !hasAnyKey(node, TYPE_COMPLETION_SKIP_KEYS)) { + node['type'] = inferTypeFromStructure(node); } recurseSchema(node); diff --git a/packages/kosong/test/providers/kimi-schema.test.ts b/packages/kosong/test/providers/kimi-schema.test.ts index c257ecf3..fb6edc6c 100644 --- a/packages/kosong/test/providers/kimi-schema.test.ts +++ b/packages/kosong/test/providers/kimi-schema.test.ts @@ -372,6 +372,26 @@ describe('normalizeKimiToolSchema', () => { }); }); + it('fixes a mismatched type when enum/const values contradict it', () => { + const schema = { + type: 'object', + properties: { + operation: { type: 'object', enum: ['move', 'copy'] }, + mode: { type: 'array', const: 'fast' }, + }, + }; + + const result = normalizeKimiToolSchema(schema); + + expect(result).toEqual({ + type: 'object', + properties: { + operation: { type: 'string', enum: ['move', 'copy'] }, + mode: { type: 'string', const: 'fast' }, + }, + }); + }); + it('infers object and array property types from container enum/const values', () => { const schema = { type: 'object', From 06d9255b3cf5e0d5476b9588ee88093b08d1d168 Mon Sep 17 00:00:00 2001 From: logic10492 <15616843641@163.com> Date: Thu, 28 May 2026 12:47:39 +0800 Subject: [PATCH 2/2] chore: add changeset for schema type inference fix --- .changeset/fix-schema-enum-type-inference.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/fix-schema-enum-type-inference.md diff --git a/.changeset/fix-schema-enum-type-inference.md b/.changeset/fix-schema-enum-type-inference.md new file mode 100644 index 00000000..7aad9e1a --- /dev/null +++ b/.changeset/fix-schema-enum-type-inference.md @@ -0,0 +1,6 @@ +--- +"@moonshot-ai/kosong": patch +"@moonshot-ai/kimi-code": patch +--- + +Fix type inference when enum or const values contradict the declared type in tool schemas.