Skip to content

Commit d5ea5f2

Browse files
authored
fix: parse kimi tagged tool calls (#60051)
* fix: parse kimi tagged tool calls * fix: parse kimi tagged tool calls (#60051) * fix: parse kimi tagged tool calls (#60051)
1 parent 98137f7 commit d5ea5f2

4 files changed

Lines changed: 395 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Docs: https://docs.openclaw.ai
4141
- MS Teams/threading: preserve channel reply threading when proactive fallback reconstructs the conversation ID, so thread replies land in the originating thread instead of the channel root. (#55198) Thanks @hyojin.
4242
- Gateway/lock: detect PID recycling in gateway lock files on Windows and macOS so stale locks from crashed processes no longer block new gateway invocations. (#59843) Thanks @TonyDerek-dot.
4343
- Plugins/Ollama: prefer real cloud auth credentials over the local-only marker so Ollama instances behind authenticated proxies use the configured API key instead of skipping auth.
44+
- Plugins/Kimi Coding: parse tagged Kimi tool-call text into structured tool calls on the provider stream path so tools execute instead of echoing raw markup. (#60051) Thanks @obviyus.
4445

4546
## 2026.4.2
4647

‎extensions/kimi-coding/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
22
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
33
import { applyKimiCodeConfig, KIMI_CODING_MODEL_REF } from "./onboard.js";
44
import { buildKimiCodingProvider } from "./provider-catalog.js";
5+
import { createKimiToolCallMarkupWrapper } from "./stream.js";
56

67
const PLUGIN_ID = "kimi";
78
const PROVIDER_ID = "kimi";
@@ -85,6 +86,7 @@ export default definePluginEntry({
8586
openAiPayloadNormalizationMode: "moonshot-thinking",
8687
preserveAnthropicThinkingSignatures: false,
8788
},
89+
wrapStreamFn: (ctx) => createKimiToolCallMarkupWrapper(ctx.streamFn),
8890
});
8991
},
9092
});
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import type { StreamFn } from "@mariozechner/pi-agent-core";
2+
import type { Context, Model } from "@mariozechner/pi-ai";
3+
import { describe, expect, it } from "vitest";
4+
import { createKimiToolCallMarkupWrapper } from "./stream.js";
5+
6+
type FakeStream = {
7+
result: () => Promise<unknown>;
8+
[Symbol.asyncIterator]: () => AsyncIterator<unknown>;
9+
};
10+
11+
function createFakeStream(params: { events: unknown[]; resultMessage: unknown }): FakeStream {
12+
return {
13+
async result() {
14+
return params.resultMessage;
15+
},
16+
[Symbol.asyncIterator]() {
17+
return (async function* () {
18+
for (const event of params.events) {
19+
yield event;
20+
}
21+
})();
22+
},
23+
};
24+
}
25+
26+
const KIMI_TOOL_TEXT =
27+
' <|tool_calls_section_begin|> <|tool_call_begin|> functions.read:0 <|tool_call_argument_begin|> {"file_path":"./package.json"} <|tool_call_end|> <|tool_calls_section_end|>';
28+
const KIMI_MULTI_TOOL_TEXT =
29+
' <|tool_calls_section_begin|> <|tool_call_begin|> functions.read:0 <|tool_call_argument_begin|> {"file_path":"./package.json"} <|tool_call_end|> <|tool_call_begin|> functions.write:1 <|tool_call_argument_begin|> {"file_path":"./out.txt","content":"done"} <|tool_call_end|> <|tool_calls_section_end|>';
30+
31+
describe("kimi tool-call markup wrapper", () => {
32+
it("converts tagged Kimi tool-call text into structured tool calls", async () => {
33+
const partial = {
34+
role: "assistant",
35+
content: [{ type: "text", text: KIMI_TOOL_TEXT }],
36+
stopReason: "stop",
37+
};
38+
const message = {
39+
role: "assistant",
40+
content: [{ type: "text", text: KIMI_TOOL_TEXT }],
41+
stopReason: "stop",
42+
};
43+
const finalMessage = {
44+
role: "assistant",
45+
content: [
46+
{ type: "thinking", thinking: "Need to read the file first." },
47+
{ type: "text", text: KIMI_TOOL_TEXT },
48+
],
49+
stopReason: "stop",
50+
};
51+
52+
const baseStreamFn: StreamFn = () =>
53+
createFakeStream({
54+
events: [{ type: "message_end", partial, message }],
55+
resultMessage: finalMessage,
56+
}) as ReturnType<StreamFn>;
57+
58+
const wrapped = createKimiToolCallMarkupWrapper(baseStreamFn);
59+
const stream = wrapped(
60+
{ api: "anthropic-messages", provider: "kimi", id: "k2p5" } as Model<"anthropic-messages">,
61+
{ messages: [] } as Context,
62+
{},
63+
) as FakeStream;
64+
65+
const events: unknown[] = [];
66+
for await (const event of stream) {
67+
events.push(event);
68+
}
69+
const result = (await stream.result()) as {
70+
content: unknown[];
71+
stopReason: string;
72+
};
73+
74+
expect(events).toEqual([
75+
{
76+
type: "message_end",
77+
partial: {
78+
role: "assistant",
79+
content: [
80+
{
81+
type: "toolCall",
82+
id: "functions.read:0",
83+
name: "functions.read",
84+
arguments: { file_path: "./package.json" },
85+
},
86+
],
87+
stopReason: "toolUse",
88+
},
89+
message: {
90+
role: "assistant",
91+
content: [
92+
{
93+
type: "toolCall",
94+
id: "functions.read:0",
95+
name: "functions.read",
96+
arguments: { file_path: "./package.json" },
97+
},
98+
],
99+
stopReason: "toolUse",
100+
},
101+
},
102+
]);
103+
expect(result).toEqual({
104+
role: "assistant",
105+
content: [
106+
{ type: "thinking", thinking: "Need to read the file first." },
107+
{
108+
type: "toolCall",
109+
id: "functions.read:0",
110+
name: "functions.read",
111+
arguments: { file_path: "./package.json" },
112+
},
113+
],
114+
stopReason: "toolUse",
115+
});
116+
});
117+
118+
it("leaves normal assistant text unchanged", async () => {
119+
const finalMessage = {
120+
role: "assistant",
121+
content: [{ type: "text", text: "normal response" }],
122+
stopReason: "stop",
123+
};
124+
const baseStreamFn: StreamFn = () =>
125+
createFakeStream({
126+
events: [],
127+
resultMessage: finalMessage,
128+
}) as ReturnType<StreamFn>;
129+
130+
const wrapped = createKimiToolCallMarkupWrapper(baseStreamFn);
131+
const stream = wrapped(
132+
{ api: "anthropic-messages", provider: "kimi", id: "k2p5" } as Model<"anthropic-messages">,
133+
{ messages: [] } as Context,
134+
{},
135+
) as FakeStream;
136+
137+
await expect(stream.result()).resolves.toBe(finalMessage);
138+
});
139+
140+
it("supports async stream functions", async () => {
141+
const finalMessage = {
142+
role: "assistant",
143+
content: [{ type: "text", text: KIMI_TOOL_TEXT }],
144+
stopReason: "stop",
145+
};
146+
const baseStreamFn: StreamFn = async () =>
147+
createFakeStream({
148+
events: [],
149+
resultMessage: finalMessage,
150+
}) as ReturnType<StreamFn>;
151+
152+
const wrapped = createKimiToolCallMarkupWrapper(baseStreamFn);
153+
const stream = (await wrapped(
154+
{ api: "anthropic-messages", provider: "kimi", id: "k2p5" } as Model<"anthropic-messages">,
155+
{ messages: [] } as Context,
156+
{},
157+
)) as FakeStream;
158+
159+
await expect(stream.result()).resolves.toEqual({
160+
role: "assistant",
161+
content: [
162+
{
163+
type: "toolCall",
164+
id: "functions.read:0",
165+
name: "functions.read",
166+
arguments: { file_path: "./package.json" },
167+
},
168+
],
169+
stopReason: "toolUse",
170+
});
171+
});
172+
173+
it("parses multiple tagged tool calls in one section", async () => {
174+
const finalMessage = {
175+
role: "assistant",
176+
content: [{ type: "text", text: KIMI_MULTI_TOOL_TEXT }],
177+
stopReason: "stop",
178+
};
179+
const baseStreamFn: StreamFn = () =>
180+
createFakeStream({
181+
events: [],
182+
resultMessage: finalMessage,
183+
}) as ReturnType<StreamFn>;
184+
185+
const wrapped = createKimiToolCallMarkupWrapper(baseStreamFn);
186+
const stream = wrapped(
187+
{ api: "anthropic-messages", provider: "kimi", id: "k2p5" } as Model<"anthropic-messages">,
188+
{ messages: [] } as Context,
189+
{},
190+
) as FakeStream;
191+
192+
await expect(stream.result()).resolves.toEqual({
193+
role: "assistant",
194+
content: [
195+
{
196+
type: "toolCall",
197+
id: "functions.read:0",
198+
name: "functions.read",
199+
arguments: { file_path: "./package.json" },
200+
},
201+
{
202+
type: "toolCall",
203+
id: "functions.write:1",
204+
name: "functions.write",
205+
arguments: { file_path: "./out.txt", content: "done" },
206+
},
207+
],
208+
stopReason: "toolUse",
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)