Skip to content

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

fix(tts): settle preparation failures on the owning stream

Sebastian Bf6268a1ecd28
diff --git a/src/lib/tts/index.test.ts b/src/lib/tts/index.test.ts
index 0523837265..aab7e15ae0 100644
--- a/src/lib/tts/index.test.ts
+++ b/src/lib/tts/index.test.ts
@@ -98,6 +98,42 @@ describe("Speaker lifecycle", () => {
]);
expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:voice-test");
});
+
+ it.each([
+ ["http", "splitter unavailable"],
+ ["network", "connection lost"],
+ ["unconfigured", "Set up a voice provider in an agent profile on this computer, then pick a voice for the agent."],
+ ])("leaves preparation with an error after a %s failure", async (failure, error) => {
+ let signal: AbortSignal | undefined;
+ vi.stubGlobal("fetch", async (_input: unknown, init?: RequestInit) => {
+ signal = init?.signal ?? undefined;
+ if (failure === "network") throw new Error(error);
+ return failure === "http"
+ ? new Response(JSON.stringify({ error }), { status: 503 })
+ : json({ ready: false });
+ });
+ const speaker = new Speaker();
+
+ await speaker.speak("Fixture reply", { botId: "bot", messageId: "reply" });
+
+ expect(speaker.state).toEqual({ status: "idle", error });
+ expect(speaker.isSpeaking("reply")).toBe(false);
+ expect(signal?.aborted).toBe(true);
+ });
+
+ it("does not publish an old preparation failure over a newer stream", async () => {
+ const response = deferredResponse();
+ vi.stubGlobal("fetch", () => response.promise);
+ const speaker = new Speaker();
+ const old = speaker.speak("Old reply", { messageId: "old" });
+ const current = speaker.begin({ messageId: "current" });
+
+ response.resolve(new Response(JSON.stringify({ error: "old failure" }), { status: 503 }));
+ await old;
+
+ expect(speaker.state).toEqual({ status: "preparing", messageId: "current" });
+ current.cancel();
+ });
});
describe("external voice claims", () => {
diff --git a/src/lib/tts/index.ts b/src/lib/tts/index.ts
index 27655a44bd..ab5915f916 100644
--- a/src/lib/tts/index.ts
+++ b/src/lib/tts/index.ts
@@ -288,8 +288,9 @@ export class Speaker {
cancel: () => { if (live()) this.stop(); },
fail: (error) => {
if (finished) return;
+ const wasLive = live();
controller.abort();
- if (live()) this.set({ ...IDLE, error });
+ if (wasLive) this.set({ ...IDLE, error });
if (this.request === controller) this.request = null;
finish();
},