fix: refresh disabled custom tools

This commit is contained in:
JOJO 2026-05-30 22:13:12 +08:00
parent 9f37479272
commit dc0149f134
2 changed files with 46 additions and 0 deletions

View File

@ -162,6 +162,7 @@ class MainTerminalToolsDefinitionMixin:
# 更新分类为空列表,避免旧缓存
if "custom" in self.tool_categories_map:
self.tool_categories_map["custom"].tools = []
self._refresh_disabled_tools()
return []
tools: List[Dict] = []
@ -194,6 +195,7 @@ class MainTerminalToolsDefinitionMixin:
# 覆盖 custom 分类的工具列表
if "custom" in self.tool_categories_map:
self.tool_categories_map["custom"].tools = tool_ids
self._refresh_disabled_tools()
return tools

View File

@ -51,6 +51,50 @@ class ServerRefactorSmokeTest(unittest.TestCase):
self.assertTrue(callable(conversation.compute_workspace_storage))
self.assertTrue(callable(conversation.collect_upload_events))
def test_custom_tools_refresh_disabled_after_dynamic_load(self):
from core.main_terminal_parts.tools_definition import MainTerminalToolsDefinitionMixin
class Registry:
def reload(self):
return [
{
"id": "demo_tool",
"description": "Demo tool",
"parameters": {"type": "object", "properties": {}},
}
]
class Terminal(MainTerminalToolsDefinitionMixin):
custom_tools_enabled = True
user_role = "admin"
custom_tool_registry = Registry()
def __init__(self):
self.tool_categories_map = {
"custom": SimpleNamespace(
tools=[],
default_enabled=True,
silent_when_disabled=False,
)
}
self.tool_category_states = {"custom": False}
self.admin_forced_category_states = {}
self.disabled_tools = set()
def _refresh_disabled_tools(self):
disabled = set()
for key, category in self.tool_categories_map.items():
if not self.tool_category_states.get(key, category.default_enabled):
disabled.update(category.tools)
self.disabled_tools = disabled
terminal = Terminal()
custom_tools = terminal._build_custom_tools()
self.assertEqual(custom_tools[0]["function"]["name"], "demo_tool")
self.assertEqual(terminal.tool_categories_map["custom"].tools, ["demo_tool"])
self.assertIn("demo_tool", terminal.disabled_tools)
if __name__ == "__main__":
unittest.main()