refactor(backend): 修复 agent router 中 HTTPException 与 problem_payload 混用问题

This commit is contained in:
qzl
2026-03-27 14:33:40 +08:00
parent 47e2aa3eb9
commit ecc1ec6ce4
+13 -36
View File
@@ -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",