|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { ProviderTransform } from "@/provider/transform" |
| 3 | +import type { ModelMessage } from "ai" |
| 4 | + |
| 5 | +/** |
| 6 | + * A request that declares no tools must not carry tool-call messages: the Altimate gateway |
| 7 | + * rejects that shape, failing every provider in its fallback chain and reporting one generic |
| 8 | + * error (issue #1315). OpenAI and Anthropic both accept it — the defect is gateway-side, and |
| 9 | + * this transform is the client-side mitigation. |
| 10 | + */ |
| 11 | +describe("ProviderTransform.flattenToolParts", () => { |
| 12 | + const history = (): ModelMessage[] => [ |
| 13 | + { role: "user", content: "list the files" }, |
| 14 | + { |
| 15 | + role: "assistant", |
| 16 | + content: [ |
| 17 | + { type: "text", text: "Checking." }, |
| 18 | + { type: "tool-call", toolCallId: "c1", toolName: "bash", input: { cmd: "ls" } }, |
| 19 | + ], |
| 20 | + } as unknown as ModelMessage, |
| 21 | + { |
| 22 | + role: "tool", |
| 23 | + content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { value: "a.txt\nb.txt" } }], |
| 24 | + } as unknown as ModelMessage, |
| 25 | + { role: "user", content: "Summarize the conversation above." }, |
| 26 | + ] |
| 27 | + |
| 28 | + function toolPartTypes(msgs: ModelMessage[]) { |
| 29 | + return msgs.flatMap((m) => (Array.isArray(m.content) ? m.content.map((p: any) => p?.type) : [])) |
| 30 | + } |
| 31 | + |
| 32 | + test("removes every tool-call and tool-result part", () => { |
| 33 | + const out = ProviderTransform.flattenToolParts(history()) |
| 34 | + expect(toolPartTypes(out)).not.toContain("tool-call") |
| 35 | + expect(toolPartTypes(out)).not.toContain("tool-result") |
| 36 | + expect(out.some((m) => m.role === "tool")).toBe(false) |
| 37 | + }) |
| 38 | + |
| 39 | + test("preserves the tool name, arguments and output as readable text", () => { |
| 40 | + const text = JSON.stringify(ProviderTransform.flattenToolParts(history())) |
| 41 | + expect(text).toContain("bash") |
| 42 | + expect(text).toContain("ls") |
| 43 | + expect(text).toContain("a.txt") |
| 44 | + }) |
| 45 | + |
| 46 | + test("keeps surrounding conversation intact", () => { |
| 47 | + const out = ProviderTransform.flattenToolParts(history()) |
| 48 | + expect(out[0]).toEqual({ role: "user", content: "list the files" }) |
| 49 | + expect(out.at(-1)).toEqual({ role: "user", content: "Summarize the conversation above." }) |
| 50 | + }) |
| 51 | + |
| 52 | + test("an assistant turn holding only tool calls does not become an empty message", () => { |
| 53 | + const out = ProviderTransform.flattenToolParts([ |
| 54 | + { role: "user", content: "go" }, |
| 55 | + { |
| 56 | + role: "assistant", |
| 57 | + content: [{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: {} }], |
| 58 | + } as unknown as ModelMessage, |
| 59 | + { role: "user", content: "summarize" }, |
| 60 | + ]) |
| 61 | + for (const msg of out) { |
| 62 | + if (Array.isArray(msg.content)) expect(msg.content.length).toBeGreaterThan(0) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + test("an orphaned tool result (head truncated away its call) still flattens", () => { |
| 67 | + const out = ProviderTransform.flattenToolParts([ |
| 68 | + { role: "user", content: "go" }, |
| 69 | + { |
| 70 | + role: "tool", |
| 71 | + content: [{ type: "tool-result", toolCallId: "gone", toolName: "bash", output: { value: "out" } }], |
| 72 | + } as unknown as ModelMessage, |
| 73 | + { role: "user", content: "summarize" }, |
| 74 | + ]) |
| 75 | + expect(out.some((m) => m.role === "tool")).toBe(false) |
| 76 | + expect(JSON.stringify(out)).toContain("out") |
| 77 | + }) |
| 78 | + |
| 79 | + test("messages with no tool parts are returned unchanged", () => { |
| 80 | + const plain: ModelMessage[] = [ |
| 81 | + { role: "user", content: "hi" }, |
| 82 | + { role: "assistant", content: [{ type: "text", text: "hello" }] } as unknown as ModelMessage, |
| 83 | + ] |
| 84 | + expect(ProviderTransform.flattenToolParts(plain)).toEqual(plain) |
| 85 | + }) |
| 86 | + |
| 87 | + // --- regressions from the consensus review of PR #1319 --- |
| 88 | + |
| 89 | + test("the canonical agentic shape does not leave consecutive assistant messages", () => { |
| 90 | + // user -> assistant(text+call) -> tool(result) -> assistant(text) -> user is the ordinary |
| 91 | + // shape of every session compaction summarizes. Dropping the tool message used to leave two |
| 92 | + // adjacent assistant turns. |
| 93 | + const out = ProviderTransform.flattenToolParts([ |
| 94 | + { role: "user", content: "go" }, |
| 95 | + { |
| 96 | + role: "assistant", |
| 97 | + content: [ |
| 98 | + { type: "text", text: "Checking." }, |
| 99 | + { type: "tool-call", toolCallId: "c1", toolName: "bash", input: { cmd: "ls" } }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { |
| 103 | + role: "tool", |
| 104 | + content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { type: "text", value: "a.txt" } }], |
| 105 | + }, |
| 106 | + { role: "assistant", content: [{ type: "text", text: "I found a.txt." }] }, |
| 107 | + { role: "user", content: "summarize" }, |
| 108 | + ] as unknown as ModelMessage[]) |
| 109 | + |
| 110 | + expect(out.map((m) => m.role)).toEqual(["user", "assistant", "user"]) |
| 111 | + for (let i = 1; i < out.length; i++) expect(out[i].role === out[i - 1].role).toBe(false) |
| 112 | + // nothing is lost in the merge |
| 113 | + const text = JSON.stringify(out) |
| 114 | + expect(text).toContain("Checking.") |
| 115 | + expect(text).toContain("a.txt") |
| 116 | + expect(text).toContain("I found a.txt.") |
| 117 | + }) |
| 118 | + |
| 119 | + test("an intentionally blank assistant text part survives", () => { |
| 120 | + // message-v2.ts preserves a single space between Anthropic signed-reasoning blocks; the |
| 121 | + // blank-part filter must only ever drop parts this transform itself produced. |
| 122 | + const out = ProviderTransform.flattenToolParts([ |
| 123 | + { role: "user", content: "go" }, |
| 124 | + { role: "assistant", content: [{ type: "reasoning", text: "thinking" }, { type: "text", text: " " }] }, |
| 125 | + { role: "user", content: "summarize" }, |
| 126 | + ] as unknown as ModelMessage[]) |
| 127 | + const parts = (out[1] as any).content |
| 128 | + expect(parts.some((p: any) => p.type === "text" && p.text === " ")).toBe(true) |
| 129 | + expect(parts.some((p: any) => p.type === "reasoning")).toBe(true) |
| 130 | + }) |
| 131 | + |
| 132 | + test("a tool-result embedded in an assistant message is flattened too", () => { |
| 133 | + const out = ProviderTransform.flattenToolParts([ |
| 134 | + { role: "user", content: "go" }, |
| 135 | + { |
| 136 | + role: "assistant", |
| 137 | + content: [{ type: "tool-result", toolCallId: "c9", toolName: "bash", output: { type: "text", value: "x" } }], |
| 138 | + }, |
| 139 | + { role: "user", content: "summarize" }, |
| 140 | + ] as unknown as ModelMessage[]) |
| 141 | + expect(JSON.stringify(out)).not.toContain('"tool-result"') |
| 142 | + expect(JSON.stringify(out)).toContain("x") |
| 143 | + }) |
| 144 | + |
| 145 | + test("content-array output is unwrapped into readable text, not JSON", () => { |
| 146 | + const out = ProviderTransform.flattenToolParts([ |
| 147 | + { role: "user", content: "go" }, |
| 148 | + { role: "assistant", content: [{ type: "tool-call", toolCallId: "c1", toolName: "read", input: {} }] }, |
| 149 | + { |
| 150 | + role: "tool", |
| 151 | + content: [ |
| 152 | + { |
| 153 | + type: "tool-result", |
| 154 | + toolCallId: "c1", |
| 155 | + toolName: "read", |
| 156 | + output: { type: "content", value: [{ type: "text", text: "hello world" }] }, |
| 157 | + }, |
| 158 | + ], |
| 159 | + }, |
| 160 | + { role: "user", content: "s" }, |
| 161 | + ] as unknown as ModelMessage[]) |
| 162 | + const rendered = JSON.stringify(out) |
| 163 | + expect(rendered).toContain("hello world") |
| 164 | + expect(rendered).not.toContain('\\"type\\":\\"text\\"') |
| 165 | + }) |
| 166 | + |
| 167 | + test("parallel tool calls and multiple results keep their order", () => { |
| 168 | + const out = ProviderTransform.flattenToolParts([ |
| 169 | + { role: "user", content: "go" }, |
| 170 | + { |
| 171 | + role: "assistant", |
| 172 | + content: [ |
| 173 | + { type: "tool-call", toolCallId: "a", toolName: "first", input: {} }, |
| 174 | + { type: "tool-call", toolCallId: "b", toolName: "second", input: {} }, |
| 175 | + ], |
| 176 | + }, |
| 177 | + { |
| 178 | + role: "tool", |
| 179 | + content: [ |
| 180 | + { type: "tool-result", toolCallId: "a", toolName: "first", output: { type: "text", value: "R1" } }, |
| 181 | + { type: "tool-result", toolCallId: "b", toolName: "second", output: { type: "text", value: "R2" } }, |
| 182 | + ], |
| 183 | + }, |
| 184 | + { role: "user", content: "s" }, |
| 185 | + ] as unknown as ModelMessage[]) |
| 186 | + const flat = (out[1] as any).content.map((p: any) => p.text).join("\n") |
| 187 | + expect(flat.indexOf("first")).toBeLessThan(flat.indexOf("second")) |
| 188 | + expect(flat.indexOf("R1")).toBeLessThan(flat.indexOf("R2")) |
| 189 | + }) |
| 190 | + |
| 191 | + test("output shapes without a value do not throw", () => { |
| 192 | + for (const output of [ |
| 193 | + { type: "error-text", value: "boom" }, |
| 194 | + { type: "json", value: { a: 1 } }, |
| 195 | + { type: "execution-denied" }, |
| 196 | + "plain string", |
| 197 | + ]) { |
| 198 | + const run = () => |
| 199 | + ProviderTransform.flattenToolParts([ |
| 200 | + { role: "user", content: "go" }, |
| 201 | + { |
| 202 | + role: "tool", |
| 203 | + content: [{ type: "tool-result", toolCallId: "c1", toolName: "t", output }], |
| 204 | + }, |
| 205 | + ] as unknown as ModelMessage[]) |
| 206 | + expect(run).not.toThrow() |
| 207 | + } |
| 208 | + }) |
| 209 | + |
| 210 | + test("the caller's input array is never mutated", () => { |
| 211 | + const input = [ |
| 212 | + { role: "user", content: "go" }, |
| 213 | + { role: "assistant", content: [{ type: "tool-call", toolCallId: "c1", toolName: "bash", input: {} }] }, |
| 214 | + { role: "tool", content: [{ type: "tool-result", toolCallId: "c1", toolName: "bash", output: { type: "text", value: "o" } }] }, |
| 215 | + ] as unknown as ModelMessage[] |
| 216 | + const before = JSON.stringify(input) |
| 217 | + ProviderTransform.flattenToolParts(input) |
| 218 | + expect(JSON.stringify(input)).toEqual(before) |
| 219 | + }) |
| 220 | +}) |
0 commit comments