| 1117 | 1117 | // Bias |
| 1118 | 1118 | if (bias && bias.trim().length) await addToChatCompletion('bias'); |
| 1119 | 1119 | |
| 1120 | | - // Tavern Extras - Summary |
| 1120 | + const injectToMain = async (/** @type {Prompt} */ prompt, /** @type {string|number} */ position) => { |
| 1121 | 1121 | if (promptschatCompletion.has('summarymain')) { |
| 1122 | 1122 | const summarymessage = promptsawait Message.getfromPromptAsync('summary'prompt); |
| 1123 | | - |
| 1123 | + chatCompletion.insert(message, 'main', position); |
| 1124 | 1124 | if} (summary.position)else { |
| 1125 | | - const message = await Message.fromPromptAsync(summary); |
| 1125 | + // Convert the relative prompt to an injection and place it relative to main prompt |
| 1126 | | - chatCompletion.insert(message, 'main', summary.position); |
| 1126 | + // Keeping prompts in the same order bucket will squash them together during in-chat injection |
| 1127 | | - } |
| 1127 | + const indexOfMain = absolutePrompts.findIndex(p => p.identifier === 'main'); |
| 1128 | | - } |
| 1128 | + if (indexOfMain >= 0) { |
| 1129 | | - |
| 1129 | + const main = absolutePrompts[indexOfMain]; |
| 1130 | | - // Authors Note |
| 1130 | + const promptCopy = new Prompt(prompt); |
| 1131 | | - if (prompts.has('authorsNote')) { |
| 1131 | + promptCopy.role = main.role; |
| 1132 | | - const authorsNote = prompts.get('authorsNote'); |
| 1132 | + promptCopy.injection_position = main.injection_position; |
| 1133 | | - |
| 1133 | + promptCopy.injection_depth = main.injection_depth; |
| 1134 | | - if (authorsNote.position) { |
| 1134 | + promptCopy.injection_order = main.injection_order; |
| 1135 | | - const message = await Message.fromPromptAsync(authorsNote); |
| 1135 | + const newIndex = position === 'end' ? indexOfMain + 1 : indexOfMain; |
| 1136 | 1136 | chatCompletion absolutePrompts.insertsplice(messagenewIndex, 'main'0, authorsNote.positionpromptCopy); |
| 1137 | 1137 | } |
| 1138 | | - } |
| 1139 | | - |
| 1140 | | - // Vectors Memory |
| 1141 | | - if (prompts.has('vectorsMemory')) { |
| 1142 | | - const vectorsMemory = prompts.get('vectorsMemory'); |
| 1143 | | - |
| 1144 | | - if (vectorsMemory.position) { |
| 1145 | | - const message = await Message.fromPromptAsync(vectorsMemory); |
| 1146 | | - chatCompletion.insert(message, 'main', vectorsMemory.position); |
| 1147 | | - } |
| 1148 | | - } |
| 1149 | | - |
| 1150 | | - // Vectors Data Bank |
| 1151 | | - if (prompts.has('vectorsDataBank')) { |
| 1152 | | - const vectorsDataBank = prompts.get('vectorsDataBank'); |
| 1153 | | - |
| 1154 | | - if (vectorsDataBank.position) { |
| 1155 | | - const message = await Message.fromPromptAsync(vectorsDataBank); |
| 1156 | | - chatCompletion.insert(message, 'main', vectorsDataBank.position); |
| 1157 | 1138 | } |
| 1158 | 1139 | }; |
| 1159 | 1140 | |
| 1160 | | - // Smart Context (ChromaDB) |
| 1141 | + const knownPrompts = [ |
| 1161 | | - if (prompts.has('smartContext')) { |
| 1142 | + 'summary', |
| 1162 | | - const smartContext = prompts.get('smartContext'); |
| 1143 | + 'authorsNote', |
| 1144 | + 'vectorsMemory', |
| 1145 | + 'vectorsDataBank', |
| 1146 | + 'smartContext', |
| 1147 | + ]; |
| 1163 | 1148 | |
| 1164 | | - if (smartContext.position) { |
| 1149 | + // Known relative extension prompts |
| 1165 | | - const message = await Message.fromPromptAsync(smartContext); |
| 1150 | + for (const key of knownPrompts) { |
| 1166 | | - chatCompletion.insert(message, 'main', smartContext.position); |
| 1151 | + if (prompts.has(key)) { |
| 1152 | + const prompt = prompts.get(key); |
| 1153 | + if (prompt.position) { |
| 1154 | + await injectToMain(prompt, prompt.position); |
| 1155 | + } |
| 1167 | 1156 | } |
| 1168 | 1157 | } |
| 1169 | 1158 | |
| 1170 | 1159 | // Other relative extension prompts |
| 1171 | 1160 | for (const prompt of prompts.collection.filter(p => p.extension && p.position)) { |
| 1172 | | - const message = await Message.fromPromptAsync(prompt); |
| 1161 | + await injectToMain(prompt, prompt.position); |
| 1173 | | - chatCompletion.insert(message, 'main', prompt.position); |
| 1174 | 1162 | } |
| 1175 | 1163 | |
| 1176 | 1164 | // Pre-allocation of tokens for tool data |