@astralyn/pi 0.81.1-1 → 0.81.1-2

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.
@@ -1,213 +1,213 @@
1
- # Extension Examples
2
-
3
- Example extensions for pi-coding-agent.
4
-
5
- ## Usage
6
-
7
- ```bash
8
- # Load an extension with --extension flag
9
- pi --extension examples/extensions/permission-gate.ts
10
-
11
- # Or copy to extensions directory for auto-discovery
12
- cp permission-gate.ts ~/.pi/agent/extensions/
13
- ```
14
-
15
- ## Examples
16
-
17
- ### Lifecycle & Safety
18
-
19
- | Extension | Description |
20
- |-----------|-------------|
21
- | `permission-gate.ts` | Prompts for confirmation before dangerous bash commands (rm -rf, sudo, etc.) |
22
- | `project-trust.ts` | Demonstrates the `project_trust` event for user/global and CLI extensions |
23
- | `protected-paths.ts` | Blocks writes to protected paths (.env, .git/, node_modules/) |
24
- | `confirm-destructive.ts` | Confirms before destructive session actions (clear, switch, fork) |
25
- | `dirty-repo-guard.ts` | Prevents session changes with uncommitted git changes |
26
- | `sandbox/` | OS-level sandboxing using `@anthropic-ai/sandbox-runtime` with per-project config |
27
- | `gondolin/` | Route built-in tools and `!` commands into a Gondolin micro-VM |
28
-
29
- ### Custom Tools
30
-
31
- | Extension | Description |
32
- |-----------|-------------|
33
- | `todo.ts` | Todo list tool + `/todos` command with custom rendering and state persistence |
34
- | `hello.ts` | Minimal custom tool example |
35
- | `question.ts` | Demonstrates `ctx.ui.select()` for asking the user questions with custom UI |
36
- | `questionnaire.ts` | Multi-question input with tab bar navigation between questions |
37
- | `tool-override.ts` | Override built-in tools (e.g., add logging/access control to `read`) |
38
- | `dynamic-tools.ts` | Register tools after startup (`session_start`) and at runtime via command, with prompt snippets and tool-specific prompt guidelines |
39
- | `kimi-deferred-tools.ts` | Search for and progressively activate tools for Kimi's deferred-tool loading protocol |
40
- | `structured-output.ts` | Final structured-output tool that returns `terminate: true` so the agent can end on the tool call |
41
- | `built-in-tool-renderer.ts` | Custom compact rendering for built-in tools (read, bash, edit, write) while keeping original behavior |
42
- | `minimal-mode.ts` | Override built-in tool rendering for minimal display (only tool calls, no output in collapsed mode) |
43
- | `truncated-tool.ts` | Wraps ripgrep with proper output truncation (50KB/2000 lines) |
44
- | `ssh.ts` | Delegate all tools to a remote machine via SSH using pluggable operations |
45
- | `subagent/` | Delegate tasks to specialized subagents with isolated context windows |
46
-
47
- ### Commands & UI
48
-
49
- | Extension | Description |
50
- |-----------|-------------|
51
- | `preset.ts` | Named presets for model, thinking level, tools, and instructions via `--preset` flag and `/preset` command |
52
- | `plan-mode/` | Claude Code-style plan mode for read-only exploration with `/plan` command and step tracking |
53
- | `tools.ts` | Interactive `/tools` command to enable/disable tools with session persistence |
54
- | `handoff.ts` | Transfer context to a new focused session via `/handoff <goal>` |
55
- | `qna.ts` | Extracts questions from last response into editor via `ctx.ui.setEditorText()` |
56
- | `status-line.ts` | Shows turn progress in footer via `ctx.ui.setStatus()` with themed colors |
57
- | `github-issue-autocomplete.ts` | Adds `#1234` issue completions by stacking a custom autocomplete provider that preloads open issues from `gh issue list` |
58
- | `widget-placement.ts` | Shows widgets above and below the editor via `ctx.ui.setWidget()` placement |
59
- | `hidden-thinking-label.ts` | Customizes the collapsed thinking label via `ctx.ui.setHiddenThinkingLabel()` |
60
- | `working-indicator.ts` | Customizes the streaming working indicator via `ctx.ui.setWorkingIndicator()` |
61
- | `model-status.ts` | Shows model changes in status bar via `model_select` hook |
62
- | `snake.ts` | Snake game with custom UI, keyboard handling, and session persistence |
63
- | `tic-tac-toe.ts` | Tic-tac-toe vs the agent with `executionMode: "sequential"` tools to prevent race conditions on shared cursor state |
64
- | `send-user-message.ts` | Demonstrates `pi.sendUserMessage()` for sending user messages from extensions |
65
- | `timed-confirm.ts` | Demonstrates AbortSignal for auto-dismissing `ctx.ui.confirm()` and `ctx.ui.select()` dialogs |
66
- | `rpc-demo.ts` | Exercises all RPC-supported extension UI methods; pair with [`examples/rpc-extension-ui.ts`](../rpc-extension-ui.ts) |
67
- | `modal-editor.ts` | Custom vim-like modal editor via `ctx.ui.setEditorComponent()` |
68
- | `rainbow-editor.ts` | Animated rainbow text effect via custom editor |
69
- | `notify.ts` | Desktop notifications via OSC 777 when agent finishes (Ghostty, iTerm2, WezTerm) |
70
- | `titlebar-spinner.ts` | Braille spinner animation in terminal title while the agent is working |
71
- | `summarize.ts` | Summarize conversation with GPT-5.2 and show in transient UI |
72
- | `custom-footer.ts` | Custom footer with git branch and token stats via `ctx.ui.setFooter()` |
73
- | `custom-header.ts` | Custom header via `ctx.ui.setHeader()` |
74
- | `overlay-test.ts` | Test overlay compositing with inline text inputs and edge cases |
75
- | `overlay-qa-tests.ts` | Comprehensive overlay QA tests: anchors, margins, stacking, overflow, animation |
76
- | `doom-overlay/` | DOOM game running as an overlay at 35 FPS (demonstrates real-time game rendering) |
77
- | `shutdown-command.ts` | Adds `/quit` command demonstrating `ctx.shutdown()` |
78
- | `reload-runtime.ts` | Adds `/reload-runtime` and `reload_runtime` tool showing safe reload flow |
79
- | `interactive-shell.ts` | Run interactive commands (vim, htop) with full terminal via `user_bash` hook |
80
- | `inline-bash.ts` | Expands `!{command}` patterns in prompts via `input` event transformation |
81
- | `input-transform-streaming.ts` | Skips expensive input preprocessing for mid-stream steering via `streamingBehavior` |
82
-
83
- ### Git Integration
84
-
85
- | Extension | Description |
86
- |-----------|-------------|
87
- | `git-checkpoint.ts` | Creates git stash checkpoints at each turn for code restoration on fork |
88
- | `auto-commit-on-exit.ts` | Auto-commits on exit using last assistant message for commit message |
89
-
90
- ### System Prompt & Compaction
91
-
92
- | Extension | Description |
93
- |-----------|-------------|
94
- | `pirate.ts` | Demonstrates `systemPromptAppend` to dynamically modify system prompt |
95
- | `claude-rules.ts` | Scans `.claude/rules/` folder and lists rules in system prompt |
96
- | `custom-compaction.ts` | Custom compaction that summarizes entire conversation |
97
- | `trigger-compact.ts` | Triggers compaction when context usage exceeds 100k tokens and adds `/trigger-compact` command |
98
-
99
- ### System Integration
100
-
101
- | Extension | Description |
102
- |-----------|-------------|
103
- | `mac-system-theme.ts` | Syncs pi theme with macOS dark/light mode |
104
-
105
- ### Resources
106
-
107
- | Extension | Description |
108
- |-----------|-------------|
109
- | `dynamic-resources/` | Loads skills, prompts, and themes using `resources_discover` |
110
-
111
- ### Messages & Communication
112
-
113
- | Extension | Description |
114
- |-----------|-------------|
115
- | `message-renderer.ts` | Custom message rendering with colors and expandable details via `registerMessageRenderer` |
116
- | `entry-renderer.ts` | TUI-only session entry rendering via `appendEntry` and `registerEntryRenderer` |
117
- | `event-bus.ts` | Inter-extension communication via `pi.events` |
118
-
119
- ### Session Metadata
120
-
121
- | Extension | Description |
122
- |-----------|-------------|
123
- | `session-name.ts` | Name sessions for the session selector via `setSessionName` |
124
- | `bookmark.ts` | Bookmark entries with labels for `/tree` navigation via `setLabel` |
125
-
126
- ### Custom Providers
127
-
128
- | Extension | Description |
129
- |-----------|-------------|
130
- | `custom-provider-anthropic/` | Custom Anthropic provider with OAuth support and custom streaming implementation |
131
- | `custom-provider-gitlab-duo/` | GitLab Duo provider using pi-ai's built-in Anthropic/OpenAI streaming via proxy |
132
-
133
- ### External Dependencies
134
-
135
- | Extension | Description |
136
- |-----------|-------------|
137
- | `with-deps/` | Extension with its own package.json and dependencies (demonstrates jiti module resolution) |
138
- | `file-trigger.ts` | Watches a trigger file and injects contents into conversation |
139
-
140
- ## Writing Extensions
141
-
142
- See [docs/extensions.md](../../docs/extensions.md) for full documentation.
143
-
144
- ```typescript
145
- import type { ExtensionAPI } from "@astralyn/pi";
146
- import { Type } from "typebox";
147
-
148
- export default function (pi: ExtensionAPI) {
149
- // Subscribe to lifecycle events
150
- pi.on("tool_call", async (event, ctx) => {
151
- if (event.toolName === "bash" && event.input.command?.includes("rm -rf")) {
152
- const ok = await ctx.ui.confirm("Dangerous!", "Allow rm -rf?");
153
- if (!ok) return { block: true, reason: "Blocked by user" };
154
- }
155
- });
156
-
157
- // Register custom tools
158
- pi.registerTool({
159
- name: "greet",
160
- label: "Greeting",
161
- description: "Generate a greeting",
162
- parameters: Type.Object({
163
- name: Type.String({ description: "Name to greet" }),
164
- }),
165
- async execute(toolCallId, params, signal, onUpdate, ctx) {
166
- return {
167
- content: [{ type: "text", text: `Hello, ${params.name}!` }],
168
- details: {},
169
- };
170
- },
171
- });
172
-
173
- // Register commands
174
- pi.registerCommand("hello", {
175
- description: "Say hello",
176
- handler: async (args, ctx) => {
177
- ctx.ui.notify("Hello!", "info");
178
- },
179
- });
180
- }
181
- ```
182
-
183
- ## Key Patterns
184
-
185
- **Use StringEnum for string parameters** (required for Google API compatibility):
186
- ```typescript
187
- import { StringEnum } from "@earendil-works/pi-ai";
188
-
189
- // Good
190
- action: StringEnum(["list", "add"] as const)
191
-
192
- // Bad - doesn't work with Google
193
- action: Type.Union([Type.Literal("list"), Type.Literal("add")])
194
- ```
195
-
196
- **State persistence via details:**
197
- ```typescript
198
- // Store state in tool result details for proper forking support
199
- return {
200
- content: [{ type: "text", text: "Done" }],
201
- details: { todos: [...todos], nextId }, // Persisted in session
202
- };
203
-
204
- // Reconstruct on session events
205
- pi.on("session_start", async (_event, ctx) => {
206
- for (const entry of ctx.sessionManager.getBranch()) {
207
- if (entry.type === "message" && entry.message.toolName === "my_tool") {
208
- const details = entry.message.details;
209
- // Reconstruct state from details
210
- }
211
- }
212
- });
213
- ```
1
+ # Extension Examples
2
+
3
+ Example extensions for pi-coding-agent.
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ # Load an extension with --extension flag
9
+ pi --extension examples/extensions/permission-gate.ts
10
+
11
+ # Or copy to extensions directory for auto-discovery
12
+ cp permission-gate.ts ~/.pi/agent/extensions/
13
+ ```
14
+
15
+ ## Examples
16
+
17
+ ### Lifecycle & Safety
18
+
19
+ | Extension | Description |
20
+ |-----------|-------------|
21
+ | `permission-gate.ts` | Prompts for confirmation before dangerous bash commands (rm -rf, sudo, etc.) |
22
+ | `project-trust.ts` | Demonstrates the `project_trust` event for user/global and CLI extensions |
23
+ | `protected-paths.ts` | Blocks writes to protected paths (.env, .git/, node_modules/) |
24
+ | `confirm-destructive.ts` | Confirms before destructive session actions (clear, switch, fork) |
25
+ | `dirty-repo-guard.ts` | Prevents session changes with uncommitted git changes |
26
+ | `sandbox/` | OS-level sandboxing using `@anthropic-ai/sandbox-runtime` with per-project config |
27
+ | `gondolin/` | Route built-in tools and `!` commands into a Gondolin micro-VM |
28
+
29
+ ### Custom Tools
30
+
31
+ | Extension | Description |
32
+ |-----------|-------------|
33
+ | `todo.ts` | Todo list tool + `/todos` command with custom rendering and state persistence |
34
+ | `hello.ts` | Minimal custom tool example |
35
+ | `question.ts` | Demonstrates `ctx.ui.select()` for asking the user questions with custom UI |
36
+ | `questionnaire.ts` | Multi-question input with tab bar navigation between questions |
37
+ | `tool-override.ts` | Override built-in tools (e.g., add logging/access control to `read`) |
38
+ | `dynamic-tools.ts` | Register tools after startup (`session_start`) and at runtime via command, with prompt snippets and tool-specific prompt guidelines |
39
+ | `kimi-deferred-tools.ts` | Search for and progressively activate tools for Kimi's deferred-tool loading protocol |
40
+ | `structured-output.ts` | Final structured-output tool that returns `terminate: true` so the agent can end on the tool call |
41
+ | `built-in-tool-renderer.ts` | Custom compact rendering for built-in tools (read, bash, edit, write) while keeping original behavior |
42
+ | `minimal-mode.ts` | Override built-in tool rendering for minimal display (only tool calls, no output in collapsed mode) |
43
+ | `truncated-tool.ts` | Wraps ripgrep with proper output truncation (50KB/2000 lines) |
44
+ | `ssh.ts` | Delegate all tools to a remote machine via SSH using pluggable operations |
45
+ | `subagent/` | Delegate tasks to specialized subagents with isolated context windows |
46
+
47
+ ### Commands & UI
48
+
49
+ | Extension | Description |
50
+ |-----------|-------------|
51
+ | `preset.ts` | Named presets for model, thinking level, tools, and instructions via `--preset` flag and `/preset` command |
52
+ | `plan-mode/` | Claude Code-style plan mode for read-only exploration with `/plan` command and step tracking |
53
+ | `tools.ts` | Interactive `/tools` command to enable/disable tools with session persistence |
54
+ | `handoff.ts` | Transfer context to a new focused session via `/handoff <goal>` |
55
+ | `qna.ts` | Extracts questions from last response into editor via `ctx.ui.setEditorText()` |
56
+ | `status-line.ts` | Shows turn progress in footer via `ctx.ui.setStatus()` with themed colors |
57
+ | `github-issue-autocomplete.ts` | Adds `#1234` issue completions by stacking a custom autocomplete provider that preloads open issues from `gh issue list` |
58
+ | `widget-placement.ts` | Shows widgets above and below the editor via `ctx.ui.setWidget()` placement |
59
+ | `hidden-thinking-label.ts` | Customizes the collapsed thinking label via `ctx.ui.setHiddenThinkingLabel()` |
60
+ | `working-indicator.ts` | Customizes the streaming working indicator via `ctx.ui.setWorkingIndicator()` |
61
+ | `model-status.ts` | Shows model changes in status bar via `model_select` hook |
62
+ | `snake.ts` | Snake game with custom UI, keyboard handling, and session persistence |
63
+ | `tic-tac-toe.ts` | Tic-tac-toe vs the agent with `executionMode: "sequential"` tools to prevent race conditions on shared cursor state |
64
+ | `send-user-message.ts` | Demonstrates `pi.sendUserMessage()` for sending user messages from extensions |
65
+ | `timed-confirm.ts` | Demonstrates AbortSignal for auto-dismissing `ctx.ui.confirm()` and `ctx.ui.select()` dialogs |
66
+ | `rpc-demo.ts` | Exercises all RPC-supported extension UI methods; pair with [`examples/rpc-extension-ui.ts`](../rpc-extension-ui.ts) |
67
+ | `modal-editor.ts` | Custom vim-like modal editor via `ctx.ui.setEditorComponent()` |
68
+ | `rainbow-editor.ts` | Animated rainbow text effect via custom editor |
69
+ | `notify.ts` | Desktop notifications via OSC 777 when agent finishes (Ghostty, iTerm2, WezTerm) |
70
+ | `titlebar-spinner.ts` | Braille spinner animation in terminal title while the agent is working |
71
+ | `summarize.ts` | Summarize conversation with GPT-5.2 and show in transient UI |
72
+ | `custom-footer.ts` | Custom footer with git branch and token stats via `ctx.ui.setFooter()` |
73
+ | `custom-header.ts` | Custom header via `ctx.ui.setHeader()` |
74
+ | `overlay-test.ts` | Test overlay compositing with inline text inputs and edge cases |
75
+ | `overlay-qa-tests.ts` | Comprehensive overlay QA tests: anchors, margins, stacking, overflow, animation |
76
+ | `doom-overlay/` | DOOM game running as an overlay at 35 FPS (demonstrates real-time game rendering) |
77
+ | `shutdown-command.ts` | Adds `/quit` command demonstrating `ctx.shutdown()` |
78
+ | `reload-runtime.ts` | Adds `/reload-runtime` and `reload_runtime` tool showing safe reload flow |
79
+ | `interactive-shell.ts` | Run interactive commands (vim, htop) with full terminal via `user_bash` hook |
80
+ | `inline-bash.ts` | Expands `!{command}` patterns in prompts via `input` event transformation |
81
+ | `input-transform-streaming.ts` | Skips expensive input preprocessing for mid-stream steering via `streamingBehavior` |
82
+
83
+ ### Git Integration
84
+
85
+ | Extension | Description |
86
+ |-----------|-------------|
87
+ | `git-checkpoint.ts` | Creates git stash checkpoints at each turn for code restoration on fork |
88
+ | `auto-commit-on-exit.ts` | Auto-commits on exit using last assistant message for commit message |
89
+
90
+ ### System Prompt & Compaction
91
+
92
+ | Extension | Description |
93
+ |-----------|-------------|
94
+ | `pirate.ts` | Demonstrates `systemPromptAppend` to dynamically modify system prompt |
95
+ | `claude-rules.ts` | Scans `.claude/rules/` folder and lists rules in system prompt |
96
+ | `custom-compaction.ts` | Custom compaction that summarizes entire conversation |
97
+ | `trigger-compact.ts` | Triggers compaction when context usage exceeds 100k tokens and adds `/trigger-compact` command |
98
+
99
+ ### System Integration
100
+
101
+ | Extension | Description |
102
+ |-----------|-------------|
103
+ | `mac-system-theme.ts` | Syncs pi theme with macOS dark/light mode |
104
+
105
+ ### Resources
106
+
107
+ | Extension | Description |
108
+ |-----------|-------------|
109
+ | `dynamic-resources/` | Loads skills, prompts, and themes using `resources_discover` |
110
+
111
+ ### Messages & Communication
112
+
113
+ | Extension | Description |
114
+ |-----------|-------------|
115
+ | `message-renderer.ts` | Custom message rendering with colors and expandable details via `registerMessageRenderer` |
116
+ | `entry-renderer.ts` | TUI-only session entry rendering via `appendEntry` and `registerEntryRenderer` |
117
+ | `event-bus.ts` | Inter-extension communication via `pi.events` |
118
+
119
+ ### Session Metadata
120
+
121
+ | Extension | Description |
122
+ |-----------|-------------|
123
+ | `session-name.ts` | Name sessions for the session selector via `setSessionName` |
124
+ | `bookmark.ts` | Bookmark entries with labels for `/tree` navigation via `setLabel` |
125
+
126
+ ### Custom Providers
127
+
128
+ | Extension | Description |
129
+ |-----------|-------------|
130
+ | `custom-provider-anthropic/` | Custom Anthropic provider with OAuth support and custom streaming implementation |
131
+ | `custom-provider-gitlab-duo/` | GitLab Duo provider using pi-ai's built-in Anthropic/OpenAI streaming via proxy |
132
+
133
+ ### External Dependencies
134
+
135
+ | Extension | Description |
136
+ |-----------|-------------|
137
+ | `with-deps/` | Extension with its own package.json and dependencies (demonstrates jiti module resolution) |
138
+ | `file-trigger.ts` | Watches a trigger file and injects contents into conversation |
139
+
140
+ ## Writing Extensions
141
+
142
+ See [docs/extensions.md](../../docs/extensions.md) for full documentation.
143
+
144
+ ```typescript
145
+ import type { ExtensionAPI } from "@astralyn/pi";
146
+ import { Type } from "typebox";
147
+
148
+ export default function (pi: ExtensionAPI) {
149
+ // Subscribe to lifecycle events
150
+ pi.on("tool_call", async (event, ctx) => {
151
+ if (event.toolName === "bash" && event.input.command?.includes("rm -rf")) {
152
+ const ok = await ctx.ui.confirm("Dangerous!", "Allow rm -rf?");
153
+ if (!ok) return { block: true, reason: "Blocked by user" };
154
+ }
155
+ });
156
+
157
+ // Register custom tools
158
+ pi.registerTool({
159
+ name: "greet",
160
+ label: "Greeting",
161
+ description: "Generate a greeting",
162
+ parameters: Type.Object({
163
+ name: Type.String({ description: "Name to greet" }),
164
+ }),
165
+ async execute(toolCallId, params, signal, onUpdate, ctx) {
166
+ return {
167
+ content: [{ type: "text", text: `Hello, ${params.name}!` }],
168
+ details: {},
169
+ };
170
+ },
171
+ });
172
+
173
+ // Register commands
174
+ pi.registerCommand("hello", {
175
+ description: "Say hello",
176
+ handler: async (args, ctx) => {
177
+ ctx.ui.notify("Hello!", "info");
178
+ },
179
+ });
180
+ }
181
+ ```
182
+
183
+ ## Key Patterns
184
+
185
+ **Use StringEnum for string parameters** (required for Google API compatibility):
186
+ ```typescript
187
+ import { StringEnum } from "@earendil-works/pi-ai";
188
+
189
+ // Good
190
+ action: StringEnum(["list", "add"] as const)
191
+
192
+ // Bad - doesn't work with Google
193
+ action: Type.Union([Type.Literal("list"), Type.Literal("add")])
194
+ ```
195
+
196
+ **State persistence via details:**
197
+ ```typescript
198
+ // Store state in tool result details for proper forking support
199
+ return {
200
+ content: [{ type: "text", text: "Done" }],
201
+ details: { todos: [...todos], nextId }, // Persisted in session
202
+ };
203
+
204
+ // Reconstruct on session events
205
+ pi.on("session_start", async (_event, ctx) => {
206
+ for (const entry of ctx.sessionManager.getBranch()) {
207
+ if (entry.type === "message" && entry.message.toolName === "my_tool") {
208
+ const details = entry.message.details;
209
+ // Reconstruct state from details
210
+ }
211
+ }
212
+ });
213
+ ```
File without changes