Files
OmniRoute/open-sse/handlers/chatCore/idempotency.ts
Diego Rodrigues de Sa e Souza 3c9883bb73 Release v3.8.29 (#4126)
OmniRoute v3.8.29 — 115 commits since v3.8.28. Full CHANGELOG + 41 i18n mirrors. All content quality gates green (build, unit 8/8, vitest 188/188, PR test policy, quality gates extended, docs sync, quality ratchet). Remaining red CI checks are pre-existing release flakes (coverage-shard/integration/node-compat teardown), a new transitive undici advisory in electron devDeps, and a workflow-level CodeQL fail (0 open alerts). VPS-validated by the operator.
2026-06-19 06:49:01 -03:00

66 lines
2.2 KiB
TypeScript

import { getIdempotencyKey, checkIdempotency } from "@/lib/idempotencyLayer";
import { calculateCost } from "@/lib/usage/costCalculator";
import { attachOmniRouteMetaHeaders } from "@/domain/omnirouteResponseMeta";
/**
* Resolve the request's idempotency key once and check the idempotency store. Returns the
* resolved `idempotencyKey` alongside the cache `hit` so the caller can reuse the SAME key
* for the later save path instead of re-deriving it — eliminating the dual-derivation that
* the chatCore modularization (#3598) introduced. (#3821-review LEDGER-6)
*/
export async function checkIdempotencyCache({
clientRawRequest,
provider,
model,
effectiveServiceTier,
startTime,
log,
}: {
clientRawRequest: unknown;
provider: string;
model: string;
effectiveServiceTier: unknown;
startTime: number;
log: unknown;
}): Promise<{ hit: { success: true; response: Response } | null; idempotencyKey: string }> {
const idempotencyKey = getIdempotencyKey(clientRawRequest?.headers);
const cachedIdemp = checkIdempotency(idempotencyKey);
if (cachedIdemp) {
log?.debug?.("IDEMPOTENCY", `Hit for key=${idempotencyKey?.slice(0, 12)}...`);
const idempotentUsage =
cachedIdemp.response && typeof cachedIdemp.response === "object"
? ((cachedIdemp.response as Record<string, unknown>).usage as
| Record<string, unknown>
| undefined)
: undefined;
const idempotentCost = idempotentUsage
? await calculateCost(provider, model, idempotentUsage as Record<string, number>, {
serviceTier: effectiveServiceTier,
})
: 0;
const headers: Record<string, string> = {
"Content-Type": "application/json",
"X-OmniRoute-Idempotent": "true",
};
attachOmniRouteMetaHeaders(headers, {
provider,
model,
cacheHit: false,
latencyMs: Date.now() - startTime,
usage: idempotentUsage,
costUsd: idempotentCost,
});
return {
idempotencyKey,
hit: {
success: true,
response: new Response(JSON.stringify(cachedIdemp.response), {
status: cachedIdemp.status,
headers,
}),
},
};
}
return { hit: null, idempotencyKey };
}