Files
SillyTavern/public/scripts/macros/definitions/state-macros.js
T
Wolfsblvt cd0627bfec Macros 2.0 [➕Macros] - Add {{hasExtension}} macro, and refactor extension lookup logic (#4948)
* Add {{hasExtension}} macro and refactor extension lookup logic

- Move findExtension() from extensions-slashcommands.js to extensions.js and export it
- Update findExtension() to return object with name and enabled properties instead of just name string
- Add {{hasExtension}} macro to check if an extension is enabled
- Update /extension-state and /extension-exists commands to use refactored findExtension()
- Remove duplicate findExtension() implementation from extensions-slashcommands.js

* Refactor extension action callbacks to use extension object instead of separate name and enabled properties

* fix eslint
2026-01-04 20:37:27 +02:00

58 lines
2.1 KiB
JavaScript

import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js';
import { eventSource, event_types } from '../../events.js';
import { findExtension } from '/scripts/extensions.js';
let lastGenerationTypeValue = '';
let lastGenerationTypeTrackingInitialized = false;
function ensureLastGenerationTypeTracking() {
if (lastGenerationTypeTrackingInitialized) {
return;
}
lastGenerationTypeTrackingInitialized = true;
try {
eventSource?.on?.(event_types.GENERATION_STARTED, (type, _params, isDryRun) => {
if (isDryRun) return;
lastGenerationTypeValue = type || 'normal';
});
eventSource?.on?.(event_types.CHAT_CHANGED, () => {
lastGenerationTypeValue = '';
});
} catch {
// In non-runtime environments (tests), eventSource may be undefined or not fully initialized.
}
}
/**
* Registers macros that depend on runtime application state or event tracking
* rather than static environment fields.
*/
export function registerStateMacros() {
ensureLastGenerationTypeTracking();
MacroRegistry.registerMacro('lastGenerationType', {
category: MacroCategory.STATE,
description: 'Type of the last queued generation request (e.g. "normal", "impersonate", "regenerate", "quiet", "swipe", "continue"). Empty if none yet or chat was switched.',
returns: 'Type of the last queued generation request.',
handler: () => lastGenerationTypeValue,
});
// Macro that checks if an extension is enabled
MacroRegistry.registerMacro('hasExtension', {
category: MacroCategory.STATE,
unnamedArgs: [{
name: 'extensionName',
type: 'string',
description: 'The name of the extension to check',
}],
description: 'Checks if a specific extension is enabled. If the extension does not exist, returns false.',
returns: 'true if the extension is enabled, false otherwise.',
handler: ({ unnamedArgs: [extensionName] }) => {
const extension = findExtension(extensionName);
return String(extension?.enabled ?? false);
},
});
}