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.

fix(opencode): expose headroom/* models in injected provider config (#1716)

Parideboye8151f059b4a
diff --git a/headroom/providers/opencode/config.py b/headroom/providers/opencode/config.py
index 097022dcc..519ad5941 100644
--- a/headroom/providers/opencode/config.py
+++ b/headroom/providers/opencode/config.py
@@ -32,6 +32,44 @@ _MCP_BLOCK_RE = re.compile(
)
HEADROOM_OPENCODE_PLUGIN = "headroom-opencode"
+# Models exposed by the injected `headroom` provider. OpenCode only resolves
+# `headroom/<id>` for ids listed in the provider's `models` map, so an empty
+# map means every documented `headroom/*` model fails with "Model not found".
+# Keep in sync with DEFAULT_MODELS in plugins/opencode/src/provider.ts and the
+# table in plugins/opencode/README.md.
+HEADROOM_OPENCODE_MODELS: dict[str, Any] = {
+ "claude-sonnet-4-6": {
+ "name": "Claude Sonnet 4.6",
+ "limit": {"context": 200000, "output": 16384},
+ },
+ "claude-opus-4-6": {
+ "name": "Claude Opus 4.6",
+ "limit": {"context": 200000, "output": 16384},
+ },
+ "claude-haiku-4-5-20251001": {
+ "name": "Claude Haiku 4.5",
+ "limit": {"context": 200000, "output": 8192},
+ },
+ "gpt-4o": {
+ "name": "GPT-4o",
+ "limit": {"context": 128000, "output": 16384},
+ },
+ "gpt-4.1": {
+ "name": "GPT-4.1",
+ "limit": {"context": 1048576, "output": 32768},
+ },
+}
+
+
+def headroom_provider_entry(port: int) -> dict[str, Any]:
+ """Return the `headroom` provider block pointed at the local proxy."""
+ return {
+ "npm": "@ai-sdk/openai-compatible",
+ "name": "Headroom Proxy",
+ "options": {"baseURL": f"http://127.0.0.1:{port}/v1"},
+ "models": HEADROOM_OPENCODE_MODELS,
+ }
+
def _opencode_home_dir() -> Path:
"""Return the OpenCode home/config directory."""
@@ -83,13 +121,7 @@ def strip_opencode_headroom_blocks(content: str, *, remove_mcp: bool = True) ->
def _render_provider_block(port: int) -> str:
"""Render a Headroom provider block as a JSON comment-wrapped snippet."""
- provider = {
- "headroom": {
- "npm": "@ai-sdk/openai-compatible",
- "name": "Headroom Proxy",
- "options": {"baseURL": f"http://127.0.0.1:{port}/v1"},
- }
- }
+ provider = {"headroom": headroom_provider_entry(port)}
lines = [
_PROVIDER_MARKER_START,
f'"provider": {json.dumps(provider, indent=2)},',
@@ -203,13 +235,7 @@ def inject_opencode_provider_config(port: int) -> None:
data = _parse_json_loose(content)
# Merge provider into the JSON data structure.
- provider = {
- "headroom": {
- "npm": "@ai-sdk/openai-compatible",
- "name": "Headroom Proxy",
- "options": {"baseURL": f"http://127.0.0.1:{port}/v1"},
- }
- }
+ provider = {"headroom": headroom_provider_entry(port)}
data = _inject_key_into_json(data, "provider", provider)
# Write back as formatted JSON (opencode uses standard JSON with comments).
diff --git a/headroom/providers/opencode/runtime.py b/headroom/providers/opencode/runtime.py
index a4c478c45..d69df9ffb 100644
--- a/headroom/providers/opencode/runtime.py
+++ b/headroom/providers/opencode/runtime.py
@@ -9,7 +9,7 @@ from pathlib import Path
from headroom.mcp_registry.install import DEFAULT_PROXY_URL
-from .config import HEADROOM_OPENCODE_PLUGIN
+from .config import HEADROOM_OPENCODE_PLUGIN, headroom_provider_entry
def proxy_base_url(port: int) -> str:
@@ -74,11 +74,7 @@ def build_opencode_config_content(
"provider": {
"anthropic": {"options": {"baseURL": base_url}},
"openai": {"options": {"baseURL": base_url}},
- "headroom": {
- "npm": "@ai-sdk/openai-compatible",
- "name": "Headroom Proxy",
- "options": {"baseURL": base_url},
- },
+ "headroom": headroom_provider_entry(port),
}
}
if include_mcp:
diff --git a/plugins/opencode/src/provider.ts b/plugins/opencode/src/provider.ts
index 0dc50e37b..dd6fa87d4 100644
--- a/plugins/opencode/src/provider.ts
+++ b/plugins/opencode/src/provider.ts
@@ -64,12 +64,9 @@ export function createHeadroomProvider(
npm: "@ai-sdk/openai-compatible",
name: "Headroom Proxy",
options: { baseURL: `${baseUrl}/v1` },
- models: Object.fromEntries(
- Object.entries(models).map(([id, mapping]) => [
- `headroom/${id}`,
- mapping,
- ]),
- ),
+ // OpenCode namespaces model ids by provider key, so entries must be bare
+ // ids ("claude-sonnet-4-6"), referenced as "headroom/<id>".
+ models: { ...models },
};
}
diff --git a/tests/test_providers_opencode_config.py b/tests/test_providers_opencode_config.py
index ae988b464..ef1d142fe 100644
--- a/tests/test_providers_opencode_config.py
+++ b/tests/test_providers_opencode_config.py
@@ -164,6 +164,10 @@ def test_inject_provider_config_creates_file(
assert config_file.exists()
config = _parse_json_loose(config_file.read_text())
assert config["provider"]["headroom"]["npm"] == "@ai-sdk/openai-compatible"
+ # Bare model ids: OpenCode resolves them as "headroom/<id>" (#1657).
+ models = config["provider"]["headroom"]["models"]
+ assert "claude-sonnet-4-6" in models
+ assert all(not model_id.startswith("headroom/") for model_id in models)
assert "mcp" not in config
assert "model" not in config # headroom provider is a transparent pass-through
@@ -419,6 +423,11 @@ def test_build_opencode_config_content_without_mcp(
providers = config["provider"]
assert providers["anthropic"]["options"]["baseURL"] == "http://127.0.0.1:8787/v1"
assert providers["openai"]["options"]["baseURL"] == "http://127.0.0.1:8787/v1"
+ # The headroom provider exposes explicit models so "headroom/<id>" resolves (#1657).
+ assert providers["headroom"]["options"]["baseURL"] == "http://127.0.0.1:8787/v1"
+ models = providers["headroom"]["models"]
+ assert "claude-sonnet-4-6" in models
+ assert all(not model_id.startswith("headroom/") for model_id in models)
# The transport plugin is injected by absolute path (opencode loads it directly).
assert config["plugin"] == [str(plugin)]