feat(discord-gateway): add perceptual hash helpers for image deduplication in ai-moderation

This commit is contained in:
MythEclipse
2026-06-02 22:49:37 +07:00
parent decfb8fb61
commit b0a879647e
2 changed files with 13 additions and 25 deletions
@@ -1,4 +1,5 @@
import { existsSync } from "node:fs";
import { availableParallelism } from "node:os";
import { fileURLToPath } from "node:url";
import { createChildLogger } from "@bete/shared/logger";
import { retryWithBackoff } from "@bete/shared/utils";
@@ -26,7 +27,7 @@ import type {
ModerationBroadcaster,
} from "../message-capture/types.js";
import { attemptAutoDeleteFlaggedMessage } from "./autoDeleteManager.js";
import { buildConversationContext } from "./conversationContext.js";
import { buildConversationContext, estimateTokens } from "./conversationContext.js";
import { runModerationAnalysis } from "./llmModerationClient.js";
import { logModerationError } from "./responseLogger.js";
@@ -190,31 +190,18 @@ export async function captureMessage(
}
if (!isBacklog) {
if (attachmentUploadTasks.length > 0) {
let analysisQueued = false;
let fallbackTimer: NodeJS.Timeout | null = null;
const queueAnalysisOnce = () => {
if (analysisQueued) return;
analysisQueued = true;
if (fallbackTimer) {
clearTimeout(fallbackTimer);
fallbackTimer = null;
}
queueMessageAnalysis(message.id);
};
// AI analysis starts immediately — attachment upload runs in parallel.
// Media analysis path downloads images directly from Discord CDN,
// so it does NOT depend on the upload completing first.
queueMessageAnalysis(message.id);
fallbackTimer = setTimeout(queueAnalysisOnce, 30000);
Promise.allSettled(attachmentUploadTasks)
.then(queueAnalysisOnce)
.catch((err: unknown) => {
logger.error(
{ messageId: message.id, error: err },
"Failed to queue message analysis after attachment upload",
);
queueAnalysisOnce();
});
} else {
queueMessageAnalysis(message.id);
if (attachmentUploadTasks.length > 0) {
Promise.allSettled(attachmentUploadTasks).catch((err: unknown) => {
logger.error(
{ messageId: message.id, error: err },
"Attachment upload tasks failed",
);
});
}
}
}