Skip to content

πŸ™Œ 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.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'(?<!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.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'(?<!execute_)(?<!_)\bbash\b', 'powershell', result, flags=re.IGNORECASE
+ )
+ return result
+ return prompt
diff --git a/openhands/mcp/utils.py b/openhands/mcp/utils.py
index 86124b16c0..fd633e5301 100644
--- a/openhands/mcp/utils.py
+++ b/openhands/mcp/utils.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from openhands.controller.agent import Agent
+ from openhands.memory.memory import Memory
from mcp import McpError
@@ -20,7 +21,6 @@ from openhands.events.observation.mcp import MCPObservation
from openhands.events.observation.observation import Observation
from openhands.mcp.client import MCPClient
from openhands.mcp.error_collector import mcp_error_collector
-from openhands.memory.memory import Memory
from openhands.runtime.base import Runtime
from openhands.runtime.impl.cli.cli_runtime import CLIRuntime
diff --git a/openhands/utils/prompt.py b/openhands/utils/prompt.py
index e77c60b3d0..786a9167a3 100644
--- a/openhands/utils/prompt.py
+++ b/openhands/utils/prompt.py
@@ -4,7 +4,6 @@ from itertools import islice
from jinja2 import Template
-from openhands.agenthub.codeact_agent.tools.bash import refine_prompt
from openhands.controller.state.state import State
from openhands.core.message import Message, TextContent
from 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)