微信扫码
添加专属顾问
我要投稿
深入解析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-22
Agent终于有了自己的邮箱!腾讯Agently Mail详解
2026-06-20
GLM-5.2本地部署:744B参数模型如何在Mac上跑
2026-06-20
Hermes Agent v0.17发布:AI助手开始扩展触角
2026-06-18
开源中医 Skill:把倪海厦课程变成可检索知识库
2026-06-18
Gemma 4 QAT 模型现已推出: 支持日常端侧设备与消费级 GPU 本地流畅运行
2026-06-17
GLM-5.2 技术解读:智谱百万上下文的新一代旗舰模型
2026-06-16
26.1%的AI编程技能有漏洞:NVIDIA开源 SkillSpector 能扫出什么?
2026-06-12
当 AI Coding Agent 成为基础设施:我们为什么要开源 LoongSuite Pilot
2026-03-30
2026-04-09
2026-04-03
2026-04-01
2026-03-31
2026-03-30
2026-04-18
2026-04-18
2026-03-31
2026-04-02
2026-06-16
2026-05-30
2026-05-16
2026-04-22
2026-04-21
2026-04-15
2026-04-09
2026-04-01