fix(api): echo X-OmniRoute-Compression response header (#6422) (#6441)

echo X-OmniRoute-Compression header on completions routes (#6422, 6/6). Reconciled with #6429 body.model echo. Integrated into release/v3.8.46.
This commit is contained in:
Chirag Singhal
2026-07-07 03:08:12 +05:30
committed by GitHub
parent d11bf528af
commit fa3a09cb43
5 changed files with 191 additions and 11 deletions

View File

@@ -8,6 +8,10 @@ import { acceptHeaderForcesStream } from "@omniroute/open-sse/utils/aiSdkCompat.
import { withEarlyStreamKeepalive } from "@omniroute/open-sse/utils/earlyStreamKeepalive";
import { resolveKeepaliveThreshold } from "@omniroute/open-sse/utils/keepaliveThreshold";
import { checkChatAdmission } from "@/shared/middleware/chatBodyAdmission";
import {
readCompressionRequestHeader,
withCompressionHeaderEcho,
} from "@/shared/utils/compressionHeaderEcho";
let initPromise = null;
@@ -99,14 +103,26 @@ export async function POST(request) {
parsedBodyIsRecord && acceptHeaderForcesStream(acceptHeader, parsedBody.stream);
const wantsStreaming = (parsedBodyIsRecord && parsedBody.stream === true) || acceptForcesStream;
// #6422 — capture the compression request header once so we can echo it back
// on the response when internal early-returns (idempotency cache, some combo
// paths) drop the meta the docs promise.
const compressionRequestHeader = readCompressionRequestHeader(request);
if (wantsStreaming) {
const reqId = generateRequestId();
return await withEarlyStreamKeepalive(handleChat(request, null, parsedBody, reqId), {
signal: request.signal,
thresholdMs: resolveKeepaliveThreshold(parsedBody?.model),
extraHeaders: { "X-Correlation-Id": reqId },
});
const streamedResponse = await withEarlyStreamKeepalive(
handleChat(request, null, parsedBody, reqId),
{
signal: request.signal,
thresholdMs: resolveKeepaliveThreshold(parsedBody?.model),
extraHeaders: { "X-Correlation-Id": reqId },
}
);
return withCompressionHeaderEcho(streamedResponse, compressionRequestHeader);
}
return await handleChat(request, null, parsedBody);
return withCompressionHeaderEcho(
await handleChat(request, null, parsedBody),
compressionRequestHeader
);
}

View File

@@ -3,6 +3,10 @@ import { buildClientRawRequest, handleChat } from "@/sse/handlers/chat";
import { initTranslators } from "@omniroute/open-sse/translator/index.ts";
import { createInjectionGuard } from "@/middleware/promptInjectionGuard";
import { asTextCompletionResponse } from "./textCompletionTransform.ts";
import {
readCompressionRequestHeader,
withCompressionHeaderEcho,
} from "@/shared/utils/compressionHeaderEcho";
let initPromise = null;
const injectionGuard = createInjectionGuard();
@@ -40,6 +44,10 @@ export async function OPTIONS() {
export async function POST(request: Request) {
await ensureInitialized();
// #6422 — capture the compression request header once so we can echo it back
// on the response when internal early-returns drop the meta the docs promise.
const compressionRequestHeader = readCompressionRequestHeader(request);
// Prompt injection guard
try {
const cloned = request.clone();
@@ -78,10 +86,14 @@ export async function POST(request: Request) {
// #3571 — translate the chat-pipeline response back to the legacy
// text-completion shape so OpenAI Completion clients (e.g. TabbyML) work.
// Thread `body.model` so response `body.model` echoes the caller's
// requested identifier, matching the `x-omniroute-model` header.
return await asTextCompletionResponse(
await handleChat(newRequest, buildClientRawRequest(request, body)),
typeof body.model === "string" ? body.model : undefined
// requested identifier, matching the `x-omniroute-model` header, and
// echo the compression header on the way out.
return withCompressionHeaderEcho(
await asTextCompletionResponse(
await handleChat(newRequest, buildClientRawRequest(request, body)),
typeof body.model === "string" ? body.model : undefined
),
compressionRequestHeader
);
}
}
@@ -101,5 +113,8 @@ export async function POST(request: Request) {
} catch {
// ignore — asTextCompletionResponse falls back to upstream body.model
}
return await asTextCompletionResponse(await handleChat(request), requestedModel);
return withCompressionHeaderEcho(
await asTextCompletionResponse(await handleChat(request), requestedModel),
compressionRequestHeader
);
}