| | import { name1, name2, characters, getCharacterCardFieldsLazy, getGeneratingModel } from '../../../script.js'; |
| | import { groups, selected_group } from '../../../scripts/group-chats.js'; |
| | import { logMacroGeneralError } from './MacroDiagnostics.js'; |
| | import { getStringHash } from '/scripts/utils.js'; |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | export const env_provider_order = { |
| | EARLIEST: 0, |
| | EARLY: 10, |
| | NORMAL: 50, |
| | LATE: 90, |
| | LATEST: 100, |
| | }; |
| |
|
| | |
| | let instance; |
| | export { instance as MacroEnvBuilder }; |
| |
|
| | class MacroEnvBuilder { |
| | static #instance; |
| | static get instance() { return MacroEnvBuilder.#instance ?? (MacroEnvBuilder.#instance = new MacroEnvBuilder()); } |
| |
|
| | |
| | #providers; |
| |
|
| | constructor() { |
| | this.#providers = []; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | registerProvider(provider, order = env_provider_order.NORMAL) { |
| | if (typeof provider !== 'function') throw new Error('Provider must be a function'); |
| | this.#providers.push({ fn: provider, order }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | buildFromRawEnv(ctx) { |
| | |
| | |
| | |
| | const env = { |
| | content: ctx.content, |
| | contentHash: getStringHash(ctx.content), |
| | names: { user: '', char: '', group: '', groupNotMuted: '', notChar: '' }, |
| | character: {}, |
| | system: { model: '' }, |
| | functions: { postProcess: (x) => x }, |
| | dynamicMacros: {}, |
| | extra: {}, |
| | }; |
| |
|
| | if (ctx.replaceCharacterCard) { |
| | |
| | const fields = getCharacterCardFieldsLazy(); |
| | if (fields) { |
| | |
| | const fieldMappings = ([ |
| | ['charPrompt', 'system'], |
| | ['charInstruction', 'jailbreak'], |
| | ['description', 'description'], |
| | ['personality', 'personality'], |
| | ['scenario', 'scenario'], |
| | ['persona', 'persona'], |
| | ['mesExamplesRaw', 'mesExamples'], |
| | ['version', 'version'], |
| | ['charDepthPrompt', 'charDepthPrompt'], |
| | ['creatorNotes', 'creatorNotes'], |
| | ]); |
| | for (const [envKey, fieldKey] of fieldMappings) { |
| | Object.defineProperty(env.character, envKey, { |
| | get() { return fields[fieldKey] || ''; }, |
| | enumerable: true, |
| | configurable: true, |
| | }); |
| | } |
| | } |
| | } |
| |
|
| | |
| | env.names.user = ctx.name1Override ?? name1 ?? ''; |
| | env.names.char = ctx.name2Override ?? name2 ?? ''; |
| | env.names.group = getGroupValue(ctx, { currentChar: env.names.char, includeMuted: true }); |
| | env.names.groupNotMuted = getGroupValue(ctx, { currentChar: env.names.char, includeMuted: false }); |
| | env.names.notChar = getGroupValue(ctx, { currentChar: env.names.char, filterOutChar: true, includeUser: env.names.user }); |
| |
|
| | |
| | env.system.model = getGeneratingModel(); |
| |
|
| | |
| | |
| | if (typeof ctx.original === 'string') { |
| | let originalSubstituted = false; |
| | env.functions.original = () => { |
| | if (originalSubstituted) return ''; |
| | originalSubstituted = true; |
| | return ctx.original; |
| | }; |
| | } |
| | env.functions.postProcess = typeof ctx.postProcessFn === 'function' ? ctx.postProcessFn : (x) => x; |
| |
|
| | |
| | if (ctx.dynamicMacros && typeof ctx.dynamicMacros === 'object') { |
| | env.dynamicMacros = { ...ctx.dynamicMacros }; |
| | } |
| |
|
| | |
| | |
| | const orderedProviders = this.#providers.slice().sort((a, b) => a.order - b.order); |
| | for (const { fn } of orderedProviders) { |
| | try { |
| | fn(env, ctx); |
| | } catch (e) { |
| | |
| | logMacroGeneralError({ message: 'MacroEnvBuilder: Provider error', error: e }); |
| | } |
| | } |
| |
|
| | return env; |
| | } |
| | } |
| |
|
| | instance = MacroEnvBuilder.instance; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function getGroupValue(ctx, { currentChar = null, includeMuted = false, filterOutChar = false, includeUser = null }) { |
| | if (typeof ctx.groupOverride === 'string') { |
| | return ctx.groupOverride; |
| | } |
| |
|
| | if (!selected_group) return filterOutChar ? (includeUser || '') : (currentChar ?? ''); |
| |
|
| | const groupEntry = Array.isArray(groups) ? groups.find(x => x && x.id === selected_group) : null; |
| | const members = (groupEntry?.members ?? []); |
| | const disabledMembers = (groupEntry?.disabled_members ?? []); |
| |
|
| | const names = Array.isArray(members) |
| | ? members |
| | .filter(((id) => includeMuted ? true : !disabledMembers.includes(id))) |
| | .map(m => Array.isArray(characters) ? characters.find(c => c && c.avatar === m) : null) |
| | .filter(c => !!c && typeof c.name === 'string') |
| | .filter(c => !filterOutChar || c.name !== currentChar) |
| | .map(c => c.name) |
| | .join(', ') |
| | : ''; |
| |
|
| | return names; |
| | } |
| |
|