teqo/src/utilities/ai/deepInfraTranscribe.ts
Francisco Solla 4668c018bf feat(B173): consulta por voz no chat da Sollinha (STT → rascunho)
Adiciona entrada por voz ao chat: botão de mic no InputGroup grava no
browser (getUserMedia + MediaRecorder), envia o clip multipart para a nova
rota POST /campanha/api/ai-transcribe (auth cookie + rate limit no mesmo
balde + same-origin) e injeta a transcrição como rascunho editável — o
envio segue o fluxo normal do chat. O áudio nunca é persistido.

STT via Deep Infra whisper-large-v3 (DEEPINFRA_API_KEY, mesma do B174);
a costura de rede vive em utilities/ai/deepInfraTranscribe.ts; a máquina
de captura em useMicTranscript. Falha de permissão/provedor degrada a
mensagem em pt-BR sem quebrar o chat por texto.

Closes #448
2026-08-09 01:47:03 -03:00

71 lines
2.3 KiB
TypeScript

import 'server-only'
/**
* Transcribes a recorded voice clip to text via Deep Infra's OpenAI-compatible
* transcription endpoint (B173). Same provider/key as the Sollinha LLM
* (`DEEPINFRA_API_KEY`), so one account, one key, one invoice.
*
* The audio is forwarded as a multipart `file` and never persisted anywhere —
* it lives only in this function's memory for the duration of the provider
* call (product requirement: B173 processes and discards the recording).
*/
export const DEEPINFRA_TRANSCRIBE_URL = 'https://api.deepinfra.com/v1/openai/audio/transcriptions'
const DEEPINFRA_WHISPER_MODEL = 'openai/whisper-large-v3'
const DEEPINFRA_LANGUAGE = 'pt'
export type TranscribeAudioResult =
| { ok: true; text: string }
| { ok: false; error: string; status: number }
/**
* Sends the clip to Deep Infra Whisper large-v3 and returns the transcript.
* Never throws: every failure becomes `{ ok: false }` with a pt-BR message the
* route can return verbatim, so a provider outage or a missing key degrades to
* "sem voz" instead of breaking the text chat.
*/
export const deepInfraTranscribe = async (file: Blob): Promise<TranscribeAudioResult> => {
const apiKey = process.env.DEEPINFRA_API_KEY
if (!apiKey) {
return { ok: false, error: 'Transcrição de voz está indisponível no momento.', status: 503 }
}
const form = new FormData()
form.append('file', file, 'voice.webm')
form.append('model', DEEPINFRA_WHISPER_MODEL)
form.append('language', DEEPINFRA_LANGUAGE)
try {
const response = await fetch(DEEPINFRA_TRANSCRIBE_URL, {
method: 'POST',
headers: { Authorization: `Bearer ${apiKey}` },
body: form,
})
if (!response.ok) {
return {
ok: false,
error: 'Não foi possível transcrever o áudio. Tente novamente.',
status: 502,
}
}
const data = (await response.json()) as { text?: unknown }
const text = typeof data.text === 'string' ? data.text.trim() : ''
if (!text) {
return {
ok: false,
error: 'Não foi possível transcrever o áudio. Tente novamente.',
status: 502,
}
}
return { ok: true, text }
} catch {
return {
ok: false,
error: 'Não foi possível transcrever o áudio. Tente novamente.',
status: 502,
}
}
}