refactor: atomic, DRY, and logging improvements across codebase

- Split llmModerationClient.ts (2170 lines) into 5 focused sub-modules
- Split aiAnalyzer.ts (1282 lines) into 4 modular pipelines
- Split messages.db.ts (826 lines) into 5 domain-specific modules
- Moved shared schema to @bete/shared, eliminated backend duplication
- Added createChildLogger to all voice-recording and AI moderation modules
- Extracted tryCommandThenFallback, normalizeMediaState, DEFAULT_VOICE_STATUS
- Created shared pagination.ts utility, eliminated 5+ cursor-pagination duplications
- Created shared messageMapper.ts for row mapping
- Standardized backend error handling with asyncHandler
- Added frontend createLogger utility and useAsyncAction hook
- Added structured logging to frontend hooks, socket, and API client

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-09 19:46:08 +07:00
co-authored by Claude Opus 4.8
parent b68789fffc
commit 07032ab521
61 changed files with 3808 additions and 3043 deletions
@@ -1,4 +1,6 @@
import { createChildLogger } from "@bete/shared/logger";
import type { Request, Response } from "express";
import { asyncHandler } from "../../shared/middlewares/index.js";
import { publishCommandNoReply } from "../../shared/redis/index.js";
import {
connectVoice,
@@ -7,10 +9,14 @@ import {
getVoiceStatus,
} from "./voice.service.js";
export async function handleGetVoiceStatus(_req: Request, res: Response) {
const status = await getVoiceStatus();
res.json(status);
}
const logger = createChildLogger("voice.controller");
export const handleGetVoiceStatus = asyncHandler(
async (_req: Request, res: Response) => {
const status = await getVoiceStatus();
res.json(status);
},
);
/** Safely extract a string value that may be a single string or string array. */
function asString(val: unknown): string {
@@ -18,47 +24,52 @@ function asString(val: unknown): string {
return String(val ?? "");
}
export async function handleConnectVoice(req: Request, res: Response) {
const guildId = asString(req.body.guildId);
const channelId = asString(req.body.channelId);
if (!guildId || !channelId) {
return res.status(400).json({
error: "VALIDATION_ERROR",
message: "guildId and channelId are required",
});
}
const status = await connectVoice(guildId, channelId);
res.json(status);
}
export const handleConnectVoice = asyncHandler(
async (req: Request, res: Response) => {
const guildId = asString(req.body.guildId);
const channelId = asString(req.body.channelId);
if (!guildId || !channelId) {
return res.status(400).json({
error: "VALIDATION_ERROR",
message: "guildId and channelId are required",
});
}
logger.debug({ guildId, channelId }, "Connecting to voice channel");
const status = await connectVoice(guildId, channelId);
res.json(status);
},
);
export async function handleDisconnectVoice(_req: Request, res: Response) {
const status = await disconnectVoice();
res.json(status);
}
export const handleDisconnectVoice = asyncHandler(
async (_req: Request, res: Response) => {
logger.debug("Disconnecting from voice");
const status = await disconnectVoice();
res.json(status);
},
);
export async function handleGetVoiceChannels(req: Request, res: Response) {
const guildId = asString(req.params.guildId);
const channels = await getVoiceChannels(guildId);
res.json(channels);
}
export const handleGetVoiceChannels = asyncHandler(
async (req: Request, res: Response) => {
const guildId = asString(req.params.guildId);
logger.debug({ guildId }, "Fetching voice channels");
const channels = await getVoiceChannels(guildId);
res.json(channels);
},
);
export async function handleVoiceCommand(req: Request, res: Response) {
const command = asString(req.body.command);
export const handleVoiceCommand = asyncHandler(
async (req: Request, res: Response) => {
const command = asString(req.body.command);
if (!command) {
return res.status(400).json({
error: "VALIDATION_ERROR",
message: "command is required",
});
}
if (!command) {
return res.status(400).json({
error: "VALIDATION_ERROR",
message: "command is required",
});
}
try {
logger.debug({ command }, "Publishing voice command");
await publishCommandNoReply(command);
res.json({ success: true, command });
} catch (err) {
res.status(500).json({
error: "COMMAND_FAILED",
message: err instanceof Error ? err.message : "Unknown error",
});
}
}
},
);