From 30906422152909aba37486c2e7a7fb6222e4c2ed Mon Sep 17 00:00:00 2001 From: Bentlybro Date: Tue, 27 Jan 2026 10:27:59 +0000 Subject: [PATCH] Include tool messages in chat summarization Updated the chat summarization logic to include 'tool' role messages, ensuring important tool outputs are preserved in context. Also improved recent message slicing to prevent summary duplication when reducing message history. --- .../backend/backend/api/features/chat/service.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py index 18cf441974..cfb4cb09cb 100644 --- a/autogpt_platform/backend/backend/api/features/chat/service.py +++ b/autogpt_platform/backend/backend/api/features/chat/service.py @@ -697,7 +697,8 @@ async def _summarize_messages( for msg in messages: role = msg.get("role", "") content = msg.get("content", "") - if content and role in ("user", "assistant"): + # Include user, assistant, and tool messages (tool outputs are important context) + if content and role in ("user", "assistant", "tool"): conversation.append(f"{role.upper()}: {content}") conversation_text = "\n\n".join(conversation) @@ -896,11 +897,16 @@ async def _stream_chat_chunks( ) for keep_count in [12, 10, 8, 5]: - recent_messages = messages[-keep_count:] + # Slice from ORIGINAL recent_messages to avoid duplicating summary + reduced_recent = ( + recent_messages[-keep_count:] + if len(recent_messages) > keep_count + else recent_messages + ) if has_system_prompt: - messages = [system_msg, summary_msg] + recent_messages + messages = [system_msg, summary_msg] + reduced_recent else: - messages = [summary_msg] + recent_messages + messages = [summary_msg] + reduced_recent new_messages_dict = [] for msg in messages: