Add TTS proxy to fix CORS issues

- /api/tts proxies requests to Edge TTS and Kokoro
- Uses Docker container names for internal networking
- Removes URL config from settings (handled server-side)
- Fixes localStorage merge for new settings fields

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Gemini Agent
2026-01-18 02:05:38 +00:00
parent f5de4749db
commit 611d57770e
3 changed files with 98 additions and 48 deletions

74
src/app/api/tts/route.ts Normal file
View File

@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from "next/server";
// POST /api/tts - Proxy TTS requests to avoid CORS issues
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { engine, url, text, voice, speed } = body;
if (!text) {
return NextResponse.json({ error: "Text is required" }, { status: 400 });
}
let ttsUrl: string;
let ttsBody: Record<string, unknown>;
if (engine === "edge") {
// Use Docker container name for internal networking, fallback to provided URL
const edgeHost = process.env.EDGE_TTS_URL || url || "http://edge-tts:5050";
ttsUrl = `${edgeHost}/v1/audio/speech`;
ttsBody = {
model: "tts-1",
input: text,
voice: voice || "en-US-AvaNeural",
response_format: "mp3",
speed: speed || 1.0,
};
} else if (engine === "kokoro") {
// Use Docker container name for internal networking, fallback to provided URL
const kokoroHost = process.env.KOKORO_TTS_URL || url || "http://kokoro-tts:8880";
ttsUrl = `${kokoroHost}/v1/audio/speech`;
ttsBody = {
model: "kokoro",
input: text,
voice: voice || "af_bella",
response_format: "mp3",
speed: speed || 1.0,
};
} else {
return NextResponse.json({ error: "Invalid engine" }, { status: 400 });
}
const response = await fetch(ttsUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(ttsBody),
});
if (!response.ok) {
const errorText = await response.text();
console.error("TTS error:", response.status, errorText);
return NextResponse.json(
{ error: `TTS service error: ${response.status}` },
{ status: 502 }
);
}
const audioBuffer = await response.arrayBuffer();
return new NextResponse(audioBuffer, {
headers: {
"Content-Type": "audio/mpeg",
"Content-Length": audioBuffer.byteLength.toString(),
},
});
} catch (error) {
console.error("TTS proxy error:", error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : "TTS proxy failed" },
{ status: 500 }
);
}
}