File size: 1,319 Bytes
5a043a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Headroom semantic compression wrapper.

Compresses massive context payloads locally before sending them to a cloud
LLM, reducing token counts and API costs.
"""

__all__ = ["HeadroomCompressor"]


class HeadroomCompressor:
    """Wraps the ``headroom-ai`` library with a graceful fallback."""

    def __init__(self, target_ratio: float = 0.8):
        self.target_ratio = target_ratio
        self._compress_fn = None

        try:
            from headroom import compress
            self._compress_fn = compress
        except ImportError:
            print(
                "Warning: headroom-ai not installed. "
                "Using fallback semantic compression simulator."
            )

    def compress(self, context: str) -> str:
        """Compress *context* using Headroom's algorithms.

        Falls back to a head + tail truncation when the library is absent.
        """
        if self._compress_fn is not None:
            return self._compress_fn(context)

        # Fallback simulator: keep the first 1000 chars and the last 2000
        # chars (where needles usually are).
        if len(context) < 3000:
            return context

        return (
            context[:1000]
            + "\n\n...[CONTENT COMPRESSED BY HEADROOM SIMULATOR]...\n\n"
            + context[-2000:]
        )