1000行代码实现极简版openclaw(附源码)(11)
10 - 完整数据流追踪github 源码欢迎star目标通过一个完整的例子追踪数据在整个系统中的流动。场景用户输入创建一个 test.txt 文件内容是 Hello数据流图解┌─────────────────────────────────────────────────────────────────┐ │ Step 1: 用户输入 │ ├─────────────────────────────────────────────────────────────────┤ │ TerminalChannel.start() │ │ └── rl.question(You: , input) │ │ └── input 创建一个 test.txt 文件内容是 \Hello\ │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 2: Channel 创建 Message │ ├─────────────────────────────────────────────────────────────────┤ │ const message { │ │ id: uuid-1, │ │ role: user, │ │ content: 创建一个 test.txt 文件内容是 \Hello\, │ │ timestamp: 1234567890 │ │ } │ │ │ │ this.onMessage?.(message, reply) // 调用 Gateway 注入的回调 │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 3: Gateway.handleMessage() │ ├─────────────────────────────────────────────────────────────────┤ │ const sessionId terminal_default │ │ let session this.sessions.get(sessionId) │ │ │ │ // 新会话创建 Session 对象 │ │ session { │ │ id: terminal_default, │ │ channelType: terminal, │ │ messages: [], │ │ createdAt: 1234567890, │ │ updatedAt: 1234567890, │ │ metadata: {} │ │ } │ │ this.sessions.set(sessionId, session) │ │ │ │ // 调用 Agent │ │ const response await this.agent.process(message.content, session)│ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 4: Agent.process() - 添加用户消息 │ ├─────────────────────────────────────────────────────────────────┤ │ session.messages.push({ │ │ id: uuid-1, │ │ role: user, │ │ content: 创建一个 test.txt 文件内容是 \Hello\ │ │ }) │ │ │ │ // 进入 ReAct 循环 │ │ const response await this.reactLoop(session, depth0) │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 5: Agent.reactLoop() - 第 1 轮 THINK │ ├─────────────────────────────────────────────────────────────────┤ │ this.setState(thinking) │ │ │ │ // 构建 LLM 请求 │ │ const messages [ │ │ { role: system, content: You are helpful... }, │ │ { role: user, content: 创建一个 test.txt 文件内容是 Hello }│ │ ] │ │ │ │ const request { │ │ model: deepseek-chat, │ │ messages: messages, │ │ tools: [write_file, read_file, list_files, ...] │ │ } │ │ │ │ const response await provider.complete(request) │ │ │ │ // LLM 返回 │ │ response { │ │ content: 我来创建文件, │ │ toolCalls: [{ │ │ id: call-1, │ │ name: write_file, │ │ parameters: { path: test.txt, content: Hello } │ │ }] │ │ } │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 6: Agent.reactLoop() - 第 1 轮 ACT │ ├─────────────────────────────────────────────────────────────────┤ │ this.setState(acting) │ │ │ │ // 添加 assistant 消息关键 │ │ session.messages.push({ │ │ id: uuid-2, │ │ role: assistant, │ │ content: 我来创建文件, │ │ metadata: { │ │ toolCalls: [{ id: call-1, name: write_file, ... }] │ │ } │ │ }) │ │ │ │ // 执行工具 │ │ const tool this.tools.get(write_file) │ │ const result await tool.execute({ │ │ path: test.txt, │ │ content: Hello │ │ }) │ │ // result: 已写入: test.txt │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 7: Agent.reactLoop() - 第 1 轮 OBSERVE │ ├─────────────────────────────────────────────────────────────────┤ │ // 添加 tool 消息 │ │ session.messages.push({ │ │ id: uuid-3, │ │ role: tool, │ │ content: 已写入: test.txt, │ │ metadata: { toolCallId: call-1 } │ │ }) │ │ │ │ // 当前 session.messages: │ │ [ │ │ { role: user, content: 创建... }, │ │ { role: assistant, metadata: { toolCalls: [...] } }, │ │ { role: tool, content: 已写入... } │ │ ] │ │ │ │ // 递归进入第 2 轮 │ │ return this.reactLoop(session, depth1) │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 8: Agent.reactLoop() - 第 2 轮 THINK │ ├─────────────────────────────────────────────────────────────────┤ │ this.setState(thinking) │ │ │ │ // 构建 LLM 请求包含完整历史 │ │ const messages [ │ │ { role: system, content: You are helpful... }, │ │ { role: user, content: 创建一个 test.txt 文件... }, │ │ { role: assistant, content: 我来创建文件, tool_calls: [{...}] },│ │ { role: tool, content: 已写入: test.txt, tool_call_id: call-1 }│ │ ] │ │ │ │ const response await provider.complete(request) │ │ │ │ // LLM 看到历史里已经有 write_file 的结果任务完成 │ │ response { │ │ content: 文件已创建完成, │ │ toolCalls: [] // 无工具调用 │ │ } │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 9: Agent.reactLoop() - 完成 │ ├─────────────────────────────────────────────────────────────────┤ │ // 没有 toolCalls任务完成 │ │ this.setState(idle) │ │ │ │ return { │ │ id: uuid-4, │ │ role: assistant, │ │ content: 文件已创建完成, │ │ timestamp: 1234567890 │ │ } │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 10: Gateway 返回响应 │ ├─────────────────────────────────────────────────────────────────┤ │ session.messages.push(response) │ │ │ │ // 调用 Channel 的 reply 回调 │ │ reply(response) │ └────────────────────────┬────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Step 11: Channel 显示给用户 │ ├─────────────────────────────────────────────────────────────────┤ │ console.log(\nAI: ${reply.content}\n) │ │ │ │ 输出: │ │ AI: 文件已创建完成 │ └─────────────────────────────────────────────────────────────────┘总结通过这个追踪我们可以看到数据是如何流动的Channel → Gateway → Agent → LLM → Tools → …消息历史的重要性让 LLM 看到完整上下文ReAct 循环的工作方式Think → Act → Observe → Loop完结恭喜你已经完成了 TurboClaw 的完整教程。你现在应该能够理解 AI Agent 的核心原理独立实现一个简单的 Agent扩展和定制功能下一步动手实践尝试添加自己的工具和功能
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2448323.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!