feat(content-engine): add autonomous validation layer to actively detect and correct hallucinated meme templates without user intervention
All checks were successful
Monorepo Pipeline / ⚡ Prioritize Release (push) Successful in 1s
Monorepo Pipeline / 🧪 Test (push) Successful in 1m0s
Monorepo Pipeline / 🧹 Lint (push) Successful in 2m46s
Monorepo Pipeline / 🏗️ Build (push) Successful in 4m49s
Monorepo Pipeline / 🚀 Release (push) Has been skipped
Monorepo Pipeline / 🐳 Build Directus (Base) (push) Has been skipped
Monorepo Pipeline / 🐳 Build Gatekeeper (Product) (push) Has been skipped
Monorepo Pipeline / 🐳 Build Build-Base (push) Has been skipped
Monorepo Pipeline / 🐳 Build Production Runtime (push) Has been skipped

This commit is contained in:
2026-02-22 18:23:44 +01:00
parent 90a9e34c7e
commit d5632b009a

View File

@@ -405,24 +405,26 @@ Return ONLY the JSON.`,
.join("\n\n"); .join("\n\n");
const memeTemplates = [ const memeTemplates = [
"distracted", "db", // Distracted Boyfriend
"gb", "gb", // Galaxy Brain
"fine", "fine", // This is Fine
"ds", "ds", // Daily Struggle
"gru", "gru", // Gru's Plan
"cmm", "cmm", // Change My Mind
"ahb", "astronaut", // Always Has Been (ahb)
"uno",
"disastergirl", "disastergirl",
"pigeon", "pigeon", // Is this a pigeon?
"rollsafe", "rollsafe",
"pikachu", "slap", // Will Smith
"slap", "exit", // Left Exit 12
"exit",
"mordor", "mordor",
"panik-kalm-panik", "panik-kalm-panik",
"womanyellingcat", "woman-cat", // Woman yelling at cat
"grumpycat", "grumpycat",
"sadfrog",
"stonks",
"same", // They're the same picture
"spongebob",
]; ];
const forcedMeme = const forcedMeme =
memeTemplates[Math.floor(Math.random() * memeTemplates.length)]; memeTemplates[Math.floor(Math.random() * memeTemplates.length)];
@@ -499,37 +501,53 @@ CRITICAL GUIDELINES (NEVER BREAK THESE):
let rawContent = response.choices[0].message.content || task.content; let rawContent = response.choices[0].message.content || task.content;
rawContent = this.cleanResponse(rawContent, socialPosts); rawContent = this.cleanResponse(rawContent, socialPosts);
// Validation Layer: Check Mermaid syntax // --- Autonomous Validation Layer ---
if (retryCount < 2 && rawContent.includes("<Mermaid>")) { let hasError = false;
let errorFeedback = "";
// 1. Validate Meme Templates
const memeRegex = /<ArticleMeme[^>]+template=["']([^"']+)["'][^>]*>/g;
let memeMatch;
const invalidMemes: string[] = [];
while ((memeMatch = memeRegex.exec(rawContent)) !== null) {
if (!memeTemplates.includes(memeMatch[1])) {
invalidMemes.push(memeMatch[1]);
}
}
if (invalidMemes.length > 0) {
hasError = true;
errorFeedback += `\n- You hallucinated invalid meme templates: ${invalidMemes.join(", ")}. You MUST ONLY use templates from this exact list: ${memeTemplates.join(", ")}. DO NOT INVENT TEMPLATES.\n`;
}
// 2. Validate Mermaid Syntax
if (rawContent.includes("<Mermaid>")) {
console.log("🔍 Validating Mermaid syntax in AI response..."); console.log("🔍 Validating Mermaid syntax in AI response...");
const mermaidBlocks = this.extractMermaidBlocks(rawContent); const mermaidBlocks = this.extractMermaidBlocks(rawContent);
let hasError = false;
let errorFeedback = "";
for (const block of mermaidBlocks) { for (const block of mermaidBlocks) {
const validationResult = await this.validateMermaidSyntax(block); const validationResult = await this.validateMermaidSyntax(block);
if (!validationResult.valid) { if (!validationResult.valid) {
hasError = true; hasError = true;
errorFeedback += `\nInvalid Mermaid block:\n${block}\nError context: ${validationResult.error}\n\n`; errorFeedback += `\n- Invalid Mermaid block:\n${block}\nError context: ${validationResult.error}\n`;
} }
} }
}
if (hasError) { if (hasError && retryCount < 3) {
console.log( console.log(
`Invalid Mermaid syntax detected. Retrying compilation (Attempt ${retryCount + 1}/2)...`, `Validation errors detected. Retrying compilation (Attempt ${retryCount + 1}/3)...`,
); );
return this.compileArticle( return this.compileArticle(
{ {
...task, ...task,
content: `The previous attempt failed because you generated invalid Mermaid.js syntax. Please rewrite the MDX and FIX the following Mermaid errors. \n\nErrors:\n${errorFeedback}\n\nOriginal Draft:\n${task.content}`, content: `CRITICAL ERROR IN PREVIOUS ATTEMPT:\nYour generated MDX contained the following errors that MUST be fixed:\n${errorFeedback}\n\nPlease rewrite the MDX and FIX these errors. Pay strict attention to the rules.\n\nOriginal Draft:\n${task.content}`,
}, },
facts, facts,
competitorInsights, competitorInsights,
socialPosts, socialPosts,
internalLinks, internalLinks,
retryCount + 1, retryCount + 1,
); );
}
} }
return rawContent; return rawContent;