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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
feat: add Copilot BYOK provider wrapper utilities and CLI support (#1041)
Umi_Mae67ee2af658b
diff --git a/headroom/cli/wrap.py b/headroom/cli/wrap.pyindex 5f85b0c4f..4860ff4b0 100644--- a/headroom/cli/wrap.py+++ b/headroom/cli/wrap.py@@ -63,6 +63,9 @@ from headroom.providers.copilot import (from headroom.providers.copilot import (detect_running_proxy_backend as _copilot_detect_running_proxy_backend,)+from headroom.providers.copilot import (+ is_auto_model as _is_auto_model,+)from headroom.providers.copilot import (model_configured as _copilot_model_configured_impl,)@@ -75,6 +78,9 @@ from headroom.providers.copilot import (from headroom.providers.copilot import (resolve_provider_type as _copilot_resolve_provider_type,)+from headroom.providers.copilot import (+ strip_auto_model_args as _strip_auto_model_args,+)from headroom.providers.copilot import (validate_configuration as _validate_copilot_configuration,)@@ -2294,9 +2300,9 @@ def _proc_identity(pid: int) -> tuple[str, float] | None:different units; we only compare like-for-like."""try:- import psutil # optional dependency; portable when present+ import psutil # type: ignore[import-untyped] # optional dependency; portable when present- return ("psutil", float(psutil.Process(pid).create_time()))+ return ("psutil", psutil.Process(pid).create_time())except Exception:pass# Linux fallback: field 22 of /proc/<pid>/stat is starttime in clock ticks@@ -3202,6 +3208,22 @@ def copilot()selected_model = _copilot_model_from_args(copilot_args, env)++ # ``--model auto`` is a Copilot-internal routing token that the BYOK+ # API rejects with ``400 The requested model is not supported``. In+ # subscription/OAuth mode we route to the real Copilot hosted API, so+ # Copilot's own native auto-selection works fine — we just need to+ # strip the ``--model auto`` flag before launch so Copilot doesn't+ # forward it to the provider endpoint.+ if _is_auto_model(selected_model):+ copilot_args = _strip_auto_model_args(copilot_args)+ selected_model = None+ click.echo(+ " Note: '--model auto' is not forwarded to the Copilot API "+ "(it would cause a 400). Removed it; Copilot will use its own "+ "automatic model selection."+ )+effective_wire_api = wire_api or (_copilot_default_wire_api_for_model(selected_model) if subscription else "completions")@@ -3270,10 +3292,26 @@ def copilot(raise SystemExit(1)if not subscription and not _copilot_model_configured(copilot_args, env):- click.echo(- " Note: Copilot BYOK requires a model. Pass `--model <name>` "- "or set `COPILOT_MODEL` / `COPILOT_PROVIDER_MODEL_ID`."- )+ # Distinguish between "--model auto" (wrong model for BYOK) and+ # genuinely missing model (no --model flag at all).+ raw_model = _copilot_model_from_args(copilot_args, env)+ if _is_auto_model(raw_model):+ click.echo(+ " Error: '--model auto' is not supported in Copilot BYOK mode.\n"+ " BYOK routes to an external provider (Anthropic/OpenAI) which\n"+ " does not recognise 'auto' as a model name — the request will\n"+ " fail with a 400 error.\n"+ " Options:\n"+ " • Use a concrete model: --model gpt-4o\n"+ " • Use subscription mode for native auto-routing:\n"+ " headroom wrap copilot --subscription -- --model auto"+ )+ raise SystemExit(1)+ else:+ click.echo(+ " Note: Copilot BYOK requires a model. Pass `--model <name>` "+ "or set `COPILOT_MODEL` / `COPILOT_PROVIDER_MODEL_ID`."+ )_launch_tool(binary=copilot_bin,diff --git a/headroom/providers/copilot/__init__.py b/headroom/providers/copilot/__init__.pyindex 4cc4a1f40..df53f3397 100644--- a/headroom/providers/copilot/__init__.py+++ b/headroom/providers/copilot/__init__.py@@ -5,11 +5,13 @@ from .wrap import (copilot_model_from_args,default_wire_api_for_model,detect_running_proxy_backend,+ is_auto_model,model_configured,model_prefers_responses_api,provider_key_source,query_proxy_config,resolve_provider_type,+ strip_auto_model_args,validate_configuration,)@@ -18,10 +20,12 @@ __all__ = ["copilot_model_from_args","default_wire_api_for_model","detect_running_proxy_backend",+ "is_auto_model","model_prefers_responses_api","model_configured","provider_key_source","query_proxy_config","resolve_provider_type",+ "strip_auto_model_args","validate_configuration",]diff --git a/headroom/providers/copilot/wrap.py b/headroom/providers/copilot/wrap.pyindex 672e50954..518b302ed 100644--- a/headroom/providers/copilot/wrap.py+++ b/headroom/providers/copilot/wrap.py@@ -67,6 +67,49 @@ def validate_configuration()+#: Copilot virtual model names that map to native auto-routing.+#: Forwarding these to BYOK endpoints causes a 400; they must be stripped.+_AUTO_MODEL_ALIASES: frozenset[str] = frozenset({"auto"})+++def is_auto_model(model: str | None) -> bool:+ """Return True when the model name is a Copilot auto-routing alias.++ ``model auto`` is a virtual model ID that Copilot resolves internally.+ It is **not** a valid model string for BYOK providers (Anthropic, OpenAI)+ and causes a ``400 The requested model is not supported`` error if forwarded+ verbatim. This helper centralises the detection so both the CLI and the+ proxy layer can guard against it.+ """+ if not model:+ return False+ return model.strip().lower() in _AUTO_MODEL_ALIASES+++def strip_auto_model_args(copilot_args: tuple[str, ...]) -> tuple[str, ...]:+ """Remove ``--model auto`` (and ``--model=auto``) from Copilot CLI args.++ Used in the subscription/OAuth path: when the user passes ``--model auto``+ to ``headroom wrap copilot --subscription``, we strip it before launching+ Copilot so the CLI falls back to its own native automatic model selection+ instead of sending the unsupported ``auto`` string to the BYOK API.+ """+ result: list[str] = []+ i = 0+ while i < len(copilot_args):+ arg = copilot_args[i]+ if arg == "--model" and i + 1 < len(copilot_args):+ if is_auto_model(copilot_args[i + 1]):+ i += 2 # skip both --model and auto+ continue+ elif arg.startswith("--model=") and is_auto_model(arg.split("=", 1)[1]):+ i += 1 # skip --model=auto+ continue+ result.append(arg)+ i += 1+ return tuple(result)++def _normalized_model_name(model: str | None) -> str:"""Return a lowercase model name without provider/path prefixes."""if not model:@@ -156,14 +199,15 @@ def build_launch_env(def model_configured(copilot_args: tuple[str, ...], env: Mapping[str, str]) -> bool:- """Return True when Copilot BYOK model selection is configured."""- if env.get("COPILOT_MODEL") or env.get("COPILOT_PROVIDER_MODEL_ID"):- return True-- for idx, arg in enumerate(copilot_args):- if arg == "--model" and idx + 1 < len(copilot_args):- return True- if arg.startswith("--model="):- return True+ """Return True when Copilot BYOK model selection is configured (non-auto).- return False+ ``--model auto`` is **not** considered configured for BYOK purposes: it is+ a virtual Copilot routing token that has no meaning to external providers+ such as Anthropic or OpenAI, and forwarding it causes a 400. Returning+ ``False`` here ensures the BYOK "model required" warning is still shown+ when the user mistakenly passes ``--model auto`` in BYOK mode.+ """+ model = copilot_model_from_args(copilot_args, env)+ if model is None or is_auto_model(model):+ return False+ return Truediff --git a/tests/test_cli/test_wrap_copilot.py b/tests/test_cli/test_wrap_copilot.pyindex abad9ace6..f42903e5e 100644--- a/tests/test_cli/test_wrap_copilot.py+++ b/tests/test_cli/test_wrap_copilot.py@@ -171,10 +171,41 @@ def test_wrap_copilot_openai_backend_sets_completions_env(f"http://127.0.0.1:8787{_expected_project_prefix()}/v1")assert env["COPILOT_PROVIDER_WIRE_API"] == "completions"- assert captured["backend"] == "anyllm"- assert captured["anyllm_provider"] == "groq"- assert captured["region"] == "us-central1"- assert captured["args"] == ("--model", "gpt-4o")+++def test_wrap_copilot_byok_rejects_auto_model_before_launch(+ runner: CliRunner,+ wrap_modules: tuple[types.ModuleType, click.Group],+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _wrap_cli, main = wrap_modules+ monkeypatch.setenv("OPENAI_API_KEY", "sk-test-dummy")++ def fail_launch_tool(**_kwargs: object) -> None:+ raise AssertionError("_launch_tool must not run with --model auto in BYOK mode")++ with (+ patch("headroom.cli.wrap.shutil.which", return_value="copilot"),+ patch("headroom.cli.wrap.has_oauth_auth", return_value=False),+ patch("headroom.cli.wrap._launch_tool", side_effect=fail_launch_tool),+ ):+ result = runner.invoke(+ main,+ [+ "wrap",+ "copilot",+ "--provider-type",+ "openai",+ "--no-context-tool",+ "--",+ "--model",+ "auto",+ ],+ )++ assert result.exit_code == 1+ assert "'--model auto' is not supported in Copilot BYOK mode" in result.output+ assert "Use a concrete model" in result.outputdef test_wrap_copilot_auto_detects_running_proxy_backend(diff --git a/tests/test_provider_copilot_wrap.py b/tests/test_provider_copilot_wrap.pyindex 3defacfa4..c3d082915 100644--- a/tests/test_provider_copilot_wrap.py+++ b/tests/test_provider_copilot_wrap.py@@ -13,11 +13,13 @@ from headroom.providers.copilot.wrap import (copilot_model_from_args,default_wire_api_for_model,detect_running_proxy_backend,+ is_auto_model,model_configured,model_prefers_responses_api,provider_key_source,query_proxy_config,resolve_provider_type,+ strip_auto_model_args,validate_configuration,)@@ -164,6 +166,70 @@ def test_model_configured_detects_env_and_cli_variants() -> None:assert model_configured(("--model", "gpt-4o"), {}) is Trueassert model_configured(("--model=gpt-4o",), {}) is Trueassert model_configured(("--other", "value"), {}) is False+ # ``auto`` is not a valid BYOK model — must be treated as unconfigured.+ assert model_configured(("--model", "auto"), {}) is False+ assert model_configured(("--model=auto",), {}) is False+ assert model_configured((), {"COPILOT_MODEL": "auto"}) is False+ assert model_configured((), {"COPILOT_PROVIDER_MODEL_ID": "auto"}) is False+++ ("model", "expected"),+ [+ ("auto", True),+ ("Auto", True),+ ("AUTO", True),+ (" auto ", True),+ ("gpt-4o", False),+ ("gpt-5", False),+ ("claude-sonnet-4.6", False),+ (None, False),+ ("", False),+ ],+)+def test_is_auto_model(model: str | None, expected: bool) -> None:+ assert is_auto_model(model) is expected+++ ("args", "expected"),+ [+ # Strips --model auto (space-separated)+ (+ ("--model", "auto", "--port", "8788"),+ ("--port", "8788"),+ ),+ # Strips --model=auto (equals form)+ (+ ("--model=auto", "-p", "hello"),+ ("-p", "hello"),+ ),+ # Case-insensitive stripping+ (+ ("--model", "AUTO", "--allow-all-tools"),+ ("--allow-all-tools",),+ ),+ # Leaves concrete models untouched+ (+ ("--model", "gpt-4o", "--port", "8788"),+ ("--model", "gpt-4o", "--port", "8788"),+ ),+ # Leaves --model=gpt-4o untouched+ (+ ("--model=gpt-4o",),+ ("--model=gpt-4o",),+ ),+ # Empty args unchanged+ ((), ()),+ # --model at end with no value (malformed) — leave as-is, don't crash+ (("--model",), ("--model",)),+ ],+)+def test_strip_auto_model_args(+ args: tuple[str, ...],+ expected: tuple[str, ...],+) -> None:+ assert strip_auto_model_args(args) == expecteddef test_build_launch_env_applies_project_path_prefix() -> None: