refactor(voice): optimize recording pipeline and improve performance

Refactor the voice recording and playback systems to improve efficiency, reduce latency, and enhance Docker build performance.

- **Infrastructure**: Optimize Dockerfiles using build mounts for pnpm cache and reorder layers for better caching of dependencies and build tools.
- **Backend/Gateway**:
  - Refactor `broadcast` module to use a generic event-based system instead of hardcoded functions.
  - Simplify voice recording logic by merging metadata and segment management into a unified `segment.ts`.
  - Optimize audio downsampling in `streamSetup.ts` using `Int16Array` views for better performance.
  - Implement `withFallback` utility for more robust Redis/Database command execution.
- **Frontend**:
  - Optimize audio playback visualization using pre-computed level shapes and efficient RMS calculation.
  - Reduce latency in voice commands by prioritizing WebSocket communication over HTTP.
  - Improve base64 encoding efficiency in audio transmission.
- **General**:
  - Add default value for `ADMIN_PASSWORD` in shared config.
  - Fix Docker healthcheck to use `127.0.0.1` instead of `localhost`.
This commit is contained in:
MythEclipse
2026-06-09 22:04:05 +07:00
parent 823b342c2c
commit f04b0f0b42
20 changed files with 447 additions and 633 deletions
+33 -98
View File
@@ -5,36 +5,19 @@
* Other modules call them to push real-time events to connected frontend clients.
*
* Usage:
* import { broadcastMessageCreated } from "../ws/broadcast.js";
* broadcastMessageCreated(messageData);
* import { broadcastEvent } from "../ws/broadcast.js";
* broadcastEvent("message_created", messageData);
*/
import { createChildLogger } from "@bete/shared/logger";
const logger = createChildLogger("broadcast");
type BroadcastFn = (data: unknown) => void;
type BroadcastRawFn = (type: string, data: unknown) => void;
type BroadcastFn = (type: string, data: unknown) => void;
type BroadcastBinaryFn = (data: Buffer) => void;
export interface BroadcastFunctions {
messageCreated: BroadcastFn;
messageUpdated: BroadcastFn;
messageDeleted: BroadcastFn;
messageAnalyzed: BroadcastFn;
attachmentCreated: BroadcastFn;
attachmentUploaded: BroadcastFn;
voiceRecordingStarted: BroadcastFn;
voiceRecordingStopped: BroadcastFn;
voiceRecordingUploaded: BroadcastFn;
voicePcmData: BroadcastFn;
voiceActiveUser: BroadcastFn;
analysisQueueStatus: BroadcastFn;
raw: BroadcastRawFn;
binary: BroadcastBinaryFn;
}
let _fns: BroadcastFunctions | null = null;
let _broadcast: BroadcastFn | null = null;
let _broadcastBinary: BroadcastBinaryFn | null = null;
let _enabled = true;
@@ -47,90 +30,42 @@ export function setBroadcastLogging(enabled: boolean): void {
* Inject broadcast functions from the WebSocket server initializer.
* Must be called once during server startup before any broadcast is used.
*/
export function setBroadcastFunctions(fns: BroadcastFunctions): void {
_fns = fns;
export function setBroadcastFunctions(
bf: BroadcastFn,
bfBinary: BroadcastBinaryFn,
): void {
_broadcast = bf;
_broadcastBinary = bfBinary;
logger.info("Broadcast functions initialized");
}
/** Clear injected functions (used during cleanup). */
export function clearBroadcastFunctions(): void {
_fns = null;
_broadcast = null;
_broadcastBinary = null;
logger.info("Broadcast functions cleared");
}
function logBroadcast(name: string, data: unknown): void {
if (!_enabled) return;
// Avoid logging binary or PCM data due to volume
if (name === "voice_pcm_data" || name === "binary") return;
logger.debug({ event: name }, "Broadcasting event");
function shouldLog(type: string): boolean {
if (!_enabled) return false;
// Avoid logging high-volume events
if (type === "voice_pcm_data") return false;
return true;
}
export const broadcastMessageCreated: BroadcastFn = (data) => {
logBroadcast("message_created", data);
_fns?.messageCreated?.(data);
};
/**
* Broadcast a JSON event to all connected WebSocket clients.
*/
export function broadcastEvent(type: string, data: unknown): void {
if (shouldLog(type)) {
logger.debug({ event: type }, "Broadcasting event");
}
_broadcast?.(type, data);
}
export const broadcastMessageUpdated: BroadcastFn = (data) => {
logBroadcast("message_updated", data);
_fns?.messageUpdated?.(data);
};
export const broadcastMessageDeleted: BroadcastFn = (data) => {
logBroadcast("message_deleted", data);
_fns?.messageDeleted?.(data);
};
export const broadcastAttachmentCreated: BroadcastFn = (data) => {
logBroadcast("attachment_created", data);
_fns?.attachmentCreated?.(data);
};
export const broadcastAttachmentUploaded: BroadcastFn = (data) => {
logBroadcast("attachment_uploaded", data);
_fns?.attachmentUploaded?.(data);
};
export const broadcastMessageAnalyzed: BroadcastFn = (data) => {
logBroadcast("message_analyzed", data);
_fns?.messageAnalyzed?.(data);
};
export const broadcastVoiceRecordingStarted: BroadcastFn = (data) => {
logBroadcast("voice_recording_started", data);
_fns?.voiceRecordingStarted?.(data);
};
export const broadcastVoiceRecordingStopped: BroadcastFn = (data) => {
logBroadcast("voice_recording_stopped", data);
_fns?.voiceRecordingStopped?.(data);
};
export const broadcastVoiceRecordingUploaded: BroadcastFn = (data) => {
logBroadcast("voice_recording_uploaded", data);
_fns?.voiceRecordingUploaded?.(data);
};
export const broadcastVoicePcmData: BroadcastFn = (data) => {
// PCM data is high-volume; logging is skipped unconditionally
_fns?.voicePcmData?.(data);
};
export const broadcastVoiceActiveUser: BroadcastFn = (data) => {
logBroadcast("voice_active_user", data);
_fns?.voiceActiveUser?.(data);
};
export const broadcastAnalysisQueueStatus: BroadcastFn = (data) => {
logBroadcast("analysis_queue_status", data);
_fns?.analysisQueueStatus?.(data);
};
export const broadcastRaw: BroadcastRawFn = (type, data) => {
logBroadcast(type, data);
_fns?.raw?.(type, data);
};
export const broadcastBinary: BroadcastBinaryFn = (data) => {
// Binary data is high-volume; logging is skipped unconditionally
_fns?.binary?.(data);
};
/**
* Broadcast binary data to all connected WebSocket clients.
*/
export function broadcastBinary(data: Buffer): void {
_broadcastBinary?.(data);
}