feat: adaptive keepalive threshold for web-session providers (#3397)

Integrated into release/v3.8.16
This commit is contained in:
Paijo
2026-06-08 10:47:34 +07:00
committed by GitHub
parent 4adc1d087f
commit 5b72dc6250
3 changed files with 151 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import { withEarlyStreamKeepalive } from "@omniroute/open-sse/utils/earlyStreamK
import { resolveResponsesApiModel } from "@/app/api/internal/codex-responses-ws/modelResolution";
import { getModelInfo } from "@/sse/services/model";
import { getComboByName } from "@/lib/db/combos";
import { resolveKeepaliveThreshold } from "@omniroute/open-sse/utils/keepaliveThreshold";
// NOTE: We do NOT call initTranslators() here — the translator registry is
// bootstrapped at module level inside open-sse/translator/index.ts when it
@@ -68,7 +69,19 @@ export async function POST(request) {
const resolved = await withCodexPreferredModel(request);
const accept = String(request.headers?.get?.("accept") || "").toLowerCase();
if (accept.includes("text/event-stream")) {
return await withEarlyStreamKeepalive(handleChat(resolved), { signal: request.signal });
// Adaptive threshold: web-session and anonymous-fallback providers are slower
// to produce the first byte, so use a longer keepalive threshold (15s vs 2s).
let model;
try {
const body = await resolved.clone().json().catch(() => null);
model = body?.model;
} catch {
}
const thresholdMs = resolveKeepaliveThreshold(model);
return await withEarlyStreamKeepalive(handleChat(resolved), {
signal: request.signal,
thresholdMs,
});
}
return await handleChat(resolved);
}