π OpenHands: AI-Driven Development
Fix circular import by moving refine_prompt to dedicated module (#10223)
Tim O'Farrell4651edd5b319
diff --git a/openhands/agenthub/codeact_agent/tools/bash.py b/openhands/agenthub/codeact_agent/tools/bash.pyindex 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_promptfrom 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'(?<!execute_)(?<!_)\bbash\b', 'powershell', result, flags=re.IGNORECASE- )- return result- return prompt--def create_cmd_run_tool(use_short_description: bool = False,) -> ChatCompletionToolParam:diff --git a/openhands/agenthub/codeact_agent/tools/prompt.py b/openhands/agenthub/codeact_agent/tools/prompt.pynew file mode 100644index 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'(?<!execute_)(?<!_)\bbash\b', 'powershell', result, flags=re.IGNORECASE+ )+ return result+ return promptdiff --git a/openhands/mcp/utils.py b/openhands/mcp/utils.pyindex 86124b16c0..fd633e5301 100644--- a/openhands/mcp/utils.py+++ b/openhands/mcp/utils.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKINGif TYPE_CHECKING:from openhands.controller.agent import Agent+ from openhands.memory.memory import Memoryfrom mcp import McpError@@ -20,7 +21,6 @@ from openhands.events.observation.mcp import MCPObservationfrom openhands.events.observation.observation import Observationfrom openhands.mcp.client import MCPClientfrom openhands.mcp.error_collector import mcp_error_collector-from openhands.memory.memory import Memoryfrom openhands.runtime.base import Runtimefrom openhands.runtime.impl.cli.cli_runtime import CLIRuntimediff --git a/openhands/utils/prompt.py b/openhands/utils/prompt.pyindex e77c60b3d0..786a9167a3 100644--- a/openhands/utils/prompt.py+++ b/openhands/utils/prompt.py@@ -4,7 +4,6 @@ from itertools import islicefrom jinja2 import Template-from openhands.agenthub.codeact_agent.tools.bash import refine_promptfrom openhands.controller.state.state import Statefrom openhands.core.message import Message, TextContentfrom openhands.events.observation.agent import MicroagentKnowledge@@ -92,6 +91,8 @@ class PromptManager:return Template(file.read())def get_system_message(self) -> str:+ from openhands.agenthub.codeact_agent.tools.prompt import refine_prompt+system_message = self.system_template.render().strip()return refine_prompt(system_message)