| 1 | import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js'; |
| 2 | import { power_user } from '../../power-user.js'; |
| 3 | |
| 4 | /** |
| 5 | * Registers instruct-mode related {{...}} macros (instruct* and system |
| 6 | * prompt/context macros) in the MacroRegistry. |
| 7 | */ |
| 8 | export function registerInstructMacros() { |
| 9 | /** |
| 10 | * Helper to register macros that just expose a value from power_user.instruct. |
| 11 | * The first name is the primary, subsequent names become visible aliases. |
| 12 | * @param {string[]} names - First is primary, rest are aliases. |
| 13 | * @param {() => string} getValue |
| 14 | * @param {() => boolean} isEnabled |
| 15 | * @param {string} description |
| 16 | * @param {string} [category=MacroCategory.PROMPTS] |
| 17 | */ |
| 18 | function registerSimple(names, getValue, isEnabled, description, category = MacroCategory.PROMPTS) { |
| 19 | const [primary, ...aliasNames] = names; |
| 20 | const aliases = aliasNames.map(alias => ({ alias })); |
| 21 | |
| 22 | MacroRegistry.registerMacro(primary, { |
| 23 | category, |
| 24 | description, |
| 25 | aliases: aliases.length > 0 ? aliases : undefined, |
| 26 | handler: () => (isEnabled() ? (getValue() ?? '') : ''), |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | const instEnabled = () => !!power_user.instruct.enabled; |
| 31 | const sysEnabled = () => !!power_user.sysprompt.enabled; |
| 32 | |
| 33 | // Instruct template macros |
| 34 | registerSimple(['instructStoryStringPrefix'], () => power_user.instruct.story_string_prefix, instEnabled, 'Instruct story string prefix.'); |
| 35 | registerSimple(['instructStoryStringSuffix'], () => power_user.instruct.story_string_suffix, instEnabled, 'Instruct story string suffix.'); |
| 36 | |
| 37 | registerSimple(['instructUserPrefix', 'instructInput'], () => power_user.instruct.input_sequence, instEnabled, 'Instruct input / user prefix sequence.'); |
| 38 | registerSimple(['instructUserSuffix'], () => power_user.instruct.input_suffix, instEnabled, 'Instruct input / user suffix sequence.'); |
| 39 | |
| 40 | registerSimple(['instructAssistantPrefix', 'instructOutput'], () => power_user.instruct.output_sequence, instEnabled, 'Instruct output / assistant prefix sequence.'); |
| 41 | registerSimple(['instructAssistantSuffix', 'instructSeparator'], () => power_user.instruct.output_suffix, instEnabled, 'Instruct output / assistant suffix sequence.'); |
| 42 | |
| 43 | registerSimple(['instructSystemPrefix'], () => power_user.instruct.system_sequence, instEnabled, 'Instruct system prefix sequence.'); |
| 44 | registerSimple(['instructSystemSuffix'], () => power_user.instruct.system_suffix, instEnabled, 'Instruct system suffix sequence.'); |
| 45 | |
| 46 | registerSimple(['instructFirstAssistantPrefix', 'instructFirstOutputPrefix'], () => power_user.instruct.first_output_sequence || power_user.instruct.output_sequence, instEnabled, 'Instruct first assistant / output prefix sequence'); |
| 47 | registerSimple(['instructLastAssistantPrefix', 'instructLastOutputPrefix'], () => power_user.instruct.last_output_sequence || power_user.instruct.output_sequence, instEnabled, 'Instruct last assistant / output prefix sequence.'); |
| 48 | |
| 49 | registerSimple(['instructStop'], () => power_user.instruct.stop_sequence, instEnabled, 'Instruct stop sequence.'); |
| 50 | registerSimple(['instructUserFiller'], () => power_user.instruct.user_alignment_message, instEnabled, 'Instruct user alignment filler.'); |
| 51 | registerSimple(['instructSystemInstructionPrefix'], () => power_user.instruct.last_system_sequence, instEnabled, 'Instruct system instruction prefix sequence.'); |
| 52 | |
| 53 | registerSimple(['instructFirstUserPrefix', 'instructFirstInput'], () => power_user.instruct.first_input_sequence || power_user.instruct.input_sequence, instEnabled, 'Instruct first user / input prefix sequence.'); |
| 54 | registerSimple(['instructLastUserPrefix', 'instructLastInput'], () => power_user.instruct.last_input_sequence || power_user.instruct.input_sequence, instEnabled, 'Instruct last user / input prefix sequence.'); |
| 55 | |
| 56 | // System prompt macros |
| 57 | registerSimple(['defaultSystemPrompt', 'instructSystem', 'instructSystemPrompt'], () => power_user.sysprompt.content, sysEnabled, 'Default system prompt.'); |
| 58 | |
| 59 | MacroRegistry.registerMacro('systemPrompt', { |
| 60 | category: MacroCategory.PROMPTS, |
| 61 | description: 'Active system prompt text (optionally overridden by character prompt)', |
| 62 | handler: ({ env }) => { |
| 63 | const isEnabled = !!power_user.sysprompt.enabled; |
| 64 | if (!isEnabled) return ''; |
| 65 | |
| 66 | if (power_user.prefer_character_prompt && env.character.charPrompt) { |
| 67 | return env.character.charPrompt; |
| 68 | } |
| 69 | return power_user.sysprompt.content ?? ''; |
| 70 | }, |
| 71 | }); |
| 72 | |
| 73 | // Context template macros |
| 74 | registerSimple(['exampleSeparator', 'chatSeparator'], () => power_user.context.example_separator, () => true, 'Separator used between example chat blocks in text completion prompts.'); |
| 75 | registerSimple(['chatStart'], () => power_user.context.chat_start, () => true, 'Chat start marker used in text completion prompts.'); |
| 76 | } |