Files
GMW/src/moderation/aiAnalyzer.ts
T

349 lines
9.7 KiB
TypeScript
Raw Normal View History

import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { Piscina } from "piscina";
import { config } from "../config.js";
import { createChildLogger } from "../logger.js";
import {
estimateTokens,
formatMessageForPrompt,
} from "./conversationContext.js";
import {
2026-05-14 19:39:25 +07:00
getMessageById,
2026-05-14 19:32:44 +07:00
getPendingConversationKeys,
getPendingMessagesByConversation,
} from "./messageStore.js";
import type {
AnalysisQueueStatus,
MessageRecord,
ModerationBroadcaster,
} from "./types.js";
const logger = createChildLogger("ai-analyzer");
2026-05-14 19:32:44 +07:00
type ModerationGlobal = typeof globalThis & {
moderationBroadcaster?: ModerationBroadcaster;
};
function getModerationBroadcaster(): ModerationBroadcaster | undefined {
return (globalThis as ModerationGlobal).moderationBroadcaster;
}
2026-05-14 19:32:44 +07:00
// Debounce state per conversation key
const conversationDebounceTimers = new Map<string, NodeJS.Timeout>();
2026-05-14 19:39:25 +07:00
// Track conversations currently being processed
const conversationProcessing = new Map<string, number>();
2026-05-14 19:39:25 +07:00
// Track conversations in error cooldown (failed recently)
const conversationErrorCooldown = new Map<string, number>();
2026-05-14 19:32:44 +07:00
const AI_PROCESSING_OVERLAP_MS = 30000;
let activeRequests = 0;
2026-05-14 19:32:44 +07:00
let lastError: string | null = null;
// Global circuit breaker state
let consecutiveErrors = 0;
const MAX_CONSECUTIVE_ERRORS = 5;
let globalCooldownUntil = 0;
function getAnalysisWorkerUrl(): URL {
const candidates = [
new URL("./aiAnalysisWorker.js", import.meta.url),
new URL("../aiAnalysisWorker.js", import.meta.url),
new URL("./aiAnalysisWorker.ts", import.meta.url),
];
for (const candidate of candidates) {
if (existsSync(fileURLToPath(candidate))) {
return candidate;
}
}
return candidates[2];
}
const workerPool = new Piscina({
filename: fileURLToPath(getAnalysisWorkerUrl()),
execArgv: process.execArgv,
});
interface AnalysisWorkerResponse {
ok: boolean;
conversationKey: string;
rows: MessageRecord[];
error?: string;
}
2026-05-14 19:32:44 +07:00
/**
* Gets the conversation key for a message (thread_id or channel_id)
*/
export function getConversationKey(message: MessageRecord): string {
return message.thread_id || message.channel_id;
}
2026-05-14 19:32:44 +07:00
/**
* Picks a batch of messages within token budget
*/
export function pickBatchWithinBudget(
messages: MessageRecord[],
2026-05-14 19:32:44 +07:00
maxTokens: number,
tokensPerMessage: number,
): MessageRecord[] {
const batch: MessageRecord[] = [];
let usedTokens = 0;
2026-05-14 19:32:44 +07:00
for (const msg of messages) {
const formatted = formatMessageForPrompt(msg, "target");
const msgTokens = estimateTokens(formatted) + tokensPerMessage;
2026-05-14 19:32:44 +07:00
if (usedTokens + msgTokens <= maxTokens) {
batch.push(msg);
usedTokens += msgTokens;
}
}
2026-05-14 19:32:44 +07:00
return batch;
}
function isConversationProcessingLocked(conversationKey: string): boolean {
const startedAt = conversationProcessing.get(conversationKey);
return Boolean(
startedAt && Date.now() - startedAt < AI_PROCESSING_OVERLAP_MS,
);
}
2026-05-14 19:32:44 +07:00
/**
* Processes a batch of messages for a conversation
*/
async function processBatch(
conversationKey: string,
messages: MessageRecord[],
): Promise<void> {
if (messages.length === 0) return;
if (Date.now() < globalCooldownUntil) {
// Should not normally hit here due to checks in scheduleConversationAnalysis, but just in case
return;
}
activeRequests++;
let shouldScheduleNext = false;
const processingStartedAt = Date.now();
conversationProcessing.set(conversationKey, processingStartedAt);
try {
const result = (await workerPool.run({
conversationKey,
messages,
})) as AnalysisWorkerResponse;
for (const row of result.rows) {
getModerationBroadcaster()?.messageAnalyzed(row);
2026-05-14 19:32:44 +07:00
}
if (!result.ok) {
consecutiveErrors++;
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
globalCooldownUntil = Date.now() + 60000;
logger.warn(
"Global circuit breaker triggered due to consecutive errors",
);
}
lastError = result.error ?? "Analysis worker failed";
conversationErrorCooldown.set(
conversationKey,
Date.now() + config.AI_ANALYSIS_ERROR_COOLDOWN_MS,
);
logger.error(
{
conversationKey,
error: lastError,
messageCount: messages.length,
messageIds: messages.map((m) => m.id),
cooldownUntil: new Date(
Date.now() + config.AI_ANALYSIS_ERROR_COOLDOWN_MS,
).toISOString(),
timestamp: new Date().toISOString(),
},
"Batch analysis failed, will retry after cooldown",
);
return;
}
consecutiveErrors = 0; // Reset circuit breaker
2026-05-14 19:39:25 +07:00
conversationErrorCooldown.delete(conversationKey);
shouldScheduleNext = true;
2026-05-14 19:32:44 +07:00
} catch (error) {
consecutiveErrors++;
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
globalCooldownUntil = Date.now() + 60000;
logger.warn("Global circuit breaker triggered due to consecutive errors");
}
2026-05-14 19:32:44 +07:00
lastError = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : undefined;
conversationErrorCooldown.set(
conversationKey,
Date.now() + config.AI_ANALYSIS_ERROR_COOLDOWN_MS,
);
2026-05-14 19:32:44 +07:00
logger.error(
{
conversationKey,
error: lastError,
stack: errorStack,
messageCount: messages.length,
messageIds: messages.map((m) => m.id),
cooldownUntil: new Date(
Date.now() + config.AI_ANALYSIS_ERROR_COOLDOWN_MS,
).toISOString(),
timestamp: new Date().toISOString(),
},
"Analysis worker failed, will retry after cooldown",
2026-05-14 19:32:44 +07:00
);
} finally {
activeRequests--;
if (conversationProcessing.get(conversationKey) === processingStartedAt) {
conversationProcessing.delete(conversationKey);
}
if (shouldScheduleNext) {
setImmediate(() => scheduleConversationAnalysis(conversationKey));
}
}
}
2026-05-14 19:32:44 +07:00
/**
* Debounced analysis trigger for a conversation
*/
function scheduleConversationAnalysis(conversationKey: string): void {
2026-05-14 19:39:25 +07:00
// Skip if already processing
if (isConversationProcessingLocked(conversationKey)) {
2026-05-14 19:39:25 +07:00
return;
}
// Check cooldowns
const convoCooldown = conversationErrorCooldown.get(conversationKey) || 0;
const activeCooldown = Math.max(convoCooldown, globalCooldownUntil);
if (activeCooldown && Date.now() < activeCooldown) {
// Instead of dropping, re-schedule for after cooldown if not already scheduled
if (!conversationDebounceTimers.has(conversationKey)) {
const remaining = activeCooldown - Date.now();
const timer = setTimeout(() => {
conversationDebounceTimers.delete(conversationKey);
scheduleConversationAnalysis(conversationKey);
}, remaining + 500); // 500ms buffer after cooldown
conversationDebounceTimers.set(conversationKey, timer);
}
2026-05-14 19:39:25 +07:00
return;
}
2026-05-14 19:32:44 +07:00
// Clear existing timer
const existingTimer = conversationDebounceTimers.get(conversationKey);
if (existingTimer) {
clearTimeout(existingTimer);
}
2026-05-14 19:32:44 +07:00
// Always use shorter debounce for immediate processing (no concurrency limit)
const debounceTime = config.AI_ANALYSIS_DEBOUNCE_MS;
2026-05-14 19:32:44 +07:00
// Set new debounced timer
const timer = setTimeout(async () => {
conversationDebounceTimers.delete(conversationKey);
// Get pending messages for this conversation
const messages = await getPendingMessagesByConversation(
conversationKey,
config.AI_ANALYSIS_MAX_BATCH_SIZE,
2026-05-14 19:32:44 +07:00
);
if (messages.length > 0) {
await processBatch(conversationKey, messages);
}
}, debounceTime);
2026-05-14 19:32:44 +07:00
conversationDebounceTimers.set(conversationKey, timer);
}
2026-05-14 19:32:44 +07:00
/**
* Queues a message for analysis (debounced by conversation)
*/
2026-05-14 19:39:25 +07:00
export async function queueMessageAnalysis(messageId: string): Promise<void> {
if (!config.AI_ANALYSIS_ENABLED) return;
2026-05-14 19:32:44 +07:00
2026-05-14 19:39:25 +07:00
try {
// Look up the message to get its conversation key
const message = await getMessageById(messageId);
if (!message) {
logger.warn({ messageId }, "Message not found for analysis queue");
return;
}
// Schedule its conversation for analysis
const conversationKey = getConversationKey(message);
queueConversationAnalysis(conversationKey);
} catch (error) {
logger.error(
{
messageId,
error: error instanceof Error ? error.message : String(error),
},
"Failed to queue message for analysis",
);
}
}
2026-05-14 19:32:44 +07:00
/**
* Queues a conversation for analysis (debounced)
*/
export function queueConversationAnalysis(conversationKey: string): void {
if (!config.AI_ANALYSIS_ENABLED) return;
// Schedule debounced analysis
scheduleConversationAnalysis(conversationKey);
}
/**
* Gets current analysis queue status
*/
export function getAnalysisQueueStatus(): AnalysisQueueStatus {
return {
queuedConversations: conversationDebounceTimers.size,
activeRequests,
lastError,
};
}
/**
* Starts the pending AI analysis recovery worker
*/
export function startPendingAIAnalysisWorker(): void {
if (!config.AI_ANALYSIS_ENABLED) return;
2026-05-14 19:32:44 +07:00
setInterval(async () => {
2026-05-14 19:32:44 +07:00
try {
// Get pending conversation keys
const conversationKeys = await getPendingConversationKeys(100);
for (const key of conversationKeys) {
2026-05-14 19:39:25 +07:00
// Skip if already scheduled
if (conversationDebounceTimers.has(key)) {
continue;
2026-05-14 19:32:44 +07:00
}
2026-05-14 19:39:25 +07:00
// Skip if currently processing
if (isConversationProcessingLocked(key)) {
2026-05-14 19:39:25 +07:00
continue;
}
// Skip if in error cooldown
const cooldownUntil = conversationErrorCooldown.get(key);
if (cooldownUntil && Date.now() < cooldownUntil) {
continue;
}
scheduleConversationAnalysis(key);
2026-05-14 19:32:44 +07:00
}
} catch (error) {
logger.error({ error }, "Pending AI analysis recovery worker failed");
}
}, config.AI_ANALYSIS_RECOVERY_INTERVAL_MS);
}