Skip to content

Commit

Permalink
Remove get_prompt_manager and add get_system_message
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Feb 6, 2025
1 parent d30c21a commit 7be2124
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 42 deletions.
26 changes: 9 additions & 17 deletions openhands/agenthub/codeact_agent/codeact_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,24 @@ def __init__(
self.condenser = Condenser.from_config(self.config.condenser)
logger.debug(f'Using condenser: {self.condenser}')

def get_prompt_manager(
self,
microagent_dir: str | None = None,
disabled_microagents: list[str] | None = None,
) -> 'PromptManager':
"""Get a prompt manager instance for this agent.
Args:
microagent_dir: Directory containing microagent prompts
disabled_microagents: List of microagents to disable
def get_system_message(self) -> str:
"""Get the system message for this agent.
Returns:
A PromptManager instance
The system message as a string
"""
from openhands.utils.prompt import PromptManager
import os

# Determine the prompt_dir for CodeActAgent
current_dir = os.path.dirname(os.path.abspath(__file__))
prompt_dir = os.path.join(current_dir, 'prompts')
system_message_path = os.path.join(prompt_dir, 'system_message.txt')

return PromptManager(
prompt_dir=prompt_dir,
microagent_dir=microagent_dir,
disabled_microagents=disabled_microagents,
)
if os.path.exists(system_message_path):
with open(system_message_path, 'r') as f:
return f.read().strip()
else:
return "You are a helpful AI assistant."


def get_action_message(
Expand Down
11 changes: 5 additions & 6 deletions openhands/controller/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,21 @@ def list_agents(cls) -> list[str]:
raise AgentNotRegisteredError()
return list(cls._registry.keys())

@abstractmethod
def get_prompt_manager(
self,
microagent_dir: str | None = None,
disabled_microagents: list[str] | None = None,
) -> 'PromptManager':
) -> 'PromptManager | None':
"""Get a prompt manager instance for this agent.
This method should be implemented by subclasses to return a specialized prompt manager.
Each agent implementation should determine its own prompt_dir.
This method can be overridden by subclasses to return a specialized prompt manager.
By default, it returns None.
Args:
microagent_dir: Directory containing microagent prompts
disabled_microagents: List of microagents to disable
Returns:
A PromptManager instance or a subclass of PromptManager
A PromptManager instance, a subclass of PromptManager, or None
"""
pass
return None
28 changes: 9 additions & 19 deletions openhands/controller/agent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,17 @@ def __init__(
# replay-related
self._replay_manager = ReplayManager(replay_events)

# prompt manager and first user message tracking
microagent_dir = None
if agent.config.enable_prompt_extensions:
microagent_dir = os.path.join(
os.path.dirname(os.path.dirname(openhands.__file__)),
'microagents',
)

# Get prompt manager from agent to allow specialized implementations
self.prompt_manager = agent.get_prompt_manager(
microagent_dir=microagent_dir,
disabled_microagents=agent.config.disabled_microagents,
)
# First user message tracking
self._first_user_message_received = False

# Send system message at initialization
if not self.is_delegate and self.prompt_manager:
self.event_stream.add_event(
SystemMessageAction(content=self.prompt_manager.get_system_message()),
EventSource.AGENT,
)
# Send system message at initialization if the agent provides one
if not self.is_delegate and hasattr(agent, 'get_system_message'):
system_message = agent.get_system_message()
if system_message:
self.event_stream.add_event(
SystemMessageAction(content=system_message),
EventSource.AGENT,
)

async def close(self) -> None:
"""Closes the agent controller, canceling any ongoing tasks and unsubscribing from the event stream.
Expand Down

0 comments on commit 7be2124

Please sign in to comment.