diff --git a/astrbot/core/computer/tools/python.py b/astrbot/core/computer/tools/python.py index cc835bc753..bf9aaa14e5 100644 --- a/astrbot/core/computer/tools/python.py +++ b/astrbot/core/computer/tools/python.py @@ -1,3 +1,4 @@ +import platform from dataclasses import dataclass, field import mcp @@ -10,6 +11,8 @@ from astrbot.core.computer.tools.permissions import check_admin_permission from astrbot.core.message.message_event_result import MessageChain +_OS_NAME = platform.system() + param_schema = { "type": "object", "properties": { @@ -61,7 +64,7 @@ async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult @dataclass class PythonTool(FunctionTool): name: str = "astrbot_execute_ipython" - description: str = "Run codes in an IPython shell." + description: str = f"Run codes in an IPython shell. Current OS: {_OS_NAME}." parameters: dict = field(default_factory=lambda: param_schema) async def call( @@ -83,7 +86,10 @@ async def call( @dataclass class LocalPythonTool(FunctionTool): name: str = "astrbot_execute_python" - description: str = "Execute codes in a Python environment." + description: str = ( + f"Execute codes in a Python environment. Current OS: {_OS_NAME}. " + "Use system-compatible commands." + ) parameters: dict = field(default_factory=lambda: param_schema) diff --git a/tests/unit/test_python_tools.py b/tests/unit/test_python_tools.py new file mode 100644 index 0000000000..2aae65d2a9 --- /dev/null +++ b/tests/unit/test_python_tools.py @@ -0,0 +1,17 @@ +import platform +from astrbot.core.computer.tools.python import PythonTool, LocalPythonTool + +def test_python_tool_description_contains_os(): + """测试 PythonTool 的描述中是否包含当前操作系统信息""" + tool = PythonTool() + current_os = platform.system() + assert current_os in tool.description + assert "IPython" in tool.description + +def test_local_python_tool_description_contains_os(): + """测试 LocalPythonTool 的描述中是否包含当前操作系统信息和兼容性提示""" + tool = LocalPythonTool() + current_os = platform.system() + assert current_os in tool.description + assert "Python environment" in tool.description + assert "system-compatible" in tool.description