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
fix(opencode): only route POST requests to LLM endpoint paths (#4464)
CyberSprout336d46225370
diff --git a/headroom/providers/opencode/_dist/index.js b/headroom/providers/opencode/_dist/index.jsindex 8c4a8b081..cc45f1d65 100644--- a/headroom/providers/opencode/_dist/index.js+++ b/headroom/providers/opencode/_dist/index.js@@ -19955,8 +19955,11 @@ function isRoutableUpstream(url2, proxy, excludeHosts) {}return true;}-function shouldRoute(url2, proxy, excludeHosts) {- return isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);+function isLlmMethod(method) {+ return typeof method === "string" && method.toUpperCase() === "POST";+}+function shouldRoute(url2, proxy, excludeHosts, method) {+ return isLlmMethod(method) && isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);}function modelBaseRoutesThroughProxy(baseUrl, proxyUrl, excludeHosts = []) {try {@@ -20021,7 +20024,8 @@ function mergeFetchHeaders(input2, init, upstream, originalPath = void 0, projec}function withRoutedFetchInput(input2, init, proxy, project, excludeHosts, sessionToken) {const upstream = requestUrl(input2);- if (!shouldRoute(upstream, proxy, excludeHosts)) {+ const method = init?.method ?? (input2 instanceof Request ? input2.method : "GET");+ if (!shouldRoute(upstream, proxy, excludeHosts, method)) {return [input2, init];}const { url: nextUrl, originalPath } = routedUrlForOpenCode(upstream, proxy);@@ -20093,7 +20097,7 @@ function headersForNodeRequest(options, upstream, originalPath, project, sessionreturn result;}function routedNodeOptions(parts, proxy, project, excludeHosts, sessionToken) {- if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {+ if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {return void 0;}const { url: nextUrl, originalPath } = routedUrlForOpenCode(parts.url, proxy);diff --git a/headroom/providers/opencode/hook-shim/handler.js b/headroom/providers/opencode/hook-shim/handler.jsindex 6b19a418b..7ce4c8922 100644--- a/headroom/providers/opencode/hook-shim/handler.js+++ b/headroom/providers/opencode/hook-shim/handler.js@@ -170,8 +170,11 @@ function isRoutableUpstream(url, proxy, excludeHosts) {}return true;}-function shouldRoute(url, proxy, excludeHosts) {- return isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);+function isLlmMethod(method) {+ return typeof method === "string" && method.toUpperCase() === "POST";+}+function shouldRoute(url, proxy, excludeHosts, method) {+ return isLlmMethod(method) && isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);}function routedUrl(upstream, proxy) {return new URL(`${upstream.pathname}${upstream.search}`, proxy.origin);@@ -229,7 +232,8 @@ function mergeFetchHeaders(input, init, upstream, originalPath = void 0, project}function withRoutedFetchInput(input, init, proxy, project, excludeHosts, sessionToken) {const upstream = requestUrl(input);- if (!shouldRoute(upstream, proxy, excludeHosts)) {+ const method = init?.method ?? (input instanceof Request ? input.method : "GET");+ if (!shouldRoute(upstream, proxy, excludeHosts, method)) {return [input, init];}const { url: nextUrl, originalPath } = routedUrlForOpenCode(upstream, proxy);@@ -301,7 +305,7 @@ function headersForNodeRequest(options, upstream, originalPath, project, sessionreturn result;}function routedNodeOptions(parts, proxy, project, excludeHosts, sessionToken) {- if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {+ if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {return void 0;}const { url: nextUrl, originalPath } = routedUrlForOpenCode(parts.url, proxy);diff --git a/plugins/opencode/src/transport.test.ts b/plugins/opencode/src/transport.test.tsindex 917407789..d99dff68e 100644--- a/plugins/opencode/src/transport.test.ts+++ b/plugins/opencode/src/transport.test.ts@@ -233,6 +233,40 @@ describe("Headroom OpenCode transport", () => {globalThis.fetch = originalFetch;});+ it("passes non-POST fetches to /messages and /responses suffix paths through unchanged (#4417)", async () => {+ const originalFetch = globalThis.fetch;+ const fetchMock = vi.fn(async (..._args: FetchCall) => new Response("ok"));+ globalThis.fetch = fetchMock as unknown as typeof fetch;++ installHeadroomTransport({ proxyUrl: "http://127.0.0.1:8787/v1" });++ await fetch("https://chat.example.com/api/v2/messages");+ await fetch("https://forms.example.com/forms/123/responses", { method: "GET" });+ await fetch(new Request("https://chat.example.com/api/v2/messages", { method: "DELETE" }));+ await fetch("https://api.anthropic.com/v1/messages", { method: "post" });++ expect(fetchMock).toHaveBeenCalledTimes(4);+ expect(fetchMock.mock.calls[0][0]).toBe("https://chat.example.com/api/v2/messages");+ expect(fetchMock.mock.calls[1][0]).toBe("https://forms.example.com/forms/123/responses");+ expect(new Headers(fetchMock.mock.calls[1][1]?.headers).get("x-headroom-original-path")).toBeNull();+ expect((fetchMock.mock.calls[2][0] as Request).url).toBe("https://chat.example.com/api/v2/messages");+ expect(fetchMock.mock.calls[3][0]).toEqual(new URL("http://127.0.0.1:8787/v1/messages"));++ globalThis.fetch = originalFetch;+ });++ it("leaves non-POST Node https.request calls to LLM-suffix paths alone (#4417)", () => {+ installHeadroomTransport({ proxyUrl: "http://127.0.0.1:8787/v1" });+ const req = https.request("https://chat.example.com/api/v2/messages", { method: "GET" });+ req.on("error", () => {});+ expect(req.host).toBe("chat.example.com");+ req.destroy();+ const req2 = https.request("https://forms.example.com/forms/123/responses");+ req2.on("error", () => {});+ expect(req2.host).toBe("forms.example.com");+ req2.destroy();+ });+it("routes Gemini native :generateContent and :streamGenerateContent through the proxy", async () => {const originalFetch = globalThis.fetch;const fetchMock = vi.fn(async (..._args: FetchCall) => new Response("ok"));diff --git a/plugins/opencode/src/transport.ts b/plugins/opencode/src/transport.tsindex 4fcc35829..d48b1fc82 100644--- a/plugins/opencode/src/transport.ts+++ b/plugins/opencode/src/transport.ts@@ -280,8 +280,16 @@ function isRoutableUpstream(url: URL, proxy: URL, excludeHosts: string[]): boolereturn true;}-function shouldRoute(url: URL, proxy: URL, excludeHosts: string[]): boolean {- return isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);+// Every LLM endpoint matched above is a POST. A GET/DELETE/... to a path that+// merely ends in /messages or /responses (chat APIs, form responses) is not an+// LLM call and must reach its original URL (#4417). A missing method means+// GET, matching fetch and node:http defaults.+function isLlmMethod(method: unknown): boolean {+ return typeof method === "string" && method.toUpperCase() === "POST";+}++function shouldRoute(url: URL, proxy: URL, excludeHosts: string[], method: unknown): boolean {+ return isLlmMethod(method) && isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);}// OpenCode 2.x hands the plugin model base URLs (e.g.@@ -377,7 +385,8 @@ function withRoutedFetchInput(sessionToken: string | undefined,): FetchArgs {const upstream = requestUrl(input);- if (!shouldRoute(upstream, proxy, excludeHosts)) {+ const method = init?.method ?? (input instanceof Request ? input.method : "GET");+ if (!shouldRoute(upstream, proxy, excludeHosts, method)) {return [input, init];}@@ -471,7 +480,7 @@ function routedNodeOptions(excludeHosts: string[],sessionToken: string | undefined,): Record<string, unknown> | undefined {- if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {+ if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {return undefined;}