Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions docs/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The `prompt` action waits for the run to finish and returns `{ status, text }`.

<Step title="Add a sandbox (optional)">

To let the agent run commands, give it a sandbox and Pi's `CodingTools`, which add `bash`:
To let the agent run commands, give it a sandbox. A sandbox brings Pi's coding tools, `read`, `write`, `edit`, and `bash`, which run in it:

```sh
npm add @rivet-dev/sandbox-adapter
Expand Down Expand Up @@ -159,7 +159,7 @@ An agent needs no sandbox. Without one, Pi's `read`, `write`, and `edit` tools w

- **Nothing to set up.** The files are saved with the conversation and survive sleep, crashes, and upgrades.
- **Large files are cheap.** A read of a few lines loads only those lines from the database.
- **No shell.** `bash` returns an error. To run commands, add a [sandbox](/guides/agent-sandboxes).
- **No shell.** There is no `bash`. To run commands, add a [sandbox](/guides/agent-sandboxes).

## Use pi-ai

Expand All @@ -184,7 +184,7 @@ Model names such as `anthropic/claude-opus-5-5` come from pi-ai's model catalog.

## Tools and tasks

The `CodingTools` extension adds `read`, `write`, `edit`, and `bash`, which run in the [sandbox](/guides/agent-sandboxes):
With a [sandbox](/guides/agent-sandboxes), the agent gets Pi's coding tools, `read`, `write`, `edit`, and `bash`, which run in the sandbox. Set `codingTools` to a list, such as `["read", "bash"]`, to install only those tools, or to `false` to install none:

<CodeSnippet file="examples/docs/pi/server.ts" title="server.ts" />

Expand Down Expand Up @@ -684,9 +684,15 @@ The `pi()` function accepts every [`actor()`](/actors/docs/actor-configuration)
| `apiKeys` | API keys by provider. See [Keys in code](#keys-in-code). |
| `providers` | Providers Pi does not know. See [Custom providers](#custom-providers). |
| `credentials` | Logins your app stores. See [Run Agents on Your Users' Subscriptions](/guides/user-subscriptions). |
| `sandbox` | Where `CodingTools` run. Without one, files live in the Actor's database. See [Providers](/guides/agent-sandboxes#providers) for each provider's options. |
| `sandbox` | Where the coding tools run. Without one, files live in the Actor's database. See [Providers](/guides/agent-sandboxes#providers) for each provider's options. |
| `codingTools` | Pi's `read`, `write`, `edit`, and `bash`. On with a `sandbox`, off without one. `true`, `false`, or a list of the tools to install. |
| `instructions` | Starting instructions of new conversations, after the extension sections. |
| `cwd` | Starting working directory of new conversations. |
| `maxTurns` | The most tool rounds one run takes. |
| `compaction` | When Pi compacts a conversation, the same as `settings.compaction`. |
| `toolSearch` | `true` installs Pi's tool search, so the model finds deferred tools by name. |
| `documents` | Documents clients may read. |
| `settings` | Pi Durable settings, such as `retry` and `compaction`. |
| `settings` | Pi Durable settings, such as `retry`. |
| `onAnswer` | Runs for every final answer. See [Deliver answers](#deliver-answers). |

Other options, such as `thinkingLevel` and `env`, pass through to Pi Durable. By default, `actionTimeout` is ten minutes and `sleepGracePeriod` is 15 minutes. A `ConversationBusy` error reaches clients as a `UserError`, and so does `pi_schema_too_new` when the actor's data has a newer Pi Durable schema than the code. Other Pi Durable errors reach clients as internal errors.
Expand Down
8 changes: 4 additions & 4 deletions docs/content/guides/agent-built-in-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: "Give an Agent File and Shell Tools"
description: "Pi's file and shell tools, where they run with and without a sandbox, and how to choose them."
---

Built-in tools are Pi's own file and shell tools. The `CodingTools` extension from `@earendil-works/pi-durable/tools` installs all four:
Built-in tools are Pi's own file and shell tools. An agent with a sandbox gets all four:

| Tool | What it does |
| --- | --- |
Expand All @@ -14,7 +14,7 @@ Built-in tools are Pi's own file and shell tools. The `CodingTools` extension fr

## Where they run

The `sandbox` option decides where the tools run. They never touch your worker's file system or shell. With a sandbox, all four tools run there. Without one, the file tools work on files in the Actor's own SQLite database and `bash` returns an error. See [Without a sandbox](/agents/docs#without-a-sandbox).
The `sandbox` option decides where the tools run. They never touch your worker's file system or shell. With a sandbox, all four tools run there. Without one, the file tools work on files in the Actor's own SQLite database and there is no shell. See [Without a sandbox](/agents/docs#without-a-sandbox).

<div style="overflow-x:auto">
<svg viewBox="0 0 640 220" role="img" aria-label="Without a sandbox, the agent Actor's read, write, and edit tools work on files in the Actor's SQLite database. With a sandbox, read, write, edit, and bash run in the sandbox." style="width:100%;min-width:520px;max-width:640px;height:auto;display:block;margin:2.5rem auto;font-family:system-ui,sans-serif">
Expand All @@ -40,7 +40,7 @@ The `sandbox` option decides where the tools run. They never touch your worker's
</svg>
</div>

Without a sandbox, there is nothing to set up. The files are saved with the conversation and survive sleep and crashes. This agent has no sandbox, so it installs the file tools without `bash`:
Without a sandbox, there is nothing to set up. The files are saved with the conversation and survive sleep and crashes. This agent has no sandbox, so `codingTools` installs the file tools without `bash`:

<CodeGroup>
<CodeSnippet file="examples/docs/built-in-tools/server.ts" title="server.ts" />
Expand All @@ -51,7 +51,7 @@ The second prompt edits the file that the first prompt wrote. To run commands, g

## Choose tools

Each tool also has its own factory: `createReadTool`, `createWriteTool`, `createEditTool`, and `createBashTool`. Install only the tools you want in your own extension. This reviewer can read files and run tests, but it has no `write` or `edit` tool:
Set `codingTools` to the tools the agent may use, or to `false` for none. This reviewer can read files and run tests, but it has no `write` or `edit` tool:

<CodeSnippet file="examples/docs/built-in-tools/reviewer.ts" title="server.ts" />

Expand Down
2 changes: 1 addition & 1 deletion docs/content/guides/agent-sandboxes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Leave out the `sandbox` option, and Pi's file tools work on files stored in the

## With a sandbox

When the agent needs to run commands, such as tests or a build, pass a sandbox provider. <a href="https://github.com/rivet-dev/agents/tree/main/packages/sandbox-adapter" target="_blank" rel="noopener noreferrer">`@rivet-dev/sandbox-adapter`</a> gives each Actor its own sandbox, and `CodingTools` run there.
When the agent needs to run commands, such as tests or a build, pass a sandbox provider. <a href="https://github.com/rivet-dev/agents/tree/main/packages/sandbox-adapter" target="_blank" rel="noopener noreferrer">`@rivet-dev/sandbox-adapter`</a> gives each Actor its own sandbox, and Pi's coding tools, `read`, `write`, `edit`, and `bash`, run there.

<div style="overflow-x:auto">
<svg viewBox="0 0 676 306" role="img" aria-label="The agent Actor sends tool calls through the sandbox adapter, which creates and connects to one sandbox per Actor from a sandbox provider: agentOS, E2B, Daytona, or Freestyle." style="width:100%;min-width:520px;max-width:676px;height:auto;display:block;margin:2.5rem auto;font-family:system-ui,sans-serif">
Expand Down
9 changes: 2 additions & 7 deletions examples/docs/built-in-tools/reviewer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { createRegistry, defineExtension } from "@earendil-works/pi-durable";
import { createBashTool, createReadTool } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { e2bProvider } from "@rivet-dev/sandbox-adapter/e2b";
import { setup } from "rivetkit";

// read and bash, without the write and edit tools
const extensions = createRegistry();
extensions.install(defineExtension({ name: "review-tools", tools: [createReadTool(), createBashTool()] }));

const reviewer = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
sandbox: e2bProvider(),
// read and bash, without the write and edit tools
codingTools: ["read", "bash"],
});

export const registry = setup({ use: { reviewer } });
Expand Down
11 changes: 3 additions & 8 deletions examples/docs/built-in-tools/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { createRegistry, defineExtension } from "@earendil-works/pi-durable";
import { createEditTool, createReadTool, createWriteTool } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { setup } from "rivetkit";

// This agent has no sandbox, so its files live in the Actor's own database.
// There is no shell, so it gets read, write, and edit without bash.
const extensions = createRegistry();
extensions.install(defineExtension({ name: "files", tools: [createReadTool(), createWriteTool(), createEditTool()] }));

const writer = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// This agent has no sandbox, so its files live in the Actor's own database.
// There is no shell, so it gets read, write, and edit without bash.
codingTools: ["read", "write", "edit"],
});

export const registry = setup({ use: { writer } });
Expand Down
8 changes: 1 addition & 7 deletions examples/docs/pi/daytona.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { daytonaProvider } from "@rivet-dev/sandbox-adapter/daytona";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: daytonaProvider(),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/docs/pi/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { e2bProvider } from "@rivet-dev/sandbox-adapter/e2b";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: e2bProvider(),
});

Expand Down
9 changes: 2 additions & 7 deletions examples/docs/quickstart/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { createRegistry, defineExtension } from "@earendil-works/pi-durable";
import { createEditTool, createReadTool, createWriteTool } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { setup } from "rivetkit";

// read, write, and edit. With no sandbox, the files live in the Actor's own database.
const extensions = createRegistry();
extensions.install(defineExtension({ name: "files", tools: [createReadTool(), createWriteTool(), createEditTool()] }));

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// read, write, and edit. With no sandbox, the files live in the Actor's own database.
codingTools: ["read", "write", "edit"],
});

export const registry = setup({ use: { agent } });
Expand Down
8 changes: 1 addition & 7 deletions examples/docs/sandboxes/agentos.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { agentOSProvider } from "@rivet-dev/sandbox-adapter/agentos";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: agentOSProvider(),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/docs/sandboxes/daytona.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { daytonaProvider } from "@rivet-dev/sandbox-adapter/daytona";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: daytonaProvider(),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/docs/sandboxes/docker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { dockerProvider } from "@rivet-dev/sandbox-adapter/docker";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in a local Docker container
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in a local Docker container.
sandbox: dockerProvider({ image: "ubuntu:24.04" }),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/docs/sandboxes/e2b.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { e2bProvider } from "@rivet-dev/sandbox-adapter/e2b";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: e2bProvider({ template: "base" }),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/docs/sandboxes/freestyle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { createRegistry } from "@earendil-works/pi-durable";
import { CodingTools } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { freestyleProvider } from "@rivet-dev/sandbox-adapter/freestyle";
import { setup } from "rivetkit";

// read, write, edit, and bash, running in the sandbox
const extensions = createRegistry();
extensions.install(CodingTools);

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// The sandbox brings read, write, edit, and bash, which run in it.
sandbox: freestyleProvider({
create: {
snapshotId: "freestyle/ubuntu-sm",
Expand Down
9 changes: 2 additions & 7 deletions examples/docs/sandboxes/no-sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { createRegistry, defineExtension } from "@earendil-works/pi-durable";
import { createEditTool, createReadTool, createWriteTool } from "@earendil-works/pi-durable/tools";
import { pi } from "@rivet-dev/pi";
import { setup } from "rivetkit";

// read, write, and edit. With no sandbox, the files live in the Actor's own database.
const extensions = createRegistry();
extensions.install(defineExtension({ name: "files", tools: [createReadTool(), createWriteTool(), createEditTool()] }));

const agent = pi({
model: "anthropic/claude-opus-5-5",
registry: extensions,
// read, write, and edit. With no sandbox, the files live in the Actor's own database.
codingTools: ["read", "write", "edit"],
});

export const registry = setup({ use: { agent } });
Expand Down
35 changes: 26 additions & 9 deletions packages/pi/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
UserActorConfig,
} from "./actor-types.js";
import type { PiAnswer } from "./answers.js";
import { withExtension } from "./registry.js";
import { builtInExtensions, withExtensions } from "./registry.js";
import {
closeHarness,
createPiRuntime,
Expand Down Expand Up @@ -145,7 +145,11 @@ export type PiToolContext<
> &
PiAccess;

/** The Pi options with `tools`, `hooks`, and `onAnswer` typed to get the actor's context. */
/**
* The Pi options with `tools`, `hooks`, and `onAnswer` typed to get the actor's
* context. The context is never inferred from them, so a tool typed against
* the registry that holds this actor cannot turn its state and events `any`.
*/
type PiConfigOptions<TContext, TTools> = Omit<
PiOptions,
"tools" | "hooks" | "onAnswer"
Expand All @@ -154,16 +158,18 @@ type PiConfigOptions<TContext, TTools> = Omit<
* Tools defined like actions, by name. `execute(c, args, api, context)`
* gets the actor's context first, and `args` typed from `parameters`.
*/
tools?: { [K in keyof TTools]: PiTool<TContext, TTools[K] & object> };
tools?: {
[K in keyof TTools]: PiTool<NoInfer<TContext>, TTools[K] & object>;
};
/** Pi Durable's hooks with the actor's context first, such as `beforeTool(c, call, api, context)`. */
hooks?: PiHooks<TContext>;
hooks?: PiHooks<NoInfer<TContext>>;
/**
* Called with the actor's context for every final answer, such as to text
* it to the user. A run that resumes after a crash can call it again for
* the same answer if it didn't return. A throw goes to `onReport` and
* leaves the run as it is.
*/
onAnswer?: (c: TContext, answer: PiAnswer) => void | Promise<void>;
onAnswer?: (c: NoInfer<TContext>, answer: PiAnswer) => void | Promise<void>;
};

/** Ordinary actor config plus the Pi Durable options. The app's actions, lifecycle hooks, Pi tools, and Pi hooks get `c.pi`. */
Expand Down Expand Up @@ -213,11 +219,17 @@ const piOptionKeys = [
"onReport",
"model",
"thinkingLevel",
"instructions",
"cwd",
"maxTurns",
"compaction",
"scopedModels",
"providers",
"apiKeys",
"credentials",
"sandbox",
"codingTools",
"toolSearch",
"documents",
"onAnswer",
] as const satisfies readonly (keyof PiOptions)[];
Expand Down Expand Up @@ -266,13 +278,18 @@ export function pi<
PiActions
> {
const { actorConfig, options } = splitConfig<PiOptions>(config, piOptionKeys);
const settings = options.compaction
? { ...options.settings, compaction: options.compaction }
: options.settings;
const registry = options.registry ?? createRegistry();
const extension = actorExtension(options.tools, options.hooks);
const actorTools = actorExtension(options.tools, options.hooks);
const piOptions: ResolvedPiOptions = {
...options,
registry,
...(extension &&
withExtension({ registry, settings: options.settings }, extension)),
settings,
...withExtensions({ registry, settings }, [
...builtInExtensions(options, registry),
...(actorTools ? [actorTools] : []),
]),
};
if (actorConfig.db !== undefined) {
throw new Error("pi() owns the actor database; remove the db option");
Expand Down
4 changes: 2 additions & 2 deletions packages/pi/src/answers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type RegistryReader,
} from "@earendil-works/pi-durable";
import type { PiAccess } from "./actor.js";
import { withExtension } from "./registry.js";
import { withExtensions } from "./registry.js";
import type { PiContext } from "./runtime.js";

/** A final answer, as `onAnswer` receives it. */
Expand Down Expand Up @@ -43,7 +43,7 @@ export function withAnswerHook(
onAnswer: PiOnAnswer,
report: (error: unknown) => void,
): { registry: RegistryReader; settings?: HarnessSettings } {
return withExtension(options, answerExtension(c, onAnswer, report));
return withExtensions(options, [answerExtension(c, onAnswer, report)]);
}

function answerExtension(
Expand Down
Loading
Loading