这一章同样分为上下两篇:This chapter also has two parts:
左边是 agent.ts 的完整原始代码,右边是逐段中文解读。543 行代码被拆成 9 个功能区块:配置合并、消息历史、启动入口、上下文注入、中途插话、事件通知、生命周期管理等。
Left column: the full original code of agent.ts. Right column: section-by-section explanations. 543 lines broken into 9 functional blocks: config merging, message history, entry point, context injection, mid-run message insertion, event notifications, lifecycle management, and more.
适合第一遍精读,搞懂管理层的每个机制。
Best for your first close reading — understand every mechanism in the management layer.
用中文伪代码重述 agent.ts 的 9 个区块,加上 types.ts 里最关键的类型合同。梳理管理层和引擎层之间"谁调谁、数据怎么流"的完整关系。
Restates agent.ts's 9 blocks in pseudocode, plus the key type contracts from types.ts. Maps out the complete relationship between the management layer and engine layer — who calls whom, and how data flows.
已优化排版,支持直接 Ctrl+P 打印 / 保存 PDF。读完上篇后用它建立完整画面,和第 1 章放在一起就是引擎层速查手册。
Print-optimized layout (Ctrl+P / Save as PDF). Use it after Part A for the complete picture — together with Chapter 1, it becomes a quick reference for the entire engine layer.