From c4dc8e3a327ebd1e719d2d9e319ee003dba66e65 Mon Sep 17 00:00:00 2001 From: toddmiao <1025332440@qq.com> Date: Mon, 2 Mar 2026 11:27:05 +0000 Subject: [PATCH 1/2] feat: add OS information to tool descriptions and implement unit tests --- astrbot/core/computer/tools/python.py | 7 +++++-- tests/unit/test_python_tools.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_python_tools.py diff --git a/astrbot/core/computer/tools/python.py b/astrbot/core/computer/tools/python.py index cc835bc753..fc83d48411 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 @@ -61,7 +62,9 @@ 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: {platform.system()}." + ) parameters: dict = field(default_factory=lambda: param_schema) async def call( @@ -83,7 +86,7 @@ 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: {platform.system()}. 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 From f37777dc4e313fbaca16c96b4111dce3c73af76d Mon Sep 17 00:00:00 2001 From: toddmiao <1025332440@qq.com> Date: Mon, 2 Mar 2026 12:02:59 +0000 Subject: [PATCH 2/2] refactor: use module-level constant for OS name as suggested in PR review --- astrbot/core/computer/tools/python.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/astrbot/core/computer/tools/python.py b/astrbot/core/computer/tools/python.py index fc83d48411..bf9aaa14e5 100644 --- a/astrbot/core/computer/tools/python.py +++ b/astrbot/core/computer/tools/python.py @@ -11,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": { @@ -62,9 +64,7 @@ async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult @dataclass class PythonTool(FunctionTool): name: str = "astrbot_execute_ipython" - description: str = ( - f"Run codes in an IPython shell. Current OS: {platform.system()}." - ) + description: str = f"Run codes in an IPython shell. Current OS: {_OS_NAME}." parameters: dict = field(default_factory=lambda: param_schema) async def call( @@ -86,7 +86,10 @@ async def call( @dataclass class LocalPythonTool(FunctionTool): name: str = "astrbot_execute_python" - description: str = f"Execute codes in a Python environment. Current OS: {platform.system()}. Use system-compatible commands." + 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)