Files
edx 68794bacfc trae 0.2.24: a Kimi model's tool schemas get the types Moonshot requires (code 4027) (#462)
Trae passed a request's tools to the model as they were. Moonshot (Kimi) refuses the whole request when a schema node has no type: Trae answers code 4027, "tools.function.parameters is not a valid moonshot flavored json schema … type is not defined". Claude Code's enum-only properties trigger it. For a model whose config, model_name or request model matches /kimi|moonshot/i, nativeTools now deep-clones each tool's parameters and rewrites them with kimi-work's moonshotSchema (ported). An enum or const with no type gets the type its values have, and a $ref beside a type and a type beside an anyOf are written the way Kimi takes them. Other models' tools, and schemas Kimi already accepts, go byte for byte. README note, new moonshot.test.mjs, and 0.2.23 -> 0.2.24.

By edifierx666 (#462).

Reviewed at 4c8dc7401f74fb44615bcb70ee0178337a017951. The only change since is 31c88fe84 (maintainer): the README's Kimi bullet moved after the native-tools bullet's continuation paragraph. The text is unchanged.

Run at 31c88fe84 (macOS, via mote-run, sandbox HOME): bun test packages/trae, 221 pass, 0 fail; bun scripts/check.mjs trae passes. Run at 4c8dc7401 as well: the same 221/0, plus check.
Broken by hand at 4c8dc7401, running moonshot.test.mjs:
- moonshotModel always false: 4 of 6 red.
- Always true: the non-Kimi test red.
- structuredClone dropped: red.
Edge cases run through chatBody, 18 schemas: null, mixed, object and empty enums; nullable; anyOf/oneOf; nested objects; arrays without items; untyped and self $refs; boolean schemas; properties named enum or const. glm-5's parameters are byte-identical in every case. For Kimi, enums and consts only gain a type.
Not tried: a live Trae CN or Trae Global request (the author ran Trae CN live, per the PR).
2026-10-28 22:30:12 +08:00

68 lines
3.4 KiB
JavaScript

// Moonshot (Kimi) refuses a tool schema whose property has no type (Trae code
// 4027, "type is not defined"): the tools sent for a Kimi model are given one.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
import { fakeTrae, signedIn, sse } from "./fake.mjs"
const { chatBody, nativeTools } = _internal
// Claude Code's shape: an enum with no type, in an array's items
const ENUM_ONLY = {
type: "object",
properties: { ops: { type: "array", items: { type: "object", properties: { op: { enum: ["add", "remove"] }, n: { enum: [1, 2] }, f: { enum: [1, 2.5] }, b: { const: true } } } } },
}
const tool = (parameters) => [{ type: "function", function: { name: "edit", description: "d", parameters } }]
const sent = (tools, model = "kimi-k3.1-pro-max") => JSON.parse(chatBody({ model, messages: [], tools }, "chat_v3", "").tools[0].function.parameters)
let f
afterEach(() => f?.close())
test("an enum or const with no type gets the one its values have", () => {
const p = sent(tool(ENUM_ONLY)).properties.ops.items.properties
expect(p.op).toEqual({ type: "string", enum: ["add", "remove"] })
expect(p.n).toEqual({ type: "integer", enum: [1, 2] })
expect(p.f).toEqual({ type: "number", enum: [1, 2.5] })
expect(p.b).toEqual({ type: "boolean", const: true })
})
test("the request's own tools are not changed", () => {
const tools = tool(ENUM_ONLY)
const before = JSON.stringify(tools)
sent(tools)
expect(JSON.stringify(tools)).toBe(before)
})
test("a model that is not Kimi or Moonshot is sent the schema as it is", () => {
for (const model of ["glm-5", "DeepSeek-V4.1-Flash-1017"]) expect(sent(tool(ENUM_ONLY), model)).toEqual(ENUM_ONLY)
expect(sent(tool(ENUM_ONLY), "Kimi-K3")).not.toEqual(ENUM_ONLY)
expect(sent(tool(ENUM_ONLY), "moonshot-v1")).not.toEqual(ENUM_ONLY)
})
test("a schema Kimi takes goes byte for byte", () => {
const ok = { type: "object", properties: { a: { type: "string", enum: ["x"] }, b: { anyOf: [{ type: "string" }, { type: "null" }] } } }
expect(nativeTools(tool(ok), true)[0].function.parameters).toBe(JSON.stringify(ok))
expect(nativeTools(tool(ok), false)[0].function.parameters).toBe(JSON.stringify(ok))
})
test("a $ref beside a type and a type beside an anyOf are written as Kimi holds them", () => {
const p = {
type: "object",
properties: { n: { type: "integer", maximum: 5, $ref: "#/$defs/s0" }, u: { type: "string", anyOf: [{ type: "string" }, { type: "null" }] } },
$defs: { s0: { type: "number" } },
}
const out = sent(tool(p))
expect(out.properties.n).toEqual({ type: "integer", maximum: 5 })
expect(out.properties.u).toEqual({ anyOf: [{ type: "string" }, { type: "null" }] })
})
test("the Kimi request that carried the enum-only tool reaches Trae with a typed schema", async () => {
f = fakeTrae()
f.route("POST /api/agent/v3/llm_utils_chat", () => sse([["output", { response: "ok" }], ["done", {}]]))
const hooks = await TraeCNAuthPlugin({ client: {} })
const opts = await hooks.auth.loader(async () => signedIn())
await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "kimi-k2.6", tools: tool(ENUM_ONLY), messages: [{ role: "user", content: "hi" }] }) })
const op = JSON.parse(f.seen[0].json.tools[0].function.parameters).properties.ops.items.properties.op
expect(op.type).toBe("string")
})