LLM 只需处理极短的 "Questions sent to user." 而不是完整的问题 JSON,减少了 token 消耗和延迟

This commit is contained in:
朱潮 2026-05-18 11:37:23 +08:00
parent 30389be119
commit 0bf7a87a0e
2 changed files with 13 additions and 26 deletions

View File

@ -144,12 +144,11 @@ async def enhanced_generate_stream_response(
is_ui_resource = (
msg.text
and msg.text.lstrip().startswith('{"')
and (
('"ui://' in msg.text and ('"text/html' in msg.text or '"text/uri-list' in msg.text))
or '"__ask_user__"' in msg.text
)
and '"ui://' in msg.text
and ('"text/html' in msg.text or '"text/uri-list' in msg.text)
)
if config.tool_response or is_ui_resource:
is_ask_user = msg.name == 'ask_user'
if config.tool_response or is_ui_resource or is_ask_user:
new_content = f"[{message_tag}] {msg.name}\n{msg.text}\n"
# Collect full content

View File

@ -21,32 +21,20 @@ from mcp_common import (
)
ASK_USER_MARKER = "__ask_user__"
ASK_USER_RESPONSE = "Questions sent to user."
def ask_user(questions: list) -> Dict[str, Any]:
"""Create an ask_user response.
def ask_user() -> Dict[str, Any]:
"""Return a minimal fixed response for ask_user tool.
Args:
questions: List of dicts, each with "question", "options", and "multi_select".
Returns a JSON structure with a marker so the backend can detect it
and emit it as a special delta.ask_user event at the end of the stream.
The actual questions/options are already in the TOOL_CALL arguments,
so the frontend parses them directly from there. This response only
serves to acknowledge the tool call and minimize token usage in the
subsequent LLM inference round.
"""
normalized = []
for q in questions:
normalized.append({
"question": q.get("question", ""),
"options": q.get("options", []),
"multi_select": q.get("multi_select", False),
})
payload = {
"__type__": ASK_USER_MARKER,
"questions": normalized,
}
return {
"content": [
{"type": "text", "text": json.dumps(payload, ensure_ascii=False)}
{"type": "text", "text": ASK_USER_RESPONSE}
]
}
@ -168,7 +156,7 @@ async def handle_request(request: Dict[str, Any]) -> Dict[str, Any]:
request_id, -32602, "Missing required parameter: questions"
)
result = ask_user(questions)
result = ask_user()
return {"jsonrpc": "2.0", "id": request_id, "result": result}
else: