RPC 模式

RPC 模式通过 stdin/stdout JSONL 协议供非 Node.js 进程集成。

pi --mode rpc

协议要点:

  • 严格使用 LF 分隔的 JSONL 帧
  • 客户端必须只用 \n 分割记录,不能用处理 Unicode 分隔符的通用行读取器(如 Node readline)
  • 详见 packages/coding-agent/docs/rpc.md

SDK 嵌入

在 Node.js/Bun 应用中嵌入 Pi Agent:

import {
  AuthStorage,
  createAgentSession,
  ModelRegistry,
  SessionManager
} from "@earendil-works/pi-coding-agent";
 
const authStorage = AuthStorage.create();
const modelRegistry = ModelRegistry.create(authStorage);
 
const { session } = await createAgentSession({
  sessionManager: SessionManager.inMemory(),
  authStorage,
  modelRegistry,
});
 
await session.prompt("What files are in the current directory?");

高级用法

对于多 Session 运行时替换,使用 createAgentSessionRuntime() 和 AgentSessionRuntime。

参考:packages/coding-agent/docs/sdk.md 和 packages/coding-agent/examples/sdk/

真实 SDK 集成示例

openclaw/openclaw 是一个真实的生产级 SDK 集成案例,展示了如何将 Pi 作为底层 Agent 引擎嵌入到更大的产品中。

相关页面:pi-coding-agent | pi-agent-core