微信扫码
添加专属顾问
我要投稿
深入解析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+中大型企业
2025-12-16
阿里重磅开源 0.5B TTS + 0.8B ASR,支持跨语种音色克隆、说唱识别!
2025-12-15
智谱手机 Agent 开源一周,iOS 版就来了
2025-12-15
OpenEvals下一代AI模型评估标准
2025-12-15
AutoGLM:推倒那面墙
2025-12-15
狂揽162K Star!n8n 2.0强势来袭,这次改动有点狠。
2025-12-14
ollama v0.13.3 最新发布:新增模型与功能优化详细解读
2025-12-14
OpenAI突然开源新模型!99.9%的权重是0,新稀疏性方法代替MoE
2025-12-14
AutoGLM开源:手机AI Agent的“安卓时刻”来了
2025-10-20
2025-11-19
2025-10-27
2025-10-27
2025-10-03
2025-09-29
2025-10-29
2025-11-17
2025-09-29
2025-11-07
2025-11-12
2025-11-10
2025-11-03
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17