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
+6
View File
@@ -11,6 +11,7 @@
type BroadcastFn = (data: unknown) => void;
type BroadcastRawFn = (type: string, data: unknown) => void;
type BroadcastBinaryFn = (data: Buffer) => void;
declare global {
// biome-ignore lint/suspicious/noAssignInExpressions: intentional global broadcast registry
@@ -21,12 +22,14 @@ declare global {
messageDeleted: BroadcastFn;
attachmentUploaded: BroadcastFn;
raw: BroadcastRawFn;
binary: BroadcastBinaryFn;
}
| undefined;
}
const noop: BroadcastFn = () => {};
const noopRaw: BroadcastRawFn = () => {};
const noopBinary: BroadcastBinaryFn = () => {};
export const broadcastMessageCreated: BroadcastFn = (data) =>
(globalThis.__broadcastFns?.messageCreated ?? noop)(data);
@@ -42,3 +45,6 @@ export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
export const broadcastRaw: BroadcastRawFn = (type, data) =>
(globalThis.__broadcastFns?.raw ?? noopRaw)(type, data);
export const broadcastBinary: BroadcastBinaryFn = (data) =>
(globalThis.__broadcastFns?.binary ?? noopBinary)(data);
+45
View File
@@ -25,8 +25,12 @@ const SUBSCRIPTIONS: ChannelMapping[] = [
channel: "discord:analysis:queue_status",
eventType: "analysis_queue_status",
},
{ channel: "discord:voice:active_user", eventType: "voice_active_user" },
];
// Binary channels that need special handling (messageBuffer event)
const BINARY_CHANNELS = ["discord:voice:pcm"];
let subscriber: Redis | null = null;
function createSubscriber(): Redis {
@@ -90,6 +94,41 @@ function handleSubscriptionMessage(channel: string, message: string): void {
broadcastRaw(mapping.eventType, data);
}
/**
* Handle binary messages from Redis (e.g. voice PCM data).
* Expected format: 4-byte userId hash + PCM buffer
*/
function handleBinaryMessage(channel: Buffer, message: Buffer): void {
const channelStr = channel.toString();
if (channelStr === "discord:voice:pcm") {
if (message.length < 4) {
logger.warn(
{ channel: channelStr, size: message.length },
"Received PCM message too short to contain userId",
);
return;
}
// First 4 bytes = userId hash, rest = PCM data
const userIdHash = message.readUInt32LE(0);
const pcmData = message.subarray(4);
logger.debug(
{ channel: channelStr, userIdHash, pcmSize: pcmData.length },
"Broadcasting voice PCM data",
);
// Broadcast as binary: userId (4 bytes) + PCM data
broadcastRaw("voice_pcm", message);
} else {
logger.warn(
{ channel: channelStr },
"Received binary message for unmapped channel",
);
}
}
export async function startRedisBridge(): Promise<void> {
if (!config.REDIS_URL && !config.REDIS_HOST) {
logger.info("Redis not configured, skipping Redis bridge");
@@ -116,6 +155,7 @@ export async function startRedisBridge(): Promise<void> {
});
subscriber.on("message", handleSubscriptionMessage);
subscriber.on("messageBuffer", handleBinaryMessage);
await subscriber.ping();
logger.info("Redis ping OK");
@@ -124,6 +164,11 @@ export async function startRedisBridge(): Promise<void> {
await subscriber.subscribe(...channels);
logger.info({ channels }, "Subscribed to Redis channels");
if (BINARY_CHANNELS.length > 0) {
await subscriber.subscribe(...BINARY_CHANNELS);
logger.info({ channels: BINARY_CHANNELS }, "Subscribed to binary Redis channels");
}
logger.info("Redis bridge started");
} catch (err) {
logger.error({ err }, "Failed to start Redis bridge");
+14 -13
View File
@@ -1,6 +1,6 @@
import type { Server } from "node:http";
import { WebSocket, WebSocketServer } from "ws";
import { createChildLogger } from "@bete/shared/logger";
import { WebSocket, WebSocketServer } from "ws";
const logger = createChildLogger("ws.server");
@@ -10,18 +10,6 @@ interface BroadcastEvent {
timestamp: string;
}
declare global {
var __broadcastFns:
| {
messageCreated: (data: unknown) => void;
messageUpdated: (data: unknown) => void;
messageDeleted: (data: unknown) => void;
attachmentUploaded: (data: unknown) => void;
raw: (type: string, data: unknown) => void;
}
| undefined;
}
async function sendInitialStates(ws: WebSocket): Promise<void> {
// Send initial user state
ws.send(
@@ -128,6 +116,18 @@ export function createWebSocketServer(server: Server): WebSocketServer {
}
}
function broadcastRaw(data: Buffer) {
for (const client of clients) {
if (client.readyState === WebSocket.OPEN) {
try {
client.send(data);
} catch (err) {
logger.error({ err }, "Failed to broadcast binary data to client");
}
}
}
}
globalThis.__broadcastFns = {
messageCreated: (data: unknown) =>
broadcast({ type: "message_created", data }),
@@ -138,6 +138,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
attachmentUploaded: (data: unknown) =>
broadcast({ type: "attachment_uploaded", data }),
raw: (type: string, data: unknown) => broadcast({ type, data }),
binary: broadcastRaw,
};
// Cleanup on close