What Pi gives you

The harness underneath: conversations, tasks, tools, and a common API for chat models.

beginner8 min read

You send a question, the assistant reads a file, and a moment later you see an answer about what was in it. Between the question and the answer, something had to remember the question, ask the model what to do, make the file call, and send the result back to the model. Most of that machinery comes to Pipal in a set of libraries called Pi.

In this chapter

  • Find the six Pi libraries in Pipal and know what each supplies.
  • Read a Pi Durable conversation as a transcript, its current state, and work in progress.
  • Follow an input through a model request and a tool call to a final answer.

Pi comes into Pipal as six libraries

What Pipal imports from Pi
  • Pi libraries in Pipal

    Pipal imports libraries from the open-source Pi project into its own server process.

    • Pi Durable

      The harness runs the conversation. Its Node SQLite adapter stores the records.

      • Harness

        Pipal core opens one Harness and supplies its model registry and extensions.

      • SQLite adapter

        @earendil-works/pi-durable/storage/sqlite/node opens durable.sqlite.

    • Pi AI

      A shared request and response interface for different model providers.

      • Model catalog

        Core imports createModels and getSupportedThinkingLevels from @earendil-works/pi-ai/models.

      • Faux provider

        Core uses @earendil-works/pi-ai/providers/faux to supply scripted replies. Pipal labels the faux model Local echo in its picker.

    • Pi Schema

      The host imports builders such as obj and str, and compile turns a schema into a validator.

    • Chord

      Pi Durable builds on Chord for document state.

      • Context

        Core imports from @earendil-works/chord/context to pass cancellation and values through a call.

    • Pi Codemode

      The host imports CodemodeSandbox when Code Mode is enabled and passes it to Pi Durable’s extension.

    • Pi MCP

      The host uses McpClient to discover a configured service’s tools and call them over HTTP.

  • Harness → Model catalog · Use the configured model registry
  • Harness → SQLite adapter · Keep conversation records
  • Harness → Pi Schema · Validate tool arguments

Pi Durable

The harness runs the conversation. Its Node SQLite adapter stores the records.

Select a package or an import to see what crosses into Pipal’s server process.

Pipal opens a Pi Durable harnessThe software that keeps track of a conversation and runs the model and tools that answer it., the machinery that runs conversations on one open store, before its HTTP server starts listening. The workspace catalog fixes all six Pi libraries at version 1.7.0. main.ts pnpm-workspace.yaml

Pipal applies a small patch to that version of Pi Durable. It lets the host stop a live provider request from being issued again once the provider has returned answer text or evidence of a search. Durable by default follows the case where a connection drops halfway through a reply. pi-durable-1.7.0-generation-retry.patch index.ts

Pi Durable does the work of carrying on a conversation. Give the harness storageThe backend where the harness keeps its records, such as a SQLite database., somewhere to keep its records, and it can take a message all the way through to an answer. It knows which work belongs to which conversationA transcript with its own identity and state, such as the model settings for one chat., a transcript with its own identity and state. Pipal opens Pi Durable’s Node SQLite adapter on durable.sqlite and passes it to the harness. README.md index.ts

Pi AI talks to the model. Provider factories supply a catalog of models and a common interface for requesting replies. The interface recognizes text arriving in pieces, or streaming, and requests to run a named function, called a toolA named function the harness can run with arguments supplied by the model or another permitted caller., arriving with arguments. It also gives callers a common way to ask for a thinking level, the amount of reasoning effort a model should use, where the model supports that choice. README.md

Local echo is a small way to see the same interface at work. Pi AI offers an in-memory provider called faux that replies from a script. Pipal supplies a script that reads the last user message and echoes its text, and names the resulting model Local echo in the picker. The harness can make a request and receive a streaming reply entirely on the host. models.ts

Pi Schema checks the shape of values. The host parses config.toml into values, then uses a compiled schema to check them. A schema compiles into ordinary JavaScript functions without eval. config.ts README.md

Chord supplies the machinery for Pi Durable’s saved JSON state. Pipal also imports @earendil-works/chord/context directly. A Chord context carries things such as a cancellation signal along with a call. When Pipal opens a watchA subscription that starts with the current view of a conversation or document and receives changes after that., or subscription to conversation changes, for an HTTP request, it can give that watch the request’s cancellation signal. README.md index.ts

Pi Codemode supplies a sandbox for JavaScript written by the model. The code can act through tools the host injects into the sandbox. Pipal creates the sandbox and registers Pi Durable’s Code Mode extension when extensions.enabled includes codemode. A profile must also grant it for a chat to use it. Tools and extensions follows the choices Pipal makes about which tools a model can reach. README.md main.ts

Pi MCP talks to services that offer tools. It speaks the Model Context ProtocolA protocol through which a client such as Pipal can discover the tools a service offers and ask the service to run one with supplied arguments. and handles the exchange of requests and replies. Pipal uses the library’s HTTP client to ask a service for its catalog. It turns the answer into tools that a profile can grant to a chat. Connecting tools with MCP shows what the model and the service each receive. README.md mcp.ts

Deep diveWhich contracts Pipal depends on at the library seam

Pi AI’s Models collection is where Pipal registers providers. Pi supplies the OpenAI factory and its OAuth implementation. Pipal wraps the login to discover the account’s models and filters that list against the pinned catalog. A library with a long list of supported providers can sit behind a picker with a short list of choices. models.ts

For tool arguments, defineTool can infer the argument type from a Pi Schema definition. The harness coerces and validates the arguments before execute(). The same schema can therefore inform the TypeScript caller and check the model’s input at runtime. Pipal’s HTTP parser uses a separate compiled validator for the body it receives from the browser. README.md request.ts

Pi Durable’s README calls the API experimental and says it can change without notice between releases. Pipal pins these packages in its workspace catalog and provides a recovery suite for checking the integration. That matters even if an upgrade still type-checks. A tool invocation saved by yesterday’s process and reopened by today’s process exercises a contract beyond the shape of an import. README.md pnpm-workspace.yaml package.json

A conversation has history, state, and work

An entryOne permanent record in a conversation’s transcript, such as a user message or a tool result. is one fact in the transcript, such as a user message or a tool result. Once written, it stays as it was. A documentNamed, typed JSON state that can change in a commit. holds named JSON state you can change, such as the current model. One all-or-nothing write, called a commitAn all-or-nothing write that can save transcript entries, document changes, and work together., can add entries, change documents, and set up work together. README.md

Document What you can learn from it
pi.agent Which model, instructions, and extensions the conversation runs with.
pi.live Which model request or tool calls are in progress and what they have produced so far.
pi.usage The conversation’s recorded token and cost totals.

A submissionAn input or entry handed to a conversation, with an identity you can use to wait for its outcome. is something you hand to the conversation, such as user input. It gives you something to wait on when you want the outcome. A taskA unit of work whose state is saved so the harness can pick it up again. is work the harness can schedule and track. A saved place to pick up that work is a checkpointThe saved information a task can use to pick up its work after waiting or restarting.. A generation task talks to the model and can own the tasks that run the model’s tool calls. README.md

A tool result can take the answer into another turn

One submission, two generations
  • App
  • Harness
  • Storage
  • Model
  • App tool
  1. App → Harness · Submit input

    The application hands Pi a message

    It asks for a run and can wait for the submission to settle.

  2. Harness → Storage · Save input

    The run has a starting point

    In this example the app submits plain user input, recorded as a pi.user entry. A pi.generation task does the work of calling the model.

  3. Harness → Model · Generate

    The first generation asks the model

    The harness uses Pi AI to send the model context, including the tools the model can request.

  4. Model → Harness · Request a tool

    The model asks for a tool call

    The assistant response names the tool and supplies arguments. The assistant response is an entry in its own right.

  5. Harness → Storage · Save intent

    The generation owns a tool task

    Pi saves the intent before executing the call. The generation waits for its tool task.

  6. Harness → App tool · Execute

    The harness runs the tool

    In this example the application supplies the tool. Pi calls its function, which returns a result.

  7. Harness → Storage · Save result

    The result joins the transcript

    The pi.tool-result entry gives the next generation something to build on.

  8. Harness → Model · Generate again

    The model gets the tool result

    A new generation makes another model request with the updated context.

  9. Model → Harness · Answer

    The model returns an answer

    In this example the model answers without asking for another tool.

  10. Harness → Storage · Save answer

    The submission settles

    The final answer is a pi.assistant entry. The application can find the outcome through its submission.

App
Harness
Storage
Model
App tool

App → Harness

The application hands Pi a message

It asks for a run and can wait for the submission to settle.

Follow a run with one tool call. The first generation asks for the tool and the second uses its result.

A turnOne model response and any tool calls it asks the harness to make. starts with one model response and includes any tool calls it requests. The full sequence from input to final answer is a runThe sequence of turns that takes an input through to its final answer or another recorded outcome.. In the figure, the first turn obtains a fact and the second turns it into an answer. README.md

A tool can parkTo end a tool invocation with a saved wait for a time, a signal, or work the call owns. if it needs something that will arrive later. It can sleep until a time or wait for a signalA typed value delivered under a key to a live task, such as a person’s answer to an approval request., a typed value delivered to a task under a key. When the call wakes, it receives the reason and the checkpoint it saved when it parked. It can use those to finish the call even though the original invocation ended. README.md

An extensionA named bundle of additions to the harness, such as tools, prompt sections, hooks, or tasks. is a named bundle that can add tools, instructions, tasks, or callbacks. Pi calls those callbacks hooksAn extension callback that can observe or influence a particular stage of a built-in task. when they let an extension observe or influence a built-in task. The process installs the definitions in its registryThe collection of extension definitions installed in the running process., and a saved conversation can name the extensions it uses. What Pipal adds to Pi picks up that seam and shows where Pipal’s policy enters. README.md

Deep diveHow a signal and a watch carry different kinds of updates

A signal targets a live task and a key. A value that arrives before the task sleeps on that key can be kept and picked up as soon as the task sleeps. The wake consumes the key, so a second wait uses another key. Pi itself uses SubmissionSettled to deliver a submission’s outcome to a task waiting for it. README.md

A watch serves readers who care about the current view of a conversation. It starts with that view and can fold changes that arrive while its reader is busy. The transcript entries in the view are immutable, while a document such as pi.live can change as work runs. A watch can also see deferred progress before storage gets it. Stopping the watch releases the subscription, and a later watch starts from the view then available. README.md

Now put a failure between the first model request and the answer. Which of those records can the next process pick up, and which work will it have to try again? Durable by default follows the same machinery across a restart.

Recap

  • Pi Durable runs and records conversations. Pi AI talks to model providers. Pi Schema checks values, Chord supplies documents and context, Pi Codemode runs scripts, and Pi MCP connects the host to tool servers.
  • Entries record what happened. Documents hold changing state. Tasks keep track of what to do next.
  • A submission can take several turns, and a parked tool can wait for a signal with its place saved.
Check yourself
  1. The model asks to read a file with a tool supplied by the application. Who runs the call?
    • The model provider
    • The Pi Durable harness through a tool task
    • The conversation watch
    Answer and explanation

    The Pi Durable harness through a tool task

    The model supplies a tool name and arguments. The harness owns the task that runs the tool and records its result.

  2. You change the model to use for the next turn. Which kind of record holds that setting?
    • A transcript entry
    • A submission
    • A document
    Answer and explanation

    A document

    The model selection belongs to pi.agent, a document that can change without changing old entries.

  3. A conversation has been compacted. What happens to the original entries?
    • They stay in storage
    • They become tool tasks
    • They are replaced in storage by the summary
    Answer and explanation

    They stay in storage

    Pi adds the compacted summary for model context and keeps the original transcript records.

  1. The model asks to read a file with a tool supplied by the application. Who runs the call?

  2. You change the model to use for the next turn. Which kind of record holds that setting?

  3. A conversation has been compacted. What happens to the original entries?

In the source

Book contents

Search the book