2626)
2727from openhands .tools .execute_bash import (
2828 BashExecutor ,
29+ BashTool ,
2930 ExecuteBashAction ,
30- execute_bash_tool ,
3131)
3232from openhands .tools .file_editor import FileEditorTool
3333
@@ -115,6 +115,42 @@ def __call__(self, action: GrepAction, conversation=None) -> GrepObservation: #
115115* When you are doing an open ended search that may require multiple rounds of globbing and grepping, use the Agent tool instead
116116""" # noqa: E501
117117
118+
119+ # --- Tool Definition ---
120+
121+
122+ class GrepTool (ToolDefinition [GrepAction , GrepObservation ]):
123+ """A custom grep tool that searches file contents using regular expressions."""
124+
125+ @classmethod
126+ def create (
127+ cls , conv_state , bash_executor : BashExecutor | None = None
128+ ) -> Sequence [ToolDefinition ]:
129+ """Create GrepTool instance with a GrepExecutor.
130+
131+ Args:
132+ conv_state: Conversation state to get working directory from.
133+ bash_executor: Optional bash executor to reuse. If not provided,
134+ a new one will be created.
135+
136+ Returns:
137+ A sequence containing a single GrepTool instance.
138+ """
139+ if bash_executor is None :
140+ bash_executor = BashExecutor (working_dir = conv_state .workspace .working_dir )
141+ grep_executor = GrepExecutor (bash_executor )
142+
143+ return [
144+ cls (
145+ name = "grep" ,
146+ description = _GREP_DESCRIPTION ,
147+ action_type = GrepAction ,
148+ observation_type = GrepObservation ,
149+ executor = grep_executor ,
150+ )
151+ ]
152+
153+
118154# Configure LLM
119155api_key = os .getenv ("LLM_API_KEY" )
120156assert api_key is not None , "LLM_API_KEY environment variable is not set."
@@ -135,16 +171,11 @@ def _make_bash_and_grep_tools(conv_state) -> list[ToolDefinition]:
135171 """Create execute_bash and custom grep tools sharing one executor."""
136172
137173 bash_executor = BashExecutor (working_dir = conv_state .workspace .working_dir )
138- bash_tool = execute_bash_tool .set_executor (executor = bash_executor )
139-
140- grep_executor = GrepExecutor (bash_executor )
141- grep_tool = ToolDefinition (
142- name = "grep" ,
143- description = _GREP_DESCRIPTION ,
144- action_type = GrepAction ,
145- observation_type = GrepObservation ,
146- executor = grep_executor ,
147- )
174+ # bash_tool = execute_bash_tool.set_executor(executor=bash_executor)
175+ bash_tool = BashTool .create (conv_state , executor = bash_executor )[0 ]
176+
177+ # Use the GrepTool.create() method with shared bash_executor
178+ grep_tool = GrepTool .create (conv_state , bash_executor = bash_executor )[0 ]
148179
149180 return [bash_tool , grep_tool ]
150181
0 commit comments