_get_tool_label,_get_tool_icon 合并为一个函数_get_tool_meta

This commit is contained in:
jingrow 2026-06-30 01:47:07 +08:00
parent dfbca1b0a7
commit 9b1602587b

View File

@ -42,28 +42,17 @@ def _summarize_tool_result(result: dict) -> str:
return text
def _get_tool_label(tool_name: str) -> str:
"""根据工具名称返回 label前端友好显示名用于前端展示"""
try:
tool = ToolRegistry.get(tool_name)
if tool and tool.label:
return tool.label
except Exception:
pass
return tool_name
def _get_tool_icon(tool_name: str) -> tuple[str, str]:
"""根据工具名称返回 (icon, color),透传 JSON 配置值
def _get_tool_meta(tool_name: str) -> tuple[str, str, str]:
"""根据工具名称返回 (label, icon, color),透传 JSON 配置值
Returns:
(icon, color) 元组 ("tabler:search", "#3b82f6")
(label, icon, color) 三元组 ("Create Record", "tabler:plus", "#10b981")
"""
default = ("", "")
default = ("", "", "")
try:
tool = ToolRegistry.get(tool_name)
if tool:
return (tool.icon or "", tool.color or "")
return (tool.label or tool_name, tool.icon or "", tool.color or "")
except Exception:
pass
return default
@ -249,19 +238,19 @@ def chat_stream_sse(chat_name, content, file_record_names=None):
for tc in tool_calls_this_round:
# ----- 实时 Tool Call SSE: begin -----
tc_icon, tc_color = _get_tool_icon(tc["name"])
tc_label, tc_icon, tc_color = _get_tool_meta(tc["name"])
begin_data = {
"id": tc["id"],
"name": tc["name"],
"label": _get_tool_label(tc["name"]),
"label": tc_label,
"arguments": tc.get("arguments", "{}"),
"icon": tc_icon,
"color": tc_color,
}
yield sse_event("tool_call_begin", begin_data)
result = ToolRegistry.execute_fc(tc["name"], tc["arguments"])
# ----- 实时 Tool Call SSE: result -----
success = result.get("success", False)
yield sse_event("tool_call_result", {
@ -270,18 +259,18 @@ def chat_stream_sse(chat_name, content, file_record_names=None):
"success": success,
"summary": _summarize_tool_result(result),
})
current_messages.append({
"role": "tool",
"tool_call_id": tc["id"],
"content": json.dumps(result, ensure_ascii=False),
})
# 累积 tool call 到持久化列表
all_tool_calls.append({
"id": tc["id"],
"name": tc["name"],
"label": _get_tool_label(tc["name"]),
"label": tc_label,
"icon": tc_icon,
"color": tc_color,
"status": "completed" if success else "failed",