Files

62 lines
1.6 KiB
Python
Raw Permalink Normal View History

"""
角色分配 API 路由
接入 RoleAllocator 服务基于任务分析分配角色
"""
from fastapi import APIRouter
from pydantic import BaseModel
from typing import List
from ..services.role_allocator import get_role_allocator
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):
"""获取任务主要角色"""
allocator = get_role_allocator()
primary = allocator.get_primary_role(request.task)
role_scores = allocator._analyze_task_roles(request.task)
return {
"task": request.task,
"primary_role": primary,
"role_scores": {k: round(v, 2) for k, v in role_scores.items()}
}
@router.post("/allocate")
async def allocate_roles(request: RoleAllocateRequest):
"""分配角色"""
allocator = get_role_allocator()
allocation = await allocator.allocate_roles(
task=request.task,
available_agents=request.agents
)
primary = allocator.get_primary_role(request.task)
return {
"task": request.task,
"primary_role": primary,
"allocation": allocation
}
@router.post("/explain")
async def explain_roles(request: RoleAllocateRequest):
"""解释角色分配"""
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}