diff --git a/astrbot/core/astr_agent_runtime/tool_executor.py b/astrbot/core/astr_agent_runtime/tool_executor.py index 13d07c30cb..06a0073a57 100644 --- a/astrbot/core/astr_agent_runtime/tool_executor.py +++ b/astrbot/core/astr_agent_runtime/tool_executor.py @@ -20,6 +20,8 @@ from astrbot.core.agent_wakeup import wake_main_agent from astrbot.core.astr_agent_runtime.context import AstrAgentContext from astrbot.core.astr_agent_runtime.tools.computer_tools import ( + BrowserBatchExecTool, + BrowserExecTool, CuaKeyboardTypeTool, CuaMouseClickTool, CuaScreenshotTool, @@ -33,6 +35,7 @@ LocalExecuteShellTool, LocalPythonTool, PythonTool, + RunBrowserSkillTool, ShellSessionTool, ) from astrbot.core.chatui import ViewResult, emit_view @@ -316,6 +319,7 @@ def _get_runtime_computer_tools( cls, runtime: str, booter: str | None = None, + session_id: str | None = None, ) -> dict[str, FunctionTool]: booter = "" if booter is None else str(booter).lower() if runtime == "sandbox": @@ -348,6 +352,31 @@ def _get_runtime_computer_tools( keyboard_type_tool.name: keyboard_type_tool, } ) + if booter == "shipyard_neo": + # Same gate as the main agent: browser tools ride along when the + # booted sandbox profile supports them, or before the first boot + # when capabilities are still unknown. + from astrbot.core.astr_agent_runtime.computer.computer_client import ( + session_booter, + ) + + existing_booter = session_booter.get(session_id) if session_id else None + capabilities = ( + getattr(existing_booter, "capabilities", None) + if existing_booter is not None + else None + ) + if capabilities is None or "browser" in capabilities: + tools.update( + { + tool.name: tool + for tool in ( + BrowserExecTool(), + BrowserBatchExecTool(), + RunBrowserSkillTool(), + ) + } + ) return tools if runtime == "local": shell_tool = LocalExecuteShellTool() @@ -374,20 +403,6 @@ def _build_handoff_toolset( run_context: ContextWrapper[AstrAgentContext], tools: list[str | FunctionTool] | None, ) -> ToolSet | None: - toolset = ToolSet() - if tools is not None: - available_tools = run_context.tools - for tool in tools: - if isinstance(tool, str): - resolved = ( - available_tools.get_tool(tool) if available_tools else None - ) - if resolved is not None and resolved.active: - toolset.add_tool(resolved) - elif isinstance(tool, FunctionTool): - toolset.add_tool(tool) - return None if toolset.empty() else toolset - ctx = run_context.context.context event = run_context.context.event cfg = ctx.get_config(umo=event.unified_msg_origin) @@ -401,18 +416,73 @@ def _build_handoff_toolset( runtime_computer_tools = cls._get_runtime_computer_tools( runtime, provider_settings.get("sandbox", {}).get("booter"), + session_id=event.unified_msg_origin, ) + toolset = ToolSet() # Keep persona semantics aligned with the main agent: tools=None means # "all tools", including runtime computer-use tools. - handoff_names = { - tool.name for tool in tool_mgr.func_list if isinstance(tool, HandoffTool) - } - for registered_tool in tool_mgr.get_full_tool_set(): - if registered_tool.name not in handoff_names and registered_tool.active: - toolset.add_tool(registered_tool) + if tools is None: + handoff_names = { + tool.name + for tool in tool_mgr.func_list + if isinstance(tool, HandoffTool) + } + for registered_tool in tool_mgr.get_full_tool_set(): + if registered_tool.name in handoff_names: + continue + if registered_tool.active: + toolset.add_tool(registered_tool) + for runtime_tool in runtime_computer_tools.values(): + toolset.add_tool(runtime_tool) + return None if toolset.empty() else toolset + + # Mirror the main agent: a persona tool whitelist narrows the plugin + # and MCP tools, but the runtime computer-use tools stay available + # even when the whitelist cannot name them (the dashboard only lists + # plugin tools, so builtin tool names never appear in `tools`). An + # empty whitelist keeps the runtime tools too, same as an empty + # persona whitelist on the main agent. + unknown_names: list[str] = [] + inactive_names: list[str] = [] for runtime_tool in runtime_computer_tools.values(): toolset.add_tool(runtime_tool) + available_tools = run_context.tools + for tool_name_or_obj in tools: + if isinstance(tool_name_or_obj, str): + registered_tool = ( + available_tools.get_tool(tool_name_or_obj) + if available_tools + else llm_tools.get_func(tool_name_or_obj) + ) + if registered_tool is not None: + if registered_tool.active: + toolset.add_tool(registered_tool) + else: + inactive_names.append(tool_name_or_obj) + continue + runtime_tool = runtime_computer_tools.get(tool_name_or_obj) + if runtime_tool is None: + unknown_names.append(tool_name_or_obj) + else: + toolset.add_tool(runtime_tool) + elif isinstance(tool_name_or_obj, FunctionTool): + toolset.add_tool(tool_name_or_obj) + if unknown_names: + logger.warning( + "Subagent tool whitelist references unknown tools (skipped): %s", + ", ".join(sorted(unknown_names)), + ) + if inactive_names: + logger.warning( + "Subagent tool whitelist references inactive tools (skipped): %s", + ", ".join(sorted(inactive_names)), + ) + if toolset.empty(): + logger.warning( + "Subagent handoff ended up with an empty toolset; the agent " + "will run without any tools." + ) return None if toolset.empty() else toolset @classmethod diff --git a/astrbot/core/utils/media_utils.py b/astrbot/core/utils/media_utils.py index 687ece38be..04f0896d04 100644 --- a/astrbot/core/utils/media_utils.py +++ b/astrbot/core/utils/media_utils.py @@ -234,7 +234,11 @@ def file_uri_to_path(file_uri: MediaRefStr) -> str: if netloc and netloc.lower() != "localhost": if len(netloc) == 2 and netloc[1] == ":" and netloc[0].isalpha(): return str(Path(url2pathname(f"{netloc}{path}"))) - return str(Path(url2pathname(f"//{netloc}{path}"))) + if os.name == "nt": + return str(Path(url2pathname(f"//{netloc}{path}"))) + # POSIX has no standard representation for a remote file URI authority. + # Preserve it rather than making urllib reject an otherwise valid image. + return str(Path(f"//{netloc}{path}")) path = url2pathname(path) # url2pathname keeps "/" on POSIX but converts it to "\" on Windows, so diff --git a/tests/unit/test_astr_agent_tool_exec.py b/tests/unit/test_astr_agent_tool_exec.py index 071b6445b5..8150f8b3d4 100644 --- a/tests/unit/test_astr_agent_tool_exec.py +++ b/tests/unit/test_astr_agent_tool_exec.py @@ -1,5 +1,7 @@ import asyncio import json +import os +from pathlib import Path from types import SimpleNamespace from unittest.mock import AsyncMock @@ -277,6 +279,149 @@ def test_build_handoff_toolset_keeps_permission_guards_for_default_tools(runtime ) +def _whitelist_run_context(runtime: str, extra_provider_settings: dict | None = None): + provider_settings = {"computer_use_runtime": runtime} + if extra_provider_settings: + provider_settings.update(extra_provider_settings) + event = _DummyEvent() + context = SimpleNamespace( + get_config=lambda **_kwargs: {"provider_settings": provider_settings}, + get_llm_tool_manager=lambda: FunctionToolManager(), + ) + return ContextWrapper(context=SimpleNamespace(event=event, context=context)) + + +def test_build_handoff_toolset_whitelist_keeps_computer_use_tools(monkeypatch): + # The persona whitelist only lists plugin/MCP tools, but the builtin + # computer-use tools must stay available, mirroring the main agent where + # they are applied after the persona whitelist narrowing and never pass + # through it. + plugin_tool = FunctionTool( + name="plugin_tool", + description="plugin tool", + parameters={"type": "object", "properties": {}}, + ) + monkeypatch.setattr( + "astrbot.core.astr_agent_runtime.tool_executor.llm_tools.func_list", + [plugin_tool], + ) + + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox"), tools=["plugin_tool"] + ) + + assert toolset is not None + assert toolset.get_tool("plugin_tool") is not None + assert toolset.get_tool("astrbot_execute_ipython") is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + + +def test_build_handoff_toolset_whitelist_warns_and_skips_unknown_names( + monkeypatch, caplog +): + monkeypatch.setattr( + "astrbot.core.astr_agent_runtime.tool_executor.llm_tools.func_list", [] + ) + + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("none"), tools=["missing_tool"] + ) + + assert toolset is None + assert any("missing_tool" in rec.message for rec in caplog.records) + assert any("empty toolset" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_whitelist_warns_on_inactive_tool(monkeypatch, caplog): + inactive_tool = FunctionTool( + name="off_tool", + description="disabled plugin tool", + parameters={"type": "object", "properties": {}}, + ) + inactive_tool.active = False + monkeypatch.setattr( + "astrbot.core.astr_agent_runtime.tool_executor.llm_tools.func_list", + [inactive_tool], + ) + + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("local"), tools=["off_tool", "astrbot_grep_tool"] + ) + + # The inactive plugin tool is skipped with a warning, while an explicit + # runtime tool name still resolves. + assert toolset is not None + assert toolset.get_tool("off_tool") is None + assert toolset.get_tool("astrbot_grep_tool") is not None + assert any("off_tool" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_empty_whitelist_keeps_runtime_tools(caplog): + # An empty whitelist narrows the plugin tools to nothing but keeps the + # runtime computer-use tools, same as an empty persona whitelist on the + # main agent. + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox"), tools=[] + ) + + assert toolset is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + assert toolset.get_tool("astrbot_execute_ipython") is not None + assert not any("empty toolset" in rec.message for rec in caplog.records) + + +def test_build_handoff_toolset_empty_whitelist_without_runtime_warns(caplog): + with caplog.at_level("WARNING"): + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("none"), tools=[] + ) + + assert toolset is None + assert any("empty toolset" in rec.message for rec in caplog.records) + + +@pytest.mark.parametrize( + ("booter_state", "expect_browser"), + [ + pytest.param("not_booted", True, id="capabilities_unknown_before_first_boot"), + pytest.param("browser", True, id="profile_supports_browser"), + pytest.param("shell_only", False, id="profile_without_browser"), + ], +) +def test_build_handoff_toolset_seeds_browser_tools_like_main_agent( + monkeypatch, booter_state, expect_browser +): + from astrbot.core.astr_agent_runtime.computer.computer_client import session_booter + + umo = _DummyEvent().unified_msg_origin + if booter_state == "not_booted": + monkeypatch.delitem(session_booter, umo, raising=False) + elif booter_state == "browser": + monkeypatch.setitem( + session_booter, umo, SimpleNamespace(capabilities=["browser", "shell"]) + ) + else: + monkeypatch.setitem( + session_booter, umo, SimpleNamespace(capabilities=["shell", "python"]) + ) + + toolset = FunctionToolExecutor._build_handoff_toolset( + _whitelist_run_context("sandbox", {"sandbox": {"booter": "shipyard_neo"}}), + tools=[], + ) + + assert toolset is not None + assert toolset.get_tool("astrbot_execute_shell") is not None + assert (toolset.get_tool("astrbot_execute_browser") is not None) == expect_browser + assert ( + toolset.get_tool("astrbot_execute_browser_batch") is not None + ) == expect_browser + assert (toolset.get_tool("astrbot_run_browser_skill") is not None) == expect_browser + + @pytest.mark.asyncio async def test_collect_handoff_image_urls_normalizes_filters_and_appends_event_image( monkeypatch: pytest.MonkeyPatch, @@ -360,6 +505,13 @@ async def test_collect_handoff_image_urls_filters_supported_schemes_and_extensio ) assert set(result) == expected_supported_refs + if os.name != "nt": + from astrbot.core.utils.media_utils import file_uri_to_path + + assert file_uri_to_path("file://fileserver/share/image.webp") == str( + Path("//fileserver/share/image.webp") + ) + @pytest.mark.asyncio async def test_collect_handoff_image_urls_collects_event_image_when_args_is_none(