Optimize `{{pick}}` macro by caching content hash in MacroEnv

000fbccda950466ceb55b4c2ba1cc96f57c8f682

Wolfsblvt <wolfsblvt@gmail.com>

3 files changed, +4 -1Ignore whitespace
public/scripts/macros/definitions/core-macros.js+1 -1
@@ -193,7 +193,7 @@ export function registerCoreMacros() {
193193 const chatIdHash = getChatIdHash();
194194
195195 // Use the full original input string for deterministic behavior
196196 const rawContentHash = getStringHash(env.content)contentHash;
197197
198198 const offset = typeof range?.startOffset === 'number' ? range.startOffset : 0;
199199
public/scripts/macros/engine/MacroEnv.types.js+1 -0
@@ -45,6 +45,7 @@
4545/**
4646 * @typedef {Object} MacroEnv
4747 * @property {string} content - The full original input string that is being processed by the macro engine. This is the same value as substituteParams "content" and is provided so macros can build deterministic behavior based on the whole prompt when needed.
48+ * @property {number} contentHash - A hash of the content string, used for caching and comparison.
4849 * @property {MacroEnvNames} names
4950 * @property {MacroEnvCharacter} character
5051 * @property {MacroEnvSystem} system
public/scripts/macros/engine/MacroEnvBuilder.js+2 -0
@@ -1,6 +1,7 @@
11import { name1, name2, characters, getCharacterCardFieldsLazy, getGeneratingModel } from '../../../script.js';
22import { groups, selected_group } from '../../../scripts/group-chats.js';
33import { logMacroGeneralError } from './MacroDiagnostics.js';
4+import { getStringHash } from '/scripts/utils.js';
45/**
56 * MacroEnvBuilder is responsible for constructing the MacroEnv object
67 * that is passed to macro handlers.
@@ -83,6 +84,7 @@ class MacroEnvBuilder {
8384 /** @type {MacroEnv} */
8485 const env = {
8586 content: ctx.content,
87+ contentHash: getStringHash(ctx.content),
8688 names: { user: '', char: '', group: '', groupNotMuted: '', notChar: '' },
8789 character: {},
8890 system: { model: '' },