Skip to content

Build and run agents you can see, understand and trust.

fix(ReActAgent): invoke the `record_to_memory` method before returning the reply message (#1039)

Osier-Yidd05db2f3324
diff --git a/src/agentscope/agent/_react_agent.py b/src/agentscope/agent/_react_agent.py
index c22ab0329..d4a3f5422 100644
--- a/src/agentscope/agent/_react_agent.py
+++ b/src/agentscope/agent/_react_agent.py
@@ -305,6 +305,7 @@ class ReActAgent(ReActAgentBase):
# -------------- The reasoning-acting loop --------------
# Cache the structured output generated in the finish function call
structured_output = None
+ reply_msg = None
for _ in range(self.max_iters):
# -------------- The reasoning process --------------
msg_reasoning = await self._reasoning(tool_choice)
@@ -339,12 +340,13 @@ class ReActAgent(ReActAgentBase):
if msg_reasoning.has_content_blocks("text"):
# Re-use the existing text response if any to avoid
# duplicate text generation
- return Msg(
+ reply_msg = Msg(
self.name,
msg_reasoning.get_content_blocks("text"),
"assistant",
metadata=structured_output,
)
+ break
# Generate a textual response in the next iteration
msg_hint = Msg(
@@ -383,11 +385,15 @@ class ReActAgent(ReActAgentBase):
# Exit the loop when no structured output is required (or
# already satisfied) and only text response is generated
msg_reasoning.metadata = structured_output
- return msg_reasoning
+ reply_msg = msg_reasoning
+ break
# When the maximum iterations are reached
- reply_msg = await self._summarizing()
- reply_msg.metadata = structured_output
+ # and no reply message is generated
+ if reply_msg is None:
+ reply_msg = await self._summarizing()
+ reply_msg.metadata = structured_output
+ await self.memory.add(reply_msg)
# Post-process the memory, long-term memory
if self._static_control:
@@ -399,7 +405,6 @@ class ReActAgent(ReActAgentBase):
],
)
- await self.memory.add(reply_msg)
return reply_msg
# pylint: disable=too-many-branches