Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat: update API bridge proxy timeout to 600000ms and enhance related tests

Jan Leonad2600b4d01a
diff --git a/src/lib/apiBridgeServer.ts b/src/lib/apiBridgeServer.ts
index d289d6f752..4109dfd6ac 100644
--- a/src/lib/apiBridgeServer.ts
+++ b/src/lib/apiBridgeServer.ts
@@ -22,7 +22,22 @@ function isOpenAiCompatiblePath(pathname: string): boolean {
return OPENAI_COMPAT_PATHS.some((pattern) => pattern.test(pathname));
}
+function requestWantsStreaming(req: IncomingMessage): boolean {
+ const accept = String(req.headers.accept || "").toLowerCase();
+ if (accept.includes("text/event-stream")) return true;
+
+ const pathname = (req.url || "/").split("?")[0] || "/";
+ return /^\/(?:v1\/)?(?:responses|chat\/completions)(?:\/|$)/.test(pathname);
+}
+
+function getProxyTimeoutMs(req: IncomingMessage): number {
+ if (!requestWantsStreaming(req)) return API_BRIDGE_TIMEOUTS.proxyTimeoutMs;
+
+ return Math.max(API_BRIDGE_TIMEOUTS.proxyTimeoutMs, API_BRIDGE_TIMEOUTS.serverRequestTimeoutMs);
+}
+
function proxyRequest(req: IncomingMessage, res: ServerResponse, dashboardPort: number): void {
+ const proxyTimeoutMs = getProxyTimeoutMs(req);
const targetReq = http.request(
{
hostname: "127.0.0.1",
@@ -33,9 +48,14 @@ function proxyRequest(req: IncomingMessage, res: ServerResponse, dashboardPort:
...req.headers,
host: `127.0.0.1:${dashboardPort}`,
},
- timeout: API_BRIDGE_TIMEOUTS.proxyTimeoutMs,
+ timeout: proxyTimeoutMs,
},
(targetRes) => {
+ const contentType = String(targetRes.headers["content-type"] || "").toLowerCase();
+ if (contentType.includes("text/event-stream")) {
+ targetReq.setTimeout(0);
+ }
+
res.writeHead(targetRes.statusCode || 502, targetRes.headers);
targetRes.pipe(res);
}
@@ -48,7 +68,7 @@ function proxyRequest(req: IncomingMessage, res: ServerResponse, dashboardPort:
res.end(
JSON.stringify({
error: "api_bridge_timeout",
- detail: `Proxy request timed out after ${API_BRIDGE_TIMEOUTS.proxyTimeoutMs}ms`,
+ detail: `Proxy request timed out after ${proxyTimeoutMs}ms`,
})
);
});
diff --git a/src/shared/utils/runtimeTimeouts.ts b/src/shared/utils/runtimeTimeouts.ts
index 672466a1c2..14e0e67afb 100644
--- a/src/shared/utils/runtimeTimeouts.ts
+++ b/src/shared/utils/runtimeTimeouts.ts
@@ -11,7 +11,7 @@ export const DEFAULT_STREAM_IDLE_TIMEOUT_MS = 600_000;
export const DEFAULT_STREAM_READINESS_TIMEOUT_MS = 30_000;
export const DEFAULT_FETCH_CONNECT_TIMEOUT_MS = 30_000;
export const DEFAULT_FETCH_KEEPALIVE_TIMEOUT_MS = 4_000;
-export const DEFAULT_API_BRIDGE_PROXY_TIMEOUT_MS = 30_000;
+export const DEFAULT_API_BRIDGE_PROXY_TIMEOUT_MS = 600_000;
export const DEFAULT_API_BRIDGE_SERVER_REQUEST_TIMEOUT_MS = 300_000;
export const DEFAULT_API_BRIDGE_SERVER_HEADERS_TIMEOUT_MS = 60_000;
export const DEFAULT_API_BRIDGE_SERVER_KEEPALIVE_TIMEOUT_MS = 5_000;
diff --git a/tests/unit/runtime-timeouts.test.ts b/tests/unit/runtime-timeouts.test.ts
index a1db61da52..2a588a63b6 100644
--- a/tests/unit/runtime-timeouts.test.ts
+++ b/tests/unit/runtime-timeouts.test.ts
@@ -12,6 +12,7 @@ test("upstream timeout config derives hidden fetch timeouts from FETCH_TIMEOUT_M
assert.deepEqual(config, {
fetchTimeoutMs: 600000,
streamIdleTimeoutMs: 600000,
+ streamReadinessTimeoutMs: 30000,
fetchHeadersTimeoutMs: 600000,
fetchBodyTimeoutMs: 600000,
fetchConnectTimeoutMs: 30000,
@@ -93,3 +94,10 @@ test("API bridge timeouts align request timeout with long proxy timeout by defau
serverSocketTimeoutMs: 0,
});
});
+
+test("API bridge proxy timeout defaults to the long upstream request window", () => {
+ const config = runtimeTimeouts.getApiBridgeTimeoutConfig({});
+
+ assert.equal(config.proxyTimeoutMs, 600000);
+ assert.equal(config.serverRequestTimeoutMs, 600000);
+});