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
+28 -17
View File
@@ -22,7 +22,7 @@ from domain.models import (
MessageRole,
StreamEvent,
)
from domain.ports import ChatStreamer, LlmProtocolCapabilityCache
from domain.ports import ChatStreamer, ClientToolTurn, LlmProtocolCapabilityCache
from infrastructure.llm.api_style import (
ProtocolAttempt,
classify_protocol_error,
@@ -55,6 +55,7 @@ class OpenAICompatibleStreamer(ChatStreamer):
tools: list[Any] | None = None,
max_tool_result_chars: int = 8000,
previous_response_id: str | None = None,
client_tool_turn: ClientToolTurn | None = None,
) -> AsyncIterator[StreamEvent]:
chain_id = (previous_response_id or "").strip() or None
style = normalize_api_style(llm.api_style)
@@ -93,6 +94,7 @@ class OpenAICompatibleStreamer(ChatStreamer):
use_responses=attempt.use_responses,
use_store=attempt.use_store,
previous_response_id=use_chain_id,
client_tool_turn=client_tool_turn,
):
if event.kind == "token" and event.text:
yielded_any = True
@@ -302,6 +304,7 @@ class OpenAICompatibleStreamer(ChatStreamer):
use_responses: bool,
use_store: bool,
previous_response_id: str | None,
client_tool_turn: ClientToolTurn | None = None,
) -> AsyncIterator[StreamEvent]:
kwargs: dict[str, Any] = {
"api_key": llm.api_key,
@@ -417,24 +420,32 @@ class OpenAICompatibleStreamer(ChatStreamer):
name = tc.get("name") or ""
args = tc.get("args") or {}
call_id = tc.get("id") or name
if is_client_tool_name(name):
yield StreamEvent.client_tool(
tool_call_id=str(call_id),
name=name,
args=args if isinstance(args, dict) else {},
label=tool_status_label(name),
)
tool = tool_map.get(name)
try:
if tool is None:
result = f"未知工具: {name}"
if is_client_tool_name(name):
# 占位函数不代表页面已写入;先登记等待再推事件,回执到达后才进 ToolMessage
tool_args = args if isinstance(args, dict) else {}
if client_tool_turn is None:
result = "写入失败:前端回执通道未配置"
else:
client_tool_turn.arm(str(call_id))
yield StreamEvent.client_tool(
tool_call_id=str(call_id),
name=name,
args=tool_args,
label=tool_status_label(name),
)
result = await client_tool_turn.wait_result(str(call_id))
else:
raw = await tool.ainvoke(args)
result = (
raw
if isinstance(raw, str)
else json.dumps(raw, ensure_ascii=False, default=str)
)
tool = tool_map.get(name)
if tool is None:
result = f"未知工具: {name}"
else:
raw = await tool.ainvoke(args)
result = (
raw
if isinstance(raw, str)
else json.dumps(raw, ensure_ascii=False, default=str)
)
except Exception as e: # noqa: BLE001
result = f"工具执行失败: {e}"
messages.append(