| 208 | | 208 | |
| 209 | // Select matching instruct preset | 209 | // 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 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 | } |
| 217 | } | 228 | } |
| 218 | } | 229 | } |
| | 230 | |
| 219 | // If no match was found, auto-select instruct preset | 231 | // 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 id | 234 | // 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; |
| 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 | } | | |
| 237 | } | 239 | } |
| 238 | } | 240 | } |
| 239 | } | 241 | } |
| 240 | | 242 | |
| 241 | return false; | 243 | return foundMatch; |
| 242 | } | 244 | } |
| 243 | | 245 | |
| 244 | /** | 246 | /** |