singularity-forge/docs/dev/what-is-pi/14-the-sdk-rpc-embedding-pi.md
Jeremy 872b0adb48 docs: reorganize into user-docs/ and dev/ subdirectories
Split flat docs/ into user-docs/ (guides, config, troubleshooting) and
dev/ (ADRs, architecture, extension guides, proposals). Updated
docs/README.md index to reflect new paths.
2026-04-10 09:25:31 -05:00

1.6 KiB

The SDK & RPC — Embedding Pi

Pi isn't just a terminal tool. It's designed to be embedded in other applications.

SDK (TypeScript)

For Node.js/TypeScript applications, import and use pi directly:

import { AuthStorage, createAgentSession, ModelRegistry, SessionManager } from "@mariozechner/pi-coding-agent";

const authStorage = AuthStorage.create();
const modelRegistry = new ModelRegistry(authStorage);

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

// Subscribe to events
session.subscribe((event) => {
  if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
    process.stdout.write(event.assistantMessageEvent.delta);
  }
});

// Send prompts
await session.prompt("What files are in the current directory?");

The SDK gives you full control: custom tools, custom resource loaders, session management, model selection, event streaming. See the openclaw/openclaw project for a real-world SDK integration.

RPC Mode (Any Language)

For non-Node.js applications, spawn pi as a subprocess and communicate via JSON over stdin/stdout:

pi --mode rpc --provider anthropic

Send commands:

{"type": "prompt", "message": "Hello, world!"}
{"type": "steer", "message": "Stop and do this instead"}
{"type": "follow_up", "message": "After you're done, also do this"}

Receive events:

{"type": "event", "event": {"type": "message_update", ...}}
{"type": "response", "command": "prompt", "success": true}