fix(eo): sanitize DeepSeek JSON response before parsing

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hermann_Kitio 2026-04-25 07:25:47 +03:00
parent 0c6e4cfac1
commit 4fde66d930

View file

@ -402,6 +402,20 @@ export function truncateToMaxWords(
// ── Appels DeepSeek ────────────────────────────────────────────────────── // ── Appels DeepSeek ──────────────────────────────────────────────────────
/**
* Strip d'éventuels fences markdown autour d'une réponse JSON.
*
* DeepSeek wrappe parfois sa sortie dans des blocs ```json ... ``` malgré
* `response_format: { type: "json_object" }`. On retire ces fences avant
* `JSON.parse` pour éviter `SyntaxError: Expected property name`.
*/
function sanitizeJsonContent(raw: string): string {
return raw
.replace(/^\s*```(?:json)?\s*/i, "")
.replace(/\s*```\s*$/i, "")
.trim();
}
async function callDeepSeek( async function callDeepSeek(
system: string, system: string,
user: string, user: string,
@ -444,7 +458,7 @@ async function callDeepSeek(
throw new Error("DeepSeek API: réponse vide"); throw new Error("DeepSeek API: réponse vide");
} }
return content; return sanitizeJsonContent(content);
} catch (err) { } catch (err) {
const kind = const kind =
err instanceof Error && err.name === "TimeoutError" err instanceof Error && err.name === "TimeoutError"