Merge pull request #3916 from SillyTavern/fix-instruct-regex Check instruct activation regex before selecting context template

37c97db96980f63357aeca393cfa0d1d553de7b0

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +26 -24Ignore whitespace
public/scripts/instruct-mode.js+26 -24
@@ -208,37 +208,39 @@ export function autoSelectInstructPreset(modelId) {
208208
209 // Select matching instruct preset209 // Select matching instruct preset
210 let foundMatch = false;210 let foundMatch = false;
211 for (const instruct_preset of instruct_presets) {211
212 // If instruct preset matches the context template212 for (const preset of instruct_presets) {
213 if (power_user.instruct.bind_to_context && instruct_preset.name === power_user.context.preset) {213 // If activation regex is set, check if it matches the model id
214 foundMatch = true;214 if (preset.activation_regex) {
215 selectInstructPreset(instruct_preset.name, { isAuto: true });215 try {
216 break;216 const regex = regexFromString(preset.activation_regex);
217
218 // Stop on first match so it won't cycle back and forth between presets if multiple regexes match
219 if (regex instanceof RegExp && regex.test(modelId)) {
220 selectInstructPreset(preset.name, { isAuto: true });
221 foundMatch = true;
222 break;
223 }
224 } catch {
225 // If regex is invalid, ignore it
226 console.warn(`Invalid instruct activation regex in preset "${preset.name}"`);
227 }
217 }228 }
218 }229 }
230
219 // If no match was found, auto-select instruct preset231 // If no match was found, auto-select instruct preset
220 if (!foundMatch) {232 if (!foundMatch && power_user.instruct.bind_to_context) {
221 for (const preset of instruct_presets) {233 for (const instruct_preset of instruct_presets) {
222 // If activation regex is set, check if it matches the model id234 // If instruct preset matches the context template
223 if (preset.activation_regex) {235 if (instruct_preset.name === power_user.context.preset) {
224 try {236 selectInstructPreset(instruct_preset.name, { isAuto: true });
225 const regex = regexFromString(preset.activation_regex);237 foundMatch = true;
226238 break;
227 // Stop on first match so it won't cycle back and forth between presets if multiple regexes match
228 if (regex instanceof RegExp && regex.test(modelId)) {
229 selectInstructPreset(preset.name, { isAuto: true });
230
231 return true;
232 }
233 } catch {
234 // If regex is invalid, ignore it
235 console.warn(`Invalid instruct activation regex in preset "${preset.name}"`);
236 }
237 }239 }
238 }240 }
239 }241 }
240242
241 return false;243 return foundMatch;
242}244}
243245
244/**246/**