微信扫码
添加专属顾问
我要投稿
深入解析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-06-12
当 AI Coding Agent 成为基础设施:我们为什么要开源 LoongSuite Pilot
2026-06-09
CPU 跑得比 Whisper GPU还快的开源语音识别,本地部署
2026-06-09
别再问我什么是爱马仕!
2026-06-09
Open Design:开源界的 Claude Design 替代品,61.7K Star,21+ Agent 都能跑,本地第一
2026-06-08
我开源了 PPT-Library:让历史 PPT 变成 AI 可复用资产
2026-06-08
knowledge-work-plugins:Anthropic 官方开源的知识工作插件集,19,000+Star
2026-06-08
从搜索、点击、表单填写到复杂网页操作,让 AI 像人一样使用浏览器
2026-06-08
比Codex快4倍!终于有开源模型卷本地Agent执行效率了~
2026-03-30
2026-04-09
2026-04-03
2026-03-23
2026-04-01
2026-03-31
2026-03-30
2026-04-18
2026-04-18
2026-03-20
2026-05-30
2026-05-16
2026-04-22
2026-04-21
2026-04-15
2026-04-09
2026-04-01
2026-03-17