feat: add OS information to tool descriptions and implement unit tests#5677
feat: add OS information to tool descriptions and implement unit tests#5677Soulter merged 2 commits intoAstrBotDevs:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the LLM Agent's ability to interact with Python execution tools by providing crucial context about the underlying operating system. By dynamically injecting OS information into tool descriptions, the agent can now generate platform-compatible commands, preventing errors that arise from OS-specific syntax mismatches. This improvement is validated by newly introduced unit tests, ensuring the reliable delivery of OS context to the agent. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Hey - 我已经检查了你的更改,看起来很棒!
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的评审。
Original comment in English
Hey - I've reviewed your changes and they look great!
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Documentation Updates 1 document(s) were updated by changes in this PR: pr4697的改动View Changes@@ -82,6 +82,7 @@
- **`runtime="sandbox"`** 提供以下工具:
- `EXECUTE_SHELL_TOOL`:在沙盒环境中执行 Shell 命令
- `PYTHON_TOOL`:在沙盒环境中执行 Python 代码
+ - 工具描述中包含当前操作系统信息(通过 `platform.system()` 获取),帮助 LLM 生成与平台兼容的代码
- `FILE_UPLOAD_TOOL`:上传文件到沙盒环境
- `FILE_DOWNLOAD_TOOL`:从沙盒环境下载文件
@@ -91,8 +92,20 @@
- 支持可选的 `cwd`(工作目录)参数
- `LOCAL_PYTHON_TOOL`:在本地环境中执行 Python 代码
- 在 `local_sandboxed` 模式下,文件写入受限于工作区目录(`~/.astrbot/workspace/<session>`)
+ - 工具描述中包含当前操作系统信息(通过 `platform.system()` 获取),帮助 LLM 生成与平台兼容的代码
这些工具在 SubAgent handoff 场景下可正常使用,与主 Agent 运行时动态挂载的工具保持一致。
+
+**操作系统感知增强(PR #5677)**
+
+Python 执行工具(`PYTHON_TOOL` 和 `LOCAL_PYTHON_TOOL`)的工具描述中动态注入了当前操作系统信息,使 LLM Agent 能够生成与平台兼容的代码:
+
+- **自动注入 OS 信息**:工具描述通过 `platform.system()` 获取操作系统类型(如 Linux、Windows、Darwin),并在描述中明确告知 Agent
+- **防止不兼容命令**:避免 Agent 生成跨平台不兼容的代码(例如在 Linux 上使用 PowerShell 特定语法,或在 Windows 上使用 Bash 特定命令)
+- **系统兼容提示**:`LOCAL_PYTHON_TOOL` 的描述中包含明确的"使用系统兼容命令"提示,引导 Agent 编写适配当前平台的代码
+- **适用范围**:该特性对沙盒环境(`PYTHON_TOOL`)和本地环境(`LOCAL_PYTHON_TOOL`)均生效
+
+该增强使 Agent 在调用 Python 工具时能够自动适配底层操作系统,提高了代码执行的成功率和跨平台兼容性。
> ⚠️ **REVERTED**: Shipyard Neo 运行时及相关浏览器自动化工具(`BROWSER_EXECUTE_TOOL`、`BROWSER_BATCH_EXECUTE_TOOL`、`RUN_BROWSER_SKILL_TOOL`)已在 [PR #5624](https://github.com/AstrBotDevs/AstrBot/pull/5624) 中被移除,不再可用。
|
There was a problem hiding this comment.
Code Review
This pull request successfully adds operating system information to the descriptions of Python tools, which will help the LLM Agent generate platform-compatible code. The implementation is straightforward and includes new unit tests to verify the functionality. My feedback includes a couple of suggestions to improve code maintainability and adhere to the DRY (Don't Repeat Yourself) principle, both in the application code and the new tests. Specifically, I recommend using a module-level constant for the OS name to avoid redundant calls and making the tests cleaner by using a pytest fixture.
| 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 |
There was a problem hiding this comment.
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.
|
No docs changes were generated in this run (docs repo had no updates). Docs repo: AstrBotDevs/AstrBot-docs AI change summary (not committed):
Experimental bot notice:
|
Motivation / 改动点动机
目前,LLM Agent 在调用 Python 执行工具时,往往不知道底层操作系统是什么。这可能导致它生成不兼容的系统命令(例如在 Linux 上使用 PowerShell 语法,或在 Windows 上使用特定的 Bash 命令)。本 PR
在工具描述中动态注入了实时操作系统信息,使 Agent 能够编写与平台兼容的代码。
Currently, when the LLM Agent calls Python execution tools, it is often unaware of the underlying operating system. This PR injects real-time OS information into the tool descriptions, enabling the Agent to write
platform-compatible code.
Modifications / 改动点
astrbot/core/computer/tools/python.py:tests/unit/test_python_tools.py:This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Unit Test Result / 单元测试结果:
1 platform linux -- Python 3.12.3, pytest-9.0.2, pluggy-1.6.0
2 rootdir: /home/toddmiao/TEST/AstrBot-dev
3 collected 2 items
4
5 tests/unit/test_python_tools.py .. [100%]
6
7 ============================== 2 passed in 3.39s ===============================
Ruff Check Result / 代码规范检查结果:
1 All checks passed!
Summary by Sourcery
将运行时操作系统信息注入到 Python 执行工具的描述中,并添加相应测试进行验证。
New Features:
Tests:
PythonTool和LocalPythonTool的描述中包含操作系统信息及兼容性提示。Original summary in English
Summary by Sourcery
Inject runtime operating system information into Python execution tool descriptions and add tests to verify it.
New Features:
Tests: