#321 Predict trailing asterisk during streaming

This commit is contained in:
SillyLossy
2023-05-19 23:40:57 +03:00
parent ade631e258
commit d50067270c
2 changed files with 23 additions and 1 deletions

View File

@ -107,7 +107,7 @@ import {
setPoeOnlineStatus, setPoeOnlineStatus,
} from "./scripts/poe.js"; } from "./scripts/poe.js";
import { debounce, delay, restoreCaretPosition, saveCaretPosition, end_trim_to_sentence } from "./scripts/utils.js"; import { debounce, delay, restoreCaretPosition, saveCaretPosition, end_trim_to_sentence, countOccurrences, isOdd } from "./scripts/utils.js";
import { extension_settings, loadExtensionSettings } from "./scripts/extensions.js"; import { extension_settings, loadExtensionSettings } from "./scripts/extensions.js";
import { executeSlashCommands, getSlashCommandsHelp, registerSlashCommand } from "./scripts/slash-commands.js"; import { executeSlashCommands, getSlashCommandsHelp, registerSlashCommand } from "./scripts/slash-commands.js";
import { import {
@ -1516,6 +1516,12 @@ class StreamingProcessor {
let isName = result.this_mes_is_name; let isName = result.this_mes_is_name;
processedText = result.getMessage; processedText = result.getMessage;
// Predict unbalanced asterisks during streaming
if (!isFinal && isOdd(countOccurrences(processedText, '*'))) {
// Add asterisk at the end to balance it
processedText = processedText.trimEnd() + '*';
}
if (isImpersonate) { if (isImpersonate) {
$('#send_textarea').val(processedText).trigger('input'); $('#send_textarea').val(processedText).trigger('input');
} }

View File

@ -216,3 +216,19 @@ export function end_trim_to_sentence(input, include_newline = false) {
return input.substring(0, last + 1).trimEnd(); return input.substring(0, last + 1).trimEnd();
} }
export function countOccurrences(string, character) {
let count = 0;
for (let i = 0; i < string.length; i++) {
if (string[i] === character) {
count++;
}
}
return count;
}
export function isOdd(number) {
return number % 2 !== 0;
}