import { OpenAI } from 'https://deno.land/x/openai@v4.20.1/mod.ts' import { createLogger } from './logger.ts' const openai = new OpenAI({ apiKey: Deno.env.get('OPENAI_API_KEY') }) export async function processAudioWithWhisper(audioUrl: string, logger: any): Promise { try { logger.info('whisper_start', 'Iniciando download do áudio', { url: audioUrl }) const audioResponse = await fetch(audioUrl) if (!audioResponse.ok) { logger.error('whisper_download_error', new Error(`HTTP ${audioResponse.status}`)) throw new Error('Falha ao baixar áudio') } const audioBlob = await audioResponse.blob() logger.info('whisper_download_complete', 'Download concluído', { size: audioBlob.size, type: audioBlob.type }) // Validação do blob if (audioBlob.size === 0) { throw new Error('Arquivo de áudio vazio') } const audioFile = new File([audioBlob], 'audio.webm', { type: audioBlob.type || 'audio/webm' }) // Transcrição com Whisper const transcription = await openai.audio.transcriptions.create({ file: audioFile, model: 'whisper-1', language: 'pt' }) if (!transcription?.text) { throw new Error('Resposta da transcrição vazia') } logger.info('whisper_complete', 'Transcrição concluída', { length: transcription.text.length }) return transcription.text } catch (error) { logger.error('whisper_error', error, { url: audioUrl }) throw error } }