PiMono Agent 框架源码精读PiMono Agent Framework: Source Code Deep Dive

看懂 Agent 的真相 —— 一个非技术人的源码自学笔记Understanding How Agents Really Work — A Non-Developer's Source Code Study Notes

3
核心章节Core Chapters
~2000
行核心源码精读Lines of Core Code
27
LLM 供应商适配LLM Providers

为什么读 Agent 源码Why Read Agent Source Code

从"通用助手"到"AI 员工",Agent 好像正在接管世界。大模型的魔力我们可以通过聊天直观感知,那 Agent 的魔力从哪里来,比大模型多了什么?弄清楚这个问题最好的方法是去看 Agent 源码。

From "general assistants" to "AI employees," Agents seem to be taking over the world. We can intuitively feel the magic of large language models through chatting — but where does the Agent's magic come from? What does it add beyond the LLM itself? The best way to answer that question is to read Agent source code.

PiMono 是 OpenClaw 的内核引擎。核心只有 5 个文件、2002 行代码,但体现了所有 Agent 的通用模式 —— 代码量最少,模式最完整,理解 Agent 核心性价比最高的一次阅读。

PiMono is the kernel engine of OpenClaw. Its core is just 5 files and 2,002 lines of code, yet it embodies all universal Agent patterns — the least code, the most complete patterns, and the highest return-on-investment reading for understanding how Agents work.

这份笔记来自非技术背景的自学者。阅读方法:先看架构全景图建立整体感 → 每章上篇是左源码右中文讲解,逐行过一遍 → 下篇是提炼后的中文模块概览,梳理全貌 → 打印下篇反复看,直到变成直觉。所有术语即时解释,从前往后读就好。

These notes are from a self-taught learner with a non-technical background. Reading approach: start with the architecture overview to build the big picture → each chapter's Part A has source code on the left and explanations on the right, going line by line → Part B is a distilled module overview → print Part B and review until it becomes intuition. All terms are explained inline — just read from start to finish.

架构全景Architecture Overview

蓝色模块是本系列精读的部分,点击可跳转Blue modules are covered in this series — click to navigate

pi-coding-agent 产品层Product Layer
用户直接使用的完整产品 —— 界面、对话历史、技能扩展等(6 万多行)
The complete product users interact with — UI, conversation history, skill extensions, etc. (60,000+ lines)
本系列聚焦底下两层 —— 理解了它们,就理解了所有 Agent 的核心
This series focuses on the two layers below — understand them, and you understand the core of all Agents
依赖depends on
pi-agent-core 引擎层Engine Layer
Agent 的心脏 —— 5 个文件实现了「问 AI → 用工具 → 把结果告诉 AI」的核心循环
The heart of the Agent — 5 files implementing the core loop: ask AI → use tools → feed results back to AI
pi-tui
终端界面渲染
Terminal UI rendering
纯显示,不影响核心逻辑
Display only, doesn't affect core logic
依赖depends on
pi-ai LLM 层LLM Layer
Agent 的翻译官 —— 让同一套代码对接 27 家不同的 AI 模型供应商
The Agent's translator — one codebase connecting to 27 different AI model providers
极简设计:核心只做一件事 —— 驱动「问 AI → 用工具 → 喂回结果」的循环。其他能力(更多工具、子 Agent、规划模式)全部按需加载,不塞进内核。这就是为什么核心只有 2000 行。 Minimalist Design: The core does one thing only — drive the "ask AI → use tools → feed results back" loop. Everything else (more tools, sub-agents, planning modes) is loaded on demand, never stuffed into the kernel. That's why the core is only 2,000 lines.

章节目录Table of Contents

1

核心循环 — agent-loop.tsCore Loop — agent-loop.ts

引擎层Engine Layer

Agent 的心脏,695 行。问 AI → AI 说要用工具 → 执行工具 → 结果告诉 AI → 再问。上篇:左源码右中文讲解;下篇:中文模块概览。

The Agent's heart, 695 lines. Ask AI → AI requests tools → execute tools → feed results back → ask again. Part A: source code with line-by-line explanations; Part B: module overview.

2

管理层 — agent.tsManagement Layer — agent.ts

引擎层Engine Layer

引擎只管跑循环,这个文件是「整车」—— 记住对话历史、管理启停、运行中插入新消息、向外通报进度。同样上下两篇。

The engine only runs the loop. This file is the "whole car" — remembering conversation history, managing start/stop, injecting messages mid-run, and reporting progress. Also in two parts.

3

AI 适配层 — 一套代码对接 27 家供应商AI Adapter Layer — One Codebase for 27 Providers

LLM 层LLM Layer

Agent 怎么和 AI 模型通信?27 家供应商(Anthropic、OpenAI、Google……)只用 9 套代码全部搞定。架构图 + 完整调用链路。

How does the Agent communicate with AI models? 27 providers (Anthropic, OpenAI, Google...) handled with just 9 code modules. Architecture diagrams + complete call chains.

源码仓库github.com/badlogic/pi-mono
精读版本:基于 2025 年 5 月的 PiMono 源码
说明:本资料的注释和解读均为学习笔记,帮助理解 Agent 架构的通用设计模式。标注了通用模式的概念在大多数 Agent 框架中通用,标注了Pi 特有的是 PiMono 的特定实现。
Source Repository: github.com/badlogic/pi-mono
Version Studied: Based on PiMono source code from May 2025
Note: All annotations and interpretations are study notes to help understand universal Agent architecture patterns. Concepts tagged Universal Pattern are common across most Agent frameworks; those tagged Pi-Specific are particular to PiMono's implementation.