[ PROMPT_NODE_22226 ]
autonomous-agent-patterns
[ SKILL_DOCUMENTATION ]
# 🕹️ 自主智能体模式
> 受 [Cline](https://github.com/cline/cline) 和 [OpenAI Codex](https://github.com/openai/codex) 启发的自主编码智能体构建设计模式。
## 何时使用此技能
在以下场景中使用:
- 构建自主 AI 智能体
- 设计工具/函数调用 API
- 实现权限与审批系统
- 为智能体创建浏览器自动化
- 设计人机协作 (Human-in-the-loop) 工作流
---
## 1. 核心智能体架构
### 1.1 智能体循环
┌─────────────────────────────────────────────────────────────┐
│ 智能体循环 │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ 思考 │───▶│ 决策 │───▶│ 行动 │ │
│ │ (推理) │ │ (规划) │ │ (执行) │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ ▲ │ │
│ │ ┌──────────┐ │ │
│ └─────────│ 观察 │◀─────────┘ │
│ │ (结果) │ │
│ └──────────┘ │
└─────────────────────────────────────────────────────────────┘
python
class AgentLoop:
def __init__(self, llm, tools, max_iterations=50):
self.llm = llm
self.tools = {t.name: t for t in tools}
self.max_iterations = max_iterations
self.history = []
def run(self, task: str) -> str:
self.history.append({"role": "user", "content": task})
for i in range(self.max_iterations):
# 思考: 获取带有工具选项的 LLM 响应
response = self.llm.chat(
messages=self.history,
tools=self._format_tools(),
tool_choice="auto"
)
# 决策: 检查智能体是否需要使用工具
if response.tool_calls:
for tool_call in response.tool_calls:
# 行动: 执行工具
result = self._execute_tool(tool_call)
# 观察: 将结果添加到历史记录
self.history.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result)
})
else:
# 无需更多工具调用