# Agent Core **Agent 核心框架**:提供单个 Agent 的执行能力 ## 文档维护规范 0. **先改文档,再动代码** - 新功能或重大修改需先完成文档更新、并完成审阅后,再进行代码实现;除非改动较小、不被文档涵盖 1. **文档分层,链接代码** - 重要或复杂设计可以另有详细文档;关键实现需标注代码文件路径;格式:`module/file.py:function_name` 2. **简洁快照,日志分离** - 只记录最重要的、与代码准确对应的或者明确的已完成的设计的信息,避免推测、建议,或大量代码;决策依据或修改日志若有必要,可在`docs/decisions.md`另行记录 --- ## 概述 Agent Core 是一个完整的 Agent 执行框架,提供: - Trace、Message、Goal 管理 - 工具系统(文件、命令、网络、浏览器) - LLM 集成(Gemini、OpenRouter、Yescode) - Skills(领域知识注入) - 子 Agent 机制 **独立性**:Agent Core 不依赖任何其他模块,可以独立运行。 --- ## 模块结构 ``` agent/ ├── core/ # 核心引擎 │ ├── runner.py # AgentRunner + 运行时配置 │ └── presets.py # Agent 预设(explore、analyst 等) │ ├── trace/ # 执行追踪(含计划管理) │ ├── models.py # Trace, Message │ ├── goal_models.py # Goal, GoalTree, GoalStats │ ├── protocols.py # TraceStore 接口 │ ├── store.py # FileSystemTraceStore 实现 │ ├── goal_tool.py # goal 工具(计划管理) │ ├── compaction.py # Context 压缩 │ ├── api.py # REST API │ └── websocket.py # WebSocket API │ ├── tools/ # 外部交互工具 │ ├── registry.py # 工具注册表 │ ├── schema.py # Schema 生成器 │ ├── models.py # ToolResult, ToolContext │ └── builtin/ │ ├── file/ # 文件操作 │ ├── browser/ # 浏览器自动化 │ ├── bash.py # 命令执行 │ ├── subagent.py # 子 Agent 创建 │ └── a2a_im.py # A2A IM 工具(桥接到 Gateway) │ ├── skill/ # 技能系统 │ ├── models.py # Skill │ ├── skill_loader.py # Skill 加载器 │ └── skills/ # 内置 Skills │ └── llm/ # LLM 集成 ├── gemini.py # Gemini Provider ├── openrouter.py # OpenRouter Provider └── yescode.py # Yescode Provider ``` --- ## 核心概念 ### Trace(任务执行) 一次完整的 Agent 执行。所有 Agent(主、子、人类协助)都是 Trace。 **实现位置**:`agent/trace/models.py:Trace` ### Goal(目标节点) 计划中的一个目标,支持层级结构。 **实现位置**:`agent/trace/goal_models.py:Goal` ### Message(执行消息) 对应 LLM API 的消息,每条 Message 关联一个 Goal。 **实现位置**:`agent/trace/models.py:Message` --- ## 快速开始 ### 基础使用 ```python from agent.core.runner import AgentRunner, RunConfig from agent.trace import FileSystemTraceStore, Trace, Message from agent.llm import create_qwen_llm_call runner = AgentRunner( llm_call=create_qwen_llm_call(model="qwen3.5-plus"), trace_store=FileSystemTraceStore(base_path=".trace"), skills_dir="./skills", # 项目 skills 目录(可选) ) async for item in runner.run( messages=[{"role": "user", "content": "分析项目架构"}], config=RunConfig(model="qwen3.5-plus"), ): if isinstance(item, Trace): print(f"Trace: {item.trace_id}") elif isinstance(item, Message) and item.role == "assistant": print(item.content) ``` ### RunConfig 关键参数 ```python RunConfig( model="qwen3.5-plus", temperature=0.3, max_iterations=200, agent_type="default", # Agent 预设(对应 presets.json) name="任务名称", tools=["read_file", "bash"], # 限制可用工具(None=全部) goal_compression="on_overflow", # Goal 压缩:"none" / "on_complete" / "on_overflow" knowledge=KnowledgeConfig(...), # 知识提取配置 ) ``` ### 续跑已有 Trace ```python config = RunConfig(model="qwen3.5-plus", trace_id="existing-trace-id") async for item in runner.run(messages=[{"role": "user", "content": "继续"}], config=config): ... ``` ### Skill 指定注入(设计中) 同一个长期续跑的 agent,不同调用可以注入不同 skill: ```python # 不同调用场景注入不同策略 async for item in runner.run( messages=[...], config=config, inject_skills=["ask_strategy"], # 本次需要的 skill skill_recency_threshold=10, # 最近 N 条消息内有就不重复注入 ): ... ``` 详见 [Context 管理 § Skill 指定注入](./docs/context-management.md#3-skill-指定注入设计中未实现) ### 自定义工具 ```python from agent.tools import tool, ToolContext, ToolResult @tool(description="自定义工具") async def my_tool(arg: str, ctx: ToolContext) -> ToolResult: return ToolResult(title="成功", output=f"处理结果: {arg}") ``` ### 完整示例 参考 `examples/research/` — 一个完整的调研 agent 项目: ``` examples/research/ ├── run.py # 入口(含交互控制、续跑、暂停) ├── config.py # RunConfig + KnowledgeConfig 配置 ├── presets.json # Agent 预设(工具权限、skills 过滤) ├── requirement.prompt # 任务 prompt($system$ + $user$ 段) ├── skills/ # 项目自定义 skills └── tools/ # 项目自定义工具 ``` --- ## 文档 | 文档 | 内容 | |------|------| | [架构设计](./docs/architecture.md) | 框架完整架构 | | [Context 管理](./docs/context-management.md) | 注入机制、压缩策略、Skill 指定注入 | | [工具系统](./docs/tools.md) | 工具定义、注册、参数注入 | | [Skills 指南](./docs/skills.md) | Skill 分类、编写、加载 | | [Prompt 规范](./docs/prompt-guidelines.md) | Prompt 撰写原则 | | [Trace API](./docs/trace-api.md) | REST API 和 WebSocket 接口 | | [设计决策](./docs/decisions.md) | 架构决策记录 | --- ## REST API | 方法 | 路径 | 说明 | |------|------|------| | GET | `/api/traces` | 列出 Traces | | GET | `/api/traces/{id}` | 获取 Trace 详情 | | GET | `/api/traces/{id}/messages` | 获取 Messages | | POST | `/api/traces` | 新建 Trace 并执行 | | POST | `/api/traces/{id}/run` | 续跑或回溯 | | POST | `/api/traces/{id}/stop` | 停止运行 | **实现**:`agent/trace/api.py`, `agent/trace/run_api.py`