mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Split messages into chunks
This commit is contained in:
@ -231,4 +231,35 @@ export function countOccurrences(string, character) {
|
||||
|
||||
export function isOdd(number) {
|
||||
return number % 2 !== 0;
|
||||
}
|
||||
|
||||
/** Split string to parts no more than length in size */
|
||||
export function splitRecursive(input, length, delimitiers = ['\n\n', '\n', ' ', '']) {
|
||||
const delim = delimitiers[0] ?? '';
|
||||
const parts = input.split(delim);
|
||||
|
||||
const flatParts = parts.flatMap(p => {
|
||||
if (p.length < length) return p;
|
||||
return splitRecursive(input, length, delimitiers.slice(1));
|
||||
});
|
||||
|
||||
// Merge short chunks
|
||||
const result = [];
|
||||
let currentChunk = '';
|
||||
for (let i = 0; i < flatParts.length;) {
|
||||
currentChunk = flatParts[i];
|
||||
let j = i + 1;
|
||||
while (j < flatParts.length) {
|
||||
const nextChunk = flatParts[j];
|
||||
if (currentChunk.length + nextChunk.length + delim.length <= length) {
|
||||
currentChunk += delim + nextChunk;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i = j;
|
||||
result.push(currentChunk);
|
||||
}
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user