Skip to content

Commit

Permalink
Fix finish action (#5428)
Browse files Browse the repository at this point in the history
  • Loading branch information
enyst authored Dec 6, 2024
1 parent de81020 commit e816231
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions openhands/agenthub/codeact_agent/codeact_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ def get_action_message(
BrowseInteractiveAction,
BrowseURLAction,
),
) or (
isinstance(action, (AgentFinishAction, CmdRunAction))
and action.source == 'agent'
):
) or (isinstance(action, 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 All @@ -166,6 +163,7 @@ def get_action_message(

llm_response: ModelResponse = tool_metadata.model_response
assistant_msg = llm_response.choices[0].message

# Add the LLM message (assistant) that initiated the tool calls
# (overwrites any previous message with the same response_id)
pending_tool_call_action_messages[llm_response.id] = Message(
Expand All @@ -177,6 +175,33 @@ def get_action_message(
tool_calls=assistant_msg.tool_calls,
)
return []
elif isinstance(action, AgentFinishAction):
role = 'user' if action.source == 'user' else 'assistant'

# when agent finishes, it has tool_metadata
# which has already been executed, and it doesn't have a response
# when the user finishes (/exit), we don't have tool_metadata
tool_metadata = action.tool_call_metadata
if tool_metadata is not None:
# take the response message from the tool call
assistant_msg = tool_metadata.model_response.choices[0].message
content = assistant_msg.content or ''

# save content if any, to thought
if action.thought:
if action.thought != content:
action.thought += '\n' + content
else:
action.thought = content

# remove the tool call metadata
action.tool_call_metadata = None
return [
Message(
role=role,
content=[TextContent(text=action.thought)],
)
]
elif isinstance(action, MessageAction):
role = 'user' if action.source == 'user' else 'assistant'
content = [TextContent(text=action.content or '')]
Expand Down

0 comments on commit e816231

Please sign in to comment.