Repository navigation
feat(coderd/x/chatd): add the view_image tool behind fork-view-image - #308
Conversation
The tool reads a PNG, JPEG, GIF or WebP file from the workspace and returns it to the model as an image tool result. It detects the type by content, checks the per-provider size limit and the 8000 pixel side limit, and fails with the model and provider named when the route accepts no image input. Part of #250.
Codex Review Summary: ✅ CompletedThis 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 |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
# Conflicts: # coderd/apidoc/docs.go # coderd/apidoc/swagger.json # coderd/x/chatd/fork_tools.go # codersdk/fork_experiments.go # docs/reference/api/schemas.md # site/src/api/typesGenerated.ts
@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". |
Merged as e44cdc1 (squash) at 06:38Z. Gates on head c61d4fc:
Not exercised: a real provider run and a browser check. |
Part of #250. Bottom of a three-PR stack that replaces #284:
view_imagetool core and the model check.mike/issue-250-read-images-bound): the bound on viewed images in the prompt.mike/issue-250-read-images-hardening): the structure and decode checks, the per-response call limit and the docs. It closes chatd: let the model read image files from the workspace #250.The tool sits behind
fork-view-image, which is off by default. A gap in this PR, such as no prompt bound and no decode check, is closed by the PRs above it. Enable the experiment only after the whole stack is in.Declined review findings worth doing later are collected in #291.
What
A new fork chat tool,
view_image, behind a newfork-view-imageexperiment. It loads a PNG, JPEG, GIF or WebP file from the workspace into the model's context as an image, so a chat can review its own screenshots.Why a separate tool and not
read_fileread_fileis upstream code (chattool). Teaching it to return images would mean changes inside upstream files. A fork tool lives infork_view_image.go. The only upstream change is the existingappendForkToolshook line inturn_environment.go:743, which now also passes the turn's model (forkToolsWithModel). The tool gets its own experiment.Behavior
chatfiles.DetectMediaTypeon the bytes. A PNG namedshot.txtworks. A text file namedshot.pngfails with... is text/plain, not a PNG, JPEG, GIF or WebP image. Use read_file for text files.chatprovider.Model.AcceptsFilePartMediaType. A route that takes no image type fails withmodel "<model>" on provider "<provider>" does not accept image input. Switch to a model with image input to view images. A route that rejects one type, such as Google and GIF, fails withdoes not accept image/gif images.codersdk.AnthropicInlineImageCapBytes(5 MiB) times 3/4, so the base64 form fits Anthropic's per-image limit.codersdk.MaxChatFileSizeBytes(10 MiB), the limit chatloop already applies to every tool result media.png,jpegandgifDecodeConfig, and a small WebP header parser (VP8, VP8L, VP8X). No new dependency.read_file. Only an all-whitespace path fails as empty.chatprovider.go:155-163). Coder Agents cannot tell a text-only model behind them, so the image goes to the provider anyway. The top PR documents this indocs/fork/pstack-adapter.md.Storage
The result is a normal media tool result. It is stored with
is_media, the data and the MIME type, like other tool results, and is sent again on later turns. The next PR bounds how many stay in the prompt.Access
explorechildren do not getview_image, because their upstream allow-lists do not name it.readonly: truekeeps it. TheReadalias maps toread_fileandview_image.view_imagegets neither:readonlydrops it, and atoolslist keeps it only when it namesview_imageitself.read_file. A caller-suppliedview_imageis not wrapped, so it stays a*dynamicTooland the policy above still drops it.Web UI
No
site/source change. The generic tool card already renders anis_mediaresult as the image (GenericToolRenderer,ToolResultMedia).site/src/api/typesGenerated.tsgains the experiment constant from the generator.Generated files
site/src/api/typesGenerated.tscomes from the generator.coderd/apidoc/docs.go,coderd/apidoc/swagger.jsonanddocs/reference/api/schemas.mdare hand edited to addfork-view-image, the same way feat: add the message_family_chat tool behind fork-family-messages #265 addedfork-family-messages. Running swag reorders the fork enum values (map order) and would churn every fork entry.make genstops at the helm golden target because helm is not installed in this workspace.Size
681 added lines, of which 14 are generated and 432 are tests.
Tests
TestViewImageReachesModel(end to end,chattest.NewAnthropic): the tool is offered, a PNG with a known payload reaches the second model request as an image block with the exact base64, the next turn's request still carries it, and the stored tool message hasis_media, the data and the MIME type.TestViewImageTool: type by content, JPEG, GIF and WebP, a non-image file, the size limit, the side limit, empty, invalid and missing files, an empty path, a path with outer spaces, a model without image input (Copilot route and an unknown provider), and a route that rejects one type.TestViewImageCustomAgentPolicy:readonly, theReadalias and a named entry, each with the built-in and a dynamicview_image.TestViewImageToolGate: the tool only with the experiment on.TestForkWorktreeBindingTools/ViewImageRelativeand/DynamicViewImageUnbound.fork_tools.go,TestViewImageReachesModel,TestViewImageToolGate/Onand/OnWithDynamicToolfail.Commands run on this head, all under
flock ~/.fork-go.lock:go test ./coderd/x/chatd/ -run 'TestViewImage|ForkTools|TestForkWorktreeBinding|AgentToolPolicy|CustomAgent|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool|CompactionModelOverride' -count=1 -v: passgo test ./codersdk -run 'TestExperimentDisplayNames|TestForkExperiments' -count=1: passgolangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/ ./codersdk/: 0 issues. The default config reports no new finding in the changed files.Not verified
make lintand fullmake gendid not run here; fork-ci covers them.fork-view-imagefor the tool to appear, after the whole stack is in.