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
ResourcesDiscoverEvent
Fired after session_start to provide additional resource pathsReturns:
ResourcesDiscoverResult with skillPaths, promptPaths, themePathsSessionStartEvent
Fired on initial session load
SessionBeforeSwitchEvent
Fired before switching sessions (can be cancelled)Returns:
{ cancel?: boolean }SessionSwitchEvent
Fired after switching to another session
SessionBeforeForkEvent
Fired before forking a session (can be cancelled)Returns:
{ cancel?: boolean }SessionForkEvent
Fired after forking a session
SessionBeforeCompactEvent
Fired before context compaction (can be cancelled or customized)Returns:
{ cancel?: boolean; compaction?: CompactionResult }SessionCompactEvent
Fired after context compaction completes
SessionShutdownEvent
Fired on process exit
SessionBeforeTreeEvent
Fired before navigating in session tree (can be cancelled)Returns:
{ cancel?: boolean; summary?: { summary: string; details?: unknown } }SessionTreeEvent
Fired after navigating in the session tree
ContextEvent
Fired before each LLM call. Can modify messagesReturns:
{ messages?: AgentMessage[] }BeforeAgentStartEvent
Fired after user submits prompt but before agent loopReturns:
{ message?: CustomMessage; systemPrompt?: string }AgentStartEvent
Fired when an agent loop starts
AgentEndEvent
Fired when an agent loop ends
TurnStartEvent
Fired at the start of each turn
TurnEndEvent
Fired at the end of each turn
MessageStartEvent
Fired when a message starts (user, assistant, or toolResult)
MessageUpdateEvent
Fired during assistant message streaming with token-by-token updates
MessageEndEvent
Fired when a message ends
ToolExecutionStartEvent
Fired when a tool starts executing
ToolExecutionUpdateEvent
Fired during tool execution with partial/streaming output
ToolExecutionEndEvent
Fired when a tool finishes executing
ModelSelectEvent
Fired when a new model is selected
ToolCallEvent
Fired before a tool executes. Can block executionReturns:
{ block?: boolean; reason?: string }ToolResultEvent
Fired after a tool executes. Can modify resultReturns:
{ content?: Content[]; details?: unknown; isError?: boolean }UserBashEvent
Fired when user executes bash via
! or !! prefixReturns: { operations?: BashOperations; result?: BashResult }InputEvent
Fired when user input is received, before agent processingReturns:
{ action: 'continue' | 'transform' | 'handled'; text?: string; images?: ImageContent[] }Tool Registration
Register LLM-callable tools:ToolDefinition
string
required
Tool name (used in LLM tool calls)
string
required
Human-readable label for UI
string
required
Description for LLM
TSchema
required
Parameter schema (TypeBox)
function
required
Execute the tool
function
Custom rendering for tool call display
function
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:ExtensionUIContext
UI methods for user interaction (see UI Context)
boolean
Whether UI is available (false in print/RPC mode)
string
Current working directory
ReadonlySessionManager
Session manager (read-only)
ModelRegistry
Model registry for API key resolution
Model<any> | undefined
Current model (may be undefined)
() => boolean
Whether the agent is idle (not streaming)
() => void
Abort the current agent operation
() => boolean
Whether there are queued messages waiting
() => void
Gracefully shutdown pi and exit
() => ContextUsage | undefined
Get current context usage for the active model
(options?: CompactOptions) => void
Trigger compaction without awaiting completion
() => string
Get the current effective system prompt
ExtensionCommandContext
Extended context for command handlers with session control:() => Promise<void>
Wait for the agent to finish streaming
function
Start a new session, optionally with initialization
(entryId: string) => Promise<{ cancelled: boolean }>
Fork from a specific entry, creating a new session file
Navigate to a different point in the session tree
(sessionPath: string) => Promise<{ cancelled: boolean }>
Switch to a different session file
() => Promise<void>
Reload extensions, skills, prompts, and themes
UI Context
UI methods available viactx.ui for interactive user interaction: