refactor(moderation): improve LLM moderation client - 10 recommendations

- Add XML delimiters to prevent prompt injection (R1)
- Use JSON Schema response format instead of json_object (R2)
- Add concurrency limiter via p-limit (R3)
- Add timeout per media analysis call (R4)
- Resize images with sharp before vision API (R5)
- Split text batches when exceeding batch size limit (R6)
- Add few-shot examples to system prompt (R7)
- Modularize system prompt builder (R8)
- Enhance deferral detection regex with exception patterns (R9)
- Sanitize error messages to avoid leaking internals (R10)

New files: concurrencyLimiter.ts, imageResizer.ts, moderationPrompt.ts
Updated: llmModerationClient.ts, config.ts, package.json, tests

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-01 18:50:05 +07:00
co-authored by Claude Opus 4.8
parent 4117f83c1b
commit a643125c7b
12 changed files with 663 additions and 289 deletions
+1 -1
View File
@@ -11,4 +11,4 @@ runMigrations()
.catch((error) => {
logger.error({ error }, "Migration failed");
process.exit(1);
});
});