mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Description The OpenCode transport plugin set `HEADROOM_PROJECT` as a shell env var for child processes but never forwarded it as `x-headroom-project` on the actual proxied HTTP requests. The proxy's `classify_project` only attributes traffic via `x-headroom-project` header or `/p/<name>` URL prefix — without the header, every OpenCode request was unattributed and the Per-Project Savings dashboard showed `0 project(s)` permanently. Fixes #2847. ## Root cause `installHeadroomTransport` was called with only `{ proxyUrl, debug }`. The `project` value was computed and used only in the `shell.env` hook (for subprocess env injection), never threaded through to `mergeFetchHeaders` or `headersForNodeRequest`. ## Changes Made 1. Add `project?: string` to `InstallOptions` and `TransportState`. 2. Resolve the project value once at plugin init (`pluginOptions.project → input.project.id → input.directory`) and pass it to `installHeadroomTransport`. 3. Both header-building seams now set `x-headroom-project` when a project is present: - `mergeFetchHeaders` (wrapped `fetch` path) - `headersForNodeRequest` (wrapped `http.request` / `https.request` path) 4. Reuse the resolved `project` in the `shell.env` hook (removes the duplicate resolution that was there before). ## Changes - `plugins/opencode/src/transport.ts` — `InstallOptions.project`, `TransportState.project`; `mergeFetchHeaders`, `headersForNodeRequest`, `routedNodeOptions`, `withRoutedFetchInput`, `installHeadroomTransport` updated - `plugins/opencode/src/plugin.ts` — resolve `project` once, pass it to transport; reuse in `shell.env` - `plugins/opencode/src/transport.test.ts` — 3 new tests: project header on fetch, project header on https.request, no header when project unset - `headroom/providers/opencode/_dist/entry.opencode.js` — rebuilt with `npm run build:standalone` to match source ## Testing - [x] Unit tests pass - [x] TypeScript typecheck passes - [x] New regression tests added ### Test Output ``` cd plugins/opencode && npm test # 17 passed (14 existing + 3 new) ``` TypeScript build also passes: `npm run typecheck` (no errors). ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature - [ ] Breaking change - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring ## Real Behavior Proof - Environment: OpenCode transport plugin test environment on the current PR head. - Exact command / steps: ran the plugin test suite and TypeScript typecheck after rebuilding the standalone bundle. - Observed result: all 17 tests passed, including project-header coverage for fetch and Node HTTPS paths plus the unset-project control; typechecking passed. - Not tested: a live OpenCode session against a deployed Headroom proxy. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review --------- Signed-off-by: Radhakrishnan P <gingeekrishna@gmail.com> Signed-off-by: Radhakrishnan Pachyappan <gingeekrishna@gmail.com> |
||
|---|---|---|
| .. | ||
| hook-shim | ||
| src | ||
| .gitignore | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
| tsup.config.ts | ||
| tsup.standalone.config.ts | ||
| vitest.config.ts | ||
headroom-opencode
OpenCode integration helpers for Headroom. The package supports two integration paths:
- Provider config helpers used by
headroom wrap opencodeand persistent installs. - A native OpenCode plugin that installs Headroom transport interception and exposes the retrieve tool.
Install
npm install headroom-opencode
Provider Config Helpers
Use these helpers when you need to generate OpenCode config that routes a headroom provider through a running Headroom proxy.
import {
buildOpencodeConfigContent,
createHeadroomProvider,
} from "headroom-opencode";
const provider = createHeadroomProvider({ proxyPort: 8787 });
const config = buildOpencodeConfigContent({
proxyPort: 8787,
defaultModel: "claude-sonnet-4-6",
});
console.log(provider.provider.headroom.npm);
console.log(config.model);
The generated provider uses @ai-sdk/openai-compatible and points model requests at http://127.0.0.1:<port>/v1.
Native OpenCode Plugin
Use HeadroomPlugin when OpenCode should intercept provider traffic in-process and expose Headroom tooling from a plugin.
import { HeadroomPlugin } from "headroom-opencode";
export default async function plugin(input) {
return HeadroomPlugin(input, {
proxyUrl: process.env.HEADROOM_PROXY_URL ?? "http://127.0.0.1:8787",
});
}
HeadroomPlugin:
- installs Headroom transport interception for OpenCode provider traffic.
- exposes the
headroom_retrievetool. - publishes
HEADROOM_PROXY_URLin the plugin output env. - defaults to
http://127.0.0.1:8787when no proxy URL is supplied.
Retrieve Tool
import { createHeadroomRetrieveTool } from "headroom-opencode";
const retrieve = createHeadroomRetrieveTool({
proxyBaseUrl: "http://127.0.0.1:8787",
});
const result = await retrieve.execute({
hash: "0123456789abcdef01234567",
});
The tool calls /v1/retrieve/<hash> on the Headroom proxy.
Compression Helper
import { compressWithHeadroom } from "headroom-opencode";
const result = await compressWithHeadroom(
[{ role: "user", content: "Summarize this file" }],
{ model: "gpt-4o", proxyUrl: "http://127.0.0.1:8787" },
);
console.log(`Saved ${result.tokensSaved} tokens`);
Models
| Model | Context | Output |
|---|---|---|
claude-sonnet-4-6 |
200K | 16K |
claude-opus-4-6 |
200K | 16K |
claude-haiku-4-5-20251001 |
200K | 8K |
gpt-4o |
128K | 16K |
gpt-4.1 |
1M | 32K |
The provider config exposes these as headroom/<model> and defaults to headroom/claude-sonnet-4-6.
Environment
| Variable | Used by | Description |
|---|---|---|
HEADROOM_PROXY_URL |
Native plugin | Proxy URL used by HeadroomPlugin |
OPENCODE_CONFIG_CONTENT |
OpenCode wrapper | Generated OpenCode provider, model, and MCP config |
License
Apache-2.0