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

4.3 KB
Raw Blame
/**
 * chatCore streaming semantic-cache store (Quality Gate v2 / Fase 9 — chatCore god-file
 * decomposition, #3501).
 *
 * Extracted from handleChatCore's onStreamComplete callback: after a 200 streaming response is
 * assembled, store it under its signature so a future temp=0 request can be served from cache.
 * Side-effect only (cache write + debug log), wrapped in fail-open try/catch. Behaviour is
 * byte-identical to the previous inline block — including the `_streamed` strip, the early
 * skip-on-too-large, and the `Number(...) || 0` token accounting. The early return was the last
 * statement of the callback, so returning from this helper is equivalent.
 */
import {
  generateSignature as defaultGenerateSignature,
  outputContractOf,
  setCachedResponse as defaultSetCachedResponse,
  isCacheableForWrite as defaultIsCacheableForWrite,
  isTruncatedStreamBody as defaultIsTruncatedStreamBody,
} from "@/lib/semanticCache";
import { isSmallEnoughForSemanticCache as defaultIsSmallEnough } from "../../utils/estimateSize.ts";
import { getSemanticCacheManager } from "../../services/cache/semanticCacheManager.ts";

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

type CacheBody = {
  messages?: unknown;
  input?: unknown;
  temperature?: number;
  top_p?: number;
};

export interface StreamingSemanticCacheStoreDeps {
  isCacheableForWrite: typeof defaultIsCacheableForWrite;
  /** Optional so pre-existing callers/tests with partial deps keep working. */
  isTruncatedStreamBody?: typeof defaultIsTruncatedStreamBody;
  isSmallEnoughForSemanticCache: typeof defaultIsSmallEnough;
  generateSignature: typeof defaultGenerateSignature;
  setCachedResponse: typeof defaultSetCachedResponse;
}

const DEFAULT_DEPS: StreamingSemanticCacheStoreDeps = {
  isCacheableForWrite: defaultIsCacheableForWrite,
  isTruncatedStreamBody: defaultIsTruncatedStreamBody,
  isSmallEnoughForSemanticCache: defaultIsSmallEnough,
  generateSignature: defaultGenerateSignature,
  setCachedResponse: defaultSetCachedResponse,
};

interface StreamingCacheArgs {
  enabled: boolean;
  streamStatus: number;
  streamResponseBody: Record<string, unknown> | null | undefined;
  body: CacheBody;
  headers: unknown;
  model: string;
  provider?: string;
  apiKeyId?: string;
  streamUsage?: Record<string, unknown> | null;
  log?: LoggerLike;
  videoTranscriptSensitive?: boolean;
}

function streamTokensSaved(streamUsage: Record<string, unknown> | null | undefined): number {
  const u = streamUsage as Record<string, unknown> | null;
  return (Number(u?.prompt_tokens ?? 0) || 0) + (Number(u?.completion_tokens ?? 0) || 0);
}

function writeStreamingCacheEntry(
  args: StreamingCacheArgs,
  deps: StreamingSemanticCacheStoreDeps
): void {
  try {
    const cleanBody = { ...(args.streamResponseBody as Record<string, unknown>) };
    delete cleanBody._streamed;
    if (!deps.isSmallEnoughForSemanticCache(cleanBody)) return;
    const sig = deps.generateSignature(
      args.model,
      args.body.messages ?? args.body.input,
      args.body.temperature,
      args.body.top_p,
      args.apiKeyId ?? undefined,
      outputContractOf(args.body)
    );
    const tokensSaved = streamTokensSaved(args.streamUsage);
    deps.setCachedResponse(sig, args.model, cleanBody, tokensSaved);
    args.log?.debug?.(
      "CACHE",
      `Stored streaming response for ${args.model} (${tokensSaved} tokens)`
    );

    getSemanticCacheManager()
      .store({
        body: args.body as Record<string, unknown>,
        headers: args.headers,
        response: cleanBody,
        model: args.model,
        provider: args.provider || (cleanBody.provider as string) || "",
        apiKeyId: args.apiKeyId,
        signature: sig,
        tokensSaved,
      })
      .catch(() => {});
  } catch {
    // Cache write failed — non-critical
  }
}

export function storeStreamingSemanticCacheResponse(
  args: StreamingCacheArgs,
  deps: StreamingSemanticCacheStoreDeps = DEFAULT_DEPS
): void {
  if (
    args.videoTranscriptSensitive ||
    !args.enabled ||
    args.streamStatus !== 200 ||
    !args.streamResponseBody ||
    !deps.isCacheableForWrite(args.body, args.headers) ||
    (deps.isTruncatedStreamBody ?? defaultIsTruncatedStreamBody)(args.streamResponseBody)
  ) {
    return;
  }
  writeStreamingCacheEntry(args, deps);
}