微信扫码
添加专属顾问
我要投稿
深入解析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-10-29
AI本地知识库+智能体系列:手把手教你本地部署 n8n,一键实现自动采集+智能处理!
2025-10-29
n8n如何调用最近爆火的deepseek OCR?
2025-10-29
OpenAI终于快要上市了,也直面了这23个灵魂拷问。
2025-10-29
保姆级教程:我用Coze干掉了最烦的周报
2025-10-29
维基百科,终结了!马斯克开源版上线,用AI重写「真相」
2025-10-28
腾讯开源Nano Banana,我总结了15种邪修玩法(附提示语)
2025-10-28
牛逼,DeepSeek-OCR 最新免费,引爆文档处理效率的黑科技模型
2025-10-28
小红书入局AI智能体开源DeepAgent,在计划什么更新?
2025-08-20
2025-09-07
2025-08-05
2025-08-20
2025-08-26
2025-08-22
2025-07-31
2025-09-06
2025-08-06
2025-10-20
2025-10-29
2025-10-28
2025-10-13
2025-09-29
2025-09-17
2025-09-09
2025-09-08
2025-09-07