Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Merge upstream/main into fix/copilot-oauth-runtime

JerrettDavis3f30474fb726
diff --cc tests/test_proxy_copilot_auth_hooks.py
index 99a98dabd,f0191a7e3..b16f22211
--- a/tests/test_proxy_copilot_auth_hooks.py
+++ b/tests/test_proxy_copilot_auth_hooks.py
@@@ -10,33 -9,12 +10,33 @@@ from types import SimpleNamespac
import pytest
ROOT = Path(__file__).resolve().parents[1]
+_ISOLATED_MODULE_NAMES = (
+ "headroom.proxy",
+ "headroom.proxy.handlers",
+ "httpx",
+ "fastapi.responses",
+ "tests.headroom_proxy_handlers_openai",
+ "tests.headroom_proxy_handlers_streaming",
+)
+
+
[email protected](autouse=True)
+def restore_isolated_modules() -> None:
+ saved_modules = {name: sys.modules.get(name) for name in _ISOLATED_MODULE_NAMES}
+ try:
+ yield
+ finally:
+ for name in _ISOLATED_MODULE_NAMES:
+ sys.modules.pop(name, None)
+ for name, module in saved_modules.items():
+ if module is not None:
+ sys.modules[name] = module
- def _load_handler_module(module_name: str, relative_path: str):
+ def _load_handler_module(monkeypatch: pytest.MonkeyPatch, module_name: str, relative_path: str):
proxy_pkg = types.ModuleType("headroom.proxy")
proxy_pkg.__path__ = [str(ROOT / "headroom" / "proxy")]
- sys.modules["headroom.proxy"] = proxy_pkg
+ monkeypatch.setitem(sys.modules, "headroom.proxy", proxy_pkg)
handlers_pkg = types.ModuleType("headroom.proxy.handlers")
handlers_pkg.__path__ = [str(ROOT / "headroom" / "proxy" / "handlers")]
@@@ -60,13 -38,9 +60,13 @@@
class StreamingResponse(Response):
pass
+ class JSONResponse(Response):
+ pass
+
responses_mod.Response = Response
responses_mod.StreamingResponse = StreamingResponse
+ responses_mod.JSONResponse = JSONResponse
- sys.modules["fastapi.responses"] = responses_mod
+ monkeypatch.setitem(sys.modules, "fastapi.responses", responses_mod)
spec = importlib.util.spec_from_file_location(module_name, ROOT / relative_path)
assert spec is not None and spec.loader is not None
@@@ -76,8 -50,10 +76,9 @@@
return module
-async def test_openai_passthrough_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
+def test_openai_passthrough_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
openai_mod = _load_handler_module(
+ monkeypatch,
"tests.headroom_proxy_handlers_openai",
"headroom/proxy/handlers/openai.py",
)
@@@ -134,8 -108,10 +135,9 @@@
assert response.status_code == 200
-async def test_streaming_response_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
+def test_streaming_response_applies_copilot_auth(monkeypatch: pytest.MonkeyPatch) -> None:
streaming_mod = _load_handler_module(
+ monkeypatch,
"tests.headroom_proxy_handlers_streaming",
"headroom/proxy/handlers/streaming.py",
)