六个综合练习项目,以及官方文档与示例索引。

难度:★★★★★ | 前置:00、01、02、03、04、05、06、07 | 本篇提供 6 个由易到难的综合项目,每个项目给出目标、涉及知识点、实现提示、验收标准,以及可对照的官方示例。

01

项目 1:个人工作流套件(无代码)★★

目标:只用上下文文件、提示模板和技能,把 Pi 配置成你日常开发的助手。

要求

  1. ~/.pi/agent/AGENTS.md:写入通用偏好(语言、代码风格、提交规范);
  2. 项目 AGENTS.md:写入该项目的构建/测试命令;
  3. 3 个提示模板:/review [focus]、/commit [type]、/explain <path>;
  4. 1 个技能 release-notes:包含 SKILL.md 和 references/template.md;
  5. 在 settings.json 中设置默认模型、思考等级和主题。

验收:新开会话后启动头列出这些资源;/review security 能按参数聚焦;问"帮我写这次发布的说明"时模型会主动读取技能。

参考:04 篇;官方 Prompt Templates、Skills、Configuration。

02

项目 2:安全护栏扩展 ★★★

目标:写一个综合安全扩展 guard.ts。

功能

  1. tool_call 拦截危险 bash(rm -rf、sudo、git push --force、curl … | sh)——有 UI 时确认,无 UI 时阻止;
  2. 阻止写入 .env、.git/、node_modules/、*.pem;
  3. 根据工具 annotations(MCP 工具的 destructiveHint 等)决定是否确认;
  4. ctx.ui.setStatus() 在底栏显示本会话被拦截次数;
  5. /guard-log 命令列出本会话拦截记录(用 pi.appendEntry() 持久化,session_start 时从 getBranch() 重建)。

验收:交互模式下会弹确认;pi -p "删除 tmp 目录" 时被阻止并返回原因;切换分支后拦截记录与分支一致。

参考:permission-gate.ts、protected-paths.ts、status-line.ts、todo.ts;Extensions 文档的 Tool exposure 一节。

03

项目 3:Git 工作流扩展 ★★★★

目标:让 Pi 更好地融入 git。

功能

  1. 注册 git_summary 工具(outputSchema + structuredContent),返回当前分支、改动文件数、ahead/behind;
  2. /checkpoint 命令:用 pi.exec("git", [...]) 创建 stash 或 WIP 提交;
  3. session_shutdown 时若有未提交改动,询问是否自动提交(无 UI 时跳过);
  4. 注册 --auto-commit CLI 参数(pi.registerFlag),开启后退出时直接提交。

验收:codemode 脚本调用 tools.git_summary({}) 能拿到结构化对象;pi --auto-commit 退出时生成提交。

参考:git-checkpoint.ts、auto-commit-on-exit.ts、dirty-repo-guard.ts、preset.ts(registerFlag)。

04

项目 4:自动化代码审查机器人(Print/JSON 模式)★★★★

目标:在 CI 中用 Pi 审查 PR。

步骤

  1. 只读启动:pi --no-session --tools read,grep,find,ls --mode json;
  2. 输入:git diff origin/main...HEAD | pi --mode json --no-session --tools read,grep,find,ls "审查这个 diff,输出风险列表" > events.jsonl;
  3. 写一个 Node 或 Python 脚本解析 JSONL(按 LF 分割,不用 Node readline),提取最终 message_end 中的 assistant 文本、所有工具调用及 durationMs、总用量;
  4. 检查是否出现 stopReason: "error",据此决定 CI 退出码(JSON 模式下失败不会自动非零);
  5. 进阶:加一个带 terminate: true 的 structured_output 工具(参照官方示例),让结果变成机器可读 JSON。

验收:在一个示例仓库上跑通,生成 Markdown 审查报告;故意让 API key 失效时 CI 失败。

安全:CI 中使用 --no-approve 或明确的 defaultProjectTrust,并在容器内运行(参照 03 的 Docker 示例)。

参考:CLI Integration、JSON Event Stream、structured-output.ts。

05

项目 5:用 SDK / RPC 构建自己的聊天前端 ★★★★★

方案 A(SDK,Node):

  1. 用 createAgentSession() + SessionManager.continueRecent() 构建一个 HTTP/WebSocket 服务;
  2. 把 session.subscribe() 的 text_delta、tool_execution_* 事件推给前端;
  3. 前端在运行中发送的消息走 session.steer() / session.followUp();
  4. 通过 extensionFactories 注入一个内联扩展,提供业务工具(如查询内部 API);
  5. 以 agent_settled 作为"完成"信号;页面关闭时 session.dispose()。

方案 B(RPC,任意语言):

  1. 用 Python/Go 启动 pi --mode rpc;
  2. 实现命令 id 关联、事件分发、streamingBehavior;
  3. 实现扩展 UI 子协议:收到 extension_ui_request(select/confirm)时在前端弹窗,并回传 extension_ui_response;
  4. 实现会话列表(get_state、switch_session、get_tree)。

验收:能在浏览器中对话、看到工具调用、中途 steer、确认危险命令。

参考:06 篇;examples/sdk/*、examples/rpc-client.ts、examples/rpc-extension-ui.ts、examples/extensions/rpc-demo.ts。

06

项目 6:MCP + Codemode + 虚拟模型 + 打包发布 ★★★★★

目标:把前面的成果整合成一个可分发的 Pi 包。

步骤

  1. 接入一个真实 MCP 服务器(如 GitHub、文件系统或你公司的服务),用 toolExposure 设计:查询类 codemode、高频工具 direct、删除类 hidden;
  2. 开启 +codemode,设计一个提示模板 /triage,引导模型用 codemode 并行拉取 issue、过滤、汇总;
  3. (可选)若有分类器模型凭据,在脚本中用 models.classify() 给 issue 打紧急度分;
  4. 实现虚拟模型 router/auto:规划用强模型、执行用便宜模型,路由状态存 phase;
  5. 把 guard 扩展、git 扩展、模板、技能、虚拟模型扩展打包成 @yourname/pi-kit:正确设置 peerDependencies、pi 清单和 pi-package 关键字;
  6. 用 pi install ./pi-kit 本地验证,pi config 检查资源;再在另一台机器用 pi install git:...@v0.1.0 安装。

验收:新机器一条命令装好全套;/triage 能产出带紧急度的 issue 列表;/session 能看到不同物理模型的费用。

参考:07 篇;MCP、Codemode、Virtual Models、Pi Packages 文档;jev-router.ts。


07

自测清单

  • [ ] 能解释 turn、run、agent_end 与 agent_settled 的区别
  • [ ] 能说出 /tree、/fork、/clone 的差异
  • [ ] 知道压缩何时触发、保留什么、原始条目是否删除
  • [ ] 能为一个需求选出最小的定制机制
  • [ ] 能写出带 TypeBox 参数、details 和渲染的工具
  • [ ] 知道扩展的状态为什么要从 getBranch() 重建
  • [ ] 能用 SDK 创建内存会话并订阅流式事件
  • [ ] 能正确解析 RPC/JSON 的 JSONL(不用 readline)
  • [ ] 能解释 MCP 四种 exposure 和 codemode 的价值
  • [ ] 知道 Pi 包中哪些依赖应放 peerDependencies
  • [ ] 清楚 Pi 默认没有权限系统,知道如何用容器隔离

08

参考资料索引

官方文档(https://pi.dev/docs/latest/)

  • 入门

    页面
    Overview(/)、Quickstart(/quickstart)、How Pi Works(/how-pi-works)
  • 使用

    页面
    Usage(/usage)、Models(/models)、Sessions(/sessions)、Security(/security)、Containerization(/containerization)、llama.cpp(/llama-cpp)、Terminal Setup(/terminal-setup)
  • 定制

    页面
    Configuration(/configuration)、Settings(/settings)、Prompt Templates(/prompt-templates)、Skills(/skills)、Themes(/themes)、Keybindings(/keybindings)
  • 扩展

    页面
    Extensions(/extensions)、Terminal UI(/tui)、Custom Providers(/custom-provider)、Virtual Models(/virtual-models)、Packages(/packages)
  • 工具与 MCP

    页面
    MCP(/mcp)、Codemode(/codemode)、CLI(/cli)
  • 集成

    页面
    SDK(/sdk)、CLI Integration(/cli-integration)、RPC(/rpc)、RPC Commands(/rpc-commands)、RPC Extension UI(/rpc-extension-ui)、JSON(/json)
  • 参考

    页面
    Session Format(/session-format)、Compaction(/compaction)、Message Types(/message-types)、Providers(/providers)、Models Configuration(/models-configuration)、Slash Commands(/slash-commands)、Environment Variables(/environment-variables)
分类页面
入门Overview(/)、Quickstart(/quickstart)、How Pi Works(/how-pi-works)
使用Usage(/usage)、Models(/models)、Sessions(/sessions)、Security(/security)、Containerization(/containerization)、llama.cpp(/llama-cpp)、Terminal Setup(/terminal-setup)
定制Configuration(/configuration)、Settings(/settings)、Prompt Templates(/prompt-templates)、Skills(/skills)、Themes(/themes)、Keybindings(/keybindings)
扩展Extensions(/extensions)、Terminal UI(/tui)、Custom Providers(/custom-provider)、Virtual Models(/virtual-models)、Packages(/packages)
工具与 MCPMCP(/mcp)、Codemode(/codemode)、CLI(/cli)
集成SDK(/sdk)、CLI Integration(/cli-integration)、RPC(/rpc)、RPC Commands(/rpc-commands)、RPC Extension UI(/rpc-extension-ui)、JSON(/json)
参考Session Format(/session-format)、Compaction(/compaction)、Message Types(/message-types)、Providers(/providers)、Models Configuration(/models-configuration)、Slash Commands(/slash-commands)、Environment Variables(/environment-variables)

完整 URL 形如 https://pi.dev/docs/latest/extensions。

源码与示例

生态

版本里程碑(摘自 CHANGELOG)

  • 0.99.0

    日期
    2026-09-29
    亮点
    Codemode 与 MCP、system 主题、Sign in with ChatGPT、虚拟模型、分类器模型
  • 0.99.2

    日期
    2026-09-30
    亮点
    默认 codemode 暴露的 MCP 不再列入 codemode 描述/不阻塞首次提示;/reload 启用新加入 defaultTools 的工具
  • 1.0.0

    日期
    2026-10-01
    亮点
    默认全屏、codemode 提示词减少约 40%、codemode 图像生成、MCP OAuth 加强
  • 1.1.0

    日期
    2026-10-07
    亮点
    --tools +name/-name、GPT-6 Luna 分类器与图像分类、OSC 7501 程序状态
  • 1.2.0

    日期
    2026-10-12
    亮点
    发布配置 JSON Schema、before_agent_start 覆盖扩展触发的运行(破坏性变化)
  • 1.3.0

    日期
    2026-10-15
    亮点
    sessionShareDestination、恢复会话保留激活工具、切换模型时丢弃旧模型思考
版本日期亮点
0.99.02026-09-29Codemode 与 MCP、system 主题、Sign in with ChatGPT、虚拟模型、分类器模型
0.99.22026-09-30默认 codemode 暴露的 MCP 不再列入 codemode 描述/不阻塞首次提示;/reload 启用新加入 defaultTools 的工具
1.0.02026-10-01默认全屏、codemode 提示词减少约 40%、codemode 图像生成、MCP OAuth 加强
1.1.02026-10-07--tools +name/-name、GPT-6 Luna 分类器与图像分类、OSC 7501 程序状态
1.2.02026-10-12发布配置 JSON Schema、before_agent_start 覆盖扩展触发的运行(破坏性变化)
1.3.02026-10-15sessionShareDestination、恢复会话保留激活工具、切换模型时丢弃旧模型思考

学习建议:Pi 迭代很快,遇到与本文不一致的地方,请以 pi --help、已安装版本的 .d.ts 和最新 CHANGELOG 为准。