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(openclaw): preserve upstream response paths

JerrettDavis8134939efb20
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 555049138..5574334a1 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -1747,6 +1747,16 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
"""OpenAI Responses API (new API introduced March 2025)."""
return await proxy.handle_openai_responses(request)
+ @app.post("/backend-api/responses")
+ async def openai_codex_responses(request: Request):
+ """OpenAI Codex Responses API path preserved from ChatGPT backend."""
+ return await proxy.handle_openai_responses(request)
+
+ @app.post("/backend-api/codex/responses")
+ async def openai_codex_nested_responses(request: Request):
+ """OpenAI Codex Responses API path for codex-shaped proxy base URLs."""
+ return await proxy.handle_openai_responses(request)
+
@app.websocket("/v1/responses")
async def openai_responses_ws(websocket: WebSocket):
"""OpenAI Responses API via WebSocket (Codex gpt-5.4+)."""
@@ -1793,6 +1803,26 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
logger.error(f"Passthrough /v1/responses/{sub_path} failed: {e}")
return Response(content=str(e), status_code=502)
+ @app.websocket("/backend-api/responses")
+ async def openai_codex_responses_ws(websocket: WebSocket):
+ """OpenAI Codex Responses WebSocket path preserved from ChatGPT backend."""
+ await proxy.handle_openai_responses_ws(websocket)
+
+ @app.websocket("/backend-api/codex/responses")
+ async def openai_codex_nested_responses_ws(websocket: WebSocket):
+ """OpenAI Codex Responses WebSocket path for codex-shaped proxy base URLs."""
+ await proxy.handle_openai_responses_ws(websocket)
+
+ @app.api_route("/backend-api/responses/{sub_path:path}", methods=["GET", "POST", "DELETE"])
+ async def openai_codex_responses_sub(request: Request, sub_path: str):
+ """Passthrough for /backend-api/responses/* sub-endpoints."""
+ return await openai_responses_sub(request, sub_path)
+
+ @app.api_route("/backend-api/codex/responses/{sub_path:path}", methods=["GET", "POST", "DELETE"])
+ async def openai_codex_nested_responses_sub(request: Request, sub_path: str):
+ """Passthrough for /backend-api/codex/responses/* sub-endpoints."""
+ return await openai_responses_sub(request, sub_path)
+
# OpenAI Batch API endpoints (with compression!)
@app.post("/v1/batches")
async def create_batch(request: Request):
diff --git a/plugins/openclaw/src/gateway-config.ts b/plugins/openclaw/src/gateway-config.ts
index 0e0b4d3f1..bd3088a4a 100644
--- a/plugins/openclaw/src/gateway-config.ts
+++ b/plugins/openclaw/src/gateway-config.ts
@@ -2,6 +2,10 @@
export const DEFAULT_GATEWAY_PROVIDER_IDS = ["openai-codex"] as const;
+const DEFAULT_PROVIDER_BASE_URLS: Readonly<Record<string, string>> = {
+ "openai-codex": "https://chatgpt.com/backend-api",
+};
+
export function resolveGatewayProviderIds(config: Record<string, unknown> | undefined): string[] {
const configuredProviderIds = normalizeGatewayProviderIds(config?.gatewayProviderIds);
if (configuredProviderIds.length > 0) {
@@ -70,21 +74,51 @@ export function applyGatewayProviderBaseUrlsInPlace(
? currentValue
: {};
const nextConfig = { ...currentConfig };
+ const nextBaseUrl = routeBaseUrlThroughProxy({
+ providerId,
+ proxyUrl,
+ currentBaseUrl:
+ typeof nextConfig.baseUrl === "string" && nextConfig.baseUrl.trim().length > 0
+ ? nextConfig.baseUrl
+ : undefined,
+ });
if (!Array.isArray(nextConfig.models)) {
nextConfig.models = [];
changed = true;
}
- if (nextConfig.baseUrl === proxyUrl) {
+ if (nextConfig.baseUrl === nextBaseUrl) {
providers[providerId] = nextConfig;
continue;
}
- nextConfig.baseUrl = proxyUrl;
+ nextConfig.baseUrl = nextBaseUrl;
providers[providerId] = nextConfig;
changed = true;
}
return changed;
}
+
+function routeBaseUrlThroughProxy(params: {
+ providerId: string;
+ proxyUrl: string;
+ currentBaseUrl?: string;
+}): string {
+ const upstreamBaseUrl = params.currentBaseUrl ?? DEFAULT_PROVIDER_BASE_URLS[params.providerId];
+ if (!upstreamBaseUrl) {
+ return params.proxyUrl;
+ }
+
+ try {
+ const proxy = new URL(params.proxyUrl);
+ const upstream = new URL(upstreamBaseUrl);
+ proxy.pathname = upstream.pathname;
+ proxy.search = upstream.search;
+ proxy.hash = "";
+ return proxy.toString().replace(/\/$/, "");
+ } catch {
+ return params.proxyUrl;
+ }
+}
diff --git a/plugins/openclaw/test/gateway-config.test.ts b/plugins/openclaw/test/gateway-config.test.ts
index d5abdf38a..72c02eecc 100644
--- a/plugins/openclaw/test/gateway-config.test.ts
+++ b/plugins/openclaw/test/gateway-config.test.ts
@@ -37,7 +37,7 @@ describe("applyGatewayProviderBaseUrls", () => {
expect(result.changed).toBe(true);
expect((result.config as any).models.providers["openai-codex"]).toEqual({
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
});
});
@@ -73,6 +73,7 @@ describe("applyGatewayProviderBaseUrls", () => {
providers: {
"openai-codex": {
api: "openai-codex-responses",
+ baseUrl: "https://chatgpt.com/backend-api",
},
},
},
@@ -84,7 +85,7 @@ describe("applyGatewayProviderBaseUrls", () => {
expect(result.changed).toBe(true);
expect((result.config as any).models.providers["openai-codex"]).toEqual({
api: "openai-codex-responses",
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
});
});
@@ -94,7 +95,7 @@ describe("applyGatewayProviderBaseUrls", () => {
models: {
providers: {
"openai-codex": {
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
},
},
@@ -106,6 +107,28 @@ describe("applyGatewayProviderBaseUrls", () => {
expect(result.changed).toBe(false);
expect(result.config).toEqual(cfg);
});
+
+ it("preserves upstream path segments when routing through the proxy", () => {
+ const result = applyGatewayProviderBaseUrls(
+ {
+ models: {
+ providers: {
+ anthropic: {
+ baseUrl: "https://api.anthropic.com/v1",
+ },
+ },
+ },
+ },
+ "http://127.0.0.1:8787",
+ ["anthropic"],
+ );
+
+ expect(result.changed).toBe(true);
+ expect((result.config as any).models.providers.anthropic).toEqual({
+ baseUrl: "http://127.0.0.1:8787/v1",
+ models: [],
+ });
+ });
});
describe("applyGatewayProviderBaseUrlsInPlace", () => {
@@ -120,7 +143,7 @@ describe("applyGatewayProviderBaseUrlsInPlace", () => {
expect(changed).toBe(true);
expect(cfg.models.providers["openai-codex"]).toEqual({
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
});
});
@@ -131,6 +154,7 @@ describe("applyGatewayProviderBaseUrlsInPlace", () => {
providers: {
"openai-codex": {
api: "openai-codex-responses",
+ baseUrl: "https://chatgpt.com/backend-api",
envKey: "OPENAI_API_KEY",
models: ["gpt-5.3-codex"],
},
@@ -148,7 +172,7 @@ describe("applyGatewayProviderBaseUrlsInPlace", () => {
expect(cfg.models.providers["openai-codex"]).toEqual({
api: "openai-codex-responses",
envKey: "OPENAI_API_KEY",
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: ["gpt-5.3-codex"],
});
});
diff --git a/plugins/openclaw/test/plugin-runtime-routing.test.ts b/plugins/openclaw/test/plugin-runtime-routing.test.ts
index 6df99bd9e..8d297b9cc 100644
--- a/plugins/openclaw/test/plugin-runtime-routing.test.ts
+++ b/plugins/openclaw/test/plugin-runtime-routing.test.ts
@@ -95,7 +95,7 @@ describe("headroomPlugin runtime routing", () => {
await proxyReadyListeners[0]?.("http://127.0.0.1:8787");
expect(api.config.models.providers["openai-codex"]).toEqual({
- baseUrl: "http://127.0.0.1:8787",
+ baseUrl: "http://127.0.0.1:8787/backend-api",
models: [],
});
expect(api.config.models.providers.anthropic).toEqual({
diff --git a/tests/test_proxy_codex_route_aliases.py b/tests/test_proxy_codex_route_aliases.py
new file mode 100644
index 000000000..cda428fc6
--- /dev/null
+++ b/tests/test_proxy_codex_route_aliases.py
@@ -0,0 +1,17 @@
+from fastapi.responses import JSONResponse
+from fastapi.testclient import TestClient
+
+from headroom.proxy.server import HeadroomProxy, ProxyConfig, create_app
+
+
+def test_backend_api_responses_alias_delegates_to_openai_handler(monkeypatch):
+ async def fake_handle(self, request): # type: ignore[no-untyped-def]
+ return JSONResponse({"ok": True, "path": request.url.path})
+
+ monkeypatch.setattr(HeadroomProxy, "handle_openai_responses", fake_handle)
+
+ with TestClient(create_app(ProxyConfig())) as client:
+ response = client.post("/backend-api/responses", json={"model": "gpt-5.3-codex"})
+
+ assert response.status_code == 200
+ assert response.json() == {"ok": True, "path": "/backend-api/responses"}