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() {
193 const chatIdHash = getChatIdHash();193 const chatIdHash = getChatIdHash();
194194
195 // Use the full original input string for deterministic behavior195 // Use the full original input string for deterministic behavior
196 const rawContentHash = getStringHash(env.content);196 const rawContentHash = env.contentHash;
197197
198 const offset = typeof range?.startOffset === 'number' ? range.startOffset : 0;198 const offset = typeof range?.startOffset === 'number' ? range.startOffset : 0;
199199
public/scripts/macros/engine/MacroEnv.types.js+1 -0
@@ -45,6 +45,7 @@
45/**45/**
46 * @typedef {Object} MacroEnv46 * @typedef {Object} MacroEnv
47 * @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.47 * @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.
48 * @property {MacroEnvNames} names49 * @property {MacroEnvNames} names
49 * @property {MacroEnvCharacter} character50 * @property {MacroEnvCharacter} character
50 * @property {MacroEnvSystem} system51 * @property {MacroEnvSystem} system
public/scripts/macros/engine/MacroEnvBuilder.js+2 -0
@@ -1,6 +1,7 @@
1import { name1, name2, characters, getCharacterCardFieldsLazy, getGeneratingModel } from '../../../script.js';1import { name1, name2, characters, getCharacterCardFieldsLazy, getGeneratingModel } from '../../../script.js';
2import { groups, selected_group } from '../../../scripts/group-chats.js';2import { groups, selected_group } from '../../../scripts/group-chats.js';
3import { logMacroGeneralError } from './MacroDiagnostics.js';3import { logMacroGeneralError } from './MacroDiagnostics.js';
4import { getStringHash } from '/scripts/utils.js';
4/**5/**
5 * MacroEnvBuilder is responsible for constructing the MacroEnv object6 * MacroEnvBuilder is responsible for constructing the MacroEnv object
6 * that is passed to macro handlers.7 * that is passed to macro handlers.
@@ -83,6 +84,7 @@ class MacroEnvBuilder {
83 /** @type {MacroEnv} */84 /** @type {MacroEnv} */
84 const env = {85 const env = {
85 content: ctx.content,86 content: ctx.content,
87 contentHash: getStringHash(ctx.content),
86 names: { user: '', char: '', group: '', groupNotMuted: '', notChar: '' },88 names: { user: '', char: '', group: '', groupNotMuted: '', notChar: '' },
87 character: {},89 character: {},
88 system: { model: '' },90 system: { model: '' },