🗺️ 概念对照表 ★
本教程的收官章!用一组大表把基础理论、LangChain、LangGraph、OpenCode 四个层面的概念两两对照。这是你复习和查阅的终极参考——当你能看懂每一行的对应关系,说明你已真正打通了 Agent 的全部知识体系。
本章目标
- 通过对照表验证自己对所有核心概念的理解
- 建立"同一概念在不同框架/产品中的映射"的完整心智模型
- 获得一份可长期查阅的"概念词典"
- 看清从"理论"到"框架"到"生产产品"的完整演进脉络
这章不是"新知识",而是知识地图。建议:① 学完后通读,验证理解;② 日后写代码忘了概念,回来查;③ 遇到新框架,用这张表"对号入座"快速理解。每行都标了源码锚点,方便深挖。
核心概念四维对照
这张表是全教程最重要的一张——同一概念在四层中的体现:
| 概念 | 基础理论(F) | LangChain(LC) | LangGraph(LG) | OpenCode(OC) |
|---|---|---|---|---|
| 消息 | system/user/assistant/tool 角色 | HumanMessage 等messages/base.py:93 |
状态里的 messages 字段 | MessageV2 + Part 体系 |
| 工具调用 | tool_calls 结构化指令 | AIMessage.tool_callsmessages/ai.py:160 |
AIMessage 的 tool_calls | ToolPart(流式聚合) |
| 提示 | system prompt 定规则 | ChatPromptTemplateprompts/chat.py:794 |
节点内的 prompt 组装 | system.ts 动态选 prompt |
| 模型 | LLM API 调用 | BaseChatModelchat_models.py:272 |
节点内调 model.invoke | Provider + LLM.Service |
| 绑工具 | 给模型工具 schema | bind_tools()chat_models.py:2338 |
模型 bind_tools | SessionTools.resolve |
| 工具 | name+desc+schema+执行 | @tool / BaseTooltools/convert.py:18 |
ToolNode tool_node.py:622 |
Tool.definetool.ts:151 |
| 组合 | 串联工作单元 | | 管道(LCEL)runnables/base.py:712 |
add_edge / 条件边 | Effect.gen + yield* |
| 循环 | ReAct 思考-行动-观察 | AgentExecutor while agents/agent.py |
图的 super-step 循环 | runLoop while(true) prompt.ts:1081 |
| 继续/停止 | Action / Finish 二态 | AgentAction / AgentFinish agents.py:44/148 |
tools_condition → END tool_node.py:1582 |
hasToolCalls + result 判断 |
| 状态 | 历史消息 + 中间结果 | intermediate_steps 列表 | State TypedDict + reducer graph/state.py:130 |
Session DB(消息+Part) |
| 记忆/持久化 | 跨调用保留历史 | memory 模块(旧) | Checkpointer + thread_id checkpoint/base:176 |
Session DB 按 sessionID |
| 人在回路 | 执行前人工批准 | (无原生支持) | interrupt + Command types.py:811/758 |
Permission.ask(三层) |
| 流式 | 实时输出过程 | Runnable.stream() | 7 种 StreamMode types.py:120 |
LLMEvent 流(16 种归一) |
| 并行 | 同时做多件事 | RunnableParallel(字典) | Send map-reduce types.py:664 |
Effect.all + 子 agent |
| 上下文管理 | 防 context 爆炸 | (需手动) | RemoveMessage 裁剪 | compaction 压缩 compaction.ts |
| 步数限制 | 防死循环 | max_iterations | recursion_limit | agent.steps + doom_loop 检测 |
Agent 循环的四种实现形态
同一个循环,四种工程实现。这是"为什么学这么多"的核心答案——理解了循环,就理解了所有框架的本质:
| 实现 | 循环机制 | 控制流 | 状态 | 特点 |
|---|---|---|---|---|
| 理论模型(F3) | ReAct 循环 | Action/Finish 二态 | 概念上的"记忆" | 心智模型,所有实现的基础 |
| LangChain(LC8) | for 循环 |
解析 AgentAction/Finish | intermediate_steps | 最直接,但状态/中断能力弱 |
| LangGraph(LG1/5/8) | Pregel super-step | 条件边 + END | State + Channel | 声明式、可视化、可并行 |
| OpenCode(OC2) | while(true) |
hasToolCalls + result | Session DB | 命令式、生产级、全链路流式 |
数据模型对照
| LangChain 数据 | LangGraph 数据 | OpenCode 数据 | ||
|---|---|---|---|---|
HumanMessage | → | messages 列表里的 user 项 | → | User Message(含 Part) |
AIMessage + tool_calls | → | messages 里的 assistant 项 | → | Assistant Message + ToolPart |
ToolMessage | → | messages 里的 tool 结果 | → | ToolPart 的 output 字段 |
AgentAction | → | (隐式:tool_calls 非空) | → | (隐式:hasToolCalls) |
AgentFinish | → | tools_condition → END | → | result === "stop" → break |
AgentStep | → | 一个 super-step 的执行 | → | runLoop 一次迭代 |
| intermediate_steps | → | State(TypedDict) | → | Session 的消息/Part 列表 |
核心源码锚点速查
贯穿全教程的关键源码位置,方便深挖时定位:
LangChain(libs/core/langchain_core/)
| 概念 | 源码锚点 |
|---|---|
| 消息基类 | messages/base.py:93 BaseMessage |
| AI 消息 + tool_calls | messages/ai.py:160 AIMessage |
| 聊天提示模板 | prompts/chat.py:1124 from_messages |
| 模型抽象 | language_models/chat_models.py:272 |
| 绑定工具 | chat_models.py:2338 bind_tools |
| LCEL 核心 | runnables/base.py:133 Runnable |
| @tool 装饰器 | tools/convert.py:18 tool |
| Agent Schema | agents.py:44/148 AgentAction/Finish |
LangGraph(libs/langgraph/langgraph/)
| 概念 | 源码锚点 |
|---|---|
| StateGraph | graph/state.py:130 |
| add_node / add_edge | state.py:662 / :915 |
| 条件边 | state.py:969 add_conditional_edges |
| START/END | constants.py:28 |
| add_messages reducer | graph/message.py:61 |
| Pregel 引擎 | pregel/main.py:449 |
| BSP 循环 tick | pregel/_loop.py:592 |
| Channel 基类 | channels/base.py:19 |
| interrupt / Command | types.py:811 / :758 |
| StreamMode | types.py:120 |
| create_react_agent | prebuilt/chat_agent_executor.py:278 |
| tools_condition | prebuilt/tool_node.py:1582 |
| Checkpointer | checkpoint/base/__init__.py:176 |
OpenCode(packages/opencode/src/)
| 概念 | 源码锚点 |
|---|---|
| ★ Agent 循环 | session/prompt.ts:1081 runLoop |
| LLM 调用服务 | session/llm.ts LLM.Service |
| 流事件处理 | session/processor.ts:625 process |
| 工具定义 | tool/tool.ts:151 define |
| 工具注册表 | tool/registry.ts |
| 工具解析(绑给LLM) | session/tools.ts:39 resolve |
| Agent 定义 | agent/agent.ts:140 build |
| system prompt 选择 | session/system.ts:26 |
| Session 持久化 | session/session.ts |
| 权限合并 | agent/agent.ts:119 |
| doom_loop 检测 | session/processor.ts:354 |
| 上下文压缩 | session/compaction.ts |
| 子 Agent 权限 | agent/subagent-permissions.ts:14 |
学习路径回顾
- 基础理论(F1-F3)—— 建立术语和心智模型,画出核心循环图
- LangChain(LC1-LC8)—— 掌握积木式组件,从消息到 Agent
- LangGraph(LG1-LG8)—— 升级为有状态、可中断、可恢复的图
- OpenCode(OC1-OC6)—— 看生产级产品如何落地,工程实践 + 概念对照
核心收获:你现在理解了"同一套 Agent 循环"在理论、框架、产品中的完整体现。无论未来遇到什么新框架/新工具,都可以用这张知识地图快速对号入座。
下一步建议
动手构建
用 LangGraph 从零构建一个自己的 Agent(带工具、记忆、HITL),把知识用起来。
精读源码
按 OC6 的源码锚点,逐个深入。建议从 runLoop 和 LCEL 的 base.py:133 开始。
扩展阅读
研究 LangGraph 的 Pregel 引擎内部(LG5 进阶指引)、多 Agent supervisor 模式。
贡献 OpenCode
尝试给 OpenCode 加一个自定义工具或插件,在生产代码里实践所学。
Agent 领域变化快,但核心思想稳定——就是本章对照表里的那些概念。新框架(无论叫什么)都在实现同一个循环。把握住"理论 → LangChain → LangGraph → 生产产品"这条主线,你就有了判断和学习的罗盘。祝你在 Agent 的世界里探索愉快!🚀
总结
- 核心概念四维对照表是全教程的知识地图——同一概念在理论/LC/LG/OC 中的体现。
- Agent 循环有四种实现形态(理论/for/图/while),但本质相同。
- 源码锚点速查表是深入研究的入口。
- 从理论到产品是一条清晰的演进线:抽象 → 积木 → 图 → 工程化。
恭喜你完成全部 25 章!回到 首页 看看你的学习进度,或重新打开任何一章复习。这份对照表会一直是你的得力助手。如果想继续深入,按"下一步建议"行动起来吧!