Auto-complete code blocks during streaming

This commit is contained in:
Cohee 2023-10-20 23:52:23 +03:00
parent e4cb916dff
commit 7f8df9533b
2 changed files with 4 additions and 3 deletions

View File

@ -2185,11 +2185,12 @@ class StreamingProcessor {
let processedText = cleanUpMessage(text, isImpersonate, isContinue, !isFinal);
// Predict unbalanced asterisks / quotes during streaming
const charsToBalance = ['*', '"'];
const charsToBalance = ['*', '"', '```'];
for (const char of charsToBalance) {
if (!isFinal && isOdd(countOccurrences(processedText, char))) {
// Add character at the end to balance it
processedText = processedText.trimEnd() + char;
const separator = char.length > 1 ? '\n' : '';
processedText = processedText.trimEnd() + separator + char;
}
}

View File

@ -498,7 +498,7 @@ export function countOccurrences(string, character) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] === character) {
if (string.substring(i, i + character.length) === character) {
count++;
}
}