feat: 发布页前端工具改为等待浏览器回执后再继续模型

This commit is contained in:
2026-10-19 15:16:08 +08:00
parent 26f438eb6a
commit 216a2516b8
13 changed files with 435 additions and 166 deletions
+32 -1
View File
@@ -8,9 +8,11 @@ from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from sse_starlette.sse import EventSourceResponse
from api.deps import chat_service, current_actor
from api.deps import chat_service, current_actor, session_service
from api.response import ok
from application.chat_service import ChatService
from application.client_tool_gate import get_client_tool_gate
from application.session_service import SessionService
from domain.models import Actor
router = APIRouter(prefix="/sessions", tags=["chat"])
@@ -22,6 +24,14 @@ class ChatBody(BaseModel):
formSnapshot: dict | None = None
class ClientToolResultBody(BaseModel):
"""浏览器执行 ui_set_* 后的真实结果,唤醒挂起的工具轮。"""
toolCallId: str = Field(min_length=1, max_length=200)
ok: bool
message: str = Field(default="", max_length=2000)
@router.get("/{thread_id}/messages")
async def list_messages(
thread_id: str,
@@ -115,3 +125,24 @@ async def chat_stream(
},
ping=15,
)
@router.post("/{thread_id}/client-tool-results")
async def submit_client_tool_result(
thread_id: str,
body: ClientToolResultBody,
actor: Actor = Depends(current_actor),
sessions: SessionService = Depends(session_service),
):
"""前端工具回执。对上当前轮等待则 accepted=true;迟到或未知 id 仍 200,避免打断页面。"""
try:
await sessions.require_owned(actor, thread_id)
except LookupError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
accepted = get_client_tool_gate().submit(
thread_id,
body.toolCallId.strip(),
body.ok,
body.message,
)
return ok({"accepted": accepted})