feat(moderation): add support for vision model in moderation analysis
This commit is contained in:
@@ -486,34 +486,15 @@ function enqueueIndividualFallbacks(messages: MessageRecord[]): void {
|
||||
const newMessages = messages.filter((m) => !individualInFlight.has(m.id));
|
||||
if (newMessages.length === 0) return;
|
||||
|
||||
// FIX #1: Enforce concurrency cap.
|
||||
const availableSlots =
|
||||
config.AI_ANALYSIS_INDIVIDUAL_MAX_CONCURRENT - individualInFlight.size;
|
||||
if (availableSlots <= 0) {
|
||||
logger.warn(
|
||||
{
|
||||
cap: config.AI_ANALYSIS_INDIVIDUAL_MAX_CONCURRENT,
|
||||
inFlight: individualInFlight.size,
|
||||
skipped: newMessages.length,
|
||||
},
|
||||
"Individual fallback concurrency cap reached — messages will be recovered by recovery worker",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const toProcess = newMessages.slice(0, availableSlots);
|
||||
const skipped = newMessages.length - toProcess.length;
|
||||
|
||||
logger.info(
|
||||
{
|
||||
count: toProcess.length,
|
||||
skipped,
|
||||
messageIds: toProcess.map((m) => m.id),
|
||||
count: newMessages.length,
|
||||
messageIds: newMessages.map((m) => m.id),
|
||||
},
|
||||
"Enqueueing individual fallback analysis for batch-incomplete messages",
|
||||
);
|
||||
|
||||
for (const msg of toProcess) {
|
||||
for (const msg of newMessages) {
|
||||
individualInFlight.add(msg.id);
|
||||
// Fire-and-forget: processIndividualFallback handles all errors internally.
|
||||
processIndividualFallback(msg).catch((err) => {
|
||||
@@ -855,8 +836,8 @@ export function startPendingAIAnalysisWorker(client?: Client): void {
|
||||
setInterval(() => {
|
||||
// FIX #3 pattern: no async arrow — chain promises explicitly.
|
||||
Promise.all([
|
||||
getPendingConversationKeys(100),
|
||||
getConversationKeysWithIncompleteAnalysis(50),
|
||||
getPendingConversationKeys(500),
|
||||
getConversationKeysWithIncompleteAnalysis(200),
|
||||
])
|
||||
.then(([pendingKeys, incompleteKeys]) => {
|
||||
const now = Date.now();
|
||||
@@ -902,10 +883,7 @@ export function startPendingAIAnalysisWorker(client?: Client): void {
|
||||
if (isConversationProcessingLocked(key)) continue;
|
||||
|
||||
promises.push(
|
||||
getIncompleteMessagesByConversation(
|
||||
key,
|
||||
config.AI_ANALYSIS_INDIVIDUAL_MAX_CONCURRENT,
|
||||
)
|
||||
getIncompleteMessagesByConversation(key, 500)
|
||||
.then(async (msgs) => {
|
||||
const processableMessages =
|
||||
await skipAgeRestrictedMessages(msgs);
|
||||
|
||||
@@ -769,7 +769,7 @@ export async function runModerationAnalysis(
|
||||
): Promise<string | null> => {
|
||||
try {
|
||||
const completion = await openai.chat.completions.create({
|
||||
model: config.AI_LLM_MODEL,
|
||||
model: config.AI_LLM_VISION_MODEL ?? config.AI_LLM_MODEL,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
|
||||
@@ -630,7 +630,7 @@ export async function getConversationContextBefore(input: {
|
||||
|
||||
export async function getPendingMessagesByConversation(
|
||||
conversationKey: string,
|
||||
limit: number = 25,
|
||||
limit: number = 200,
|
||||
): Promise<MessageRecord[]> {
|
||||
try {
|
||||
const database = db();
|
||||
@@ -667,7 +667,7 @@ export async function getPendingMessagesByConversation(
|
||||
}
|
||||
|
||||
export async function getPendingConversationKeys(
|
||||
limit: number = 100,
|
||||
limit: number = 500,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const database = db();
|
||||
@@ -782,7 +782,7 @@ export async function searchMessages(input: {
|
||||
* the individual-fallback queue.
|
||||
*/
|
||||
export async function getConversationKeysWithIncompleteAnalysis(
|
||||
limit: number = 50,
|
||||
limit: number = 200,
|
||||
): Promise<string[]> {
|
||||
try {
|
||||
const database = db();
|
||||
@@ -826,7 +826,7 @@ export async function getConversationKeysWithIncompleteAnalysis(
|
||||
*/
|
||||
export async function getIncompleteMessagesByConversation(
|
||||
conversationKey: string,
|
||||
limit: number = 20,
|
||||
limit: number = 500,
|
||||
): Promise<MessageRecord[]> {
|
||||
try {
|
||||
const database = db();
|
||||
|
||||
Reference in New Issue
Block a user