Skip to content

feat: add OS information to tool descriptions and implement unit tests#5677

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
miaoxutao123:feature-new-update
Mar 3, 2026
Merged

feat: add OS information to tool descriptions and implement unit tests#5677
Soulter merged 2 commits intoAstrBotDevs:masterfrom
miaoxutao123:feature-new-update

Conversation

@miaoxutao123
Copy link
Contributor

@miaoxutao123 miaoxutao123 commented Mar 2, 2026

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:

    • 导入了 platform 模块。
    • 更新了 PythonTool 和 LocalPythonTool 的描述,包含 platform.system()。
    • 增加了显式的系统兼容命令提示。
  • tests/unit/test_python_tools.py:

    • 新增单元测试文件,验证操作系统信息是否正确注入到工具描述中。
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

286f28b36a7d73065a7bb94f06e5c77c 0d67c2ae-7e38-4a9f-afac-616e1a8b9e83

可以看到通过描述注入后,Agent 一次即可调用成功。

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:

  • 在 Python 执行工具的描述中暴露当前操作系统名称,以指导生成与平台兼容的代码。

Tests:

  • 添加单元测试,确保 PythonToolLocalPythonTool 的描述中包含操作系统信息及兼容性提示。
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:

  • Expose the current operating system name in the descriptions of Python execution tools to guide platform-compatible code generation.

Tests:

  • Add unit tests to ensure PythonTool and LocalPythonTool descriptions include OS information and compatibility hints.

@auto-assign auto-assign bot requested review from Raven95676 and Soulter March 2, 2026 11:49
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 2, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • OS Information Injection: Dynamically injected the current operating system information into the descriptions of PythonTool and LocalPythonTool to help LLM Agents generate platform-compatible code.
  • Unit Tests: Added new unit tests to verify that the operating system information is correctly included in the tool descriptions.
Changelog
  • astrbot/core/computer/tools/python.py
    • Imported the platform module.
    • Updated the PythonTool description to include the current operating system.
    • Updated the LocalPythonTool description to include the current operating system and a hint for system-compatible commands.
  • tests/unit/test_python_tools.py
    • Added a new unit test file.
    • Implemented test_python_tool_description_contains_os to verify OS info in PythonTool.
    • Implemented test_local_python_tool_description_contains_os to verify OS info and compatibility hint in LocalPythonTool.
Activity
  • Unit tests were executed and passed successfully, collecting 2 items.
  • Code style checks (Ruff) were run and passed.
  • Screenshots demonstrating the agent's improved ability to call tools successfully after the description injection were provided.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我已经检查了你的更改,看起来很棒!


Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,请考虑分享给更多人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进之后的评审。
Original comment in English

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot
Copy link

dosubot bot commented Mar 2, 2026

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) 中被移除,不再可用。
 

How did I do? Any feedback?  Join Discord

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Mar 2, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +4 to +17
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
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.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 3, 2026
@Soulter Soulter merged commit 92ee534 into AstrBotDevs:master Mar 3, 2026
6 checks passed
@astrbot-doc-agent
Copy link

No docs changes were generated in this run (docs repo had no updates).

Docs repo: AstrBotDevs/AstrBot-docs
Trigger: PR merged


AI change summary (not committed):


Experimental bot notice:

  • This output is generated by AstrBot-Doc-Agent for review only.
  • It does not represent the final documentation form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants