Skip to content

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

fix(tts): stop failed stream audio before publishing idle

Sebastian B0a98081dbb92
diff --git a/src/lib/tts/index.test.ts b/src/lib/tts/index.test.ts
index aab7e15ae0..10225bd8fb 100644
--- a/src/lib/tts/index.test.ts
+++ b/src/lib/tts/index.test.ts
@@ -134,6 +134,48 @@ describe("Speaker lifecycle", () => {
expect(speaker.state).toEqual({ status: "preparing", messageId: "current" });
current.cancel();
});
+
+ it("stops active audio before publishing a stream failure", async () => {
+ vi.stubGlobal("fetch", async () => new Response(new Blob(["mp3"])));
+ const speaker = new Speaker();
+ const stream = speaker.begin({ messageId: "reply" });
+ stream.push("Playing block.");
+ await vi.waitFor(() => expect(FakeAudio.latest).not.toBeNull());
+ const audio = FakeAudio.latest!;
+ let sourceAtFailure: string | undefined;
+ speaker.subscribe(snapshot => {
+ if (snapshot.error) sourceAtFailure = audio.src;
+ });
+
+ stream.fail("splitter unavailable");
+ await stream.end();
+
+ expect(sourceAtFailure).toBe("");
+ expect(audio.pause).toHaveBeenCalled();
+ expect(audio.onended).toBeNull();
+ expect(audio.onerror).toBeNull();
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:voice-test");
+ expect(speaker.state).toEqual({ status: "idle", error: "splitter unavailable" });
+ });
+
+ it("does not stop a newer stream's audio when an old stream fails", async () => {
+ vi.stubGlobal("fetch", async () => new Response(new Blob(["mp3"])));
+ const speaker = new Speaker();
+ const old = speaker.begin({ messageId: "old" });
+ const current = speaker.begin({ messageId: "current" });
+ current.push("Current block.");
+ await vi.waitFor(() => expect(FakeAudio.latest).not.toBeNull());
+ const audio = FakeAudio.latest!;
+
+ old.fail("old failure");
+ await old.end();
+
+ expect(audio.src).toBe("blob:voice-test");
+ expect(audio.pause).not.toHaveBeenCalled();
+ expect(speaker.state).toMatchObject({ status: "speaking", messageId: "current" });
+ audio.onended?.();
+ await current.end();
+ });
});
describe("external voice claims", () => {
diff --git a/src/lib/tts/index.ts b/src/lib/tts/index.ts
index ab5915f916..803b65e033 100644
--- a/src/lib/tts/index.ts
+++ b/src/lib/tts/index.ts
@@ -290,7 +290,11 @@ export class Speaker {
if (finished) return;
const wasLive = live();
controller.abort();
- if (wasLive) this.set({ ...IDLE, error });
+ if (wasLive) {
+ if (this.settlePlayback) this.settlePlayback(false);
+ else this.teardownAudio();
+ this.set({ ...IDLE, error });
+ }
if (this.request === controller) this.request = null;
finish();
},