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.6 KB
Raw Blame
import type { OutputStyleSelectionEntry } from "./apply.ts";

/** The CompressionRunTelemetry fields this sub-project (A) fills. Clock-free / pure. */
export interface OutputStyleTelemetryRecord {
  requestId: string;
  model: string;
  provider: string;
  source: string;
  tokensBefore: number;
  tokensAfter: number;
  ratio: number;
  outputStyles?: OutputStyleSelectionEntry[];
  outputStyleBypass?: string;
  outputTokens?: number;
}

// Benign (non-content-bypass) skips that must NOT be recorded as a bypass reason.
const BENIGN_SKIPS = new Set(["disabled", "no_styles", "no_messages", "already_applied"]);

export function buildOutputStyleTelemetry(input: {
  requestId: string;
  model: string;
  provider: string;
  source: string;
  tokensBefore: number;
  tokensAfter: number;
  applied: boolean;
  appliedStyles?: OutputStyleSelectionEntry[];
  skippedReason?: string;
  outputTokens?: number;
}): OutputStyleTelemetryRecord {
  const ratio = input.tokensBefore > 0 ? input.tokensAfter / input.tokensBefore : 0;
  const record: OutputStyleTelemetryRecord = {
    requestId: input.requestId,
    model: input.model,
    provider: input.provider,
    source: input.source,
    tokensBefore: input.tokensBefore,
    tokensAfter: input.tokensAfter,
    ratio,
  };
  if (input.applied && input.appliedStyles && input.appliedStyles.length > 0) {
    record.outputStyles = input.appliedStyles;
  }
  if (!input.applied && input.skippedReason && !BENIGN_SKIPS.has(input.skippedReason)) {
    record.outputStyleBypass = input.skippedReason;
  }
  if (typeof input.outputTokens === "number") record.outputTokens = input.outputTokens;
  return record;
}