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