Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions astrbot/core/computer/tools/python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import platform
from dataclasses import dataclass, field

import mcp
Expand All @@ -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": {
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_python_tools.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In these tests, platform.system() is called within each test function, which is a minor duplication. To adhere to the Don't Repeat Yourself (DRY) principle and improve the test structure, you could use a pytest fixture. A fixture that returns the result of platform.system() can be defined once and then passed as an argument to your test functions. This centralizes the logic and makes the tests cleaner and more maintainable.