| 1117 | // Bias | 1117 | // Bias |
| 1118 | if (bias && bias.trim().length) await addToChatCompletion('bias'); | 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 | if (prompts.has('summary')) { | 1121 | if (chatCompletion.has('main')) { |
| 1122 | const summary = prompts.get('summary'); | 1122 | const message = await Message.fromPromptAsync(prompt); |
| 1123 | | 1123 | chatCompletion.insert(message, 'main', position); |
| 1124 | if (summary.position) { | 1124 | } 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 | chatCompletion.insert(message, 'main', authorsNote.position); | 1136 | absolutePrompts.splice(newIndex, 0, promptCopy); |
| 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 | } | 1137 | } |
| 1148 | } | 1138 | } |
| | 1139 | }; |
| 1149 | | 1140 | |
| 1150 | // Vectors Data Bank | 1141 | const knownPrompts = [ |
| 1151 | if (prompts.has('vectorsDataBank')) { | 1142 | 'summary', |
| 1152 | const vectorsDataBank = prompts.get('vectorsDataBank'); | 1143 | 'authorsNote', |
| | 1144 | 'vectorsMemory', |
| | 1145 | 'vectorsDataBank', |
| | 1146 | 'smartContext', |
| | 1147 | ]; |
| 1153 | | 1148 | |
| 1154 | if (vectorsDataBank.position) { | 1149 | // Known relative extension prompts |
| 1155 | const message = await Message.fromPromptAsync(vectorsDataBank); | 1150 | for (const key of knownPrompts) { |
| 1156 | chatCompletion.insert(message, 'main', vectorsDataBank.position); | 1151 | if (prompts.has(key)) { |
| | 1152 | const prompt = prompts.get(key); |
| | 1153 | if (prompt.position) { |
| | 1154 | await injectToMain(prompt, prompt.position); |
| 1157 | } | 1155 | } |
| 1158 | } | 1156 | } |
| 1159 | | | |
| 1160 | // Smart Context (ChromaDB) | | |
| 1161 | if (prompts.has('smartContext')) { | | |
| 1162 | const smartContext = prompts.get('smartContext'); | | |
| 1163 | | | |
| 1164 | if (smartContext.position) { | | |
| 1165 | const message = await Message.fromPromptAsync(smartContext); | | |
| 1166 | chatCompletion.insert(message, 'main', smartContext.position); | | |
| 1167 | } | | |
| 1168 | } | 1157 | } |
| 1169 | | 1158 | |
| 1170 | // Other relative extension prompts | 1159 | // Other relative extension prompts |
| 1171 | for (const prompt of prompts.collection.filter(p => p.extension && p.position)) { | 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 | // Pre-allocation of tokens for tool data | 1164 | // Pre-allocation of tokens for tool data |