Move files between tools
Capture file bytes from tool results once and let the agent move them by fileId to upload URLs, download links, and the sandbox.
Agents often need to take a file from one tool and give it to another: an email attachment to an
upload URL, a sandbox report to an email, an MCP export to cloud storage. Yolk does this with a
claim check (ADR 0009):
tool resolution stores the bytes once, the model and code mode scripts only see a short fileId,
and file tools turn the fileId back into bytes or a link where it is needed. Bytes never pass
through the model, scripts, the tool ledger, or the transcript.
Install
pnpm add @yolk-sdk/files@canary @yolk-sdk/agent@canary effect@4.2.0Available from 0.1.0-canary.114. The package is runtime-neutral (Web Crypto, no Node builtins).
How it works
- A typed tool returns bytes in a file field of its output: declare
outputwith aToolFilefield and returnnew FileBytes({ bytes, name, mimeType })(both from@yolk-sdk/agent/protocol;view: truealso shows the model an image or document). Tools withoutoutputmay return aFilePart(filePartFromBytes) in their content. Connector actions withfileOutput(gmail.get_attachment,outlook.get_attachment,email.get_attachment,fortnox.get_sie_file) and MCP blobs, images and audio already do. resolveTools(modules, context, { files: { store } })stores the bytes before the ledger sees the result. The value holds aFileRef { fileId, name, mimeType, size }where the bytes were, the model sees a label such as[file file_…: Faktura_225.pdf, application/pdf, 89 KB], andToolResult.fileslists the descriptors.- The agent passes the
fileIdto a tool that needs the file.
Files the host already stored
A tool that wrote the file to the store itself (for example a connector download streamed straight
to object storage, so the host never holds the whole file) returns
new FileStored({ fileId, name, mimeType, size }) in the ToolFile field instead of the bytes; a
FileDescriptor from its own write constructs it directly. Capture never stores or reads it: it
checks the file with store.head(fileId) and answers the same FileRef, label and
ToolResult.files entry as FileBytes with the same content would get. A file the store's scope
does not hold (never written, a malformed id, or a fileId of another scope) answers
FileUnavailable not found, one stored with another size size mismatch, never a reference to a
missing file. Write the object where the store's head finds it: with makeObjectStorageFileStore,
the key <prefix>/<scope>/<fileId> (prefix defaults to files; fileId from fileIdFor with
the content's sha256) and the URI-encoded name and the hex sha256 in its metadata, as put
writes them. The fileId depends on the sha256 of the whole content, so a streaming host writes to
a temporary key while hashing, then copies the object to its final key. A FileStored cannot be
viewed by the model; return FileBytes with view: true for that.
Without files, over maxFileBytes (default 100 MiB, also for a FileStored), or when storing
fails, a file field becomes FileUnavailable { unavailable: reason, name, mimeType } and the model
sees a [file not captured (reason): …] label: base64 never falls through to the model. Inline
images, documents and audio the model views keep their source and also get a stored copy and a
label.
Store
Fragment; your app supplies r2ObjectStorage, workspaceId and sessionId:
import { makeObjectStorageFileStore } from '@yolk-sdk/files'
const store = makeObjectStorageFileStore({
storage: r2ObjectStorage, // your ObjectStorageApi: put, head, get, signGet, deletePrefix
scope: `${workspaceId}/${sessionId}` // host-derived, never from the model
})Keys are files/<scope>/<fileId>, with the name and sha256 in object metadata: no database. The
fileId is derived from the scope and the content hash, so replays and duplicates write nothing,
and a fileId from another scope never resolves. deleteScopeFiles({ storage, scope }) removes a
scope's files, for example with its session. Scope segments are /-separated and use only
A-Z a-z 0-9 . _ - (no . or .. segments); the store throws for an invalid scope, so check
untrusted values with isValidFileScope.
Tools
Fragment; your app supplies modules, context, the transport and the sandbox:
import { Effect } from 'effect'
import { resolveTools } from '@yolk-sdk/agent/tools'
import { makeFileToolModule } from '@yolk-sdk/files'
const toolSet = Effect.gen(function* () {
return yield* resolveTools(
[...modules, makeFileToolModule({ store, transport: guardedUploadTransport, sandbox })],
context,
{ files: { store } }
)
})| Tool | Input | Does |
|---|---|---|
file_upload | { fileId, url, headers? } | PUTs the file to an https URL, for example a presigned upload URL |
file_url | { fileId, ttlMinutes? } | Signs a temporary download link (default one hour, at most seven days) |
file_save | { name, content, mimeType? } | Saves text (JSON, CSV, Markdown, plain text) as a file |
file_to_sandbox | { fileId, path? } | Copies the file into the sandbox workspace (default files/<fileId>/<name>) |
file_from_sandbox | { path, name?, mimeType? } | Turns a workspace file into a fileId |
All five are write tools without approval. file_save stores text the model or a script
produced as UTF-8 (MIME type from the name's extension, else text/plain; content over
maxTransferBytes is invalid_input); the same content returns the same fileId. file_upload
exists only with a FileUploadTransportApi, and the sandbox tools only with a sandbox that has writeFile/readFile
(the @yolk-sdk/sandbox Vercel layer has them). file_upload results and errors name the
destination host, never the URL. A network failure during an upload is an unknown outcome: the
agent is told to check the destination before retrying.
makeFileToolModule also takes maxTransferBytes (default 50 MiB: file_upload and the sandbox
tools read the whole file into memory, so a captured file larger than this cannot be transferred,
and file_save stores at most this many bytes), defaultUrlTtlSeconds (default one hour, clamped to one minute to seven days) and isEnabled.
file_upload streams instead when your store has the optional FileStoreApi.stream and your
transport the optional putStream: the file goes out as the store reads it (content-length its
size), never held in memory, so maxTransferBytes does not bound it. A read that breaks midway fails
the call as cut short, told apart from the transport's unknown outcome. makeObjectStorageFileStore
has stream when your ObjectStorageApi has the optional getStream (the object's bytes as a
scoped stream, for example the body of an R2 or S3 GET, cancelled when the scope closes); each
stream is held to the stored size.
A script on the Dynamic Worker executor sends a long file_save content (64 Ki characters or more)
as a stream beside the call, never as one JSON string your Worker holds (streamedArgument, ADR
0022): the call's arguments, its ledger entry and its nestedCalls record hold a placeholder.
file_save stores it through your store's optional FileStoreApi.putStream (the same fileId
put answers for the same bytes) when it has one, a chunk held at a time and no bound but your
store's: implementing putStream opts that path out of maxTransferBytes, so your store (and the
script's own memory and CPU limit) bound it. Otherwise it reads the content whole within
maxTransferBytes. A content cut short (the
script hit its CPU limit) saves nothing.
From a code mode script
Scripts read file fields as FileRef (or FileUnavailable), never labels, so one script moves a
Gmail attachment to an MCP server's presigned upload URL (script fragment; the IDs come from earlier
calls; get_attachment and MCP tools resolve to { value, files }):
const attachment = await tools['gmail.get_attachment']({ messageId, attachmentId, filename })
const file = attachment.files[0]
if ('unavailable' in file) return `not stored: ${file.unavailable}`
const upload = await tools.mcp_afloat_create_receipt_upload({
contentLength: file.size,
contentType: file.mimeType
})
await tools.file_upload({
fileId: file.fileId,
url: upload.value.url,
headers: upload.value.headers
})A script that computed data saves it as a file for a tool that takes fileIds, instead of the
model copying the data into the next call (file_save resolves to { fileId, name, mimeType, size }):
const saved = await tools.file_save({ name: 'totals.json', content: JSON.stringify(totals) })
return saved.fileIdYour app owns
- the
ObjectStorageApiover private storage and the scope (give a subagent a store bound to the same scope to share files); - which surfaces get
filesand the file module (for example not automatic continuations); - a guarded
FileUploadTransportApi: public addresses only (checked on the resolved IP), TLS verified, no redirects, no cookies or ambient credentials, no logging of the URL. An unguarded transport allows SSRF; - claiming and ledgering the
writetools, retention, and download UI.
Connector sinks such as Gmail draft attachments do not take fileIds yet; attach files through
your own tools until they do.