Skip to content
Merged
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
15 changes: 10 additions & 5 deletions docs/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,19 @@ To keep the whole transcript across reconnects, merge every view into Pi Durable
| `entries` | The newest entries of the transcript, starting with the newest 50. |
| `before` | The id of the entry just before `entries`. It is absent when `entries` reaches the start of the transcript. |
| `docs["pi.live"].run` | Present while a run is in progress. |
| `docs["pi.live"].generation` | The partial answer in `message`, and the retry backoff in `retry`. |
| `docs["pi.live"].tools` | The tools of the current round, each with a `name`, a `status`, and its running `output`. |
| `docs["pi.live"].generation` | The partial answer in `message`, the retry backoff in `retry`, and a deferred response being polled in `deferred`. |
| `docs["pi.live"].tools` | The tools of the current round, each with a `name`, a `status` (`pending`, `running`, `sleeping` or `waiting` while parked, `done`), and its running `output`. |
| `docs["pi.live"].nestedTools` | Calls a running tool made through `executeTool()`. |
| `docs["pi.live"].compactions` | Older messages are being summarized to free up context. |
| `docs["pi.live"].compactionFailure` | Why the newest compaction failed. |
| `docs["pi.live"].runFailure` | Why the newest run ended without an answer. |
| `docs["pi.agent"]` | What the conversation runs with: model, thinking level, extensions, tools, instructions, `maxTurns`, and compaction settings. |
| `docs["pi.provider"]` | The session id providers get for prompt caching. |
| `docs["pi.inbox"]` | Messages waiting for the current run. |
| `docs["pi.usage"]` | The conversation's token and cost totals. |

To resume a run that ended without an answer, such as after a model error, call `conversation.submit(id, { type: "continue" })`. The model answers the context as it is. It rejects with `ConversationBusy` while a run is working.

To read older entries, pass `before` to `conversation.entries(id, { maxEntryId: before }, limit)`. To read the messages the model sees, call `conversation.context(id)`, which returns `{ messages }`.

## Your own actions and `c.pi`
Expand Down Expand Up @@ -597,7 +603,7 @@ The agent loop runs in an Actor on your backend, and the commands the model choo

- Model keys, sandbox provider keys, and the secrets your [custom tools](/guides/agent-custom-tools) use stay on your worker. The model and the sandbox never see them.
- [User subscription](/guides/user-subscriptions) logins stay in your `credentials` Actor. The agent never receives a refresh token.
- A conversation stores every message and tool result, and clients can read them with `conversation.context` or watch them as events, so keep secrets out of tool results and error messages.
- A conversation stores every message and tool result, and clients can read them with `conversation.context` or `conversation.watch`, so keep secrets out of tool results and error messages.
- [Traces](/guides/agent-observability) never record prompt text, tool arguments, or tool results.

### Who can connect
Expand All @@ -613,10 +619,9 @@ The agent loop runs in an Actor on your backend, and the commands the model choo
- **Plain tool calls aren't rerun after a crash.** Use a task or `replay: "safe"`.
- **A tool that ignores its abort signal blocks sleep.** An upgrade then waits up to `sleepGracePeriod` for it to finish.
- **A cut-off model request is sent again from the start.**
- **No image reads, and no binary writes** in a sandbox.
- **No binary writes** in a sandbox.
- **Waits cap at ten minutes.** For longer jobs, use `conversation.submit` and stream.
- **No `db` option.** `pi()` owns the Actor's database. Create your own tables in `c.db` from `onWake`, as in [Tools and hooks with the Actor's context](#tools-and-hooks-with-the-actors-context), or keep data in documents or another Actor.
- **No built-in approval flow.** See [Human in the Loop](/guides/agent-human-in-the-loop).
- **Roll forward only.** See [Upgrades](#upgrades).
- **Node.js 22.19 or later.**

Expand Down
4 changes: 2 additions & 2 deletions docs/content/guides/agent-built-in-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Built-in tools are Pi's own file and shell tools. The `CodingTools` extension fr

| Tool | What it does |
| --- | --- |
| `read` | Reads a text file. The model reads a long file in parts. |
| `read` | Reads a text file, or a PNG, JPEG, GIF, or WebP image. The model reads a long file in parts. |
| `write` | Creates or replaces a file. |
| `edit` | Changes part of a file. |
| `bash` | Runs a shell command. |
Expand Down Expand Up @@ -59,6 +59,6 @@ Each tool also has its own factory: `createReadTool`, `createWriteTool`, `create
- A client can narrow the tools of one conversation with `conversation.configure(id, { tools: ["read"] })`.
- In a sandbox, the file tools reject paths outside the sandbox's working directory.
- The `bash` tool accepts a timeout, and long output is truncated before it reaches the model.
- Images aren't supported yet. The `read` tool returns an error for an image file.
- The `read` tool sends an image to the model as an attachment. Without an image processor, it sends PNG, JPEG, GIF, and WebP files up to 2000×2000 pixels and 4.5 MB as they are, and refuses larger images and other formats. To resize and convert them, install `createCodingTools({ images: await createNodePhotonImages() })` from Pi Durable. A model without image input sees a placeholder instead.

To give an agent your own tools, see [Custom Tools](/guides/agent-custom-tools).
2 changes: 1 addition & 1 deletion docs/content/guides/agent-custom-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ npm add @earendil-works/pi-schema
Both `defineTool` and `defineExtension` come from `@earendil-works/pi-durable`. A tool is installed through an [extension](/guides/pi-extensions), and by default every conversation uses every installed extension.

- The model reads the tool's `description` and calls it with arguments that match `parameters`. Pi rejects a call whose arguments don't match.
- The model reads `content` as the result. Clients get `details` for your UI.
- The model reads `output` as the result. Clients get `details` for your UI.
- A thrown error, like the 404 in `fetchOrder`, becomes an error result. The model reads the message and can try something else.
- The `context.abortSignal` aborts when the run is cancelled or the Actor stops, so a Stop from the client also cancels the request. Until the tool returns, the Actor can't sleep, and an upgrade waits for it.

Expand Down
2 changes: 1 addition & 1 deletion docs/content/guides/agent-human-in-the-loop.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: "Human in the Loop"
description: "Make a tool wait for a person's approval before it acts."
---

Pi has no built-in approval flow, so the check goes in the tool. A tool can refuse to act until a person approves, and the model can't skip it: `deploy` only runs after someone approves that exact deploy.
This guide puts the check in the tool: `deploy` only runs after someone approves that exact deploy, and the model can't skip it. Pi can also park a tool call before it runs: a `beforeTool` hook returns `{ sleep: { signals } }`, and your approve action delivers the decision with `c.pi.signal`. See [Gates before a tool runs](https://github.com/earendil-works/pi/blob/main/packages/durable/README.md#gates-before-a-tool-runs) in Pi Durable's README.

<div style="overflow-x:auto">
<svg viewBox="0 0 600 256" role="img" aria-label="The agent&#x27;s deploy tool creates an approval Actor and ends the run, and the UI shows the request. On the next call it calls consume on the approval Actor. Approve calls approve on the approval Actor and prompts the agent, which calls deploy again." style="width:100%;min-width:520px;max-width:600px;height:auto;display:block;margin:2.5rem auto;font-family:system-ui,sans-serif">
Expand Down
13 changes: 8 additions & 5 deletions docs/content/guides/pi-extensions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ An extension is a named bundle of what an agent runs with: tools, prompt section
| `hooks` | Code that runs around model requests, tool calls, and compaction. | [Hooks](#hooks) |
| `wraps` | Decorators for a tool or section, by name. | [Wrap a tool](#wrap-a-tool) |
| `tasks` | Multi-step work that resumes after a crash. | [Tools and tasks](/agents/docs#tools-and-tasks) |
| `namespace` | A name and description that group the extension's tools for Pi's code mode. Code mode doesn't run in `pi()` environments yet. | [Pi Durable README](https://github.com/earendil-works/pi/blob/main/packages/durable/README.md) |

By default, every conversation uses every installed extension, in install order. When two selected extensions have a tool with the same name, the later one wins.

Expand All @@ -57,15 +58,17 @@ A hook runs inside a built-in task, such as a tool call, in every conversation t

| Hook | Task | Runs | Can |
| --- | --- | --- | --- |
| `beforeTool` | `ToolTask` | Before a tool call runs. | Block the call or rewrite its arguments. A throw also blocks it. |
| `afterTool` | `ToolTask` | After a tool call returns. | Replace the result. |
| `beforeTool` | `ToolTask` | Before a tool call runs, and again each time a parked call wakes. | Block the call, rewrite its arguments, or park it with `{ sleep: { signals, until } }`. A throw also blocks it. |
| `afterTool` | `ToolTask` | After every result a tool call writes, also the error results Pi writes itself, such as for a blocked or interrupted call. | Replace the result. |
| `beforeRequest` | `GenerationTask` | Before every model request. | Replace the messages of that request only. |
| `beforeProviderRequest` | `GenerationTask` | After pi-ai builds the provider's request body, before it is sent. | Replace that body, such as to set a provider-specific field. |
| `afterResponse` | `GenerationTask` | After every model response. | Observe it. |
| `afterTools` | `GenerationTask` | After every tool call of a round finishes. | Observe the results. |
| `onYield` | `GenerationTask` | When the model gives a final answer. | Continue the run with another user message. |
| `afterTools` | `GenerationTask` | After every tool call of a round finishes. | Return `{ tasks }` to run before the round ends. |
| `onYield` | `GenerationTask` | When the model gives a final answer. The first hook that returns a result wins. | Return `{ kind: "continue", continue }` to continue the run with another user message, or `{ kind: "tasks", tasks }` that decide whether the answer stands, such as a review. |
| `afterRun` | `GenerationTask` | After a run's answer stands. | Return `{ tasks }` to run before the run ends, such as tests after a run that edited files. |
| `beforeCompact` | `CompactionTask` | Before a compaction summarizes the conversation. | Decline it or supply your own summary. |

Hooks run in the agent Actor on your worker, so they can call your APIs. To enforce a rule in every conversation, keep its extension in the default selection. To approve tool calls one by one, see [Human in the Loop](/guides/agent-human-in-the-loop).
Hooks build tasks with Pi's `hookTask()`. Hooks run in the agent Actor on your worker, so they can call your APIs. To enforce a rule in every conversation, keep its extension in the default selection. To approve tool calls one by one, see [Human in the Loop](/guides/agent-human-in-the-loop).

## Wrap a tool

Expand Down
Loading