perf: memo GridCard, fuse save fns, fix slash tab active color
This commit is contained in:
43
memento-note/lib/ai/utils/clean-ai-response.ts
Normal file
43
memento-note/lib/ai/utils/clean-ai-response.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Utilitaires de nettoyage des réponses IA
|
||||
* Certains modèles (DeepSeek-R1, MiniMax, etc.) génèrent des blocs <think>
|
||||
* qui corrompent le parsing JSON si non supprimés.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Supprime les blocs <think>...</think> d'une réponse IA.
|
||||
* Gère aussi les blocs non fermés (balise ouvrante sans fermeture).
|
||||
* Supprime ensuite les fences Markdown (```json ... ```) éventuelles.
|
||||
*/
|
||||
export function cleanAIJsonResponse(text: string): string {
|
||||
// 1. Supprimer les blocs <think>...</think> fermés (greedy-safe)
|
||||
let cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').trim()
|
||||
|
||||
// 2. Si un <think> reste (non fermé), chercher le premier '[' ou '{'
|
||||
if (/<think>/i.test(cleaned)) {
|
||||
const jsonStart = cleaned.search(/[\[{]/)
|
||||
if (jsonStart !== -1) {
|
||||
cleaned = cleaned.slice(jsonStart)
|
||||
} else {
|
||||
// Aucun JSON trouvé après le think → vider pour provoquer une erreur propre
|
||||
cleaned = ''
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Supprimer les fences Markdown
|
||||
cleaned = cleaned.replace(/^```(?:json)?\n?/, '').replace(/\n?```$/, '').trim()
|
||||
|
||||
return cleaned
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprime les blocs <think> d'une réponse texte libre (non-JSON).
|
||||
*/
|
||||
export function cleanAITextResponse(text: string): string {
|
||||
let cleaned = text.replace(/<think>[\s\S]*?<\/think>/gi, '').trim()
|
||||
if (/<think>/i.test(cleaned)) {
|
||||
// Supprimer tout ce qui précède la fermeture du think ou jusqu'à la fin
|
||||
cleaned = cleaned.replace(/<think>[\s\S]*/i, '').trim()
|
||||
}
|
||||
return cleaned
|
||||
}
|
||||
Reference in New Issue
Block a user