agent-Specialization/modules/multi_agent/__init__.py
JOJO 811974d6e7 feat(multi-agent): 在现有架构上直接实现多智能体模式实验功能
放弃完全隔离策略,改为在现有 MainTerminal/SubAgentManager/SubAgentTask 主链路
按对话级开关 metadata.multi_agent_mode=true 增加多智能体分支。

新增模块:
- modules/multi_agent/__init__.py: 模块入口
- modules/multi_agent/role_store.py: 角色 Markdown Frontmatter 解析与归档
- modules/multi_agent/state.py: 多智能体会话状态机与消息格式化
- modules/multi_agent/prompts.py: 主智能体(Team Leader) + 子智能体提示词
- modules/multi_agent/tools.py: 9 个主智能体工具 + 4 个子智能体工具定义
- server/multi_agent.py: /multiagent/new 页面 + /api/multiagent/* 蓝图

现有代码改动:
- modules/sub_agent/task.py: 扩展 multi_agent_mode/multi_agent_state/display_name 字段,
  增加 ask_master/ask_other_agent/answer_other_agent/list_active_sub_agents 工具处理逻辑,
  子智能体自然结束 assistant 输出即本轮结束(不调用 finish_task),上下文保留。
- modules/sub_agent/manager.py: create_sub_agent 增加 multi_agent_mode/role_id/display_name 参数,
  增加 get_or_create_multi_agent_state/get_multi_agent_state/inject_message_to_sub_agent/_on_multi_agent_task_done 方法。
- core/main_terminal_parts/tools_definition/agent_tools.py: 多智能体模式下用 modules.multi_agent.tools 替换旧版工具集。
- core/main_terminal_parts/context/messages.py: 多智能体模式下追加 Team Leader 系统提示词。
- core/main_terminal_parts/tools_execution.py: create_sub_agent handler 增加多智能体分支,新增 send_message_to_sub_agent/ask_sub_agent/answer_sub_agent_question/create_custom_agent/list_agents/list_active_sub_agents handler。
- core/web_terminal.py: load_conversation 时检测 metadata.multi_agent_mode 设置 self.multi_agent_mode。
- server/app_legacy.py: 注册 multi_agent_bp 蓝图。

前端改动:
- static/src/auth/LoginApp.vue: 登录页增加'多智能体模式(beta)'按钮
- static/src/app/methods/ui/route.ts: 识别 /multiagent/new 和 /multiagent/conv_xxx 路径,进入多智能体模式并创建带 metadata.multi_agent_mode=true 的对话
- static/src/app/state.ts: 增加 multiAgentMode 状态字段

数据:
- ~/.astrion/astrion/host/mutiagents/agents/: 4 个预置角色 ui-operator / full-stack-engineer / code-reviewer / researcher
- ~/.astrion/astrion/host/mutiagents/conversations/: 会话数据

验证:所有 Python 文件语法检查通过;冒烟测试 test.test_server_refactor_smoke 6 项全通过;前端构建通过(6.04s);模块导入与功能断言测试全部通过。
2026-07-12 03:26:02 +08:00

53 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""多智能体模式核心实现。
由 conversations metadata.multi_agent_mode = true 触发启用,
不另起隔离链路,直接复用现有 MainTerminal、SubAgentManager、SubAgentTask
但通过对话级开关注入多智能体版工具集、prompt、消息路由。
关键组件:
- RoleConfig角色 Markdown Frontmatter 解析与归档
- MultiAgentState一个多智能体会话的运行态状态机
- 消息格式构造format_multi_agent_message
- 工具定义master 侧、sub_agent 侧扩展
- prompt 构造build_multi_agent_master_prompt / build_multi_agent_sub_agent_prompt
"""
from __future__ import annotations
from .state import MultiAgentState, format_multi_agent_message
from .role_store import (
RoleConfig,
load_preset_role,
list_roles,
save_custom_role,
build_role_system_prompt,
)
from .prompts import (
build_multi_agent_master_prompt,
build_multi_agent_sub_agent_prompt,
MULTI_AGENT_MASTER_PROMPT_BODY,
MULTI_AGENT_SUB_AGENT_PROMPT_BODY,
)
from .tools import (
MULTI_AGENT_MASTER_TOOLS,
MULTI_AGENT_SUB_AGENT_TOOLS,
build_master_tools_for_conversation,
build_sub_agent_tools_for_role,
)
__all__ = [
"MultiAgentState",
"format_multi_agent_message",
"RoleConfig",
"load_preset_role",
"list_roles",
"save_custom_role",
"build_role_system_prompt",
"build_multi_agent_master_prompt",
"build_multi_agent_sub_agent_prompt",
"MULTI_AGENT_MASTER_PROMPT_BODY",
"MULTI_AGENT_SUB_AGENT_PROMPT_BODY",
"MULTI_AGENT_MASTER_TOOLS",
"MULTI_AGENT_SUB_AGENT_TOOLS",
"build_master_tools_for_conversation",
"build_sub_agent_tools_for_role",
]