- app/ (Electron 45 + TypeScript): the core joins one voice channel, decodes each speaker's Opus to 48 kHz stereo and streams it to the voice worker, sends speaking_end and speaker_leave, and emits speech_start/partial/final with the speaker's display name. - Sentence playback from the worker's TTS, first come first served, with stop that silences the clip at once and cancels unfinished requests; slash command join/leave/stop (create-or-update by name). - Transcription notice posted on every join; other bots ignored by default; at most maxSpeakers people transcribed at once. - Registered chat channels: live message events, history reads, sends split at 2,000 characters with mentions disabled; Message Content intent checked before login. - The app starts the Python voice worker with a fresh token in the environment, restarts it with backoff, and the worker client reconnects and fails in-flight TTS on a lost connection. - Spec in app/src/core/discord_core.spec.md; Vitest unit tests, a protocol test against the real Python worker server with fake engines, and an opt-in live test with two bots on real Discord. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
4.3 KiB
TypeScript
90 lines
4.3 KiB
TypeScript
/**
|
|
* The app's worker client against the real Python voice worker server (with fake engines), so both
|
|
* sides of the protocol are checked together. Needs a Python with the worker's requirements:
|
|
* JARVIS_VOICE_PYTHON=<venv python> npm test
|
|
*/
|
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
import { createInterface } from "node:readline";
|
|
import { fileURLToPath } from "node:url";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { Playback } from "../src/core/playback.js";
|
|
import { VoiceWorkerClient, type Final, type TtsDone } from "../src/core/workerClient.js";
|
|
import { freePort } from "../src/core/workerProcess.js";
|
|
|
|
const PYTHON = process.env.JARVIS_VOICE_PYTHON;
|
|
const SRC = fileURLToPath(new URL("../../src", import.meta.url));
|
|
const FIXTURE = fileURLToPath(new URL("./fixtures/fake_engine_worker.py", import.meta.url));
|
|
const TOKEN = "protocol-test-token";
|
|
const FMT = { rate: 48000, channels: 2 } as const;
|
|
|
|
/** 20 ms of a 48 kHz stereo tone whose amplitude and pitch (x1000 Hz) are `level`. */
|
|
function frame(level: number): Buffer {
|
|
const n = 960;
|
|
const buf = Buffer.alloc(n * 4);
|
|
for (let i = 0; i < n; i++) {
|
|
const v = Math.round(level * Math.sin((2 * Math.PI * level * 1000 * i) / 48000) * 32767);
|
|
buf.writeInt16LE(v, i * 4);
|
|
buf.writeInt16LE(v, i * 4 + 2);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
describe.skipIf(!PYTHON)("worker protocol against the Python server", () => {
|
|
let child: ChildProcess;
|
|
let client: VoiceWorkerClient;
|
|
|
|
beforeAll(async () => {
|
|
const port = await freePort();
|
|
child = spawn(PYTHON!, [FIXTURE, String(port)], {
|
|
env: { ...process.env, PYTHONPATH: SRC, JARVIS_VOICE_TOKEN: TOKEN },
|
|
stdio: ["ignore", "pipe", "inherit"],
|
|
});
|
|
await new Promise<void>((resolve, reject) => {
|
|
child.once("exit", (c) => reject(new Error(`fake worker exited ${c}`)));
|
|
createInterface({ input: child.stdout! }).on("line", (l) => l.startsWith("READY") && resolve());
|
|
});
|
|
client = new VoiceWorkerClient({ url: `ws://127.0.0.1:${port}`, token: TOKEN, audio: FMT, tts: FMT,
|
|
stt: { partials: false, hotwords: ["자비스"] } });
|
|
client.start();
|
|
const ok = await client.waitReady(5000);
|
|
expect(ok).toMatchObject({ stt_ready: true, tts_ready: true });
|
|
expect(ok.stt).toMatchObject({ partials: false, hotwords: ["자비스"] });
|
|
});
|
|
|
|
afterAll(() => {
|
|
client?.close();
|
|
child?.kill();
|
|
});
|
|
|
|
it("Discord-format audio from two speakers comes back as each speaker's final", async () => {
|
|
const finals: Final[] = [];
|
|
client.on("final", (f) => finals.push(f));
|
|
for (let i = 0; i < 30; i++) {
|
|
client.sendAudio("111111111111111111", frame(0.9));
|
|
client.sendAudio("222222222222222222", frame(0.6));
|
|
}
|
|
client.speakingEnd("111111111111111111");
|
|
// The second speaker leaves mid-utterance: the final still comes.
|
|
client.speakerLeave("222222222222222222");
|
|
const end = Date.now() + 5000;
|
|
while (finals.length < 2 && Date.now() < end) await new Promise((r) => setTimeout(r, 20));
|
|
const bySpeaker = Object.fromEntries(finals.map((f) => [f.speaker, f]));
|
|
expect(bySpeaker["111111111111111111"]).toMatchObject({ text: "level 0.9", end_reason: "speaking_end", utt: 1 });
|
|
expect(bySpeaker["222222222222222222"]).toMatchObject({ text: "level 0.6", end_reason: "speaking_end", utt: 1 });
|
|
});
|
|
|
|
it("TTS sentences play in order through Playback and the request ends cleanly", async () => {
|
|
const played: Buffer[] = [];
|
|
const playback = new Playback(client, { play: (pcm) => { played.push(pcm); setTimeout(() => playback.clipEnded(), 5); }, stop: () => {} });
|
|
client.on("tts_audio", (f) => playback.audio(f.id, f.seq, f.pcm));
|
|
client.on("tts_done", (d: TtsDone) => playback.done(d));
|
|
const ended = new Promise((resolve) => playback.once("end", resolve));
|
|
const text = "첫 번째 문장입니다. 두 번째 문장은 조금 더 깁니다.";
|
|
playback.say(text);
|
|
expect(await ended).toMatchObject({ played: 2, cancelled: false, failed: false });
|
|
// 10 samples per character at 48 kHz, mono duplicated to stereo, 2 bytes each.
|
|
const sentences = ["첫 번째 문장입니다.", "두 번째 문장은 조금 더 깁니다."];
|
|
expect(played.map((p) => p.length)).toEqual(sentences.map((s) => s.length * 10 * 2 * 2));
|
|
});
|
|
});
|