Skip to content

Tools

A tool is a function that Pi can call. Pi gets the name, the description, and the parameters of each tool with each request, and calls a tool when its task needs it.

Use defineTool from @earendil-works/pi-durable, and give the tool to your extension:

const wordCount = defineTool({
name: "my_word_count",
description: "Count the words in a text.",
parameters: obj({ text: str({ description: "The text to count" }) }),
replay: "safe",
execute: async (args) => ({
output: [{ type: "text" as const, text: `${countWords(args.text)} words` }],
}),
});
// It returns `output`, so it needs version 2 of the extension API: Pi Pocket 0.18 says so, and does not load it.
export const pocketApi = 2;
export default function createMyWords() {
return defineExtension({ name: "my-words", tools: [wordCount] });
}
  • name must be different from the name of each other tool (Names). Use a prefix of your own.
  • description and parameters go to the model with each request. Keep them short and exact.
  • deferred: true keeps a tool out of the requests until Pi loads it with tool_search, which only the Tool search module offers: while it is off, Pi cannot load the tool. Use it for a tool that few conversations need, or for a large set of tools. test/prompt-budget.test.ts keeps Pi Pocket’s own prompt and tools under 14,000 characters (about 5,000 tokens) in a plain session, a guard against growth by accident: a rule that makes the work better is worth its tokens.
  • parameters is a JSON Schema object. Build it with the builders of @earendil-works/pi-schema: obj, str, num, int, bool, arr, union, and opt for a property that can be left out. obj makes a closed object (additionalProperties: false); obj(properties, { open: true }) lets other properties through. Pi Durable checks the arguments against the schema before execute runs. Since Pi 1.5 (Pi Pocket 0.28.0), pi-schema does the check, and the model gets its messages, such as - path: missing. A drop-in written before can keep TypeBox’s Type from @earendil-works/pi-ai (API versions).
  • execute(args, api, context) does the work.

Set subagents: "always" on a tool that every subagent needs. When Pi starts a subagent with a tools list, Pi Pocket adds the marked tools that the session enables, from the extensions that run there, once each. It never adds an extension: a tool from one removed for subagents, or left out of a session’s fixed extension list, does not come along. Without a tool list, nothing changes. The tool’s prompt and spawn result do not mention the marker or the tools it adds.

A marked tool with deferred: true is offered at once when the subagent has a tool list: a subagent has no tool search. A tool that only scripts call keeps that rule, and brings codemode with it. Sending more work to an existing subagent does not change its tools.

Pi Durable’s defineTool does not type this Pi Pocket option. Add it after defineTool, as with annotations:

import type { ToolOptions } from "./_pocket.d.ts";
const lookup = Object.assign(defineTool({ … }), {
subagents: "always",
} satisfies ToolOptions);

Since 0.29.0, experimental: it can change in a minor release. This is an addition, so the API version stays 2. Older Pi Pocket ignores the field; it does not add the tool. A gallery entry that needs this behavior sets minPocket (API versions).

  • Return { output: [{ type: "text", text }] }. The model reads the text.
  • Pi Durable 1.2 renamed the result field content to output. Pi Pocket still reads content from what execute of a drop-in’s tool returns, but new code must use output. A hook (afterTool) or a wrap (wrapTool) that reads or returns a result must use output too. A module that returns output exports pocketApi = 2 (API versions): Pi Pocket 0.18 then refuses it with a clear message.
  • To give the model an error, throw an Error. The model gets its message as an error result.
  • api.output(text) streams output while the tool runs. When execute returns no output, this output becomes the result.
  • Since Pi 1.6 (Pi Pocket 0.30.0), execute can return a park in place of a result, as Pi Durable’s README tells; since Pi 1.7 (Pi Pocket 0.31.0), parks are tagged ({ kind: "sleep", … } or { kind: "wait", … }). A park has no result fields: a wrap (wrapTool) that reads a result must let a park through. A call from a codemode script cannot park: Pi Pocket calls the tool’s cancel, and the script’s call fails with an error.

A restart of the server can stop a tool call while it runs. Then the call is replayed: it runs again, or the model gets “interrupted”.

  • Set replay: "safe" only when it does no harm to run the call two times. For example, the tool only reads, or it does an action with a key, so that a second run finds the result of the first.
  • Without replay: "safe", the tool is unsafe to replay. A call that a restart stops comes back to the model as interrupted, and does not run again. This is the default.
  • api.memo(name, context) reads a value that is kept with this call. api.memo(name, value, context) stores it. A replay after a restart finds the value. Use a memo for work that must not occur two times, for example a message that you send.
  • api.conversationId, api.taskId, and api.callId identify the call. The key ${api.taskId}:${api.callId} stays the same when the call runs again.

annotations: { readOnlyHint: true }, as in Pi’s tool definitions, says that the tool does not change files or other state. Plan mode lets such a tool run. Pi Pocket does not check this: it is your promise. A tool that only changes the conversation, such as context management, can also use it. Plan mode ignores the hint on a built-in tool, so a wrap cannot let a built-in tool run in plan mode.

Pi Durable’s type for defineTool does not have annotations yet. TypeScript reports it as an unknown property, but the module loads and works, because Node does not check types. To keep the type checker quiet, add it after defineTool:

const lookup = Object.assign(defineTool({ … }), { annotations: { readOnlyHint: true } });
  • api.env gives the files and the shell of the session’s folder. (await api.agent(context)).cwd is the folder.
  • api.snapshot(Doc, api.conversationId, context) reads a document, for example one of Pi Pocket’s in src/server/docs.ts.

A tool can ask a model itself, for example to choose each step of a browser run. Use the session’s model, through Pi Durable:

if (host.heldBack(api.conversationId) !== undefined) {
throw new Error("A spend limit holds this session back.");
}
const chosen = (await api.agent(context)).model;
const model = chosen && api.models.getModel(chosen.provider, chosen.modelId);
if (model === undefined) {
throw new Error("This session has no model.");
}
const reply = await api.models.completeSimple(model, {
messages: [
{ role: "user", content: [{ type: "text", text: question }], timestamp: Date.now() },
],
});
  • The provider’s request limits apply, as to Pi’s own requests.
  • The session’s spend does not count this reply by itself. Add reply.usage to Pi Durable’s UsageDoc for the session, in a commit, as Pi Pocket’s titles do (titles.ts). Check host.heldBack before each call, so a spend limit stops your tool too.
  • Say in your tool’s result what the model decided, so Pi and the people can follow it.

These pages describe Pi Pocket 0.31.3. Your copy has the pages of its own version in its docs/ folder.