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