返回源码地图

packages/ai/test/transform-messages-cross-model-thinking.test.ts

v1.3.0 · 71023aec1546 · 04:完整读取,未本机执行上游suite

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1import { describe, expect, it } from "vitest";
2import { transformMessages } from "../src/api/transform-messages.ts";
3import type { AssistantMessage, Message, Model } from "../src/types.ts";
4
5const kimi: Model<"openai-completions"> = {
6 id: "moonshotai/Kimi-K3",
7 name: "Kimi K3",
8 api: "openai-completions",
9 provider: "sference",
10 baseUrl: "https://api.sference.com/v1",
11 reasoning: true,
12 input: ["text", "image"],
13 cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
14 contextWindow: 1048576,
15 maxTokens: 32768,
16};
17
18function assistant(model: string, content: AssistantMessage["content"]): AssistantMessage {
19 return {
20 role: "assistant",
21 content,
22 api: "openai-completions",
23 provider: "sference",
24 model,
25 usage: {
26 input: 0,
27 output: 0,
28 cacheRead: 0,
29 cacheWrite: 0,
30 totalTokens: 0,
31 cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
32 },
33 stopReason: "toolUse",
34 timestamp: Date.now(),
35 };
36}
37
38describe("thinking from other models", () => {
39 // https://github.com/earendil-works/pi/issues/10995
40 it("does not send another model's readable thinking as text", () => {
41 const messages: Message[] = [
42 { role: "user", content: "Why does curl work but node fails?", timestamp: Date.now() },
43 assistant("zai-org/GLM-5.3", [
44 {
45 type: "thinking",
46 thinking: "The user asks about ECONNRESET. Candidates: proxy, IPv6, TLS.",
47 thinkingSignature: "reasoning_content",
48 },
49 { type: "text", text: "Let me check the TLS versions." },
50 { type: "toolCall", id: "call_1", name: "bash", arguments: { command: "curl -v" } },
51 ]),
52 {
53 role: "toolResult",
54 toolCallId: "call_1",
55 toolName: "bash",
56 content: [{ type: "text", text: "TLSv1.2" }],
57 isError: false,
58 timestamp: Date.now(),
59 },
60 ];
61
62 const result = transformMessages(messages, kimi);
63 const assistantMsg = result.find((m) => m.role === "assistant") as AssistantMessage;
64
65 expect(assistantMsg.content).toEqual([
66 { type: "text", text: "Let me check the TLS versions." },
67 { type: "toolCall", id: "call_1", name: "bash", arguments: { command: "curl -v" } },
68 ]);
69 });
70
71 it("keeps thinking only from the target model when models alternate", () => {
72 const thinking = (text: string) => ({
73 type: "thinking" as const,
74 thinking: text,
75 thinkingSignature: "reasoning_content",
76 });
77 const messages: Message[] = [
78 { role: "user", content: "1", timestamp: Date.now() },
79 { ...assistant("moonshotai/Kimi-K3", [thinking("kimi 1"), { type: "text", text: "a" }]), stopReason: "stop" },
80 { role: "user", content: "2", timestamp: Date.now() },
81 { ...assistant("zai-org/GLM-5.3", [thinking("glm 1"), { type: "text", text: "b" }]), stopReason: "stop" },
82 { role: "user", content: "3", timestamp: Date.now() },
83 { ...assistant("moonshotai/Kimi-K3", [thinking("kimi 2"), { type: "text", text: "c" }]), stopReason: "stop" },
84 { role: "user", content: "4", timestamp: Date.now() },
85 { ...assistant("zai-org/GLM-5.3", [thinking("glm 2"), { type: "text", text: "d" }]), stopReason: "stop" },
86 { role: "user", content: "hi", timestamp: Date.now() },
87 ];
88
89 const result = transformMessages(messages, kimi);
90 const contents = result.filter((m) => m.role === "assistant").map((m) => (m as AssistantMessage).content);
91
92 expect(contents).toEqual([
93 [thinking("kimi 1"), { type: "text", text: "a" }],
94 [{ type: "text", text: "b" }],
95 [thinking("kimi 2"), { type: "text", text: "c" }],
96 [{ type: "text", text: "d" }],
97 ]);
98 });
99});