All files / web/src/lib/ai-usage record.ts

100% Statements 20/20
100% Branches 3/3
100% Functions 1/1
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 2x 3x 3x  
import type { NewAiUsageRecord } from '@/db/schema/ai-usage'
 
/**
 * Fire-and-forget DB insert for AI usage tracking.
 *
 * Never throws — logs errors to console. Returns immediately;
 * the insert runs in the background.
 *
 * Uses dynamic imports so that `@/db` (which pulls in `node:http` via
 * libsql) is NOT in the static import graph. This prevents webpack from
 * trying to bundle the DB driver into client-side code when helpers or
 * llm-middleware are transitively imported by client components.
 */
export function recordAiUsage(record: Omit<NewAiUsageRecord, 'id' | 'createdAt'>): void {
  void Promise.all([import('@/db'), import('@/db/schema/ai-usage')])
    .then(([{ db }, { aiUsage }]) => db.insert(aiUsage).values(record))
    .catch((err) => {
      console.error('[ai-usage] Failed to record usage:', err)
    })
}