Repository navigation
feat: convert Anthropic tool result media like user files - #73
Conversation
2a6d7dc to
e7fe298
Compare
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
e7fe298 to
574b1ff
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
ToolResultOutputContentMedia and ToolResponse gain an optional Filename, and the agent copies it into the media tool result. FilePart() decodes the base64 data so providers can convert tool-result media with the same rules they use for user files.
Tool-result media always became an image block, so a PDF from a tool got a 400 from the API. Tool media now goes through the same file converter as user files: a PDF becomes a document block titled with the filename, text becomes a text document, and images stay images. Blocks inside tool_result get no cache_control; the breakpoint stays on tool_result. Media that cannot be converted becomes a text note plus upstream's warning, "tool result media type %s not supported, sending text placeholder only". The note uses "file" when the media type is empty.
Tool-result media data is already base64, but FilePart() decoded it and the Anthropic converter encoded it again. For a 4 MiB image this costs about 4 ms and 15 MB of extra allocation per request. providers/internal/filepart.File now keeps the content in the encoding of its source: raw bytes for user file parts, base64 for tool-result media. Images and PDFs send the base64 unchanged. Only text documents decode it. FilePart() is removed. Invalid base64 in an image or PDF now goes to the API unchanged, the same as upstream. Invalid base64 in text media still becomes a text notice and a warning.
3e303b9 to
0a6f9c5
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430). This is the first of four stacked PRs. Today a PDF in an MCP tool result reaches no provider. On Anthropic we strip it in chatd. On OpenAI Chat Completions fantasy swaps it for a "cannot be displayed" note, and on Responses it sends a placeholder that points at a user message it never sends. coder/fantasy#73 and coder/fantasy#74 make fantasy convert tool result media with the converter it already uses for user files: an Anthropic document inside `tool_result`, an `input_file` inside `function_call_output` on Responses, and a user file part straight after the tool message on Chat Completions. Both have merged, and this PR pins `charm.land/fantasy` to the coder_2_33 tip that has them. Chat Completions file parts now refuse text files, as Responses already did, so `AcceptsFilePartMediaType` follows. The converters take a filename, so an MCP embedded resource blob is now named after the last element of its URI path, and we store the name with the tool result so later turns send the same one. OpenAI picks the file type from the extension, so we append the right one when it's missing or wrong. We drop names with control characters, as Postgres `jsonb` rejects NUL, and we redact the name like the rest of the tool response, since a URI like `https://host/sse/<token>` would otherwise put the token in the chat history. There's also a test proving aibridge's Chat Completions re-encode keeps a user file part, as that's where these PDFs end up. Anthropic still strips tool result PDFs until the next PR in the stack.
Closes CODAGT-1425 Stacked on #31386. coder/fantasy#78, which is already on `main`, fixed the searches with no reasoning before them by sending them inline without their `id`. Stored chats still send a search as an `item_reference` when a reasoning item came right before it, though. The API pairs any stored ID with that reasoning item and rejects the whole request if it's missing, and a reference doesn't save any tokens over an inline item anyway. This bumps fantasy to pick up coder/fantasy#77, which just never references a search, so stored chats now replay earlier searches inline, like unstored ones. It also drops the `id` from replayed computer calls, which have the same problem. I've had to stack this on the CODAGT-1430 PRs, as coder/fantasy#77 merged after coder/fantasy#73 and coder/fantasy#74, so any pin that has it has them too. `TestOpenAIResponsesNoStaleWebSearchReplay` now covers a stored chat, and I deleted `TestOpenAIResponsesFullReplayPairsReasoningAndWebSearch`, as it asserted the order of references we no longer send.
Tool result media had no filename, and each provider converted it with its own code rather than the code it already uses for user file attachments. Those copies drifted apart. On Anthropic, tool media always became an
imageblock, so a PDF from an MCP tool got a 400, which is why coder/coder currently swaps PDF tool results for a note before they reach the provider.This adds an optional
FilenametoToolResultOutputContentMediaandToolResponse, and an internalfilepart.Filetype that holds either a user file's bytes or tool media's base64, so a provider can run tool media through its user file converter without decoding it first. Stored results without a filename still decode as before.On Anthropic, we'll now do just that. A PDF becomes a
documentblock inside thetool_result, titled with the filename,text/*becomes a text document, and images stay images. The nested blocks don't getcache_control, as the breakpoint stays on thetool_resultand Anthropic only allows four. Anything we can't convert becomes a short note saying the content can't be displayed, plus upstream's warning, rather than an invalid block.I sent a PDF tool result to Claude Haiku through our AI gateway with this stack, and it read the PDF fine. I've since run it through chatd on a local coderd, where PDF and PNG results from an MCP server (and from a workspace agent's MCP server) reached Claude as
documentandimageblocks. That included a turn where Claude thought before calling the tools, so the signed thinking block was replayed next to the PDFtool_result, and Anthropic accepted it. #74 does the same for OpenAI.Part of CODAGT-1430