AI代理系统

📎 引用文件

本文引用的文件 - loop.py - context.py - memory.py - tools.py - chat.py - llm.py - persistent.py - skills.py - service.py - __init__.py

目录

  1. 简介
  2. 项目结构
  3. 核心组件
  4. 架构总览
  5. 详细组件分析
  6. 依赖关系分析
  7. 性能考量
  8. 故障排除指南
  9. 结论
  10. 附录:使用示例与最佳实践

简介

本文件为 Vibe-Trading 的 AI 代理系统提供系统化、可操作的技术文档。重点覆盖以下能力与机制: - 基于自然语言的智能研究与策略开发(含回测验证) - Agent 核心循环(ReAct)与上下文管理 - 工具注册与发现、工具调用流程与安全控制 - 会话状态管理与持久化记忆 - 与 LLM 提供商的集成方式、流式推理与重试 - 性能优化策略、故障排查与最佳实践

该系统通过“五层上下文压缩”、“并行只读工具执行”、“结构化摘要更新”、“跨会话持久记忆”和“安全沙箱化的工具白名单”,在长对话与复杂金融任务中保持稳定、可控与可追溯。

项目结构

围绕 AgentLoop 的核心代码位于 agent/src/agent,配合 providers、session、memory、tools 等模块构成完整闭环: - agent/src/agent:AgentLoop、ContextBuilder、SkillsLoader、WorkspaceMemory、工具基类与注册表 - agent/src/providers:ChatLLM、LLM 工厂与多提供商适配 - agent/src/session:会话生命周期编排、SSE 事件总线、消息历史与尝试(Attempt)管理 - agent/src/memory:跨会话持久化记忆(文件索引、语义链接、FTS 搜索) - agent/src/tools:自动发现与构建工具注册表,支持 MCP 远程工具注入与安全过滤

graph TB A["用户输入"] --> B["SessionService<br/>会话编排"] B --> C["ToolRegistry<br/>工具注册表"] B --> D["ChatLLM<br/>聊天客户端"] B --> E["PersistentMemory<br/>持久记忆"] B --> F["AgentLoop<br/>ReAct 核心循环"] F --> G["ContextBuilder<br/>上下文构建"] F --> H["工具执行<br/>读写批处理/并发"] F --> I["TraceWriter<br/>运行轨迹"] F --> J["GroundingLedger<br/>归因记录"] D --> K["LLM 工厂<br/>providers/llm.py"]

图表来源 - service.py:158-440 - loop.py:502-750 - context.py:210-322 - tools.py:54-95 - chat.py:272-397 - llm.py:89-423

章节来源 - service.py:158-440 - loop.py:502-750 - context.py:210-322 - tools.py:54-95 - chat.py:272-397 - llm.py:89-423

核心组件

章节来源 - loop.py:502-750 - context.py:210-322 - tools.py:13-95 - chat.py:272-397 - persistent.py:196-438 - service.py:158-440

架构总览

下图展示从用户输入到最终输出的端到端流程,包括 ReAct 循环、上下文构建、工具执行、记忆存取与 SSE 事件推送。

sequenceDiagram participant U as "用户" participant S as "SessionService" participant R as "ToolRegistry" participant L as "ChatLLM" participant A as "AgentLoop" participant C as "ContextBuilder" participant M as "PersistentMemory" participant T as "工具执行" U->>S : 发送消息 S->>R : 构建工具注册表(本地+MCP) S->>A : 创建并运行 AgentLoop A->>C : 构建系统提示与消息 C-->>A : 消息列表 A->>L : 流式/同步调用(带工具定义) L-->>A : 文本/思考/工具调用请求 alt 需要工具 A->>T : 执行工具(只读并行/写串行) T-->>A : 结果(JSON/错误) A->>C : 格式化工具结果 A->>L : 继续下一轮(携带结果) else 直接回答 A-->>S : 最终文本 end S-->>U : SSE 事件(文本增量/工具调用/完成)

图表来源 - service.py:158-440 - loop.py:624-800 - context.py:286-322 - chat.py:299-397 - tools.py:66-245

详细组件分析

AgentLoop(ReAct 核心循环)

flowchart TD Start(["进入循环"]) --> Estimate["估计消息 token 数"] Estimate --> Check1{"是否超过阈值 50%?"} Check1 -- 是 --> Micro["微压缩:清理旧工具结果"] Check1 -- 否 --> CollapseCheck Micro --> CollapseCheck{"是否超过阈值 70%?"} CollapseCheck -- 是 --> Collapse["上下文折叠:长文本首尾保留"] CollapseCheck -- 否 --> AutoCheck{"是否超过阈值 100%?"} Collapse --> AutoCheck AutoCheck -- 是 --> AutoCompact["自动摘要:LLM 结构化总结"] AutoCheck -- 否 --> NextIter["下一轮迭代"] AutoCompact --> NextIter NextIter --> End(["结束或继续"])

图表来源 - loop.py:227-322 - loop.py:727-749

章节来源 - loop.py:227-322 - loop.py:502-750

上下文管理(ContextBuilder)

classDiagram class ContextBuilder { +build_system_prompt(user_message) str +build_messages(user_message, history) List[Dict] +_format_tool_descriptions() str +format_tool_result(tool_call_id, tool_name, result) Dict +format_assistant_tool_calls(tool_calls, content, reasoning_content) Dict } class WorkspaceMemory { +run_dir str +counters dict +increment(key) int +to_summary() str } class SkillsLoader { +get_descriptions() str +get_content(name) str } ContextBuilder --> WorkspaceMemory : "读取状态摘要" ContextBuilder --> SkillsLoader : "获取技能描述"

图表来源 - context.py:210-322 - memory.py:13-54 - skills.py:100-189

章节来源 - context.py:210-322 - memory.py:13-54 - skills.py:100-189

工具注册与发现(ToolRegistry + build_registry)

flowchart TD A["build_registry()"] --> B["扫描 src/tools 模块"] B --> C{"check_available()"} C -- 通过 --> D["注册工具到 ToolRegistry"] C -- 不通过 --> E["跳过该工具"] D --> F{"是否配置 MCP 服务器?"} F -- 是 --> G["构建 MCP 工具包装器"] G --> H{"是否实时券商?"} H -- 是 --> I["检查授权/交互模式"] I -- 通过 --> J["注册包装工具"] I -- 不通过 --> K["跳过并告警"] H -- 否 --> J F -- 否 --> L["返回注册表"] J --> L

图表来源 - __init__.py:33-245 - tools.py:54-95

章节来源 - __init__.py:33-245 - tools.py:54-95

会话状态管理与持久化存储(SessionService + PersistentMemory)

sequenceDiagram participant U as "用户" participant S as "SessionService" participant P as "PersistentMemory" participant A as "AgentLoop" U->>S : 发送消息 S->>P : 自动召回相关记忆 S->>A : 运行 AgentLoop A-->>S : 工具调用/结果/最终答案 S-->>U : SSE 事件(文本增量/工具/完成) Note over P,S : 记忆写入/索引更新/链接维护

图表来源 - service.py:158-440 - persistent.py:196-438

章节来源 - service.py:158-440 - persistent.py:196-438

与 LLM 提供商的集成(ChatLLM + llm.py)

classDiagram class ChatLLM { +chat(messages, tools, timeout) LLMResponse +stream_chat(messages, tools, on_text_chunk, on_reasoning_chunk, timeout, should_cancel) LLMResponse -_parse_response(ai_message) LLMResponse } class LLMFactory { +build_llm(model_name) Any } ChatLLM --> LLMFactory : "获取底层模型"

图表来源 - chat.py:272-397 - llm.py:89-423

章节来源 - chat.py:272-397 - llm.py:89-423

依赖关系分析

graph LR S["SessionService"] --> R["ToolRegistry"] S --> L["ChatLLM"] S --> PM["PersistentMemory"] S --> AL["AgentLoop"] AL --> CB["ContextBuilder"] AL --> TR["ToolRegistry"] AL --> CHAT["ChatLLM"] AL --> MEM["WorkspaceMemory"] AL --> TRACE["TraceWriter"] AL --> GROUND["GroundingLedger"]

图表来源 - service.py:346-440 - loop.py:502-750 - tools.py:54-95 - context.py:210-322

章节来源 - service.py:346-440 - loop.py:502-750 - tools.py:54-95 - context.py:210-322

性能考量

[本节为通用指导,无需特定文件来源]

故障排除指南

章节来源 - __init__.py:136-154 - service.py:43-50 - chat.py:117-163 - persistent.py:42-73

结论

Vibe-Trading 的 AI 代理系统通过 ReAct 循环、分层上下文管理、安全工具注册与发现、跨会话持久记忆以及健壮的 LLM 集成,实现了高可用、可扩展且可追溯的智能研究与策略开发平台。其设计兼顾了性能、安全与可维护性,适合复杂的金融分析与自动化交易研究场景。

[本节为总结,无需特定文件来源]

附录:使用示例与最佳实践

自然语言驱动的研究与策略开发

章节来源 - context.py:23-200 - loop.py:727-749

回测验证与报告

章节来源 - service.py:567-581 - context.py:82-129

工具调用安全控制

章节来源 - __init__.py:136-154 - __init__.py:174-244

性能优化建议

[本节为通用指导,无需特定文件来源]