mirror of
https://github.com/AstrBotDevs/AstrBot.git
synced 2026-10-16 15:06:04 +08:00
fix: skip network policy notices for inactive tools
This commit is contained in:
@@ -301,7 +301,7 @@ class Context:
|
||||
if (
|
||||
tools
|
||||
and any(
|
||||
tools.get_tool(name)
|
||||
(tool := tools.get_tool(name)) is not None and tool.active
|
||||
for name in (
|
||||
"astrbot_execute_shell",
|
||||
"astrbot_shell_session",
|
||||
|
||||
@@ -4,28 +4,35 @@ from unittest.mock import AsyncMock, MagicMock
|
||||
import pytest
|
||||
|
||||
from astrbot.core.agent.tool import FunctionTool, ToolSet
|
||||
from astrbot.core.provider.provider import Provider
|
||||
from astrbot.core.provider.func_tool_manager import FunctionToolManager
|
||||
from astrbot.core.provider.provider import Provider
|
||||
from astrbot.core.star.context import Context
|
||||
from astrbot.core.star.star import StarMetadata, star_registry
|
||||
from astrbot.core.tools.computer_tools.shell import LocalExecuteShellTool
|
||||
from astrbot.core.tools.computer_tools.python import LocalPythonTool
|
||||
from astrbot.core.tools.computer_tools.shell import (
|
||||
LocalExecuteShellTool,
|
||||
ShellSessionTool,
|
||||
)
|
||||
from astrbot.core.tools.computer_tools.util import LOCAL_NETWORK_POLICY_NOTICE
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("tool_class", [LocalExecuteShellTool, LocalPythonTool, ShellSessionTool])
|
||||
@pytest.mark.parametrize(
|
||||
("runtime", "allow_network", "include_tools", "existing_notice", "expected_count"),
|
||||
("runtime", "allow_network", "tool_state", "existing_notice", "expected_count"),
|
||||
[
|
||||
("local", False, True, False, 1),
|
||||
("local", False, True, True, 1),
|
||||
("local", True, True, False, 0),
|
||||
("local", False, False, False, 0),
|
||||
("sandbox", False, True, False, 0),
|
||||
("none", False, True, False, 0),
|
||||
("local", False, "active", False, 1),
|
||||
("local", False, "active", True, 1),
|
||||
("local", True, "active", False, 0),
|
||||
("local", False, "missing", False, 0),
|
||||
("local", False, "inactive", False, 0),
|
||||
("sandbox", False, "active", False, 0),
|
||||
("none", False, "active", False, 0),
|
||||
],
|
||||
)
|
||||
async def test_tool_loop_agent_adds_network_policy_to_system_prompt(
|
||||
monkeypatch, runtime, allow_network, include_tools, existing_notice, expected_count
|
||||
monkeypatch, runtime, allow_network, tool_state, existing_notice, expected_count,
|
||||
tool_class,
|
||||
):
|
||||
async def finished_steps(_max_steps):
|
||||
for response in ():
|
||||
@@ -41,7 +48,10 @@ async def test_tool_loop_agent_adds_network_policy_to_system_prompt(
|
||||
provider_manager=SimpleNamespace(
|
||||
get_provider_by_id=AsyncMock(return_value=MagicMock(spec=Provider))
|
||||
),
|
||||
get_config=lambda **_kwargs: {
|
||||
get_config=MagicMock(side_effect=AssertionError("Caller policy must not be used")),
|
||||
)
|
||||
agent_config = SimpleNamespace(
|
||||
get_config=MagicMock(return_value={
|
||||
"provider_settings": {
|
||||
"computer_use_runtime": runtime,
|
||||
"computer_use_local_permissions": {
|
||||
@@ -52,12 +62,13 @@ async def test_tool_loop_agent_adds_network_policy_to_system_prompt(
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}),
|
||||
)
|
||||
event = SimpleNamespace(role="member", unified_msg_origin="test")
|
||||
event = SimpleNamespace(role="admin", unified_msg_origin="caller-test")
|
||||
agent_event = SimpleNamespace(role="member", unified_msg_origin="agent-test")
|
||||
tools = ToolSet()
|
||||
if include_tools:
|
||||
tools.add_tool(LocalExecuteShellTool())
|
||||
if tool_state != "missing":
|
||||
tools.add_tool(tool_class(active=tool_state == "active"))
|
||||
system_prompt = "Agent instructions."
|
||||
if existing_notice:
|
||||
system_prompt += f"\n{LOCAL_NETWORK_POLICY_NOTICE}"
|
||||
@@ -68,12 +79,17 @@ async def test_tool_loop_agent_adds_network_policy_to_system_prompt(
|
||||
chat_provider_id="test",
|
||||
tools=tools,
|
||||
system_prompt=system_prompt,
|
||||
agent_context=SimpleNamespace(context=context, event=event),
|
||||
agent_context=SimpleNamespace(context=agent_config, event=agent_event),
|
||||
)
|
||||
|
||||
request = runner.reset.await_args.kwargs["request"]
|
||||
assert request.system_prompt.startswith("Agent instructions.")
|
||||
assert request.system_prompt.count(LOCAL_NETWORK_POLICY_NOTICE) == expected_count
|
||||
context.get_config.assert_not_called()
|
||||
if tool_state == "active":
|
||||
agent_config.get_config.assert_called_with(umo="agent-test")
|
||||
else:
|
||||
agent_config.get_config.assert_not_called()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
|
||||
Reference in New Issue
Block a user