Skip to content

feat: split xagent/core/agent.py into focused collaborative modules#4

Draft
Copilot wants to merge 3 commits into
copilot/complete-project-refactorfrom
copilot/refactor-agent-into-modules
Draft

feat: split xagent/core/agent.py into focused collaborative modules#4
Copilot wants to merge 3 commits into
copilot/complete-project-refactorfrom
copilot/refactor-agent-into-modules

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 13, 2026

xagent/core/agent.py was a 1,128-line monolith mixing OpenAI API calls, tool execution, MCP server management, image captioning, and conversation orchestration.

New modules

Module Responsibility
xagent/core/model_caller.py ReplyType enum + ModelCaller — OpenAI API calls, retry, streaming event classification/extraction, input sanitization
xagent/core/tool_executor.py ToolExecutor — tool registration, invalidation-aware spec cache, semaphore-controlled concurrent execution; make_http_agent_tool() HTTP sub-agent factory
xagent/core/mcp_manager.py MCPManager — MCP server connections, TTL-based caching, tool discovery with tenacity retry
xagent/core/image_processor.py ImageProcessor — vision-model caption generation with prompt-based fallback
agent.py (refactored) Orchestration only: conversation loop, memory, message storage, background tasks, sub-agent conversion (~570 lines, down from 1,128)

All four components are composed inside Agent.__init__:

self._image_processor = ImageProcessor(self.client, ...)
self._tool_executor    = ToolExecutor(self.logger, self._image_processor)
self._mcp_manager      = MCPManager(servers=..., cache_ttl=..., logger=self.logger)
self._model_caller     = ModelCaller(self.client, self.model, self.logger)

Additional changes

  • Lazy storage imports in agent.py: MemoryStorageLocal/MessageStorageLocal are deferred to __init__ body so importing the module no longer eagerly loads chromadb/langfuse. This incidentally fixed several pre-existing test failures (11 → 49 passing in the existing suite).
  • xagent/core/__init__.py now uses __getattr__-based lazy loading (matching the top-level package pattern) and exports all new public classes.
  • tests/test_modular_core.py: 32 new unit tests covering each module's public API, static helpers, cache-invalidation logic, TTL behaviour, captioning fallback, and Agent backward-compat proxies.

Backward compatibility

All existing Agent attributes (tools, mcp_tools, cached_tool_specs, mcp_servers) are preserved as property proxies to the internal components. _register_tools() delegates to ToolExecutor.register() so callers like Swarm continue to work unchanged.

Original prompt

建议:将 Agent 拆分为多个专注的协作模块:

xagent/core/
├── agent.py # 只负责对话编排(< 300 行)
├── model_caller.py # 封装 OpenAI API 调用和重试逻辑
├── tool_executor.py # 工具执行引擎(并发、信号量控制)
├── mcp_manager.py # MCP 服务器连接、缓存和工具发现
└── image_processor.py # 图像字幕生成


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 13, 2026 00:39
- Create xagent/core/model_caller.py: ReplyType enum + ModelCaller class
  (OpenAI API calls with retry, streaming helpers, static message utilities)
- Create xagent/core/tool_executor.py: ToolExecutor class (tool registration,
  invalidation-aware spec cache, semaphore-controlled concurrent execution)
  + make_http_agent_tool() factory function
- Create xagent/core/mcp_manager.py: MCPManager class (TTL-cached MCP server
  tool discovery with tenacity retry)
- Create xagent/core/image_processor.py: ImageProcessor class (vision-model
  captioning with prompt-based fallback)
- Refactor xagent/core/agent.py to use the four new components; reduce from
  1128 lines to ~570 lines by delegating all sub-concerns to components
- Make agent.py storage imports lazy (TYPE_CHECKING guard + deferred Local
  imports) so importing the module no longer triggers chromadb/langfuse
- Replace eager imports in xagent/core/__init__.py with lazy __getattr__
  pattern matching the top-level xagent/__init__.py
- Add tests/test_modular_core.py: 32 unit tests covering each new module
  and backward-compat proxies on Agent

Co-authored-by: ZJCODE <5073837+ZJCODE@users.noreply.github.com>
…ew helper

Co-authored-by: ZJCODE <5073837+ZJCODE@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor agent into focused collaborative modules feat: split xagent/core/agent.py into focused collaborative modules Mar 13, 2026
Copilot AI requested a review from ZJCODE March 13, 2026 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants