From bf6ec728c49b599aae399555569486dcdfcecbee Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Tue, 2 Jun 2026 22:20:46 +0700 Subject: [PATCH] `chore(discord-gateway): remove stale eslint-disable comment for sticker cache stats` --- services/discord-gateway/src/app/bootstrap.ts | 2 +- .../src/modules/ai-moderation/aiAnalyzer.ts | 26 +++++++++++-------- .../ai-moderation/llmModerationClient.ts | 19 +++++++++++--- .../src/modules/ai-moderation/stickerCache.ts | 16 +++++------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/services/discord-gateway/src/app/bootstrap.ts b/services/discord-gateway/src/app/bootstrap.ts index 0b3e6d0..a0570bd 100644 --- a/services/discord-gateway/src/app/bootstrap.ts +++ b/services/discord-gateway/src/app/bootstrap.ts @@ -1,3 +1,4 @@ +import { createChildLogger } from "@bete/shared/logger"; import { Client } from "discord.js-selfbot-v13"; import { startPendingAIAnalysisWorker } from "../modules/ai-moderation/aiAnalyzer.js"; import { CommandHandler } from "../modules/command-handler/commandHandler.js"; @@ -17,7 +18,6 @@ import { } from "../shared/database/drizzle.js"; import { runMigrations } from "../shared/database/migrate.js"; import { createDiscordClientOptions } from "../shared/discord/clientOptions.js"; -import { createChildLogger } from "@bete/shared/logger"; import { createGracefulShutdown } from "./shutdown.js"; const logger = createChildLogger("discord-gateway"); diff --git a/services/discord-gateway/src/modules/ai-moderation/aiAnalyzer.ts b/services/discord-gateway/src/modules/ai-moderation/aiAnalyzer.ts index 6595faf..0bce9dd 100644 --- a/services/discord-gateway/src/modules/ai-moderation/aiAnalyzer.ts +++ b/services/discord-gateway/src/modules/ai-moderation/aiAnalyzer.ts @@ -1,11 +1,11 @@ import { existsSync } from "node:fs"; import { fileURLToPath } from "node:url"; +import { createChildLogger } from "@bete/shared/logger"; +import { retryWithBackoff } from "@bete/shared/utils"; import type { Client } from "discord.js-selfbot-v13"; import { AbortError } from "p-retry"; import { Piscina } from "piscina"; import { config } from "../../shared/config/config.js"; -import { createChildLogger } from "@bete/shared/logger"; -import { retryWithBackoff } from "@bete/shared/utils"; import type { EventBroadcaster } from "../event-broadcaster/index.js"; import { invalidateAnalyticsCache } from "../message-capture/analyticsStore.js"; import { isAgeRestrictedMetadata } from "../message-capture/messageMetadata.js"; @@ -28,9 +28,7 @@ import type { import { attemptAutoDeleteFlaggedMessage } from "./autoDeleteManager.js"; import { buildConversationContext } from "./conversationContext.js"; import { runModerationAnalysis } from "./llmModerationClient.js"; -import { - logModerationError, -} from "./responseLogger.js"; +import { logModerationError } from "./responseLogger.js"; const logger = createChildLogger("ai-analyzer"); @@ -163,7 +161,8 @@ const MAX_CONSECUTIVE_ERRORS = 5; const CONVERSATION_CB_COOLDOWN_MS = 60000; function recordConversationBatchFailure(conversationKey: string): void { - const nextCount = (conversationConsecutiveErrors.get(conversationKey) ?? 0) + 1; + const nextCount = + (conversationConsecutiveErrors.get(conversationKey) ?? 0) + 1; conversationConsecutiveErrors.set(conversationKey, nextCount); if (nextCount >= MAX_CONSECUTIVE_ERRORS) { @@ -471,11 +470,16 @@ async function processIndividualFallback( lastError = error instanceof Error ? error.message : String(error); // Log error with responseLogger - logModerationError([messageId], config.AI_LLM_MODEL, error as Error | string, { - phase: "individual_fallback", - conversationKey, - exhaustedOnIncomplete, - }); + logModerationError( + [messageId], + config.AI_LLM_MODEL, + error as Error | string, + { + phase: "individual_fallback", + conversationKey, + exhaustedOnIncomplete, + }, + ); // Infinite-loop prevention: if all retries were exhausted because the LLM // consistently dropped this specific message (not a transient error), diff --git a/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts b/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts index 8682c80..e4f9d0a 100644 --- a/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts +++ b/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts @@ -1,9 +1,9 @@ +import { createChildLogger } from "@bete/shared/logger"; +import { retryWithBackoff } from "@bete/shared/utils"; import type { ChatCompletion } from "openai/resources/chat/completions"; import { AbortError } from "p-retry"; import { z } from "zod"; import { config } from "../../shared/config/config.js"; -import { createChildLogger } from "@bete/shared/logger"; -import { retryWithBackoff } from "@bete/shared/utils"; import { resizeImageForVision } from "../attachment-upload/imageResizer.js"; import { extractMessageMediaEvidence } from "../message-capture/messageMetadata.js"; import type { @@ -532,7 +532,10 @@ const analyzeSingleMediaImage = async ( // processing this exact image, wait for it instead of starting a duplicate. const existing = inFlightVisionCalls.get(cacheKey); if (existing) { - log.debug({ cacheKey }, "Media analysis in-flight dedupe — waiting for existing call"); + log.debug( + { cacheKey }, + "Media analysis in-flight dedupe — waiting for existing call", + ); const result = await existing; if (!result) return null; return `[Media analysis for message ${messageId}] ${image.sourceLabel}: ${result}`; @@ -910,7 +913,15 @@ async function runTextOnlyBatch( }), ])) as { results: AnalysisResult[]; raw: unknown }; - const rawUsage = (batchResult.raw as { usage?: { prompt_tokens: number; completion_tokens: number; total_tokens: number } })?.usage; + const rawUsage = ( + batchResult.raw as { + usage?: { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + }; + } + )?.usage; allResults.push(...batchResult.results); if (batchResult.raw) lastRaw = batchResult.raw; diff --git a/services/discord-gateway/src/modules/ai-moderation/stickerCache.ts b/services/discord-gateway/src/modules/ai-moderation/stickerCache.ts index 90f222e..eb151b3 100644 --- a/services/discord-gateway/src/modules/ai-moderation/stickerCache.ts +++ b/services/discord-gateway/src/modules/ai-moderation/stickerCache.ts @@ -1,5 +1,5 @@ -import { executeAll, executeGet } from "../../shared/database/drizzle.js"; import { createChildLogger } from "@bete/shared/logger"; +import { executeAll, executeGet } from "../../shared/database/drizzle.js"; const logger = createChildLogger("sticker-cache"); @@ -42,10 +42,9 @@ export async function initStickerCache(): Promise { `CREATE INDEX IF NOT EXISTS "idx_sticker_cache_fetched_at" ON "sticker_cache" USING btree ("fetched_at")`, ); - await executeAll( - "DELETE FROM sticker_cache WHERE fetched_at < $1", - [Date.now() - TTL_MS], - ); + await executeAll("DELETE FROM sticker_cache WHERE fetched_at < $1", [ + Date.now() - TTL_MS, + ]); const row = await executeGet( "SELECT count(*) as cnt, COALESCE(SUM(size), 0) as total FROM sticker_cache", [], @@ -149,10 +148,9 @@ async function evictIfNeeded(newSize: number): Promise { if (freed >= targetToFree) break; } - await executeAll( - `DELETE FROM sticker_cache WHERE name = ANY($1)`, - [namesToDelete], - ); + await executeAll(`DELETE FROM sticker_cache WHERE name = ANY($1)`, [ + namesToDelete, + ]); statsCache.totalSizeBytes -= freed; statsCache.entryCount -= namesToDelete.length;