rebreak-monorepo/backend/server/api/coach/speak-google.post.ts

70 lines
1.8 KiB
TypeScript

/**
* POST /api/coach/speak-google
* Test endpoint for Google Cloud TTS
*/
export default defineEventHandler(async (event) => {
await requireUser(event);
const body = await readBody(event);
const { text } = body as { text: string };
if (!text?.trim()) {
throw createError({ statusCode: 400, message: "text fehlt" });
}
const trimmed = text.slice(0, 4096);
const config = useRuntimeConfig();
if (!config.googleApiKey) {
throw createError({ statusCode: 503, message: "Google API Key nicht konfiguriert" });
}
try {
const response = await fetch(
`https://texttospeech.googleapis.com/v1/text:synthesize?key=${config.googleApiKey}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
input: { text: trimmed },
voice: {
languageCode: "de-DE",
name: "de-DE-Neural2-F",
ssmlGender: "FEMALE",
},
audioConfig: {
audioEncoding: "MP3",
speakingRate: 1.0,
pitch: 0,
},
}),
}
);
const result = await response.json();
if (!response.ok) {
console.error("[speak-google] Google TTS error:", result);
throw createError({
statusCode: response.status,
message: result.error?.message || "Google TTS fehlgeschlagen",
});
}
if (!result.audioContent) {
console.error("[speak-google] No audioContent:", result);
throw createError({ statusCode: 502, message: "Google TTS: kein Audio zurückgegeben" });
}
return { audio: `data:audio/mp3;base64,${result.audioContent}` };
} catch (err: any) {
console.error("[speak-google] Error:", err);
throw createError({
statusCode: 502,
message: err?.message || "Google TTS fehlgeschlagen",
});
}
});