From 1b3f7d75328ab5aa466de7569761b35e4e3dccc0 Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Sat, 4 Oct 2025 12:19:55 +0000 Subject: [PATCH] Improve AI text generation parsing with flexible regex and normalization Update parsing logic in OpenRouter service to handle variations in AI-generated text formatting, including normalizing line endings and employing more robust regular expressions for version matching. Replit-Commit-Author: Agent Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/cIV2GBh --- server/services/openrouter.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/server/services/openrouter.ts b/server/services/openrouter.ts index 88bedab..fdb9467 100644 --- a/server/services/openrouter.ts +++ b/server/services/openrouter.ts @@ -90,9 +90,27 @@ VERSION 3 - ÉMOTIONNELLE: private parseGeneratedText(content: string): GeneratedText[] { const variants: GeneratedText[] = []; - const version1Match = content.match(/VERSION 1 - DYNAMIQUE:\s*\n([\s\S]*?)(?=VERSION 2|$)/i); - const version2Match = content.match(/VERSION 2 - INFORMATIVE:\s*\n([\s\S]*?)(?=VERSION 3|$)/i); - const version3Match = content.match(/VERSION 3 - ÉMOTIONNELLE:\s*\n([\s\S]*?)$/i); + // Normaliser les retours à la ligne (CRLF -> LF) + const normalizedContent = content.replace(/\r\n/g, '\n'); + + // Debug: Logger le contenu pour diagnostiquer les échecs de parsing + console.log('📝 AI Generated Content (first 500 chars):', normalizedContent.substring(0, 500)); + + // Regex ultra-flexibles acceptant: + // - markdown (** optionnel) + // - tirets variés (-, –, —) + // - espaces variables + // - deux-points optionnel + // - séparateurs (---, ***, etc.) + const version1Match = normalizedContent.match(/\*{0,2}\s*VERSION\s+1\s*[-–—]\s*DYNAMIQUE\s*\*{0,2}:?\s*[\r\n]+([\s\S]*?)(?=\s*[-*]{3,}\s*[\r\n]+|\*{0,2}\s*VERSION\s+2|$)/i); + const version2Match = normalizedContent.match(/\*{0,2}\s*VERSION\s+2\s*[-–—]\s*INFORMATIVE?\s*\*{0,2}:?\s*[\r\n]+([\s\S]*?)(?=\s*[-*]{3,}\s*[\r\n]+|\*{0,2}\s*VERSION\s+3|$)/i); + const version3Match = normalizedContent.match(/\*{0,2}\s*VERSION\s+3\s*[-–—]\s*[ÉE]MOTIONNELLE?\s*\*{0,2}:?\s*[\r\n]+([\s\S]*?)$/i); + + console.log('🔍 Regex matches:', { + version1: !!version1Match, + version2: !!version2Match, + version3: !!version3Match + }); if (version1Match) { const text = version1Match[1].trim();