context-optimizer-mcp-server
Version:
Context optimization tools MCP server for AI coding assistants - compatible with GitHub Copilot, Cursor AI, and other MCP-supporting assistants
104 lines • 4.13 kB
JavaScript
/**
* Session management for follow-up questions
*
* Manages file-based session storage for terminal execution history
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionManager = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const manager_1 = require("../config/manager");
const logger_1 = require("../utils/logger");
class SessionManager {
static getSessionFilePath() {
const config = manager_1.ConfigurationManager.getConfig();
const sessionDir = config.server.sessionStoragePath || path.join(os.tmpdir(), 'context-optimizer-mcp');
return path.join(sessionDir, 'terminal-session.json');
}
static async saveTerminalSession(data) {
try {
const sessionPath = this.getSessionFilePath();
const sessionDir = path.dirname(sessionPath);
// Ensure session directory exists
await fs.mkdir(sessionDir, { recursive: true });
// Save session data
await fs.writeFile(sessionPath, JSON.stringify(data, null, 2), 'utf8');
logger_1.Logger.debug(`Terminal session saved: ${sessionPath}`);
}
catch (error) {
logger_1.Logger.error('Failed to save terminal session:', error);
// Non-critical error - don't throw
}
}
static async loadTerminalSession() {
try {
const sessionPath = this.getSessionFilePath();
const content = await fs.readFile(sessionPath, 'utf8');
const data = JSON.parse(content);
// Check if session has expired
const config = manager_1.ConfigurationManager.getConfig();
const sessionAge = Date.now() - new Date(data.timestamp).getTime();
if (sessionAge > config.security.sessionTimeout) {
logger_1.Logger.debug('Terminal session expired, removing...');
await this.clearTerminalSession();
return null;
}
return data;
}
catch (error) {
// Session file doesn't exist or is invalid
return null;
}
}
static async clearTerminalSession() {
try {
const sessionPath = this.getSessionFilePath();
await fs.unlink(sessionPath);
logger_1.Logger.debug('Terminal session cleared');
}
catch (error) {
// File doesn't exist - that's fine
}
}
static async hasActiveSession() {
const session = await this.loadTerminalSession();
return session !== null;
}
}
exports.SessionManager = SessionManager;
//# sourceMappingURL=manager.js.map
;