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
+2
View File
@@ -49,6 +49,7 @@
"motion": "^12.40.0",
"openai": "^6.38.0",
"opusscript": "^0.0.8",
"p-limit": "^7.3.0",
"p-retry": "^8.0.0",
"pg": "^8.21.0",
"piscina": "^5.1.4",
@@ -57,6 +58,7 @@
"prom-client": "^15.1.3",
"react": "^19.2.6",
"react-dom": "^19.2.6",
"sharp": "^0.34.5",
"tailwind-merge": "^3.6.0",
"vite": "^8.0.13",
"winston": "^3.19.0",