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

2.3 KB
Raw Blame
/**
 * chatCore compression cache-stats hook (Quality Gate v2 / Fase 9 — chatCore god-file
 * decomposition, #3501).
 *
 * Extracted from handleChatCore's request-setup compression path: when a prompt was compressed,
 * record the caching-context cache-stats receipt (estimated cache hit + tokens saved). Best-effort,
 * fire-and-forget — the inner work is an un-awaited IIFE that swallows its own errors and never
 * affects the request. Behaviour is byte-identical to the previous inline block.
 */

import type { ConnectionCacheOverride } from "../../utils/cacheControlPolicy.ts";

type LoggerLike = { debug?: (...args: unknown[]) => void } | null | undefined;

export function recordCompressionCacheStats(args: {
  compressionInputBody: unknown;
  provider: string | null | undefined;
  targetFormat: string | null | undefined;
  effectiveModel: string | null | undefined;
  mode: string;
  stats: { originalTokens: number; compressedTokens: number };
  connectionCacheOverride?: ConnectionCacheOverride | null;
  log?: LoggerLike;
}): void {
  void (async () => {
    try {
      const { detectCachingContext } = await import("../../services/compression/cachingAware.ts");
      const { recordCacheStats } = await import("@/lib/db/compressionCacheStats");
      const cacheContext = detectCachingContext(args.compressionInputBody, {
        provider: args.provider,
        targetFormat: args.targetFormat,
        model: args.effectiveModel,
        connectionCacheOverride: args.connectionCacheOverride ?? null,
      });
      const tokensSavedCompression = Math.max(
        0,
        args.stats.originalTokens - args.stats.compressedTokens
      );
      recordCacheStats({
        provider: cacheContext.provider ?? args.provider ?? "unknown",
        model: args.effectiveModel ?? "",
        compressionMode: args.mode,
        cacheControlPresent: cacheContext.hasCacheControl,
        estimatedCacheHit: cacheContext.hasCacheControl && cacheContext.isCachingProvider,
        tokensSavedCompression,
        tokensSavedCaching: 0,
        netSavings: tokensSavedCompression,
      });
    } catch (err) {
      args.log?.debug?.(
        "COMPRESSION",
        "Compression cache stats write skipped: " +
          (err instanceof Error ? err.message : String(err))
      );
    }
  })();
}