fix: ensure that tool attachments arent sent as user messages (#8944)

This commit is contained in:
Aiden Cline
2026-01-16 10:47:43 -08:00
committed by GitHub
parent 22e3240296
commit 8fd1b92e6e
3 changed files with 89 additions and 43 deletions

View File

@@ -1,8 +1,35 @@
import { describe, expect, test } from "bun:test"
import { MessageV2 } from "../../src/session/message-v2"
import type { ToolSet } from "ai"
const sessionID = "session"
// Mock tool that transforms output to content format with media support
function createMockTools(): ToolSet {
return {
bash: {
description: "mock bash tool",
inputSchema: { type: "object", properties: {} } as any,
toModelOutput(result: { output: string; attachments?: MessageV2.FilePart[] }) {
return {
type: "content" as const,
value: [
{ type: "text" as const, text: result.output },
...(result.attachments?.map((attachment) => {
const base64 = attachment.url.startsWith("data:") ? attachment.url.split(",", 2)[1] : attachment.url
return {
type: "media" as const,
data: base64,
mediaType: attachment.mime,
}
}) ?? []),
],
}
},
},
} as ToolSet
}
function userInfo(id: string): MessageV2.User {
return {
id,
@@ -259,23 +286,11 @@ describe("session.message-v2.toModelMessage", () => {
},
]
expect(MessageV2.toModelMessage(input)).toStrictEqual([
expect(MessageV2.toModelMessage(input, { tools: createMockTools() })).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
},
{
role: "user",
content: [
{ type: "text", text: "Tool bash returned an attachment:" },
{
type: "file",
mediaType: "image/png",
filename: "attachment.png",
data: "https://example.com/attachment.png",
},
],
},
{
role: "assistant",
content: [
@@ -297,7 +312,13 @@ describe("session.message-v2.toModelMessage", () => {
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "text", value: "ok" },
output: {
type: "content",
value: [
{ type: "text", text: "ok" },
{ type: "media", data: "https://example.com/attachment.png", mediaType: "image/png" },
],
},
providerOptions: { openai: { tool: "meta" } },
},
],
@@ -341,7 +362,7 @@ describe("session.message-v2.toModelMessage", () => {
},
]
expect(MessageV2.toModelMessage(input)).toStrictEqual([
expect(MessageV2.toModelMessage(input, { tools: createMockTools() })).toStrictEqual([
{
role: "user",
content: [{ type: "text", text: "run tool" }],
@@ -365,7 +386,10 @@ describe("session.message-v2.toModelMessage", () => {
type: "tool-result",
toolCallId: "call-1",
toolName: "bash",
output: { type: "text", value: "[Old tool result content cleared]" },
output: {
type: "content",
value: [{ type: "text", text: "[Old tool result content cleared]" }],
},
},
],
},