co-mono/packages/coding-agent/examples/sdk
Mario Zechner 302404684f feat(coding-agent): add resume scope toggle with async loading
- /resume and --resume now toggle between Current Folder and All sessions with Tab
- SessionManager.list() and listAll() are now async with optional progress callback
- Shows loading progress (e.g. Loading 5/42) while scanning sessions
- SessionInfo.cwd field shows session working directory in All view
- Lazy loading: All sessions only loaded when user presses Tab

closes #619

Co-authored-by: Thomas Mustier <mustierthomas@gmail.com>
2026-01-12 00:00:03 +01:00
..
01-minimal.ts Merge hooks and custom-tools into unified extensions system (#454) 2026-01-05 01:43:35 +01:00
02-custom-model.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
03-custom-prompt.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
04-skills.ts Fix --no-skills flag not preventing skills from loading 2026-01-08 23:41:54 +01:00
05-tools.ts Merge hooks and custom-tools into unified extensions system (#454) 2026-01-05 01:43:35 +01:00
06-extensions.ts docs: remove CLI-specific --extension mention from SDK example 2026-01-05 18:03:41 +01:00
07-context-files.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
08-prompt-templates.ts Merge hooks and custom-tools into unified extensions system (#454) 2026-01-05 01:43:35 +01:00
09-api-keys-and-oauth.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
10-settings.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
11-sessions.ts feat(coding-agent): add resume scope toggle with async loading 2026-01-12 00:00:03 +01:00
12-full-control.ts fix(sdk): extensions: [] now disables discovery as documented (#465) 2026-01-05 16:55:51 +01:00
README.md Merge hooks and custom-tools into unified extensions system (#454) 2026-01-05 01:43:35 +01:00

SDK Examples

Programmatic usage of pi-coding-agent via createAgentSession().

Examples

File Description
01-minimal.ts Simplest usage with all defaults
02-custom-model.ts Select model and thinking level
03-custom-prompt.ts Replace or modify system prompt
04-skills.ts Discover, filter, or replace skills
05-tools.ts Built-in tools, custom tools
06-extensions.ts Logging, blocking, result modification
07-context-files.ts AGENTS.md context files
08-slash-commands.ts File-based slash commands
09-api-keys-and-oauth.ts API key resolution, OAuth config
10-settings.ts Override compaction, retry, terminal settings
11-sessions.ts In-memory, persistent, continue, list sessions
12-full-control.ts Replace everything, no discovery

Running

cd packages/coding-agent
npx tsx examples/sdk/01-minimal.ts

Quick Reference

import { getModel } from "@mariozechner/pi-ai";
import {
  AuthStorage,
  createAgentSession,
  discoverAuthStorage,
  discoverModels,
  discoverSkills,
  discoverExtensions,
  discoverCustomTools,
  discoverContextFiles,
  discoverSlashCommands,
  loadSettings,
  buildSystemPrompt,
  ModelRegistry,
  SessionManager,
  codingTools,
  readOnlyTools,
  readTool, bashTool, editTool, writeTool,
} from "@mariozechner/pi-coding-agent";

// Auth and models setup
const authStorage = discoverAuthStorage();
const modelRegistry = discoverModels(authStorage);

// Minimal
const { session } = await createAgentSession({ authStorage, modelRegistry });

// Custom model
const model = getModel("anthropic", "claude-opus-4-5");
const { session } = await createAgentSession({ model, thinkingLevel: "high", authStorage, modelRegistry });

// Modify prompt
const { session } = await createAgentSession({
  systemPrompt: (defaultPrompt) => defaultPrompt + "\n\nBe concise.",
  authStorage,
  modelRegistry,
});

// Read-only
const { session } = await createAgentSession({ tools: readOnlyTools, authStorage, modelRegistry });

// In-memory
const { session } = await createAgentSession({
  sessionManager: SessionManager.inMemory(),
  authStorage,
  modelRegistry,
});

// Full control
const customAuth = new AuthStorage("/my/app/auth.json");
customAuth.setRuntimeApiKey("anthropic", process.env.MY_KEY!);
const customRegistry = new ModelRegistry(customAuth);

const { session } = await createAgentSession({
  model,
  authStorage: customAuth,
  modelRegistry: customRegistry,
  systemPrompt: "You are helpful.",
  tools: [readTool, bashTool],
  customTools: [{ tool: myTool }],
  extensions: [{ factory: myExtension }],
  skills: [],
  contextFiles: [],
  slashCommands: [],
  sessionManager: SessionManager.inMemory(),
});

// Run prompts
session.subscribe((event) => {
  if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
    process.stdout.write(event.assistantMessageEvent.delta);
  }
});
await session.prompt("Hello");

Options

Option Default Description
authStorage discoverAuthStorage() Credential storage
modelRegistry discoverModels(authStorage) Model registry
cwd process.cwd() Working directory
agentDir ~/.pi/agent Config directory
model From settings/first available Model to use
thinkingLevel From settings/"off" off, low, medium, high
systemPrompt Discovered String or (default) => modified
tools codingTools Built-in tools
customTools Discovered Replaces discovery
additionalCustomToolPaths [] Merge with discovery
extensions Discovered Replaces discovery
additionalExtensionPaths [] Merge with discovery
skills Discovered Skills for prompt
contextFiles Discovered AGENTS.md files
slashCommands Discovered File commands
sessionManager SessionManager.create(cwd) Persistence
settingsManager From agentDir Settings overrides

Events

session.subscribe((event) => {
  switch (event.type) {
    case "message_update":
      if (event.assistantMessageEvent.type === "text_delta") {
        process.stdout.write(event.assistantMessageEvent.delta);
      }
      break;
    case "tool_execution_start":
      console.log(`Tool: ${event.toolName}`);
      break;
    case "tool_execution_end":
      console.log(`Result: ${event.result}`);
      break;
    case "agent_end":
      console.log("Done");
      break;
  }
});