refactor: 重构 Agent 模块为 AgentScope,删除旧版 CrewAI/LiteLLM 实现
This commit is contained in:
@@ -15,7 +15,8 @@ from fastapi import HTTPException
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
|
||||
from core.agentscope.events import to_sse_event
|
||||
from core.agent.domain.agui_input import (
|
||||
from core.agentscope.schemas.agui_input import (
|
||||
extract_latest_tool_result,
|
||||
parse_run_input,
|
||||
validate_run_request_messages_contract,
|
||||
)
|
||||
@@ -29,6 +30,7 @@ from v1.users.dependencies import get_current_user
|
||||
router = APIRouter(prefix="/agent", tags=["agent"])
|
||||
_LAST_EVENT_ID_RE = re.compile(r"^\d+-\d+$")
|
||||
_RUNS_PER_MINUTE = 30
|
||||
_TRANSCRIBES_PER_MINUTE = 20
|
||||
_MAX_SSE_CONNECTIONS_PER_USER = 3
|
||||
_SSE_SLOT_TTL_SECONDS = 15 * 60
|
||||
_MAX_TRANSCRIBE_AUDIO_BYTES = 10 * 1024 * 1024
|
||||
@@ -61,6 +63,19 @@ async def _allow_run_request(*, user_id: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
async def _allow_transcribe_request(*, user_id: str) -> bool:
|
||||
try:
|
||||
redis = await get_or_init_redis_client()
|
||||
minute_bucket = int(time.time() // 60)
|
||||
key = f"agent:transcribe-rate:{user_id}:{minute_bucket}"
|
||||
count = await redis.incr(key)
|
||||
if count == 1:
|
||||
await redis.expire(key, 70)
|
||||
return int(count) <= _TRANSCRIBES_PER_MINUTE
|
||||
except Exception: # noqa: BLE001
|
||||
return False
|
||||
|
||||
|
||||
async def _acquire_sse_slot(*, user_id: str) -> bool:
|
||||
try:
|
||||
redis = await get_or_init_redis_client()
|
||||
@@ -130,9 +145,13 @@ async def enqueue_resume(
|
||||
if request.thread_id != thread_id:
|
||||
raise HTTPException(status_code=422, detail="thread_id path/body mismatch")
|
||||
try:
|
||||
parse_run_input(request.model_dump(mode="json", by_alias=True))
|
||||
normalized = parse_run_input(request.model_dump(mode="json", by_alias=True))
|
||||
extract_latest_tool_result(normalized)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
||||
allowed = await _allow_run_request(user_id=str(current_user.id))
|
||||
if not allowed:
|
||||
raise HTTPException(status_code=429, detail="Too many run requests")
|
||||
task = await service.enqueue_resume(
|
||||
thread_id=thread_id,
|
||||
run_input=request,
|
||||
@@ -240,9 +259,12 @@ async def transcribe(
|
||||
request: Request,
|
||||
current_user: Annotated[CurrentUser, Depends(get_current_user)],
|
||||
) -> Union[AsrTranscribeResponse, JSONResponse]:
|
||||
del current_user
|
||||
temp_path: str | None = None
|
||||
try:
|
||||
allowed = await _allow_transcribe_request(user_id=str(current_user.id))
|
||||
if not allowed:
|
||||
raise HTTPException(status_code=429, detail="Too many transcribe requests")
|
||||
|
||||
if audio.content_type not in _ALLOWED_AUDIO_CONTENT_TYPES:
|
||||
raise ValueError("Unsupported audio format")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user