MewCode TypeScript Agent Loop source-code walkthrough
A source-code analysis walks through MewCode’s TypeScript Agent Loop and its ReAct-style sequence of calling an LLM, executing tools, and returning results. It covers the src/agent/ module, AgentEvent streaming, the run() loop, concurrent tool execution, permission checks, AbortSignal cancellation, Plan Mode, and output-recovery mechanisms. The supplied material is a single source-code walkthrough and does not independently validate the implementation.
The walkthrough provides a concrete TypeScript example of how an agent loop organizes LLM calls, tool execution, event streaming, permissions, cancellation, and recovery. The report does not establish that the code is production-validated.
TypeScript源码解析:Agent主循环与事件流
理论篇讲了 Agent Loop 的设计理念和 ReAct 范式,这篇带你走读 MewCode 的真实代码,看看「调 LLM → 执行工具 → 喂回结果」这条主线是怎么实现的。
模块概览
Agent Loop 的代码集中在 src/agent/ 目录,三个文件:
| 文件 | 职责 |
|---|---|
events.ts | 定义 AgentEvent 联合类型,是循环对外通信的唯一出口 |
agent.ts | Agent 类本体,包含主循环 run() 、工具执行、权限检查、自愈逻辑 |
streaming-executor.ts | 工具并发执行器,接收 pending 调用并用 Promise.all 并行跑 |
agent.ts 是整个项目最核心的文件,所有其他模块最终都汇聚到这里。