From e81623110d5107651c7b64e18c281bd114d65825 Mon Sep 17 00:00:00 2001 From: Engel Nyst Date: Fri, 6 Dec 2024 04:36:19 +0100 Subject: [PATCH] Fix finish action (#5428) --- .../agenthub/codeact_agent/codeact_agent.py | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/openhands/agenthub/codeact_agent/codeact_agent.py b/openhands/agenthub/codeact_agent/codeact_agent.py index 1113fd0271d3..931ae4b17a1c 100644 --- a/openhands/agenthub/codeact_agent/codeact_agent.py +++ b/openhands/agenthub/codeact_agent/codeact_agent.py @@ -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: ' @@ -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( @@ -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 '')]