Files
kai 5952eda953
release / which version (push) Successful in 3s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m5s
release / tag and release (push) Successful in 2s
26.11.46: Einkaufsliste einsprechen (#47)
2026-11-08 05:35:00 +00:00

30 lines
1.4 KiB
TypeScript

import "server-only";
import { getLocale } from "next-intl/server";
import { AiNotConfigured, AiUnavailable, transcribe } from "@/lib/ai";
import { aiFor } from "@/lib/ai-settings";
import { fail } from "@/lib/auth/http";
const MAX_BYTES = 25 * 1024 * 1024; // rund zehn Minuten Sprache
/**
* Aufnahme aus dem Formular (Feld „audio“) → Abschrift über den Router
* („default-stt“) oder das eigene Sprachmodell. Die Aufnahme wird nicht gespeichert.
* Gemeinsam für „Gericht einsprechen“ (26.11.24) und die Einkaufsliste (26.11.46).
*/
export async function transcribeUpload(request: Request, householdId: string): Promise<{ text: string } | Response> {
const form = await request.formData().catch(() => null);
const audio = form?.get("audio");
if (!(audio instanceof File) || audio.size === 0 || audio.size > MAX_BYTES || !/^(audio|video)\//.test(audio.type)) return fail("audio", 400);
const ai = await aiFor(householdId);
if (!ai?.speechModel) return fail("noSpeech", 409);
try {
const ext = audio.type.includes("mp4") || audio.type.includes("m4a") ? "m4a" : audio.type.includes("ogg") ? "ogg" : audio.type.includes("wav") ? "wav" : "webm";
const text = await transcribe(ai, audio, `aufnahme.${ext}`, await getLocale());
if (!text) return fail("empty", 422);
return { text };
} catch (error) {
if (error instanceof AiUnavailable || error instanceof AiNotConfigured) return fail("aiUnavailable", 503);
throw error;
}
}