Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): distinguish inactive tool groups from missing tools in check_tool_available (#2316)
wizardeurcf10eafd65d8
diff --git a/src/agentscope/tool/_toolkit.py b/src/agentscope/tool/_toolkit.pyindex 77d3bebfa..759388da5 100644--- a/src/agentscope/tool/_toolkit.py+++ b/src/agentscope/tool/_toolkit.py@@ -571,20 +571,28 @@ class Toolkit:"""tools = await self._get_available_tools(activated_groups)if tool_name not in tools:+ # The dict above is already filtered to the basic + activated+ # groups, so a tool from an inactive group is missing from it.+ # Look the name up across all registered groups to distinguish+ # "inactive" from "doesn't exist" - the same fallback call_tool+ # performs - so the agent gets the activation hint instead of a+ # misleading not-found error.+ all_tools = await self._get_available_tools(+ [_.name for _ in self.tool_groups],+ )+ if tool_name in all_tools:+ raise ToolGroupInactiveError(+ f"ToolGroupInactiveError: The tool '{tool_name}' in "+ f"group '{all_tools[tool_name].group}' is currently "+ f"inactive. You should first activate the group by "+ f"calling the "+ f"'{self.builtin_meta_tool.tool.name}' tool.",+ )raise ToolNotFoundError(f"ToolNotFoundError: The tool named '{tool_name}' doesn't "f"exist.",)- group_name = tools[tool_name].group- if group_name != "basic" and group_name not in activated_groups:- raise ToolGroupInactiveError(- f"ToolGroupInactiveError: The tool '{tool_name}' in group "- f"'{group_name}' is currently inactive. "- f"You should first activate the group by calling the "- f"'{self.builtin_meta_tool.tool.name}' tool.",- )-return tools[tool_name].toolasync def get_tool(self, name: str) -> ToolBase | None:diff --git a/tests/toolkit_test.py b/tests/toolkit_test.pyindex e9a1ea222..194fd1540 100644--- a/tests/toolkit_test.py+++ b/tests/toolkit_test.py@@ -30,6 +30,10 @@ from agentscope.permission import (PermissionDecision,PermissionBehavior,)+from agentscope.exception import (+ ToolNotFoundError,+ ToolGroupInactiveError,+)class Tool1(ToolBase):@@ -1388,6 +1392,40 @@ The tool instructions are a collection of suggestions, rules and notifications aself.assertEqual(await toolkit.get_tool_schemas(), [])+ async def test_check_tool_available_distinguishes_inactive_group(+ self,+ ) -> None:+ """A tool in an inactive group raises ToolGroupInactiveError with+ the activation hint (matching call_tool), while an unregistered+ name still raises ToolNotFoundError."""+ toolkit = Toolkit(+ tool_groups=[+ ToolGroup(+ name="group_1",+ description="Group 1",+ tools=[Tool1()],+ ),+ ],+ )++ # Inactive group -> the agent-facing check names the group and+ # the meta tool, instead of claiming the tool doesn't exist.+ with self.assertRaises(ToolGroupInactiveError) as ctx:+ await toolkit.check_tool_available("tool_1", [])+ self.assertIn("group_1", str(ctx.exception))+ self.assertIn(+ toolkit.builtin_meta_tool.tool.name,+ str(ctx.exception),+ )++ # Activated group -> resolves normally.+ tool = await toolkit.check_tool_available("tool_1", ["group_1"])+ self.assertEqual(tool.name, "tool_1")++ # Unregistered name -> still not found.+ with self.assertRaises(ToolNotFoundError):+ await toolkit.check_tool_available("no_such_tool", [])+class RemoveTitleFieldTest(TestCase):"""Unit tests for _remove_title_field."""