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
@@ -68,16 +68,26 @@ export default async function workerRouter(
job: WorkerJob,
): Promise<WorkerResponse> {
if (!config.AI_LLM_API_KEY) {
const errorMsg =
"AI_LLM_API_KEY is missing from environment. Worker cannot process moderation requests without credentials.";
console.error(
JSON.stringify({
level: "FATAL",
level: "ERROR",
context: "aiAnalysisWorker",
error:
"AI_LLM_API_KEY is missing from environment. Force closing worker operation.",
error: errorMsg,
timestamp: new Date().toISOString(),
}),
);
process.exit(1);
if (job.type === "batch") {
return {
ok: false,
conversationKey: job.conversationKey,
rows: [],
error: errorMsg,
};
}
return { ok: false, results: [], error: errorMsg };
}
try {
@@ -3,6 +3,7 @@ import { availableParallelism } from "node:os";
import { fileURLToPath } from "node:url";
import { createChildLogger } from "@bete/shared/logger";
import type { Client } from "discord.js-selfbot-v13";
import { LRUCache } from "lru-cache";
import { Piscina } from "piscina";
import { config } from "../../shared/config/config.js";
import type { EventBroadcaster } from "../event-broadcaster/index.js";
@@ -72,7 +73,7 @@ function scheduleAutoDelete(row: MessageRecord): void {
);
return;
}
autoDeleteInFlight.add(row.id);
autoDeleteInFlight.set(row.id, true);
const run = () => {
attemptAutoDeleteFlaggedMessage(moderationClient, row)
@@ -153,15 +154,20 @@ async function skipAgeRestrictedMessages(
}
// ---------------------------------------------------------------------------
// Batch pipeline state
// Batch pipeline state (with LRU eviction to prevent unbounded memory growth)
// ---------------------------------------------------------------------------
/** Debounce timer handle per conversation key. */
const conversationDebounceTimers = new Map<string, NodeJS.Timeout>();
const conversationDebounceTimers = new LRUCache<string, NodeJS.Timeout>({
max: 10000,
dispose: (value) => {
clearTimeout(value);
},
});
/** Timestamp of when processing started per conversation key. */
const conversationProcessing = new Map<string, number>();
const conversationProcessing = new LRUCache<string, number>({ max: 10000 });
/** Cooldown expiry timestamp per conversation key after an error. */
const conversationErrorCooldown = new Map<string, number>();
const conversationErrorCooldown = new LRUCache<string, number>({ max: 10000 });
/**
* Per-message in-flight guard for the auto-delete side-effect.
@@ -170,15 +176,18 @@ const conversationErrorCooldown = new Map<string, number>();
* races through both paths, without this guard two concurrent
* `attemptAutoDeleteFlaggedMessage` calls would be launched — producing a
* duplicate moderation-action log and an unnecessary Discord 10008 error.
* (LRU-backed to prevent unbounded growth from message IDs accumulating forever)
*/
const autoDeleteInFlight = new Set<string>();
const autoDeleteInFlight = new LRUCache<string, true>({ max: 10000 });
let activeRequests = 0;
let lastError: string | null = null;
let moderationClient: Client | undefined;
// Batch circuit breaker
const conversationConsecutiveErrors = new Map<string, number>();
// Batch circuit breaker (LRU-backed to prevent unbounded growth)
const conversationConsecutiveErrors = new LRUCache<string, number>({
max: 10000,
});
const MAX_CONSECUTIVE_ERRORS = 5;
const CONVERSATION_CB_COOLDOWN_MS = 60000;
@@ -250,20 +259,26 @@ function resetConversationBatchFailures(conversationKey: string): void {
// that already have individual work in progress (#4 fix).
// • A separate circuit breaker prevents a cascade of individual failures
// from hammering a down/rate-limited LLM endpoint (#1+#5 fix).
// • All collections use LRU eviction to prevent unbounded memory growth.
// ---------------------------------------------------------------------------
/** IDs currently being processed one-by-one. */
const individualInFlight = new Set<string>();
/** IDs currently being processed one-by-one (LRU-backed, max 10k entries). */
const individualInFlight = new LRUCache<string, true>({ max: 10000 });
/**
* Per-conversation count of in-flight individual messages.
* Used by the recovery worker to avoid re-scheduling a conversation that
* already has individual fallback work running for it.
* (LRU-backed to prevent unbounded growth)
*/
const individualInFlightByConversation = new Map<string, number>();
const individualInFlightByConversation = new LRUCache<string, number>({
max: 10000,
});
/** Last-touched timestamp for pruning stale entries. */
const individualInFlightLastTouched = new Map<string, number>();
/** Last-touched timestamp for pruning stale entries (LRU-backed). */
const individualInFlightLastTouched = new LRUCache<string, number>({
max: 10000,
});
/** Counter for observability. */
let activeIndividualRequests = 0;
@@ -659,7 +674,7 @@ function enqueueIndividualFallbacks(messages: MessageRecord[]): void {
);
for (const msg of newMessages) {
individualInFlight.add(msg.id);
individualInFlight.set(msg.id, true);
// Fire-and-forget: processIndividualFallback handles all errors internally.
processIndividualFallback(msg).catch((err: unknown) => {
// Belt-and-suspenders guard — should never reach here.
@@ -387,7 +387,7 @@ export class CommandHandler {
private publishMediaStatus(): void {
const status: MediaStatusPayload = {
playing: discordPlayer.getStatus(),
playing: String(discordPlayer.getStatus()),
musicVolume: discordPlayer.getMusicVolume(),
current: null,
queue: [],
@@ -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;
@@ -26,16 +26,12 @@ export class PacketFilter extends Transform {
this.push(chunk);
} else {
this.filteredCount++;
if (this.filteredCount % 10 === 0) {
// console.log(`[packet-filter] Filtered ${this.filteredCount} small packets (size < ${this.minPacketSize} bytes)`);
}
}
callback();
}
_flush(callback: TransformCallback): void {
// console.log(`[packet-filter] Total packets: ${this.totalCount}, filtered: ${this.filteredCount}, passed: ${this.totalCount - this.filteredCount}`);
callback();
}
}
@@ -1,4 +1,5 @@
import { Readable } from "node:stream";
import { createChildLogger } from "@bete/shared/logger";
import {
AudioPlayer,
AudioPlayerStatus,
@@ -10,6 +11,8 @@ import {
} from "@discordjs/voice";
import type { DiscordPlayerOwner, DiscordPlayOptions } from "./mediaTypes.js";
const logger = createChildLogger("player");
export class DiscordPlayer {
private player: AudioPlayer;
private connection: VoiceConnection | null = null;
@@ -21,11 +24,11 @@ export class DiscordPlayer {
this.player = createAudioPlayer();
this.player.on(AudioPlayerStatus.Playing, () => {
console.log("[player] Audio player is now playing!");
logger.info("Audio player is now playing!");
});
this.player.on("error", (error) => {
console.error(`[player] Error: ${error.message}`);
logger.error({ error: error.message }, "Audio player error");
this.owner = "none";
this.resource = null;
});
@@ -1,4 +1,4 @@
import fs from "node:fs";
import fs, { promises as fsPromises } from "node:fs";
import path from "node:path";
import { createChildLogger } from "@bete/shared/logger";
import { retryWithBackoff } from "@bete/shared/utils";
@@ -13,7 +13,7 @@ import {
} from "@discordjs/voice";
import type { Client, VoiceChannel } from "discord.js-selfbot-v13";
import { config } from "../../shared/config/config.js";
import type { PcmBroadcaster } from "../message-capture/types.js";
import type { EventBroadcaster } from "../event-broadcaster/eventBroadcaster.js";
import { PacketFilter } from "./packetFilter.js";
import { OpusDecoder } from "./recorder/decoder.js";
import {
@@ -30,12 +30,25 @@ import { uploadRecordingSegment } from "./recorder/uploader.js";
const logger = createChildLogger("recorder");
let _eventBroadcaster: EventBroadcaster | undefined;
export function setEventBroadcaster(broadcaster: EventBroadcaster | undefined) {
_eventBroadcaster = broadcaster;
}
const recordingsDir = config.RECORDINGS_DIR;
// Pastikan folder recordings ada
if (!fs.existsSync(recordingsDir)) {
fs.mkdirSync(recordingsDir, { recursive: true });
}
(async () => {
try {
await fsPromises.mkdir(recordingsDir, { recursive: true });
} catch (error) {
// Directory might already exist, that's fine
if ((error as NodeJS.ErrnoException).code !== "EEXIST") {
logger.error({ error }, "Failed to create recordings directory");
}
}
})();
const activeSessions = new Map<string, RecordingSession>();
@@ -115,7 +128,6 @@ export async function startRecording(
}
const receiver = connection.receiver;
const broadcaster = globalThis as typeof globalThis & PcmBroadcaster;
// Dengarkan siapapun yang mulai bicara
receiver.speaking.on("start", async (userId) => {
@@ -130,7 +142,7 @@ export async function startRecording(
);
// Notify webserver
broadcaster.updateActiveUser?.(userId, {
_eventBroadcaster?.voiceActiveUser(userId, {
username: userMetadata.username,
avatar: userMetadata.avatarUrl,
speaking: true,
@@ -140,9 +152,9 @@ export async function startRecording(
if (receiver.subscriptions.has(userId)) return;
const userDir = path.join(recordingsDir, userId);
if (!fs.existsSync(userDir)) {
fs.mkdirSync(userDir, { recursive: true });
}
await fsPromises.mkdir(userDir, { recursive: true }).catch(() => {
// Directory already exists, ignore
});
try {
// --- OGG file recording with segment rotation ---
@@ -166,13 +178,12 @@ export async function startRecording(
cooldownMs: config.DECODER_COOLDOWN_MS,
rotateMs: config.DECODER_ROTATE_MS,
onData: (pcm) => {
if (!broadcaster.broadcastPcmToWeb) return;
// Downsample 48kHz stereo → 24kHz mono (left channel, every 2nd sample)
const outBuf = Buffer.alloc(pcm.length / 4);
for (let i = 0; i < outBuf.length / 2; i++) {
outBuf.writeInt16LE(pcm.readInt16LE(i * 8), i * 2);
}
broadcaster.broadcastPcmToWeb(outBuf, userId);
_eventBroadcaster?.voicePcmData(outBuf, userId);
},
});
@@ -200,16 +211,25 @@ export async function startRecording(
activeSession?.startTime ?? 0,
config.RECORDING_SEGMENT_MS,
);
fs.writeFileSync(
currentSegment.jsonFilename,
JSON.stringify(metadata, null, 2),
);
if (config.VERBOSE) {
logger.info(
{ jsonFile: currentSegment.jsonFilename },
"Metadata saved",
);
}
fsPromises
.writeFile(
currentSegment.jsonFilename,
JSON.stringify(metadata, null, 2),
)
.then(() => {
if (config.VERBOSE) {
logger.info(
{ jsonFile: currentSegment.jsonFilename },
"Metadata saved",
);
}
})
.catch((err: unknown) => {
logger.error(
{ error: err instanceof Error ? err.message : String(err) },
"Failed to write segment metadata",
);
});
// Trigger async voice segment upload
const segmentId = `${userId}-${currentSegment.startTime}`;
@@ -240,7 +260,6 @@ export async function startRecording(
audioStream.on("data", (chunk: Buffer) => {
if (chunk.length < 8) return;
segmentManager.rotateIfNeeded(oggPacketStream);
if (!broadcaster.broadcastPcmToWeb) return;
decoder.rotateIfNeeded();
decoder.write(chunk);
});
@@ -248,7 +267,7 @@ export async function startRecording(
audioStream.on("end", () => {
segmentManager.close(oggPacketStream);
decoder.destroy();
broadcaster.updateActiveUser?.(userId, {
_eventBroadcaster?.voiceActiveUser(userId, {
username: userMetadata.username,
avatar: userMetadata.avatarUrl,
speaking: false,
@@ -1,8 +1,10 @@
import { createRequire } from "node:module";
import { createChildLogger } from "@bete/shared/logger";
import * as prism from "prism-media";
import { config } from "../../../shared/config/config.js";
const require = createRequire(import.meta.url);
const logger = createChildLogger("opus-decoder");
interface OpusDecoderRuntime {
isBun: boolean;
@@ -84,10 +86,7 @@ export class OpusDecoder {
try {
decoder.write(chunk);
} catch (error) {
console.warn(
"[recorder] Opus decoder write failed, cooling down:",
error,
);
logger.warn({ error }, "Opus decoder write failed, cooling down");
this.coolDown();
}
}
@@ -107,14 +106,14 @@ export class OpusDecoder {
const decoder = this.createDecoderFn();
decoder.on("data", this.onData);
decoder.on("error", (error) => {
console.warn("[recorder] Opus decoder error, cooling down:", error);
logger.warn({ error }, "Opus decoder error, cooling down");
this.coolDown();
});
this.decoder = decoder;
this.createdAt = Date.now();
return decoder;
} catch (error) {
console.warn("[recorder] Opus decoder init failed, cooling down:", error);
logger.warn({ error }, "Opus decoder init failed, cooling down");
this.disabledUntil = Date.now() + this.cooldownMs;
return null;
}
@@ -1,4 +1,4 @@
import fs from "node:fs";
import fs, { promises as fsPromises } from "node:fs";
import path from "node:path";
import type { UserMetadata } from "../../message-capture/types.js";
import {
@@ -71,8 +71,11 @@ export interface RecordingSession {
export interface FinalizeRecordingSessionDependencies {
endTime?: number;
mkdir?: (dir: string) => void;
writeJson?: (file: string, metadata: SessionRecordingMetadata) => void;
mkdir?: (dir: string) => Promise<void>;
writeJson?: (
file: string,
metadata: SessionRecordingMetadata,
) => Promise<void>;
runFfmpeg?: (args: string[]) => Promise<void>;
}
@@ -153,18 +156,18 @@ export async function finalizeRecordingSession(
const outputFile = path.join(sessionDir, "full.ogg");
const metadataFile = path.join(sessionDir, "session.json");
const mkdir =
dependencies.mkdir ?? ((dir) => fs.mkdirSync(dir, { recursive: true }));
dependencies.mkdir ?? ((dir) => fsPromises.mkdir(dir, { recursive: true }));
const writeJson =
dependencies.writeJson ??
((file, metadata) =>
fs.writeFileSync(file, JSON.stringify(metadata, null, 2)));
fsPromises.writeFile(file, JSON.stringify(metadata, null, 2)));
const runFfmpeg = dependencies.runFfmpeg ?? defaultRunFfmpeg;
mkdir(sessionDir);
await mkdir(sessionDir);
const metadata = session.snapshot(endTime);
if (metadata.segments.length === 0) {
writeJson(metadataFile, { ...metadata, status: "empty" });
await writeJson(metadataFile, { ...metadata, status: "empty" });
return;
}
@@ -177,13 +180,13 @@ export async function finalizeRecordingSession(
codec: "libopus",
}),
);
writeJson(metadataFile, {
await writeJson(metadataFile, {
...metadata,
status: "completed",
outputFile,
});
} catch (error) {
writeJson(metadataFile, {
await writeJson(metadataFile, {
...metadata,
status: "failed",
error: error instanceof Error ? error.message : String(error),