From 7a7f13f623f3b31e27cbf4f40f7c4ce393b6a58a Mon Sep 17 00:00:00 2001 From: ThomasOrtiz3647 <58230587-ThomasOrtiz3647@users.noreply.replit.com> Date: Sun, 26 Apr 2026 14:34:43 +0000 Subject: [PATCH] Improve streaming by correctly detecting client disconnects Externalize OpenAI and Anthropic SDKs and fix streaming logic by using `res.on("close")` instead of `req.on("close")` to accurately detect client disconnects and prevent premature loop termination. Replit-Commit-Author: Agent Replit-Commit-Session-Id: dcd090ca-935a-42ae-8db4-503e1bbe165a Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: e74b2f37-ad8b-469c-8d06-fdc381ac4a39 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/40608c50-a053-47e8-aa03-1bdcc1a2698b/dcd090ca-935a-42ae-8db4-503e1bbe165a/ZBSB1hW Replit-Helium-Checkpoint-Created: true --- artifacts/api-server/build.mjs | 2 ++ artifacts/api-server/src/routes/proxy.ts | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/artifacts/api-server/build.mjs b/artifacts/api-server/build.mjs index 86ebf7f..111c94c 100644 --- a/artifacts/api-server/build.mjs +++ b/artifacts/api-server/build.mjs @@ -29,6 +29,8 @@ async function buildAll() { // - use path traversal to read files (e.g. @google-cloud/secret-manager loads sibling .proto files) external: [ "*.node", + "openai", + "@anthropic-ai/sdk", "sharp", "better-sqlite3", "sqlite3", diff --git a/artifacts/api-server/src/routes/proxy.ts b/artifacts/api-server/src/routes/proxy.ts index 0d6047e..77e48c7 100644 --- a/artifacts/api-server/src/routes/proxy.ts +++ b/artifacts/api-server/src/routes/proxy.ts @@ -153,9 +153,9 @@ async function handleOpenAI( } let keepalive: NodeJS.Timeout | undefined; - let closed = false; - req.on("close", () => { - closed = true; + let clientGone = false; + res.on("close", () => { + if (!res.writableEnded) clientGone = true; if (keepalive) clearInterval(keepalive); }); @@ -168,12 +168,12 @@ async function handleOpenAI( keepalive = setupSse(res); for await (const chunk of upstream as AsyncIterable) { - if (closed) break; + if (clientGone) break; res.write(`data: ${JSON.stringify(chunk)}\n\n`); res.flush?.(); } - if (!closed) { + if (!clientGone) { res.write("data: [DONE]\n\n"); res.flush?.(); } @@ -193,7 +193,7 @@ async function handleOpenAI( } } finally { if (keepalive) clearInterval(keepalive); - if (!closed) { + if (!clientGone) { try { res.end(); } catch { @@ -292,9 +292,9 @@ async function handleAnthropic( } let keepalive: NodeJS.Timeout | undefined; - let closed = false; - req.on("close", () => { - closed = true; + let clientGone = false; + res.on("close", () => { + if (!res.writableEnded) clientGone = true; if (keepalive) clearInterval(keepalive); }); @@ -322,7 +322,7 @@ async function handleAnthropic( let stopReason: string | null = null; for await (const event of upstream) { - if (closed) break; + if (clientGone) break; if ( event.type === "content_block_delta" && event.delta.type === "text_delta" @@ -350,7 +350,7 @@ async function handleAnthropic( } } - if (!closed) { + if (!clientGone) { const finishChunk = { id, object: "chat.completion.chunk", @@ -384,7 +384,7 @@ async function handleAnthropic( } } finally { if (keepalive) clearInterval(keepalive); - if (!closed) { + if (!clientGone) { try { res.end(); } catch {