feat:采用tool索引展开方式压缩tool,移除tool过滤器

This commit is contained in:
SengokuCola
2026-04-09 20:29:01 +08:00
parent 243b8deb43
commit 0852c38e81
10 changed files with 359 additions and 319 deletions

View File

@@ -24,6 +24,8 @@ from .reply import get_tool_spec as get_reply_tool_spec
from .reply import handle_tool as handle_reply_tool
from .send_emoji import get_tool_spec as get_send_emoji_tool_spec
from .send_emoji import handle_tool as handle_send_emoji_tool
from .tool_search import get_tool_spec as get_tool_search_tool_spec
from .tool_search import handle_tool as handle_tool_search_tool
from .view_complex_message import get_tool_spec as get_view_complex_message_tool_spec
from .view_complex_message import handle_tool as handle_view_complex_message_tool
from .wait import get_tool_spec as get_wait_tool_spec
@@ -52,6 +54,7 @@ def get_action_tool_specs() -> List[ToolSpec]:
get_query_jargon_tool_spec(),
get_query_memory_tool_spec(enabled=bool(global_config.memory.enable_memory_query_tool)),
get_send_emoji_tool_spec(),
get_tool_search_tool_spec(),
]
@@ -73,6 +76,7 @@ def get_all_builtin_tool_specs() -> List[ToolSpec]:
get_query_memory_tool_spec(enabled=True),
get_query_person_info_tool_spec(),
get_send_emoji_tool_spec(),
get_tool_search_tool_spec(),
]
@@ -111,6 +115,7 @@ def build_builtin_tool_handlers(tool_ctx: BuiltinToolRuntimeContext) -> Dict[str
),
"wait": lambda invocation, context=None: handle_wait_tool(tool_ctx, invocation, context),
"send_emoji": lambda invocation, context=None: handle_send_emoji_tool(tool_ctx, invocation, context),
"tool_search": lambda invocation, context=None: handle_tool_search_tool(tool_ctx, invocation, context),
"view_complex_message": lambda invocation, context=None: handle_view_complex_message_tool(
tool_ctx,
invocation,

View File

@@ -0,0 +1,106 @@
"""tool_search 内置工具。"""
from typing import Any, Dict, List, Optional
import json
from src.core.tooling import ToolExecutionContext, ToolExecutionResult, ToolInvocation, ToolSpec
from .context import BuiltinToolRuntimeContext
def get_tool_spec() -> ToolSpec:
"""获取 tool_search 工具声明。"""
return ToolSpec(
name="tool_search",
brief_description="在 deferred tools 列表中按名称或关键词搜索工具,并将命中的工具加入后续轮次的可用工具列表。",
detailed_description=(
"参数说明:\n"
"- queryString必填。工具名、前缀或关键词。\n"
"- limitInteger可选。最多返回多少个匹配工具默认为 5。"
),
parameters_schema={
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "要搜索的工具名、前缀或关键词。",
},
"limit": {
"type": "integer",
"description": "最多返回多少个匹配工具。",
"minimum": 1,
},
},
"required": ["query"],
},
provider_name="maisaka_builtin",
provider_type="builtin",
)
async def handle_tool(
tool_ctx: BuiltinToolRuntimeContext,
invocation: ToolInvocation,
context: Optional[ToolExecutionContext] = None,
) -> ToolExecutionResult:
"""执行 tool_search 内置工具。"""
del context
raw_query = invocation.arguments.get("query")
if not isinstance(raw_query, str) or not raw_query.strip():
return tool_ctx.build_failure_result(
invocation.tool_name,
"tool_search 需要提供非空的 `query` 字符串参数。",
)
raw_limit = invocation.arguments.get("limit", 5)
try:
limit = max(1, int(raw_limit))
except (TypeError, ValueError):
limit = 5
matched_tool_specs = tool_ctx.runtime.search_deferred_tool_specs(raw_query, limit=limit)
matched_tool_names = [tool_spec.name for tool_spec in matched_tool_specs]
newly_discovered_tool_names = tool_ctx.runtime.discover_deferred_tools(matched_tool_names)
structured_content: Dict[str, Any] = {
"query": raw_query.strip(),
"matched_tool_names": matched_tool_names,
"newly_discovered_tool_names": newly_discovered_tool_names,
}
if not matched_tool_names:
return tool_ctx.build_success_result(
invocation.tool_name,
"未找到匹配的 deferred tools请尝试更完整的工具名、前缀或其他关键词。",
structured_content=structured_content,
metadata={"record_display_prompt": "tool_search 未找到匹配工具。"},
)
content_lines: List[str] = [
f"已找到 {len(matched_tool_names)} 个 deferred tools它们会在后续轮次中加入可用工具列表",
*[f"- {tool_name}" for tool_name in matched_tool_names],
]
if newly_discovered_tool_names:
content_lines.extend(
[
"",
"本次新发现的工具:",
*[f"- {tool_name}" for tool_name in newly_discovered_tool_names],
]
)
else:
content_lines.extend(["", "这些工具此前已经发现过,无需重复展开。"])
return tool_ctx.build_success_result(
invocation.tool_name,
"\n".join(content_lines),
structured_content=structured_content,
metadata={
"matched_tool_names": matched_tool_names,
"newly_discovered_tool_names": newly_discovered_tool_names,
"record_display_prompt": json.dumps(structured_content, ensure_ascii=False),
},
)