Dwsy

pi-extensions

Pi extension development master reference. Use when: building pi extensions, debugging extension behavior, or choosing the right pattern.

Dwsy 22 2 Updated 5mo ago

Resources

13
GitHub

Install

npx skillscat add dwsy/pi-extensions-skill

Install via the SkillsCat registry.

SKILL.md

Pi Extensions โ€” LLM Master Reference

Start Here (Read Order)

Priority Document Read When
1 `ARCHITECTURE.md` Before writing ANY extension code
2 `PATTERNS.md` When you need copy-paste code for a specific task
3 `ANTI-PATTERNS.md` When reviewing or debugging extension code

The guides/ and references/ directories contain deeper narratives and examples. Use them after scanning the three master docs above.


One-Line Directives


Master Decision Trees

Which document do I need?

I need to understand how extensions work
  โ””โ”€โ–บ ARCHITECTURE.md

I need working code to copy
  โ””โ”€โ–บ PATTERNS.md

I need to know what NOT to do
  โ””โ”€โ–บ ANTI-PATTERNS.md

I need a step-by-step first tutorial
  โ””โ”€โ–บ guides/01-quickstart.md

I need deep narrative on tools/events/UI
  โ””โ”€โ–บ guides/02-paradigms.md

I need state persistence strategies
  โ””โ”€โ–บ guides/03-state.md

I need production architecture (workflows, memory)
  โ””โ”€โ–บ guides/04-production.md

I need RPC mode specifics
  โ””โ”€โ–บ guides/05-rpc-mode.md

I need pi internals (loader, runner, binding)
  โ””โ”€โ–บ guides/06-internals.md

I need provider plugins, OAuth, overrides
  โ””โ”€โ–บ guides/07-advanced-patterns.md

Which paradigm should I use?

Need LLM to perform action? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Tool (PATTERNS P2โ€“P5)
Need user to type /command? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Command (PATTERNS P6โ€“P7)
Need keyboard shortcut? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Shortcut (guides/02-paradigms.md)
Need to react to system events? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Event handler (PATTERNS P8โ€“P11)
Need interactive TUI? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Custom UI (PATTERNS P12โ€“P14)
Need to inject a model provider? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ registerProvider (PATTERNS P19โ€“P20)
Need to override a built-in tool? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Tool override (PATTERNS P4)

Which state persistence mechanism?

State should go to LLM context? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ sendMessage({ customType, ... })
State is extension-private? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ appendEntry("customType", data)
State is user preference (cross-project)? โ”€โ–บ File in ~/.pi/agent/
State is project-local? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ File in .pi/
State is temporary cache? โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Local variable (reconstructed on reload)

Document Map

Master References (Read First)

File Purpose Length
`ARCHITECTURE.md` Mental model, execution flow, exact event semantics ~9KB
`PATTERNS.md` 38 copy-paste patterns with exact imports ~17KB
`ANTI-PATTERNS.md` 15 common mistakes with corrections ~9KB

Progressive Guides (Read as Needed)

File Level Topic
`guides/01-quickstart.md` ๐ŸŒฑ Beginner First extension in 5 minutes
`guides/02-paradigms.md` ๐ŸŒฟ Intermediate Tools, commands, events, UI deep dive
`guides/03-state.md` ๐ŸŒณ Advanced Persistent and branch-resilient state
`guides/04-production.md` ๐Ÿ”๏ธ Expert Multi-mode, workflows, memory systems
`guides/05-rpc-mode.md` ๐Ÿ”Œ RPC RPC mode compatibility and degradation
`guides/06-internals.md` โš™๏ธ Internals Loader, runner, event dispatch, binding
`guides/07-advanced-patterns.md` ๐Ÿš€ Advanced Provider plugins, OAuth, tool overrides, file mutation queues

Reference Docs

File Purpose
`references/api.md` Complete API documentation
`references/api-quickref.md` Quick reference card
`references/events.md` Full event reference
`references/examples.md` Additional code examples
`references/ui-components.md` TUI component catalog
`references/extension-patterns-from-source.md` Multi-agent, diff rendering, config patterns from 10+ extensions
`references/tui-beautiful-rendering.md` Beautiful TUI: box drawing, transcript, overlays, badges, SVG/HTML widgets

Tutorials

File Topic
`tutorials/tool-browser.md` Build a searchable tool list with fuzzy filtering

Examples

File Purpose
`examples/gallery.md` Annotated real-world extensions

5-Minute Quick Test

Prerequisite: This skill is installed (cloned to skills/pi-extensions).
Below creates a sample Extension, placed in ~/.pi/agent/extensions/ โ€” separate from the skill directory.

# 1. Create the sample extension file
mkdir -p ~/.pi/agent/extensions
cat > ~/.pi/agent/extensions/hello.ts << 'EOF'
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";

export default function (pi: ExtensionAPI) {
  pi.registerCommand("hello", {
    description: "Say hello",
    handler: async (_args, ctx) => {
      ctx.ui.notify("Hello from Pi Extensions!", "success");
    },
  });
}
EOF

# 2. Test with -e flag (temporary load)
pi -e ~/.pi/agent/extensions/hello.ts
# Then type: /hello
#
# Tip: Extensions in ~/.pi/agent/extensions/ are auto-discovered.
#      The -e flag is only needed for temporary testing.

Quick Import Cheat Sheet

// Core types
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";

// Schema
import { Type } from "@sinclair/typebox";
import { StringEnum } from "@earendil-works/pi-ai";

// TUI
import { Container, Text, SelectList } from "@earendil-works/pi-tui";

// Utilities
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
import { isToolCallEventType, isBashToolResult } from "@earendil-works/pi-coding-agent";

Hot Topics

Dynamic Model Injection

Use pi.registerProvider() for proxies, custom endpoints, or team-wide model configs. See `PATTERNS.md` ยงP19โ€“P20 and `guides/07-advanced-patterns.md`.

Tool Override

Register a tool with the same name as a built-in (read, bash, edit, write) to wrap or replace it. See `PATTERNS.md` ยงP4.

Parallel Execution Safety

Custom tools that mutate files must use withFileMutationQueue() to avoid race conditions with built-in edit/write. See `PATTERNS.md` ยงP5 and `ANTI-PATTERNS.md` ยงA4.

RPC Safety

ctx.hasUI is true in RPC, but custom() returns undefined. Use select/confirm/input/editor for blocking dialogs that work in both modes. See `ANTI-PATTERNS.md` ยงA2 and `guides/05-rpc-mode.md`.


Master references: ARCHITECTURE ยท PATTERNS ยท ANTI-PATTERNS