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:
co-authored by
Claude Opus 4.8
parent
d0d9e1669e
commit
4becf0d6f1
@@ -1,8 +1,10 @@
|
||||
// ─── Audio transmit hook — captures mic, encodes to PCM, sends via WebSocket ──
|
||||
import { useCallback, useRef, useState } from "react";
|
||||
import { getAPIURL } from "../api/client";
|
||||
import { createChildLogger } from "../logger";
|
||||
|
||||
const SAMPLE_RATE = 24000;
|
||||
const logger = createChildLogger("useAudioTransmit");
|
||||
|
||||
async function sendTransmitCommand(command: string): Promise<void> {
|
||||
// Send via HTTP API
|
||||
@@ -12,9 +14,12 @@ async function sendTransmitCommand(command: string): Promise<void> {
|
||||
body: JSON.stringify({ command }),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
console.warn("HTTP command response:", resp.status, resp.statusText);
|
||||
logger.warn("HTTP command response", {
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
});
|
||||
const text = await resp.text().catch(() => resp.statusText);
|
||||
console.warn("HTTP command failed:", text);
|
||||
logger.warn("HTTP command failed", { error: text });
|
||||
throw new Error(`HTTP ${resp.status}: ${text}`);
|
||||
}
|
||||
}
|
||||
@@ -72,16 +77,18 @@ export function useAudioTransmit(socketRef: {
|
||||
|
||||
// Base64 encode
|
||||
const bytes = new Uint8Array(pcmData.buffer);
|
||||
let binary = '';
|
||||
let binary = "";
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
binary += String.fromCharCode(bytes[i]);
|
||||
}
|
||||
const base64 = btoa(binary);
|
||||
|
||||
socketRef.current.send(JSON.stringify({
|
||||
type: 'voice_transmit',
|
||||
buffer: base64
|
||||
}));
|
||||
socketRef.current.send(
|
||||
JSON.stringify({
|
||||
type: "voice_transmit",
|
||||
buffer: base64,
|
||||
}),
|
||||
);
|
||||
};
|
||||
}, [socketRef]);
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { createChildLogger } from "../logger";
|
||||
|
||||
const logger = createChildLogger("useMascotChat");
|
||||
|
||||
export interface ChatContext {
|
||||
messageCount: number;
|
||||
@@ -28,7 +31,7 @@ export function useMascotChat(context?: ChatContext) {
|
||||
const data = (await response.json()) as { response?: string };
|
||||
return data.response || fallbackResponse(message, context);
|
||||
} catch (error) {
|
||||
console.warn("Mascot backend unavailable, using fallback", error);
|
||||
logger.warn("Mascot backend unavailable, using fallback", { error });
|
||||
return fallbackResponse(message, context);
|
||||
}
|
||||
},
|
||||
@@ -53,7 +56,10 @@ function fallbackResponse(input: string, context?: ChatContext): string {
|
||||
return `Ada ${context?.messageCount || 0} pesan di konteks dashboard saat ini 📊`;
|
||||
}
|
||||
|
||||
if (lower.includes("berapa") && (lower.includes("orang") || lower.includes("user"))) {
|
||||
if (
|
||||
lower.includes("berapa") &&
|
||||
(lower.includes("orang") || lower.includes("user"))
|
||||
) {
|
||||
return `Ada ${context?.activeParticipants || 0} user aktif yang terdeteksi 👥`;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ function generateInsight(messages: MessageRecord[]): string {
|
||||
// Hitung average panjang pesan
|
||||
const avgLength = Math.round(
|
||||
recentMessages.reduce((sum, m) => sum + (m.content?.length || 0), 0) /
|
||||
recentMessages.length
|
||||
recentMessages.length,
|
||||
);
|
||||
|
||||
// Tentukan tipe percakapan
|
||||
|
||||
Reference in New Issue
Block a user