# Pi adapter

Shows the sessions of Pi, the coding agent `pi` of Earendil, in the notch: working, done when a turn settles, needing you while a dialog of an extension waits for you, what the tool in use does, its first prompt, its repository and its branch as its name, and its tool calls as steps.
Pi has no hooks of the shell. It loads modules in TypeScript, its extensions, into its own process. Blossom installs one of its own, `~/.pi/agent/extensions/blossom.ts`, a copy of [`extension.ts`](extension.ts), which posts a few fields of each event to a Unix socket in your own folder, `~/Library/Application Support/Blossom/pi/socket`, where this adapter listens. Blossom launches the adapter.
**Pi asks for no permission, so nothing is answered from the notch.** A dialog that an extension of yours opens, a confirmation of a permission gate for example, makes the session need you until it closes; you answer it in Pi.

**This adapter was written from the documentation and the source of Pi and was never run against a real one.** What they say and what is only supposed is in [../../documentation/roadmap/pi.md](../../documentation/roadmap/pi.md); the checks at the end say what to look at. Why Blossom installs a module, and what the module may and may not do, is in [the decision](../../documentation/decisions/module.md).

You need:

- Blossom. It includes this adapter and the Bun that runs it; from a clone, `bun run build` makes one.
- Pi, `pi`, version 1.7.0 or later, the first to send `agent_settled`. The module follows the page [Extensions](https://docs.pi.dev/latest/extensions) and the source of [earendil-works/pi](https://github.com/earendil-works/pi) at commit `4fcd17e92adaf1595e206768dbbd2904b1147595`, its release 1.7.0.

## Connect from Blossom

The quickest way. Blossom finds `pi` in your `PATH` or in the usual folders, and connecting it is one click: "Continue" on the second step of the introduction, with Pi checked, or its switch in Settings > Agents. Blossom then adds this adapter to its configuration, starts it, and writes `~/.pi/agent/extensions/blossom.ts`, readable by you alone, creating the folders it needs. No file of yours is changed: your other extensions stay as they are.

"Repair" in the row writes the module again when it is missing, when you changed it or when an update of Blossom brings another one; it keeps a copy of the file it replaces in `~/Library/Application Support/Blossom/backups/pi/`. "Disconnect", shown while the agent is off, takes the adapter out of Blossom and deletes `blossom.ts`, after the same copy. A file named `blossom.ts` there that is not Blossom's, because it does not name the socket, is left alone and the row says so. Blossom does not follow `PI_CODING_AGENT_DIR`: a Pi that uses another folder does not load the module.

Pi loads its extensions when a session starts. A session that was open when you connected reports from its next start, or after `/reload`.

## Run Blossom with the adapter

From the root of the clone:

```sh
bun install && bun run bootstrap && bun run build
BLOSSOM_CONFIGURATION=adapters/pi/configuration.json apps/macos/build/Build/Products/Debug/Blossom.app/Contents/MacOS/Blossom
```

The notch shows nothing until a session of Pi reports. Run one Blossom at a time: the adapter holds the socket, and the adapter of a second Blossom logs `cannot listen for the extension of pi` and exits.

### From Finder

An app opened from Finder reads `~/Library/Application Support/Blossom/configuration.json`. Name the adapter by its package:

```json
{ "adapters": [{ "package": "pi" }] }
```

## Install by hand

For a clone without the app:

```sh
mkdir -p ~/.pi/agent/extensions
cp adapters/pi/extension.ts ~/.pi/agent/extensions/blossom.ts
```

To try it without touching your folder, load it for one run only: `pi -e "<absolute path of the clone>/adapters/pi/extension.ts"`.

## Remove by hand

After an install by hand; after connecting from Blossom, Disconnect does it, and removing Blossom deletes the module, see [Removing it](../../documentation/privacy.md#removing-it).

```sh
rm ~/.pi/agent/extensions/blossom.ts
```

## What to know

- **What the module does.** It listens to `input`, `agent_start`, `tool_execution_start`, `tool_execution_end`, `ui_prompt_start`, `ui_prompt_end`, `agent_settled` and `session_shutdown`, and to nothing else: not to `tool_call` or `tool_result`, whose handlers can block or change a call. Every handler returns at once with nothing, and the posts go one after the other in the background, one second each at most. With Blossom closed, every post fails at once and Pi notices nothing. It imports `node:http` alone, reads no file and logs nothing.
- **What it sends.** The identifier of the session, its folder and the process of Pi with every event; the first 4096 characters of a prompt; the identifier of a call, the name of its tool, `path` of its arguments and whether it failed; the reason Pi shuts a session down. Never a command, the content of a file, what a tool printed or an answer of the model. The adapter keeps the title, taken from the first line of the first prompt, the name of the tool and the words of its step, the folder, and drops the rest.
- **A session shows up with its first prompt.** `input` starts a session, and so does `agent_start`, for a prompt sent without one. The title is the first line of the first prompt, cut to 60 characters; a prompt that starts with `/` or `!` titles nothing.
- **Steps are tool calls.** Each call is a step from its `tool_execution_start` to its `tool_execution_end`, paired by its `toolCallId`, failed when `isError` is set. `read`, `edit` and `write` show the name of their file without its folder; `bash` shows "Running a command", since its input has no description; the others a label. A step still running when the turn settles is shown failed.
- **How a turn ends.** `agent_settled`, which Pi sends once it will not continue by itself, is done, aborted or not. `agent_end` is not used: Pi may retry or continue after it.
- **When it needs you.** Between `ui_prompt_start` and `ui_prompt_end`, which Pi sends around a dialog an extension opens with `ctx.ui`. Pi itself asks nothing: it has no permissions.
- **How a session ends.** `session_shutdown` closes it, unless its reason is `reload`, after which the same session starts again. `/new`, `/resume` and `/fork` shut the session down and start another. A session whose `pi` was killed leaves within five seconds: the module sends the process of Pi itself, and the adapter asks the system whether it still exists.
- **The terminal of a session.** With each prompt, the module sends the variables a terminal sets, such as `TERM_PROGRAM`, and the processes of Pi and of its parent; the adapter reads the terminal device of Pi with `/bin/ps` and checks every value as it checks those of a hook. Blossom uses it only to bring that terminal to the front when you ask.
- **The socket is yours alone.** A process that runs as you can still post to it, or listen on it while Blossom is closed and read what the module sends. See [../../documentation/decisions/socket.md](../../documentation/decisions/socket.md).
- **Sixty-four sessions at most.** When a sixty-fifth starts, the adapter closes the one it heard from least recently.

### Not confirmed

Read in the documentation and the source, never seen in a run:

1. That Pi loads `~/.pi/agent/extensions/blossom.ts` with `jiti`, from the Node of npm and from the binary built with Bun, and that `node:http` reaches a Unix socket in both.
2. That `ctx.sessionManager.getSessionId()` and `ctx.cwd` answer in every handler, the one of `session_shutdown` included.
3. That `tool_execution_start` carries the arguments of the call with `path` for `read`, `edit` and `write`.
4. That `agent_settled` comes once per prompt, after any retry.
5. That the module adds no wait a person can notice to a turn.

## Checks by hand

The module was loaded into a double of the API of extensions of Pi, written from its source, and its posts reached the adapter through a real socket; it never ran inside Pi. Each check below needs `pi` signed in.

1. **It loads.** Connect Pi from Blossom and start `pi` in a new folder. *Does it start with no error about an extension?*
2. **A turn.** Ask `hi`. *Does the tile appear with the prompt, titled `hi` and named after the folder, with no badge, and turn green when the answer ends?*
3. **A tool.** Ask `read the file package.json`. *Does detail list `Read package.json` as a step?*
4. **A dialog.** With an extension that asks for a confirmation, such as the example `permission-gate.ts` of Pi, ask for `rm -rf build`. *Does the tile turn amber while the dialog waits, and go back once you answer?*
5. **A reload.** Run `/reload`. *Does the tile stay?* Run `/new`. *Does the old tile leave?*
6. **The end.** Quit `pi`. *Does the tile leave?* Start it, ask `hi` and `kill -9` it from another terminal. *Does the tile leave within about five seconds?*
7. **Without Blossom.** Quit Blossom and ask `hi`. *Does Pi answer as before, with nothing printed about an extension?*
8. **The terminal.** In Terminal or iTerm2, ask `hi`, open detail and go to the terminal. *Does the right tab come forward?*
9. **Disconnect.** Disconnect Pi in Settings > Agents. *Is `~/.pi/agent/extensions/blossom.ts` gone, and your other extensions still there?*
