fix(toolset): 工具传递改为展开的 tools 列表,不再用 FunctionToolset 包装

前端/DB 仍用 toolset 做逻辑分组管理,但传给 pydantic-ai Agent 时
把 toolset 内的 callable 展开为 tools=[] 扁平列表,MCP server 等
需要 toolset 语义的单独走 toolsets=[] 参数。解决工具"存在但调不了"的问题。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 19:05:59 +00:00
parent 78d03388c0
commit b15eeb9e74
8 changed files with 76 additions and 67 deletions
@@ -142,31 +142,24 @@ def reset_local_cache() -> None:
# ─── 客户端 helper:从快照重建本地视图 ─────────────────────────────
def build_toolsets_for_scope(
def build_tools_for_scope(
snapshot: GSMSnapshot,
scope: str,
toolset_ids: Optional[List[str]] = None,
) -> List[Any]:
"""在调用方进程里按 ``snapshot`` 现场组装 FunctionToolset 列表。
) -> List[Callable]:
"""从快照中按 toolset 配置展开为扁平的 tool callable 列表。
新模型下"系统工具集"也存在 ``custom_toolsets`` 里(``is_system=True``),
所以本函数只按 ``toolset_ids`` 在 ``custom_toolsets`` 中按需挑选并装配;
所有工具函数(system + 第三方)都从 ``snapshot.all_funcs`` 统一查表
管理层面仍然用 toolset 做逻辑分组(前端展示、权限控制),
但传给 pydantic-ai Agent 时直接展开为 tools 列表,避免
FunctionToolset 封装带来的兼容性问题
Args:
snapshot: 当前 GSM 快照。
scope: 调用方所属 scope(保留参数未来可按 scope 过滤系统 toolset
的可见性,目前仅用于命名/日志)
toolset_ids: agent 配置的 toolset 列表;为 None 表示返回全部 toolset
(兼容老调用,但建议传入显式列表)。
scope: 调用方所属 scope(保留参数未来可按 scope 过滤可见性)。
toolset_ids: agent 配置的 toolset 列表;为 None 表示返回全部
"""
try:
from pydantic_ai.toolsets import FunctionToolset
except ImportError:
_logger.warning("pydantic_ai.toolsets unavailable; cannot build toolsets")
return []
result: List[Any] = []
result: List[Callable] = []
seen: set = set()
target_ids = (
list(toolset_ids)
if toolset_ids is not None
@@ -177,14 +170,12 @@ def build_toolsets_for_scope(
if not defn:
continue
names = defn.get("tools") or []
funcs = [snapshot.all_funcs[n] for n in names if n in snapshot.all_funcs]
if not funcs:
continue
try:
result.append(
FunctionToolset(tools=funcs, id=f"toolset::{toolset_id}")
)
except Exception as e: # pragma: no cover - 防御
_logger.error(f"build toolset {toolset_id} failed: {e}")
for name in names:
if name in seen:
continue
func = snapshot.all_funcs.get(name)
if func is not None:
result.append(func)
seen.add(name)
return result