refactor: use a single agent model path

This commit is contained in:
wuyang
2026-07-26 18:50:51 +08:00
parent 2e8e81c790
commit d95d06233f
23 changed files with 330 additions and 454 deletions
+4 -14
View File
@@ -10,7 +10,7 @@ from typing import Annotated, Any
import httpx
import uvicorn
from fastapi import FastAPI, Header, HTTPException, status
from fastapi import FastAPI, Header, HTTPException
from fastapi.responses import JSONResponse, StreamingResponse
from agent_platform.auth import UserIdentity, decode_openwebui_identity, verify_service_bearer
@@ -198,16 +198,6 @@ def create_app(
return await forward_direct()
stable_chat_id = (chat_id or message_id or f"ephemeral-{uuid.uuid4().hex}").strip()
selected_mode = await app.state.store.select_mode(identity.user_id, chat_id or "", spec.mode)
if selected_mode == "work" and spec.mode == "chat":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="This conversation has been upgraded to Work and cannot return to Chat.",
)
if spec.mode == "chat":
return await forward_direct()
messages = [message.model_dump(exclude_none=True) for message in body.messages]
if not body.stream:
@@ -224,7 +214,7 @@ def create_app(
)
return _completion(body.model, answer)
async def work_stream() -> AsyncIterator[bytes]:
async def agent_stream() -> AsyncIterator[bytes]:
queue: asyncio.Queue[tuple[str, Any]] = asyncio.Queue()
async def publish(event_type: str, payload: dict[str, Any]) -> None:
@@ -255,7 +245,7 @@ def create_app(
if kind == "done":
break
if kind == "error":
yield _sse_chunk(body.model, f"\n\nWork 运行失败:{value}")
yield _sse_chunk(body.model, f"\n\nAgent 运行失败:{value}")
continue
if kind == "answer":
text = str(value)
@@ -271,7 +261,7 @@ def create_app(
await asyncio.gather(task, return_exceptions=True)
return StreamingResponse(
work_stream(),
agent_stream(),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)