feat(tts): switch out retry for key:model rotation
Build and Push Image / build-backend (push) Successful in 4m51s
Build and Push Image / build-frontend (push) Failing after 1m28s
Build and Push Image / push (push) Has been skipped

This commit is contained in:
2026-07-12 11:35:27 +07:00 Unverified
parent c6c4afbc38
commit 78211e6443
+27 -3
View File
@@ -10,9 +10,17 @@ const { DONATE_WIDGET_KEY, GEMINI_TTS_API_KEY } = process.env as Record<
string
>;
const MODELS = [
"gemini-2.5-flash-preview-tts",
"gemini-3.1-flash-preview-tts",
] as const;
const API_KEYS = GEMINI_TTS_API_KEY.split(",")
.map((k) => k.trim())
.filter(Boolean);
const POSSIBLE_CHOICES = MODELS.flatMap((m) =>
API_KEYS.map((k) => `${k}:${m}` as `${string}:${(typeof MODELS)[number]}`),
);
const MAX_TRIES = 10;
const Schema = z.object({
message: z.string().max(1000),
@@ -33,19 +41,33 @@ export async function GET(request: Request) {
const textHash = hash(message).toString(36);
const cached = await redis!.get(`tts:${textHash}`);
console.log(
`[TTS] ${textHash}:${cached ? "cached" : "fresh"} "${message.slice(0, 200)}${message.length > 200 ? "..." : ""}"`,
);
if (cached)
return new Response(pcmToWav(cached), {
headers: { "Content-Type": "audio/wav" },
});
let audioData: string | undefined;
for (const apiKey of API_KEYS) {
let currentRotation = await redis!
.get("tts_key_rotation")
.then((e) => (e ? Number(e) : 0))
.catch(() => 0);
for (
let i = 0;
i < MAX_TRIES;
i++, currentRotation = (currentRotation + 1) % POSSIBLE_CHOICES.length
) {
const [apiKey, model] = POSSIBLE_CHOICES[currentRotation].split(":");
console.log(
`[TTS] Trying key ${apiKey.slice(0, 8)}:${model} (${currentRotation}/${POSSIBLE_CHOICES} #${i + 1})...`,
);
const client = new GoogleGenAI({ apiKey });
try {
const response = await client.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
model,
contents: [{ parts: [{ text: message }] }],
config: {
responseModalities: ["AUDIO"],
@@ -65,11 +87,13 @@ export async function GET(request: Request) {
console.error(`TTS API key failed: ${apiKey.slice(0, 8)}...`, e);
}
}
console.log("[TTS] Got response?", audioData);
if (!audioData)
return Response.json({ error: "Response is empty" }, { status: 500 });
queueMicrotask(() => {
redis!.setex(`tts:${textHash}`, 604800, audioData);
redis!.setex("tts_key_rotation", 604800, `${currentRotation}`);
});
return new Response(pcmToWav(audioData), {
headers: { "Content-Type": "audio/wav" },