UNPKG

giga-code

Version:

A personal AI CLI assistant powered by Grok for local development.

202 lines 7.67 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sessionManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); const crypto_1 = require("crypto"); class SessionManager { constructor() { this.instanceId = (0, crypto_1.randomUUID)(); this.sessionDir = this.getSessionDir(); this.ensureSessionDir(); this.saveSessionConfig(); } getGigaDir() { const homeDir = os.homedir(); const gigaDir = path.join(homeDir, '.giga'); if (!fs.existsSync(gigaDir)) { fs.mkdirSync(gigaDir, { mode: 0o700 }); } return gigaDir; } getSessionDir() { const gigaDir = this.getGigaDir(); return path.join(gigaDir, 'sessions', this.instanceId); } ensureSessionDir() { if (!fs.existsSync(this.sessionDir)) { fs.mkdirSync(this.sessionDir, { recursive: true, mode: 0o700 }); } } getSessionConfigPath() { return path.join(this.sessionDir, 'config.json'); } saveSessionConfig() { try { const config = { instanceId: this.instanceId, currentModel: 'moonshotai/kimi-k2-instruct', createdAt: new Date().toISOString(), lastUsed: new Date().toISOString() }; fs.writeFileSync(this.getSessionConfigPath(), JSON.stringify(config, null, 2), { mode: 0o600 }); } catch (error) { console.error('Error saving session config:', error); } } getInstanceId() { return this.instanceId; } getCurrentModel() { try { const configPath = this.getSessionConfigPath(); if (!fs.existsSync(configPath)) { return 'moonshotai/kimi-k2-instruct'; // default fallback } const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); return config.currentModel || 'moonshotai/kimi-k2-instruct'; } catch (error) { console.error('Error loading current model:', error); return 'moonshotai/kimi-k2-instruct'; } } setCurrentModel(modelName) { try { const configPath = this.getSessionConfigPath(); let config; if (fs.existsSync(configPath)) { config = JSON.parse(fs.readFileSync(configPath, 'utf8')); } else { config = { instanceId: this.instanceId, currentModel: modelName, createdAt: new Date().toISOString(), lastUsed: new Date().toISOString() }; } config.currentModel = modelName; config.lastUsed = new Date().toISOString(); fs.writeFileSync(configPath, JSON.stringify(config, null, 2), { mode: 0o600 }); } catch (error) { console.error('Error saving current model:', error); } } getTemperature() { try { const configPath = this.getSessionConfigPath(); if (!fs.existsSync(configPath)) { return 0.7; // default temperature } const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); return config.temperature ?? 0.7; } catch (error) { console.error('Error loading temperature:', error); return 0.7; } } setTemperature(temperature) { try { const configPath = this.getSessionConfigPath(); let config; if (fs.existsSync(configPath)) { config = JSON.parse(fs.readFileSync(configPath, 'utf8')); } else { config = { instanceId: this.instanceId, currentModel: 'moonshotai/kimi-k2-instruct', temperature: temperature, createdAt: new Date().toISOString(), lastUsed: new Date().toISOString() }; } config.temperature = temperature; config.lastUsed = new Date().toISOString(); fs.writeFileSync(configPath, JSON.stringify(config, null, 2), { mode: 0o600 }); } catch (error) { console.error('Error saving temperature:', error); } } getSessionInfo() { try { const configPath = this.getSessionConfigPath(); if (!fs.existsSync(configPath)) { return null; } return JSON.parse(fs.readFileSync(configPath, 'utf8')); } catch (error) { console.error('Error loading session info:', error); return null; } } // Cleanup old sessions (optional - called periodically) static cleanupOldSessions(olderThanDays = 7) { try { const homeDir = os.homedir(); const sessionsDir = path.join(homeDir, '.giga', 'sessions'); if (!fs.existsSync(sessionsDir)) { return; } const cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() - olderThanDays); const sessionDirs = fs.readdirSync(sessionsDir); for (const sessionDir of sessionDirs) { const sessionPath = path.join(sessionsDir, sessionDir); const configPath = path.join(sessionPath, 'config.json'); if (fs.existsSync(configPath)) { try { const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); const lastUsed = new Date(config.lastUsed); if (lastUsed < cutoffDate) { fs.rmSync(sessionPath, { recursive: true, force: true }); } } catch (error) { // If we can't read the config, consider it for cleanup const stats = fs.statSync(sessionPath); if (stats.mtime < cutoffDate) { fs.rmSync(sessionPath, { recursive: true, force: true }); } } } } } catch (error) { console.error('Error cleaning up old sessions:', error); } } } // Export singleton instance exports.sessionManager = new SessionManager(); //# sourceMappingURL=session-manager.js.map