让 AI 之间自动对话:基于 TRAE Solo 的多智能体团队协作系统设计与> 当前端 AI 遇到接口问题,它能否直接问后端 AI?后端 AI 解答不了时,能否自动通知工程师介入?本文记录了从问题到完整实现的全过程。
背景:AI 编程工具的团队困境
TRAE Solo 是一款强大的 AI 编程助手,能独立完成前端页面开发、后端接口编写、代码审查等工作。但一个现实问题摆在面前:它是单机工具。
想象这样一个场景:团队中前端工程师和后端工程师各自用 TRAE Solo 开发。前端 AI 在对接后端接口时遇到 401 错误,不知道后端的认证逻辑。此时它只能停下来,等工程师手动去问后端同事。这个"停下来等人"的环节,恰恰是效率瓶颈所在。
如果前端 AI 能直接向后端 AI 提问,后端 AI 分析后自动回复,回复不了再通知人工介入------整个协作链路就打通了。
核心思路:中间件 + MCP Server
TRAE Solo 本身没有跨实例通信能力,但有两个关键接口可以利用:
- MCP(Model Context Protocol):TRAE 原生支持自定义 MCP Server,AI 可以调用 MCP 工具与外部系统交互
- 企业微信待办:通过 wecom-cli 可以创建待办事项通知团队成员
方案的核心是搭建一个中间件服务,作为所有 TRAE 实例之间的通信枢纽。每个 TRAE 通过自己的 MCP Server 连接到中间件,实现消息的发送、接收和路由。
整体架构如下:
┌─────────────────┐ ┌──────────────────────┐ ┌─────────────────┐
│ 前端 TRAE Solo │ │ 通信中间件 │ │ 后端 TRAE Solo │
│ │ │ (FastAPI + SQLite) │ │ │
│ MCP Server ────┼──HTTP──→│ │←──HTTP──┼──── MCP Server │
│ │ │ - 消息路由 │ │ │
│ AI 自动开发 │ │ - 状态管理 │ │ AI 自动分析 │
│ 遇到接口问题 │ │ - 超时升级 │ │ 尝试解答 │
└─────────────────┘ └──────┬───────────────┘ └─────────────────┘
│
┌──────▼──────┐
│ 企业微信 │
│ 待办通知 │
└─────────────┘
消息交互格式:三种消息覆盖全链路
系统定义了三种消息类型,覆盖从提问到解决的完整协作链路。所有消息使用 JSON 格式,通过 HTTP 传输。
Question:提问消息
前端 AI 遇到接口问题时发送。包含标题、详细描述,以及结构化的接口上下文:
json
{
"id": "a1b2c3d4e5f6",
"type": "question",
"title": "登录接口返回401",
"detail": "前端调用 POST /api/login 传入正确的用户名密码,但返回 401 Unauthorized",
"context": {
"endpoint": "POST /api/login",
"request_body": "{\"username\":\"admin\",\"password\":\"123456\"}",
"response_body": "{\"error\":\"Unauthorized\"}",
"error_code": 401,
"file_path": "src/api/auth.ts",
"line_number": 42
},
"priority": "high",
"status": "pending",
"from_actor": {
"name": "张志航",
"userid": "20260193",
"role": "frontend"
},
"to_role": "backend"
}
context 字段是关键设计。它把接口路径、请求体、响应体、错误码、代码位置等信息结构化,让后端 AI 不用猜就能定位问题。
Answer:回答消息
后端 AI 分析后回复。包含回答内容、解决方案步骤和代码示例:
json
{
"type": "answer",
"question_id": "a1b2c3d4e5f6",
"content": "问题原因:登录接口需要先获取 CSRF token",
"solution": "1. 先调用 GET /api/csrf-token 获取 token\n2. 在登录请求头中添加 X-CSRF-Token",
"code_example": "const token = await fetch('/api/csrf-token').then(r => r.json());\nawait fetch('/api/login', { headers: { 'X-CSRF-Token': token } })",
"answered_by_ai": true
}
answered_by_ai 字段标记回答来源,方便后续统计 AI 自动解决率。
Escalation:升级消息
当后端 AI 无法解答时触发。系统自动通过企业微信待办通知后端工程师和项目经理:
json
{
"type": "escalation",
"question_id": "a1b2c3d4e5f6",
"reason": "无法确定 CSRF token 的生成逻辑,涉及自定义中间件",
"attempted_solutions": [
"检查了认证中间件代码",
"查看了路由配置",
"搜索了项目中的 token 生成逻辑"
]
}
attempted_solutions 字段记录 AI 已尝试的方案,避免人工重复排查。
消息状态流转
pending → answering → answered → closed
└────→ escalated → closed
所有状态变更都记录在 SQLite 数据库中,中间件重启不丢数据。
代码实现
整个系统由 9 个文件组成,用 Python + FastAPI 实现。
文件结构
trae-collab/
├── team_config.json # 团队成员和中间件配置
├── config.py # 配置加载
├── models.py # 消息数据模型(Pydantic)
├── database.py # SQLite 数据库操作
├── notifier.py # 企业微信待办通知
├── middleware.py # FastAPI 中间件服务
├── mcp_server.py # MCP Server(TRAE 集成入口)
├── requirements.txt # Python 依赖
└── README.md # 设计文档
数据模型(models.py)
使用 Pydantic 定义所有消息类型,自带字段校验和 JSON 序列化。三种角色 frontend、backend、manager 完全对等,任意角色都可以向任意角色提问:
python
class Role(str, Enum):
frontend = "frontend"
backend = "backend"
manager = "manager"
class Question(BaseModel):
id: str = Field(default_factory=_uuid)
title: str = Field(..., max_length=120)
detail: str = Field(...)
context: ApiContext | None = Field(None)
priority: Priority = Field(Priority.medium)
status: MessageStatus = Field(MessageStatus.pending)
from_actor: Actor = Field(...)
to_role: Role = Field(...)
中间件服务(middleware.py)
FastAPI 实现,提供 8 个 REST API 端点。核心是问题路由和升级通知:
python
@app.post("/api/questions/{qid}/escalate")
async def api_escalate(qid: str, req: CreateEscalationRequest):
# 创建升级记录
escalation = Escalation(question_id=qid, reason=req.reason, ...)
create_escalation(escalation)
# 通知后端工程师
notify_human_escalation(question_id=qid, ...)
# 同时通知项目经理
manager = get_member_by_role("manager")
if manager:
notify_human_escalation(question_id=qid, to_userid=manager["userid"], ...)
项目经理有专属的仪表盘端点,可以全量查看所有待处理、已回答、已升级的问题:
python
@app.get("/api/dashboard")
async def api_dashboard():
return {
"pending": list_questions(status=MessageStatus.pending),
"answered": list_questions(status=MessageStatus.answered),
"escalated": list_questions(status=MessageStatus.escalated),
}
MCP Server(mcp_server.py)
这是 TRAE 与中间件之间的桥梁。基于 MCP 协议,定义了 7 个工具供 AI 调用:
| 工具 | 作用 |
|---|---|
ask_colleague |
向同事发送问题 |
check_inbox |
检查待处理问题 |
get_question_detail |
查看问题详情 |
reply_answer |
回答问题 |
escalate_to_human |
升级到人工 |
close_question |
关闭问题 |
get_dashboard |
项目经理仪表盘 |
每个工具内部通过 HTTP 调用中间件 API,对 AI 来说就像调用本地函数一样简单:
python
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
async with httpx.AsyncClient(base_url=BASE_URL) as client:
if name == "ask_colleague":
resp = await client.post("/api/questions", json=payload)
elif name == "check_inbox":
resp = await client.get(f"/api/inbox/{role}")
elif name == "escalate_to_human":
resp = await client.post(f"/api/questions/{qid}/escalate", json=payload)
# ...
企业微信通知(notifier.py)
通过 wecom-cli 创建待办事项,通知人工介入。选择企微待办而非消息,是因为待办功能已授权且不会被消息流淹没:
python
def notify_human_escalation(question_id, title, reason, to_userid, ...):
content = f"[接口协作-需人工介入]\n问题: {title}\n原因: {reason}"
_run_wecom_cli([
"todo", "create_todo", "--json",
json.dumps({
"content": content,
"follower_list": {"followers": [{"follower_id": to_userid}]},
"remind_type_list": [1],
})
])
部署与使用
安装
bash
cd trae-collab
pip install -r requirements.txt
配置团队
编辑 team_config.json,填入团队成员信息:
json
{
"members": [
{"name": "张志航", "userid": "20260193", "role": "frontend"},
{"name": "后端同事", "userid": "xxx", "role": "backend"},
{"name": "项目经理", "userid": "yyy", "role": "manager"}
]
}
启动中间件
bash
python middleware.py
TRAE 中配置 MCP Server
在 TRAE 的 MCP 设置中添加:
- 命令:
python - 参数:
mcp_server.py的完整路径
配置完成后,TRAE 中的 AI 就获得了 7 个协作工具。
实际使用
前端 AI 遇到接口问题时,对 TRAE 说:
"我在对接后端的登录接口时遇到了 401 错误,帮我问问后端同事"
AI 会自动调用 ask_colleague 发送问题。后端 AI 定期检查收件箱,收到问题后分析并回复。如果无法解答,自动调用 escalate_to_human,后端工程师和项目经理同时收到企微待办通知。
AI实现