Extension Interface
Extensions are TypeScript modules that export a default function receiving theExtensionAPI:
~/.pi/agent/extensions/<cwd>/.pi/extensions/- Paths in
settings.json“extensions” array
ExtensionAPI
Thepi object passed to extensions provides methods for registration and interaction.
Event Subscription
Subscribe to lifecycle events usingpi.on():
Available Events
Fired after session_start to provide additional resource pathsReturns:
ResourcesDiscoverResult with skillPaths, promptPaths, themePathsFired on initial session load
Fired before switching sessions (can be cancelled)Returns:
{ cancel?: boolean }Fired after switching to another session
Fired before forking a session (can be cancelled)Returns:
{ cancel?: boolean }Fired after forking a session
Fired before context compaction (can be cancelled or customized)Returns:
{ cancel?: boolean; compaction?: CompactionResult }Fired after context compaction completes
Fired on process exit
Fired before navigating in session tree (can be cancelled)Returns:
{ cancel?: boolean; summary?: { summary: string; details?: unknown } }Fired after navigating in the session tree
Fired before each LLM call. Can modify messagesReturns:
{ messages?: AgentMessage[] }Fired after user submits prompt but before agent loopReturns:
{ message?: CustomMessage; systemPrompt?: string }Fired when an agent loop starts
Fired when an agent loop ends
Fired at the start of each turn
Fired at the end of each turn
Fired when a message starts (user, assistant, or toolResult)
Fired during assistant message streaming with token-by-token updates
Fired when a message ends
Fired when a tool starts executing
Fired during tool execution with partial/streaming output
Fired when a tool finishes executing
Fired when a new model is selected
Fired before a tool executes. Can block executionReturns:
{ block?: boolean; reason?: string }Fired after a tool executes. Can modify resultReturns:
{ content?: Content[]; details?: unknown; isError?: boolean }Fired when user executes bash via
! or !! prefixReturns: { operations?: BashOperations; result?: BashResult }Fired when user input is received, before agent processingReturns:
{ action: 'continue' | 'transform' | 'handled'; text?: string; images?: ImageContent[] }Tool Registration
Register LLM-callable tools:ToolDefinition
Tool name (used in LLM tool calls)
Human-readable label for UI
Description for LLM
Parameter schema (TypeBox)
Execute the tool
Custom rendering for tool call display
Custom rendering for tool result display
Example
Command Registration
Register custom slash commands:Example
Shortcut Registration
Register keyboard shortcuts:Example
Flag Registration
Register CLI flags:Example
Message Actions
Send messages to the session:Session Metadata
Tool Management
Model Management
Provider Registration
Register or override model providers:Example
Message Rendering
Register custom renderers forCustomMessageEntry:
Utility
ExtensionContext
Context passed to event handlers and tool execution:UI methods for user interaction (see UI Context)
Whether UI is available (false in print/RPC mode)
Current working directory
Session manager (read-only)
Model registry for API key resolution
Current model (may be undefined)
Whether the agent is idle (not streaming)
Abort the current agent operation
Whether there are queued messages waiting
Gracefully shutdown pi and exit
Get current context usage for the active model
Trigger compaction without awaiting completion
Get the current effective system prompt
ExtensionCommandContext
Extended context for command handlers with session control:Wait for the agent to finish streaming
Start a new session, optionally with initialization
Fork from a specific entry, creating a new session file
Navigate to a different point in the session tree
Switch to a different session file
Reload extensions, skills, prompts, and themes
UI Context
UI methods available viactx.ui for interactive user interaction: