tools
js/ai/tools.ts
fino:ai/tools — workspace file and shell tools for coding agents, bounded by explicit grants.
fino:ai/tool defines one tool; this module ships the set a coding agent
needs to work in a project: list_files, read_file and search_files,
plus the mutating write_file, edit_file and shell. Each is an ordinary
Tool, so the set can be handed to an Agent, mounted on an MCP server, or
invoked directly.
Grants
Every tool is bounded by a SandboxFilesystemGrant, the same grant shape
fino:ai/sandbox uses, checked by the same canonical-path rule. Relative
paths resolve against cwd. Reads must resolve, after following links,
beneath a read root; writes and edits beneath a write root. The default
grant is cwd for both. Pass filesystem to widen it (a neighbouring
checkout, a scratch directory) or narrow it (write: [] for a read-only
set). The check bounds what a model asks for; it is not a defence against a
hostile local process racing the filesystem.
shell runs /bin/sh -c under the runtime's strict process sandbox, with
the same read and write roots, read-only system roots (/bin, /usr and
the like, adjustable through systemRoots), and all network access denied.
Toolchains installed elsewhere, such as Homebrew or Xcode command-line
tools, need their directories in the read grant. Where a strict
backend is unavailable the tool fails closed and reports why rather than
running unconfined. A caller that wants a different policy, including
granting network access or choosing bestEffort, passes sandbox
explicitly. Commands see only the environment variables in environment,
which defaults to PATH, HOME, LANG and TERM from this process.
Approval and limits
Mutating tools declare requiresApproval unless approval is 'never',
so a Session suspends for a human decision before they run. Outputs are
capped (48 000 characters per result, 2 000 lines per read, 500 listed
paths, 200 search matches) so one call cannot flood a context window, and
each cap is reported in the result so the model can page or narrow.
import { agent } from 'fino:ai/agent';
import { openai } from 'fino:ai/model';
import { createWorkspaceTools } from 'fino:ai/tools';
const coder = agent({
model: openai({ model: 'gpt-4o' }),
tools: createWorkspaceTools({
cwd: '/repo',
filesystem: { read: ['/repo', '/opt/homebrew'], write: ['/repo'] },
}),
});Interfaces
interface WorkspaceToolOptions {
Options shared by every workspace tool.
import { readFileTool } from 'fino:ai/tools';
const read = readFileTool({ cwd: '/repo', filesystem: { read: ['/repo'] } });Properties
cwd: string
Absolute workspace root that relative tool paths resolve against.
filesystem?: SandboxFilesystemGrant
Roots the tools may read and write. Defaults to cwd for both; an
explicit grant is used exactly as given, so omit write for read-only.
approval?: 'mutations' | 'never'
'mutations' (the default) marks write_file, edit_file and shell
with requiresApproval; 'never' runs them without suspending.
interface ShellToolOptions extends WorkspaceToolOptions {
Options for shellTool().
import { shellTool } from 'fino:ai/tools';
const shell = shellTool({ cwd: '/repo', environment: { PATH: '/usr/bin:/bin' } });Properties
environment?: Record<string, string>
Complete environment for commands. Defaults to PATH, HOME, LANG and
TERM copied from this process when set.
sandbox?: ProcessSandboxOptions
Process sandbox policy, replacing the default derived from filesystem
(strict mode, read and write roots, all network denied).
systemRoots?: string[]
Read-only system roots added to the default policy so commands can find
and load system binaries. Defaults to /bin, /sbin, /usr and
/private/etc on macOS, plus /lib, /lib64 and /etc on Linux; roots
missing on the host are skipped. Ignored when sandbox is given.
maxTimeoutMs?: number
Longest timeout a call may request, in milliseconds. Defaults to 600 000.
interface WorkspaceToolsOptions extends WorkspaceToolOptions {
Options for createWorkspaceTools().
import { createWorkspaceTools, type WorkspaceToolsOptions } from 'fino:ai/tools';
const planning: WorkspaceToolsOptions = { cwd: '/repo', writes: false };
const tools = createWorkspaceTools(planning);Properties
writes?: boolean
Include write_file, edit_file and shell. Defaults to true.
shell?:
| Pick<ShellToolOptions, 'environment' | 'sandbox' | 'systemRoots' | 'maxTimeoutMs'>
| false
Shell-specific options, or false to omit shell from a writable set.
Functions
function listFilesTool(opts: WorkspaceToolOptions): Tool
Build list_files: glob for file paths beneath cwd.
Matches outside the read roots are omitted. Results are sorted, reported
relative to cwd, and capped at 500 entries. Read-only.
import { listFilesTool } from 'fino:ai/tools';
const list = listFilesTool({ cwd: '/repo' });function readFileTool(opts: WorkspaceToolOptions): Tool
Build read_file: read a text file with line numbers.
Long lines are clipped to 500 characters, a page is at most 2 000 lines, and the result names the offset to continue from. Read-only.
import { readFileTool } from 'fino:ai/tools';
const read = readFileTool({ cwd: '/repo' });function searchFilesTool(opts: WorkspaceToolOptions): Tool
Build search_files: grep beneath a directory with a regular expression.
The walk starts at path (default cwd), which must be beneath a read
root. It never follows links, so it cannot leave that directory, and it
skips VCS metadata and common build output, files over 1 MiB, and files
with NUL bytes in their first 4 KiB. Results are capped at 200 matches.
Read-only.
import { searchFilesTool } from 'fino:ai/tools';
const search = searchFilesTool({ cwd: '/repo' });function writeFileTool(opts: WorkspaceToolOptions): Tool
Build write_file: create or overwrite one file beneath a write root.
Missing parent directories are created. Requires approval unless approval
is 'never'.
import { writeFileTool } from 'fino:ai/tools';
const write = writeFileTool({ cwd: '/repo' });function editFileTool(opts: WorkspaceToolOptions): Tool
Build edit_file: replace exact text in one existing file beneath a write root.
oldText must match exactly once unless replaceAll is set, so an
ambiguous edit fails instead of guessing. Requires approval unless
approval is 'never'.
import { editFileTool } from 'fino:ai/tools';
const edit = editFileTool({ cwd: '/repo' });function shellTool(opts: ShellToolOptions): Tool
Build shell: run a command from cwd under the strict process sandbox.
The command runs as /bin/sh -c with the grant's read and write roots,
no network access, and only environment. The shell is killed on timeout or
run cancellation. Descendants it started are not, because the runtime has no
process-group kill yet, but the tool stops reading their output 200 ms after
the shell exits, so a background process cannot hold a call open. The result reports the exit code and capped stdout and
stderr; a non-zero exit, a timeout, or a sandbox that cannot be enforced is
an error result. Requires approval unless approval is 'never'.
import { shellTool } from 'fino:ai/tools';
const shell = shellTool({ cwd: '/repo' });function createWorkspaceTools(opts: WorkspaceToolsOptions): Tool[]
Build the workspace tool set.
Always includes list_files, read_file and search_files. Unless
writes is false, also includes write_file, edit_file and, unless
shell is false, shell. Every tool shares one grant, cwd and
approval policy.
import { createWorkspaceTools } from 'fino:ai/tools';
const planning = createWorkspaceTools({ cwd: '/repo', writes: false });
const unattended = createWorkspaceTools({ cwd: '/repo', approval: 'never', shell: false });