Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Voice: render the next streamed sentence while the current one plays
AdityaUmale88d16bb40d20
diff --git a/src/lib/tts/index.test.ts b/src/lib/tts/index.test.tsindex e713717134..bd5a2a8020 100644--- a/src/lib/tts/index.test.ts+++ b/src/lib/tts/index.test.ts@@ -480,6 +480,28 @@ describe("prefetch window", () => {expect(speaker.state).toEqual({ status: "idle" });});+ it("starts rendering a streamed block while the block before it is still playing", async () => {+ installTts([]);+ const speaker = new Speaker();+ const stream = speaker.begin({ botId: "bot" });+ stream.push("B0");+ await vi.waitFor(() => expect(synthOrder).toEqual(["B0"]));+ gates[0]!.respond(clip());+ await vi.waitFor(() => expect(playedUrls).toEqual(["blob:block-1"]));++ stream.push("B1");+ await vi.waitFor(() => expect(synthOrder).toEqual(["B0", "B1"]));++ gates[1]!.respond(clip());+ FakeAudio.latest!.onended?.();+ await vi.waitFor(() => expect(playedUrls).toEqual(["blob:block-1", "blob:block-2"]));+ const ending = stream.end();+ FakeAudio.latest!.onended?.();+ await ending;++ expect(speaker.state).toEqual({ status: "idle" });+ });+it("surfaces a prefetched block's error only after earlier blocks finish playing", async () => {installTts(["B0", "B1", "B2"]);const speaker = new Speaker();diff --git a/src/lib/tts/index.ts b/src/lib/tts/index.tsindex c67725291f..ad4015caa4 100644--- a/src/lib/tts/index.ts+++ b/src/lib/tts/index.ts@@ -215,16 +215,22 @@ export class Speaker {(error: unknown) => ({ error }),);};+ // Renders not yet played, oldest first; the block playing now still holds its place in the window.+ const queued: Promise<Rendered>[] = [];+ let started = 0;+ const fill = () => {+ while (queued.length < PREFETCH_WINDOW && started < pending.length) {+ queued.push(render(pending[started]!, started));+ started += 1;+ }+ };const run = async (): Promise<void> => {- const inFlight: Promise<Rendered>[] = [];- let index = 0;+ let at = 0;let playedAll = false;for (;;) {- while (inFlight.length < PREFETCH_WINDOW && index + inFlight.length < pending.length) {- const block = index + inFlight.length;- inFlight.push(render(pending[block]!, block));- }- if (!inFlight.length) {+ fill();+ const next = queued[0];+ if (!next) {if (closed || !live()) {playedAll = closed;break;@@ -233,10 +239,7 @@ export class Speaker {wake = null;continue;}- const current = inFlight.shift()!;- const at = index;- index += 1;- const rendered = await current;+ const rendered = await next;if ("error" in rendered) {if (live()) {this.set({ ...IDLE, error: rendered.error instanceof Error ? rendered.error.message : String(rendered.error) });@@ -255,6 +258,8 @@ export class Speaker {controller.abort();return;}+ queued.shift();+ at += 1;}if (playedAll && live() && !finished) this.set(IDLE);if (this.request === controller) this.request = null;@@ -262,11 +267,13 @@ export class Speaker {const kick = () => {if (finished) return;wake?.();- if (running || (!pending.length && !closed)) return;- // A microtask lets one batch of pushes fill the prefetch window before- // the first render starts, the way the whole-text path always did.+ if (running) {+ if (live()) fill();+ return;+ }+ if (!pending.length && !closed) return;running = true;- void Promise.resolve().then(run).finally(() => {+ void run().finally(() => {running = false;finish();});