diff --git a/openhands/agenthub/codeact_agent/tools/bash.py b/openhands/agenthub/codeact_agent/tools/bash.py index 73ac602056..4787cf85cb 100644 --- a/openhands/agenthub/codeact_agent/tools/bash.py +++ b/openhands/agenthub/codeact_agent/tools/bash.py @@ -1,8 +1,6 @@ -import re -import sys - from litellm import ChatCompletionToolParam, ChatCompletionToolParamFunctionChunk +from openhands.agenthub.codeact_agent.tools.prompt import refine_prompt from openhands.llm.tool_names import EXECUTE_BASH_TOOL_NAME _DETAILED_BASH_DESCRIPTION = """Execute a bash command in the terminal within a persistent shell session. @@ -36,21 +34,6 @@ _SHORT_BASH_DESCRIPTION = """Execute a bash command in the terminal. * One command at a time: You can only execute one bash command at a time. If you need to run multiple commands sequentially, you can use `&&` or `;` to chain them together.""" -def refine_prompt(prompt: str): - if sys.platform == 'win32': - # Replace 'bash' with 'powershell' including tool names like 'execute_bash' - # First replace 'execute_bash' with 'execute_powershell' to handle tool names - result = re.sub( - r'\bexecute_bash\b', 'execute_powershell', prompt, flags=re.IGNORECASE - ) - # Then replace standalone 'bash' with 'powershell' - result = re.sub( - r'(? ChatCompletionToolParam: diff --git a/openhands/agenthub/codeact_agent/tools/prompt.py b/openhands/agenthub/codeact_agent/tools/prompt.py new file mode 100644 index 0000000000..6c1548a2c8 --- /dev/null +++ b/openhands/agenthub/codeact_agent/tools/prompt.py @@ -0,0 +1,29 @@ +import re +import sys + + +def refine_prompt(prompt: str): + """ + Refines the prompt based on the platform. + + On Windows systems, replaces 'bash' with 'powershell' and 'execute_bash' with 'execute_powershell' + to ensure commands work correctly on the Windows platform. + + Args: + prompt: The prompt text to refine + + Returns: + The refined prompt text + """ + if sys.platform == 'win32': + # Replace 'bash' with 'powershell' including tool names like 'execute_bash' + # First replace 'execute_bash' with 'execute_powershell' to handle tool names + result = re.sub( + r'\bexecute_bash\b', 'execute_powershell', prompt, flags=re.IGNORECASE + ) + # Then replace standalone 'bash' with 'powershell' + result = re.sub( + r'(? str: + from openhands.agenthub.codeact_agent.tools.prompt import refine_prompt + system_message = self.system_template.render().strip() return refine_prompt(system_message)