Check instruct activation regex before selecting bound context template match

a927ab557a1c97a2e67efc3b3da4626a7ec3aa77

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

1 files changed, +26 -24Ignore whitespace
public/scripts/instruct-mode.js+26 -24
@@ -208,37 +208,39 @@ export function autoSelectInstructPreset(modelId) {
208208
209209 // Select matching instruct preset
210210 let foundMatch = false;
211- for (const instruct_preset of instruct_presets) {
211+
212- // If instruct preset matches the context template
212+ 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+ }
217228 }
218229 }
230+
219231 // If no match was found, auto-select instruct preset
220232 if (!foundMatch && power_user.instruct.bind_to_context) {
221233 for (const presetinstruct_preset of instruct_presets) {
222234 // If activation regex is set, check ifinstruct itpreset matches the modelcontext idtemplate
223235 if (presetinstruct_preset.activation_regexname === power_user.context.preset) {
224- try {
236+ selectInstructPreset(instruct_preset.name, { isAuto: true });
225- const regex = regexFromString(preset.activation_regex);
237+ foundMatch = true;
226-
238+ 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- }
237239 }
238240 }
239241 }
240242
241243 return falsefoundMatch;
242244}
243245
244246/**