Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fix-schema-enum-type-inference.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 22 additions & 8 deletions packages/kosong/src/providers/kimi-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions packages/kosong/test/providers/kimi-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down