From 6501543814de3abb658ac98504b7b760bb766327 Mon Sep 17 00:00:00 2001 From: sharath <29162020+tnfssc@users.noreply.github.com> Date: Thu, 19 Jun 2025 18:05:45 +0000 Subject: [PATCH] feat(thread_manager): implement middle-out message compression to optimize message handling --- backend/agentpress/thread_manager.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/agentpress/thread_manager.py b/backend/agentpress/thread_manager.py index ef87d436..1d0d708e 100644 --- a/backend/agentpress/thread_manager.py +++ b/backend/agentpress/thread_manager.py @@ -250,7 +250,7 @@ class ThreadManager: logger.warning(f"Further token compression is needed: {compressed_token_count} > {max_tokens}") result = self._compress_messages(messages, llm_model, max_tokens, int(token_threshold / 2), max_iterations - 1) - return result + return self._middle_out_messages(result) def _compress_messages_by_omitting_messages( self, @@ -322,6 +322,17 @@ class ThreadManager: logger.info(f"_compress_messages_by_omitting_messages: {initial_token_count} -> {final_token_count} tokens ({len(messages)} -> {len(final_messages)} messages)") return final_messages + + def _middle_out_messages(self, messages: List[Dict[str, Any]], max_messages: int = 320) -> List[Dict[str, Any]]: + """Remove messages from the middle of the list, keeping max_messages total.""" + if len(messages) <= max_messages: + return messages + + # Keep half from the beginning and half from the end + keep_start = max_messages // 2 + keep_end = max_messages - keep_start + + return messages[:keep_start] + messages[-keep_end:] def add_tool(self, tool_class: Type[Tool], function_names: Optional[List[str]] = None, **kwargs):