Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.0 KB
Raw Blame
import { AsyncLocalStorage } from "node:async_hooks";

export interface CallLogApiKeyContext {
  apiKeyId: string | null;
  apiKeyName: string | null;
  /**
   * Provider connection selected for this attempt. Optional: modal handlers
   * that already pass `connectionId` on the entry keep precedence.
   */
  connectionId?: string | null;
}

const callLogApiKeyContext = new AsyncLocalStorage<CallLogApiKeyContext>();

/**
 * Bind API-key (and connection) attribution to every call log emitted by one request.
 *
 * Modal handlers fan out into provider-specific helpers that write their own
 * call logs. Request-scoped storage keeps that attribution available without
 * threading identity parameters through every provider handler.
 */
export function runWithCallLogApiKeyContext<TResult>(
  context: CallLogApiKeyContext,
  callback: () => TResult
): TResult {
  return callLogApiKeyContext.run(context, callback);
}

export function getCallLogApiKeyContext(): CallLogApiKeyContext | null {
  return callLogApiKeyContext.getStore() ?? null;
}