Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: preserve referenced types when normalizing Kimi enums

jonathanwxh-cell25cd283eb157
diff --git a/server/mcp-tool-schema.test.ts b/server/mcp-tool-schema.test.ts
index c97b958bb9..94a84dad79 100644
--- a/server/mcp-tool-schema.test.ts
+++ b/server/mcp-tool-schema.test.ts
@@ -171,6 +171,22 @@ describe("createToolListNormalizer", () => {
describe("a tool list under a provider's schema profile", () => {
const small = { name: "search", description: "Find pages.", inputSchema: { type: "object", properties: { query: { type: "string" } } } };
+ it("preserves a referenced type beside a mixed enum", () => {
+ const inputSchema = { type: "object", $defs: {
+ text: { type: "string" }, integer: { type: "integer" },
+ alias: { $ref: "#/$defs/text" },
+ }, properties: {
+ choice: { $ref: "#/$defs/alias", enum: ["a", null] },
+ count: { $ref: "#/$defs/integer", enum: [1, 2.5, null] },
+ nullable: { enum: ["a", null] },
+ } };
+ const [tool] = fitToolList([{ name: "choose", inputSchema }], MOONSHOT) as Array<{ inputSchema: typeof inputSchema }>;
+ expect(tool!.inputSchema.properties.choice).toEqual(inputSchema.properties.choice);
+ expect(tool!.inputSchema.properties.count).toEqual(inputSchema.properties.count);
+ expect(tool!.inputSchema.properties.nullable).toEqual({ enum: ["a", null], type: ["string", "null"] });
+ expect(inputSchema.properties.nullable).not.toHaveProperty("type");
+ });
+
it("keeps nullable and mixed enum choices usable in Kimi's tools/list", () => {
const genre = { enum: ["puzzle", "rpg", null], description: "Choose a genre" };
const values = { enum: [1, 2.5, false, null, { name: "one" }, []] };
diff --git a/server/tool-schema-profile.ts b/server/tool-schema-profile.ts
index 771c700943..840b72ef2f 100644
--- a/server/tool-schema-profile.ts
+++ b/server/tool-schema-profile.ts
@@ -170,12 +170,25 @@ function valueType(value: unknown): string {
return typeof value === "object" ? "object" : typeof value;
}
+function hasReferencedType(node: Json, root: Json): boolean {
+ const seen = new Set<Json>();
+ let current = node;
+ while (typeof current.$ref === "string" && !seen.has(current)) {
+ seen.add(current);
+ const target = pointer(root, current.$ref);
+ if (!isObject(target)) return false;
+ if ("type" in target) return true;
+ current = target;
+ }
+ return false;
+}
+
/** Kimi Code 2.7.2 tries to infer a single type from each untyped enum, even for a nullable field. */
function withTypedMixedEnums(schema: Json): Json {
const copy = structuredClone(schema);
let changed = false;
walk(copy, 0, (node) => {
- if ("type" in node || !Array.isArray(node.enum)) return;
+ if ("type" in node || !Array.isArray(node.enum) || hasReferencedType(node, copy)) return;
const types = new Set(node.enum.map(valueType));
if (types.has("number")) types.delete("integer");
if (types.size < 2) return;