From bf3e7a8eb14a8d7688878eb500c3a66a7443bc8d Mon Sep 17 00:00:00 2001 From: Chris Malloy Date: Fri, 24 Jan 2025 11:06:02 -0400 Subject: [PATCH] Only try to fix linefeeds for claude --- src/app/mods/ai.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app/mods/ai.ts b/src/app/mods/ai.ts index 5e2bff67..7dd1b4a4 100644 --- a/src/app/mods/ai.ts +++ b/src/app/mods/ai.ts @@ -171,26 +171,26 @@ export const aiQueryPlugin: Plugin = { system, messages: messages.filter(m => m.role !== 'system'), }); - completion = '{"ref":[' + res.content[0]?.text; + function fixJsonLinefeeds(json) { + return json.replace(/"(?:\\\\.|[^"])*"|[^"]+/g, (match) => { + if (match.startsWith('"')) { + // This is a string, replace linefeeds + return match.replace(/\\n/g, '\\\\n'); + } + // This is not a string, return as-is + return match; + }); + } + completion = fixJsonLinefeeds('{"ref":[' + res.content[0]?.text); usage = { 'prompt_tokens': res.usage.input_tokens, 'completion_tokens': res.usage.output_tokens, 'total_tokens': res.usage.input_tokens + res.usage.output_tokens, }; } - function fixJsonLinefeeds(json) { - return json.replace(/"(?:\\\\.|[^"])*"|[^"]+/g, (match) => { - if (match.startsWith('"')) { - // This is a string, replace linefeeds - return match.replace(/\\n/g, '\\\\n'); - } - // This is not a string, return as-is - return match; - }); - } let bundle; try { - bundle = JSON.parse(fixJsonLinefeeds(completion)); + bundle = JSON.parse(completion); if (!bundle.ref) { // Model returned a bare Ref? bundle = {