Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/ai/src/protocols/anthropic-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,25 +820,23 @@ const lowerToolResultContentItem = Effect.fnUntraced(function* (item: Tool.Conte

const lowerToolResultContent = Effect.fnUntraced(function* (part: ToolResultPart, request: LLMRequest) {
const rich = ProviderShared.toolErrorContent(part)
// Text / json / error results stay as a string for backward compatibility
// with existing cassettes and provider expectations.
if (part.result.type !== "content" && rich === undefined) return ProviderShared.toolResultText(part)
// Preserve the narrowed array element type when compiled through a consumer package.
const content: ReadonlyArray<Tool.Content> = part.result.type === "content" ? part.result.value : rich!
// An is_error tool_result may contain only text. Image and document blocks in that content are
// rejected, so replace them with a note instead of lowering media.
if (part.result.type === "error") return lowerToolErrorContent(part, content)
return yield* Effect.forEach(content, (item) => lowerToolResultContentItem(item, request))
// An is_error tool_result may contain only text. Blank text is rejected, and image or document
// blocks are replaced with a note instead of lowering media.
if (part.result.type === "error") return lowerToolErrorContent(part, rich ?? [])
// Text and json results stay as a string for backward compatibility with existing cassettes.
if (part.result.type !== "content") return ProviderShared.toolResultText(part)
return yield* Effect.forEach(part.result.value, (item) => lowerToolResultContentItem(item, request))
})

const lowerToolErrorContent = (part: ToolResultPart, content: ReadonlyArray<Tool.Content>) => [
{ type: "text" as const, text: ProviderShared.toolResultText(part) },
...content.map((item) =>
item.type === "text"
? { type: "text" as const, text: item.text }
: { type: "text" as const, text: omittedToolErrorNote(item) },
),
]
const lowerToolErrorContent = (part: ToolResultPart, content: ReadonlyArray<Tool.Content>) => {
const blocks = [ProviderShared.toolResultText(part), ...content.map(toolErrorItemText)].filter(
(text) => text.trim().length > 0,
)
const text = blocks.length > 0 ? blocks : ["[tool error]"]
return text.map((item) => ({ type: "text" as const, text: item }))
}

const toolErrorItemText = (item: Tool.Content) => (item.type === "text" ? item.text : omittedToolErrorNote(item))

const omittedToolErrorNote = (item: Tool.FileContent) => {
if (item.mime.startsWith("image/")) return "[image omitted]"
Expand Down
64 changes: 64 additions & 0 deletions packages/ai/test/provider/anthropic-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,70 @@ describe("Anthropic Messages route", () => {
}),
)

const compileErrorToolResult = (
value: unknown,
content?: ReadonlyArray<{ type: "text"; text: string } | { type: "file"; uri: string; mime: string }>,
) =>
compileRequest(
LLM.request({
model,
cache: "none",
messages: [
Message.assistant([ToolCallPart.make({ id: "call_1", name: "snapshot", input: {} })]),
Message.tool({
id: "call_1",
name: "snapshot",
result: { type: "error", value, ...(content === undefined ? {} : { content }) },
}),
],
}),
)

const expectNoEmptyText = (content: AnthropicToolResult["content"]) => {
expect(Array.isArray(content)).toBe(true)
if (!Array.isArray(content)) return
expect(content.length).toBeGreaterThan(0)
for (const block of content) {
expect(block.type).toBe("text")
if (block.type === "text") expect(block.text.trim().length).toBeGreaterThan(0)
}
}

it.effect("drops an empty text block from an empty-string error", () =>
Effect.gen(function* () {
const prepared = yield* compileErrorToolResult("", [{ type: "text", text: "" }])
const result = expectToolResult(prepared.body)
expect(result.is_error).toBe(true)
expect(result.content).toEqual([{ type: "text", text: "(no tool output)" }])
expectNoEmptyText(result.content)
}),
)

it.effect("keeps an omission note when an error has only media", () =>
Effect.gen(function* () {
const prepared = yield* compileErrorToolResult("", [
{ type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png" },
])
const result = expectToolResult(prepared.body)
expect(result.is_error).toBe(true)
expect(result.content).toEqual([
{ type: "text", text: "(no tool output)" },
{ type: "text", text: "[image omitted]" },
])
expectNoEmptyText(result.content)
}),
)

it.effect("drops whitespace-only error text", () =>
Effect.gen(function* () {
const prepared = yield* compileErrorToolResult(" \n", [{ type: "text", text: "\t " }])
const result = expectToolResult(prepared.body)
expect(result.is_error).toBe(true)
expect(result.content).toEqual([{ type: "text", text: "[tool error]" }])
expectNoEmptyText(result.content)
}),
)

const video = { type: "video", source: { type: "base64", media_type: "video/mp4", data: "AAECAw==" } }
const videoFile = { type: "file" as const, uri: "data:video/mp4;base64,AAECAw==", mime: "video/mp4" }
const customModel = AnthropicMessages.route.with({ provider: "custom" }).model({ id: "video-model" })
Expand Down
Loading