feat: update components and hooks to use get_untracked for improved performance

This commit is contained in:
asepharyana
2026-07-04 03:03:36 +07:00
parent 8166023f91
commit 27e929580e
85 changed files with 1704 additions and 1844 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
* E2E API tests — runs against a running backend instance.
* Usage: vitest run (or: API_BASE=http://localhost:3001 vitest run)
*/
import { describe, it, expect } from "vitest";
import { describe, expect, it } from "vitest";
const BASE = process.env.API_BASE ?? "https://imphnen.asepharyana.my.id/api";
+1 -4
View File
@@ -18,11 +18,8 @@ import { createRecordingsRouter } from "../modules/recordings/recordings.routes.
import { createUiStateRouter } from "../modules/ui-state/ui-state.routes.js";
import { createGuildsRouter } from "../modules/voice/guilds.routes.js";
import { createVoiceRouter } from "../modules/voice/voice.routes.js";
import {
adminAuth,
errorHandler,
} from "../shared/middlewares/index.js";
import { config } from "../shared/config/index.js";
import { adminAuth, errorHandler } from "../shared/middlewares/index.js";
const ADMIN_PASSWORD = config.ADMIN_PASSWORD || "admin";
@@ -67,7 +67,9 @@ export class RecordingsService {
const items = rows.slice(0, limit) as unknown as RecordingRow[];
const hasMore = rows.length > limit;
const nextCursor = hasMore ? String(items[items.length - 1]!.created_at) : null;
const nextCursor = hasMore
? String(items[items.length - 1]!.created_at)
: null;
return { items, nextCursor, hasMore };
}
@@ -2,7 +2,11 @@ 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, disconnectVoice, getVoiceStatus } from "./voice.service.js";
import {
connectVoice,
disconnectVoice,
getVoiceStatus,
} from "./voice.service.js";
const logger = createChildLogger("voice.controller");
@@ -166,4 +166,3 @@ export async function disconnectVoice(): Promise<VoiceStatus> {
"disconnectVoice",
);
}
+19 -24
View File
@@ -115,29 +115,27 @@ export function createWebSocketServer(server: Server): WebSocketServer {
data[0] === 0x50 && // 'P'
data[1] === 0x43 && // 'C'
data[2] === 0x4d && // 'M'
data[3] === 0x00 // '\0'
data[3] === 0x00 // '\0'
) {
const pcmBuffer = data.subarray(4);
const base64 = pcmBuffer.toString("base64");
import("../shared/redis/index.js").then(
({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
publisher
.publish(
BACKEND_VOICE_TRANSMIT,
JSON.stringify({
type: "pcm",
buffer: base64,
}),
)
.catch((err: Error) => {
logger.error(
{ err },
"Failed to publish voice transmit to Redis",
);
});
},
);
import("../shared/redis/index.js").then(({ getCommandPublisher }) => {
const publisher = getCommandPublisher();
publisher
.publish(
BACKEND_VOICE_TRANSMIT,
JSON.stringify({
type: "pcm",
buffer: base64,
}),
)
.catch((err: Error) => {
logger.error(
{ err },
"Failed to publish voice transmit to Redis",
);
});
});
return;
}
@@ -243,10 +241,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
try {
client.send(data);
} catch (err) {
logger.error(
{ err },
"Failed to send binary to frontend client",
);
logger.error({ err }, "Failed to send binary to frontend client");
}
}
}
@@ -23,14 +23,16 @@ import { getExpiredMessages } from "../modules/message-capture/messageStore.js";
import { registerReactionCapture } from "../modules/reaction-tracking/index.js";
import { registerThreadCapture } from "../modules/thread-tracking/index.js";
import { registerPresenceCapture } from "../modules/user-presence/index.js";
import { VoicePcmWsClient } from "../modules/voice-pcm-ws/index.js";
import {
startMuxerWorker,
stopMuxerWorker,
} from "../modules/voice-recording/muxer.js";
import { setEventBroadcaster as setRecorderEventBroadcaster } from "../modules/voice-recording/recorder.js";
import { setPcmWsClient } from "../modules/voice-recording/recorder.js";
import {
setPcmWsClient,
setEventBroadcaster as setRecorderEventBroadcaster,
} from "../modules/voice-recording/recorder.js";
import { VoiceController } from "../modules/voice-recording/voiceController.js";
import { VoicePcmWsClient } from "../modules/voice-pcm-ws/index.js";
import { config } from "../shared/config/config.js";
import {
closeDatabase,
@@ -229,10 +231,7 @@ export async function initializeDiscordGateway() {
);
pcmWsClient.connect();
setPcmWsClient(pcmWsClient);
logger.info(
{ url: config.BACKEND_WS_URL },
"Voice PCM WS client enabled",
);
logger.info({ url: config.BACKEND_WS_URL }, "Voice PCM WS client enabled");
} else if (config.VOICE_PCM_WS_ENABLED && !config.BACKEND_WS_TOKEN) {
logger.warn(
"VOICE_PCM_WS_ENABLED=true but BACKEND_WS_TOKEN is empty — falling back to Redis for PCM",
+1 -1
View File
@@ -3,8 +3,8 @@ import type { Client } from "discord.js-selfbot-v13";
import type { CommandHandler } from "../modules/command-handler/commandHandler.js";
import type { EventBroadcaster } from "../modules/event-broadcaster/index.js";
import { stopMetricsServer } from "../modules/gateway-metrics/index.js";
import { stopMuxerWorker } from "../modules/voice-recording/muxer.js";
import type { VoicePcmWsClient } from "../modules/voice-pcm-ws/index.js";
import { stopMuxerWorker } from "../modules/voice-recording/muxer.js";
import type { VoiceController } from "../modules/voice-recording/voiceController.js";
import type { closeDatabase } from "../shared/database/drizzle.js";
@@ -1,6 +1,7 @@
import { createChildLogger } from "@bete/shared/logger";
import { config } from "../../shared/config/config.js";
import { initializeDatabase } from "../../shared/database/drizzle.js";
import { extractMessageMediaEvidence } from "../message-capture/messageMetadata.js";
import {
getAttachmentsForMessages,
getConversationContextBefore,
@@ -10,7 +11,6 @@ import type {
AnalysisResult,
MessageRecord,
} from "../message-capture/types.js";
import { extractMessageMediaEvidence } from "../message-capture/messageMetadata.js";
import { buildConversationContext } from "./conversationContext.js";
import {
runModerationAnalysis,
@@ -173,8 +173,15 @@ async function processBatch(job: {
const media: MessageRecord[] = [];
for (const msg of messages) {
const meta = msg.metadata ? extractMessageMediaEvidence(msg.metadata) : null;
if (meta && (meta.attachments.length > 0 || meta.stickers.length > 0 || meta.embeds.length > 0)) {
const meta = msg.metadata
? extractMessageMediaEvidence(msg.metadata)
: null;
if (
meta &&
(meta.attachments.length > 0 ||
meta.stickers.length > 0 ||
meta.embeds.length > 0)
) {
media.push(msg);
// If the message also has text content, analyze it in the text batch too
const rawContent = msg.edited_content ?? msg.content;
@@ -193,73 +200,80 @@ async function processBatch(job: {
// Running both in parallel means media downloads overlap with text LLM call.
// Each path saves to DB as soon as its own results are ready.
// ────────────────────────────────────────────────────────────────────
const textPromise = textOnly.length > 0
? runModerationAnalysis({
targets: textOnly,
contextText: contextLines.join("\n"),
attachments,
}).then((result) => {
const updates = result.results.map((analysisResult) => ({
messageId: analysisResult.messageId,
result: {
status: analysisResult.status,
flags: JSON.stringify(analysisResult.flags),
score: analysisResult.score,
analysis: analysisResult.analysis,
categories: analysisResult.categories,
severity: analysisResult.severity,
confidence: analysisResult.confidence,
recommendedAction: analysisResult.recommendedAction,
analyzedAt: Date.now(),
error: null,
},
}));
if (updates.length > 0) {
return updateMessagesAIAnalysisBulk(updates).then((rows) => {
allRows.push(...rows);
logger.info(
{ count: updates.length, conversationKey },
"Text-only batch saved — media analysis still in progress",
);
});
}
})
: Promise.resolve();
const textPromise =
textOnly.length > 0
? runModerationAnalysis({
targets: textOnly,
contextText: contextLines.join("\n"),
attachments,
}).then((result) => {
const updates = result.results.map((analysisResult) => ({
messageId: analysisResult.messageId,
result: {
status: analysisResult.status,
flags: JSON.stringify(analysisResult.flags),
score: analysisResult.score,
analysis: analysisResult.analysis,
categories: analysisResult.categories,
severity: analysisResult.severity,
confidence: analysisResult.confidence,
recommendedAction: analysisResult.recommendedAction,
analyzedAt: Date.now(),
error: null,
},
}));
if (updates.length > 0) {
return updateMessagesAIAnalysisBulk(updates).then((rows) => {
allRows.push(...rows);
logger.info(
{ count: updates.length, conversationKey },
"Text-only batch saved — media analysis still in progress",
);
});
}
})
: Promise.resolve();
const mediaPromise = media.length > 0
? runModerationAnalysis({
targets: media,
contextText: contextLines.join("\n"),
attachments,
}).then((result) => {
const updates = result.results.map((analysisResult) => ({
messageId: analysisResult.messageId,
result: {
status: analysisResult.status,
flags: JSON.stringify(analysisResult.flags),
score: analysisResult.score,
analysis: analysisResult.analysis,
categories: analysisResult.categories,
severity: analysisResult.severity,
confidence: analysisResult.confidence,
recommendedAction: analysisResult.recommendedAction,
analyzedAt: Date.now(),
error: null,
},
}));
if (updates.length > 0) {
return updateMessagesAIAnalysisBulk(updates).then((rows) => {
allRows.push(...rows);
});
}
})
: Promise.resolve();
const mediaPromise =
media.length > 0
? runModerationAnalysis({
targets: media,
contextText: contextLines.join("\n"),
attachments,
}).then((result) => {
const updates = result.results.map((analysisResult) => ({
messageId: analysisResult.messageId,
result: {
status: analysisResult.status,
flags: JSON.stringify(analysisResult.flags),
score: analysisResult.score,
analysis: analysisResult.analysis,
categories: analysisResult.categories,
severity: analysisResult.severity,
confidence: analysisResult.confidence,
recommendedAction: analysisResult.recommendedAction,
analyzedAt: Date.now(),
error: null,
},
}));
if (updates.length > 0) {
return updateMessagesAIAnalysisBulk(updates).then((rows) => {
allRows.push(...rows);
});
}
})
: Promise.resolve();
// Wait for both to complete
await Promise.all([textPromise, mediaPromise]);
logger.info(
{ total: messages.length, textOnly: textOnly.length, media: media.length, saved: allRows.length },
{
total: messages.length,
textOnly: textOnly.length,
media: media.length,
saved: allRows.length,
},
"Batch analysis complete",
);
@@ -10,6 +10,10 @@
*/
export { sniffImageMimeType } from "./imageMimeSniffer.js";
export { extractJson } from "./jsonExtractor.js";
export {
runModerationAnalysis,
runSimpleTextFallback,
} from "./moderationOrchestrator.js";
export {
parseModerationResponse,
sanitizeErrorMessage,
@@ -28,7 +32,3 @@ export {
deriveSeverity,
hasDeferralAnalysis,
} from "./severityDeriver.js";
export {
runModerationAnalysis,
runSimpleTextFallback,
} from "./moderationOrchestrator.js";
@@ -6,11 +6,11 @@
* preparation for the LLM moderation pipeline.
*/
import { execFile } from "node:child_process";
import { createChildLogger } from "@bete/shared/logger";
import { readFile, writeFile, unlink, rm, mkdtemp } from "node:fs/promises";
import { mkdtemp, readFile, rm, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { createChildLogger } from "@bete/shared/logger";
import { createAbortControllerWithTimeout, delay } from "@bete/shared/utils";
import { LRUCache } from "lru-cache";
import { config } from "../../shared/config/config.js";
@@ -20,8 +20,24 @@ import type {
AttachmentRecord,
MessageRecord,
} from "../message-capture/types.js";
import { sniffImageMimeType } from "./imageMimeSniffer.js";
import { llmVision } from "./llmClient.js";
import {
buildReferenceXml,
escapeXml,
getAnalysisContent,
} from "./moderationBuilders.js";
import { sanitizeAiContent } from "./moderationPrompt.js";
import {
extractSearchQueries,
formatSearchResults,
searchSearxng,
} from "./searxngSearch.js";
import {
getStickerFromCache,
isStickerCacheReady,
uploadAndCacheSticker,
} from "./stickerCache.js";
import {
buildCustomEmojiVisionPrompt,
buildGeneralImageVisionPrompt,
@@ -40,17 +56,9 @@ import {
upsertCachedMediaAnalysis,
upsertCachedMediaByPhash,
} from "./textCacheStore.js";
import { sniffImageMimeType } from "./imageMimeSniffer.js";
import { fetchUrlSafely, extractUrlsFromText } from "./urlFetcher.js";
import {
getStickerFromCache,
isStickerCacheReady,
uploadAndCacheSticker,
} from "./stickerCache.js";
import { searchSearxng, extractSearchQueries, formatSearchResults } from "./searxngSearch.js";
import { extractUrlsFromText, fetchUrlSafely } from "./urlFetcher.js";
import { getUserProfile } from "./userProfileStore.js";
import { initializeUserReputation } from "./userReputationStore.js";
import { escapeXml, getAnalysisContent, buildReferenceXml } from "./moderationBuilders.js";
// ---------------------------------------------------------------------------
// Types
@@ -122,10 +130,18 @@ function buildMediaCandidates(
...evidence.embeds.flatMap((embed): MediaCandidate[] =>
[
embed.image
? ({ messageId, url: embed.image, label: `[gambar di atas berasal dari embed image pada pesan id=${messageId}]` } as MediaCandidate)
? ({
messageId,
url: embed.image,
label: `[gambar di atas berasal dari embed image pada pesan id=${messageId}]`,
} as MediaCandidate)
: null,
embed.thumbnail
? ({ messageId, url: embed.thumbnail, label: `[gambar di atas berasal dari embed thumbnail pada pesan id=${messageId}]` } as MediaCandidate)
? ({
messageId,
url: embed.thumbnail,
label: `[gambar di atas berasal dari embed thumbnail pada pesan id=${messageId}]`,
} as MediaCandidate)
: null,
].filter((c): c is MediaCandidate => c !== null),
),
@@ -234,12 +250,19 @@ export const analyzeSingleMediaImage = async (
const phashCached = await getCachedMediaByPhash(phash);
if (phashCached) {
visionLruCache.set(cacheKey, phashCached);
await upsertCachedMediaAnalysis(cacheKey, phashCached, "vision_llm", Date.now() + 24 * 60 * 60 * 1000).catch(() => {});
await upsertCachedMediaAnalysis(
cacheKey,
phashCached,
"vision_llm",
Date.now() + 24 * 60 * 60 * 1000,
).catch(() => {});
return phashCached;
}
}
}
} catch { phash = null; }
} catch {
phash = null;
}
}
// Vision API call
@@ -248,10 +271,20 @@ export const analyzeSingleMediaImage = async (
try {
const content = await llmVision(promptText, image.image_url);
if (content) {
await upsertCachedMediaAnalysis(cacheKey, content, "vision_llm", Date.now() + 24 * 60 * 60 * 1000);
await upsertCachedMediaAnalysis(
cacheKey,
content,
"vision_llm",
Date.now() + 24 * 60 * 60 * 1000,
);
visionLruCache.set(cacheKey, content);
if (phash) {
upsertCachedMediaByPhash(phash, content, "vision_llm", Date.now() + 7 * 24 * 60 * 60 * 1000).catch(() => {});
upsertCachedMediaByPhash(
phash,
content,
"vision_llm",
Date.now() + 7 * 24 * 60 * 60 * 1000,
).catch(() => {});
}
return content;
}
@@ -260,13 +293,27 @@ export const analyzeSingleMediaImage = async (
} catch (err) {
lastError = err instanceof Error ? err : new Error(String(err));
if (attempt < 2) {
const backoffMs = Math.min(2_000 * 3 ** attempt + Math.random() * 500, 30_000);
log.warn({ messageId, attempt: attempt + 1, backoffMs, error: lastError.message }, "Vision retry");
const backoffMs = Math.min(
2_000 * 3 ** attempt + Math.random() * 500,
30_000,
);
log.warn(
{
messageId,
attempt: attempt + 1,
backoffMs,
error: lastError.message,
},
"Vision retry",
);
await delay(backoffMs);
}
}
}
log.warn({ messageId, lastError: lastError?.message ?? "null" }, "Vision failed after 3 attempts");
log.warn(
{ messageId, lastError: lastError?.message ?? "null" },
"Vision failed after 3 attempts",
);
await deleteCachedMediaAnalysis(cacheKey).catch(() => {});
return FAILED_ANALYSIS_PREFIX;
})();
@@ -276,7 +323,14 @@ export const analyzeSingleMediaImage = async (
const content = await visionPromise;
return `[Media analysis for message ${messageId}] ${image.sourceLabel}: ${content}`;
} catch (outerErr) {
log.error({ messageId, cacheKey, error: outerErr instanceof Error ? outerErr.message : String(outerErr) }, "visionPromise threw unexpectedly");
log.error(
{
messageId,
cacheKey,
error: outerErr instanceof Error ? outerErr.message : String(outerErr),
},
"visionPromise threw unexpectedly",
);
return `[Media analysis for message ${messageId}] ${image.sourceLabel}: ${FAILED_ANALYSIS_PREFIX}`;
} finally {
inFlightVisionCalls.delete(cacheKey);
@@ -310,7 +364,10 @@ async function downloadSingleAttachment(
if (done) break;
if (value) {
totalBytes += value.length;
if (totalBytes > 10 * 1024 * 1024) { reader.cancel(); return; }
if (totalBytes > 10 * 1024 * 1024) {
reader.cancel();
return;
}
chunks.push(value);
}
}
@@ -318,7 +375,13 @@ async function downloadSingleAttachment(
const sniffedMime = sniffImageMimeType(imageBytes);
if (!sniffedMime && att.type.startsWith("video/")) {
await extractVideoFrames(att, imageBytes, targetId, maxDimension, imageMap);
await extractVideoFrames(
att,
imageBytes,
targetId,
maxDimension,
imageMap,
);
return;
}
@@ -327,19 +390,27 @@ async function downloadSingleAttachment(
if (!resolvedMime) {
if (att.type.startsWith("image/")) {
resolvedMime = att.type;
log.warn({ attachmentId: att.id, filename: att.filename, type: att.type },
"Image MIME sniff failed — using attachment metadata type as fallback");
log.warn(
{ attachmentId: att.id, filename: att.filename, type: att.type },
"Image MIME sniff failed — using attachment metadata type as fallback",
);
} else {
// Last resort: check file extension
const ext = att.filename?.toLowerCase().split(".").pop();
if (ext && ["jpg", "jpeg", "png", "gif", "webp", "bmp"].includes(ext)) {
const mimeMap: Record<string, string> = {
jpg: "image/jpeg", jpeg: "image/jpeg", png: "image/png",
gif: "image/gif", webp: "image/webp", bmp: "image/bmp",
jpg: "image/jpeg",
jpeg: "image/jpeg",
png: "image/png",
gif: "image/gif",
webp: "image/webp",
bmp: "image/bmp",
};
resolvedMime = mimeMap[ext];
log.warn({ attachmentId: att.id, filename: att.filename, ext },
"Image MIME sniff failed — using file extension fallback");
log.warn(
{ attachmentId: att.id, filename: att.filename, ext },
"Image MIME sniff failed — using file extension fallback",
);
}
}
}
@@ -347,11 +418,14 @@ async function downloadSingleAttachment(
// If all fallbacks fail, still try with generic image/jpeg (better than silent skip)
if (!resolvedMime) {
resolvedMime = "image/jpeg";
log.warn({ attachmentId: att.id, filename: att.filename },
"All MIME detection failed — forcing image/jpeg as last resort");
log.warn(
{ attachmentId: att.id, filename: att.filename },
"All MIME detection failed — forcing image/jpeg as last resort",
);
}
const { data: resizedBuffer, mimeType: resizedMime } = await resizeImageForVision(imageBytes, maxDimension);
const { data: resizedBuffer, mimeType: resizedMime } =
await resizeImageForVision(imageBytes, maxDimension);
const dataUrl = `data:${resizedMime};base64,${resizedBuffer.toString("base64")}`;
addImageToMap(imageMap, targetId, {
type: "image_url",
@@ -359,7 +433,13 @@ async function downloadSingleAttachment(
sourceLabel: `[gambar di atas adalah attachment ${att.filename} dari pesan id=${att.message_id}]`,
});
} catch (err) {
log.warn({ attachmentId: att.id, error: err instanceof Error ? err.message : String(err) }, "Download failed");
log.warn(
{
attachmentId: att.id,
error: err instanceof Error ? err.message : String(err),
},
"Download failed",
);
} finally {
clear();
}
@@ -379,36 +459,87 @@ async function extractVideoFrames(
const outputPattern = path.join(tmpDir, "frame-%03d.jpg");
try {
await writeFile(inputPath, videoBytes);
const { stdout: durationStr } = await execFileAsync("/usr/bin/ffprobe", [
"-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", inputPath,
], { timeout: 10000 });
const { stdout: durationStr } = await execFileAsync(
"/usr/bin/ffprobe",
[
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"csv=p=0",
inputPath,
],
{ timeout: 10000 },
);
const duration = parseFloat(durationStr.trim()) || 1;
const fps = (3 / duration).toFixed(6);
await execFileAsync("/usr/bin/ffmpeg", [
"-i", inputPath, "-vf", `fps=${fps}`, "-frames:v", "4", "-vsync", "vfr", "-q:v", "2", outputPattern,
], { timeout: 30000 });
await execFileAsync(
"/usr/bin/ffmpeg",
[
"-i",
inputPath,
"-vf",
`fps=${fps}`,
"-frames:v",
"4",
"-vsync",
"vfr",
"-q:v",
"2",
outputPattern,
],
{ timeout: 30000 },
);
for (let i = 1; i <= 4; i++) {
try {
const framePath = path.join(tmpDir, `frame-${String(i).padStart(3, "0")}.jpg`);
const framePath = path.join(
tmpDir,
`frame-${String(i).padStart(3, "0")}.jpg`,
);
const frameBytes = await readFile(framePath);
const { data: resizedBuffer, mimeType: resizedMime } = await resizeImageForVision(frameBytes, maxDimension);
const { data: resizedBuffer, mimeType: resizedMime } =
await resizeImageForVision(frameBytes, maxDimension);
const dataUrl = `data:${resizedMime};base64,${resizedBuffer.toString("base64")}`;
addImageToMap(imageMap, targetId, {
type: "image_url",
image_url: { url: dataUrl },
sourceLabel: `[frame ${i}/4 dari video ${att.filename} (attachment), pesan id=${att.message_id}]`,
});
} catch { /* skip */ }
} catch {
/* skip */
}
}
log.info({ attachmentId: att.id }, "Video frames extracted");
} catch (ffmpegErr) {
log.warn({ attachmentId: att.id, error: ffmpegErr instanceof Error ? ffmpegErr.message : String(ffmpegErr) }, "ffmpeg failed");
log.warn(
{
attachmentId: att.id,
error:
ffmpegErr instanceof Error ? ffmpegErr.message : String(ffmpegErr),
},
"ffmpeg failed",
);
} finally {
try { await unlink(inputPath); } catch { /* ignore */ }
for (let i = 1; i <= 4; i++) {
try { await unlink(path.join(tmpDir, `frame-${String(i).padStart(3, "0")}.jpg`)); } catch { /* ignore */ }
try {
await unlink(inputPath);
} catch {
/* ignore */
}
for (let i = 1; i <= 4; i++) {
try {
await unlink(
path.join(tmpDir, `frame-${String(i).padStart(3, "0")}.jpg`),
);
} catch {
/* ignore */
}
}
try {
await rm(tmpDir, { recursive: true, force: true });
} catch {
/* ignore */
}
try { await rm(tmpDir, { recursive: true, force: true }); } catch { /* ignore */ }
}
}
@@ -429,7 +560,9 @@ async function downloadMediaCandidate(
const cached = await getCachedMediaAnalysis(vck);
if (cached) {
const existing = mediaAnalysisMap.get(targetId) ?? [];
existing.push(`[Media analysis for message ${candidate.messageId}] ${candidate.label}: ${cached}`);
existing.push(
`[Media analysis for message ${candidate.messageId}] ${candidate.label}: ${cached}`,
);
mediaAnalysisMap.set(targetId, existing);
// Warm the LRU cache so subsequent calls in the same process skip DB query
visionLruCache.set(vck, cached);
@@ -449,15 +582,22 @@ async function downloadMediaCandidate(
});
return;
}
} catch { /* fall through */ }
} catch {
/* fall through */
}
}
const result = await fetchUrlSafely(candidate.url);
if (result.type !== "image" || !result.data || !result.mimeType) return;
const { data: resizedBuffer, mimeType: resizedMime } = await resizeImageForVision(result.data, maxDimension);
const { data: resizedBuffer, mimeType: resizedMime } =
await resizeImageForVision(result.data, maxDimension);
const base64 = resizedBuffer.toString("base64");
if (candidate.stickerName) {
uploadAndCacheSticker(candidate.stickerName, resizedBuffer, resizedMime).catch(() => {});
uploadAndCacheSticker(
candidate.stickerName,
resizedBuffer,
resizedMime,
).catch(() => {});
}
addImageToMap(imageMap, targetId, {
type: "image_url",
@@ -478,14 +618,19 @@ async function fetchUrlInline(
): Promise<void> {
const result = await fetchUrlSafely(url);
if (result.type === "image" && result.data && result.mimeType) {
const { data: resizedBuffer, mimeType: resizedMime } = await resizeImageForVision(result.data, maxDimension);
const { data: resizedBuffer, mimeType: resizedMime } =
await resizeImageForVision(result.data, maxDimension);
addImageToMap(imageMap, targetId, {
type: "image_url",
image_url: { url: `data:${resizedMime};base64,${resizedBuffer.toString("base64")}` },
image_url: {
url: `data:${resizedMime};base64,${resizedBuffer.toString("base64")}`,
},
sourceLabel: `[gambar dari URL ${url} (inline), pesan id=${targetId}]`,
});
} else if (result.type === "text" && result.textContent) {
webTexts.push(`<web_content url="${escapeXml(url)}">${escapeXml(result.textContent.slice(0, 2000))}</web_content>`);
webTexts.push(
`<web_content url="${escapeXml(url)}">${escapeXml(result.textContent.slice(0, 2000))}</web_content>`,
);
}
}
@@ -512,23 +657,40 @@ export async function prepareMediaMessage(
// Attachments
const msgAttachments = (allAttachments ?? [])
.filter((a) => a.message_id === targetId && (a.uploaded_url ?? a.discord_url ?? null) && (a.type.startsWith("image/") || a.type.startsWith("video/")))
.filter(
(a) =>
a.message_id === targetId &&
(a.uploaded_url ?? a.discord_url ?? null) &&
(a.type.startsWith("image/") || a.type.startsWith("video/")),
)
.slice(0, 8);
for (const att of msgAttachments) {
downloadPromises.push(downloadSingleAttachment(att, targetId, maxDimension, imageMap));
downloadPromises.push(
downloadSingleAttachment(att, targetId, maxDimension, imageMap),
);
}
// URLs
const urls = extractUrlsFromText(content).slice(0, 3);
const urlWebTexts: string[] = [];
for (const url of urls) {
downloadPromises.push(fetchUrlInline(url, targetId, maxDimension, imageMap, urlWebTexts));
downloadPromises.push(
fetchUrlInline(url, targetId, maxDimension, imageMap, urlWebTexts),
);
}
// Stickers, embeds, custom emoji
const mediaEvidence = extractMessageMediaEvidence(target.metadata);
for (const candidate of buildMediaCandidates(targetId, mediaEvidence)) {
downloadPromises.push(downloadMediaCandidate(candidate, targetId, maxDimension, imageMap, mediaAnalysisMap));
downloadPromises.push(
downloadMediaCandidate(
candidate,
targetId,
maxDimension,
imageMap,
mediaAnalysisMap,
),
);
}
await Promise.all(downloadPromises);
@@ -550,28 +712,37 @@ export async function prepareMediaMessage(
let searxngXml = "";
const queries = extractSearchQueries(content);
if (queries.length > 0) {
const results = await Promise.allSettled(queries.map((q) => searchSearxng(q)));
const results = await Promise.allSettled(
queries.map((q) => searchSearxng(q)),
);
const parts: string[] = [];
for (let i = 0; i < results.length; i++) {
const r = results[i];
if (r.status === "fulfilled" && r.value.length > 0) parts.push(formatSearchResults(r.value));
if (r.status === "fulfilled" && r.value.length > 0)
parts.push(formatSearchResults(r.value));
}
if (parts.length > 0) searxngXml = `\n<web_searches>\n${parts.join("\n")}\n</web_searches>`;
if (parts.length > 0)
searxngXml = `\n<web_searches>\n${parts.join("\n")}\n</web_searches>`;
}
// Build XML block
const webTexts = webTextMap.get(targetId) ?? [];
const mediaAnalyses = mediaAnalysisMap.get(targetId) ?? [];
const webContext = webTexts.length > 0 ? `\n${webTexts.join("\n")}` : "";
const mediaAnalysisContext = mediaAnalyses.length > 0 ? `\n${mediaAnalyses.join("\n")}` : "";
const mediaAnalysisContext =
mediaAnalyses.length > 0 ? `\n${mediaAnalyses.join("\n")}` : "";
const mediaContext = [
mediaEvidence.stickers.length > 0
? mediaEvidence.stickers.map((s) => buildStickerTextOnlyWarning(s.name, s.url)).join(" ")
? mediaEvidence.stickers
.map((s) => buildStickerTextOnlyWarning(s.name, s.url))
.join(" ")
: null,
mediaEvidence.embeds.length > 0
? `[embed evidence: ${mediaEvidence.embeds.map((e) => [e.title, e.description, e.url, e.image, e.thumbnail].filter(Boolean).join(" | ")).join(" || ")}]`
: null,
].filter(Boolean).join(" ");
]
.filter(Boolean)
.join(" ");
const rep = await initializeUserReputation(target.user_id, target.guild_id);
const profile = await getUserProfile(target.user_id);
@@ -4,8 +4,9 @@
* Shared builder utilities extracted from llmModerationClient.ts.
* Used by both mediaAnalysisClient.ts and moderationOrchestrator.ts.
*/
import type { MessageRecord } from "../message-capture/types.js";
import { getMessageById } from "../message-capture/messageStore.js";
import type { MessageRecord } from "../message-capture/types.js";
/** Simple XML-escaping for content text. */
export function escapeXml(s: string): string {
@@ -11,15 +11,38 @@ import type { ChatCompletion } from "openai/resources/chat/completions";
import { config } from "../../shared/config/config.js";
import { extractMessageMediaEvidence } from "../message-capture/messageMetadata.js";
import { getMessageById } from "../message-capture/messageStore.js";
import type { AnalysisResult, AttachmentRecord, MessageRecord } from "../message-capture/types.js";
import type {
AnalysisResult,
AttachmentRecord,
MessageRecord,
} from "../message-capture/types.js";
import { getChannelCulture } from "./channelCultureStore.js";
import { llmChat } from "./llmClient.js";
import { buildSystemPrompt as buildSystemPromptModular, sanitizeAiContent } from "./moderationPrompt.js";
import type {
MessageImagePart,
PreparedMediaMessage,
} from "./mediaAnalysisClient.js";
import {
analyzeSingleMediaImage,
hasMediaContent,
prepareMediaMessage,
} from "./mediaAnalysisClient.js";
import {
buildReferenceXml,
escapeXml,
getAnalysisContent,
} from "./moderationBuilders.js";
import {
buildSystemPrompt as buildSystemPromptModular,
sanitizeAiContent,
} from "./moderationPrompt.js";
import { logModerationAnalysis, logModerationError } from "./responseLogger.js";
import { searchSearxng, extractSearchQueries, formatSearchResults, initSearxngCache } from "./searxngSearch.js";
import { escapeXml, getAnalysisContent, buildReferenceXml } from "./moderationBuilders.js";
import { hasMediaContent, analyzeSingleMediaImage, prepareMediaMessage } from "./mediaAnalysisClient.js";
import type { PreparedMediaMessage, MessageImagePart } from "./mediaAnalysisClient.js";
import {
extractSearchQueries,
formatSearchResults,
initSearxngCache,
searchSearxng,
} from "./searxngSearch.js";
import {
getCachedTextModeration,
getRecentCorrectedModerations,
@@ -55,9 +78,13 @@ async function buildCorrectedFewShotExamples(): Promise<string> {
const origFlags = c.originalFlags.join(", ") || "(none)";
const corrFlags = c.correctedFlags.join(", ") || "(clean)";
const notes = c.correctionNotes ? ` — ${c.correctionNotes}` : "";
lines.push(`- Konten: "${c.contentSnippet.substring(0, 100)}" → sebelumnya di-flag sebagai [${origFlags}], dikoreksi menjadi [${corrFlags}]${notes}`);
lines.push(
`- Konten: "${c.contentSnippet.substring(0, 100)}" → sebelumnya di-flag sebagai [${origFlags}], dikoreksi menjadi [${corrFlags}]${notes}`,
);
}
lines.push("JANGAN ulangi kesalahan yang sama. Jika konten serupa dengan contoh di atas, gunakan koreksi yang sudah ditentukan.");
lines.push(
"JANGAN ulangi kesalahan yang sama. Jika konten serupa dengan contoh di atas, gunakan koreksi yang sudah ditentukan.",
);
return lines.join("\n");
} catch {
return "";
@@ -97,8 +124,13 @@ async function callModerationLLM(
signal,
});
if (!completion) throw new Error("LLM client unavailable (no API key)");
if (!completion.choices || !Array.isArray(completion.choices) || !completion.choices[0]) {
if (!completion)
throw new Error("LLM client unavailable (no API key)");
if (
!completion.choices ||
!Array.isArray(completion.choices) ||
!completion.choices[0]
) {
throw new Error("Invalid LLM response structure");
}
@@ -106,17 +138,36 @@ async function callModerationLLM(
if (!rawContent) throw new Error("No content in LLM response");
try {
const { parseModerationResponse } = await import("./moderationResponseParser.js");
return { parsed: parseModerationResponse(rawContent, targetIds), result: completion };
const { parseModerationResponse } = await import(
"./moderationResponseParser.js"
);
return {
parsed: parseModerationResponse(rawContent, targetIds),
result: completion,
};
} catch (parseError) {
state.lastParseError = parseError instanceof Error ? parseError.message : String(parseError);
state.lastParseError =
parseError instanceof Error
? parseError.message
: String(parseError);
state.lastInvalidContent = rawContent;
log.warn({ error: state.lastParseError, contentLength: rawContent.length, targetIds, model: config.AI_LLM_MODEL }, `Failed to parse moderation response (${label})`);
log.warn(
{
error: state.lastParseError,
contentLength: rawContent.length,
targetIds,
model: config.AI_LLM_MODEL,
},
`Failed to parse moderation response (${label})`,
);
throw parseError;
}
} catch (apiError: any) {
if (apiError?.status === 429) {
log.warn({ status: 429, targetIds, model: config.AI_LLM_MODEL, label }, "LLM API 429 — will retry");
log.warn(
{ status: 429, targetIds, model: config.AI_LLM_MODEL, label },
"LLM API 429 — will retry",
);
await delay(Math.floor(Math.random() * 1000) + 500);
throw apiError;
}
@@ -125,7 +176,12 @@ async function callModerationLLM(
abortErr.name = "AbortError";
throw abortErr;
}
if (apiError?.status >= 500 || apiError?.code === "ECONNRESET" || apiError?.code === "ETIMEDOUT" || apiError?.name === "APIError") {
if (
apiError?.status >= 500 ||
apiError?.code === "ECONNRESET" ||
apiError?.code === "ETIMEDOUT" ||
apiError?.name === "APIError"
) {
throw apiError;
}
throw apiError;
@@ -146,11 +202,21 @@ async function callModerationLLM(
const errorMsg = err instanceof Error ? err.message : String(err);
const isApiError = !state.lastInvalidContent;
const apiErrorCode = isApiError ? `MOD_${Date.now().toString(36).slice(0, 6)}` : null;
const apiErrorCode = isApiError
? `MOD_${Date.now().toString(36).slice(0, 6)}`
: null;
if (isApiError) {
log.warn({ error: errorMsg, targetIds, model: config.AI_LLM_MODEL, label }, `LLM API error after retries (${label})`);
logModerationError(targetIds, config.AI_LLM_MODEL, err instanceof Error ? err : new Error(String(err)), { phase: "api_call", label });
log.warn(
{ error: errorMsg, targetIds, model: config.AI_LLM_MODEL, label },
`LLM API error after retries (${label})`,
);
logModerationError(
targetIds,
config.AI_LLM_MODEL,
err instanceof Error ? err : new Error(String(err)),
{ phase: "api_call", label },
);
parsed = targetIds.map((id) => ({
messageId: id,
status: "error" as const,
@@ -166,9 +232,28 @@ async function callModerationLLM(
}));
} else {
const parseMsg = err instanceof Error ? err.message : String(err);
const contentPreview = state.lastInvalidContent?.substring(0, 500) ?? "<empty>";
log.error({ error: parseMsg, contentLength: state.lastInvalidContent?.length ?? 0, contentPreview, targetIds, model: config.AI_LLM_MODEL }, `Robust Fallback (${label}): parse error`);
logModerationError(targetIds, config.AI_LLM_MODEL, err instanceof Error ? err : new Error(String(err)), { phase: "parse_response", label, contentLength: state.lastInvalidContent?.length ?? 0 });
const contentPreview =
state.lastInvalidContent?.substring(0, 500) ?? "<empty>";
log.error(
{
error: parseMsg,
contentLength: state.lastInvalidContent?.length ?? 0,
contentPreview,
targetIds,
model: config.AI_LLM_MODEL,
},
`Robust Fallback (${label}): parse error`,
);
logModerationError(
targetIds,
config.AI_LLM_MODEL,
err instanceof Error ? err : new Error(String(err)),
{
phase: "parse_response",
label,
contentLength: state.lastInvalidContent?.length ?? 0,
},
);
const errorCode = `MOD_${Date.now().toString(36).slice(0, 6)}`;
parsed = targetIds.map((id) => ({
messageId: id,
@@ -204,15 +289,22 @@ async function runTextOnlyBatch(
const urlFetchPromise = (async () => {
const allUrls = new Set<string>();
for (const msg of targets) {
for (const url of extractUrlsFromText(msg.edited_content ?? msg.content)) allUrls.add(url);
for (const url of extractUrlsFromText(msg.edited_content ?? msg.content))
allUrls.add(url);
}
const urlArr = Array.from(allUrls).slice(0, 10);
if (urlArr.length === 0) return new Map<string, string>();
const results = await Promise.allSettled(urlArr.map((url) => fetchUrlSafely(url)));
const results = await Promise.allSettled(
urlArr.map((url) => fetchUrlSafely(url)),
);
const map = new Map<string, string>();
for (let i = 0; i < urlArr.length; i++) {
const r = results[i];
if (r.status === "fulfilled" && r.value.type === "text" && r.value.textContent) {
if (
r.status === "fulfilled" &&
r.value.type === "text" &&
r.value.textContent
) {
map.set(urlArr[i], r.value.textContent);
}
}
@@ -222,20 +314,27 @@ async function runTextOnlyBatch(
const searxngPromise = (async () => {
const queries = new Set<string>();
for (const msg of targets) {
for (const q of extractSearchQueries(msg.edited_content ?? msg.content)) queries.add(q);
for (const q of extractSearchQueries(msg.edited_content ?? msg.content))
queries.add(q);
}
if (queries.size === 0) return new Map<string, string>();
const queryArr = Array.from(queries).slice(0, 3);
const results = await Promise.allSettled(queryArr.map((q) => searchSearxng(q)));
const results = await Promise.allSettled(
queryArr.map((q) => searchSearxng(q)),
);
const map = new Map<string, string>();
for (let i = 0; i < queryArr.length; i++) {
const r = results[i];
if (r.status === "fulfilled" && r.value.length > 0) map.set(queryArr[i], formatSearchResults(r.value));
if (r.status === "fulfilled" && r.value.length > 0)
map.set(queryArr[i], formatSearchResults(r.value));
}
return map;
})();
const [urlFetchMap, searxngResults] = await Promise.all([urlFetchPromise, searxngPromise]);
const [urlFetchMap, searxngResults] = await Promise.all([
urlFetchPromise,
searxngPromise,
]);
// Deduplicate identical short messages
const shortContentGroups = new Map<string, MessageRecord[]>();
@@ -256,7 +355,11 @@ async function runTextOnlyBatch(
}
}
for (const [, members] of shortContentGroups) {
if (members.length > 1) groupMapping.set(members[0].id, members.map((m) => m.id));
if (members.length > 1)
groupMapping.set(
members[0].id,
members.map((m) => m.id),
);
}
// Split into sub-batches
@@ -268,7 +371,9 @@ async function runTextOnlyBatch(
const allResults: AnalysisResult[] = [];
let lastRaw: unknown = null;
const channelId = targets[0]?.channel_id ?? "";
const channelCultureObj = channelId ? await getChannelCulture(channelId) : null;
const channelCultureObj = channelId
? await getChannelCulture(channelId)
: null;
const channelCulture = channelCultureObj?.culture_summary;
for (let i = 0; i < subBatches.length; i++) {
@@ -281,36 +386,70 @@ async function runTextOnlyBatch(
for (const msg of batch) {
if (!userContexts.has(msg.user_id)) {
const rep = await initializeUserReputation(msg.user_id, msg.guild_id);
userContexts.set(msg.user_id, `<user_reputation trust_score="${rep.trust_score}" />`);
userContexts.set(
msg.user_id,
`<user_reputation trust_score="${rep.trust_score}" />`,
);
}
if (!userProfiles.has(msg.user_id)) {
const profile = await getUserProfile(msg.user_id);
userProfiles.set(msg.user_id, profile ? `<user_profile>${sanitizeAiContent(profile.profile_summary)}</user_profile>` : "");
userProfiles.set(
msg.user_id,
profile
? `<user_profile>${sanitizeAiContent(profile.profile_summary)}</user_profile>`
: "",
);
}
}
const buildContent = async (state: RetryState): Promise<string> => {
const correction = state.lastParseError ? { error: state.lastParseError, preview: state.lastInvalidContent?.slice(0, 800) ?? "<empty>" } : undefined;
const correction = state.lastParseError
? {
error: state.lastParseError,
preview: state.lastInvalidContent?.slice(0, 800) ?? "<empty>",
}
: undefined;
const correctedExamples = await buildCorrectedFewShotExamples();
const systemText = buildSystemPromptModular({ contextText, mode: "text", correction, correctedExamples, channelCulture });
const systemText = buildSystemPromptModular({
contextText,
mode: "text",
correction,
correctedExamples,
channelCulture,
});
const messagesBlock = (await Promise.all(batch.map(async (msg) => {
const content = getAnalysisContent(msg);
const msgUrls = extractUrlsFromText(content);
const urlContexts = msgUrls.map((url) => {
const ft = urlFetchMap.get(url);
return ft ? `<web_content url="${escapeXml(url)}">${escapeXml(ft)}</web_content>` : null;
}).filter(Boolean).join("\n");
const webContext = urlContexts ? `\n${urlContexts}` : "";
const userCtx = userContexts.get(msg.user_id) ?? "";
const userProfileCtx = userProfiles.get(msg.user_id) ?? "";
const refXml = await buildReferenceXml(msg);
return `<message id="${msg.id}" user="${msg.username}">\n ${userCtx}${userProfileCtx ? `\n ${userProfileCtx}` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(content)}</content>${webContext}\n</message>`;
}))).join("\n");
const messagesBlock = (
await Promise.all(
batch.map(async (msg) => {
const content = getAnalysisContent(msg);
const msgUrls = extractUrlsFromText(content);
const urlContexts = msgUrls
.map((url) => {
const ft = urlFetchMap.get(url);
return ft
? `<web_content url="${escapeXml(url)}">${escapeXml(ft)}</web_content>`
: null;
})
.filter(Boolean)
.join("\n");
const webContext = urlContexts ? `\n${urlContexts}` : "";
const userCtx = userContexts.get(msg.user_id) ?? "";
const userProfileCtx = userProfiles.get(msg.user_id) ?? "";
const refXml = await buildReferenceXml(msg);
return `<message id="${msg.id}" user="${msg.username}">\n ${userCtx}${userProfileCtx ? `\n ${userProfileCtx}` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(content)}</content>${webContext}\n</message>`;
}),
)
).join("\n");
const searxngBlock = searxngResults.size > 0
? `\n\n<web_searches>\n${Array.from(searxngResults.entries()).map(([q, xml]) => ` <search_query query="${escapeXml(q)}">\n${xml} </search_query>`).join("\n")}\n</web_searches>`
: "";
const searxngBlock =
searxngResults.size > 0
? `\n\n<web_searches>\n${Array.from(searxngResults.entries())
.map(
([q, xml]) =>
` <search_query query="${escapeXml(q)}">\n${xml} </search_query>`,
)
.join("\n")}\n</web_searches>`
: "";
return `${systemText}${searxngBlock}\n\n<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`;
};
@@ -320,10 +459,17 @@ async function runTextOnlyBatch(
let batchResult: { results: AnalysisResult[]; raw: unknown };
try {
batchResult = await callModerationLLM(buildContent, targetIds, `text-batch-${i + 1}`, abortController.signal);
batchResult = await callModerationLLM(
buildContent,
targetIds,
`text-batch-${i + 1}`,
abortController.signal,
);
} catch (err: any) {
if (err.name === "AbortError" || abortController.signal.aborted) {
throw new Error(`Text-only batch sub-batch ${i + 1} timed out for messages ${targetIds.join(", ")}`);
throw new Error(
`Text-only batch sub-batch ${i + 1} timed out for messages ${targetIds.join(", ")}`,
);
}
throw err;
} finally {
@@ -331,20 +477,36 @@ async function runTextOnlyBatch(
}
// Fan-out results for deduplicated messages
const fannedOutResults = groupMapping.size > 0
? batchResult.results.flatMap((result) => {
const members = groupMapping.get(result.messageId);
return members ? members.map((memberId) => ({ ...result, messageId: memberId })) : [result];
})
: batchResult.results;
const fannedOutResults =
groupMapping.size > 0
? batchResult.results.flatMap((result) => {
const members = groupMapping.get(result.messageId);
return members
? members.map((memberId) => ({ ...result, messageId: memberId }))
: [result];
})
: batchResult.results;
allResults.push(...fannedOutResults);
if (batchResult.raw) lastRaw = batchResult.raw;
logModerationAnalysis(targetIds, config.AI_LLM_MODEL, batchResult.results, 0, undefined);
logModerationAnalysis(
targetIds,
config.AI_LLM_MODEL,
batchResult.results,
0,
undefined,
);
}
log.debug({ targetCount: targets.length, resultCount: allResults.length, subBatchCount: subBatches.length }, "Text-only batch analysis complete");
log.debug(
{
targetCount: targets.length,
resultCount: allResults.length,
subBatchCount: subBatches.length,
},
"Text-only batch analysis complete",
);
return { results: allResults, raw: lastRaw };
}
@@ -359,27 +521,46 @@ async function runMediaBatch(
if (!targets.length) return { results: [], raw: null };
// Lazy init sticker cache
const { isStickerCacheReady, initStickerCache } = await import("./stickerCache.js");
const { isStickerCacheReady, initStickerCache } = await import(
"./stickerCache.js"
);
if (!isStickerCacheReady()) {
await initStickerCache().catch((err: unknown) => log.warn({ error: err instanceof Error ? err.message : String(err) }, "Sticker cache init failed"));
await initStickerCache().catch((err: unknown) =>
log.warn(
{ error: err instanceof Error ? err.message : String(err) },
"Sticker cache init failed",
),
);
}
// Phase A: Prepare ALL messages in parallel
const prepared = await Promise.all(targets.map((target) => prepareMediaMessage(target, attachments)));
const prepared = await Promise.all(
targets.map((target) => prepareMediaMessage(target, attachments)),
);
// Phase B: ONE batched LLM call
const targetIds = targets.map((t) => t.id);
const channelId = targets[0].channel_id;
const channelCultureObj = channelId ? await getChannelCulture(channelId) : null;
const channelCultureObj = channelId
? await getChannelCulture(channelId)
: null;
const channelCulture = channelCultureObj?.culture_summary;
const correctedExamples = await buildCorrectedFewShotExamples();
const systemText = buildSystemPromptModular({ contextText, mode: "mixed", correctedExamples, channelCulture });
const systemText = buildSystemPromptModular({
contextText,
mode: "mixed",
correctedExamples,
channelCulture,
});
const messagesBlock = prepared.map((p) => p.messageBlock).join("\n");
const userContent = `${systemText}\n\n<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`;
const perMsgTimeout = config.AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS ?? 60000;
const batchTimeout = Math.min(Math.max(perMsgTimeout, perMsgTimeout * targets.length), 300_000);
const batchTimeout = Math.min(
Math.max(perMsgTimeout, perMsgTimeout * targets.length),
300_000,
);
const abortController = new AbortController();
const timeoutId = setTimeout(() => abortController.abort(), batchTimeout);
@@ -392,11 +573,16 @@ async function runMediaBatch(
`media-batch:${targetIds.length}msgs`,
abortController.signal,
);
log.info({ mediaCount: targets.length, resultCount: result.results.length }, "Media batch analysis complete");
log.info(
{ mediaCount: targets.length, resultCount: result.results.length },
"Media batch analysis complete",
);
return result;
} catch (err: any) {
if (err.name === "AbortError" || abortController.signal.aborted) {
throw new Error(`Media batch analysis timed out after ${batchTimeout}ms for ${targets.length} messages`);
throw new Error(
`Media batch analysis timed out after ${batchTimeout}ms for ${targets.length} messages`,
);
}
throw err;
} finally {
@@ -441,10 +627,16 @@ export async function runModerationAnalysis(
for (const target of targets) {
const hasMedia = hasMediaContent(target, attachments);
if (hasMedia) { uncachedTargets.push(target); continue; }
if (hasMedia) {
uncachedTargets.push(target);
continue;
}
const rawContent = target.edited_content ?? target.content;
if (!rawContent.trim()) { uncachedTargets.push(target); continue; }
if (!rawContent.trim()) {
uncachedTargets.push(target);
continue;
}
const cacheKey = makeTextModerationCacheKey(rawContent);
if (seenCacheKeys.has(cacheKey)) {
@@ -461,15 +653,35 @@ export async function runModerationAnalysis(
try {
const cached = await getCachedTextModeration(cacheKey);
if (cached) {
const hasMediaInMeta = target.metadata && (() => {
const ev = extractMessageMediaEvidence(target.metadata);
return ev.attachments.length > 0 || ev.stickers.length > 0 || ev.embeds.length > 0;
})();
const hasMediaInMeta =
target.metadata &&
(() => {
const ev = extractMessageMediaEvidence(target.metadata);
return (
ev.attachments.length > 0 ||
ev.stickers.length > 0 ||
ev.embeds.length > 0
);
})();
if (hasMediaInMeta) {
log.debug({ messageId: target.id, cacheKey }, "Cache entry but message has media — treating as miss");
} else if (cached.flags.some((f) => ["analysis_api_failed", "analysis_parse_failed", "analysis_incomplete"].includes(f))) {
log.warn({ messageId: target.id, cacheKey }, "Cache entry contains error artifact — treating as miss");
log.debug(
{ messageId: target.id, cacheKey },
"Cache entry but message has media — treating as miss",
);
} else if (
cached.flags.some((f) =>
[
"analysis_api_failed",
"analysis_parse_failed",
"analysis_incomplete",
].includes(f),
)
) {
log.warn(
{ messageId: target.id, cacheKey },
"Cache entry contains error artifact — treating as miss",
);
} else {
cacheHits.push({
messageId: target.id,
@@ -480,20 +692,30 @@ export async function runModerationAnalysis(
categories: cached.categories,
severity: cached.severity as AnalysisResult["severity"],
confidence: cached.confidence,
recommendedAction: cached.recommendedAction as AnalysisResult["recommendedAction"],
recommendedAction:
cached.recommendedAction as AnalysisResult["recommendedAction"],
policyVersion: "cached-user-moderation-2026-06",
evidence: [],
} as AnalysisResult);
continue;
}
}
} catch { /* proceed */ }
} catch {
/* proceed */
}
uncachedTargets.push(target);
}
if (cacheHits.length > 0) {
log.info({ cacheHits: cacheHits.length, uncached: uncachedTargets.length, total: targets.length }, "User moderation cache applied");
log.info(
{
cacheHits: cacheHits.length,
uncached: uncachedTargets.length,
total: targets.length,
},
"User moderation cache applied",
);
}
if (uncachedTargets.length === 0) return { results: cacheHits, raw: null };
@@ -509,7 +731,15 @@ export async function runModerationAnalysis(
}
}
log.debug({ total: targets.length, textOnly: textOnlyTargets.length, media: mediaTargets.length, cacheHits: cacheHits.length }, "Split uncached targets");
log.debug(
{
total: targets.length,
textOnly: textOnlyTargets.length,
media: mediaTargets.length,
cacheHits: cacheHits.length,
},
"Split uncached targets",
);
// Run both paths in parallel
const [textBatchResult, mediaBatchResult] = await Promise.all([
@@ -531,7 +761,12 @@ export async function runModerationAnalysis(
if (target.metadata) {
const evidence = extractMessageMediaEvidence(target.metadata);
if (evidence.attachments.length > 0 || evidence.stickers.length > 0 || evidence.embeds.length > 0) continue;
if (
evidence.attachments.length > 0 ||
evidence.stickers.length > 0 ||
evidence.embeds.length > 0
)
continue;
}
const cacheKey = makeTextModerationCacheKey(rawContent);
@@ -547,10 +782,21 @@ export async function runModerationAnalysis(
}).catch(() => {});
}
const allResults = [...cacheHits, ...textBatchResult.results, ...mediaBatchResult.results];
const allResults = [
...cacheHits,
...textBatchResult.results,
...mediaBatchResult.results,
];
const raw = textBatchResult.raw ?? mediaBatchResult.raw;
log.debug({ targetCount: targets.length, resultCount: allResults.length, cacheHits: cacheHits.length }, "Moderation analysis complete");
log.debug(
{
targetCount: targets.length,
resultCount: allResults.length,
cacheHits: cacheHits.length,
},
"Moderation analysis complete",
);
return { results: allResults, raw };
}
@@ -568,7 +814,10 @@ export async function runSimpleTextFallback(
): Promise<AnalysisResult> {
const content = getAnalysisContent(message);
const MAX_CONTENT_CHARS = 500;
const truncatedContent = content.length > MAX_CONTENT_CHARS ? content.slice(0, MAX_CONTENT_CHARS) + "..." : content;
const truncatedContent =
content.length > MAX_CONTENT_CHARS
? content.slice(0, MAX_CONTENT_CHARS) + "..."
: content;
let userProfileCtx = "";
try {
@@ -576,7 +825,9 @@ export async function runSimpleTextFallback(
if (profile?.profile_summary) {
userProfileCtx = `\n\nProfil pengirim pesan:\n${sanitizeAiContent(profile.profile_summary, 2000, false)}\n`;
}
} catch { /* non-fatal */ }
} catch {
/* non-fatal */
}
// Step 1: Single-word classification
const classifyPrompt = `Pesan berikut perlu diklasifikasikan sebagai: clean, warn, atau flagged.
@@ -603,13 +854,20 @@ Jawab HANYA dengan satu kata: clean, warn, atau flagged`;
max_tokens: 10,
temperature: 0.1,
});
const raw = completion?.choices[0]?.message?.content?.trim().toLowerCase() ?? "";
const raw =
completion?.choices[0]?.message?.content?.trim().toLowerCase() ?? "";
if (raw.includes("flagged")) status = "flagged";
else if (raw.includes("warn")) status = "warn";
else status = "clean";
log.info({ messageId: message.id, status, raw }, "Simple fallback step 1");
} catch (error) {
log.warn({ messageId: message.id, error: error instanceof Error ? error.message : String(error) }, "Simple fallback step 1 failed — defaulting to clean");
log.warn(
{
messageId: message.id,
error: error instanceof Error ? error.message : String(error),
},
"Simple fallback step 1 failed — defaulting to clean",
);
status = "clean";
}
@@ -621,7 +879,8 @@ Jawab HANYA dengan satu kata: clean, warn, atau flagged`;
analysis = `${message.username ?? "user"}: ${content.length > 200 ? content.slice(0, 200) + "..." : content}. Percakapan normal, tidak ada pelanggaran.`;
} else {
category = status === "flagged" ? "harassment" : "spam";
const categoryOptions = status === "flagged" ? "harassment, gambling, atau sara" : "spam";
const categoryOptions =
status === "flagged" ? "harassment, gambling, atau sara" : "spam";
const reasonPrompt = `Pesan berikut telah diklasifikasikan sebagai "${status}".
${userProfileCtx}
Pesan: "${truncatedContent}"
@@ -659,13 +918,28 @@ Kategori: spam`;
const categoryMatch = analysis.match(/[Kk]ategori:\s*(\w+)/i);
if (categoryMatch) {
const parsedCat = categoryMatch[1].toLowerCase();
if (["harassment", "spam", "gambling", "sara"].includes(parsedCat)) category = parsedCat;
if (["harassment", "spam", "gambling", "sara"].includes(parsedCat))
category = parsedCat;
analysis = analysis.replace(/[Kk]ategori:\s*\w+\s*/i, "").trim();
}
log.info({ messageId: message.id, status, category, analysis: analysis.slice(0, 100) }, "Simple fallback step 2");
log.info(
{
messageId: message.id,
status,
category,
analysis: analysis.slice(0, 100),
},
"Simple fallback step 2",
);
} catch (error) {
analysis = `Pesan diklasifikasikan sebagai ${status} oleh sistem moderasi otomatis berdasarkan analisis konten.`;
log.warn({ messageId: message.id, error: error instanceof Error ? error.message : String(error) }, "Simple fallback step 2 failed");
log.warn(
{
messageId: message.id,
error: error instanceof Error ? error.message : String(error),
},
"Simple fallback step 2 failed",
);
}
}
@@ -676,10 +950,15 @@ Kategori: spam`;
score: status === "flagged" ? 0.7 : status === "warn" ? 0.4 : 0,
analysis,
categories: status === "clean" ? [] : [category],
severity: status === "flagged" ? "medium" : status === "warn" ? "low" : "none",
severity:
status === "flagged" ? "medium" : status === "warn" ? "low" : "none",
confidence: 0.6,
recommendedAction: status === "flagged" ? "review" : status === "warn" ? "warn" : "none",
recommendedAction:
status === "flagged" ? "review" : status === "warn" ? "warn" : "none",
policyVersion: "default-simple-2026-06",
evidence: status !== "clean" ? [content.length > 120 ? content.slice(0, 120) + "..." : content] : [],
evidence:
status !== "clean"
? [content.length > 120 ? content.slice(0, 120) + "..." : content]
: [],
};
}
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
import Redis from "ioredis";
import { createChildLogger } from "@bete/shared/logger";
import { createAbortControllerWithTimeout } from "@bete/shared/utils";
import Redis from "ioredis";
const log = createChildLogger("searxng-search");
@@ -103,7 +103,10 @@ export async function searchSearxng(
});
}
log.debug({ query, category, resultCount: mapped.length }, "SearXNG search OK");
log.debug(
{ query, category, resultCount: mapped.length },
"SearXNG search OK",
);
return mapped;
} finally {
clear();
@@ -151,7 +154,10 @@ export function extractSearchQueries(content: string): string[] {
);
if (titleBeforeCategory) {
const title = titleBeforeCategory[1].trim();
if (title.length >= 3 && !/^(yang|yang|sama|dari|untuk|ini|itu|ada)$/i.test(title)) {
if (
title.length >= 3 &&
!/^(yang|yang|sama|dari|untuk|ini|itu|ada)$/i.test(title)
) {
queries.add(title);
}
}
@@ -163,7 +169,8 @@ export function extractSearchQueries(content: string): string[] {
if (properNouns) {
for (const noun of properNouns) {
// Skip common non-title proper nouns
const skip = /^(Discord|YouTube|Google|Facebook|Instagram|Twitter|Github|ChatGPT|OpenAI|Claude|Telegram|WhatsApp|TikTok|Netflix|Spotify|Steam|Instagram)$/i;
const skip =
/^(Discord|YouTube|Google|Facebook|Instagram|Twitter|Github|ChatGPT|OpenAI|Claude|Telegram|WhatsApp|TikTok|Netflix|Spotify|Steam|Instagram)$/i;
if (!skip.test(noun) && noun.length >= 5) {
queries.add(noun);
}
@@ -113,7 +113,8 @@ export async function fetchUrlSafely(
return { url, type: "error", error: "Unsafe URL blocked" };
}
const { controller, clear } = createAbortControllerWithTimeout(FETCH_TIMEOUT_MS);
const { controller, clear } =
createAbortControllerWithTimeout(FETCH_TIMEOUT_MS);
try {
const response = await fetch(url, {
@@ -41,7 +41,10 @@ async function learnUserProfile(
}
// Group messages by channel for channel-aware profiling
const channelGroups = new Map<string, { content: string; channelId: string }[]>();
const channelGroups = new Map<
string,
{ content: string; channelId: string }[]
>();
for (const msg of recentMessages) {
const ch = msg.channelId ?? "unknown";
if (!channelGroups.has(ch)) channelGroups.set(ch, []);
@@ -78,10 +78,7 @@ export class CommandHandler {
this.voiceController = voiceController;
// Create domain-specific handlers with their dependencies
this.voiceHandler = new VoiceHandler(
client,
voiceController,
);
this.voiceHandler = new VoiceHandler(client, voiceController);
this.mediaHandler = new MediaHandler();
this.guildHandler = new GuildHandler(client);
this.moderationHandler = new ModerationHandler(client);
@@ -1,10 +1,16 @@
import { randomUUID } from "node:crypto";
import { type CommandMessage, type CommandReply } from "@bete/shared";
import { createChildLogger } from "@bete/shared/logger";
import { StreamType } from "@discordjs/voice";
import { randomUUID } from "node:crypto";
import { extractMediaInfo, resolveMediaUrl } from "../voice-recording/mediaSource.js";
import {
extractMediaInfo,
resolveMediaUrl,
} from "../voice-recording/mediaSource.js";
import type {
MediaMode,
MediaQueueItem,
} from "../voice-recording/mediaTypes.js";
import { discordPlayer } from "../voice-recording/player.js";
import type { MediaMode, MediaQueueItem } from "../voice-recording/mediaTypes.js";
// ---------------------------------------------------------------------------
// Types
@@ -51,8 +57,7 @@ function mapToStatusItem(item: MediaQueueItem): MediaStatusItem {
function buildStatusPayload(): MediaStatusPayload {
return {
playing:
currentTrackItem !== null &&
discordPlayer.getStatus() === "playing",
currentTrackItem !== null && discordPlayer.getStatus() === "playing",
musicVolume: discordPlayer.getMusicVolume(),
current: currentTrackItem ? mapToStatusItem(currentTrackItem) : null,
queue: mediaQueue.map(mapToStatusItem),
@@ -81,8 +86,7 @@ export class MediaHandler {
async handleMediaQueue(cmd: CommandMessage): Promise<CommandReply<unknown>> {
const url = String(cmd.payload.url ?? "").trim();
const mode: MediaMode =
cmd.payload.mode === "screen" ? "screen" : "music";
const mode: MediaMode = cmd.payload.mode === "screen" ? "screen" : "music";
const requestedBy = String(cmd.payload.requestedBy ?? "unknown");
if (!url) {
@@ -96,9 +100,7 @@ export class MediaHandler {
}
if (!discordPlayer.isConnected()) {
this.logger.warn(
"media:queue attempted without active voice connection",
);
this.logger.warn("media:queue attempted without active voice connection");
return {
id: cmd.id,
success: false,
@@ -256,7 +258,10 @@ export class MediaHandler {
// Try the next item in the queue
setImmediate(() => {
this.playNext().catch((err2) => {
this.logger.error({ err: err2 }, "playNext after error recovery failed");
this.logger.error(
{ err: err2 },
"playNext after error recovery failed",
);
});
});
}
@@ -291,8 +291,7 @@ export function getMessageMetadata(message: Message): RichMessageMetadata {
messageId: ref.messageId ?? null,
channelId: ref.channelId ?? null,
guildId: ref.guildId ?? null,
type:
(ref.type as unknown as string | undefined) ?? null,
type: (ref.type as unknown as string | undefined) ?? null,
content: referenceContent?.content ?? null,
repliedUsername: referenceContent?.username ?? null,
repliedUserId: referenceContent?.userId ?? null,
@@ -62,7 +62,10 @@ export function runFfmpeg(args: string[]): Promise<void> {
resolve();
} else {
const detail = stderrBuf.trim().slice(0, 2000);
logger.warn({ exitCode: code, stderr: detail }, "ffmpeg exited with non-zero code");
logger.warn(
{ exitCode: code, stderr: detail },
"ffmpeg exited with non-zero code",
);
reject(new Error(`ffmpeg exited with code ${code}: ${detail}`));
}
});
+17 -15
View File
@@ -1,7 +1,7 @@
use serde::de::DeserializeOwned;
use wasm_bindgen::prelude::*;
use web_sys::{Request, RequestInit, RequestMode, Headers, Response};
use wasm_bindgen_futures::JsFuture;
use web_sys::{Headers, Request, RequestInit, RequestMode, Response};
#[derive(Debug)]
pub struct ApiError {
@@ -22,7 +22,9 @@ fn get_base_url() -> String {
let location = window.location();
let protocol = location.protocol().unwrap_or_else(|_| "http:".to_string());
let protocol = protocol.trim_end_matches(':');
let host = location.host().unwrap_or_else(|_| "localhost:3001".to_string());
let host = location
.host()
.unwrap_or_else(|_| "localhost:3001".to_string());
format!("{}://{}", protocol, host)
} else {
"http://localhost:3001".to_string()
@@ -88,12 +90,10 @@ pub async fn request<T: DeserializeOwned>(
let status = response.status();
if status >= 400 {
let text = JsFuture::from(
response.text().map_err(|_| ApiError {
message: "Failed to read error body".to_string(),
status_code: status,
})?
)
let text = JsFuture::from(response.text().map_err(|_| ApiError {
message: "Failed to read error body".to_string(),
status_code: status,
})?)
.await
.ok()
.and_then(|v| v.as_string())
@@ -105,12 +105,10 @@ pub async fn request<T: DeserializeOwned>(
});
}
let text = JsFuture::from(
response.text().map_err(|_| ApiError {
message: "Failed to read response body".to_string(),
status_code: status,
})?
)
let text = JsFuture::from(response.text().map_err(|_| ApiError {
message: "Failed to read response body".to_string(),
status_code: status,
})?)
.await
.map_err(|_| ApiError {
message: "Failed to await response".to_string(),
@@ -123,7 +121,11 @@ pub async fn request<T: DeserializeOwned>(
})?;
serde_json::from_str(&text).map_err(|e| ApiError {
message: format!("JSON parse error: {} — body: {}", e, &text[..text.len().min(200)]),
message: format!(
"JSON parse error: {} — body: {}",
e,
&text[..text.len().min(200)]
),
status_code: status,
})
}
+36 -11
View File
@@ -14,10 +14,18 @@ pub async fn get_dashboard_users(
) -> Result<PaginatedUsers, ApiError> {
let mut path = "/api/dashboard/users".to_string();
let mut params = vec![];
if let Some(l) = limit { params.push(format!("limit={}", l)); }
if let Some(c) = cursor { params.push(format!("cursor={}", c)); }
if let Some(s) = search { params.push(format!("search={}", s)); }
if !params.is_empty() { path.push_str(&format!("?{}", params.join("&"))); }
if let Some(l) = limit {
params.push(format!("limit={}", l));
}
if let Some(c) = cursor {
params.push(format!("cursor={}", c));
}
if let Some(s) = search {
params.push(format!("search={}", s));
}
if !params.is_empty() {
path.push_str(&format!("?{}", params.join("&")));
}
request("GET", &path, None).await
}
@@ -42,11 +50,21 @@ pub async fn get_dashboard_channels(
) -> Result<PaginatedChannels, ApiError> {
let mut path = "/api/dashboard/channels".to_string();
let mut params = vec![];
if let Some(l) = limit { params.push(format!("limit={}", l)); }
if let Some(c) = cursor { params.push(format!("cursor={}", c)); }
if let Some(s) = search { params.push(format!("search={}", s)); }
if let Some(g) = guild_id { params.push(format!("guild_id={}", g)); }
if !params.is_empty() { path.push_str(&format!("?{}", params.join("&"))); }
if let Some(l) = limit {
params.push(format!("limit={}", l));
}
if let Some(c) = cursor {
params.push(format!("cursor={}", c));
}
if let Some(s) = search {
params.push(format!("search={}", s));
}
if let Some(g) = guild_id {
params.push(format!("guild_id={}", g));
}
if !params.is_empty() {
path.push_str(&format!("?{}", params.join("&")));
}
request("GET", &path, None).await
}
@@ -58,6 +76,13 @@ pub struct PaginatedChannels {
}
/// GET /api/dashboard/channels/{channelId}
pub async fn get_dashboard_channel_detail(channel_id: &str) -> Result<DashboardChannelDetail, ApiError> {
request("GET", &format!("/api/dashboard/channels/{}", channel_id), None).await
pub async fn get_dashboard_channel_detail(
channel_id: &str,
) -> Result<DashboardChannelDetail, ApiError> {
request(
"GET",
&format!("/api/dashboard/channels/{}", channel_id),
None,
)
.await
}
+36 -10
View File
@@ -9,9 +9,15 @@ pub async fn get_messages(
cursor: Option<&str>,
) -> Result<PageResult<MessageRecord>, ApiError> {
let mut path = format!("/api/messages?guildId={}", guild_id);
if let Some(l) = limit { path.push_str(&format!("&limit={}", l)); }
if let Some(c) = channel_id { path.push_str(&format!("&channelId={}", c)); }
if let Some(c) = cursor { path.push_str(&format!("&cursor={}", c)); }
if let Some(l) = limit {
path.push_str(&format!("&limit={}", l));
}
if let Some(c) = channel_id {
path.push_str(&format!("&channelId={}", c));
}
if let Some(c) = cursor {
path.push_str(&format!("&cursor={}", c));
}
request("GET", &path, None).await
}
@@ -22,8 +28,12 @@ pub async fn get_review_messages(
channel_id: Option<&str>,
) -> Result<PageResult<MessageRecord>, ApiError> {
let mut path = format!("/api/review?guildId={}", guild_id);
if let Some(l) = limit { path.push_str(&format!("&limit={}", l)); }
if let Some(c) = channel_id { path.push_str(&format!("&channelId={}", c)); }
if let Some(l) = limit {
path.push_str(&format!("&limit={}", l));
}
if let Some(c) = channel_id {
path.push_str(&format!("&channelId={}", c));
}
request("GET", &path, None).await
}
@@ -34,24 +44,40 @@ pub async fn get_message_detail(id: &str) -> Result<Option<MessageRecord>, ApiEr
/// POST /api/messages/{id}/reanalyze
pub async fn reanalyze_message(id: &str) -> Result<(), ApiError> {
let _: serde_json::Value = request("POST", &format!("/api/messages/{}/reanalyze", id), Some("{}")).await?;
let _: serde_json::Value = request(
"POST",
&format!("/api/messages/{}/reanalyze", id),
Some("{}"),
)
.await?;
Ok(())
}
/// POST /api/messages/reanalyze-batch
pub async fn reanalyze_batch() -> Result<u64, ApiError> {
#[derive(serde::Deserialize)]
struct BatchResp { ok: bool, count: u64 }
#[allow(dead_code)]
struct BatchResp {
ok: bool,
count: u64,
}
let resp: BatchResp = request("POST", "/api/messages/reanalyze-batch", Some("{}")).await?;
Ok(resp.count)
}
/// GET /api/analysis/search?q=&limit=
pub async fn search_messages(query: &str, limit: Option<u32>) -> Result<Vec<MessageRecord>, ApiError> {
pub async fn search_messages(
query: &str,
limit: Option<u32>,
) -> Result<Vec<MessageRecord>, ApiError> {
#[derive(serde::Deserialize)]
struct SearchResult { results: Vec<MessageRecord> }
struct SearchResult {
results: Vec<MessageRecord>,
}
let mut path = format!("/api/analysis/search?q={}", query);
if let Some(l) = limit { path.push_str(&format!("&limit={}", l)); }
if let Some(l) = limit {
path.push_str(&format!("&limit={}", l));
}
let resp: SearchResult = request("GET", &path, None).await?;
Ok(resp.results)
}
+3 -3
View File
@@ -1,7 +1,7 @@
pub mod client;
pub mod auth;
pub mod messages;
pub mod voice;
pub mod client;
pub mod dashboard;
pub mod mascot;
pub mod messages;
pub mod recordings;
pub mod voice;
@@ -8,9 +8,15 @@ pub async fn get_recordings(
) -> Result<VoiceRecordingListResponse, ApiError> {
let mut path = "/api/recordings".to_string();
let mut params = vec![];
if let Some(l) = limit { params.push(format!("limit={}", l)); }
if let Some(c) = cursor { params.push(format!("cursor={}", c)); }
if !params.is_empty() { path.push_str(&format!("?{}", params.join("&"))); }
if let Some(l) = limit {
params.push(format!("limit={}", l));
}
if let Some(c) = cursor {
params.push(format!("cursor={}", c));
}
if !params.is_empty() {
path.push_str(&format!("?{}", params.join("&")));
}
request("GET", &path, None).await
}
+17 -8
View File
@@ -1,8 +1,8 @@
use crate::api::client::{request, request_no_body, ApiError};
use shared_types::voice::VoiceStatus;
use shared_types::media::MediaState;
use shared_types::guild::{Guild, Channel};
use crate::api::client::{request, ApiError};
use serde::Serialize;
use shared_types::guild::{Channel, Guild};
use shared_types::media::MediaState;
use shared_types::voice::VoiceStatus;
/// GET /api/guilds
pub async fn get_guilds() -> Result<Vec<Guild>, ApiError> {
@@ -11,7 +11,12 @@ pub async fn get_guilds() -> Result<Vec<Guild>, ApiError> {
/// GET /api/guilds/{guildId}/voice-channels
pub async fn get_voice_channels(guild_id: &str) -> Result<Vec<Channel>, ApiError> {
request("GET", &format!("/api/guilds/{}/voice-channels", guild_id), None).await
request(
"GET",
&format!("/api/guilds/{}/voice-channels", guild_id),
None,
)
.await
}
/// GET /api/guilds/{guildId}/channels
@@ -35,7 +40,8 @@ pub async fn connect_voice(guild_id: &str, channel_id: &str) -> Result<VoiceStat
let body = serde_json::to_string(&ConnectPayload {
guild_id: guild_id.to_string(),
channel_id: channel_id.to_string(),
}).unwrap();
})
.unwrap();
request("POST", "/api/voice/connect", Some(&body)).await
}
@@ -59,7 +65,8 @@ pub async fn media_queue(source: &str, mode: &str) -> Result<MediaState, ApiErro
let body = serde_json::to_string(&MediaQueuePayload {
source: source.to_string(),
mode: mode.to_string(),
}).unwrap();
})
.unwrap();
request("POST", "/api/media/queue", Some(&body)).await
}
@@ -75,7 +82,9 @@ pub async fn media_stop() -> Result<MediaState, ApiError> {
/// POST /api/media/volume { volume }
#[derive(Serialize)]
struct VolumePayload { volume: f64 }
struct VolumePayload {
volume: f64,
}
pub async fn media_volume(volume: f64) -> Result<MediaState, ApiError> {
let body = serde_json::to_string(&VolumePayload { volume }).unwrap();
request("POST", "/api/media/volume", Some(&body)).await
+1
View File
@@ -167,6 +167,7 @@ img {
.gap-4 { gap: var(--space-4); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }
.shrink-0 { flex-shrink: 0; }
.grid { display: grid; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
+33 -39
View File
@@ -1,12 +1,30 @@
use leptos::prelude::*;
use shared_types::ui_state::Tab;
use crate::auth::AuthOverlay;
use crate::ws::context::WsContext;
use crate::features::dashboard::DashboardPanel;
use crate::features::live::LivePanel;
use crate::features::messages::MessagesPanel;
use crate::features::polish::{initial_theme, ThemeContext};
use crate::features::polish::components::{MascotChatbot, ParticleBackground, ThemeToggle};
use crate::features::polish::{initial_theme, ThemeContext};
use crate::ws::context::WsContext;
use leptos::prelude::*;
use shared_types::ui_state::Tab;
/// Derive WebSocket URL from the page's own origin.
/// In development (serve on :8080, backend on :3001) use the detected host + /ws path.
/// In production (nginx proxies /ws to backend) the same logic works.
fn get_ws_url() -> String {
web_sys::window()
.map(|w| {
let loc = w.location();
let protocol = loc.protocol().unwrap_or_else(|_| "http:".to_string());
let host = loc.host().unwrap_or_else(|_| "localhost:3001".to_string());
let ws_proto = if protocol.starts_with("https") {
"wss"
} else {
"ws"
};
format!("{}://{}/ws", ws_proto, host)
})
.unwrap_or_else(|| "ws://localhost:3001/ws".to_string())
}
#[derive(Clone)]
pub struct AppConfig {
@@ -33,15 +51,15 @@ pub struct UiContext {
pub fn App() -> impl IntoView {
// Initialize contexts
let auth = AuthContext {
authenticated: create_rw_signal(false),
password: create_rw_signal(String::new()),
authenticated: RwSignal::new(false),
password: RwSignal::new(String::new()),
};
let ui = UiContext {
active_tab: create_rw_signal(Tab::Messages),
selected_guild: create_rw_signal(None),
active_tab: RwSignal::new(Tab::Messages),
selected_guild: RwSignal::new(None),
};
let theme = ThemeContext {
theme: create_rw_signal(initial_theme()),
theme: RwSignal::new(initial_theme()),
};
provide_context(auth.clone());
@@ -53,35 +71,15 @@ pub fn App() -> impl IntoView {
};
provide_context(config);
let ws = WsContext::new("ws://localhost:3001/ws");
let ws = WsContext::new(&get_ws_url());
provide_context(ws.clone());
// Auth check: redirect "live" tab to "messages" if not authenticated
create_effect(move |_| {
if !auth.authenticated.get() && ui.active_tab.get() == Tab::Live {
ui.active_tab.set(Tab::Messages);
}
});
{
let ws = ws.clone();
let auth = auth.clone();
create_effect(move |_| {
if auth.authenticated.get() {
ws.connect();
}
});
}
ws.connect();
view! {
<div data-theme=move || theme.theme.get()>
<ParticleBackground />
// Auth overlay
{move || (!auth.authenticated.get()).then(|| {
view! { <AuthOverlay /> }
})}
// Main content
<div class="app-shell">
<header class="app-header">
@@ -96,8 +94,8 @@ pub fn App() -> impl IntoView {
<nav class="app-sidebar">
<div class="flex flex-col gap-2">
<TabButton tab=Tab::Messages ui=ui.clone() label="Pesan & Moderasi" />
<TabButton tab=Tab::Live ui=ui.clone() label="Voice & Media" />
<TabButton tab=Tab::Dashboard ui=ui.clone() label="Dashboard Guild" />
<TabButton tab=Tab::Live ui=ui.clone() label="Voice & Media" />
</div>
</nav>
@@ -119,12 +117,8 @@ pub fn App() -> impl IntoView {
// ── Tab Button Helper ───────────────────────────────────
#[component]
fn TabButton(
tab: Tab,
ui: UiContext,
label: &'static str,
) -> impl IntoView {
let active_tab = ui.active_tab.clone();
fn TabButton(tab: Tab, ui: UiContext, label: &'static str) -> impl IntoView {
let active_tab = ui.active_tab;
let tab1 = tab.clone();
let tab2 = tab.clone();
let tab3 = tab.clone();
+7 -7
View File
@@ -1,15 +1,15 @@
// services/frontend-leptos/frontend/src/auth.rs
use crate::api::auth as auth_api;
use crate::app::AuthContext;
use leptos::prelude::*;
use wasm_bindgen_futures::spawn_local;
use crate::app::AuthContext;
use crate::api::auth as auth_api;
#[component]
pub fn AuthOverlay() -> impl IntoView {
let auth = use_context::<AuthContext>().expect("AuthContext not provided");
let (password, set_password) = create_signal(String::new());
let (error, set_error) = create_signal(Option::<String>::None);
let (loading, set_loading) = create_signal(false);
let (password, set_password) = signal(String::new());
let (error, set_error) = signal(Option::<String>::None);
let (loading, set_loading) = signal(false);
let handle_submit = move |ev: leptos::ev::SubmitEvent| {
ev.prevent_default();
@@ -23,8 +23,8 @@ pub fn AuthOverlay() -> impl IntoView {
let auth_clone = auth.clone();
let pwd_clone = pwd.clone();
let set_loading_clone = set_loading.clone();
let set_error_clone = set_error.clone();
let set_loading_clone = set_loading;
let set_error_clone = set_error;
spawn_local(async move {
match auth_api::login(&pwd_clone).await {
@@ -79,7 +79,10 @@ pub fn ChannelSummaryList(
#[component]
fn ChannelRow(channel: DashboardChannel) -> impl IntoView {
let name = channel.channel_name.clone().unwrap_or_else(|| channel.channel_id.clone());
let name = channel
.channel_name
.clone()
.unwrap_or_else(|| channel.channel_id.clone());
let summary = channel
.culture_summary
.clone()
@@ -125,7 +128,9 @@ fn format_number(value: u64) -> String {
let raw = value.to_string();
let mut out = String::new();
for (idx, ch) in raw.chars().rev().enumerate() {
if idx > 0 && idx % 3 == 0 { out.push(','); }
if idx > 0 && idx % 3 == 0 {
out.push(',');
}
out.push(ch);
}
out.chars().rev().collect()
@@ -133,5 +138,6 @@ fn format_number(value: u64) -> String {
fn format_timestamp(ts: i64) -> String {
let d = js_sys::Date::new(&wasm_bindgen::JsValue::from_f64((ts as f64) * 1000.0));
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED).into()
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED)
.into()
}
@@ -1,7 +1,7 @@
pub mod channel_summary_list;
pub mod stats_overview;
pub mod user_summary_list;
pub mod channel_summary_list;
pub use channel_summary_list::ChannelSummaryList;
pub use stats_overview::StatsOverview;
pub use user_summary_list::UserSummaryList;
pub use channel_summary_list::ChannelSummaryList;
@@ -73,7 +73,12 @@ pub fn StatsOverview(
}
#[component]
fn MetricCard(label: &'static str, value: u64, icon: &'static str, tone: &'static str) -> impl IntoView {
fn MetricCard(
label: &'static str,
value: u64,
icon: &'static str,
tone: &'static str,
) -> impl IntoView {
view! {
<div class="card dashboard-metric-card">
<div class="dashboard-metric-content">
@@ -115,7 +120,8 @@ fn TopChannels(channels: Vec<TopChannel>) -> impl IntoView {
}
}).collect::<Vec<_>>()}
</div>
}.into_any()
}
.into_any()
}
#[component]
@@ -79,7 +79,10 @@ pub fn UserSummaryList(
#[component]
fn UserRow(user: DashboardUser) -> impl IntoView {
let name = user.username.clone().unwrap_or_else(|| user.user_id.clone());
let name = user
.username
.clone()
.unwrap_or_else(|| user.user_id.clone());
let summary = user
.profile_summary
.clone()
@@ -130,7 +133,9 @@ fn format_number(value: u64) -> String {
let raw = value.to_string();
let mut out = String::new();
for (idx, ch) in raw.chars().rev().enumerate() {
if idx > 0 && idx % 3 == 0 { out.push(','); }
if idx > 0 && idx % 3 == 0 {
out.push(',');
}
out.push(ch);
}
out.chars().rev().collect()
@@ -138,5 +143,6 @@ fn format_number(value: u64) -> String {
fn format_timestamp(ts: i64) -> String {
let d = js_sys::Date::new(&wasm_bindgen::JsValue::from_f64((ts as f64) * 1000.0));
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED).into()
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED)
.into()
}
@@ -56,7 +56,13 @@ pub fn DashboardPanel() -> impl IntoView {
let search = users_search.get();
spawn_local(async move {
let search_ref = (!search.trim().is_empty()).then_some(search.trim());
match crate::api::dashboard::get_dashboard_users(Some(20), cursor.as_deref(), search_ref).await {
match crate::api::dashboard::get_dashboard_users(
Some(20),
cursor.as_deref(),
search_ref,
)
.await
{
Ok(page) => {
if reset {
users.set(page.data);
@@ -84,7 +90,14 @@ pub fn DashboardPanel() -> impl IntoView {
let search = channels_search.get();
spawn_local(async move {
let search_ref = (!search.trim().is_empty()).then_some(search.trim());
match crate::api::dashboard::get_dashboard_channels(Some(20), cursor.as_deref(), search_ref, None).await {
match crate::api::dashboard::get_dashboard_channels(
Some(20),
cursor.as_deref(),
search_ref,
None,
)
.await
{
Ok(page) => {
if reset {
channels.set(page.data);
@@ -105,7 +118,7 @@ pub fn DashboardPanel() -> impl IntoView {
let fetch_stats = fetch_stats.clone();
let fetch_users = fetch_users.clone();
let fetch_channels = fetch_channels.clone();
create_effect(move |_| {
Effect::new(move |_| {
fetch_stats();
fetch_users(true);
fetch_channels(true);
@@ -131,67 +144,86 @@ pub fn DashboardPanel() -> impl IntoView {
</div>
<div class="tab-content" style:display=move || if active_tab.get() == DashboardTab::Stats { "block" } else { "none" }>
<StatsOverview
stats=stats.get()
loading=stats_loading.get()
error=stats_error.get()
on_retry=Box::new({
{move || {
let on_retry = {
let fetch_stats = fetch_stats.clone();
move || fetch_stats()
})
/>
Box::new(move || fetch_stats())
};
view! {
<StatsOverview
stats=stats.get()
loading=stats_loading.get()
error=stats_error.get()
on_retry=on_retry
/>
}
}}
</div>
<div class="tab-content" style:display=move || if active_tab.get() == DashboardTab::Users { "block" } else { "none" }>
<UserSummaryList
users=users.get()
loading=users_loading.get()
error=users_error.get()
search=users_search.get()
has_more=users_cursor.get().is_some()
on_search_change=Box::new({
{move || {
let on_search_change = {
let fetch_users = fetch_users.clone();
move |value| {
Box::new(move |value| {
users_search.set(value);
users_cursor.set(None);
fetch_users(true);
}
})
on_load_more=Box::new({
})
};
let on_load_more = {
let fetch_users = fetch_users.clone();
move || fetch_users(false)
})
on_retry=Box::new({
Box::new(move || fetch_users(false))
};
let on_retry = {
let fetch_users = fetch_users.clone();
move || fetch_users(true)
})
/>
Box::new(move || fetch_users(true))
};
view! {
<UserSummaryList
users=users.get()
loading=users_loading.get()
error=users_error.get()
search=users_search.get()
has_more=users_cursor.get().is_some()
on_search_change=on_search_change
on_load_more=on_load_more
on_retry=on_retry
/>
}
}}
</div>
<div class="tab-content" style:display=move || if active_tab.get() == DashboardTab::Channels { "block" } else { "none" }>
<ChannelSummaryList
channels=channels.get()
loading=channels_loading.get()
error=channels_error.get()
search=channels_search.get()
has_more=channels_cursor.get().is_some()
on_search_change=Box::new({
{move || {
let on_search_change = {
let fetch_channels = fetch_channels.clone();
move |value| {
Box::new(move |value| {
channels_search.set(value);
channels_cursor.set(None);
fetch_channels(true);
}
})
on_load_more=Box::new({
})
};
let on_load_more = {
let fetch_channels = fetch_channels.clone();
move || fetch_channels(false)
})
on_retry=Box::new({
Box::new(move || fetch_channels(false))
};
let on_retry = {
let fetch_channels = fetch_channels.clone();
move || fetch_channels(true)
})
/>
Box::new(move || fetch_channels(true))
};
view! {
<ChannelSummaryList
channels=channels.get()
loading=channels_loading.get()
error=channels_error.get()
search=channels_search.get()
has_more=channels_cursor.get().is_some()
on_search_change=on_search_change
on_load_more=on_load_more
on_retry=on_retry
/>
}
}}
</div>
</div>
</div>
@@ -1,2 +1,2 @@
pub mod ring_buffer;
pub mod pcm_decoder;
pub mod ring_buffer;
@@ -46,7 +46,7 @@ pub fn encode_samples_to_base64(samples: &[f32]) -> String {
// Convert f32 samples to i16 bytes
let mut bytes = Vec::with_capacity(samples.len() * 2);
for &sample in samples {
let clamped = sample.max(-1.0).min(1.0);
let clamped = sample.clamp(-1.0, 1.0);
let int_sample = (clamped * 32767.0) as i16;
bytes.extend_from_slice(&int_sample.to_le_bytes());
}
@@ -65,5 +65,3 @@ fn encode_bytes_base64(data: &[u8]) -> String {
.and_then(|r| r.as_string())
.unwrap_or_default()
}
use wasm_bindgen::prelude::*;
@@ -22,36 +22,36 @@ pub fn ActiveSpeakers(
key=|s| s.user_id.clone() + &s.username
let:speaker
>
<div class="flex items-center gap-3 rounded-xl border border-border bg-card p-3">
<div class="h-8 w-8 flex-shrink-0">
<div style="display:flex;align-items:center;gap:0.75rem;border-radius:0.75rem;border:1px solid var(--surface-border);background:var(--surface-base);padding:0.75rem">
<div style="width:2rem;height:2rem;flex-shrink:0">
{speaker.avatar.as_ref().map(|avatar_url| {
let url = avatar_url.clone();
view! {
<img
src=url
alt=""
class="h-8 w-8 rounded-full object-cover ring-2 ring-primary/30"
style="width:2rem;height:2rem;border-radius:9999px;object-fit:cover;box-shadow:0 0 0 2px rgba(35,161,235,0.3)"
/>
}
})}
</div>
<div class="min-w-0 flex-1">
<div style="min-width:0;flex:1">
<div class="truncate text-sm font-medium">
{speaker.username.clone()}
</div>
<div class="flex items-center gap-1.5">
<span class=move || {
<div style="display:flex;align-items:center;gap:0.375rem">
<span style=move || {
if speaker.speaking {
"inline-block h-2 w-2 rounded-full bg-emerald-500"
"display:inline-block;width:0.5rem;height:0.5rem;border-radius:9999px;background:#10b981"
} else {
"inline-block h-2 w-2 rounded-full bg-muted-foreground/40"
"display:inline-block;width:0.5rem;height:0.5rem;border-radius:9999px;background:color-mix(in srgb, var(--text-tertiary) 40%, transparent)"
}
}></span>
<span class=move || {
<span style=move || {
if speaker.speaking {
"text-xs font-medium text-emerald-600 dark:text-emerald-400"
"font-size:0.75rem;font-weight:500;color:#059669"
} else {
"text-xs font-medium text-muted-foreground"
"font-size:0.75rem;font-weight:500;color:var(--text-secondary)"
}
}>
{move || if speaker.speaking { "Speaking" } else { "Silent" }}
@@ -64,12 +64,12 @@ pub fn ActiveSpeakers(
}
}
>
<div class="rounded-xl border border-border bg-card p-8 text-center shadow-sm">
<div class="space-y-2">
<div class="text-4xl">
<div style="border-radius:0.75rem;border:1px solid var(--surface-border);background:var(--surface-base);padding:2rem;text-align:center">
<div>
<div style="font-size:2.25rem;line-height:2.5rem">
"🎤"
</div>
<p class="text-sm text-muted-foreground">
<p style="font-size:0.875rem;color:var(--text-secondary)">
"No active speakers"
</p>
</div>
@@ -8,17 +8,17 @@ pub fn AudioVisualizer(
#[prop(default = true)] _active: bool,
#[prop(optional)] pcm_data: Option<Arc<Mutex<Vec<f32>>>>,
) -> impl IntoView {
let bars = create_rw_signal::<Vec<f32>>(vec![0.0; 32]);
let bars = RwSignal::new(vec![0.0; 32]);
// Periodically update bars from PCM data
create_effect(move |_| {
Effect::new(move |_| {
if let Some(ref pcm_arc) = pcm_data {
if let Ok(pcm_vec) = pcm_arc.lock() {
let computed = compute_frequency_bands(&pcm_vec);
bars.update(|b| {
for i in 0..32 {
let target = computed.get(i).copied().unwrap_or(0.0).max(0.0).min(1.0);
b[i] = b[i] * 0.7 + target * 0.3; // Smooth decay
for (i, band) in b.iter_mut().enumerate() {
let target = computed.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0);
*band = *band * 0.7 + target * 0.3; // Smooth decay
}
});
}
@@ -9,11 +9,11 @@ pub fn MicLevelMeter(
#[prop(optional)] pcm_data: Option<Arc<Mutex<Vec<f32>>>>,
#[prop(optional)] label: Option<&'static str>,
) -> impl IntoView {
let level = create_rw_signal::<f32>(0.0);
let peak = create_rw_signal::<f32>(0.0);
let level = RwSignal::new(0.0f32);
let peak = RwSignal::new(0.0f32);
// Update level periodically
create_effect(move |_| {
Effect::new(move |_| {
if !active {
return;
}
@@ -1,19 +1,19 @@
pub mod voice_connection_card;
pub mod active_speakers;
pub mod audio_visualizer;
pub mod mic_level_meter;
pub mod now_playing;
pub mod music_sub_panel;
pub mod screen_sub_panel;
pub mod now_playing;
pub mod recordings_sub_panel;
pub mod screen_sub_panel;
pub mod voice_connection_card;
pub mod waveform_player;
pub use voice_connection_card::VoiceConnectionCard;
pub use active_speakers::ActiveSpeakers;
pub use audio_visualizer::AudioVisualizer;
pub use mic_level_meter::MicLevelMeter;
pub use now_playing::NowPlaying;
pub use music_sub_panel::MusicSubPanel;
pub use screen_sub_panel::ScreenSubPanel;
pub use now_playing::NowPlaying;
pub use recordings_sub_panel::RecordingsSubPanel;
pub use screen_sub_panel::ScreenSubPanel;
pub use voice_connection_card::VoiceConnectionCard;
pub use waveform_player::WaveformPlayer;
@@ -5,11 +5,11 @@ use leptos::prelude::*;
pub fn MusicSubPanel(
#[prop(optional)] on_queue: Option<Box<dyn Fn(String) + Send + Sync + 'static>>,
) -> impl IntoView {
let (url_input, set_url_input) = create_signal::<String>(String::new());
let (is_loading, set_is_loading) = create_signal::<bool>(false);
let (url_input, set_url_input) = signal::<String>(String::new());
let (is_loading, set_is_loading) = signal::<bool>(false);
let handle_queue_click = move |_| {
let url = url_input.get().trim().to_string();
let url = url_input.get_untracked().trim().to_string();
if !url.is_empty() {
if let Some(ref cb) = on_queue {
set_is_loading.set(true);
@@ -24,7 +24,7 @@ pub fn MusicSubPanel(
<div class="music-sub-panel card">
<div class="card-header">
<div class="card-title flex items-center gap-2">
<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<path d="M9 8h6v8h-6z"></path>
</svg>
@@ -8,7 +8,7 @@ pub fn NowPlaying(
#[prop(optional)] on_skip: Option<Box<dyn Fn() + Send + Sync + 'static>>,
#[prop(optional)] on_stop: Option<Box<dyn Fn() + Send + Sync + 'static>>,
) -> impl IntoView {
let media_state = create_rw_signal::<Option<MediaState>>(state);
let media_state = RwSignal::new(state);
// Wrap callbacks in StoredValue for shareable non-Clone ownership in Leptos context
let skip_cb = StoredValue::new(on_skip);
@@ -1,21 +1,27 @@
use crate::api::recordings::{delete_recording, get_recordings};
use leptos::prelude::*;
use shared_types::recording::VoiceRecording;
use crate::api::recordings::{get_recordings, delete_recording};
/// RecordingsSubPanel — Paginated list of voice recordings
#[component]
pub fn RecordingsSubPanel() -> impl IntoView {
let recordings = create_rw_signal::<Vec<VoiceRecording>>(Vec::new());
let loading = create_rw_signal::<bool>(false);
let has_more = create_rw_signal::<bool>(true);
let next_cursor = create_rw_signal::<Option<String>>(None);
let recordings = RwSignal::new(Vec::<VoiceRecording>::new());
let loading = RwSignal::new(false);
let has_more = RwSignal::new(true);
let next_cursor = RwSignal::new(None::<String>);
// Load recordings
let load = move |reset: bool| {
if loading.get() { return; }
if loading.get_untracked() {
return;
}
loading.set(true);
let cursor_val = if reset { None } else { next_cursor.get() };
let cursor_val = if reset {
None
} else {
next_cursor.get_untracked()
};
wasm_bindgen_futures::spawn_local({
async move {
match get_recordings(Some(20), cursor_val.as_deref()).await {
@@ -23,7 +29,7 @@ pub fn RecordingsSubPanel() -> impl IntoView {
if reset {
recordings.set(resp.items);
} else {
let mut current = recordings.get();
let mut current = recordings.get_untracked();
current.extend(resp.items);
recordings.set(current);
}
@@ -42,7 +48,7 @@ pub fn RecordingsSubPanel() -> impl IntoView {
};
// Load on mount
create_effect(move |_| {
Effect::new(move |_| {
load(true);
});
@@ -61,7 +67,7 @@ pub fn RecordingsSubPanel() -> impl IntoView {
<div class="recordings-sub-panel card">
<div class="card-header">
<div class="card-title flex items-center gap-2">
<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path>
<path d="M19 10v2a7 7 0 0 1-14 0v-2"></path>
<line x1="12" y1="19" x2="12" y2="23"></line>
@@ -106,7 +112,7 @@ pub fn RecordingsSubPanel() -> impl IntoView {
<span>{created_at}</span>
</div>
</div>
<div class="flex items-center gap-1.5 shrink-0">
<div class="flex items-center" style="gap:0.375rem;flex-shrink:0">
{has_url.then(|| {
view! {
<a
@@ -166,5 +172,6 @@ fn format_size(bytes: u64) -> String {
/// Format timestamp i64 to readable date
fn format_timestamp(ts: i64) -> String {
let d = js_sys::Date::new(&wasm_bindgen::JsValue::from_f64((ts as f64) * 1000.0));
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED).into()
d.to_locale_date_string("en-US", &wasm_bindgen::JsValue::UNDEFINED)
.into()
}
@@ -6,7 +6,7 @@ pub fn ScreenSubPanel(
#[prop(optional)] on_start_stream: Option<Box<dyn Fn() + Send + Sync + 'static>>,
#[prop(optional)] on_stop_stream: Option<Box<dyn Fn() + Send + Sync + 'static>>,
) -> impl IntoView {
let (is_streaming, set_is_streaming) = create_signal::<bool>(false);
let (is_streaming, set_is_streaming) = signal::<bool>(false);
let has_start = on_start_stream.is_some();
let has_stop = on_stop_stream.is_some();
@@ -15,7 +15,7 @@ pub fn ScreenSubPanel(
<div class="screen-sub-panel card">
<div class="card-header">
<div class="card-title flex items-center gap-2">
<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="2" y="3" width="20" height="14" rx="2" ry="2"></rect>
<line x1="8" y1="21" x2="16" y2="21"></line>
<line x1="12" y1="17" x2="12" y2="21"></line>
@@ -35,7 +35,7 @@ pub fn ScreenSubPanel(
class=move || format!("btn btn-success flex-1 {}", if is_streaming.get() { "opacity-50" } else { "" })
disabled=move || is_streaming.get()
on:click=move |_| {
if !is_streaming.get() {
if !is_streaming.get_untracked() {
set_is_streaming.set(true);
if let Some(ref cb) = on_start_stream {
cb();
@@ -54,7 +54,7 @@ pub fn ScreenSubPanel(
class=move || format!("btn btn-destructive flex-1 {}", if !is_streaming.get() { "opacity-50" } else { "" })
disabled=move || !is_streaming.get()
on:click=move |_| {
if is_streaming.get() {
if is_streaming.get_untracked() {
set_is_streaming.set(false);
if let Some(ref cb) = on_stop_stream {
cb();
@@ -81,4 +81,3 @@ pub fn ScreenSubPanel(
</div>
}
}
@@ -1,6 +1,6 @@
use leptos::prelude::*;
use wasm_bindgen::prelude::*;
use crate::features::live::hooks::use_voice_control::{use_voice_control, VoiceControlState};
use leptos::prelude::*;
use wasm_bindgen::JsCast;
/// VoiceConnectionCard component for Leptos
/// Renders guild and voice channel selectors with connect/disconnect controls
@@ -13,12 +13,12 @@ pub fn VoiceConnectionCard(
let state = voice_state.unwrap_or(default_state);
// Reactive signal for selected guild
let (selected_guild, set_selected_guild) = create_signal::<String>(String::new());
let (selected_guild, set_selected_guild) = signal::<String>(String::new());
// Reactive signal for selected channel
let (selected_channel, set_selected_channel) = create_signal::<String>(String::new());
let (selected_channel, set_selected_channel) = signal::<String>(String::new());
// When guild is selected, load voice channels
create_effect(move |_| {
Effect::new(move |_| {
let guild_id = selected_guild.get();
if !guild_id.is_empty() {
(state.load_voice_channels)(guild_id);
@@ -26,7 +26,7 @@ pub fn VoiceConnectionCard(
});
// Load guilds on mount
create_effect(move |_| {
Effect::new(move |_| {
(state.load_guilds)();
});
@@ -65,17 +65,13 @@ pub fn VoiceConnectionCard(
let error = state.error;
let voice_status = state.voice_status;
let is_connected = move || {
voice_status.get().map(|s| s.connected).unwrap_or(false)
};
let is_connected = move || voice_status.get().map(|s| s.connected).unwrap_or(false);
let can_join = move || {
!selected_guild.get().is_empty() && !selected_channel.get().is_empty() && !loading.get()
};
let can_disconnect = move || {
is_connected() && !loading.get()
};
let can_disconnect = move || is_connected() && !loading.get();
view! {
<div class=format!("rounded-xl border border-border bg-card shadow-sm {}", class)>
@@ -213,7 +209,8 @@ pub fn VoiceConnectionCard(
</span>
}.into_any()
} else {
view! { <></> }.into_any()
let _: () = view! { <></> };
().into_any()
}
}}
</div>
@@ -1,5 +1,5 @@
use leptos::prelude::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
/// WaveformPlayer — Audio player with waveform progress bar
#[component]
@@ -7,10 +7,10 @@ pub fn WaveformPlayer(
audio_url: String,
#[prop(default = "Recording".to_string())] title: String,
) -> impl IntoView {
let is_playing = create_rw_signal::<bool>(false);
let current_time = create_rw_signal::<f64>(0.0);
let duration = create_rw_signal::<f64>(0.0);
let audio_id = format!("audio_{}", &audio_url);
let is_playing = RwSignal::new(false);
let current_time = RwSignal::new(0.0);
let duration = RwSignal::new(0.0);
let audio_id = format!("audio_{}", audio_url);
// Clone audio_url for the audio element
let audio_src = audio_url.clone();
@@ -18,17 +18,17 @@ pub fn WaveformPlayer(
let toggle_play = move |_| {
let doc = web_sys::window().unwrap().document().unwrap();
let audio_opt = doc.get_element_by_id(&format!("audio_{}", &audio_src_for_id));
let audio_opt = doc.get_element_by_id(&format!("audio_{}", audio_src_for_id));
if let Some(audio_el) = audio_opt {
if let Ok(audio) = audio_el.dyn_into::<web_sys::HtmlAudioElement>() {
if is_playing.get() {
if is_playing.get_untracked() {
let _ = audio.pause();
is_playing.set(false);
} else {
if audio.ended() {
audio.set_current_time(0.0);
}
if let Ok(_) = audio.play() {
if audio.play().is_ok() {
is_playing.set(true);
}
}
@@ -87,11 +87,17 @@ pub fn WaveformPlayer(
}
fn progress_pct(current: f64, dur: f64) -> f64 {
if dur > 0.0 { (current / dur * 100.0).min(100.0) } else { 0.0 }
if dur > 0.0 {
(current / dur * 100.0).min(100.0)
} else {
0.0
}
}
fn format_time(secs: f64) -> String {
if !secs.is_finite() || secs < 0.0 { return "00:00".to_string(); }
if !secs.is_finite() || secs < 0.0 {
return "00:00".to_string();
}
let total = secs as u32;
format!("{:02}:{:02}", total / 60, total % 60)
}
@@ -1,4 +1,4 @@
pub mod use_voice_control;
pub mod use_media_control;
pub mod use_audio_playback;
pub mod use_audio_transmit;
pub mod use_media_control;
pub mod use_voice_control;
@@ -1,7 +1,6 @@
use leptos::prelude::*;
use std::sync::Arc;
use crate::features::live::audio::pcm_decoder::decode_pcm_frame;
use crate::features::live::audio::ring_buffer::SharedRingBuffer;
use leptos::prelude::*;
/// AudioPlaybackState — Manages PCM audio playback from WebSocket binary frames
pub struct AudioPlaybackState {
@@ -16,8 +15,8 @@ pub struct AudioPlaybackState {
/// Create and initialize audio playback state
pub fn use_audio_playback() -> AudioPlaybackState {
let buffer = SharedRingBuffer::new(44100 * 5); // 5 seconds at 44.1kHz
let active = create_rw_signal::<bool>(false);
let volume = create_rw_signal::<f64>(0.5);
let active = RwSignal::new(false);
let volume = RwSignal::new(0.5);
AudioPlaybackState {
buffer,
@@ -36,7 +35,7 @@ pub fn process_pcm_data(state: &AudioPlaybackState, data: Vec<u8>) {
/// Start consuming the ring buffer and playing through AudioContext
pub fn start_playback(state: &AudioPlaybackState) {
if state.active.get() {
if state.active.get_untracked() {
return;
}
state.active.set(true);
@@ -56,7 +55,7 @@ pub fn start_playback(state: &AudioPlaybackState) {
let ctx_ref = &ctx;
let _ = ctx_ref.resume();
while active.get() {
while active.get_untracked() {
let available = buffer.available_samples();
if available >= 4410 {
// ~100ms worth at 44.1kHz
@@ -90,7 +89,7 @@ fn play_samples(ctx: &web_sys::AudioContext, samples: &[f32]) {
return;
};
let len = samples.len().min(channel_data.len() as usize);
let len = samples.len().min(channel_data.len());
if len == 0 {
return;
}
@@ -1,5 +1,5 @@
use leptos::prelude::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::spawn_local;
use web_sys::{MediaStream, MediaStreamConstraints, MediaStreamTrack};
@@ -11,14 +11,14 @@ pub struct AudioTransmitState {
/// Create microphone transmit state
pub fn use_audio_transmit() -> AudioTransmitState {
let active = create_rw_signal::<bool>(false);
let active = RwSignal::new(false);
let stream = StoredValue::new(None::<MediaStream>);
AudioTransmitState { active, stream }
}
/// Start microphone capture - requests getUserMedia and stores the stream
pub fn start_transmit(state: &AudioTransmitState) {
if state.active.get() {
if state.active.get_untracked() {
return;
}
state.active.set(true);

Some files were not shown because too many files have changed in this diff Show More