微信扫码
添加专属顾问
我要投稿
深入解析OpenManus的内部结构和功能实现,带你一探智能代理的奥秘。 核心内容: 1. OpenManus概览与目录结构解析 2. 核心Agent类的设计与功能 3. 代码调用逻辑梳理与任务执行流程

async run(request):if state != IDLE:raise RuntimeErrorif request:update_memory(USER, request)async with state_context(RUNNING):while current_step < max_steps and state != FINISHED:current_step += 1step_result = await step()if is_stuck():handle_stuck_state()results.append(step_result)if current_step >= max_steps:state = IDLEresults.append("Terminated: Reached max steps")return "\n".join(results)
async step(): should_act = await think() # 调用子类的 think() if should_act: return await act() # 调用子类的 act() else: return "Thinking complete - no action needed"
async run(request):if request:await create_initial_plan(request)return await super().run() # 调用 ToolCallAgent.run() -> ReActAgent.run() -> BaseAgent.run()async create_initial_plan(request):# 1. 构造消息,让 LLM 创建计划messages = [...]response = await llm.ask_tool(..., tool_choice=ToolChoice.AUTO)# 2. 处理 LLM 的响应,提取工具调用(应该是 planning 工具的调用)for tool_call in response.tool_calls:if tool_call.function.name == "planning":result = await execute_tool(tool_call) # 执行 planning 工具# 3. 将工具执行结果(计划)存入内存update_memory(TOOL, result, tool_call_id=tool_call.id)async think():prompt = f"CURRENT PLAN STATUS:\n{await self.get_plan()}\n\n{self.next_step_prompt}"self.messages.append(Message.user_message(prompt))self.current_step_index = await self._get_current_step_index()result = await super().think() # 调用 ToolCallAgent 的 think()if result and self.tool_calls:# 记录工具和步骤的关联latest_tool_call = self.tool_calls[0]if latest_tool_call 不是 planning tool 且 不是 special tool:self.step_execution_tracker[latest_tool_call.id] = {"step_index": self.current_step_index,"tool_name": latest_tool_call.function.name,"status": "pending"}return resultasync act():result = await super().act() # 调用 ToolCallAgent 的 act()if self.tool_calls:latest_tool_call = self.tool_calls[0]if latest_tool_call.id in self.step_execution_tracker:self.step_execution_tracker[latest_tool_call.id]["status"] = "completed"self.step_execution_tracker[latest_tool_call.id]["result"] = resultif latest_tool_call 不是 planning tool 且 不是 special tool:await self.update_plan_status(latest_tool_call.id)return resultasync update_plan_status(tool_call_id):# 1. 检查 tool_call_id 是否在 tracker 中,以及状态是否为 completed# 2. 调用 planning 工具的 mark_step 命令,将对应步骤标记为 completedasync _get_current_step_index():# 1. 获取当前计划 (文本)# 2. 解析计划文本,找到第一个 [ ] 或 [→] 的步骤# 3. 调用 planning 工具的 mark_step 命令,将当前步骤设置为 in_progress# 4. 返回步骤索引
async think(): self.working_dir = await self.bash.execute("pwd") # 获取当前工作目录 self.next_step_prompt = self.next_step_prompt.format(current_dir=self.working_dir) # 更新提示 return await super().think() # 调用 ToolCallAgent 的 think()53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-02-03
Claude Code——智能路由与切换工具
2026-02-03
作为投资方,我们试了试Kimi K2.5
2026-02-03
暴涨11.7k Star!复刻Manus上下文工程的开源Skills,Claude code等多个平台即插即用
2026-02-03
ollama v0.15.4 更新:OpenClaw全面上线,优化集成流程与工具解析能力大升级!
2026-02-03
阶跃星辰 Step 3.5 Flash 上线!更快更强更稳的 Agent 大脑,开源!
2026-02-02
一分钟部署OpenClaw+QQ,国内最爽的一键启动!
2026-02-02
这家字节系AI初创,推出首个开源Agent Skills Builder!对话Refly.AI李锦威:大多数Agent都停留在专业用户层面,没有做到真正的普惠
2026-02-02
OCR又出宠OpenDoc,速度超MinerU6倍
2025-11-19
2026-01-27
2025-12-22
2025-12-10
2025-11-17
2026-01-12
2025-11-07
2025-12-23
2026-01-29
2026-01-06
2026-01-28
2026-01-26
2026-01-21
2026-01-21
2026-01-20
2026-01-16
2026-01-02
2025-12-24