| 1 | import { PermissionMode } from "../permissions/types.js"; |
| 2 | import { constructSystemMessage } from "../systemMessage.js"; |
| 3 | import { logger } from "../util/logger.js"; |
| 4 | |
| 5 | import { BaseService } from "./BaseService.js"; |
| 6 | import { modeService } from "./ModeService.js"; |
| 7 | |
| 8 | export interface SystemMessageServiceState { |
| 9 | additionalRules?: string[]; |
| 10 | format?: "json"; |
| 11 | headless?: boolean; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Service for managing dynamic system message construction |
| 16 | * Provides fresh system messages that reflect current mode and configuration |
| 17 | */ |
| 18 | export class SystemMessageService extends BaseService<SystemMessageServiceState> { |
| 19 | private currentMode: PermissionMode = "normal"; |
| 20 | |
| 21 | constructor() { |
| 22 | super("SystemMessageService", {}); |
| 23 | |
| 24 | // Listen to mode changes from ModeService |
| 25 | this.setupModeListener(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Set up listener for mode changes |
| 30 | */ |
| 31 | private setupModeListener(): void { |
| 32 | modeService.on("modeChanged", (newMode: PermissionMode) => { |
| 33 | logger.debug(`SystemMessageService: Mode changed to ${newMode}`); |
| 34 | this.currentMode = newMode; |
| 35 | }); |
| 36 | |
| 37 | // Initialize current mode |
| 38 | this.currentMode = modeService.getCurrentMode(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Initialize the service |
| 43 | */ |
| 44 | async doInitialize(args: { |
| 45 | additionalRules?: string[]; |
| 46 | format?: "json"; |
| 47 | headless?: boolean; |
| 48 | }): Promise<SystemMessageServiceState> { |
| 49 | this.setState({ |
| 50 | additionalRules: args.additionalRules, |
| 51 | format: args.format, |
| 52 | headless: args.headless, |
| 53 | }); |
| 54 | |
| 55 | logger.debug("SystemMessageService initialized", { |
| 56 | hasAdditionalRules: !!args.additionalRules?.length, |
| 57 | format: args.format, |
| 58 | headless: args.headless, |
| 59 | }); |
| 60 | |
| 61 | return this.currentState; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get a fresh system message with current mode and configuration |
| 66 | */ |
| 67 | public async getSystemMessage(): Promise<string> { |
| 68 | const { additionalRules, format, headless } = this.currentState; |
| 69 | |
| 70 | const systemMessage = await constructSystemMessage( |
| 71 | additionalRules, |
| 72 | format, |
| 73 | headless, |
| 74 | this.currentMode, |
| 75 | ); |
| 76 | |
| 77 | logger.debug("Generated fresh system message", { |
| 78 | mode: this.currentMode, |
| 79 | messageLength: systemMessage.length, |
| 80 | }); |
| 81 | |
| 82 | return systemMessage; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Update configuration that affects system message |
| 87 | */ |
| 88 | public updateConfig(config: Partial<SystemMessageServiceState>): void { |
| 89 | this.setState({ |
| 90 | ...this.currentState, |
| 91 | ...config, |
| 92 | }); |
| 93 | |
| 94 | logger.debug("SystemMessageService config updated", config); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the current mode being used for system messages |
| 99 | */ |
| 100 | public getCurrentMode(): PermissionMode { |
| 101 | return this.currentMode; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Export singleton instance |
| 106 | export const systemMessageService = new SystemMessageService(); |