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 {