@z-test/memory-bank-mcp
Version:
MCP Server for managing Memory Bank
163 lines • 5.63 kB
JavaScript
import { EventEmitter } from 'events';
import { ExternalRulesLoader } from './ExternalRulesLoader.js';
/**
* Events emitted by ModeManager
*/
export var ModeManagerEvent;
(function (ModeManagerEvent) {
ModeManagerEvent["MODE_CHANGED"] = "modeChanged";
ModeManagerEvent["MODE_TRIGGER_DETECTED"] = "modeTriggerDetected";
ModeManagerEvent["UMB_TRIGGERED"] = "umbTriggered";
ModeManagerEvent["UMB_COMPLETED"] = "umbCompleted";
})(ModeManagerEvent || (ModeManagerEvent = {}));
/**
* Class responsible for managing modes based on loaded rules
*/
export class ModeManager extends EventEmitter {
constructor(config) {
super();
this.currentMode = 'code'; // Default mode
this.isUmbActive = false;
this.memoryBankStatus = 'INACTIVE';
const projectPath = typeof config === 'string' ? config : process.cwd();
this.rulesLoader = new ExternalRulesLoader(projectPath);
// Set up listener for rule changes
this.rulesLoader.on('ruleChanged', (mode, rule) => {
if (mode === this.currentMode) {
this.emit(ModeManagerEvent.MODE_CHANGED, this.getCurrentModeState());
}
});
}
/**
* Initializes the mode manager
* @param initialMode Initial mode (optional)
*/
async initialize(initialMode) {
await this.rulesLoader.detectAndLoadRules();
if (initialMode && this.rulesLoader.hasModeRules(initialMode)) {
this.currentMode = initialMode;
}
else {
// Use the first available mode or keep the default
const availableModes = this.rulesLoader.getAvailableModes();
if (availableModes.length > 0) {
this.currentMode = availableModes[0];
}
}
this.emit(ModeManagerEvent.MODE_CHANGED, this.getCurrentModeState());
}
/**
* Gets the current mode state
* @returns Current mode state
*/
getCurrentModeState() {
return {
name: this.currentMode,
rules: this.rulesLoader.getRulesForMode(this.currentMode),
isUmbActive: this.isUmbActive,
memoryBankStatus: this.memoryBankStatus,
};
}
/**
* Switches to a specific mode
* @param mode Mode name
* @returns true if the switch was successful, false otherwise
*/
switchMode(mode) {
if (!this.rulesLoader.hasModeRules(mode)) {
return false;
}
this.currentMode = mode;
this.emit(ModeManagerEvent.MODE_CHANGED, this.getCurrentModeState());
return true;
}
/**
* Checks if a text matches the UMB trigger
* @param text Text to check
* @returns true if the text matches the UMB trigger, false otherwise
*/
checkUmbTrigger(text) {
const currentRules = this.rulesLoader.getRulesForMode(this.currentMode);
if (!currentRules || !currentRules.instructions.umb || !currentRules.instructions.umb.trigger) {
return false;
}
const triggerRegex = new RegExp(currentRules.instructions.umb.trigger, 'i');
return triggerRegex.test(text);
}
/**
* Activates the UMB mode
* @returns true if the activation was successful, false otherwise
*/
activateUmb() {
const currentRules = this.rulesLoader.getRulesForMode(this.currentMode);
if (!currentRules || !currentRules.instructions.umb) {
return false;
}
this.isUmbActive = true;
this.emit(ModeManagerEvent.UMB_TRIGGERED, this.getCurrentModeState());
return true;
}
/**
* Deactivates the UMB mode
*/
deactivateUmb() {
this.isUmbActive = false;
this.emit(ModeManagerEvent.UMB_COMPLETED, this.getCurrentModeState());
}
/**
* Checks if a text matches any mode trigger
* @param text Text to check
* @returns Array with the modes corresponding to the found triggers
*/
checkModeTriggers(text) {
const triggeredModes = [];
const currentRules = this.rulesLoader.getRulesForMode(this.currentMode);
if (!currentRules || !currentRules.mode_triggers) {
return triggeredModes;
}
// Check triggers for each mode
for (const [mode, triggers] of Object.entries(currentRules.mode_triggers)) {
if (this.rulesLoader.hasModeRules(mode)) {
for (const trigger of triggers) {
if (text.includes(trigger.condition)) {
triggeredModes.push(mode);
break; // One trigger is enough for each mode
}
}
}
}
if (triggeredModes.length > 0) {
this.emit(ModeManagerEvent.MODE_TRIGGER_DETECTED, triggeredModes);
}
return triggeredModes;
}
/**
* Sets the Memory Bank status
* @param status New status
*/
setMemoryBankStatus(status) {
this.memoryBankStatus = status;
this.emit(ModeManagerEvent.MODE_CHANGED, this.getCurrentModeState());
}
/**
* Gets the status prefix for responses
* @returns Status prefix
*/
getStatusPrefix() {
return `[MEMORY BANK: ${this.memoryBankStatus}]`;
}
/**
* Checks if the UMB mode is active
* @returns true if the UMB mode is active, false otherwise
*/
isUmbModeActive() {
return this.isUmbActive;
}
/**
* Cleans up all resources
*/
dispose() {
this.removeAllListeners();
}
}
//# sourceMappingURL=ModeManager.js.map