返回 CodeWhale
READ_MEDIA.md
根目录 / docs / READ_MEDIA.md
1 # `read_media` Operator Guide
2
3 `read_media` is a safe, first-class image reading and preprocessing tool for Codewhale. It allows vision-capable coding models to inspect visual assets (diagrams, UI mockups, screenshots, rendered graphs) with strict security, memory bounds, and privacy guards.
4
5 ---
6
7 ## 1. Overview and Scope
8
9 - **Supported Formats:** PNG, JPEG, GIF, WebP.
10 - **Out of Scope:** Video files, audio streams, and background/automatic screenshot watching are deliberately excluded.
11 - **Provider-Neutral Wiring:** Decoded and normalized images are converted into native image parts across all supported providers (OpenAI Chat Completions, Anthropic Messages, and OpenAI Responses API).
12
13 ### Show the agent a screenshot
14
15 - Paste a clipboard image into the composer with `Ctrl+V`, or run
16 `/attach <path>` for an existing PNG, JPEG, GIF, or WebP.
17 - Drag an image file onto the terminal, or paste its absolute path (plain,
18 quoted, shell-escaped, or as a `file://` URL), and it attaches the same way.
19 A paste becomes an attachment only when every path in it names an existing
20 image; otherwise it stays text. A text-only model gets a notice instead of
21 the image.
22 - Every attached image is drawn above the composer as a truecolor cell
23 thumbnail, laid out left to right and sharing the row, with the file name
24 (one image) or the count (several) under it. The
25 picture is ordinary cell output (`▀` half blocks, top pixel as foreground,
26 bottom as background), not a graphics protocol, so it renders on Windows
27 Terminal, iTerm2, Terminal.app, GNOME Terminal, Konsole and WezTerm alike.
28 A terminal that reports only 256 or 16 colors - Terminal.app among them -
29 receives a downsampled palette.
30 It is skipped in ASCII-safe mode, and it costs no rows while the decode is
31 still running.
32 - Temporary macOS `NSIRD_screencaptureui` paths are copied into Codewhale's
33 stable attachment store when ingested.
34 - Ask the agent to inspect the screenshot. The image is sent only as part of
35 that explicit turn action; merely having a screenshot path or artifact does
36 not trigger background analysis.
37
38 ### Pasted image storage
39
40 A pasted clipboard image is saved as `clipboard-<number>.png` under
41 `~/.codewhale/clipboard-images/` (or `<workspace>/clipboard-images/` when no
42 home directory is available), and the attachment names that absolute path.
43 The image bytes enter the request when the message is built, so a later
44 missing file never fails a resume; editing and resending an old message
45 reattaches from the path and reports a missing image instead of sending it.
46
47 Nothing deletes these files on its own. To reclaim space:
48
49 ```sh
50 codewhale sessions prune-images # report only
51 codewhale sessions prune-images --apply # delete what the report lists
52 codewhale sessions prune-images --older-than-days 30
53 ```
54
55 The command lists only images that no file under the Codewhale state
56 directory, the sessions directory or the Runtime task store still names
57 (saved sessions, checkpoints, archives, Runtime threads, the composer stash
58 and history) and that are older than the grace window (7 days by default),
59 so a draft or a running turn keeps its picture. If any file there cannot be
60 read, it reports what it found and deletes nothing. A file another process
61 still holds open (common on Windows) is reported and skipped. Close open
62 Codewhale sessions before `--apply`.
63
64 ### How `Ctrl+V` reaches Codewhale
65
66 Most terminals own the paste chord, and Windows Terminal turns `Ctrl+V` into a
67 paste of the clipboard's *text*. When the clipboard holds only a picture there
68 is no text — but Windows Terminal still sends an **empty** bracketed paste.
69 That is deliberate (`// LOAD BEARING: Send an empty bracketed paste even if the
70 clipboard was empty` in its paste handler): it is how an application learns
71 that the person pasted even when the clipboard carried nothing the terminal
72 could read. Codewhale treats an empty paste as "ask the clipboard yourself"
73 and attaches the picture.
74
75 If a paste does nothing at all:
76
77 - The terminal may not honor bracketed paste. Codewhale enables it at startup,
78 but a terminal that ignores the request never sends the event.
79 - In an SSH session with no forwarded graphical display the remote host cannot
80 read your clipboard; paste stays terminal-owned and delivers text only.
81
82 `read_media` is the corresponding agent-side path for inspecting another
83 image later in the task without requiring the operator to attach it again.
84 The everyday `read` tool also returns a PNG, JPEG, GIF, or WebP as image
85 content (downscaled when over the inline limit). Both may open an image the
86 user attached from outside the workspace — that exact file, not its
87 directory.
88
89 ---
90
91 ## 2. Activation and Catalog Policy
92
93 - **Default-Off / Deferred Loading:** To preserve model context budgets, `read_media` is registered as a deferred tool (`defer_loading = true`) rather than occupying active slots in the default core catalog.
94 - **Explicit Model Invocation:** The tool is invoked explicitly by name when the model or user requests media inspection.
95 - **Eager Configuration:** Operators who want `read_media` always pre-loaded in the model tool catalog can configure:
96
97 ```toml
98 [tools]
99 always_load = ["read_media"]
100 ```
101
102 ---
103
104 ## 3. Tool Parameters and Schema
105
106 ```json
107 {
108 "path": "docs/architecture.png",
109 "crop": {
110 "x": 100,
111 "y": 50,
112 "width": 800,
113 "height": 600
114 },
115 "detail": "auto"
116 }
117 ```
118
119 | Parameter | Type | Required | Description |
120 |-----------|------|----------|-------------|
121 | `path` | string | **Yes** | Workspace-relative or trusted external path to the image file. |
122 | `crop` | object | No | Optional pixel bounding box `{ "x": u32, "y": u32, "width": u32, "height": u32 }` (0-indexed). |
123 | `detail` | string | No | Resolution target: `"auto"` (max 2048px, default), `"low"` (max 1024px), `"high"` / `"original"` (up to 4096px). |
124
125 ---
126
127 ## 4. Safety, Privacy, and Guardrails
128
129 ### 4.1. Workspace Boundary and Credential Protection
130 - **Workspace Containment:** Paths must resolve within the workspace or user-approved trusted external paths (`/trust`). Symlink escapes outside trusted roots are rejected.
131 - **Credential Protection:** Codewhale configuration (`config.toml`, `.codewhale/`, `.deepseek/`, secrets directory) cannot be read via `read_media` and will fail with `PermissionDenied`.
132
133 ### 4.2. Decompression-Bomb and Memory Limits
134 - **Source Byte Limit:** Maximum source file size before decoding is **20 MiB** (`MAX_SOURCE_IMAGE_BYTES`). Oversized files are rejected before allocation.
135 - **Dimension Guards:** Maximum permitted image width and height is **8192 px** (`MAX_IMAGE_DIMENSION`).
136 - **Pixel Budget:** Maximum total pixel count is **33,554,432 pixels** (~33.5 megapixels, `MAX_IMAGE_PIXELS`).
137 - **Memory Ceiling:** Safe memory allocation during decode is capped at **64 MiB** (`MAX_DECODE_ALLOC_BYTES`).
138 - **Wire Payload Limit:** Re-encoded image payload is capped at **5 MiB** (`MAX_WIRE_IMAGE_BYTES`), matching provider constraints.
139
140 ### 4.3. Active Route Vision Checks
141 - Before reading an image, `read_media` inspects `context.route_capabilities.image_input`.
142 - If the active model route explicitly lacks vision support (`CapabilityState::Unsupported`), the tool returns an actionable error directing the operator to switch to a vision model:
143
144 ```text
145 read_media: the active model route does not support image input. Switch to a route marked vision-capable with /model, or configure the route's image_input capability, then try again.
146 ```
147
148 Only a known `Unsupported` capability blocks the explicit tool call. An
149 `Unknown` capability is admitted deliberately, matching normal attachment
150 routing: custom and self-hosted providers often do not publish modality
151 metadata, so their provider response remains authoritative. Operators who
152 need a fail-closed route can set its `image_input` capability explicitly.
153
154 ---
155
156 ## 5. Typed Receipts and Wire Integration
157
158 Each successful execution yields:
159 1. **Human-Readable Receipt (`content`):** Summarizes original format, source and final dimensions, crop details, and byte sizes.
160 2. **Typed JSON Metadata (`metadata`):** Contains structured dimension, crop, and byte information without exposing credentials or internal tokens.
161 3. **Rich Image Content Block (`content_blocks`):** Attaches a standardized `ToolResultContentBlock::Image` which provider adapters wire into outbound requests:
162 - **OpenAI Chat Completions:** `image_url` block in following user message.
163 - **Anthropic Messages:** `image` base64 source inside `tool_result` block.
164 - **OpenAI Responses:** `input_image` block inside function call output.
165
165 lines MARKDOWN