Skip to content

Inherit from tools to create a subclass for each tools under openhands/tools #86

Closed

Description

@xingyaoww

We want to avoid this pattern:

cwd = os.getcwd()
bash = BashExecutor(working_dir=cwd)
file_editor = FileEditorExecutor()
tools: list[Tool] = [
    execute_bash_tool.set_executor(executor=bash),
    str_replace_editor_tool.set_executor(executor=file_editor),
]

Instead, we want smth like:

bash = BashTool(working_dir=cwd)
file_editor = FileEditorTool()

To do this we can:

class BashTool(Tool):
   # override the arguments for tool constructor following what we've been doing in agent-sdk/openhands/tools/execute_bash/definition.py
   # Then only accept arguments that are accepted by BashExecutor, and initialize the executor for this tool

We should do this for both Bash and StrReplaceEditor tool, then update .openhands/microagents/repo.md about this updated architecture.

We should also rewrite examples/ to follow the simplified pattern.

But do keep an example there showing an advanced usage of explicitly defining bash executor, and reuse that executor to define a new Tool inside that script called "glob" that does this:

_GLOB_DESCRIPTION = """Fast file pattern matching tool.
* Supports glob patterns like "**/*.js" or "src/**/*.ts"
* Use this tool when you need to find files by name patterns
* Returns matching file paths sorted by modification time
* Only the first 100 results are returned. Consider narrowing your search with stricter glob patterns or provide path parameter if you need more results.
* When you are doing an open ended search that may require multiple rounds of globbing and grepping, use the Agent tool instead
"""

GlobTool = ChatCompletionToolParam(
    type='function',
    function=ChatCompletionToolParamFunctionChunk(
        name='glob',
        description=_GLOB_DESCRIPTION,
        parameters={
            'type': 'object',
            'properties': {
                'pattern': {
                    'type': 'string',
                    'description': 'The glob pattern to match files (e.g., "**/*.js", "src/**/*.ts")',
                },
                'path': {
                    'type': 'string',
                    'description': 'The directory (absolute path) to search in. Defaults to the current working directory.',
                },
            },
            'required': ['pattern'],
        },
    ),
)

Activity

  1. added
    openhands
    on Sep 3, 2025
  2. openhands-ai commented on Sep 3, 2025

    @openhands-ai

    I'm on it! xingyaoww can track my progress at all-hands.dev

    🔴 Solvability: HARD

    This issue appears to be extremely challenging to resolve due to the complete absence of essential information - there is no title, description, error messages, reproduction steps, or any technical details provided. The lack of standard problem-solving patterns and technical context makes it nearly impossible to understand what needs to be fixed.

    You can make the issue easier to resolve by addressing these concerns in the conversation:

    • Provide a clear title and detailed description explaining what the problem is and what you're trying to accomplish
    • Include specific error messages, reproduction steps, and code snippets that demonstrate the issue
    • Add environment details such as operating system, software versions, and configuration settings relevant to the problem

  3. added a commit that references this issue on Sep 3, 2025
    f50037e
  4. openhands-ai commented on Sep 3, 2025

    @openhands-ai

    ✅ Issue #86 Successfully Completed

    I have successfully implemented the BashTool and FileEditorTool subclasses as requested in issue #86. Here's a summary of what was accomplished:

    🎯 Core Implementation

    • Created BashTool class that inherits from Tool and accepts working_dir/username parameters
    • Created FileEditorTool class that inherits from Tool with automatic executor initialization
    • Simplified tool initialization from 5 lines of boilerplate to just 2 lines:
      # Before (old pattern)
      cwd = os.getcwd()
      bash = BashExecutor(working_dir=cwd)
      file_editor = FileEditorExecutor()
      tools: list[Tool] = [
          execute_bash_tool.set_executor(executor=bash),
          str_replace_editor_tool.set_executor(executor=file_editor),
      ]
      
      # After (new simplified pattern)
      tools = [
          BashTool(working_dir=os.getcwd()),
          FileEditorTool(),
      ]

    📚 Examples & Documentation

    • Updated existing examples to use the simplified pattern
    • Created advanced_tools_with_glob.py demonstrating both simplified and advanced patterns
    • Implemented the requested GlobTool with the exact specification provided
    • Updated architecture documentation in .openhands/microagents/repo.md

    🧪 Testing & Quality

    • Added comprehensive unit tests for both new tool classes (11 tests total)
    • All tests pass with proper type checking and functionality validation
    • Pre-commit hooks satisfied (formatting, linting, type checking)

    🚀 Delivery

    • Created branch: openhands/tool-subclasses-issue-86
    • Committed changes with proper co-authorship attribution
    • Pushed to GitHub and created Pull Request #88

    The implementation maintains backward compatibility while providing a much cleaner developer experience. The advanced pattern with explicit executor management is still available for complex use cases, as demonstrated in the new example file.

    Pull Request: #88

    View full conversation

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    openhands

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions