// Speech to text for voice notes (PEP-45): any OpenAI-compatible audio API — // POST $PEPPA_TRANSCRIBE_URL/audio/transcriptions (multipart: file, model, language — the // speaker's, see transcribeLanguage —, // response_format=json) with "Authorization: Bearer $PEPPA_TRANSCRIBE_KEY" → {"text": "…"}. // OpenAI (https://api.openai.com/v1, whisper-1), Groq (https://api.groq.com/openai/v1, // whisper-large-v3), a local faster-whisper/speaches server — all speak this. Peppa's own is // infra/whisper/server.py (faster-whisper, 127.0.0.1:8178). The web dictation route // (apps/web/app/api/transcribe) uses it too. Tests use // mock-transcribe (packages/testkit). import { m as tr, type Locale } from "@peppa/shared/i18n"; export interface TranscribeConfig { url: string; key: string | null; model: string; /** PEPPA_TRANSCRIBE_LANG: the language of users with the Russian interface; null — `auto`, nobody's language is sent. */ language: string | null; /** How long to wait for the text: a local model on CPU (PEP-108) is slower than a cloud API. */ timeoutMs: number; } /** From env; null when voice is not set up (the bot asks for text then). */ export function transcribeConfig(env: NodeJS.ProcessEnv = process.env): TranscribeConfig | null { const url = env.PEPPA_TRANSCRIBE_URL?.trim(); if (!url) return null; return { url: url.replace(/\/$/, ""), key: env.PEPPA_TRANSCRIBE_KEY?.trim() || null, model: env.PEPPA_TRANSCRIBE_MODEL?.trim() || "whisper-1", language: env.PEPPA_TRANSCRIBE_LANG === "" || env.PEPPA_TRANSCRIBE_LANG?.trim().toLowerCase() === "auto" ? null : (env.PEPPA_TRANSCRIBE_LANG?.trim() || "ru"), timeoutMs: (Number(env.PEPPA_TRANSCRIBE_TIMEOUT_SEC) > 0 ? Number(env.PEPPA_TRANSCRIBE_TIMEOUT_SEC) : 300) * 1000, }; } /** * PEP-131: the language to recognise a user's recording in — their interface language (people * speak the language they read: Kazakh forced to "ru" comes out as nonsense, and autodetect * mistakes short Kazakh phrases for Russian). The Russian interface keeps PEPPA_TRANSCRIBE_LANG; * `auto` there switches the hint off for everyone (null — the service detects the language). */ export function transcribeLanguage(cfg: TranscribeConfig, locale: Locale): string | null { if (cfg.language == null) return null; return locale === "ru" ? cfg.language : locale; } export class TranscribeError extends Error {} /** `language`: what transcribeLanguage() picked for the speaker; omitted — the configured one. */ export async function transcribe(cfg: TranscribeConfig, audio: Buffer, filename = "voice.ogg", mime = "audio/ogg", language: string | null = cfg.language): Promise { const form = new FormData(); form.set("file", new Blob([new Uint8Array(audio)], { type: mime }), filename); form.set("model", cfg.model); form.set("response_format", "json"); if (language) form.set("language", language); let res: Response; try { res = await fetch(`${cfg.url}/audio/transcriptions`, { method: "POST", headers: cfg.key ? { Authorization: `Bearer ${cfg.key}` } : {}, body: form, signal: AbortSignal.timeout(cfg.timeoutMs), }); } catch (e) { throw new TranscribeError(tr("сервис расшифровки недоступен: {error}", { error: (e as Error).message })); } const body = (await res.json().catch(() => ({}))) as { text?: string; error?: { message?: string } | string }; if (!res.ok) { const msg = typeof body.error === "string" ? body.error : body.error?.message; throw new TranscribeError(tr("расшифровка не удалась: {status}", { status: `${res.status}${msg ? ` ${msg}` : ""}` })); } return (body.text ?? "").trim(); }