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:
@@ -4,6 +4,7 @@ import {
|
||||
COMMAND_VOICE_CHANNELS,
|
||||
COMMAND_VOICE_CONNECT,
|
||||
COMMAND_VOICE_DISCONNECT,
|
||||
CommandReply,
|
||||
VOICE_STATUS_KEY,
|
||||
} from "@bete/shared";
|
||||
import {
|
||||
@@ -41,6 +42,18 @@ export const DEFAULT_VOICE_STATUS: VoiceStatus = {
|
||||
activeChannelName: null,
|
||||
};
|
||||
|
||||
/**
|
||||
* Wraps tryCommandThenFallback with a cleaner signature for use within this module.
|
||||
* Attempts a Redis command first; on failure, falls back to the provided function.
|
||||
*/
|
||||
async function withFallback<T>(
|
||||
commandFn: () => Promise<CommandReply<T> | null>,
|
||||
fallbackFn: () => Promise<T>,
|
||||
name: string,
|
||||
): Promise<T> {
|
||||
return tryCommandThenFallback(commandFn, fallbackFn, name);
|
||||
}
|
||||
|
||||
function readVoiceStatusFallback(): Promise<VoiceStatus> {
|
||||
return readRedisStatus(VOICE_STATUS_KEY).then(
|
||||
(cached) => (cached as unknown as VoiceStatus) ?? DEFAULT_VOICE_STATUS,
|
||||
@@ -53,7 +66,7 @@ function readVoiceStatusFallback(): Promise<VoiceStatus> {
|
||||
*/
|
||||
export async function getGuilds(): Promise<Guild[]> {
|
||||
logger.info("getGuilds called");
|
||||
return tryCommandThenFallback(
|
||||
return withFallback(
|
||||
() => publishCommand<Guild[]>(COMMAND_GUILDS_LIST, {}),
|
||||
async () => {
|
||||
const pool = getPool();
|
||||
@@ -76,7 +89,7 @@ export async function getGuilds(): Promise<Guild[]> {
|
||||
*/
|
||||
export async function getTextChannels(guildId: string): Promise<Channel[]> {
|
||||
logger.info({ guildId }, "getTextChannels called");
|
||||
return tryCommandThenFallback(
|
||||
return withFallback(
|
||||
() => publishCommand<Channel[]>(COMMAND_GUILDS_TEXT_CHANNELS, { guildId }),
|
||||
async () => {
|
||||
const pool = getPool();
|
||||
@@ -122,7 +135,7 @@ export async function connectVoice(
|
||||
channelId: string,
|
||||
): Promise<VoiceStatus> {
|
||||
logger.info({ guildId, channelId }, "connectVoice called");
|
||||
return tryCommandThenFallback(
|
||||
return withFallback(
|
||||
() =>
|
||||
publishCommand<VoiceStatus>(COMMAND_VOICE_CONNECT, {
|
||||
guildId,
|
||||
@@ -138,7 +151,7 @@ export async function connectVoice(
|
||||
*/
|
||||
export async function disconnectVoice(): Promise<VoiceStatus> {
|
||||
logger.info("disconnectVoice called");
|
||||
return tryCommandThenFallback(
|
||||
return withFallback(
|
||||
() => publishCommand<VoiceStatus>(COMMAND_VOICE_DISCONNECT, {}),
|
||||
() => readVoiceStatusFallback(),
|
||||
"disconnectVoice",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
import { createChildLogger } from "@bete/shared/logger";
|
||||
import Redis from "ioredis";
|
||||
import { config } from "../shared/config/index.js";
|
||||
import { broadcastRaw } from "./broadcast.js";
|
||||
import { broadcastEvent } from "./broadcast.js";
|
||||
|
||||
const logger = createChildLogger("ws.redis-bridge");
|
||||
|
||||
@@ -77,7 +77,7 @@ function handleSubscriptionMessage(channel: string, message: string): void {
|
||||
{ channel, eventType: mapping.eventType },
|
||||
"Broadcasting Redis event",
|
||||
);
|
||||
broadcastRaw(mapping.eventType, data);
|
||||
broadcastEvent(mapping.eventType, data);
|
||||
}
|
||||
|
||||
export async function startRedisBridge(): Promise<void> {
|
||||
|
||||
@@ -191,34 +191,10 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
||||
}
|
||||
}
|
||||
|
||||
setBroadcastFunctions({
|
||||
messageCreated: (data: unknown) =>
|
||||
broadcast({ type: "message_created", data }),
|
||||
messageUpdated: (data: unknown) =>
|
||||
broadcast({ type: "message_updated", data }),
|
||||
messageDeleted: (data: unknown) =>
|
||||
broadcast({ type: "message_deleted", data }),
|
||||
messageAnalyzed: (data: unknown) =>
|
||||
broadcast({ type: "message_analyzed", data }),
|
||||
attachmentCreated: (data: unknown) =>
|
||||
broadcast({ type: "attachment_created", data }),
|
||||
attachmentUploaded: (data: unknown) =>
|
||||
broadcast({ type: "attachment_uploaded", data }),
|
||||
voiceRecordingStarted: (data: unknown) =>
|
||||
broadcast({ type: "voice_recording_started", data }),
|
||||
voiceRecordingStopped: (data: unknown) =>
|
||||
broadcast({ type: "voice_recording_stopped", data }),
|
||||
voiceRecordingUploaded: (data: unknown) =>
|
||||
broadcast({ type: "voice_recording_uploaded", data }),
|
||||
voicePcmData: (data: unknown) =>
|
||||
broadcast({ type: "voice_pcm_data", data }),
|
||||
voiceActiveUser: (data: unknown) =>
|
||||
broadcast({ type: "voice_active_user", data }),
|
||||
analysisQueueStatus: (data: unknown) =>
|
||||
broadcast({ type: "analysis_queue_status", data }),
|
||||
raw: (type: string, data: unknown) => broadcast({ type, data }),
|
||||
binary: broadcastBinary,
|
||||
});
|
||||
setBroadcastFunctions(
|
||||
(type: string, data: unknown) => broadcast({ type, data }),
|
||||
broadcastBinary,
|
||||
);
|
||||
|
||||
// Cleanup on close
|
||||
wss.on("close", () => {
|
||||
|
||||
Reference in New Issue
Block a user