refactor(app): rewrite the M2 Discord voice core in Rust

The user asked for a faster, real app instead of Electron. The app is now a Rust crate (serenity +
songbird); the GUI in M5 will be a Tauri 2 shell on the system WebView2.

- Same behaviour as the Electron core: one voice channel, per-speaker PCM to the voice worker,
  speaking_end and speaker_leave, sentence playback with stop and cancel, transcription notice on
  join, slash command join/leave/stop (create-or-update), registered chat channels with live events,
  history and sends that never ping, worker process with a fresh token and restart backoff.
- songbird 0.6.1 garbles about a third of received frames under DAVE (its decoder keeps the bytes
  DAVE decryption leaves at the end of the payload; a 20 ms stepped test tone showed repeats, skips
  and reordering, while sending was exact). songbird now only decrypts; the core reorders packets in
  a small window (Opus FEC for the last lost frame) and decodes them with libopus itself.
- Live test on real Discord with two bots: five sentences heard (CER 0/0/0/5/12.5%), 0.71-0.90 s
  from end of speech to final, speech read back at 3.4%, sound gone 0.08 s after stop. The release
  binary uses about 16 MB of memory; a Windows build (MinGW) logs in under Wine.
- Spec moved to app/src/discord_core.spec.md; architecture, decisions, roadmap and pc-features updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 11:57:53 +09:00
parent 8479112ddd
commit f25c26cf53
54 changed files with 8454 additions and 5135 deletions

65
app/src/services.rs Normal file
View File

@@ -0,0 +1,65 @@
//! Starts everything the app runs: the voice worker process, the connection to it, and the Discord
//! core. Used by the app binary (and the Tauri shell from M5).
use std::path::Path;
use serde_json::Value;
use tokio::sync::mpsc;
use crate::discord::{CoreEvent, DiscordCore};
use crate::settings::load_settings;
use crate::worker_client::{WorkerClient, WorkerClientOptions};
use crate::worker_process::{ProcessEvent, WorkerProcess, WorkerProcessOptions};
use crate::debug_log;
pub struct Services {
pub core: DiscordCore,
pub events: mpsc::UnboundedReceiver<CoreEvent>,
pub worker: WorkerClient,
pub worker_process: WorkerProcess,
}
impl Services {
pub async fn stop(&self) {
self.core.close().await;
self.worker.close();
self.worker_process.stop().await;
}
}
pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::error::Error + Send + Sync>> {
let settings = load_settings(&data_dir.join("config").join("settings.json"))?;
// Until the setup wizard stores it encrypted (M5), the token comes from the environment only.
let token = std::env::var("JARVIS_DISCORD_TOKEN").map_err(|_| "JARVIS_DISCORD_TOKEN is not set")?;
let w = &settings.voice_worker;
let worker_process = WorkerProcess::new(WorkerProcessOptions::new(&w.python, &w.cwd, w.args.clone()))?;
let mut process_events = worker_process.subscribe();
worker_process.start();
let mut opts = WorkerClientOptions::new(worker_process.url(), worker_process.token.clone());
opts.stt = settings.voice.stt.clone();
opts.stt.insert("hotwords".into(), Value::from(settings.voice.wake_words.clone()));
let (worker, worker_events) = WorkerClient::new(opts);
// The worker loads its models before it listens: connect once it says READY (after restarts the
// client keeps retrying on its own).
let w2 = worker.clone();
tokio::spawn(async move {
loop {
match process_events.recv().await {
Ok(ProcessEvent::Ready) => return w2.start(),
Ok(_) => {}
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {}
Err(_) => return,
}
}
});
let (core, events) = DiscordCore::new(settings, token, worker.clone(), worker_events);
if let Err(e) = core.start().await {
worker.close();
worker_process.stop().await;
return Err(Box::new(e));
}
debug_log!("app", "services started");
Ok(Services { core, events, worker, worker_process })
}