mirror of
https://github.com/Zippland/Bubbles.git
synced 2026-03-02 08:09:52 +08:00
feat(tools): 为工具添加执行状态提示并优化工具使用指引
This commit is contained in:
@@ -20,6 +20,7 @@ class Tool:
|
||||
description: str
|
||||
parameters: dict # JSON Schema
|
||||
handler: Callable[..., str] = None # (ctx, **kwargs) -> str
|
||||
status_text: str = "" # 执行前发给用户的状态提示,空则不发
|
||||
|
||||
def to_openai_schema(self) -> dict:
|
||||
return {
|
||||
@@ -54,9 +55,30 @@ class ToolRegistry:
|
||||
return [t.to_openai_schema() for t in self._tools.values()]
|
||||
|
||||
def create_handler(self, ctx: Any) -> Callable[[str, dict], str]:
|
||||
"""创建一个绑定了消息上下文的 tool_handler 函数。"""
|
||||
"""创建一个绑定了消息上下文的 tool_handler 函数。
|
||||
|
||||
执行工具前,如果该工具配置了 status_text,会先给用户发一条状态提示,
|
||||
让用户知道"机器人在干什么"(类似 OpenClaw/OpenCode 的中间过程输出)。
|
||||
"""
|
||||
registry = self._tools
|
||||
|
||||
def _send_status(tool: 'Tool', arguments: dict) -> None:
|
||||
"""发送工具执行状态消息给用户。"""
|
||||
if not tool.status_text:
|
||||
return
|
||||
try:
|
||||
# 对搜索类工具,把查询关键词带上
|
||||
status = tool.status_text
|
||||
if tool.name == "web_search" and arguments.get("query"):
|
||||
status = f"{status}{arguments['query']}"
|
||||
elif tool.name == "lookup_chat_history" and arguments.get("keywords"):
|
||||
kw_str = "、".join(str(k) for k in arguments["keywords"][:3])
|
||||
status = f"{status}{kw_str}"
|
||||
|
||||
ctx.send_text(status, record_message=False)
|
||||
except Exception:
|
||||
pass # 状态提示失败不影响工具执行
|
||||
|
||||
def handler(tool_name: str, arguments: dict) -> str:
|
||||
tool = registry.get(tool_name)
|
||||
if not tool:
|
||||
@@ -64,6 +86,9 @@ class ToolRegistry:
|
||||
{"error": f"Unknown tool: {tool_name}"},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
_send_status(tool, arguments)
|
||||
|
||||
try:
|
||||
result = tool.handler(ctx, **arguments)
|
||||
if not isinstance(result, str):
|
||||
|
||||
@@ -142,6 +142,7 @@ def _handle_lookup_chat_history(ctx, mode: str = "", keywords: list = None,
|
||||
|
||||
tool_registry.register(Tool(
|
||||
name="lookup_chat_history",
|
||||
status_text="正在翻阅聊天记录: ",
|
||||
description=(
|
||||
"查询聊天历史记录。你当前只能看到最近的消息,调用此工具可以回溯更早的上下文。"
|
||||
"支持三种模式:\n"
|
||||
|
||||
@@ -106,6 +106,7 @@ tool_registry.register(Tool(
|
||||
"创建提醒。支持 once(一次性)、daily(每日)、weekly(每周) 三种类型。"
|
||||
"当前时间已在对话上下文中提供,请据此计算目标时间。"
|
||||
),
|
||||
status_text="正在设置提醒...",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -42,6 +42,7 @@ tool_registry.register(Tool(
|
||||
"在网络上搜索信息。用于回答需要最新数据、实时信息或你不确定的事实性问题。"
|
||||
"deep_research 仅在问题非常复杂、需要深度研究时才开启。"
|
||||
),
|
||||
status_text="正在联网搜索: ",
|
||||
parameters={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
Reference in New Issue
Block a user