Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Fix DeepSeek streaming WAV playback
reisen-ww75512b670d61
diff --git a/docs/verification/deepseek-speech.md b/docs/verification/deepseek-speech.mdindex d9779e8f11..a0fa3d078e 100644--- a/docs/verification/deepseek-speech.md+++ b/docs/verification/deepseek-speech.md@@ -31,6 +31,11 @@ transcripts, input limits, safe errors, the Mac using the page recorder whileDeepSeek is the voice, the 115-second recording cap with 64 ms frame rounding,and the Voice settings picker.+The TTS fixture also sends a WAV with unknown RIFF and data sizes, as returned+by the streaming service, and checks the output has concrete playable sizes.+An unknown length cannot reveal a stream silently cut at a complete PCM frame;+HTTP read errors, declared length mismatches, and partial frames still fail.+Contract: [TTS](https://api-docs.deepseek.com/guides/tts/) and[ASR](https://api-docs.deepseek.com/guides/asr/).diff --git a/scripts/testing/key-readback.e2e.test.ts b/scripts/testing/key-readback.e2e.test.tsindex f2bfc18e55..c5a1af61c3 100644--- a/scripts/testing/key-readback.e2e.test.ts+++ b/scripts/testing/key-readback.e2e.test.ts@@ -98,7 +98,10 @@ beforeAll(async () => {const input = JSON.parse(raw) as { text: string };if (input.text.includes("refuse")) return reply(429, { error: input.text });res.writeHead(200, { "content-type": "audio/wav" });- res.end(pcmWav(0.25, 24_000));+ const streaming = pcmWav(0.25, 24_000);+ streaming.writeUInt32LE(0xffffffff, 4);+ streaming.writeUInt32LE(0xffffffff, 40);+ res.end(streaming);return;}return reply(200, { text: "hello from deepseek", request_id: "fixture", duration_ms: 1000 });diff --git a/server/tts/deepseek.test.ts b/server/tts/deepseek.test.tsindex d52ce6adfd..715c4b65e6 100644--- a/server/tts/deepseek.test.ts+++ b/server/tts/deepseek.test.ts@@ -83,6 +83,41 @@ describe("DeepSeek speech", () => {expect(JSON.parse(seen[0]!.body.toString())).toEqual({ text: "Hello there", voice_id: "tide", format: "wav", filter_markdown: true });});+ it("repairs unknown streaming WAV lengths after the full response is read", async () => {+ const expected = wav(0.5, 24_000);+ reply = Buffer.from(expected);+ reply.writeUInt32LE(0xffffffff, 4);+ reply.writeUInt32LE(0xffffffff, 40);+ const audio = await (await deepseek()).synthesize("hello", "stella", KEY);+ expect(Buffer.from(audio.bytes)).toEqual(expected);+ await expect((await deepseek()).transcribe(reply, KEY)).rejects.toThrow("16-bit PCM WAV");+ });++ it("handles an extra padded chunk before streaming audio", async () => {+ const source = wav(0.5, 24_000);+ const extra = Buffer.from([0x4a, 0x55, 0x4e, 0x4b, 1, 0, 0, 0, 7, 0]);+ const expected = Buffer.concat([source.subarray(0, 36), extra, source.subarray(36)]);+ expected.writeUInt32LE(expected.length - 8, 4);+ reply = Buffer.from(expected);+ reply.writeUInt32LE(0xffffffff, 4);+ reply.writeUInt32LE(0xffffffff, 50);+ expect(Buffer.from((await (await deepseek()).synthesize("hello", "stella", KEY)).bytes)).toEqual(expected);+ });++ it("rejects malformed streaming WAV headers and incomplete frames", async () => {+ const { synthesize } = await deepseek();+ const streaming = wav(0.5, 24_000);+ streaming.writeUInt32LE(0xffffffff, 4);+ streaming.writeUInt32LE(0xffffffff, 40);+ for (const invalid of [streaming.subarray(0, 44), streaming.subarray(0, 45)]) {+ reply = invalid;+ await expect(synthesize("hi", "stella", KEY)).rejects.toThrow("incomplete audio");+ }+ reply = Buffer.from(streaming);+ reply.writeUInt32LE(100_000, 16);+ await expect(synthesize("hi", "stella", KEY)).rejects.toThrow("incomplete audio");+ });+it("refuses bad input before any request", async () => {const { synthesize, transcribe, TTS_MAX_CHARS } = await deepseek();await expect(synthesize("hi", "alloy", KEY)).rejects.toThrow("Stella or Tide");diff --git a/server/tts/deepseek.ts b/server/tts/deepseek.tsindex d396dcb8b3..8b6f32a6ba 100644--- a/server/tts/deepseek.ts+++ b/server/tts/deepseek.ts@@ -124,6 +124,26 @@ export function readWav(bytes: Uint8Array): WavInfo | undefined {return undefined;}+function normalizeStreamingWav(bytes: Uint8Array): Uint8Array {+ if (bytes.byteLength < 12) return bytes;+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);+ const tag = (at: number) => String.fromCharCode(...bytes.subarray(at, at + 4));+ if (tag(0) !== "RIFF" || tag(8) !== "WAVE" || view.getUint32(4, true) !== 0xffffffff) return bytes;+ for (let at = 12; at + 8 <= bytes.byteLength;) {+ const size = view.getUint32(at + 4, true);+ const body = at + 8;+ if (tag(at) === "data" && size === 0xffffffff) {+ const normalized = Buffer.from(bytes);+ normalized.writeUInt32LE(bytes.byteLength - 8, 4);+ normalized.writeUInt32LE(bytes.byteLength - body, at + 4);+ return normalized;+ }+ if (body + size > bytes.byteLength) break;+ at = body + size + (size % 2);+ }+ return bytes;+}+export async function synthesize(text: string, voiceId: string, key: string, signal?: AbortSignal): Promise<Audio> {if (!key) throw new DeepSeekSpeechError("Add a DeepSeek API key in Settings → AI → Other services to use DeepSeek voice.", 409);if (!isDeepSeekVoice(voiceId)) throw new DeepSeekSpeechError("Choose Stella or Tide as the DeepSeek voice.", 409);@@ -141,8 +161,9 @@ export async function synthesize(text: string, voiceId: string, key: string, sigawait response.body?.cancel().catch(() => {});throw new DeepSeekSpeechError("DeepSeek returned an unexpected audio format. Try again later.");}- const bytes = await readBounded(response, MAX_AUDIO_BYTES, "speech");- if (!bytes.byteLength) throw new DeepSeekSpeechError("DeepSeek returned empty audio. Try again.");+ const received = await readBounded(response, MAX_AUDIO_BYTES, "speech");+ if (!received.byteLength) throw new DeepSeekSpeechError("DeepSeek returned empty audio. Try again.");+ const bytes = normalizeStreamingWav(received);if (!readWav(bytes)) throw new DeepSeekSpeechError("DeepSeek returned incomplete audio. Try again.");return { bytes, mime: "audio/wav" };}