UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

32 lines (26 loc) • 1.71 kB
import type { SpendScopes } from "../../lib/spend-reservation-ledger"; import type { OcxProviderConfig } from "../../types"; import type { RequestLogContext } from "../request-log"; type PhysicalSpendTarget = SpendScopes & { targetKey?: string }; const targets = new WeakMap<RequestLogContext, () => PhysicalSpendTarget>(); type HostedAdvisorTarget = (provider?: OcxProviderConfig) => PhysicalSpendTarget; const hostedAdvisorTargets = new WeakMap<RequestLogContext, HostedAdvisorTarget>(); /** A prepared candidate can own admission without overwriting the last physical send's log. */ export function bindResponsesSpendTarget(context: RequestLogContext, target?: () => PhysicalSpendTarget): void { if (target) targets.set(context, target); else { targets.delete(context); hostedAdvisorTargets.delete(context); } } /** Optional hosted work checks an admitted identity without changing main-model accounting. */ export function bindResponsesHostedAdvisorSpendTarget(context: RequestLogContext, target: HostedAdvisorTarget): void { hostedAdvisorTargets.set(context, target); } export function responsesHostedAdvisorSpendTarget(context: RequestLogContext, fallbackPool?: string, provider?: OcxProviderConfig): PhysicalSpendTarget { return hostedAdvisorTargets.get(context)?.(provider) ?? responsesSpendTarget(context, fallbackPool); } /** Resolve late, after queued negotiation; ordinary callers retain their original pool fallback. */ export function responsesSpendTarget(context: RequestLogContext, fallbackPool?: string): PhysicalSpendTarget { return targets.get(context)?.() ?? { poolId: context.spendPoolId ?? fallbackPool ?? context.provider, identityId: context.accountLogLabel, }; }