|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { createOpenAIChatAdapter } from "../src/adapters/openai-chat"; |
| 3 | +import type { OcxContentPart, OcxMessage, OcxParsedRequest, OcxProviderConfig } from "../src/types"; |
| 4 | + |
| 5 | +// Issue #888: role:"tool" content is text-only on chat-completions providers, so images inside a |
| 6 | +// tool result were flattened to an "[image]" marker and vision-capable routed models hallucinated |
| 7 | +// what they never saw. Tool-result images now ride in a follow-up user vision message released when |
| 8 | +// the tool round closes, without splitting the round (strict providers reject interleaved users). |
| 9 | + |
| 10 | +const provider: OcxProviderConfig = { |
| 11 | + adapter: "openai-chat", |
| 12 | + baseUrl: "https://example.test/v1", |
| 13 | + apiKey: "sk-test", |
| 14 | + authMode: "key", |
| 15 | +}; |
| 16 | + |
| 17 | +const IMAGE_URL = "data:image/png;base64,aGVsbG8taW1hZ2UtYnl0ZXM="; |
| 18 | + |
| 19 | +interface ChatPart { |
| 20 | + type: string; |
| 21 | + text?: string; |
| 22 | + image_url?: { url: string; detail?: string }; |
| 23 | +} |
| 24 | + |
| 25 | +interface ChatMsg { |
| 26 | + role: string; |
| 27 | + content?: string | ChatPart[]; |
| 28 | + tool_calls?: { id: string; function: { name: string; arguments: string } }[]; |
| 29 | + tool_call_id?: string; |
| 30 | +} |
| 31 | + |
| 32 | +function wire(messages: OcxMessage[]): ChatMsg[] { |
| 33 | + const parsed: OcxParsedRequest = { |
| 34 | + modelId: "test-model", |
| 35 | + context: { messages }, |
| 36 | + stream: false, |
| 37 | + options: {}, |
| 38 | + }; |
| 39 | + const req = createOpenAIChatAdapter(provider).buildRequest(parsed) as { body: string }; |
| 40 | + return (JSON.parse(req.body) as { messages: ChatMsg[] }).messages; |
| 41 | +} |
| 42 | + |
| 43 | +function user(text: string): OcxMessage { |
| 44 | + return { role: "user", content: text, timestamp: 0 }; |
| 45 | +} |
| 46 | + |
| 47 | +function assistantWithCalls(calls: { id: string; name: string }[]): OcxMessage { |
| 48 | + return { |
| 49 | + role: "assistant", |
| 50 | + content: calls.map(c => ({ type: "toolCall" as const, id: c.id, name: c.name, arguments: {} })), |
| 51 | + timestamp: 0, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function toolResult(callId: string, name: string, content: string | OcxContentPart[]): OcxMessage { |
| 56 | + return { role: "toolResult", toolCallId: callId, toolName: name, content, isError: false, timestamp: 0 }; |
| 57 | +} |
| 58 | + |
| 59 | +/** The carrier is a user message whose parts start with an "[ocx]" text label followed by image_url parts. */ |
| 60 | +function isImageCarrier(msg: ChatMsg): boolean { |
| 61 | + if (msg.role !== "user" || !Array.isArray(msg.content)) return false; |
| 62 | + const [head, ...rest] = msg.content; |
| 63 | + return head?.type === "text" && typeof head.text === "string" && head.text.startsWith("[ocx]") |
| 64 | + && rest.length > 0 && rest.every(p => p.type === "image_url"); |
| 65 | +} |
| 66 | + |
| 67 | +/** Every role:"tool" message must sit in an unbroken block right after its assistant tool_calls message. */ |
| 68 | +function assertRoundsUnbroken(messages: ChatMsg[]): void { |
| 69 | + for (let i = 0; i < messages.length; i++) { |
| 70 | + const m = messages[i]; |
| 71 | + if (m.role !== "tool") continue; |
| 72 | + let j = i - 1; |
| 73 | + while (j >= 0 && messages[j].role === "tool") j--; |
| 74 | + expect(j).toBeGreaterThanOrEqual(0); |
| 75 | + expect(messages[j].role).toBe("assistant"); |
| 76 | + expect((messages[j].tool_calls ?? []).map(tc => tc.id)).toContain(m.tool_call_id); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +test("tool-result images ride a follow-up user message; text, detail, and https URLs survive", () => { |
| 81 | + const messages = wire([ |
| 82 | + user("read the screenshot"), |
| 83 | + assistantWithCalls([{ id: "call_1", name: "Read" }]), |
| 84 | + toolResult("call_1", "Read", [ |
| 85 | + { type: "text", text: "1 match found" }, |
| 86 | + { type: "image", imageUrl: IMAGE_URL, detail: "high" }, |
| 87 | + { type: "image", imageUrl: "https://example.test/shot.png" }, |
| 88 | + ]), |
| 89 | + ]); |
| 90 | + assertRoundsUnbroken(messages); |
| 91 | + const tool = messages.find(m => m.role === "tool")!; |
| 92 | + expect(tool.content).toBe("1 match found[image][image]"); |
| 93 | + const carrier = messages.find(isImageCarrier)!; |
| 94 | + expect(carrier).toBeDefined(); |
| 95 | + expect(messages.indexOf(carrier)).toBe(messages.indexOf(tool) + 1); |
| 96 | + const parts = (carrier.content as ChatPart[]).filter(p => p.type === "image_url"); |
| 97 | + expect(parts.map(p => p.image_url)).toEqual([ |
| 98 | + { url: IMAGE_URL, detail: "high" }, |
| 99 | + { url: "https://example.test/shot.png" }, |
| 100 | + ]); |
| 101 | +}); |
| 102 | + |
| 103 | +test("images from a multi-call round flush once, only after the whole round closes", () => { |
| 104 | + const messages = wire([ |
| 105 | + assistantWithCalls([{ id: "call_1", name: "shot" }, { id: "call_2", name: "list" }]), |
| 106 | + toolResult("call_1", "shot", [{ type: "image", imageUrl: IMAGE_URL }]), |
| 107 | + toolResult("call_2", "list", "file1.txt"), |
| 108 | + ]); |
| 109 | + assertRoundsUnbroken(messages); |
| 110 | + const toolIdx = messages.map((m, i) => (m.role === "tool" ? i : -1)).filter(i => i >= 0); |
| 111 | + expect(toolIdx).toEqual([toolIdx[0], toolIdx[0] + 1]); // nothing interleaves the round |
| 112 | + const carriers = messages.filter(isImageCarrier); |
| 113 | + expect(carriers.length).toBe(1); |
| 114 | + expect(messages.indexOf(carriers[0])).toBe(toolIdx[1] + 1); |
| 115 | +}); |
| 116 | + |
| 117 | +test("orphan tool result with an image still emits the carrier after its synthesized pair", () => { |
| 118 | + const messages = wire([ |
| 119 | + user("hi"), |
| 120 | + toolResult("call_orphan", "shot", [{ type: "image", imageUrl: IMAGE_URL }]), |
| 121 | + ]); |
| 122 | + assertRoundsUnbroken(messages); |
| 123 | + const tool = messages.find(m => m.role === "tool")!; |
| 124 | + expect(tool.content).toBe("[image]"); |
| 125 | + const carrier = messages.find(isImageCarrier)!; |
| 126 | + expect(messages.indexOf(carrier)).toBe(messages.indexOf(tool) + 1); |
| 127 | +}); |
| 128 | + |
| 129 | +test("interrupted round: the synthetic closure still flushes collected images", () => { |
| 130 | + const messages = wire([ |
| 131 | + assistantWithCalls([{ id: "call_1", name: "shot" }, { id: "call_2", name: "list" }]), |
| 132 | + toolResult("call_1", "shot", [{ type: "image", imageUrl: IMAGE_URL }]), |
| 133 | + ]); |
| 134 | + assertRoundsUnbroken(messages); |
| 135 | + const carrier = messages.find(isImageCarrier)!; |
| 136 | + expect(carrier).toBeDefined(); |
| 137 | + expect(messages.indexOf(carrier)).toBe(messages.length - 1); |
| 138 | +}); |
| 139 | + |
| 140 | +test("image-free tool results emit no carrier and an unchanged wire", () => { |
| 141 | + const messages = wire([ |
| 142 | + user("hi"), |
| 143 | + assistantWithCalls([{ id: "call_1", name: "list" }]), |
| 144 | + toolResult("call_1", "list", "file1.txt"), |
| 145 | + user("thanks"), |
| 146 | + ]); |
| 147 | + expect(messages.some(isImageCarrier)).toBe(false); |
| 148 | + expect(messages.map(m => m.role)).toEqual(["user", "assistant", "tool", "user"]); |
| 149 | +}); |
0 commit comments