refactor: comprehensive codebase cleanup and architecture hardening

- Sprint 1 (Quick Wins): Remove dead analytics modules, fix 4 unresolved
  imports, replace 3 console.warn with logger, remove mock-crc import
- Sprint 2 (Architecture): Create MascotChatRepository, AnalysisRepository,
  3 Zod schemas (mascot-chat, analysis, voice), deduplicate error classes,
  move 3 SQL queries from routes to repository
- Sprint 3 (Complexity): Replace 7 any types with proper interfaces,
  extract 6 helpers from prepareMediaMessage (CC 85 -> ~15)
- Sprint 4 (Config): Remove 22 dead env vars from .env, add 30 missing
  vars to .env.example, standardize naming

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-09 10:16:04 +07:00
co-authored by Claude Opus 4.8
parent d0d9e1669e
commit 4becf0d6f1
89 changed files with 1260 additions and 5499 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
import { createChildLogger } from "@bete/shared/logger";
import Redis from "ioredis";
import { config } from "../shared/config/index.js";
import { getCommandPublisher } from "../shared/redis/index.js";
import { createChildLogger } from "@bete/shared/logger";
import { broadcastRaw } from "./broadcast.js";
const logger = createChildLogger("ws.redis-bridge");
+48 -25
View File
@@ -66,38 +66,61 @@ export function createWebSocketServer(server: Server): WebSocketServer {
ws.on("message", (data: Buffer) => {
// Handle JSON messages from browser
if (typeof data === 'string' || (Buffer.isBuffer(data) && data.length > 0 && data[0] === 0x7B)) {
if (
typeof data === "string" ||
(Buffer.isBuffer(data) && data.length > 0 && data[0] === 0x7b)
) {
try {
const message = JSON.parse(data.toString());
if (message.type === 'voice_transmit' && message.buffer) {
if (message.type === "voice_transmit" && message.buffer) {
// Forward PCM data to Redis for discord-gateway
import('../shared/redis/index.js').then(({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
publisher.publish('backend:voice:transmit', JSON.stringify({
type: 'pcm',
buffer: message.buffer
})).catch((err: Error) => {
logger.error({ err }, 'Failed to publish voice transmit to Redis');
});
});
} else if (message.type === 'voice_command' && message.command) {
import("../shared/redis/index.js").then(
({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
publisher
.publish(
"backend:voice:transmit",
JSON.stringify({
type: "pcm",
buffer: message.buffer,
}),
)
.catch((err: Error) => {
logger.error(
{ err },
"Failed to publish voice transmit to Redis",
);
});
},
);
} else if (message.type === "voice_command" && message.command) {
// Forward voice commands to discord-gateway
import('../shared/redis/index.js').then(({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
const commandId = `cmd-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
publisher.publish('backend:command', JSON.stringify({
id: commandId,
type: message.command,
payload: {},
replyChannel: `reply:${commandId}`
})).catch((err: Error) => {
logger.error({ err }, 'Failed to publish voice command to Redis');
});
});
import("../shared/redis/index.js").then(
({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
const commandId = `cmd-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
publisher
.publish(
"backend:command",
JSON.stringify({
id: commandId,
type: message.command,
payload: {},
replyChannel: `reply:${commandId}`,
}),
)
.catch((err: Error) => {
logger.error(
{ err },
"Failed to publish voice command to Redis",
);
});
},
);
}
} catch (err) {
logger.debug({ err }, 'Failed to parse WebSocket message as JSON');
logger.debug({ err }, "Failed to parse WebSocket message as JSON");
}
}
});