|
| 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