You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit be784f2
Browse filesBrowse the repository at this point in the historyBrowse files
feat(llm-helper): attachments on a single llm.chat call - images and PDF pages named by reference and read by the helper (RFC-0016, step 2)
llm.chat took text only, so a bounded AI node that had to read a page could
not show it to the model. A request may now name files inside
llm.attachments.root: the helper reads them in a worker thread and inside the
call's deadline, decides the kind by the first bytes and sends base64 image
and document blocks before the last turn's text; pages cuts a PDF to the
pages named. One root with the agent tools' workspace rules, the provider's
limits checked before any model call, never on llm.stream or a loop turn.
With the audit of a single call, the record keeps a reference with the source
file's size and SHA-256, never its bytes. The Node twin of
Accenture/mercury-python#82.
The reading is a module of its own, attachments.mjs, run in a worker thread:
a hash, base64 and a PDF's cut are CPU work, and the helper's event loop
carries every other call and stream. A new optional package, @cantoo/pdf-lib
(a devDependency here), cuts pages and tells an encrypted PDF.
The shared file test/vectors/llm-attachments-vectors.json (48 cases) over
eight fixture files, all byte for byte the Python pack's, and seven tests pin
it; thirty-three mutations of the code, the file and a fixture were each
caught by a test.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: extensions/llm-helper/README.md
+121-9Lines changed: 121 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,6 +99,7 @@ token batches as they are produced.
99
99
| `prompt` or `messages` | A single user turn, or conversation turns `[{role: user\|assistant, content}]`. `messages` wins when both are given. Content is plain text. |
100
100
| `system` | Optional system prompt (text). |
101
101
| `schema` | `llm.chat` only. A JSON schema; the reply is then structured output, returned parsed as `data`. `additionalProperties` defaults to `false`. A schema on `llm.stream` is a 400. |
102
+
| `attachments` | `llm.chat` only, and never a loop turn. Files for the model to look at, named by reference: a non-empty list whose entries are a path inside `llm.attachments.root`, or `{path, pages}` for pages of a PDF, such as `13` or `1-3,7`. See Attachments. |
102
103
| `params.model` | The model. Default `claude-opus-5-5` (`llm.model`). On Bedrock's default surface a bare id is sent with the `anthropic.` prefix and an id already spelled for Bedrock as written; on the classic runtime surface every id is sent as written, an inference profile id as the account lists it. |
| `params.timeout_ms` | `llm.chat`: a deadline for the whole call, SDK retries included. `llm.stream`: the idle allowance between events. Default 60000 (`llm.timeout.ms`). |
@@ -158,6 +159,103 @@ the model refused. A stream that ends before its first token fails with a 422 in
158
159
is too small for the question can end with no text at all (seen live with a 120-token budget): that is
159
160
the 422 above, not an empty stream. Raise `params.max_tokens` or lower `params.effort`.
160
161
162
+
## Attachments
163
+
164
+
A bounded AI node sometimes has to read a page, not a text: a scanned form, a table printed as an image, a chart, a
165
+
screenshot, chosen pages of a long PDF. A single `llm.chat` call may name such files in `attachments` (RFC-0016 in
166
+
mercury-composable). **The request names a file and never carries one,** so the event stays small and a file's bytes
167
+
reach no log, trace or stored record.
168
+
169
+
```text
170
+
graph node ── a path ──> llm-helper ── reads the file in its folder ── the file's bytes ──> the model
171
+
```
172
+
173
+
**Who reads the file.** The helper does. The model is a remote service and reads nothing on this host: the node
174
+
passes a path to the helper, the helper reads the file and sends its bytes to the model inside the request, as an
175
+
image or a document block before the text of the last turn. The path never reaches the model, and the file's content
176
+
leaves for the provider, as a prompt's text does.
177
+
178
+
**The shared folder.** `llm.attachments.root` names the one folder a request may read in; unset, every attachment is
179
+
refused. Whatever puts the files there does so before the graph runs: an upload, a copy, a mounted volume, or a
180
+
script that downloads them from an object store (the helper downloading from one itself is a later iteration). A
181
+
node names a file by its path inside the folder, sub-folders included:
182
+
183
+
```
184
+
create node read-page
185
+
with type Module
186
+
with properties
187
+
skill=graph.task
188
+
task=llm.chat
189
+
input[]=text(Return the blocks of the attached page.) -> prompt
190
+
input[]=model.page_schema -> schema
191
+
input[]=model.page_ref -> attachments[0].path
192
+
input[]=model.page_number -> attachments[0].pages
193
+
output[]=result.data -> model.page_reading
194
+
```
195
+
196
+
A mapping whose source is null supplies no parameter, so a node with no `model.page_ref` sends a text-only call that
197
+
still answers: guard the node with a decision on the reference when the page is required.
198
+
199
+
**What the helper does.**
200
+
201
+
- **Stays inside the folder.** A path is relative; the folder is resolved once at start, and a file is read only when
202
+
its resolved path lies inside it, so a path that climbs out and a link that leads out are refused. Hidden entries
203
+
(a name that starts with a dot) are refused, and only a regular file is read. These are the rules of the agent
204
+
tools' workspace (`tools.file.root`).
205
+
- **Decides the kind by the first bytes, never by the name.** PNG, JPEG, GIF or WebP becomes an `image` block; a PDF
206
+
a `document` block, from which the model reads both the text and the page image.
207
+
- **Cuts a PDF to the pages asked for.** With `pages`, the helper writes a new PDF that holds only those pages, in the
208
+
order named, each named once, so a long document costs the pages asked for. This needs the `@cantoo/pdf-lib`
209
+
package (`npm install @cantoo/pdf-lib`). With the package installed a whole document is also checked; without it
210
+
a whole document goes as it is and the provider judges it.
211
+
- **Refuses before any model call** what the provider would refuse, with a status that names the rule, so a caller
212
+
learns its mistake without spending a token.
213
+
- **Reads in a worker thread and inside the call's deadline:** the files, the hashes and a PDF's cut never hold the
214
+
event loop, which carries every other call and stream (`attachments.mjs`, next to the helper), and
215
+
`params.timeout_ms`covers the files and the model.
216
+
217
+
| Case | Status |
218
+
|---|---|
219
+
| `attachments` on `llm.stream`, or on a loop turn; a last turn that is not the user's | 400 |
220
+
| Not a non-empty list; an entry that is neither a path nor `{path, pages}`; `pages` that are malformed, run backward or name a page twice | 400 |
221
+
| More files than `llm.attachments.max.count`, or more bytes of files than `llm.attachments.max.bytes` (taken from each file's size before it is read) | 400 |
222
+
| An absolute path, a path or a link that leads out of the folder, a hidden entry, a missing file, a folder | 400 |
223
+
| A file that is not PNG, JPEG, GIF, WebP or PDF; `pages` on an image; a page the document does not have; an encrypted or unreadable PDF | 400 |
224
+
| An image larger than the backend takes, base64-encoded: 10 MB on the Claude API, 5 MB on Amazon Bedrock | 400 |
225
+
| No `llm.attachments.root` configured | 503 |
226
+
| `pages` without the `@cantoo/pdf-lib` package | 503, naming the package |
227
+
| A configured `llm.attachments.root` that is not a folder | the application stops at start (a request that meets it later is a 500) |
228
+
229
+
**Limits that are the provider's.** The helper sends no more than 20 files by default because a request with more
230
+
than 20 image and document blocks is held to a tighter limit on each image's dimensions. A request takes at most 600
231
+
PDF pages, 100 on a model whose context window is under 1M tokens, and each page costs its text and its image in
232
+
tokens: name `pages`. The helper sets no page limit of its own; a document over the provider's is the provider's 400,
233
+
passed through. Both Bedrock surfaces take these blocks.
234
+
235
+
**What it costs.** The file's content travels in every call that names it - nothing is kept at the provider between
236
+
calls - and it counts as input tokens, by what the model sees and not by the file's size in bytes. An image costs one
237
+
token per 28 by 28 pixel patch, capped by the model's resolution tier: at most 1,568 tokens on the standard tier and
238
+
4,784 on the high-resolution tier of the newer models, so a full-page scan is a few thousand tokens at most whatever
239
+
its megabytes. A PDF page costs its text, typically 1,500 to 3,000 tokens, plus its image, at most 1,568. `pages` is
240
+
therefore the cost control: ten pages named cost ten pages, however long the document.
241
+
242
+
**The folder is readable through the model.** Every caller that can reach this app can have a file of the folder
243
+
sent to the provider and read back in the reply. Choose the folder as deployment configuration, give it only what a
244
+
graph may show a model, and keep it out of reach of anything that is not trusted to write there.
245
+
246
+
**In the audit store.** With the audit of a single call switched on, the turn's `request` keeps each attachment as a
247
+
reference in place of its bytes - `{type: attachment, path, pages, media_type, bytes, sha256}`, the size and the
248
+
SHA-256 of the source file as the helper read it - followed by the turn's text. It is the one block of the audit
249
+
that is not the provider's format: the exchange is reconstructed from the record and the files, by hash. The hash
250
+
tells whether a file is still the one the model saw; it cannot bring back a file that was changed or removed. Where
251
+
an exchange must be reconstructable, keep the files unchanged for as long as the audit's retention
252
+
(`llm.transcript.audit.retention`): write-once names in the folder, or versioned objects once an object store is the
253
+
source. The prompt, the system prompt and the model's reply are in the audit in full, so what the model wrote about a
254
+
file is kept even when the file is not.
255
+
256
+
Not on `llm.stream`, where a reply carries no schema, and not on a loop turn: a transcript keeps every turn, so it
257
+
would have to keep the files, or references whose files may change between turns.
258
+
161
259
## The loop contract
162
260
163
261
`llm.chat`also serves one model turn of an agent loop. The loop is `llm.run`, a function of the Java and Rust
@@ -332,6 +430,9 @@ of the box). The audit does not redact; a redaction policy is the installation's
332
430
| `llm.effort` | unset | The default effort. |
333
431
| `llm.log.batches` | `false` | Log each streamed batch's number, size and arrival time (never its text). |
334
432
| `llm.provider` | `anthropic` | Only `anthropic`; any other value is a 400. |
433
+
| `llm.attachments.root` | unset | The one folder whose files a request may name in `attachments` (see Attachments). Unset, every attachment is a 503. |
434
+
| `llm.attachments.max.count` | `20` | Files per call. |
435
+
| `llm.attachments.max.bytes` | `20971520` | Bytes of files per call (20 MB), counted from the files' sizes before they are read. |
335
436
| `llm.transcript.store` | unset | `memory` or `redis`; a loop turn needs one (see The transcript store). |
336
437
| `llm.transcript.ttl` | `7d` | A loop turn's `ttl` when the request carries none. |
337
438
| `llm.transcript.retention` | `0` | A loop turn's `retention` when the request carries none. |
@@ -367,9 +468,10 @@ section above gives each model's cadence.
367
468
A call logs its model, `stop_reason`, token usage, `request_id` and elapsed time. A prompt, a
368
469
system prompt or a completion never reaches a log. Usage also rides the call's trace record as
0 commit comments