All files / web/src/lib/tasks collected-clip-generate.ts

0% Statements 0/266
0% Branches 0/1
0% Functions 0/1
0% Lines 0/266

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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { existsSync, mkdirSync, writeFileSync } from 'fs'
import { join } from 'path'
import { db } from '@/db'
import { ttsCollectedClips, ttsCollectedClipSay } from '@/db/schema'
import { inArray } from 'drizzle-orm'
import { createTask } from '../task-manager'
import type { CollectedClipGenerateEvent } from './events'
import { resolveCanonicalText } from '../audio/clipHash'
import { recordTtsUsage } from '../ai-usage/helpers'
import { AiFeature } from '../ai-usage/features'

const AUDIO_DIR = join(process.cwd(), 'data', 'audio')

export interface CollectedClipGenerateInput {
  voice: string
  clipIds: string[]
  _userId?: string
}

export interface CollectedClipGenerateOutput {
  voice: string
  generated: number
  errors: number
  total: number
}

/**
 * Resolve the best input text for TTS generation from the say table.
 * Uses shared resolveCanonicalText for consistent priority logic.
 */
function resolveSayText(
  sayEntries: Map<string, Record<string, string>>,
  clipId: string
): string | null {
  const sayMap = sayEntries.get(clipId)
  if (!sayMap) return null
  const text = resolveCanonicalText(sayMap)
  return text || null
}

/**
 * Start a background task to generate OpenAI TTS mp3s for collected clips.
 *
 * Collected clips store their tone as freeform text which is passed directly
 * to the OpenAI `instructions` param. Files are saved as `{clipId}.mp3`.
 */
export async function startCollectedClipGeneration(
  input: CollectedClipGenerateInput
): Promise<string> {
  return createTask<
    CollectedClipGenerateInput,
    CollectedClipGenerateOutput,
    CollectedClipGenerateEvent
  >('collected-clip-generate', input, async (handle, config) => {
    const apiKey = process.env.LLM_OPENAI_API_KEY || process.env.OPENAI_API_KEY
    if (!apiKey) {
      handle.fail('LLM_OPENAI_API_KEY is not configured')
      return
    }

    const voiceDir = join(AUDIO_DIR, config.voice)
    mkdirSync(voiceDir, { recursive: true })

    console.log('[collected-clip-generate] config:', {
      voice: config.voice,
      clipIds: config.clipIds,
      voiceDir,
    })

    // Fetch clip data from DB
    const clips = await db
      .select()
      .from(ttsCollectedClips)
      .where(inArray(ttsCollectedClips.id, config.clipIds))

    console.log('[collected-clip-generate] DB query result:', {
      requestedIds: config.clipIds,
      foundCount: clips.length,
      foundIds: clips.map((c) => c.id),
    })

    if (clips.length === 0) {
      handle.emit({
        type: 'cc_gen_complete',
        generated: 0,
        errors: 0,
        total: 0,
      })
      handle.complete({
        voice: config.voice,
        generated: 0,
        errors: 0,
        total: 0,
      })
      return
    }

    // Fetch say entries for these clips
    const sayRows = await db
      .select()
      .from(ttsCollectedClipSay)
      .where(inArray(ttsCollectedClipSay.clipId, config.clipIds))

    const sayByClipId = new Map<string, Record<string, string>>()
    for (const row of sayRows) {
      let map = sayByClipId.get(row.clipId)
      if (!map) {
        map = {}
        sayByClipId.set(row.clipId, map)
      }
      map[row.locale] = row.text
    }

    // Filter to clips that don't already have files on disk
    const missing = clips.filter((clip) => {
      const filePath = join(voiceDir, `${clip.id}.mp3`)
      const exists = existsSync(filePath)
      console.log('[collected-clip-generate] file check:', { clipId: clip.id, filePath, exists })
      return !exists
    })

    console.log('[collected-clip-generate] missing clips:', {
      total: clips.length,
      missing: missing.length,
    })

    handle.emit({
      type: 'cc_gen_started',
      voice: config.voice,
      totalClips: clips.length,
      missingClips: missing.length,
    })

    if (missing.length === 0) {
      handle.emit({
        type: 'cc_gen_complete',
        generated: 0,
        errors: 0,
        total: clips.length,
      })
      handle.complete({
        voice: config.voice,
        generated: 0,
        errors: 0,
        total: clips.length,
      })
      return
    }

    handle.setProgress(0, `Generating 0/${missing.length} clips`)

    let generated = 0
    let errors = 0
    let consecutiveErrors = 0
    let lastErrorMessage = ''
    const MAX_CONSECUTIVE_ERRORS = 3

    for (let i = 0; i < missing.length; i++) {
      if (handle.isCancelled()) break

      const clip = missing[i]

      // Resolve input text from say table, falling back to clipId itself
      // (older collected clips may lack say entries if they were registered
      // as plain strings before the default-say fix).
      const inputText = resolveSayText(sayByClipId, clip.id) ?? clip.id

      try {
        const response = await fetch('https://api.openai.com/v1/audio/speech', {
          method: 'POST',
          headers: {
            Authorization: `Bearer ${apiKey}`,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            model: 'gpt-4o-mini-tts',
            voice: config.voice,
            input: inputText,
            instructions: clip.tone,
            response_format: 'mp3',
          }),
        })

        if (!response.ok) {
          const errText = await response.text()
          errors++
          const errorMsg = `HTTP ${response.status}: ${errText}`
          consecutiveErrors++
          lastErrorMessage = errorMsg
          handle.emit({
            type: 'cc_clip_error',
            clipId: clip.id,
            error: errorMsg,
          })
        } else {
          const arrayBuffer = await response.arrayBuffer()
          writeFileSync(join(voiceDir, `${clip.id}.mp3`), Buffer.from(arrayBuffer))
          generated++
          consecutiveErrors = 0
          if (config._userId) {
            recordTtsUsage(inputText, 'gpt-4o-mini-tts', {
              userId: config._userId,
              feature: AiFeature.TTS_COLLECTED,
              backgroundTaskId: handle.id,
            })
          }
          handle.emit({ type: 'cc_clip_done', clipId: clip.id })
        }
      } catch (err) {
        errors++
        const errorMsg = err instanceof Error ? err.message : String(err)
        consecutiveErrors++
        lastErrorMessage = errorMsg
        handle.emit({
          type: 'cc_clip_error',
          clipId: clip.id,
          error: errorMsg,
        })
      }

      // Abort early on systemic failures
      if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
        const friendlyMsg = describeSystemicError(lastErrorMessage, config.voice)
        handle.fail(friendlyMsg)
        return
      }

      const progress = Math.round(((i + 1) / missing.length) * 100)
      handle.setProgress(progress, `Generating ${i + 1}/${missing.length} clips`)
    }

    handle.emit({
      type: 'cc_gen_complete',
      generated,
      errors,
      total: clips.length,
    })

    handle.complete({
      voice: config.voice,
      generated,
      errors,
      total: clips.length,
    })
  })
}

/** Map raw error messages to user-friendly descriptions for systemic failures. */
function describeSystemicError(rawError: string, voice: string): string {
  if (rawError.includes('EACCES') || rawError.includes('permission denied')) {
    return `Permission denied writing audio files for voice "${voice}". The server cannot write to the audio storage directory. An admin needs to fix the directory permissions.`
  }
  if (rawError.includes('ENOSPC') || rawError.includes('no space')) {
    return `Disk full — not enough space to write audio files for voice "${voice}".`
  }
  if (rawError.includes('ENOENT')) {
    return `Audio storage directory not found for voice "${voice}". The volume may not be mounted.`
  }
  if (rawError.includes('HTTP 401') || rawError.includes('HTTP 403')) {
    return `OpenAI API authentication failed. Check that LLM_OPENAI_API_KEY is valid.`
  }
  if (rawError.includes('HTTP 429')) {
    return `OpenAI API rate limit exceeded. Try again later or reduce batch size.`
  }
  return `Generation failed after repeated errors: ${rawError}`
}