@bitkyc08/opencodex
Version:
Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code
61 lines (59 loc) • 2.62 kB
text/typescript
import {
boundEntryIdForLane,
recordProviderCacheAffinity,
releaseProviderCacheAffinity,
} from "../providers/cache-affinity";
import { isAnthropicBillingFailure } from "../providers/anthropic-billing-refusal";
import type { OcxUsage } from "../types";
import type { RequestLogContext } from "./request-log";
import { loadConfig } from "../config";
import { capturedCacheAffinityIdentity, currentCacheAffinityIdentity } from "../providers/cache-affinity-identity";
/** Finalize against the serving credential, never a logical combo's display label. */
export function finalizeProviderCacheAffinity(
logCtx: RequestLogContext,
status: number,
usage: OcxUsage | undefined,
): void {
const {
affinityLane: lane,
affinityKeyEntryId: entryId,
affinityProviderName: providerName,
} = logCtx;
if (!lane || !entryId || !providerName || logCtx.cacheAffinityApplicable !== true) return;
const identity = capturedCacheAffinityIdentity(logCtx);
if (!identity || identity !== currentCacheAffinityIdentity(providerName, loadConfig().providers[providerName], entryId)) return;
if (status >= 200 && status < 300 && logCtx.observedTerminalStatus !== "failed") {
const previouslyBound = boundEntryIdForLane(lane);
recordProviderCacheAffinity({
lane,
providerName,
entryId,
identity,
model: logCtx.affinityModel ?? logCtx.model,
cacheRead: usage?.cacheReadInputTokens ?? usage?.cachedInputTokens,
cacheWrite: usage?.cacheCreationInputTokens,
cacheWriteLong: usage?.cacheCreation1hInputTokens,
inputTokens: usage?.inputTokens,
});
if (logCtx.cacheAffinity === "kept" && logCtx.affinityKeptEntryId !== entryId) {
logCtx.cacheAffinity = "bound";
logCtx.cacheAffinityReason = "new";
} else if (!logCtx.cacheAffinity) {
logCtx.cacheAffinity = previouslyBound === entryId ? "kept" : "bound";
logCtx.cacheAffinityReason = previouslyBound === entryId ? "cache" : "new";
}
} else if (
logCtx.terminalSource !== "synthetic" &&
(status === 401 ||
status === 402 ||
status === 429 ||
(logCtx.providerAdapter === "anthropic" &&
isAnthropicBillingFailure(status, logCtx.upstreamError ?? "", logCtx.upstreamErrorType)))
) {
// Only this request's refusal is evidence. A shared cooldown may have been
// written by another conversation while this request was in flight.
if (!releaseProviderCacheAffinity(lane, providerName, entryId, identity)) return;
logCtx.cacheAffinity = "released";
logCtx.cacheAffinityReason = status === 401 ? "auth" : "quota";
}
}