|
3570 | 3570 | );
|
3571 | 3571 | }
|
3572 | 3572 |
|
3573 |
| -/** |
3574 |
| - * Split a string with a RegExp separator an optionally limited number of times. |
3575 |
| - * (https://stackoverflow.com/a/64296576) |
3576 |
| - * @param {string} input |
3577 |
| - * @param {RegExp} separator |
3578 |
| - * @param {number} [limit] - If not included, splits the maximum times |
3579 |
| - * @returns {[string[], string[]]} |
3580 |
| - */ |
3581 |
| -function split(input, separator, limit) { |
3582 |
| - separator = new RegExp(separator, 'g'); |
3583 |
| - limit = limit ?? -1; |
3584 |
| - |
3585 |
| - const output = []; |
3586 |
| - const separators = []; |
3587 |
| - let finalIndex = 0; |
3588 |
| - |
3589 |
| - while (limit--) { |
3590 |
| - const lastIndex = separator.lastIndex; |
3591 |
| - const search = separator.exec(input); |
3592 |
| - if (search === null) { |
3593 |
| - break; |
3594 |
| - } |
3595 |
| - finalIndex = separator.lastIndex; |
3596 |
| - output.push(input.slice(lastIndex, search.index)); |
3597 |
| - separators.push(search[0]); |
3598 |
| - } |
3599 |
| - |
3600 |
| - output.push(input.slice(finalIndex)); |
3601 |
| - |
3602 |
| - return [output, separators]; |
| 3573 | +function regexSplitString(str, separator, limit) { |
| 3574 | + const result = []; |
| 3575 | + const separators = []; |
| 3576 | + let lastIndex = 0; |
| 3577 | + let match; |
| 3578 | + const regex = new RegExp(separator, 'g'); |
| 3579 | + |
| 3580 | + while ((match = regex.exec(str)) !== null) { |
| 3581 | + if (limit !== undefined && result.length >= limit) break; |
| 3582 | + |
| 3583 | + result.push(str.slice(lastIndex, match.index)); |
| 3584 | + separators.push(match[0]); |
| 3585 | + lastIndex = match.index + match[0].length; |
| 3586 | + } |
| 3587 | + |
| 3588 | + result.push(str.slice(lastIndex)); // Add the remainder of the string |
| 3589 | + |
| 3590 | + return [result, separators]; |
3603 | 3591 | }
|
3604 | 3592 |
|
3605 | 3593 | function useSessionState(sessionStorage, name, initialState) {
|
|
4097 | 4085 |
|
4098 | 4086 | if (chunk.content.includes(fillPlaceholder) || chunk.content.includes(predictPlaceholder)) {
|
4099 | 4087 | // split the chunk in 2
|
4100 |
| - let [sides, separators] = split(chunk.content, placeholderRegex, 1); |
| 4088 | + let [sides, separators] = regexSplitString(chunk.content, placeholderRegex, 1); |
4101 | 4089 | foundFillPlaceholder = separators[0] == fillPlaceholder;
|
4102 | 4090 | foundPredictPlaceholder = separators[0] == predictPlaceholder;
|
4103 | 4091 |
|
|
4135 | 4123 | } else {
|
4136 | 4124 | promptText = joinPrompt(leftPromptChunks);
|
4137 | 4125 | }
|
4138 |
| - |
| 4126 | + |
4139 | 4127 | const assembledWorldInfo = assembleWorldInfo(promptText);
|
4140 | 4128 | const additionalContextPrompt = assembleAdditionalContext(assembledWorldInfo, promptText);
|
4141 | 4129 | const finalPrompt = replacePlaceholders(additionalContextPrompt, templateReplacements);
|
|
0 commit comments