Skip to content

LIBERATED AI CHAT

TypeScript
6.1 KB
Raw Blame
/**
 * Client-Side Telemetry Beacon
 *
 * Fires metadata events to the Cloudflare Pages telemetry proxy after
 * every LLM request. The proxy commits batches to the HuggingFace
 * dataset repo (pliny-the-prompter/g0dm0d3) as JSONL files.
 *
 * Event schema boundaries:
 * - NO message content, prompts, or responses
 * - NO dedicated API key, token, or identity fields
 * - Only structural metadata: model, latency, pipeline config, context type
 *
 * Events are batched in memory and flushed every 30 seconds or when
 * the batch hits 20 events (whichever comes first). Fire-and-forget —
 * telemetry never blocks the UI or throws user-facing errors.
 */

// ── Config ───────────────────────────────────────────────────────────

// The telemetry endpoint lives on the same origin as the static site
// (Cloudflare Pages Function at /api/telemetry)
const TELEMETRY_ENDPOINT = '/api/telemetry'
const BATCH_SIZE = 20
const FLUSH_INTERVAL_MS = 30_000 // 30 seconds

// ── Types ────────────────────────────────────────────────────────────

export interface TelemetryEvent {
  type: string
  timestamp: number
  session_id: string
  [key: string]: unknown
}

export interface ChatTelemetryData {
  mode: 'standard' | 'ultraplinian'
  model: string
  duration_ms: number
  response_length: number
  success: boolean
  error_type?: string

  // Pipeline config (what was enabled, not what it produced)
  pipeline: {
    autotune: boolean
    parseltongue: boolean
    stm_modules: string[]
    strategy?: string
    godmode: boolean
  }

  // AutoTune metadata (context detection only)
  autotune?: {
    detected_context: string
    confidence: number
  }

  // Parseltongue metadata
  parseltongue?: {
    triggers_found: number
    technique: string
    intensity: string
  }

  // ULTRAPLINIAN race metadata
  ultraplinian?: {
    tier: string
    models_queried: number
    models_succeeded: number
    winner_model: string
    winner_score: number
    total_duration_ms: number
  }

  // Prompt classification (harm taxonomy — category labels only, never content)
  classification?: {
    domain: string
    subcategory: string
    confidence: number
    flags: string[]
    intent?: string           // what the user is trying to do (LLM-only)
  }

  // Structural context (no prompt or response text)
  persona?: string                  // active persona id
  prompt_length?: number            // character count of user input
  conversation_depth?: number       // number of messages in conversation so far
  memory_count?: number             // number of active memories injected
  no_log?: boolean                  // was no-log mode enabled
  parseltongue_transformed?: boolean // did parseltongue actually transform the prompt
}

// ── State ────────────────────────────────────────────────────────────

let eventBuffer: TelemetryEvent[] = []
let flushTimer: ReturnType<typeof setInterval> | null = null
let sessionId: string | null = null

function getSessionId(): string {
  if (!sessionId) {
    // Generate a random session ID (not tied to any user identity)
    sessionId = typeof crypto !== 'undefined' && crypto.randomUUID
      ? crypto.randomUUID()
      : `${Date.now().toString(36)}-${Array.from(crypto.getRandomValues(new Uint8Array(6)), b => b.toString(36)).join('')}`
  }
  return sessionId
}

// ── Public API ───────────────────────────────────────────────────────

/**
 * Record a chat completion event. Fire-and-forget.
 * Call this after every LLM request (both standard and ULTRAPLINIAN).
 */
export function recordChatEvent(data: ChatTelemetryData): void {
  const event: TelemetryEvent = {
    type: 'chat_completion',
    timestamp: Date.now(),
    session_id: getSessionId(),
    ...data,
  }

  eventBuffer.push(event)

  // Auto-flush if buffer is full
  if (eventBuffer.length >= BATCH_SIZE) {
    flush()
  }
}

/**
 * Record an arbitrary telemetry event. Fire-and-forget.
 */
export function recordEvent(type: string, data: Record<string, unknown> = {}): void {
  eventBuffer.push({
    type,
    timestamp: Date.now(),
    session_id: getSessionId(),
    ...data,
  })

  if (eventBuffer.length >= BATCH_SIZE) {
    flush()
  }
}

/**
 * Start the periodic flush timer.
 * Call once on app startup.
 */
export function startTelemetry(): void {
  if (flushTimer) return

  flushTimer = setInterval(() => {
    if (eventBuffer.length > 0) {
      flush()
    }
  }, FLUSH_INTERVAL_MS)

  // Flush on page unload (best-effort via sendBeacon)
  if (typeof window !== 'undefined') {
    window.addEventListener('beforeunload', () => {
      if (eventBuffer.length > 0) {
        const payload = JSON.stringify({ events: eventBuffer })
        navigator.sendBeacon(TELEMETRY_ENDPOINT, payload)
        eventBuffer = []
      }
    })
  }
}

/**
 * Stop the periodic flush timer and flush remaining events.
 */
export function stopTelemetry(): void {
  if (flushTimer) {
    clearInterval(flushTimer)
    flushTimer = null
  }
  if (eventBuffer.length > 0) {
    flush()
  }
}

// ── Flush ────────────────────────────────────────────────────────────

function flush(): void {
  if (eventBuffer.length === 0) return

  // Grab current buffer and reset
  const events = eventBuffer
  eventBuffer = []

  // Fire-and-forget — never block the UI
  fetch(TELEMETRY_ENDPOINT, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ events }),
  }).catch(() => {
    // Silent fail — telemetry should never disrupt the user experience.
    // Events are lost if the endpoint is down; this is acceptable.
  })
}