Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(copilot): use messages_dict in fallback context compaction (#11922)

Ottof7350c797a53
diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py
index 20216162b5..ddc64d64b2 100644
--- a/autogpt_platform/backend/backend/api/features/chat/service.py
+++ b/autogpt_platform/backend/backend/api/features/chat/service.py
@@ -1184,11 +1184,14 @@ async def _stream_chat_chunks(
else recent_messages
)
# Ensure tool pairs stay intact in the reduced slice
- reduced_slice_start = max(
+ # Note: Search in messages_dict (full conversation) not recent_messages
+ # (already sliced), so we can find assistants outside the current slice.
+ # Calculate where reduced_recent starts in messages_dict
+ reduced_start_in_dict = slice_start + max(
0, len(recent_messages) - keep_count
)
reduced_recent = _ensure_tool_pairs_intact(
- reduced_recent, recent_messages, reduced_slice_start
+ reduced_recent, messages_dict, reduced_start_in_dict
)
if has_system_prompt:
messages = [