fix(voice): restore voice features and apply critical optimizations

Voice Feature Restoration:
- Implemented full Redis pub/sub pipeline for real-time voice data
- Added VOICE_PCM and VOICE_ACTIVE_USER Redis channels
- Implemented EventBroadcaster.voicePcmData() and voiceActiveUser() methods
- Extended backend redis-bridge to subscribe to voice channels
- Updated backend WebSocket server for binary PCM broadcast
- Replaced globalThis PcmBroadcaster pattern with proper EventBroadcaster DI
- Fixed root cause: PcmBroadcaster functions were never initialized

Bug Fixes:
- Fixed prism-media version conflict (2.0.0-alpha.0 → 1.3.5)
- Fixed type inconsistency in commandHandler.ts (AudioPlayerStatus → string)

Critical Optimizations:
- P1.1: Fixed unbounded memory growth in aiAnalyzer (added LRU caching, max 10K entries)
- P1.2: Converted sync file I/O to async in audio hot paths (recorder, sessionRecording)
- P1.3: Replaced process.exit(1) with proper error handling (bootstrap, aiAnalysisWorker)

Code Quality:
- Removed unused logger field in EventBroadcaster
- Replaced console.* with structured logger.* calls (player, decoder)
- Fixed typos and removed commented debug code
- Added DatabaseError class for better error handling

Files Modified: 18 (discord-gateway: 14, backend: 3, root: 1)
Architecture: Discord → EventBroadcaster → Redis → Backend WebSocket → Frontend

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-08 19:14:34 +07:00
co-authored by Claude Opus 4.8
parent 2643a3c278
commit 13a75a5101
18 changed files with 243 additions and 91 deletions
@@ -43,11 +43,9 @@ export class RedisEventPublisher {
export class EventBroadcaster {
private publisher: RedisEventPublisher;
private logger: CustomLogger;
constructor(publisher: RedisEventPublisher, logger: CustomLogger) {
constructor(publisher: RedisEventPublisher) {
this.publisher = publisher;
this.logger = logger;
}
async messageCreated(data: unknown): Promise<void> {
@@ -131,6 +129,49 @@ export class EventBroadcaster {
});
}
/**
* Broadcasts PCM audio data for real-time voice streaming
* @param pcmBuffer - Raw PCM audio buffer
* @param userId - Discord user ID
* @param metadata - Optional metadata about the audio chunk
*/
async voicePcmData(
pcmBuffer: Buffer,
userId: string,
metadata?: any,
): Promise<void> {
await this.publisher.publish("discord:voice:pcm", {
type: "voice_pcm_data",
data: {
userId,
pcm: pcmBuffer.toString("base64"),
metadata,
},
timestamp: Date.now(),
source: "discord-gateway",
});
}
/**
* Broadcasts voice user activity state changes
* @param userId - Discord user ID
* @param data - User state data including username, avatar, and speaking status
*/
async voiceActiveUser(
userId: string,
data: { username: string; avatar: string; speaking: boolean },
): Promise<void> {
await this.publisher.publish("discord:voice:active_user", {
type: "voice_active_user",
data: {
userId,
...data,
},
timestamp: Date.now(),
source: "discord-gateway",
});
}
async analysisQueueStatus(data: unknown): Promise<void> {
await this.publisher.publish("discord:analysis:queue_status", {
type: "analysis_queue_status",
@@ -15,6 +15,9 @@ export const EventChannels = {
VOICE_STARTED: "discord:voice:started",
VOICE_STOPPED: "discord:voice:stopped",
VOICE_UPLOADED: "discord:voice:uploaded",
// Real-time voice streaming channels
VOICE_ACTIVE_USER: "discord:voice:active_user", // Active speaker state updates
VOICE_PCM: "discord:voice:pcm", // Live PCM audio data stream
ANALYSIS_QUEUE_STATUS: "discord:analysis:queue_status",
} as const;