- 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>
86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { once } from "node:events";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { VoiceWorkerProcess } from "../src/core/workerProcess.js";
|
|
|
|
// A stand-in `voice_worker` package run by a real Python, so the spawn line is the one the app uses.
|
|
const PYTHON = process.env.JARVIS_TEST_PYTHON ?? (process.platform === "win32" ? "python" : "python3");
|
|
const FAKE_MAIN = `
|
|
import json, os, sys, time
|
|
out = os.environ["FAKE_OUT"]
|
|
runs = len(os.listdir(out))
|
|
with open(os.path.join(out, f"run{runs}.json"), "w") as f:
|
|
json.dump({"argv": sys.argv[1:], "token": os.environ.get("JARVIS_VOICE_TOKEN")}, f)
|
|
if os.environ.get("FAKE_CRASH_FIRST") == "1" and runs == 0:
|
|
sys.exit(3)
|
|
print("loading models...", flush=True)
|
|
print("READY 127.0.0.1:" + sys.argv[sys.argv.index("--port") + 1], flush=True)
|
|
time.sleep(60)
|
|
`;
|
|
|
|
function fakeWorkerDir() {
|
|
const root = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "jarvis-fake-worker-"));
|
|
mkdirSync(join(root, "voice_worker"));
|
|
writeFileSync(join(root, "voice_worker", "__init__.py"), "");
|
|
writeFileSync(join(root, "voice_worker", "__main__.py"), FAKE_MAIN);
|
|
const out = join(root, "out");
|
|
mkdirSync(out);
|
|
return { root, out, run: (n: number) => JSON.parse(readFileSync(join(out, `run${n}.json`), "utf8")) };
|
|
}
|
|
|
|
const procs: VoiceWorkerProcess[] = [];
|
|
afterEach(async () => {
|
|
for (const p of procs.splice(0)) await p.stop(1000);
|
|
});
|
|
|
|
describe("VoiceWorkerProcess", () => {
|
|
it("runs python -m voice_worker on loopback with the token only in the environment", async () => {
|
|
const d = fakeWorkerDir();
|
|
const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: ["--melo-python", "/x/python"],
|
|
env: { FAKE_OUT: d.out } });
|
|
procs.push(p);
|
|
const ready = once(p, "ready");
|
|
await p.start();
|
|
await ready;
|
|
const run = d.run(0);
|
|
const port = new URL(p.url).port;
|
|
expect(run.argv).toEqual(["--host", "127.0.0.1", "--port", port, "--melo-python", "/x/python"]);
|
|
expect(run.token).toBe(p.token);
|
|
expect(p.token.length).toBeGreaterThanOrEqual(40);
|
|
expect(run.argv.join(" ")).not.toContain(p.token);
|
|
expect(new URL(p.url).hostname).toBe("127.0.0.1");
|
|
});
|
|
|
|
it("restarts a worker that dies, on the same port with the same token", async () => {
|
|
const d = fakeWorkerDir();
|
|
const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: [], restartDelayMs: 50,
|
|
env: { FAKE_OUT: d.out, FAKE_CRASH_FIRST: "1" } });
|
|
procs.push(p);
|
|
const exited = once(p, "exit");
|
|
const ready = once(p, "ready");
|
|
await p.start();
|
|
expect((await exited)[0]).toMatchObject({ code: 3 });
|
|
await ready;
|
|
expect(p.restarts).toBe(1);
|
|
expect(d.run(1).argv).toEqual(d.run(0).argv);
|
|
expect(d.run(1).token).toBe(d.run(0).token);
|
|
});
|
|
|
|
it("stop ends the worker for good", async () => {
|
|
const d = fakeWorkerDir();
|
|
const p = new VoiceWorkerProcess({ python: PYTHON, cwd: d.root, args: [], restartDelayMs: 20,
|
|
env: { FAKE_OUT: d.out } });
|
|
const ready = once(p, "ready");
|
|
await p.start();
|
|
await ready;
|
|
let exits = 0;
|
|
p.on("exit", () => exits++);
|
|
await p.stop(2000);
|
|
await new Promise((r) => setTimeout(r, 200));
|
|
expect(exits).toBe(1);
|
|
expect(p.restarts).toBe(0);
|
|
});
|
|
});
|