headroom-opencode 0.42.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,6 +11,25 @@ OpenCode integration helpers for Headroom. The package supports two integration
11
11
  npm install headroom-opencode
12
12
  ```
13
13
 
14
+ ## Building from source
15
+
16
+ Tsup 9 requires Node.js `^22.18.0 || ^24.11.0 || >=26.0.0`. The current locked
17
+ graph also includes ini 7, which requires `^22.22.2 || ^24.15.0 || >=26.0.0`.
18
+ These are development-install requirements; the published plugin and its host
19
+ retain their own runtime requirements. Node.js 24.22.2 is a verified choice
20
+ for this dependency graph. From `plugins/opencode`, run:
21
+
22
+ ```bash
23
+ npm ci
24
+ npm test
25
+ npm run typecheck
26
+ npm run build
27
+ npm run build:standalone
28
+ ```
29
+
30
+ For a reproducible real OpenCode 2.x → proxy → local SSE upstream check, see
31
+ [the transport runtime proof](RUNTIME_PROOF.md). It records the tested scope and limitations.
32
+
14
33
  ## Provider Config Helpers
15
34
 
16
35
  Use these helpers when you need to generate OpenCode config that routes a `headroom` provider through a running Headroom proxy.
@@ -57,7 +76,7 @@ The default export is `{ id: "headroom", server, setup }`. OpenCode 1.x calls `s
57
76
 
58
77
  On 2.x, setup returns a cleanup that releases the transport, so hot reloads do not stack interceptors.
59
78
 
60
- OpenCode 2.x keeps its own reference to `fetch` from before plugins load, so patching `fetch` alone does not catch its model traffic. On 2.x the plugin also rewrites each remote model that uses the OpenAI chat-completions or responses format so that it points at the proxy. Headers name the real upstream, as the transport does. Models on other formats (Anthropic messages, Google) are left alone. `headroom wrap opencode` routes native Anthropic and OpenAI through the proxy separately, via their `baseURL`.
79
+ OpenCode 2.x keeps its own reference to `fetch` from before plugins load, so patching `fetch` alone does not catch its model traffic. For models with a resolved `baseURL`, the plugin also rewrites OpenAI chat-completions, Responses, and Anthropic Messages traffic so that it points at the proxy — including GitHub Copilot's Claude models. Anthropic Messages base URLs with a path prefix (for example a gateway rooted at `/api/...`) stay on their own base URL, because the proxy's Messages route ignores the original-path header and cannot preserve that prefix. Headers name the real upstream and request path, as the transport does, and authentication settings are retained. Local, excluded, and unsupported-protocol models are left alone. `headroom wrap opencode` routes native Anthropic and OpenAI through the proxy separately, via their `baseURL`.
61
80
 
62
81
  To keep a provider off the proxy, list its host. An entry matches the host and all of its subdomains, and `.example.com` / `*.example.com` are accepted as spellings of `example.com`:
63
82
 
@@ -244,8 +244,11 @@ function isRoutableUpstream(url2, proxy, excludeHosts) {
244
244
  }
245
245
  return true;
246
246
  }
247
- function shouldRoute(url2, proxy, excludeHosts) {
248
- return isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);
247
+ function isLlmMethod(method) {
248
+ return typeof method === "string" && method.toUpperCase() === "POST";
249
+ }
250
+ function shouldRoute(url2, proxy, excludeHosts, method) {
251
+ return isLlmMethod(method) && isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);
249
252
  }
250
253
  function modelBaseRoutesThroughProxy(baseUrl, proxyUrl, excludeHosts = []) {
251
254
  try {
@@ -310,7 +313,8 @@ function mergeFetchHeaders(input2, init, upstream, originalPath = void 0, projec
310
313
  }
311
314
  function withRoutedFetchInput(input2, init, proxy, project, excludeHosts, sessionToken) {
312
315
  const upstream = requestUrl(input2);
313
- if (!shouldRoute(upstream, proxy, excludeHosts)) {
316
+ const method = init?.method ?? (input2 instanceof Request ? input2.method : "GET");
317
+ if (!shouldRoute(upstream, proxy, excludeHosts, method)) {
314
318
  return [input2, init];
315
319
  }
316
320
  const { url: nextUrl, originalPath } = routedUrlForOpenCode(upstream, proxy);
@@ -382,7 +386,7 @@ function headersForNodeRequest(options, upstream, originalPath, project, session
382
386
  return result;
383
387
  }
384
388
  function routedNodeOptions(parts, proxy, project, excludeHosts, sessionToken) {
385
- if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {
389
+ if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {
386
390
  return void 0;
387
391
  }
388
392
  const { url: nextUrl, originalPath } = routedUrlForOpenCode(parts.url, proxy);
@@ -20267,23 +20271,35 @@ function headroomShellEnv(proxyUrl, project, options) {
20267
20271
  ...options.backend ? { HEADROOM_BACKEND: options.backend } : {}
20268
20272
  };
20269
20273
  }
20270
- function openAiWireSuffix(pkg) {
20274
+ function modelWireSuffix(pkg) {
20271
20275
  if (!pkg?.startsWith("@opencode/ai/providers/")) return void 0;
20272
20276
  if (pkg === "@opencode/ai/providers/openai-compatible" || pkg.endsWith("/chat")) {
20273
20277
  return "/chat/completions";
20274
20278
  }
20275
20279
  if (pkg.endsWith("-responses") || pkg.endsWith("/responses")) return "/responses";
20280
+ if (pkg === "@opencode/ai/providers/anthropic" || pkg === "@opencode/ai/providers/anthropic-compatible" || pkg.endsWith("/messages")) {
20281
+ return "/messages";
20282
+ }
20276
20283
  return void 0;
20277
20284
  }
20285
+ function anthropicBaseKeepsPath(baseURL) {
20286
+ try {
20287
+ const pathname = trimTrailingSlashes(new URL(baseURL).pathname);
20288
+ return pathname === "" || pathname === "/v1";
20289
+ } catch {
20290
+ return false;
20291
+ }
20292
+ }
20278
20293
  function routeModelsThroughProxy(models, proxyUrl, project, excludeHosts, sessionToken) {
20279
20294
  for (const model of models.list()) {
20280
20295
  const providerID = String(model.providerID);
20281
20296
  const modelID = String(model.id);
20282
20297
  const provider = models.provider.get(providerID)?.provider;
20283
- const suffix = openAiWireSuffix(model.package ?? provider?.package);
20298
+ const suffix = modelWireSuffix(model.package ?? provider?.package);
20284
20299
  const baseURL = model.settings?.baseURL ?? provider?.settings?.baseURL;
20285
20300
  if (!suffix || typeof baseURL !== "string") continue;
20286
20301
  if (!modelBaseRoutesThroughProxy(baseURL, proxyUrl, excludeHosts)) continue;
20302
+ if (suffix === "/messages" && !anthropicBaseKeepsPath(baseURL)) continue;
20287
20303
  const upstream = new URL(baseURL);
20288
20304
  models.update(providerID, modelID, (draft) => {
20289
20305
  draft.settings = { ...draft.settings, baseURL: `${proxyUrl}/v1` };
@@ -20391,4 +20407,4 @@ export {
20391
20407
  HeadroomOpenCodePlugin,
20392
20408
  plugin_default
20393
20409
  };
20394
- //# sourceMappingURL=chunk-FYZXE4YW.js.map
20410
+ //# sourceMappingURL=chunk-PY2FZKSF.js.map