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.
This commit is contained in:
Bentlybro
2026-01-27 10:27:59 +00:00
parent 27721145a5
commit 3090642215

View File

@@ -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: