Skip to content

Commit

Permalink
Only try to fix linefeeds for claude
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Jan 24, 2025
1 parent cd3f29e commit bf3e7a8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/app/mods/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit bf3e7a8

Please sign in to comment.