@oh-my-pi/pi-coding-agent 12.7.5 → 12.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -37
- package/README.md +9 -1052
- package/package.json +7 -7
- package/src/cli/args.ts +1 -0
- package/src/cli/update-cli.ts +49 -35
- package/src/cli/web-search-cli.ts +3 -2
- package/src/commands/web-search.ts +1 -0
- package/src/config/model-registry.ts +6 -0
- package/src/config/model-resolver.ts +2 -0
- package/src/config/settings-schema.ts +25 -3
- package/src/config/settings.ts +1 -0
- package/src/extensibility/extensions/wrapper.ts +20 -13
- package/src/extensibility/slash-commands.ts +12 -91
- package/src/lsp/client.ts +24 -27
- package/src/lsp/index.ts +92 -42
- package/src/mcp/config-writer.ts +33 -0
- package/src/mcp/config.ts +6 -1
- package/src/mcp/types.ts +1 -0
- package/src/modes/components/custom-editor.ts +8 -5
- package/src/modes/components/settings-defs.ts +2 -1
- package/src/modes/controllers/command-controller.ts +12 -6
- package/src/modes/controllers/input-controller.ts +21 -186
- package/src/modes/controllers/mcp-command-controller.ts +60 -3
- package/src/modes/interactive-mode.ts +2 -2
- package/src/modes/types.ts +1 -1
- package/src/sdk.ts +23 -1
- package/src/secrets/index.ts +116 -0
- package/src/secrets/obfuscator.ts +269 -0
- package/src/secrets/regex.ts +21 -0
- package/src/session/agent-session.ts +143 -21
- package/src/session/compaction/branch-summarization.ts +2 -2
- package/src/session/compaction/compaction.ts +10 -3
- package/src/session/compaction/utils.ts +25 -1
- package/src/slash-commands/builtin-registry.ts +419 -0
- package/src/web/scrapers/github.ts +50 -12
- package/src/web/search/index.ts +5 -5
- package/src/web/search/provider.ts +13 -2
- package/src/web/search/providers/brave.ts +165 -0
- package/src/web/search/types.ts +1 -1
- package/docs/compaction.md +0 -436
- package/docs/config-usage.md +0 -176
- package/docs/custom-tools.md +0 -585
- package/docs/environment-variables.md +0 -257
- package/docs/extension-loading.md +0 -106
- package/docs/extensions.md +0 -1342
- package/docs/fs-scan-cache-architecture.md +0 -50
- package/docs/hooks.md +0 -906
- package/docs/models.md +0 -234
- package/docs/python-repl.md +0 -110
- package/docs/rpc.md +0 -1173
- package/docs/sdk.md +0 -1039
- package/docs/session-tree-plan.md +0 -84
- package/docs/session.md +0 -368
- package/docs/skills.md +0 -254
- package/docs/theme.md +0 -696
- package/docs/tree.md +0 -206
- package/docs/tui.md +0 -487
package/docs/extensions.md
DELETED
|
@@ -1,1342 +0,0 @@
|
|
|
1
|
-
> omp can create extensions. Ask it to build one for your use case.
|
|
2
|
-
|
|
3
|
-
# Extensions
|
|
4
|
-
|
|
5
|
-
Extensions are TypeScript modules that extend omp's behavior. They can subscribe to lifecycle events, register custom tools callable by the LLM, add commands, and more.
|
|
6
|
-
|
|
7
|
-
**Key capabilities:**
|
|
8
|
-
|
|
9
|
-
- **Custom tools** - Register tools the LLM can call via `pi.registerTool()`
|
|
10
|
-
- **Event interception** - Block or modify tool calls, inject context, customize compaction
|
|
11
|
-
- **User interaction** - Prompt users via `ctx.ui` (select, confirm, input, notify)
|
|
12
|
-
- **Custom UI components** - Full TUI components with keyboard input via `ctx.ui.custom()` for complex interactions
|
|
13
|
-
- **Custom commands** - Register commands like `/mycommand` via `pi.registerCommand()`
|
|
14
|
-
- **Session persistence** - Store state that survives restarts via `pi.appendEntry()`
|
|
15
|
-
- **Custom rendering** - Control how tool calls/results and messages appear in TUI
|
|
16
|
-
|
|
17
|
-
**Example use cases:**
|
|
18
|
-
|
|
19
|
-
- Permission gates (confirm before `rm -rf`, `sudo`, etc.)
|
|
20
|
-
- Git checkpointing (stash at each turn, restore on branch)
|
|
21
|
-
- Path protection (block writes to `.env`, `node_modules/`)
|
|
22
|
-
- Custom compaction (summarize conversation your way)
|
|
23
|
-
- Interactive tools (questions, wizards, custom dialogs)
|
|
24
|
-
- Stateful tools (todo lists, connection pools)
|
|
25
|
-
- External integrations (file watchers, webhooks, CI triggers)
|
|
26
|
-
|
|
27
|
-
See [examples/extensions/](../examples/extensions/) for working implementations.
|
|
28
|
-
|
|
29
|
-
## Table of Contents
|
|
30
|
-
|
|
31
|
-
- [Quick Start](#quick-start)
|
|
32
|
-
- [Extension Locations](#extension-locations)
|
|
33
|
-
- [Available Imports](#available-imports)
|
|
34
|
-
- [Writing an Extension](#writing-an-extension)
|
|
35
|
-
- [Extension Styles](#extension-styles)
|
|
36
|
-
- [Events](#events)
|
|
37
|
-
- [Lifecycle Overview](#lifecycle-overview)
|
|
38
|
-
- [Session Events](#session-events)
|
|
39
|
-
- [Agent Events](#agent-events)
|
|
40
|
-
- [Input Events](#input-events)
|
|
41
|
-
- [User Bash/Python Events](#user-bashpython-events)
|
|
42
|
-
- [Tool Events](#tool-events)
|
|
43
|
-
- [ExtensionContext](#extensioncontext)
|
|
44
|
-
- [ExtensionCommandContext](#extensioncommandcontext)
|
|
45
|
-
- [ExtensionAPI Methods](#extensionapi-methods)
|
|
46
|
-
- [State Management](#state-management)
|
|
47
|
-
- [Custom Tools](#custom-tools)
|
|
48
|
-
- [Custom UI](#custom-ui)
|
|
49
|
-
- [Error Handling](#error-handling)
|
|
50
|
-
- [Mode Behavior](#mode-behavior)
|
|
51
|
-
|
|
52
|
-
## Quick Start
|
|
53
|
-
|
|
54
|
-
Create `~/.omp/agent/extensions/my-extension.ts` (legacy alias: `~/.pi/agent/extensions/`):
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent";
|
|
58
|
-
import { Type } from "@sinclair/typebox";
|
|
59
|
-
|
|
60
|
-
export default function (pi: ExtensionAPI) {
|
|
61
|
-
// React to events
|
|
62
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
63
|
-
ctx.ui.notify("Extension loaded!", "info");
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
pi.on("tool_call", async (event, ctx) => {
|
|
67
|
-
if (event.toolName === "bash" && event.input.command?.includes("rm -rf")) {
|
|
68
|
-
const ok = await ctx.ui.confirm("Dangerous!", "Allow rm -rf?");
|
|
69
|
-
if (!ok) return { block: true, reason: "Blocked by user" };
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Register a custom tool
|
|
74
|
-
pi.registerTool({
|
|
75
|
-
name: "greet",
|
|
76
|
-
label: "Greet",
|
|
77
|
-
description: "Greet someone by name",
|
|
78
|
-
parameters: Type.Object({
|
|
79
|
-
name: Type.String({ description: "Name to greet" }),
|
|
80
|
-
}),
|
|
81
|
-
async execute(toolCallId, params, onUpdate, ctx, signal) {
|
|
82
|
-
return {
|
|
83
|
-
content: [{ type: "text", text: `Hello, ${params.name}!` }],
|
|
84
|
-
details: {},
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// Register a command
|
|
90
|
-
pi.registerCommand("hello", {
|
|
91
|
-
description: "Say hello",
|
|
92
|
-
handler: async (args, ctx) => {
|
|
93
|
-
ctx.ui.notify(`Hello ${args || "world"}!`, "info");
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Test with `--extension` (or `-e`) flag:
|
|
100
|
-
|
|
101
|
-
```bash
|
|
102
|
-
omp -e ./my-extension.ts
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
## Extension Locations
|
|
106
|
-
|
|
107
|
-
Extensions are auto-discovered from:
|
|
108
|
-
|
|
109
|
-
| Location | Scope |
|
|
110
|
-
| ---------------------------------------- | ---------------------------- |
|
|
111
|
-
| `~/.omp/agent/extensions/*.{ts,js}` | Global (all projects) |
|
|
112
|
-
| `~/.omp/agent/extensions/*/index.{ts,js}` | Global (subdirectory) |
|
|
113
|
-
| `.omp/extensions/*.{ts,js}` | Project-local |
|
|
114
|
-
| `.omp/extensions/*/index.{ts,js}` | Project-local (subdirectory) |
|
|
115
|
-
|
|
116
|
-
Legacy `.pi` directories are supported as aliases for the `.omp` paths above.
|
|
117
|
-
|
|
118
|
-
`settings.json` lives in `~/.omp/agent/settings.json` (user) or `.omp/settings.json` (project).
|
|
119
|
-
|
|
120
|
-
Additional paths via `settings.json`:
|
|
121
|
-
|
|
122
|
-
```json
|
|
123
|
-
{
|
|
124
|
-
"extensions": ["/path/to/extension.ts", "/path/to/extension/dir"]
|
|
125
|
-
}
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
**Discovery rules:**
|
|
129
|
-
|
|
130
|
-
1. **Direct files:** `extensions/*.ts` or `*.js` → loaded directly
|
|
131
|
-
2. **Subdirectory with index:** `extensions/myext/index.ts` or `index.js` → loaded as single extension
|
|
132
|
-
3. **Subdirectory with package.json:** `extensions/myext/package.json` with `"omp"` field (legacy `"pi"` supported) → loads declared paths
|
|
133
|
-
|
|
134
|
-
Discovery only recurses one level under `extensions/`. Deeper entry points must be listed in the manifest.
|
|
135
|
-
|
|
136
|
-
```
|
|
137
|
-
~/.omp/agent/extensions/
|
|
138
|
-
├── simple.ts # Direct file (auto-discovered)
|
|
139
|
-
├── my-tool/
|
|
140
|
-
│ └── index.ts # Subdirectory with index (auto-discovered)
|
|
141
|
-
└── my-extension-pack/
|
|
142
|
-
├── package.json # Declares multiple extensions
|
|
143
|
-
├── node_modules/ # Dependencies installed here
|
|
144
|
-
└── src/
|
|
145
|
-
├── safety-gates.ts # First extension
|
|
146
|
-
└── custom-tools.ts # Second extension
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
```json
|
|
150
|
-
// my-extension-pack/package.json
|
|
151
|
-
{
|
|
152
|
-
"name": "my-extension-pack",
|
|
153
|
-
"dependencies": {
|
|
154
|
-
"zod": "^3.0.0"
|
|
155
|
-
},
|
|
156
|
-
"omp": {
|
|
157
|
-
"extensions": ["./src/safety-gates.ts", "./src/custom-tools.ts"]
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
The `package.json` approach enables:
|
|
163
|
-
|
|
164
|
-
- Multiple extensions from one package
|
|
165
|
-
- Third-party dependencies resolved via Bun's module loader
|
|
166
|
-
- Nested source structure (no depth limit within the package)
|
|
167
|
-
- Deployment to and installation from npm
|
|
168
|
-
|
|
169
|
-
## Available Imports
|
|
170
|
-
|
|
171
|
-
| Package | Purpose |
|
|
172
|
-
| --------------------------- | ------------------------------------------------------------ |
|
|
173
|
-
| `@oh-my-pi/pi-coding-agent` | Extension types (`ExtensionAPI`, `ExtensionContext`, events) |
|
|
174
|
-
| `@sinclair/typebox` | Schema definitions for tool parameters |
|
|
175
|
-
| `@oh-my-pi/pi-ai` | AI utilities (`StringEnum` for Google-compatible enums) |
|
|
176
|
-
| `@oh-my-pi/pi-tui` | TUI components for custom rendering |
|
|
177
|
-
|
|
178
|
-
`ExtensionAPI` also exposes:
|
|
179
|
-
|
|
180
|
-
- `pi.logger` - file logger (preferred over `console.*`)
|
|
181
|
-
- `pi.typebox` - injected TypeBox module
|
|
182
|
-
- `pi.pi` - access to `@oh-my-pi/pi-coding-agent` exports
|
|
183
|
-
|
|
184
|
-
Dependencies work like any Bun project. Add a `package.json` next to your extension (or in a parent directory), run `bun install`, and imports from `node_modules/` resolve automatically.
|
|
185
|
-
|
|
186
|
-
Node.js built-ins (`node:fs`, `node:path`, etc.) are also available.
|
|
187
|
-
|
|
188
|
-
## Writing an Extension
|
|
189
|
-
|
|
190
|
-
An extension exports a default function that receives `ExtensionAPI`:
|
|
191
|
-
|
|
192
|
-
```typescript
|
|
193
|
-
import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent";
|
|
194
|
-
|
|
195
|
-
export default function (pi: ExtensionAPI) {
|
|
196
|
-
// Subscribe to events
|
|
197
|
-
pi.on("event_name", async (event, ctx) => {
|
|
198
|
-
// ctx.ui for user interaction
|
|
199
|
-
const ok = await ctx.ui.confirm("Title", "Are you sure?");
|
|
200
|
-
ctx.ui.notify("Done!", "success");
|
|
201
|
-
ctx.ui.setStatus("my-ext", "Processing..."); // Footer status
|
|
202
|
-
ctx.ui.setWidget("my-ext", ["Line 1", "Line 2"]); // Widget above editor
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
// Register tools, commands, shortcuts, flags
|
|
206
|
-
pi.registerTool({ ... });
|
|
207
|
-
pi.registerCommand("name", { ... });
|
|
208
|
-
pi.registerShortcut("ctrl+x", { ... });
|
|
209
|
-
pi.registerFlag("--my-flag", { ... });
|
|
210
|
-
}
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
Extensions are loaded via Bun's native module loader, so TypeScript works without a build step. Both `.ts` and `.js` entry points are supported.
|
|
214
|
-
|
|
215
|
-
### Extension Styles
|
|
216
|
-
|
|
217
|
-
**Single file** - simplest, for small extensions (also supports `.js`):
|
|
218
|
-
|
|
219
|
-
```
|
|
220
|
-
~/.omp/agent/extensions/
|
|
221
|
-
└── my-extension.ts
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
**Directory with index.ts** - for multi-file extensions (also supports `index.js`):
|
|
225
|
-
|
|
226
|
-
```
|
|
227
|
-
~/.omp/agent/extensions/
|
|
228
|
-
└── my-extension/
|
|
229
|
-
├── index.ts # Entry point (exports default function)
|
|
230
|
-
├── tools.ts # Helper module
|
|
231
|
-
└── utils.ts # Helper module
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
**Package with dependencies** - for extensions that need npm packages:
|
|
235
|
-
|
|
236
|
-
```
|
|
237
|
-
~/.omp/agent/extensions/
|
|
238
|
-
└── my-extension/
|
|
239
|
-
├── package.json # Declares dependencies and entry points
|
|
240
|
-
├── bun.lockb
|
|
241
|
-
├── node_modules/ # After bun install
|
|
242
|
-
└── src/
|
|
243
|
-
└── index.ts
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
```json
|
|
247
|
-
// package.json
|
|
248
|
-
{
|
|
249
|
-
"name": "my-extension",
|
|
250
|
-
"dependencies": {
|
|
251
|
-
"zod": "^3.0.0",
|
|
252
|
-
"chalk": "^5.0.0"
|
|
253
|
-
},
|
|
254
|
-
"omp": {
|
|
255
|
-
"extensions": ["./src/index.ts"]
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
The manifest key can be `omp` (preferred) or `pi` (legacy).
|
|
261
|
-
|
|
262
|
-
Run `bun install` in the extension directory, then imports from `node_modules/` work automatically.
|
|
263
|
-
|
|
264
|
-
## Events
|
|
265
|
-
|
|
266
|
-
### Lifecycle Overview
|
|
267
|
-
|
|
268
|
-
```
|
|
269
|
-
omp starts
|
|
270
|
-
│
|
|
271
|
-
└─► session_start
|
|
272
|
-
│
|
|
273
|
-
▼
|
|
274
|
-
user submits input ────────────────────────────────────────┐
|
|
275
|
-
│ │
|
|
276
|
-
├─► input (can modify or handle) │
|
|
277
|
-
├─► before_agent_start (can inject message, modify system prompt)
|
|
278
|
-
├─► agent_start │
|
|
279
|
-
│ │
|
|
280
|
-
│ ┌─── turn (repeats while LLM calls tools) ───┐ │
|
|
281
|
-
│ │ │ │
|
|
282
|
-
│ ├─► turn_start │ │
|
|
283
|
-
│ ├─► context (can modify messages) │ │
|
|
284
|
-
│ │ │ │
|
|
285
|
-
│ │ LLM responds, may call tools: │ │
|
|
286
|
-
│ │ ├─► tool_call (can block) │ │
|
|
287
|
-
│ │ │ tool executes │ │
|
|
288
|
-
│ │ └─► tool_result (can modify) │ │
|
|
289
|
-
│ │ │ │
|
|
290
|
-
│ └─► turn_end │ │
|
|
291
|
-
│ │
|
|
292
|
-
└─► agent_end │
|
|
293
|
-
│
|
|
294
|
-
user sends another prompt ◄────────────────────────────────┘
|
|
295
|
-
|
|
296
|
-
/new (new session) or /resume (switch session)
|
|
297
|
-
├─► session_before_switch (can cancel)
|
|
298
|
-
└─► session_switch
|
|
299
|
-
|
|
300
|
-
/branch
|
|
301
|
-
├─► session_before_branch (can cancel)
|
|
302
|
-
└─► session_branch
|
|
303
|
-
|
|
304
|
-
/compact or auto-compaction
|
|
305
|
-
├─► session_before_compact (can cancel or customize)
|
|
306
|
-
├─► session.compacting (add context or override prompt)
|
|
307
|
-
└─► session_compact
|
|
308
|
-
|
|
309
|
-
/tree navigation
|
|
310
|
-
├─► session_before_tree (can cancel or customize)
|
|
311
|
-
└─► session_tree
|
|
312
|
-
|
|
313
|
-
exit (Ctrl+C, Ctrl+D)
|
|
314
|
-
└─► session_shutdown
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
### Session Events
|
|
318
|
-
|
|
319
|
-
#### session_start
|
|
320
|
-
|
|
321
|
-
Fired on initial session load.
|
|
322
|
-
|
|
323
|
-
```typescript
|
|
324
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
325
|
-
ctx.ui.notify(`Session: ${ctx.sessionManager.getSessionFile() ?? "ephemeral"}`, "info");
|
|
326
|
-
});
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
**Examples:** [todo.ts](../examples/extensions/todo.ts), [tools.ts](../examples/extensions/tools.ts)
|
|
330
|
-
|
|
331
|
-
#### session_before_switch / session_switch
|
|
332
|
-
|
|
333
|
-
Fired when starting a new session (`/new`), resuming (`/resume`), or forking a session.
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
pi.on("session_before_switch", async (event, ctx) => {
|
|
337
|
-
// event.reason - "new", "resume", or "fork"
|
|
338
|
-
// event.targetSessionFile - session we're switching to ("resume" only)
|
|
339
|
-
|
|
340
|
-
if (event.reason === "new") {
|
|
341
|
-
const ok = await ctx.ui.confirm("Clear?", "Delete all messages?");
|
|
342
|
-
if (!ok) return { cancel: true };
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
pi.on("session_switch", async (event, ctx) => {
|
|
347
|
-
// event.reason - "new", "resume", or "fork"
|
|
348
|
-
// event.previousSessionFile - session we came from
|
|
349
|
-
});
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
**Examples:** [todo.ts](../examples/extensions/todo.ts)
|
|
353
|
-
|
|
354
|
-
#### session_before_branch / session_branch
|
|
355
|
-
|
|
356
|
-
Fired when branching via `/branch`.
|
|
357
|
-
|
|
358
|
-
```typescript
|
|
359
|
-
pi.on("session_before_branch", async (event, ctx) => {
|
|
360
|
-
// event.entryId - ID of the entry being branched from
|
|
361
|
-
return { cancel: true }; // Cancel branch
|
|
362
|
-
// OR
|
|
363
|
-
return { skipConversationRestore: true }; // Branch but don't rewind messages
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
pi.on("session_branch", async (event, ctx) => {
|
|
367
|
-
// event.previousSessionFile - previous session file
|
|
368
|
-
});
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
**Examples:** [todo.ts](../examples/extensions/todo.ts), [tools.ts](../examples/extensions/tools.ts)
|
|
372
|
-
|
|
373
|
-
#### session_before_compact / session_compact
|
|
374
|
-
|
|
375
|
-
Fired on compaction. See [compaction.md](compaction.md) for details.
|
|
376
|
-
|
|
377
|
-
```typescript
|
|
378
|
-
pi.on("session_before_compact", async (event, ctx) => {
|
|
379
|
-
const { preparation, branchEntries, customInstructions, signal } = event;
|
|
380
|
-
|
|
381
|
-
// Cancel:
|
|
382
|
-
return { cancel: true };
|
|
383
|
-
|
|
384
|
-
// Custom summary:
|
|
385
|
-
return {
|
|
386
|
-
compaction: {
|
|
387
|
-
summary: "...",
|
|
388
|
-
firstKeptEntryId: preparation.firstKeptEntryId,
|
|
389
|
-
tokensBefore: preparation.tokensBefore,
|
|
390
|
-
},
|
|
391
|
-
};
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
pi.on("session_compact", async (event, ctx) => {
|
|
395
|
-
// event.compactionEntry - the saved compaction
|
|
396
|
-
// event.fromExtension - whether extension provided it
|
|
397
|
-
});
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
#### session.compacting
|
|
402
|
-
|
|
403
|
-
Fired before compaction summarization to adjust the prompt or inject extra context.
|
|
404
|
-
|
|
405
|
-
```typescript
|
|
406
|
-
pi.on("session.compacting", async (event, ctx) => {
|
|
407
|
-
// event.messages - messages being summarized
|
|
408
|
-
return {
|
|
409
|
-
context: ["Important context line"],
|
|
410
|
-
prompt: "Summarize with an emphasis on decisions and follow-ups",
|
|
411
|
-
preserveData: { ticketId: "ABC-123" },
|
|
412
|
-
};
|
|
413
|
-
});
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
#### session_before_tree / session_tree
|
|
417
|
-
|
|
418
|
-
Fired on `/tree` navigation.
|
|
419
|
-
|
|
420
|
-
```typescript
|
|
421
|
-
pi.on("session_before_tree", async (event, ctx) => {
|
|
422
|
-
const { preparation, signal } = event;
|
|
423
|
-
return { cancel: true };
|
|
424
|
-
// OR provide custom summary:
|
|
425
|
-
return { summary: { summary: "...", details: {} } };
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
pi.on("session_tree", async (event, ctx) => {
|
|
429
|
-
// event.newLeafId, oldLeafId, summaryEntry, fromExtension
|
|
430
|
-
});
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
**Examples:** [tools.ts](../examples/extensions/tools.ts)
|
|
434
|
-
|
|
435
|
-
#### session_shutdown
|
|
436
|
-
|
|
437
|
-
Fired on exit (Ctrl+C, Ctrl+D, SIGTERM).
|
|
438
|
-
|
|
439
|
-
```typescript
|
|
440
|
-
pi.on("session_shutdown", async (_event, ctx) => {
|
|
441
|
-
// Cleanup, save state, etc.
|
|
442
|
-
});
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
### Agent Events
|
|
446
|
-
|
|
447
|
-
#### before_agent_start
|
|
448
|
-
|
|
449
|
-
Fired after user submits prompt, before agent loop. Can inject a message and/or modify the system prompt.
|
|
450
|
-
|
|
451
|
-
```typescript
|
|
452
|
-
pi.on("before_agent_start", async (event, ctx) => {
|
|
453
|
-
// event.prompt - user's prompt text
|
|
454
|
-
// event.images - attached images (if any)
|
|
455
|
-
// event.systemPrompt - current system prompt
|
|
456
|
-
|
|
457
|
-
return {
|
|
458
|
-
// Inject a persistent message (stored in session, sent to LLM)
|
|
459
|
-
message: {
|
|
460
|
-
customType: "my-extension",
|
|
461
|
-
content: "Additional context for the LLM",
|
|
462
|
-
display: true,
|
|
463
|
-
},
|
|
464
|
-
// Replace the system prompt for this turn (chained across extensions)
|
|
465
|
-
systemPrompt: event.systemPrompt + "\n\nExtra instructions for this turn...",
|
|
466
|
-
};
|
|
467
|
-
});
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
**Examples:** [pirate.ts](../examples/extensions/pirate.ts), [plan-mode.ts](../examples/extensions/plan-mode.ts)
|
|
471
|
-
|
|
472
|
-
#### agent_start / agent_end
|
|
473
|
-
|
|
474
|
-
Fired once per user prompt.
|
|
475
|
-
|
|
476
|
-
```typescript
|
|
477
|
-
pi.on("agent_start", async (_event, ctx) => {});
|
|
478
|
-
|
|
479
|
-
pi.on("agent_end", async (event, ctx) => {
|
|
480
|
-
// event.messages - messages from this prompt
|
|
481
|
-
});
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
**Examples:** [chalk-logger.ts](../examples/extensions/chalk-logger.ts), [plan-mode.ts](../examples/extensions/plan-mode.ts)
|
|
485
|
-
|
|
486
|
-
#### turn_start / turn_end
|
|
487
|
-
|
|
488
|
-
Fired for each turn (one LLM response + tool calls).
|
|
489
|
-
|
|
490
|
-
```typescript
|
|
491
|
-
pi.on("turn_start", async (event, ctx) => {
|
|
492
|
-
// event.turnIndex, event.timestamp
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
pi.on("turn_end", async (event, ctx) => {
|
|
496
|
-
// event.turnIndex, event.message, event.toolResults
|
|
497
|
-
});
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
**Examples:** [plan-mode.ts](../examples/extensions/plan-mode.ts)
|
|
501
|
-
|
|
502
|
-
#### Runtime reliability events
|
|
503
|
-
|
|
504
|
-
Fired for internal recovery/continuation mechanics:
|
|
505
|
-
|
|
506
|
-
- `auto_compaction_start` / `auto_compaction_end`
|
|
507
|
-
- `auto_retry_start` / `auto_retry_end`
|
|
508
|
-
- `ttsr_triggered`
|
|
509
|
-
- `todo_reminder`
|
|
510
|
-
|
|
511
|
-
```typescript
|
|
512
|
-
pi.on("todo_reminder", async (event, _ctx) => {
|
|
513
|
-
// event.todos, event.attempt, event.maxAttempts
|
|
514
|
-
});
|
|
515
|
-
|
|
516
|
-
pi.on("auto_retry_start", async (event, _ctx) => {
|
|
517
|
-
// event.attempt, event.maxAttempts, event.delayMs, event.errorMessage
|
|
518
|
-
});
|
|
519
|
-
```
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
#### context
|
|
523
|
-
|
|
524
|
-
Fired before each LLM call. Modify messages non-destructively.
|
|
525
|
-
|
|
526
|
-
```typescript
|
|
527
|
-
pi.on("context", async (event, ctx) => {
|
|
528
|
-
// event.messages - deep copy, safe to modify
|
|
529
|
-
const filtered = event.messages.filter((m) => !shouldPrune(m));
|
|
530
|
-
return { messages: filtered };
|
|
531
|
-
});
|
|
532
|
-
```
|
|
533
|
-
|
|
534
|
-
### Input Events
|
|
535
|
-
|
|
536
|
-
#### input
|
|
537
|
-
|
|
538
|
-
Fired when the user submits input (interactive, RPC, or extension-triggered). Can rewrite or handle input.
|
|
539
|
-
|
|
540
|
-
```typescript
|
|
541
|
-
pi.on("input", async (event, ctx) => {
|
|
542
|
-
// event.text, event.images, event.source
|
|
543
|
-
if (event.text.startsWith("/noop")) {
|
|
544
|
-
return { handled: true };
|
|
545
|
-
}
|
|
546
|
-
return { text: event.text.trim() };
|
|
547
|
-
});
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
### User Bash/Python Events
|
|
551
|
-
|
|
552
|
-
#### user_bash
|
|
553
|
-
|
|
554
|
-
Fired when the user runs a `!`/`!!` command. Return a `result` to override execution.
|
|
555
|
-
|
|
556
|
-
```typescript
|
|
557
|
-
pi.on("user_bash", async (event, ctx) => {
|
|
558
|
-
// event.command, event.excludeFromContext, event.cwd
|
|
559
|
-
if (event.command === "pwd") {
|
|
560
|
-
return {
|
|
561
|
-
result: {
|
|
562
|
-
stdout: event.cwd,
|
|
563
|
-
stderr: "",
|
|
564
|
-
code: 0,
|
|
565
|
-
killed: false,
|
|
566
|
-
},
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
});
|
|
570
|
-
```
|
|
571
|
-
|
|
572
|
-
#### user_python
|
|
573
|
-
|
|
574
|
-
Fired when the user runs a `$`/`$$` block. Return a `result` to override execution.
|
|
575
|
-
|
|
576
|
-
```typescript
|
|
577
|
-
pi.on("user_python", async (event, ctx) => {
|
|
578
|
-
// event.code, event.excludeFromContext, event.cwd
|
|
579
|
-
});
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
### Tool Events
|
|
583
|
-
|
|
584
|
-
#### tool_call
|
|
585
|
-
|
|
586
|
-
Fired before tool executes. **Can block.**
|
|
587
|
-
|
|
588
|
-
```typescript
|
|
589
|
-
pi.on("tool_call", async (event, ctx) => {
|
|
590
|
-
// event.toolName - "bash", "read", "write", "edit", etc.
|
|
591
|
-
// event.toolCallId
|
|
592
|
-
// event.input - tool parameters
|
|
593
|
-
|
|
594
|
-
if (shouldBlock(event)) {
|
|
595
|
-
return { block: true, reason: "Not allowed" };
|
|
596
|
-
}
|
|
597
|
-
});
|
|
598
|
-
```
|
|
599
|
-
|
|
600
|
-
**Examples:** [chalk-logger.ts](../examples/extensions/chalk-logger.ts), [plan-mode.ts](../examples/extensions/plan-mode.ts)
|
|
601
|
-
|
|
602
|
-
#### tool_result
|
|
603
|
-
|
|
604
|
-
Fired after tool executes. **Can modify result.**
|
|
605
|
-
|
|
606
|
-
`tool_result` handlers chain like middleware:
|
|
607
|
-
- Handlers run in extension load order
|
|
608
|
-
- Each handler sees the latest result after previous handler changes
|
|
609
|
-
- Handlers can return partial patches (`content`, `details`, or `isError`); omitted fields keep their current values
|
|
610
|
-
|
|
611
|
-
```typescript
|
|
612
|
-
pi.on("tool_result", async (event, ctx) => {
|
|
613
|
-
// event.toolName, event.toolCallId, event.input
|
|
614
|
-
// event.content, event.details, event.isError
|
|
615
|
-
|
|
616
|
-
if (event.toolName === "bash") {
|
|
617
|
-
// event.details is typed as BashToolDetails
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// Modify result:
|
|
621
|
-
return { content: [...], details: {...}, isError: false };
|
|
622
|
-
});
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
**Examples:** [plan-mode.ts](../examples/extensions/plan-mode.ts)
|
|
626
|
-
|
|
627
|
-
## ExtensionContext
|
|
628
|
-
|
|
629
|
-
Every handler receives `ctx: ExtensionContext`:
|
|
630
|
-
|
|
631
|
-
### ctx.ui
|
|
632
|
-
|
|
633
|
-
UI methods for user interaction. See [Custom UI](#custom-ui) for full details.
|
|
634
|
-
|
|
635
|
-
### ctx.hasUI
|
|
636
|
-
|
|
637
|
-
`false` in print mode (`-p`) and JSON mode. `true` in interactive and RPC mode. In RPC mode, dialog methods (`select`, `confirm`, `input`, `editor`) work via the extension UI sub-protocol, and fire-and-forget methods (`notify`, `setStatus`, `setWidget`, `setTitle`, `setEditorText`) emit requests to the client. Some TUI-specific methods are no-ops or return defaults (see [rpc.md](rpc.md#extension-ui-protocol)).
|
|
638
|
-
|
|
639
|
-
### ctx.cwd
|
|
640
|
-
|
|
641
|
-
Current working directory.
|
|
642
|
-
|
|
643
|
-
### ctx.sessionManager
|
|
644
|
-
|
|
645
|
-
Read-only access to session state:
|
|
646
|
-
|
|
647
|
-
```typescript
|
|
648
|
-
ctx.sessionManager.getEntries(); // All entries
|
|
649
|
-
ctx.sessionManager.getBranch(); // Current branch
|
|
650
|
-
ctx.sessionManager.getLeafId(); // Current leaf entry ID
|
|
651
|
-
```
|
|
652
|
-
|
|
653
|
-
### ctx.modelRegistry / ctx.model
|
|
654
|
-
|
|
655
|
-
Access to models and API keys.
|
|
656
|
-
|
|
657
|
-
### ctx.getContextUsage()
|
|
658
|
-
|
|
659
|
-
Returns current context usage for the active model, if available.
|
|
660
|
-
|
|
661
|
-
### ctx.compact(instructionsOrOptions?)
|
|
662
|
-
|
|
663
|
-
Trigger compaction programmatically (interactive mode shows UI).
|
|
664
|
-
|
|
665
|
-
### ctx.shutdown()
|
|
666
|
-
|
|
667
|
-
Gracefully shut down and exit.
|
|
668
|
-
|
|
669
|
-
### ctx.isIdle() / ctx.abort() / ctx.hasPendingMessages()
|
|
670
|
-
|
|
671
|
-
Control flow helpers.
|
|
672
|
-
|
|
673
|
-
## ExtensionCommandContext
|
|
674
|
-
|
|
675
|
-
Command handlers receive `ExtensionCommandContext`, which extends `ExtensionContext` with session control methods. These are only available in commands because they can deadlock if called from event handlers.
|
|
676
|
-
|
|
677
|
-
### ctx.waitForIdle()
|
|
678
|
-
|
|
679
|
-
Wait for the agent to finish streaming:
|
|
680
|
-
|
|
681
|
-
```typescript
|
|
682
|
-
pi.registerCommand("my-cmd", {
|
|
683
|
-
handler: async (args, ctx) => {
|
|
684
|
-
await ctx.waitForIdle();
|
|
685
|
-
// Agent is now idle, safe to modify session
|
|
686
|
-
},
|
|
687
|
-
});
|
|
688
|
-
```
|
|
689
|
-
|
|
690
|
-
### ctx.newSession(options?)
|
|
691
|
-
|
|
692
|
-
Create a new session:
|
|
693
|
-
|
|
694
|
-
```typescript
|
|
695
|
-
const result = await ctx.newSession({
|
|
696
|
-
parentSession: ctx.sessionManager.getSessionFile(),
|
|
697
|
-
setup: async (sm) => {
|
|
698
|
-
sm.appendMessage({
|
|
699
|
-
role: "user",
|
|
700
|
-
content: [{ type: "text", text: "Context from previous session..." }],
|
|
701
|
-
timestamp: Date.now(),
|
|
702
|
-
});
|
|
703
|
-
},
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
if (result.cancelled) {
|
|
707
|
-
// An extension cancelled the new session
|
|
708
|
-
}
|
|
709
|
-
```
|
|
710
|
-
|
|
711
|
-
### ctx.branch(entryId)
|
|
712
|
-
|
|
713
|
-
Branch from a specific entry:
|
|
714
|
-
|
|
715
|
-
```typescript
|
|
716
|
-
const result = await ctx.branch("entry-id-123");
|
|
717
|
-
if (!result.cancelled) {
|
|
718
|
-
// Now in the branched session
|
|
719
|
-
}
|
|
720
|
-
```
|
|
721
|
-
|
|
722
|
-
### ctx.navigateTree(targetId, options?)
|
|
723
|
-
|
|
724
|
-
Navigate to a different point in the session tree:
|
|
725
|
-
|
|
726
|
-
```typescript
|
|
727
|
-
const result = await ctx.navigateTree("entry-id-456", {
|
|
728
|
-
summarize: true,
|
|
729
|
-
});
|
|
730
|
-
```
|
|
731
|
-
|
|
732
|
-
### ctx.reload()
|
|
733
|
-
|
|
734
|
-
Run the same reload flow as `/reload`.
|
|
735
|
-
|
|
736
|
-
```typescript
|
|
737
|
-
pi.registerCommand("reload-runtime", {
|
|
738
|
-
description: "Reload extensions, skills, prompts, and themes",
|
|
739
|
-
handler: async (_args, ctx) => {
|
|
740
|
-
await ctx.reload();
|
|
741
|
-
return;
|
|
742
|
-
},
|
|
743
|
-
});
|
|
744
|
-
```
|
|
745
|
-
|
|
746
|
-
Important behavior:
|
|
747
|
-
- `await ctx.reload()` emits `session_shutdown` for the current extension runtime
|
|
748
|
-
- It then reloads resources and emits `session_start` (and `resources_discover` with reason `"reload"`) for the new runtime
|
|
749
|
-
- The currently running command handler still continues in the old call frame
|
|
750
|
-
- Code after `await ctx.reload()` still runs from the pre-reload version
|
|
751
|
-
- Code after `await ctx.reload()` must not assume old in-memory extension state is still valid
|
|
752
|
-
- After the handler returns, future commands/events/tool calls use the new extension version
|
|
753
|
-
|
|
754
|
-
For predictable behavior, treat reload as terminal for that handler (`await ctx.reload(); return;`).
|
|
755
|
-
|
|
756
|
-
Tools run with `ExtensionContext`, so they cannot call `ctx.reload()` directly. Use a command as the reload entrypoint, then expose a tool that queues that command as a follow-up user message.
|
|
757
|
-
|
|
758
|
-
Example tool the LLM can call to trigger reload:
|
|
759
|
-
|
|
760
|
-
```typescript
|
|
761
|
-
import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent";
|
|
762
|
-
import { Type } from "@sinclair/typebox";
|
|
763
|
-
|
|
764
|
-
export default function (pi: ExtensionAPI) {
|
|
765
|
-
pi.registerCommand("reload-runtime", {
|
|
766
|
-
description: "Reload extensions, skills, prompts, and themes",
|
|
767
|
-
handler: async (_args, ctx) => {
|
|
768
|
-
await ctx.reload();
|
|
769
|
-
return;
|
|
770
|
-
},
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
pi.registerTool({
|
|
774
|
-
name: "reload_runtime",
|
|
775
|
-
label: "Reload Runtime",
|
|
776
|
-
description: "Reload extensions, skills, prompts, and themes",
|
|
777
|
-
parameters: Type.Object({}),
|
|
778
|
-
async execute() {
|
|
779
|
-
pi.sendUserMessage("/reload-runtime", { deliverAs: "followUp" });
|
|
780
|
-
return {
|
|
781
|
-
content: [{ type: "text", text: "Queued /reload-runtime as a follow-up command." }],
|
|
782
|
-
};
|
|
783
|
-
},
|
|
784
|
-
});
|
|
785
|
-
}
|
|
786
|
-
```
|
|
787
|
-
|
|
788
|
-
## ExtensionAPI Methods
|
|
789
|
-
|
|
790
|
-
### pi.on(event, handler)
|
|
791
|
-
|
|
792
|
-
Subscribe to events. See [Events](#events).
|
|
793
|
-
|
|
794
|
-
### pi.registerTool(definition)
|
|
795
|
-
|
|
796
|
-
Register a custom tool callable by the LLM. See [Custom Tools](#custom-tools) for full details.
|
|
797
|
-
|
|
798
|
-
```typescript
|
|
799
|
-
import { Type } from "@sinclair/typebox";
|
|
800
|
-
import { StringEnum } from "@oh-my-pi/pi-ai";
|
|
801
|
-
|
|
802
|
-
pi.registerTool({
|
|
803
|
-
name: "my_tool",
|
|
804
|
-
label: "My Tool",
|
|
805
|
-
description: "What this tool does",
|
|
806
|
-
parameters: Type.Object({
|
|
807
|
-
action: StringEnum(["list", "add"] as const),
|
|
808
|
-
text: Type.Optional(Type.String()),
|
|
809
|
-
}),
|
|
810
|
-
|
|
811
|
-
async execute(toolCallId, params, onUpdate, ctx, signal) {
|
|
812
|
-
// Stream progress
|
|
813
|
-
onUpdate?.({ content: [{ type: "text", text: "Working..." }] });
|
|
814
|
-
|
|
815
|
-
return {
|
|
816
|
-
content: [{ type: "text", text: "Done" }],
|
|
817
|
-
details: { result: "..." },
|
|
818
|
-
};
|
|
819
|
-
},
|
|
820
|
-
|
|
821
|
-
// Optional: Custom rendering
|
|
822
|
-
renderCall(args, theme) { ... },
|
|
823
|
-
renderResult(result, options, theme, args) { ... },
|
|
824
|
-
});
|
|
825
|
-
```
|
|
826
|
-
|
|
827
|
-
### pi.sendMessage(message, options?)
|
|
828
|
-
|
|
829
|
-
Inject a message into the session:
|
|
830
|
-
|
|
831
|
-
```typescript
|
|
832
|
-
pi.sendMessage({
|
|
833
|
-
customType: "my-extension",
|
|
834
|
-
content: "Message text",
|
|
835
|
-
display: true,
|
|
836
|
-
details: { ... },
|
|
837
|
-
}, {
|
|
838
|
-
triggerTurn: true,
|
|
839
|
-
deliverAs: "steer",
|
|
840
|
-
});
|
|
841
|
-
```
|
|
842
|
-
|
|
843
|
-
**Options:**
|
|
844
|
-
|
|
845
|
-
- `deliverAs` - Delivery mode:
|
|
846
|
-
- `"steer"` (default) - Interrupts streaming. Delivered after current tool finishes, remaining tools skipped.
|
|
847
|
-
- `"followUp"` - Waits for agent to finish. Delivered only when agent has no more tool calls.
|
|
848
|
-
- `"nextTurn"` - Queued for next user prompt. Does not interrupt or trigger anything.
|
|
849
|
-
- `triggerTurn: true` - If agent is idle, trigger an LLM response immediately. Only applies to `"steer"` and `"followUp"` modes (ignored for `"nextTurn"`).
|
|
850
|
-
|
|
851
|
-
### pi.sendUserMessage(content, options?)
|
|
852
|
-
|
|
853
|
-
Send a user message into the session and trigger a turn immediately:
|
|
854
|
-
|
|
855
|
-
```typescript
|
|
856
|
-
pi.sendUserMessage("Follow up with the latest status", { deliverAs: "followUp" });
|
|
857
|
-
```
|
|
858
|
-
|
|
859
|
-
### pi.appendEntry(customType, data?)
|
|
860
|
-
|
|
861
|
-
Persist extension state (does NOT participate in LLM context):
|
|
862
|
-
|
|
863
|
-
```typescript
|
|
864
|
-
pi.appendEntry("my-state", { count: 42 });
|
|
865
|
-
|
|
866
|
-
// Restore on reload
|
|
867
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
868
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
869
|
-
if (entry.type === "custom" && entry.customType === "my-state") {
|
|
870
|
-
// Reconstruct from entry.data
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
});
|
|
874
|
-
```
|
|
875
|
-
|
|
876
|
-
### pi.registerCommand(name, options)
|
|
877
|
-
|
|
878
|
-
Register a command:
|
|
879
|
-
|
|
880
|
-
```typescript
|
|
881
|
-
pi.registerCommand("stats", {
|
|
882
|
-
description: "Show session statistics",
|
|
883
|
-
handler: async (args, ctx) => {
|
|
884
|
-
const count = ctx.sessionManager.getEntries().length;
|
|
885
|
-
ctx.ui.notify(`${count} entries`, "info");
|
|
886
|
-
},
|
|
887
|
-
});
|
|
888
|
-
```
|
|
889
|
-
### pi.registerProvider(name, config)
|
|
890
|
-
|
|
891
|
-
Register or override providers/models at runtime:
|
|
892
|
-
|
|
893
|
-
```typescript
|
|
894
|
-
pi.registerProvider("my-provider", {
|
|
895
|
-
baseUrl: "https://api.example.com/v1",
|
|
896
|
-
apiKey: "MY_PROVIDER_API_KEY",
|
|
897
|
-
api: "openai-completions",
|
|
898
|
-
models: [
|
|
899
|
-
{
|
|
900
|
-
id: "my-model",
|
|
901
|
-
name: "My Model",
|
|
902
|
-
reasoning: false,
|
|
903
|
-
input: ["text"],
|
|
904
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
905
|
-
contextWindow: 128000,
|
|
906
|
-
maxTokens: 8192,
|
|
907
|
-
},
|
|
908
|
-
],
|
|
909
|
-
});
|
|
910
|
-
```
|
|
911
|
-
|
|
912
|
-
`registerProvider()` also supports:
|
|
913
|
-
|
|
914
|
-
- `streamSimple` for custom API adapters
|
|
915
|
-
- `headers` / `authHeader` for request customization
|
|
916
|
-
- `oauth` for `/login <provider>` support with extension-defined login/refresh behavior
|
|
917
|
-
|
|
918
|
-
Provider registrations are queued during extension load and applied when the session initializes.
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
### pi.registerMessageRenderer(customType, renderer)
|
|
922
|
-
|
|
923
|
-
Register a custom TUI renderer for messages with your `customType`. See [Custom UI](#custom-ui).
|
|
924
|
-
|
|
925
|
-
### pi.registerShortcut(shortcut, options)
|
|
926
|
-
|
|
927
|
-
Register a keyboard shortcut:
|
|
928
|
-
|
|
929
|
-
```typescript
|
|
930
|
-
pi.registerShortcut("ctrl+shift+p", {
|
|
931
|
-
description: "Toggle plan mode",
|
|
932
|
-
handler: async (ctx) => {
|
|
933
|
-
ctx.ui.notify("Toggled!");
|
|
934
|
-
},
|
|
935
|
-
});
|
|
936
|
-
```
|
|
937
|
-
|
|
938
|
-
### pi.registerFlag(name, options)
|
|
939
|
-
|
|
940
|
-
Register a CLI flag:
|
|
941
|
-
|
|
942
|
-
```typescript
|
|
943
|
-
pi.registerFlag("--plan", {
|
|
944
|
-
description: "Start in plan mode",
|
|
945
|
-
type: "boolean",
|
|
946
|
-
default: false,
|
|
947
|
-
});
|
|
948
|
-
|
|
949
|
-
// Check value
|
|
950
|
-
if (pi.getFlag("--plan")) {
|
|
951
|
-
// Plan mode enabled
|
|
952
|
-
}
|
|
953
|
-
```
|
|
954
|
-
|
|
955
|
-
### pi.setLabel(label)
|
|
956
|
-
|
|
957
|
-
Set a display label for the extension:
|
|
958
|
-
|
|
959
|
-
```typescript
|
|
960
|
-
pi.setLabel("My Extension");
|
|
961
|
-
```
|
|
962
|
-
|
|
963
|
-
### pi.exec(command, args, options?)
|
|
964
|
-
|
|
965
|
-
Execute a shell command:
|
|
966
|
-
|
|
967
|
-
```typescript
|
|
968
|
-
const result = await pi.exec("git", ["status"], { signal, timeout: 5000 });
|
|
969
|
-
// result.stdout, result.stderr, result.code, result.killed
|
|
970
|
-
```
|
|
971
|
-
|
|
972
|
-
### pi.getActiveTools() / pi.getAllTools() / pi.setActiveTools(names)
|
|
973
|
-
|
|
974
|
-
Manage active tools:
|
|
975
|
-
|
|
976
|
-
```typescript
|
|
977
|
-
const active = pi.getActiveTools(); // ["read", "bash", "edit", "write"]
|
|
978
|
-
pi.setActiveTools(["read", "bash"]); // Switch to read-only
|
|
979
|
-
```
|
|
980
|
-
|
|
981
|
-
### pi.setModel(model) / pi.getThinkingLevel() / pi.setThinkingLevel(level)
|
|
982
|
-
|
|
983
|
-
Control the active model and thinking level:
|
|
984
|
-
|
|
985
|
-
```typescript
|
|
986
|
-
const model = ctx.modelRegistry.find("anthropic", "claude-sonnet-4-5");
|
|
987
|
-
if (model) {
|
|
988
|
-
const ok = await pi.setModel(model);
|
|
989
|
-
}
|
|
990
|
-
const level = pi.getThinkingLevel();
|
|
991
|
-
pi.setThinkingLevel(level);
|
|
992
|
-
```
|
|
993
|
-
|
|
994
|
-
### pi.events
|
|
995
|
-
|
|
996
|
-
Shared event bus for communication between extensions:
|
|
997
|
-
|
|
998
|
-
```typescript
|
|
999
|
-
pi.events.on("my:event", (data) => { ... });
|
|
1000
|
-
pi.events.emit("my:event", { ... });
|
|
1001
|
-
```
|
|
1002
|
-
|
|
1003
|
-
## State Management
|
|
1004
|
-
|
|
1005
|
-
Extensions with state should store it in tool result `details` for proper branching support. Tools can also implement `onSession` to rebuild or clean up state on start/switch/branch/tree/shutdown:
|
|
1006
|
-
|
|
1007
|
-
```typescript
|
|
1008
|
-
export default function (pi: ExtensionAPI) {
|
|
1009
|
-
let items: string[] = [];
|
|
1010
|
-
|
|
1011
|
-
// Reconstruct state from session
|
|
1012
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
1013
|
-
items = [];
|
|
1014
|
-
for (const entry of ctx.sessionManager.getBranch()) {
|
|
1015
|
-
if (entry.type === "message" && entry.message.role === "toolResult") {
|
|
1016
|
-
if (entry.message.toolName === "my_tool") {
|
|
1017
|
-
items = entry.message.details?.items ?? [];
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
});
|
|
1022
|
-
|
|
1023
|
-
pi.registerTool({
|
|
1024
|
-
name: "my_tool",
|
|
1025
|
-
// ...
|
|
1026
|
-
async execute(toolCallId, params, onUpdate, ctx, signal) {
|
|
1027
|
-
items.push("new item");
|
|
1028
|
-
return {
|
|
1029
|
-
content: [{ type: "text", text: "Added" }],
|
|
1030
|
-
details: { items: [...items] }, // Store for reconstruction
|
|
1031
|
-
};
|
|
1032
|
-
},
|
|
1033
|
-
});
|
|
1034
|
-
}
|
|
1035
|
-
```
|
|
1036
|
-
|
|
1037
|
-
## Custom Tools
|
|
1038
|
-
|
|
1039
|
-
Register tools the LLM can call via `pi.registerTool()`. Tools appear in the system prompt and can have custom rendering.
|
|
1040
|
-
|
|
1041
|
-
### Tool Definition
|
|
1042
|
-
|
|
1043
|
-
```typescript
|
|
1044
|
-
import { Type } from "@sinclair/typebox";
|
|
1045
|
-
import { StringEnum } from "@oh-my-pi/pi-ai";
|
|
1046
|
-
import { Text } from "@oh-my-pi/pi-tui";
|
|
1047
|
-
|
|
1048
|
-
pi.registerTool({
|
|
1049
|
-
name: "my_tool",
|
|
1050
|
-
label: "My Tool",
|
|
1051
|
-
description: "What this tool does (shown to LLM)",
|
|
1052
|
-
parameters: Type.Object({
|
|
1053
|
-
action: StringEnum(["list", "add"] as const), // Use StringEnum for Google compatibility
|
|
1054
|
-
text: Type.Optional(Type.String()),
|
|
1055
|
-
}),
|
|
1056
|
-
hidden: false, // Optional: set true to hide unless explicitly enabled
|
|
1057
|
-
onSession(event, ctx) {
|
|
1058
|
-
// event.reason: "start" | "switch" | "branch" | "tree" | "shutdown"
|
|
1059
|
-
},
|
|
1060
|
-
|
|
1061
|
-
async execute(toolCallId, params, onUpdate, ctx, signal) {
|
|
1062
|
-
// Check for cancellation
|
|
1063
|
-
if (signal?.aborted) {
|
|
1064
|
-
return { content: [{ type: "text", text: "Cancelled" }] };
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
// Stream progress updates
|
|
1068
|
-
onUpdate?.({
|
|
1069
|
-
content: [{ type: "text", text: "Working..." }],
|
|
1070
|
-
details: { progress: 50 },
|
|
1071
|
-
});
|
|
1072
|
-
|
|
1073
|
-
// Run commands via pi.exec (captured from extension closure)
|
|
1074
|
-
const result = await pi.exec("some-command", [], { signal });
|
|
1075
|
-
|
|
1076
|
-
// Return result
|
|
1077
|
-
return {
|
|
1078
|
-
content: [{ type: "text", text: "Done" }], // Sent to LLM
|
|
1079
|
-
details: { data: result }, // For rendering & state
|
|
1080
|
-
};
|
|
1081
|
-
},
|
|
1082
|
-
|
|
1083
|
-
// Optional: Custom rendering
|
|
1084
|
-
renderCall(args, theme) { ... },
|
|
1085
|
-
renderResult(result, options, theme, args) { ... },
|
|
1086
|
-
});
|
|
1087
|
-
```
|
|
1088
|
-
|
|
1089
|
-
**Important:** Use `StringEnum` from `@oh-my-pi/pi-ai` for string enums. `Type.Union`/`Type.Literal` doesn't work with Google's API.
|
|
1090
|
-
|
|
1091
|
-
### Multiple Tools
|
|
1092
|
-
|
|
1093
|
-
One extension can register multiple tools with shared state:
|
|
1094
|
-
|
|
1095
|
-
```typescript
|
|
1096
|
-
export default function (pi: ExtensionAPI) {
|
|
1097
|
-
let connection = null;
|
|
1098
|
-
|
|
1099
|
-
pi.registerTool({ name: "db_connect", ... });
|
|
1100
|
-
pi.registerTool({ name: "db_query", ... });
|
|
1101
|
-
pi.registerTool({ name: "db_close", ... });
|
|
1102
|
-
|
|
1103
|
-
pi.on("session_shutdown", async () => {
|
|
1104
|
-
connection?.close();
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
```
|
|
1108
|
-
|
|
1109
|
-
### Custom Rendering
|
|
1110
|
-
|
|
1111
|
-
Tools can provide `renderCall` and `renderResult` for custom TUI display. See [tui.md](tui.md) for the full component API.
|
|
1112
|
-
|
|
1113
|
-
Tool output is wrapped in a `Box` that handles padding and background. Your render methods return `Component` instances (typically `Text`).
|
|
1114
|
-
|
|
1115
|
-
#### renderCall
|
|
1116
|
-
|
|
1117
|
-
Renders the tool call (before/during execution):
|
|
1118
|
-
|
|
1119
|
-
```typescript
|
|
1120
|
-
import { Text } from "@oh-my-pi/pi-tui";
|
|
1121
|
-
|
|
1122
|
-
renderCall(args, theme) {
|
|
1123
|
-
let text = theme.fg("toolTitle", theme.bold("my_tool "));
|
|
1124
|
-
text += theme.fg("muted", args.action);
|
|
1125
|
-
if (args.text) {
|
|
1126
|
-
text += " " + theme.fg("dim", `"${args.text}"`);
|
|
1127
|
-
}
|
|
1128
|
-
return new Text(text, 0, 0); // 0,0 padding - Box handles it
|
|
1129
|
-
}
|
|
1130
|
-
```
|
|
1131
|
-
|
|
1132
|
-
#### renderResult
|
|
1133
|
-
|
|
1134
|
-
Renders the tool result:
|
|
1135
|
-
|
|
1136
|
-
```typescript
|
|
1137
|
-
renderResult(result, { expanded, isPartial }, theme) {
|
|
1138
|
-
// Handle streaming
|
|
1139
|
-
if (isPartial) {
|
|
1140
|
-
return new Text(theme.fg("warning", "Processing..."), 0, 0);
|
|
1141
|
-
}
|
|
1142
|
-
|
|
1143
|
-
// Handle errors
|
|
1144
|
-
if (result.details?.error) {
|
|
1145
|
-
return new Text(theme.fg("error", `Error: ${result.details.error}`), 0, 0);
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
// Normal result - support expanded view (Ctrl+O)
|
|
1149
|
-
let text = theme.fg("success", "✓ Done");
|
|
1150
|
-
if (expanded && result.details?.items) {
|
|
1151
|
-
for (const item of result.details.items) {
|
|
1152
|
-
text += "\n " + theme.fg("dim", item);
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
return new Text(text, 0, 0);
|
|
1156
|
-
}
|
|
1157
|
-
```
|
|
1158
|
-
|
|
1159
|
-
#### Best Practices
|
|
1160
|
-
|
|
1161
|
-
- Use `Text` with padding `(0, 0)` - the Box handles padding
|
|
1162
|
-
- Use `\n` for multi-line content
|
|
1163
|
-
- Handle `isPartial` for streaming progress
|
|
1164
|
-
- Support `expanded` for detail on demand
|
|
1165
|
-
- Keep default view compact
|
|
1166
|
-
|
|
1167
|
-
#### Fallback
|
|
1168
|
-
|
|
1169
|
-
If `renderCall`/`renderResult` is not defined or throws:
|
|
1170
|
-
|
|
1171
|
-
- `renderCall`: Shows tool name
|
|
1172
|
-
- `renderResult`: Shows raw text from `content`
|
|
1173
|
-
|
|
1174
|
-
## Custom UI
|
|
1175
|
-
|
|
1176
|
-
Extensions can interact with users via `ctx.ui` methods and customize how messages/tools render.
|
|
1177
|
-
|
|
1178
|
-
### Dialogs
|
|
1179
|
-
|
|
1180
|
-
```typescript
|
|
1181
|
-
// Select from options
|
|
1182
|
-
const choice = await ctx.ui.select("Pick one:", ["A", "B", "C"]);
|
|
1183
|
-
|
|
1184
|
-
// Confirm dialog
|
|
1185
|
-
const ok = await ctx.ui.confirm("Delete?", "This cannot be undone");
|
|
1186
|
-
|
|
1187
|
-
// Text input
|
|
1188
|
-
const name = await ctx.ui.input("Name:", "placeholder");
|
|
1189
|
-
|
|
1190
|
-
// Multi-line editor
|
|
1191
|
-
const text = await ctx.ui.editor("Edit:", "prefilled text");
|
|
1192
|
-
|
|
1193
|
-
// Notification (non-blocking)
|
|
1194
|
-
ctx.ui.notify("Done!", "info"); // "info" | "warning" | "error"
|
|
1195
|
-
```
|
|
1196
|
-
|
|
1197
|
-
### Widgets and Status
|
|
1198
|
-
|
|
1199
|
-
```typescript
|
|
1200
|
-
// Status in footer (persistent until cleared)
|
|
1201
|
-
ctx.ui.setStatus("my-ext", "Processing...");
|
|
1202
|
-
ctx.ui.setStatus("my-ext", undefined); // Clear
|
|
1203
|
-
|
|
1204
|
-
// Working message shown during streaming
|
|
1205
|
-
ctx.ui.setWorkingMessage("Connecting...");
|
|
1206
|
-
ctx.ui.setWorkingMessage(); // Restore default
|
|
1207
|
-
|
|
1208
|
-
// Widget above editor (string array or factory function)
|
|
1209
|
-
ctx.ui.setWidget("my-widget", ["Line 1", "Line 2"]);
|
|
1210
|
-
ctx.ui.setWidget("my-widget", (tui, theme) => new Text(theme.fg("accent", "Custom"), 0, 0));
|
|
1211
|
-
ctx.ui.setWidget("my-widget", undefined); // Clear
|
|
1212
|
-
|
|
1213
|
-
// Custom header/footer
|
|
1214
|
-
ctx.ui.setHeader((tui, theme) => new Text(theme.fg("accent", "Header"), 0, 0));
|
|
1215
|
-
ctx.ui.setFooter((tui, theme) => new Text(theme.fg("accent", "Footer"), 0, 0));
|
|
1216
|
-
ctx.ui.setHeader(undefined); // Restore default
|
|
1217
|
-
ctx.ui.setFooter(undefined); // Restore default
|
|
1218
|
-
|
|
1219
|
-
// Terminal title
|
|
1220
|
-
ctx.ui.setTitle("omp - my-project");
|
|
1221
|
-
|
|
1222
|
-
// Editor text
|
|
1223
|
-
ctx.ui.setEditorText("Prefill text");
|
|
1224
|
-
const current = ctx.ui.getEditorText();
|
|
1225
|
-
|
|
1226
|
-
// Paste into editor (triggers paste handling, including collapse for large content)
|
|
1227
|
-
ctx.ui.pasteToEditor("pasted content");
|
|
1228
|
-
|
|
1229
|
-
// Custom editor component
|
|
1230
|
-
ctx.ui.setEditorComponent((tui, theme, keybindings) => new MyEditor(tui, theme, keybindings)); // EditorComponent
|
|
1231
|
-
ctx.ui.setEditorComponent(undefined); // Restore default
|
|
1232
|
-
```
|
|
1233
|
-
|
|
1234
|
-
### Custom Components
|
|
1235
|
-
|
|
1236
|
-
For complex UI, use `ctx.ui.custom()`. This temporarily replaces the editor with your component until `done()` is called:
|
|
1237
|
-
|
|
1238
|
-
```typescript
|
|
1239
|
-
import { Text, Component } from "@oh-my-pi/pi-tui";
|
|
1240
|
-
|
|
1241
|
-
const result = await ctx.ui.custom<boolean>((tui, theme, keybindings, done) => {
|
|
1242
|
-
const text = new Text("Press Enter to confirm, Escape to cancel", 1, 1);
|
|
1243
|
-
|
|
1244
|
-
text.onKey = (key) => {
|
|
1245
|
-
if (key === "return") done(true);
|
|
1246
|
-
if (key === "escape") done(false);
|
|
1247
|
-
return true;
|
|
1248
|
-
};
|
|
1249
|
-
|
|
1250
|
-
return text;
|
|
1251
|
-
}, { overlay: true });
|
|
1252
|
-
|
|
1253
|
-
if (result) {
|
|
1254
|
-
// User pressed Enter
|
|
1255
|
-
}
|
|
1256
|
-
```
|
|
1257
|
-
|
|
1258
|
-
The callback receives:
|
|
1259
|
-
|
|
1260
|
-
- `tui` - TUI instance (for screen dimensions, focus management)
|
|
1261
|
-
- `theme` - Current theme for styling
|
|
1262
|
-
- `keybindings` - Keybindings manager for resolving bindings
|
|
1263
|
-
- `done(value)` - Call to close component and return value
|
|
1264
|
-
|
|
1265
|
-
See [tui.md](tui.md) for the full component API and [examples/extensions/](../examples/extensions/) for working examples (todo.ts, tools.ts, reload-runtime.ts).
|
|
1266
|
-
|
|
1267
|
-
### Message Rendering
|
|
1268
|
-
|
|
1269
|
-
Register a custom renderer for messages with your `customType`:
|
|
1270
|
-
|
|
1271
|
-
```typescript
|
|
1272
|
-
import { Text } from "@oh-my-pi/pi-tui";
|
|
1273
|
-
|
|
1274
|
-
pi.registerMessageRenderer("my-extension", (message, options, theme) => {
|
|
1275
|
-
const { expanded } = options;
|
|
1276
|
-
let text = theme.fg("accent", `[${message.customType}] `);
|
|
1277
|
-
text += message.content;
|
|
1278
|
-
|
|
1279
|
-
if (expanded && message.details) {
|
|
1280
|
-
text += "\n" + theme.fg("dim", JSON.stringify(message.details, null, 2));
|
|
1281
|
-
}
|
|
1282
|
-
|
|
1283
|
-
return new Text(text, 0, 0);
|
|
1284
|
-
});
|
|
1285
|
-
```
|
|
1286
|
-
|
|
1287
|
-
Messages are sent via `pi.sendMessage()`:
|
|
1288
|
-
|
|
1289
|
-
```typescript
|
|
1290
|
-
pi.sendMessage({
|
|
1291
|
-
customType: "my-extension", // Matches registerMessageRenderer
|
|
1292
|
-
content: "Status update",
|
|
1293
|
-
display: true, // Show in TUI
|
|
1294
|
-
details: { ... }, // Available in renderer
|
|
1295
|
-
});
|
|
1296
|
-
```
|
|
1297
|
-
|
|
1298
|
-
### Themes
|
|
1299
|
-
|
|
1300
|
-
```typescript
|
|
1301
|
-
const themes = await ctx.ui.getAllThemes();
|
|
1302
|
-
const current = ctx.ui.theme;
|
|
1303
|
-
const loaded = await ctx.ui.getTheme("celestial");
|
|
1304
|
-
const result = await ctx.ui.setTheme("celestial");
|
|
1305
|
-
```
|
|
1306
|
-
|
|
1307
|
-
### Theme Colors
|
|
1308
|
-
|
|
1309
|
-
All render functions receive a `theme` object:
|
|
1310
|
-
|
|
1311
|
-
```typescript
|
|
1312
|
-
// Foreground colors
|
|
1313
|
-
theme.fg("toolTitle", text); // Tool names
|
|
1314
|
-
theme.fg("accent", text); // Highlights
|
|
1315
|
-
theme.fg("success", text); // Success (green)
|
|
1316
|
-
theme.fg("error", text); // Errors (red)
|
|
1317
|
-
theme.fg("warning", text); // Warnings (yellow)
|
|
1318
|
-
theme.fg("muted", text); // Secondary text
|
|
1319
|
-
theme.fg("dim", text); // Tertiary text
|
|
1320
|
-
|
|
1321
|
-
// Text styles
|
|
1322
|
-
theme.bold(text);
|
|
1323
|
-
theme.italic(text);
|
|
1324
|
-
theme.strikethrough(text);
|
|
1325
|
-
```
|
|
1326
|
-
|
|
1327
|
-
## Error Handling
|
|
1328
|
-
|
|
1329
|
-
- Extension errors are logged, agent continues
|
|
1330
|
-
- `tool_call` errors block the tool (fail-safe)
|
|
1331
|
-
- Tool `execute` errors are reported to the LLM with `isError: true`
|
|
1332
|
-
|
|
1333
|
-
## Mode Behavior
|
|
1334
|
-
|
|
1335
|
-
| Mode | UI Methods | Notes |
|
|
1336
|
-
| ------------ | ------------- | ------------------------------- |
|
|
1337
|
-
| Interactive | Full TUI | Normal operation |
|
|
1338
|
-
| JSON | No-op | `--mode json` output |
|
|
1339
|
-
| RPC | JSON protocol | Host handles UI |
|
|
1340
|
-
| Print (`-p`) | No-op | Extensions run but can't prompt |
|
|
1341
|
-
|
|
1342
|
-
In print/JSON/RPC modes, check `ctx.hasUI` before using UI methods.
|