Files
AutoGPT/autogpt_platform/backend
Zamil Majdy 72ae2e9504 refactor(backend): unify context compression into compress_context()
Consolidates context window management into a single reusable function
in prompt.py that supports both truncation-only and summarization strategies.

Changes:
- Add CompressResult dataclass with compression metadata
- Add compress_context() async function with configurable strategies:
  1. Content truncation (always)
  2. LLM summarization (if client provided)
  3. Progressive message reduction
  4. Middle-out deletion (fallback)
  5. First/last trim (last resort)
- Add _ensure_tool_pairs_intact() with full orphan tool_call_id handling
- Add _summarize_messages_llm() for LLM-based summarization
- Migrate llm_call() to use compress_context(client=None) for truncation-only
- Remove compress_prompt() - replaced by compress_context()
- Refactor chat service to use unified compress_context()
- Fix: use async context manager for AsyncOpenAI client to prevent socket leaks
- Fix: only create OpenAI client when api_key is provided
- Fix: handle OpenAI-style tool messages (role=tool with string content) in truncation
- Fix: use client.with_options(timeout=...) for per-request timeout (OpenAI SDK v1.x)
- Add comprehensive tests for all new functions

The unified function allows:
- Chat service: client=openai_client for summarization
- LLM blocks: client=None for truncation-only
2026-02-03 12:13:04 +04:00
..