| 1 | import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js'; |
| 2 | import { eventSource, event_types } from '../../events.js'; |
| 3 | import { findExtension } from '/scripts/extensions.js'; |
| 4 | |
| 5 | let lastGenerationTypeValue = ''; |
| 6 | let lastGenerationTypeTrackingInitialized = false; |
| 7 | |
| 8 | function ensureLastGenerationTypeTracking() { |
| 9 | if (lastGenerationTypeTrackingInitialized) { |
| 10 | return; |
| 11 | } |
| 12 | lastGenerationTypeTrackingInitialized = true; |
| 13 | |
| 14 | try { |
| 15 | eventSource?.on?.(event_types.GENERATION_STARTED, (type, _params, isDryRun) => { |
| 16 | if (isDryRun) return; |
| 17 | lastGenerationTypeValue = type || 'normal'; |
| 18 | }); |
| 19 | |
| 20 | eventSource?.on?.(event_types.CHAT_CHANGED, () => { |
| 21 | lastGenerationTypeValue = ''; |
| 22 | }); |
| 23 | } catch { |
| 24 | // In non-runtime environments (tests), eventSource may be undefined or not fully initialized. |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Registers macros that depend on runtime application state or event tracking |
| 30 | * rather than static environment fields. |
| 31 | */ |
| 32 | export function registerStateMacros() { |
| 33 | ensureLastGenerationTypeTracking(); |
| 34 | |
| 35 | MacroRegistry.registerMacro('lastGenerationType', { |
| 36 | category: MacroCategory.STATE, |
| 37 | description: 'Type of the last queued generation request (e.g. "normal", "impersonate", "regenerate", "quiet", "swipe", "continue"). Empty if none yet or chat was switched.', |
| 38 | returns: 'Type of the last queued generation request.', |
| 39 | handler: () => lastGenerationTypeValue, |
| 40 | }); |
| 41 | |
| 42 | // Macro that checks if an extension is enabled |
| 43 | MacroRegistry.registerMacro('hasExtension', { |
| 44 | category: MacroCategory.STATE, |
| 45 | unnamedArgs: [{ |
| 46 | name: 'extensionName', |
| 47 | type: 'string', |
| 48 | description: 'The name of the extension to check', |
| 49 | }], |
| 50 | description: 'Checks if a specific extension is enabled. If the extension does not exist, returns false.', |
| 51 | returns: 'true if the extension is enabled, false otherwise.', |
| 52 | handler: ({ unnamedArgs: [extensionName] }) => { |
| 53 | const extension = findExtension(extensionName); |
| 54 | return String(extension?.enabled ?? false); |
| 55 | }, |
| 56 | }); |
| 57 | } |