Skip to content

File readers for agents (ctx.agents.reader)

An init file (or omfp_eval) declares a reader that agents’ read uses for the files whose names end with its extensions, instead of reading their bytes as text:

ctx.agents.reader({
name: "csv",
description: "a CSV file as a Markdown table; `<file>:<column>` one column.",
extensions: [".csv", ".tsv"],
async read(path, { selector, signal }) {
const text = await Bun.file(path).text();
return { text: toTable(text, selector), contentType: "text/markdown" };
},
});
Field What it is
name 1–64 lowercase letters, digits, _, -. Declaring the same name again shadows the earlier reader (the flavor’s archive, sqlite, json, pdf, notebook included) until the later one goes.
description What read’s description tells the model after the reader’s extensions: what it shows and the selectors it takes.
extensions The name endings it reads, each with its dot (.zip, .tar.gz), any case.
read(path, call) Reads one file: path is absolute; call = { selector?, offset?, limit?, threadId, folders, signal }. Returns a string (Markdown) or { text, contentType?, paged? } (text/markdown, application/json, text/plain); a throw fails the call with its message.

read sends a path to a reader when it is <file> or <file>:<selector>, where <file> is an existing file (relative to the thread’s folder, absolute, ~/… or local://…) whose name ends with a declared extension. The selector is everything after the colon that ends the file, so it may hold colons, spaces and quotes; each reader gives it its own meaning, which its description states:

Path Read
dist.zip the archive’s members
dist.zip:src/main.ts one member
shop.sqlite:users a table’s rows
shop.sqlite:SELECT name FROM users WHERE id < 10 a query’s rows
package.json:.dependencies | keys a jq filter’s output
report.pdf:2-5 some pages
analysis.ipynb:3 one cell

A path whose file does not exist, whose extension no reader declares, or that names an existing file as a whole (a file literally named c.zip:odd) reads as pi always does; so does any path with raw: true (read takes raw once the workspace has readers): read({ path: "data.json", raw: true }) is the file’s own text. When two readers declare the same extension, the first by name reads it; the longest matching extension wins (.tar.gz over .gz).

The core pages the answer by lines (offset, limit) like omfp://. A reader that pages by itself (rows of a table) reads call.offset (1-based) and call.limit and answers paged: true: its text is then shown as it is, so it says how to read on. Text longer than 4 MB is cut with a note. The code runs in the workspace’s host; aborting the agent’s call aborts signal, and so does removing the reader. Reads change nothing, so none asks for approval.

A thread reads its workspace’s readers when its session starts: a reader declared or removed later reaches a running thread once it is idle, by restarting its session.

@omfp/flavor (omfp://docs/flavor) declares, in each workspace, under its readers part:

  • archive: .zip, .jar, .whl, .tar, .tar.gz, .tgz, .tar.zst, .tzst. The file lists the members (size, date, path); <archive>:<member> reads one as text (a binary member is described, not shown); <archive>:<folder>/ lists a folder.
  • sqlite: .sqlite, .sqlite3, .db, .db3, opened read-only. The file shows the schema; <db>:<table> a table’s or view’s rows and <db>:<query> the rows of one SELECT, WITH or VALUES query, a Markdown table of limit rows (100) from row offset. Any other statement is refused; a query over its time is stopped.
  • json: .json, .jsonl, .ndjson, through jq. The file reads pretty-printed (JSON Lines one value per line); <file>:<filter> is the jq filter’s output.
  • pdf: .pdf, its text under ## Page <n> headings; <file>:<pages> (3, 2-5, 4-, 1,4-6) some pages.
  • notebook: .ipynb, its cells as Markdown with their outputs summarized; <file>:<cells> some cells.

Other formats (Word, Excel, PowerPoint, EPUB, images, binaries) read as pi reads them.