From a2c05c0dd7afe15aab89829bccc2597eb84a89bd Mon Sep 17 00:00:00 2001 From: alt-glitch Date: Mon, 16 Nov 2026 12:45:24 +0530 Subject: [PATCH] fix(core): keep create_custom_toolset for embedding hosts Projects that embed AIAgent and import toolsets.create_custom_toolset to give it an exact tool list import and run again instead of failing with ImportError. The shared-bot route-gate tests reach the gate through _live_adapter again, so a change that stops forwarding the target thread now fails them. The zh-Hans tools reference no longer shows an empty messaging toolset section. --- .../test_send_message_shared_bot_route_gate.py | 18 +++++++++++++++--- tests/tools/test_toolsets.py | 17 +++++++++++++++++ toolsets.py | 9 +++++++-- .../current/reference/tools-reference.md | 5 ----- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/tests/tools/test_send_message_shared_bot_route_gate.py b/tests/tools/test_send_message_shared_bot_route_gate.py index 4704539ecf..36ed865a6f 100644 --- a/tests/tools/test_send_message_shared_bot_route_gate.py +++ b/tests/tools/test_send_message_shared_bot_route_gate.py @@ -76,6 +76,18 @@ def _tool(args): return json.loads(send_message_tool(args)) +def _live_adapter_allows(platform, profile, chat_id, thread_id): + """The gate as the choke point applies it: ``_live_adapter`` resolves the profile itself (the + fixture's ``sec``) and must forward the target's thread.""" + from tools.send_message_senders import SharedBotTargetRefused, _live_adapter + + assert profile == "sec" + try: + return _live_adapter(platform, chat_id, thread_id)[1] is not None + except SharedBotTargetRefused: + return False + + def test_send_to_unrouted_chat_never_uses_the_primary_bot(satellite): slack = satellite.primary[Platform.SLACK] result = _tool({"action": "send", "target": "slack:C0UNROUTED", "message": "hi"}) @@ -136,7 +148,7 @@ def test_satellite_acts_in_its_inbound_routed_conversation_only(satellite, shape routes, origin = _ORIGIN_ROUTED[shape] platform = Platform(origin["platform"]) satellite.runner.config.profile_routes = parse_profile_routes(routes) - allowed = satellite.runner._shared_bot_target_allowed + allowed = _live_adapter_allows tokens = set_session_vars(profile="sec", **origin) try: assert allowed(platform, "sec", origin["chat_id"], None) is True @@ -164,7 +176,7 @@ def test_origin_topic_does_not_vouch_for_the_rest_of_its_chat(satellite): satellite.runner.config.profile_routes = parse_profile_routes([ {"name": "t", "platform": "telegram", "chat_id": "-100222", "thread_id": "7", "profile": "sec"}]) - allowed = satellite.runner._shared_bot_target_allowed + allowed = _live_adapter_allows tokens = set_session_vars(profile="sec", platform="telegram", chat_id="-100222", thread_id="7") try: assert allowed(Platform.TELEGRAM, "sec", "-100222", None) is False @@ -181,7 +193,7 @@ def test_shared_channel_origin_matches_on_the_turn_sender(satellite): satellite.runner.config.profile_routes = parse_profile_routes([ {"name": "u", "platform": "slack", "user_id": "U1", "profile": "sec"}]) - allowed = satellite.runner._shared_bot_target_allowed + allowed = _live_adapter_allows tokens = set_session_vars(profile="sec", platform="slack", chat_id="C0SHARED", actor_id="U1") try: assert allowed(Platform.SLACK, "sec", "C0SHARED", None) is True diff --git a/tests/tools/test_toolsets.py b/tests/tools/test_toolsets.py index ea0e07f5b4..c04d56fce1 100644 --- a/tests/tools/test_toolsets.py +++ b/tests/tools/test_toolsets.py @@ -8,6 +8,7 @@ from toolsets import ( resolve_toolset, get_all_toolsets, validate_toolset, + create_custom_toolset, get_toolset_info, ) @@ -118,6 +119,22 @@ class TestGetToolsetInfo: assert info["is_composite"] is True assert info["tool_count"] > len(info["direct_tools"]) +class TestCreateCustomToolset: + def test_runtime_creation(self): + create_custom_toolset( + name="_test_custom", + description="Test toolset", + tools=["web_search"], + includes=["terminal"], + ) + try: + tools = resolve_toolset("_test_custom") + assert "web_search" in tools + assert "terminal" in tools + assert validate_toolset("_test_custom") is True + finally: + del TOOLSETS["_test_custom"] + class TestRegistryOwnedToolsets: def test_registry_membership_is_live(self, monkeypatch): reg = ToolRegistry() diff --git a/toolsets.py b/toolsets.py index 1e2ba04bbd..588cb0f117 100644 --- a/toolsets.py +++ b/toolsets.py @@ -235,8 +235,8 @@ TOOLSETS = { # Toolset -> the session platforms it exists for, from the specs that carry ``platforms``. TOOLSET_SESSION_PLATFORMS = {name: spec["platforms"] for name, spec in TOOLSETS.items() if "platforms" in spec} -# Built-in names only: shared metrics may export only these names, so a plugin, MCP server or custom -# toolset name never leaves the machine. +# Captured before create_custom_toolset() can add user-named tools: shared metrics may export only +# these names, so a plugin, MCP server or custom toolset name never leaves the machine. BUILTIN_TOOL_NAMES = frozenset(tool for spec in TOOLSETS.values() for tool in spec["tools"]) BUILTIN_TOOLSET_NAMES = frozenset(TOOLSETS) @@ -488,6 +488,11 @@ def validate_toolset(name: str) -> bool: or name in _get_plugin_toolset_names() or name in _get_registry_toolset_aliases()) +def create_custom_toolset(name: str, description: str, tools: list[str] | None = None, includes: list[str] | None = None) -> None: + """Register a runtime toolset in TOOLSETS.""" + TOOLSETS[name] = _ts(description, tools or [], includes or []) + + def get_toolset_info(name: str) -> dict[str, Any]: """Toolset definition plus its resolved tools, or None if unknown.""" toolset = get_toolset(name) diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/tools-reference.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/tools-reference.md index 3878751695..736740311f 100644 --- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/tools-reference.md +++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/tools-reference.md @@ -119,11 +119,6 @@ description: "Hermes 内置工具权威参考,按工具集分组" |------|------|----------| | `memory` | 将重要信息保存到跨会话持久化的记忆中。你的记忆会在会话启动时出现在系统 prompt 中——这是你在对话之间记住用户信息和环境信息的方式。何时保存… | — | -## `messaging` 工具集 - -| 工具 | 描述 | 所需环境 | -|------|------|----------| - ## `session_search` 工具集 | 工具 | 描述 | 所需环境 |