feat(api): structured X-Routing-Fallback-Reason header for relay routing (#6872) (#7262)

This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-07-17 10:41:30 -03:00
committed by GitHub
parent 29bb59e18d
commit 6bb3207912
5 changed files with 66 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ import {
import {
getBifrostRoutingConfig,
getRoutingFallbackHeader,
getRoutingFallbackReasonHeader,
resolveRelayRoutingBackend,
shouldTryBifrostForRequest,
type BifrostRoutingConfig,
@@ -378,6 +379,12 @@ export async function POST(request: Request) {
// #5526 helper gates emission (auto + enabled); #5519 dynamic cooldown/error
// reason wins as the value when set, else falls back to the static "bifrost".
newHeaders.set("X-Routing-Fallback", bifrostFallbackReason ?? routingFallback);
// #6872: stable, machine-readable companion header — one of the 4 enum
// reason codes, or unset when the legacy value has no specific reason.
const fallbackReasonCode = getRoutingFallbackReasonHeader(bifrostFallbackReason);
if (fallbackReasonCode) {
newHeaders.set("X-Routing-Fallback-Reason", fallbackReasonCode);
}
}
return new Response(response.body, {

View File

@@ -102,3 +102,33 @@ export function getRoutingFallbackHeader(
): "bifrost" | undefined {
return backend === "auto" && config?.enabled ? "bifrost" : undefined;
}
export type RoutingFallbackReasonCode =
| "bifrost-cooldown"
| "bifrost-error"
| "bifrost-ineligible"
| "bifrost-provider-unknown";
const ROUTING_FALLBACK_REASON_CODES = new Set<RoutingFallbackReasonCode>([
"bifrost-cooldown",
"bifrost-error",
"bifrost-ineligible",
"bifrost-provider-unknown",
]);
/**
* Derives the stable, machine-readable reason code for X-Routing-Fallback-Reason
* from the existing (possibly parameterized) X-Routing-Fallback detail string.
* #6872: splits the enum token from the legacy ad-hoc detail (e.g. strips the
* "; remaining=<ms>" suffix on the cooldown case) without changing the legacy
* X-Routing-Fallback value itself.
*/
export function getRoutingFallbackReasonHeader(
fallbackReason: string | null | undefined
): RoutingFallbackReasonCode | undefined {
if (!fallbackReason) return undefined;
const code = fallbackReason.split(";", 1)[0]?.trim();
return code && ROUTING_FALLBACK_REASON_CODES.has(code as RoutingFallbackReasonCode)
? (code as RoutingFallbackReasonCode)
: undefined;
}