Extensibility System
Pi’s extensibility system allows you to customize and extend its behavior without forking the codebase. The system provides four primary extension mechanisms: Extensions, Skills, Prompt Templates, and Themes.Extension Mechanisms
Extensions
Extensions are TypeScript or JavaScript modules that receive full access to the agent’s lifecycle, allowing you to:- Subscribe to lifecycle events
- Register LLM-callable tools
- Add slash commands
- Define keyboard shortcuts
- Customize the UI
- Modify context before LLM calls
- Intercept tool executions
Extension Structure
An extension is a module that exports a factory function:Discovery and Loading
Extensions are discovered from:- Global:
~/.pi/agent/extensions/ - Project:
.pi/extensions/ - Explicit:
--extension path/to/extension.ts
- Direct
.tsor.jsfiles in the extensions directory - Subdirectories with
index.tsorindex.js - Subdirectories with
package.jsoncontaining api.extensionsfield
Extension API Reference
TheExtensionAPI provides methods for registering functionality and accessing the agent:
Event Subscription
session_start,session_switch,session_compactagent_start,agent_endturn_start,turn_endmessage_start,message_update,message_endtool_execution_start,tool_execution_update,tool_execution_endtool_call,tool_result(interceptors)context(modify messages before LLM)model_select,input
Tool Registration
Command Registration
Keyboard Shortcuts
UI Customization
Provider Registration
Extension Context
The context object (ctx) passed to event handlers provides:
Extension Patterns
Pattern: Custom Tool with UI
Pattern: Custom Tool with UI
Pattern: Context Modification
Pattern: Context Modification
Pattern: Monitoring Extension
Pattern: Monitoring Extension
Skills
Skills are markdown files that provide task-specific instructions to the agent. They’re discovered automatically and presented in the system prompt or loaded explicitly via/skill commands.
Skill Structure
-
Commit messages: Use conventional commits format
- feat: New feature
- fix: Bug fix
- docs: Documentation
- refactor: Code restructuring
-
Before pushing: Review changes
Skill Frontmatter
disable-model-invocation: When true, the skill is NOT automatically added to the system prompt. It can only be loaded explicitly via /skill:name commands. Useful for:
- Large reference documents
- Context-specific guides
- Skills that conflict with default behavior
Using Skills
Automatic loading: Skills are presented in the system prompt:Prompt Templates
Prompt templates are reusable prompts with argument substitution. They’re useful for frequently used prompts or standardized workflows.Template Structure
Template Syntax
Positional arguments:$1, $2, $3, …
All arguments: $@ or $ARGUMENTS
Argument slicing: ${@:N} or ${@:N:L}
${@:2}: All arguments from 2nd onwards${@:2:3}: 3 arguments starting from 2nd
Template Discovery
Templates are loaded from:- Global:
~/.pi/agent/prompts/ - Project:
.pi/prompts/ - Explicit:
--prompt path/to/template.md
Themes
Themes customize the visual appearance of Pi’s TUI. They’re JSON files that define colors and styles.Theme Structure
Theme Discovery
Themes are loaded from:- Global:
~/.pi/agent/themes/ - Project:
.pi/themes/ - Built-in: Shipped with pi-coding-agent
Pi Packages
Pi packages bundle extensions, skills, prompts, and themes into a single npm package for distribution.Package Structure
Installation
Creating a Package
1
Initialize package
2
Add package.json pi field
3
Create extension
4
Build and publish
Extension Loading
Extensions are loaded usingjiti, a just-in-time TypeScript compiler. This enables:
- Writing extensions in TypeScript without pre-compilation
- Hot module reloading during development
- Support for ES modules and CommonJS
- Automatic dependency resolution
Module Resolution
Extensions have access to bundled packages:@mariozechner/pi-coding-agent@mariozechner/pi-agent-core@mariozechner/pi-ai@mariozechner/pi-tui@sinclair/typebox
Error Handling
Extension errors are caught and logged without crashing Pi:~/.pi/agent/logs/ for detailed error logs.
Best Practices
Extension Design
Extension Design
- Keep extensions focused on a single responsibility
- Use event handlers for observation, tools for capabilities
- Provide clear descriptions for commands and tools
- Handle errors gracefully (extensions shouldn’t crash Pi)
- Use TypeScript for type safety
Skill Writing
Skill Writing
- Write clear, actionable instructions
- Include code examples where relevant
- Keep skills focused on a specific task or domain
- Use descriptive names (lowercase, hyphens)
- Test skills by invoking manually first
Template Design
Template Design
- Use positional args for required parameters
- Use
$ARGUMENTSfor optional/flexible content - Provide good default behavior
- Include examples in the description
Performance
Performance
- Avoid expensive operations in frequently-called event handlers
- Use
onUpdatefor streaming tool results - Debounce UI updates if needed
- Clean up resources in
dispose()methods
Next Steps
Architecture
Understand the overall system design
Packages
Explore package capabilities