fix: accepter les réponses IA avec contrôles OCR
This commit is contained in:
@@ -75,9 +75,54 @@ async function extractTextFromPdf(pdfBuffer: Buffer): Promise<string> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean JSON response from LLM by removing markdown code blocks
|
||||
* Supprime ou échappe les caractères de contrôle bruts uniquement à
|
||||
* l'intérieur des chaînes JSON. Certains moteurs retournent ponctuellement
|
||||
* un caractère NUL provenant du texte OCR : JSON.parse le refuse alors que
|
||||
* le reste de la réponse est exploitable.
|
||||
*/
|
||||
function cleanJsonResponse(content: string): string {
|
||||
function normalizeControlCharactersInJsonStrings(content: string): string {
|
||||
let normalized = "";
|
||||
let inString = false;
|
||||
let escaped = false;
|
||||
|
||||
for (const character of content) {
|
||||
if (escaped) {
|
||||
normalized += character;
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === "\\") {
|
||||
normalized += character;
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (character === '"') {
|
||||
inString = !inString;
|
||||
normalized += character;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inString && character.charCodeAt(0) < 0x20) {
|
||||
if (character === "\n") normalized += "\\n";
|
||||
else if (character === "\r") normalized += "\\r";
|
||||
else if (character === "\t") normalized += "\\t";
|
||||
// Les autres contrôles (notamment NUL) n'ont pas de valeur métier.
|
||||
continue;
|
||||
}
|
||||
|
||||
normalized += character;
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean JSON response from LLM by removing markdown code blocks.
|
||||
* Exportée pour couvrir les réponses LLM mal formées en régression.
|
||||
*/
|
||||
export function cleanJsonResponse(content: string): string {
|
||||
let cleaned = content.trim();
|
||||
|
||||
// Remove markdown code blocks
|
||||
@@ -123,7 +168,7 @@ function cleanJsonResponse(content: string): string {
|
||||
cleaned = cleaned.substring(startIdx, endIdx + 1);
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
return normalizeControlCharactersInJsonStrings(cleaned);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user