Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(tts): validate Gemini voices before reporting readiness

Krishna Dobhal1107b76d1b6a
diff --git a/scripts/testing/key-readback.e2e.test.ts b/scripts/testing/key-readback.e2e.test.ts
index 8d57305237..1dbe578222 100644
--- a/scripts/testing/key-readback.e2e.test.ts
+++ b/scripts/testing/key-readback.e2e.test.ts
@@ -149,6 +149,12 @@ it("configures Gemini speech and synthesizes WAV through the isolated server", a
const status = (await call("GET", "/api/config")).body;
expect(status.tts).toMatchObject({ provider: "gemini", configured: true, ready: true, model: "gemini-3.8-flash-lite-tts" });
expect(JSON.stringify(status)).not.toContain("fixture-gemini-secret");
+ expect((await call("POST", "/api/tts/prepare", { text: "Hello", voiceId: "invalid" })).body.ready).toBe(false);
+ expect((await call("POST", "/api/tts/prepare", { text: "Hello", voiceId: "" })).body.ready).toBe(true);
+ expect((await call("PUT", "/api/config", { tts: { voice: "invalid" } })).body.tts).toMatchObject({ configured: true, ready: false });
+ expect((await call("POST", "/api/tts/prepare", { text: "Hello" })).body.ready).toBe(false);
+ expect((await call("POST", "/api/tts/prepare", { text: "Hello", voiceId: "Puck" })).body.ready).toBe(true);
+ expect((await call("PUT", "/api/config", { tts: { voice: "Kore" } })).status).toBe(200);
expect((await call("GET", "/api/tts/voices")).body.voices).toEqual(expect.arrayContaining([{ id: "Puck", label: "Puck" }]));
const response = await fetch(`${fixture.info.url}/api/tts/speak`, { method: "POST", headers: { "content-type": "application/json", origin: fixture.info.url }, body: JSON.stringify({ text: "Hello from the fixture", voiceId: "Puck" }) });
expect(response.status).toBe(200);
diff --git a/server/tts/gemini.test.ts b/server/tts/gemini.test.ts
index 2b5ff51527..07b8340202 100644
--- a/server/tts/gemini.test.ts
+++ b/server/tts/gemini.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { describeVoice, listVoices, providerConfigured, speak, voiceReady } from "./index.ts";
+import { describeVoice, listVoices, providerConfigured, speak, voiceConfigured, voiceReady } from "./index.ts";
import type { AppConfig } from "../config.ts";
const cfg: AppConfig = { tts: { provider: "gemini", geminiKey: "private-gemini-key", voice: "Kore" } };
@@ -20,6 +20,22 @@ const result = (data = wav.toString("base64"), mime_type = "audio/wav") => ({ st
afterEach(() => vi.unstubAllGlobals());
describe("Gemini speech", () => {
+ it.each([
+ { saved: "Kore", override: undefined, key: "key", configured: true, ready: true },
+ { saved: "invalid", override: undefined, key: "key", configured: false, ready: false },
+ { saved: "", override: undefined, key: "key", configured: false, ready: false },
+ { saved: undefined, override: undefined, key: "key", configured: false, ready: false },
+ { saved: "invalid", override: "Puck", key: "key", configured: false, ready: true },
+ { saved: "Kore", override: "invalid", key: "key", configured: true, ready: false },
+ { saved: "Kore", override: "", key: "key", configured: true, ready: true },
+ { saved: "Kore", override: "Puck", key: "", configured: false, ready: false },
+ { saved: "kore", override: undefined, key: "key", configured: false, ready: false },
+ ])("checks catalog membership and voice precedence: $saved / $override / $key", ({ saved, override, key, configured, ready }) => {
+ const config: AppConfig = { tts: { provider: "gemini", geminiKey: key, voice: saved } };
+ expect(voiceConfigured(config)).toBe(configured);
+ expect(voiceReady(config, override)).toBe(ready);
+ });
+
it("requires its own credential and keeps it out of status", async () => {
const missing: AppConfig = { tts: { provider: "gemini", key: "eleven", fishKey: "fish" }, xai: { key: "xai" } };
expect(providerConfigured(missing)).toBe(false);
diff --git a/server/tts/gemini.ts b/server/tts/gemini.ts
index e2132b4578..3bdee24283 100644
--- a/server/tts/gemini.ts
+++ b/server/tts/gemini.ts
@@ -13,8 +13,12 @@ export function listVoices(): Voice[] {
return VOICES.map((id) => ({ id, label: id }));
}
+export function isSupportedVoice(voiceId: string | undefined): boolean {
+ return voiceId !== undefined && VOICES.includes(voiceId);
+}
+
export async function synthesize(text: string, voiceId: string, key: string, model = DEFAULT_GEMINI_MODEL): Promise<Audio> {
- if (!VOICES.includes(voiceId)) throw new Error("Pick a Gemini voice from the voice list.");
+ if (!isSupportedVoice(voiceId)) throw new Error("Pick a Gemini voice from the voice list.");
let response: Response;
try {
response = await fetch(`${API}/interactions`, {
diff --git a/server/tts/index.ts b/server/tts/index.ts
index 27c3d5e5dd..4c427e77c7 100644
--- a/server/tts/index.ts
+++ b/server/tts/index.ts
@@ -43,7 +43,7 @@ export function providerConfigured(cfg: AppConfig): boolean {
export function voiceConfigured(cfg: AppConfig): boolean {
const provider = voiceProvider(cfg);
- if (provider === "gemini") return Boolean(cfg.tts?.geminiKey && cfg.tts?.voice);
+ if (provider === "gemini") return Boolean(cfg.tts?.geminiKey && gemini.isSupportedVoice(cfg.tts?.voice));
if (provider === "xai") return Boolean(cfg.xai?.key && cfg.tts?.voice);
if (provider === "system") {
return systemVoices.systemVoicesAvailable() && Boolean(cfg.tts?.voice);
@@ -55,7 +55,7 @@ export function voiceConfigured(cfg: AppConfig): boolean {
export function voiceReady(cfg: AppConfig, voiceId?: string): boolean {
const provider = voiceProvider(cfg);
- if (provider === "gemini") return Boolean(cfg.tts?.geminiKey && (voiceId || cfg.tts?.voice));
+ if (provider === "gemini") return Boolean(cfg.tts?.geminiKey && gemini.isSupportedVoice(voiceId || cfg.tts?.voice));
if (provider === "xai") return Boolean(cfg.xai?.key && (voiceId || cfg.tts?.voice));
if (provider === "system") {
return systemVoices.systemVoicesAvailable() && Boolean(voiceId || cfg.tts?.voice);