2026-03-09 17:32:11 +08:00
|
|
|
|
"""
|
|
|
|
|
|
角色分配 API 路由
|
2026-03-10 16:36:25 +08:00
|
|
|
|
接入 RoleAllocator 服务,基于任务分析分配角色
|
2026-03-09 17:32:11 +08:00
|
|
|
|
"""
|
|
|
|
|
|
from fastapi import APIRouter
|
|
|
|
|
|
from pydantic import BaseModel
|
2026-03-10 16:36:25 +08:00
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
from ..services.role_allocator import get_role_allocator
|
2026-03-09 17:32:11 +08:00
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RoleRequest(BaseModel):
|
|
|
|
|
|
task: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RoleAllocateRequest(BaseModel):
|
|
|
|
|
|
task: str
|
|
|
|
|
|
agents: List[str]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/primary")
|
|
|
|
|
|
async def get_primary_role(request: RoleRequest):
|
|
|
|
|
|
"""获取任务主要角色"""
|
2026-03-10 16:36:25 +08:00
|
|
|
|
allocator = get_role_allocator()
|
|
|
|
|
|
primary = allocator.get_primary_role(request.task)
|
|
|
|
|
|
role_scores = allocator._analyze_task_roles(request.task)
|
2026-03-09 17:32:11 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"task": request.task,
|
2026-03-10 16:36:25 +08:00
|
|
|
|
"primary_role": primary,
|
|
|
|
|
|
"role_scores": {k: round(v, 2) for k, v in role_scores.items()}
|
2026-03-09 17:32:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/allocate")
|
|
|
|
|
|
async def allocate_roles(request: RoleAllocateRequest):
|
|
|
|
|
|
"""分配角色"""
|
2026-03-10 16:36:25 +08:00
|
|
|
|
allocator = get_role_allocator()
|
|
|
|
|
|
allocation = await allocator.allocate_roles(
|
|
|
|
|
|
task=request.task,
|
|
|
|
|
|
available_agents=request.agents
|
|
|
|
|
|
)
|
|
|
|
|
|
primary = allocator.get_primary_role(request.task)
|
2026-03-09 17:32:11 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"task": request.task,
|
2026-03-10 16:36:25 +08:00
|
|
|
|
"primary_role": primary,
|
2026-03-09 17:32:11 +08:00
|
|
|
|
"allocation": allocation
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/explain")
|
|
|
|
|
|
async def explain_roles(request: RoleAllocateRequest):
|
|
|
|
|
|
"""解释角色分配"""
|
2026-03-10 16:36:25 +08:00
|
|
|
|
allocator = get_role_allocator()
|
|
|
|
|
|
allocation = await allocator.allocate_roles(
|
|
|
|
|
|
task=request.task,
|
|
|
|
|
|
available_agents=request.agents
|
|
|
|
|
|
)
|
|
|
|
|
|
explanation = allocator.explain_allocation(request.task, allocation)
|
|
|
|
|
|
return {"explanation": explanation}
|