Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user commands in terminal with function calling #4955

Merged
merged 11 commits into from
Nov 14, 2024
22 changes: 20 additions & 2 deletions openhands/agenthub/codeact_agent/codeact_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from litellm import ModelResponse

import openhands.agenthub.codeact_agent.function_calling as codeact_function_calling
from openhands.agenthub.codeact_agent.function_calling import action_to_str
from openhands.controller.agent import Agent
from openhands.controller.state.state import State
from openhands.core.config import AgentConfig
Expand Down Expand Up @@ -148,12 +149,14 @@ def get_action_message(
action,
(
AgentDelegateAction,
CmdRunAction,
IPythonRunCellAction,
FileEditAction,
BrowseInteractiveAction,
),
) or (isinstance(action, AgentFinishAction) and action.source == 'agent'):
) or (
isinstance(action, (AgentFinishAction, CmdRunAction))
and action.source == 'agent'
):
tool_metadata = action.tool_call_metadata
assert tool_metadata is not None, (
'Tool call metadata should NOT be None when function calling is enabled. Action: '
Expand Down Expand Up @@ -184,6 +187,14 @@ def get_action_message(
content=content,
)
]
elif isinstance(action, CmdRunAction) and action.source == 'user':
content = [TextContent(text=action_to_str(action))]
return [
Message(
role='user',
content=content,
)
]
return []

def get_observation_message(
Expand Down Expand Up @@ -219,6 +230,13 @@ def get_observation_message(
message: Message
max_message_chars = self.llm.config.max_message_chars
if isinstance(obs, CmdOutputObservation):
# if the cause of this observation is a user action, show that clearly
if (
obs.cause
and isinstance(obs.cause, CmdRunAction)
and obs.cause.source == 'user'
):
obs.content += f'\n[Observed result of command executed by user:\n{obs.cause.command}]'
text = truncate_content(
obs.content + obs.interpreter_details, max_message_chars
)
Expand Down
8 changes: 8 additions & 0 deletions openhands/agenthub/codeact_agent/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,11 @@ def get_tools(
else:
tools.append(StrReplaceEditorTool)
return tools


def action_to_str(action: Action) -> str:
# for example, when the user runs a command from the terminal
# we want to show the command in the chat history
if isinstance(action, CmdRunAction) and action.source == 'user':
return f'User executed the command:\n{action.command}'
return ''
Loading