From ecc1ec6ce49cd1eea100172779158aa461d0192a Mon Sep 17 00:00:00 2001 From: qzl Date: Fri, 27 Mar 2026 14:33:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(backend):=20=E4=BF=AE=E5=A4=8D=20agent?= =?UTF-8?q?=20router=20=E4=B8=AD=20HTTPException=20=E4=B8=8E=20problem=5Fp?= =?UTF-8?q?ayload=20=E6=B7=B7=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/v1/agent/router.py | 49 +++++++++------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/backend/src/v1/agent/router.py b/backend/src/v1/agent/router.py index db031a8..ba714ff 100644 --- a/backend/src/v1/agent/router.py +++ b/backend/src/v1/agent/router.py @@ -9,7 +9,7 @@ from datetime import date from typing import Annotated from ag_ui.core import RunAgentInput -from core.http.errors import problem_payload +from core.http.errors import ApiProblemError, problem_payload from core.agentscope.events import to_sse_event from core.agentscope.schemas.agui_input import ( parse_run_input, @@ -132,14 +132,14 @@ async def enqueue_run( try: request = parse_run_input(request.model_dump(by_alias=True, exclude_none=True)) except ValueError as exc: - raise HTTPException( + raise ApiProblemError( status_code=422, detail=problem_payload(code="AGENT_RUN_INPUT_INVALID", detail=str(exc)), ) from exc try: validate_run_request_messages_contract(request) except ValueError as exc: - raise HTTPException( + raise ApiProblemError( status_code=422, detail=problem_payload(code="AGENT_RUN_MESSAGES_INVALID", detail=str(exc)), ) from exc @@ -197,20 +197,14 @@ async def stream_events( ): raise HTTPException( status_code=422, - detail=problem_payload( - code="AGENT_INVALID_LAST_EVENT_ID", - detail="Invalid Last-Event-ID", - ), + detail="Invalid Last-Event-ID", ) sse_slot_acquired = await _acquire_sse_slot(user_id=str(current_user.id)) if not sse_slot_acquired: raise HTTPException( status_code=429, - detail=problem_payload( - code="AGENT_SSE_CONNECTION_LIMIT", - detail="Too many SSE connections", - ), + detail="Too many SSE connections", ) async def _event_iter() -> AsyncIterator[str]: @@ -304,19 +298,12 @@ async def upload_attachment( if not payload: raise HTTPException( status_code=422, - detail=problem_payload( - code="AGENT_ATTACHMENT_EMPTY", - detail="Empty attachment", - ), + detail="Empty attachment", ) if len(payload) > _MAX_ATTACHMENT_UPLOAD_BYTES: raise HTTPException( status_code=413, - detail=problem_payload( - code="AGENT_ATTACHMENT_TOO_LARGE", - detail="Attachment too large", - params={"maxBytes": _MAX_ATTACHMENT_UPLOAD_BYTES}, - ), + detail="Attachment too large", ) attachment = await service.upload_attachment( thread_id=thread_id, @@ -362,7 +349,7 @@ async def transcribe( temp_path: str | None = None try: if audio.content_type not in _ALLOWED_AUDIO_CONTENT_TYPES: - raise HTTPException( + raise ApiProblemError( status_code=400, detail=problem_payload( code="AGENT_AUDIO_UNSUPPORTED_FORMAT", @@ -381,7 +368,7 @@ async def transcribe( and declared_length > _MAX_TRANSCRIBE_AUDIO_BYTES + _MULTIPART_OVERHEAD_BYTES ): - raise HTTPException( + raise ApiProblemError( status_code=400, detail=problem_payload( code="AGENT_AUDIO_TOO_LARGE", @@ -403,11 +390,7 @@ async def transcribe( if total_bytes > _MAX_TRANSCRIBE_AUDIO_BYTES: raise HTTPException( status_code=400, - detail=problem_payload( - code="AGENT_AUDIO_TOO_LARGE", - detail="Audio file too large", - params={"maxBytes": _MAX_TRANSCRIBE_AUDIO_BYTES}, - ), + detail="Audio file too large", ) if len(header) < _WAV_HEADER_MIN_BYTES: required = _WAV_HEADER_MIN_BYTES - len(header) @@ -417,18 +400,12 @@ async def transcribe( if total_bytes == 0: raise HTTPException( status_code=400, - detail=problem_payload( - code="AGENT_AUDIO_EMPTY", - detail="Empty audio file", - ), + detail="Empty audio file", ) if not _looks_like_wav_header(bytes(header)): raise HTTPException( status_code=400, - detail=problem_payload( - code="AGENT_AUDIO_UNSUPPORTED_FORMAT", - detail="Unsupported audio format", - ), + detail="Unsupported audio format", ) transcript = await asr_service.transcribe_file( @@ -440,7 +417,7 @@ async def transcribe( except HTTPException: raise except RuntimeError: - raise HTTPException( + raise ApiProblemError( status_code=502, detail=problem_payload( code="AGENT_ASR_UNAVAILABLE",