feat: add OS information to tool descriptions and implement unit tests (#5677)
* feat: add OS information to tool descriptions and implement unit tests * refactor: use module-level constant for OS name as suggested in PR review
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import platform
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
import mcp
|
||||
@@ -10,6 +11,8 @@ from astrbot.core.computer.computer_client import get_booter, get_local_booter
|
||||
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 @@ class PythonTool(FunctionTool):
|
||||
@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)
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user