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:
@@ -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
|
||||
|
||||
@@ -45,6 +45,7 @@ class ConsciousnessNode:
|
||||
global_state_machine: GlobalStateMachine,
|
||||
provider_title: str,
|
||||
model_id: str,
|
||||
tools=None,
|
||||
toolsets=None,
|
||||
locale: str | None = None,
|
||||
custom_system_prompt: str | None = None,
|
||||
@@ -67,6 +68,7 @@ class ConsciousnessNode:
|
||||
system_prompt=system_prompt,
|
||||
deps_type=ConsciousnessNodeDeps,
|
||||
agent_name="consciousness_node",
|
||||
tools=tools,
|
||||
toolsets=toolsets,
|
||||
)
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ class RegulatoryNode:
|
||||
global_state_machine: GlobalStateMachine,
|
||||
provider_title: str,
|
||||
model_id: str,
|
||||
tools=None,
|
||||
toolsets=None,
|
||||
locale: str | None = None,
|
||||
custom_system_prompt: str | None = None,
|
||||
@@ -59,7 +60,8 @@ class RegulatoryNode:
|
||||
global_state_machine: 全局状态机
|
||||
provider_title: 供应商名
|
||||
model_id: 模型id
|
||||
toolsets: 已装配好的 FunctionToolset 列表
|
||||
tools: 展开的 tool callable 列表
|
||||
toolsets: MCP 等外部 toolset 列表
|
||||
locale: 语言代码(zh/en),控制system prompt语言
|
||||
custom_system_prompt: 管理员自定义追加提示词(可选)
|
||||
Returns:
|
||||
@@ -84,6 +86,7 @@ class RegulatoryNode:
|
||||
system_prompt=system_prompt,
|
||||
deps_type=RegulatoryNodeDeps,
|
||||
agent_name="regulatory_node",
|
||||
tools=tools,
|
||||
toolsets=toolsets,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user