Skip to content

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

ElevenLabs Live: stale key save/check ignored after a service change; close during mic prompt rejects

Bugfixer 2a4d75d8ec50d
diff --git a/src/components/keys/KeyRow.tsx b/src/components/keys/KeyRow.tsx
index 3d564f66d8..0c32f429a5 100644
--- a/src/components/keys/KeyRow.tsx
+++ b/src/components/keys/KeyRow.tsx
@@ -141,6 +141,8 @@ export function KeyRow({
setVerdict(null);
setWorks(false);
}
+ // A check or save still running for the previous service must not show its result on this one.
+ useEffect(() => () => { generation.current += 1; }, [entry.name]);
const target = entry.target;
if (!target) throw new Error(`${id} is not saved by a key row`);
const pointer = row.id !== entry.id;
@@ -197,7 +199,7 @@ export function KeyRow({
return;
}
busyRef.current = true;
- generation.current += 1;
+ const mine = ++generation.current;
const hadFocus = document.activeElement === inputRef.current;
setBusy("saving");
setProblem(null);
@@ -208,17 +210,20 @@ export function KeyRow({
const status = await saveKey(target, value, !saved.configured);
dispatch({ type: "configStatus", config: status });
const now = target.saved(status);
- setDraft("");
- setShown(false);
- setReplacing(false);
- setOwnKey(false);
- if (hadFocus) focusAfter.current = now.configured ? "replace" : "input";
- if (!now.configured) setProblem(t("keys.row.notKept"));
+ const current = mine === generation.current;
+ if (current) {
+ setDraft("");
+ setShown(false);
+ setReplacing(false);
+ setOwnKey(false);
+ if (hadFocus) focusAfter.current = now.configured ? "replace" : "input";
+ if (!now.configured) setProblem(t("keys.row.notKept"));
+ }
onSaved?.(now.configured);
if (now.configured && via === "settings") target.settingsEvent?.("add_key");
- keySaved(target.analytics, via, now.configured && target.test ? await runCheck() : savedKeyResult(target.analytics));
+ keySaved(target.analytics, via, current && now.configured && target.test ? await runCheck() : savedKeyResult(target.analytics));
} catch (cause) {
- setProblem(errorText(cause));
+ if (mine === generation.current) setProblem(errorText(cause));
keySaved(target.analytics, via, keySaveFailure(cause));
} finally {
busyRef.current = false;
diff --git a/src/components/keys/KeysPage.dom.test.ts b/src/components/keys/KeysPage.dom.test.ts
index 86554e7d62..ce60dc5f7c 100644
--- a/src/components/keys/KeysPage.dom.test.ts
+++ b/src/components/keys/KeysPage.dom.test.ts
@@ -159,6 +159,23 @@ describe("the keys page", () => {
expect(field().value).toBe("");
});
+ it("shows nothing of a key save that finishes after the Live calls service changed", async () => {
+ const live = { configured: false, voice: "", readTypedReplies: true, idleMinutes: 5 };
+ setConfig({ ...fx.config, live: { ...live, provider: "fish" } });
+ let answer: (value: unknown) => void = () => {};
+ fx.api.mockImplementation(() => new Promise((resolve) => { answer = resolve; }));
+ page();
+ const field = () => row("liveKey")!.querySelector<HTMLInputElement>('input[type="password"]')!;
+ paste(field(), "typed-for-fish-audio");
+ expect(fx.api).toHaveBeenCalledWith("/api/config", expect.objectContaining({ body: JSON.stringify({ tts: { fishKey: "typed-for-fish-audio" } }) }));
+
+ flushSync(() => setConfig({ ...fx.config, live: { ...live, provider: "elevenlabs" } }));
+ await answered(() => answer({ ...fx.config }));
+ expect(row("liveKey")!.textContent).toContain("ElevenLabs");
+ expect(row("liveKey")!.textContent).not.toContain("didn't stay saved");
+ expect(fx.api).toHaveBeenCalledTimes(1);
+ });
+
it("opens a section that holds a saved key, and counts what is connected", () => {
setConfig({ ...fx.config, box: { configured: true, last4: "c3d4" } });
page();
diff --git a/src/lib/live-call-elevenlabs.test.ts b/src/lib/live-call-elevenlabs.test.ts
index 6e87b6f2bc..7ffd072e11 100644
--- a/src/lib/live-call-elevenlabs.test.ts
+++ b/src/lib/live-call-elevenlabs.test.ts
@@ -36,9 +36,12 @@ class FakeAudio implements GeminiAudio {
flushed = 0;
closed = false;
mic: ((pcm: ArrayBuffer) => void) | null = null;
- constructor(readonly rate: number, readonly denied = false) {}
+ /** Set while the microphone permission prompt is still open; calling it answers the prompt. */
+ allowMic: (() => void) | null = null;
+ constructor(readonly rate: number, readonly denied = false, readonly held = false) {}
async startMic(onChunk: (pcm: ArrayBuffer) => void) {
if (this.denied) throw Object.assign(new Error("denied"), { name: "NotAllowedError" });
+ if (this.held) await new Promise<void>((resolve) => { this.allowMic = resolve; });
this.mic = onChunk;
}
play(pcm: ArrayBuffer) { this.played.push(pcm.byteLength); this.playing += 1; }
@@ -48,12 +51,12 @@ class FakeAudio implements GeminiAudio {
close() { this.closed = true; }
}
-function harness({ denied = false } = {}) {
+function harness({ denied = false, held = false } = {}) {
const sockets: FakeSocket[] = [];
const audios: FakeAudio[] = [];
const start = elevenLabsSessionStarter({
openSocket: (url) => { const socket = new FakeSocket(url); sockets.push(socket); return socket; },
- audio: (rate) => { const audio = new FakeAudio(rate, denied); audios.push(audio); return audio; },
+ audio: (rate) => { const audio = new FakeAudio(rate, denied, held); audios.push(audio); return audio; },
});
const callbacks = {
onConnect: vi.fn(), onDisconnect: vi.fn(), onModeChange: vi.fn(), onUserTranscript: vi.fn(), onAgentResponseDelta: vi.fn(), onError: vi.fn(),
@@ -116,6 +119,17 @@ describe("an ElevenLabs Live session", () => {
await expect(t.starting).rejects.toMatchObject({ code: "elevenlabs_1008" });
});
+ it("rejects at once when ElevenLabs closes while the microphone prompt is still open, and never reports a connection", async () => {
+ const t = harness({ held: true });
+ t.socket().open();
+ t.socket().receive(METADATA);
+ t.socket().drop(1011);
+ await expect(t.starting).rejects.toMatchObject({ code: "elevenlabs_1011" });
+ t.audio().allowMic?.();
+ await settle();
+ expect(t.callbacks.onConnect).not.toHaveBeenCalled();
+ });
+
it("answers ElevenLabs' pings so the conversation stays open", async () => {
const t = harness();
await t.connect();
diff --git a/src/lib/live-call-elevenlabs.ts b/src/lib/live-call-elevenlabs.ts
index 5a8a1b09a5..a44365500f 100644
--- a/src/lib/live-call-elevenlabs.ts
+++ b/src/lib/live-call-elevenlabs.ts
@@ -98,8 +98,9 @@ class ElevenLabsLiveSession implements FishAgentSession {
if (this.socket !== socket) return;
this.socket = null;
if (this.ready) return this.lost();
+ // Also while the microphone prompt is open: the call can no longer start, and open() then reports no connection.
this.shut();
- if (!started) reject(Object.assign(new Error("ElevenLabs closed the call before it started."), { code: `elevenlabs_${event.code}` }));
+ reject(Object.assign(new Error("ElevenLabs closed the call before it started."), { code: `elevenlabs_${event.code}` }));
};
});
}