refactor(backend): 修复 agent router 中 HTTPException 与 problem_payload 混用问题
This commit is contained in:
@@ -9,7 +9,7 @@ from datetime import date
|
|||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
from ag_ui.core import RunAgentInput
|
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.events import to_sse_event
|
||||||
from core.agentscope.schemas.agui_input import (
|
from core.agentscope.schemas.agui_input import (
|
||||||
parse_run_input,
|
parse_run_input,
|
||||||
@@ -132,14 +132,14 @@ async def enqueue_run(
|
|||||||
try:
|
try:
|
||||||
request = parse_run_input(request.model_dump(by_alias=True, exclude_none=True))
|
request = parse_run_input(request.model_dump(by_alias=True, exclude_none=True))
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise HTTPException(
|
raise ApiProblemError(
|
||||||
status_code=422,
|
status_code=422,
|
||||||
detail=problem_payload(code="AGENT_RUN_INPUT_INVALID", detail=str(exc)),
|
detail=problem_payload(code="AGENT_RUN_INPUT_INVALID", detail=str(exc)),
|
||||||
) from exc
|
) from exc
|
||||||
try:
|
try:
|
||||||
validate_run_request_messages_contract(request)
|
validate_run_request_messages_contract(request)
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
raise HTTPException(
|
raise ApiProblemError(
|
||||||
status_code=422,
|
status_code=422,
|
||||||
detail=problem_payload(code="AGENT_RUN_MESSAGES_INVALID", detail=str(exc)),
|
detail=problem_payload(code="AGENT_RUN_MESSAGES_INVALID", detail=str(exc)),
|
||||||
) from exc
|
) from exc
|
||||||
@@ -197,20 +197,14 @@ async def stream_events(
|
|||||||
):
|
):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=422,
|
status_code=422,
|
||||||
detail=problem_payload(
|
detail="Invalid Last-Event-ID",
|
||||||
code="AGENT_INVALID_LAST_EVENT_ID",
|
|
||||||
detail="Invalid Last-Event-ID",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
sse_slot_acquired = await _acquire_sse_slot(user_id=str(current_user.id))
|
sse_slot_acquired = await _acquire_sse_slot(user_id=str(current_user.id))
|
||||||
if not sse_slot_acquired:
|
if not sse_slot_acquired:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=429,
|
status_code=429,
|
||||||
detail=problem_payload(
|
detail="Too many SSE connections",
|
||||||
code="AGENT_SSE_CONNECTION_LIMIT",
|
|
||||||
detail="Too many SSE connections",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _event_iter() -> AsyncIterator[str]:
|
async def _event_iter() -> AsyncIterator[str]:
|
||||||
@@ -304,19 +298,12 @@ async def upload_attachment(
|
|||||||
if not payload:
|
if not payload:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=422,
|
status_code=422,
|
||||||
detail=problem_payload(
|
detail="Empty attachment",
|
||||||
code="AGENT_ATTACHMENT_EMPTY",
|
|
||||||
detail="Empty attachment",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
if len(payload) > _MAX_ATTACHMENT_UPLOAD_BYTES:
|
if len(payload) > _MAX_ATTACHMENT_UPLOAD_BYTES:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=413,
|
status_code=413,
|
||||||
detail=problem_payload(
|
detail="Attachment too large",
|
||||||
code="AGENT_ATTACHMENT_TOO_LARGE",
|
|
||||||
detail="Attachment too large",
|
|
||||||
params={"maxBytes": _MAX_ATTACHMENT_UPLOAD_BYTES},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
attachment = await service.upload_attachment(
|
attachment = await service.upload_attachment(
|
||||||
thread_id=thread_id,
|
thread_id=thread_id,
|
||||||
@@ -362,7 +349,7 @@ async def transcribe(
|
|||||||
temp_path: str | None = None
|
temp_path: str | None = None
|
||||||
try:
|
try:
|
||||||
if audio.content_type not in _ALLOWED_AUDIO_CONTENT_TYPES:
|
if audio.content_type not in _ALLOWED_AUDIO_CONTENT_TYPES:
|
||||||
raise HTTPException(
|
raise ApiProblemError(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=problem_payload(
|
detail=problem_payload(
|
||||||
code="AGENT_AUDIO_UNSUPPORTED_FORMAT",
|
code="AGENT_AUDIO_UNSUPPORTED_FORMAT",
|
||||||
@@ -381,7 +368,7 @@ async def transcribe(
|
|||||||
and declared_length
|
and declared_length
|
||||||
> _MAX_TRANSCRIBE_AUDIO_BYTES + _MULTIPART_OVERHEAD_BYTES
|
> _MAX_TRANSCRIBE_AUDIO_BYTES + _MULTIPART_OVERHEAD_BYTES
|
||||||
):
|
):
|
||||||
raise HTTPException(
|
raise ApiProblemError(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=problem_payload(
|
detail=problem_payload(
|
||||||
code="AGENT_AUDIO_TOO_LARGE",
|
code="AGENT_AUDIO_TOO_LARGE",
|
||||||
@@ -403,11 +390,7 @@ async def transcribe(
|
|||||||
if total_bytes > _MAX_TRANSCRIBE_AUDIO_BYTES:
|
if total_bytes > _MAX_TRANSCRIBE_AUDIO_BYTES:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=problem_payload(
|
detail="Audio file too large",
|
||||||
code="AGENT_AUDIO_TOO_LARGE",
|
|
||||||
detail="Audio file too large",
|
|
||||||
params={"maxBytes": _MAX_TRANSCRIBE_AUDIO_BYTES},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
if len(header) < _WAV_HEADER_MIN_BYTES:
|
if len(header) < _WAV_HEADER_MIN_BYTES:
|
||||||
required = _WAV_HEADER_MIN_BYTES - len(header)
|
required = _WAV_HEADER_MIN_BYTES - len(header)
|
||||||
@@ -417,18 +400,12 @@ async def transcribe(
|
|||||||
if total_bytes == 0:
|
if total_bytes == 0:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=problem_payload(
|
detail="Empty audio file",
|
||||||
code="AGENT_AUDIO_EMPTY",
|
|
||||||
detail="Empty audio file",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
if not _looks_like_wav_header(bytes(header)):
|
if not _looks_like_wav_header(bytes(header)):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=400,
|
status_code=400,
|
||||||
detail=problem_payload(
|
detail="Unsupported audio format",
|
||||||
code="AGENT_AUDIO_UNSUPPORTED_FORMAT",
|
|
||||||
detail="Unsupported audio format",
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
transcript = await asr_service.transcribe_file(
|
transcript = await asr_service.transcribe_file(
|
||||||
@@ -440,7 +417,7 @@ async def transcribe(
|
|||||||
except HTTPException:
|
except HTTPException:
|
||||||
raise
|
raise
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
raise HTTPException(
|
raise ApiProblemError(
|
||||||
status_code=502,
|
status_code=502,
|
||||||
detail=problem_payload(
|
detail=problem_payload(
|
||||||
code="AGENT_ASR_UNAVAILABLE",
|
code="AGENT_ASR_UNAVAILABLE",
|
||||||
|
|||||||
Reference in New Issue
Block a user