giga-code
Version:
A personal AI CLI assistant powered by Grok for local development.
158 lines • 6.17 kB
JavaScript
;
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.expertModelsManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
class ExpertModelsManager {
constructor() { }
static getInstance() {
if (!ExpertModelsManager.instance) {
ExpertModelsManager.instance = new ExpertModelsManager();
}
return ExpertModelsManager.instance;
}
getGigaDir() {
const homeDir = os.homedir();
const gigaDir = path.join(homeDir, '.giga');
if (!fs.existsSync(gigaDir)) {
fs.mkdirSync(gigaDir, { mode: 0o700 });
}
return gigaDir;
}
getConfigPath() {
const gigaDir = this.getGigaDir();
return path.join(gigaDir, 'expert-models.json');
}
getExpertModelsConfig() {
try {
const configPath = this.getConfigPath();
if (!fs.existsSync(configPath)) {
return {
enabled: false,
fastModel: null,
codeModel: null,
reasoningModel: null,
toolsModel: null,
};
}
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
// Validate config structure
if (typeof config.enabled !== 'boolean') {
config.enabled = false;
}
return config;
}
catch (error) {
console.error('Error loading expert models config:', error);
return {
enabled: false,
fastModel: null,
codeModel: null,
reasoningModel: null,
toolsModel: null,
};
}
}
setExpertModelsConfig(expertModels) {
try {
const configPath = this.getConfigPath();
fs.writeFileSync(configPath, JSON.stringify(expertModels, null, 2), { mode: 0o600 });
}
catch (error) {
console.error('Error saving expert models config:', error);
}
}
migrateFromSessionConfig(sessionConfigPath) {
try {
if (!fs.existsSync(sessionConfigPath)) {
return false;
}
const sessionConfig = JSON.parse(fs.readFileSync(sessionConfigPath, 'utf8'));
if (sessionConfig.expertModels) {
// Check if global config already exists
const globalConfigPath = this.getConfigPath();
if (!fs.existsSync(globalConfigPath)) {
console.log('Migrating expert models config from session to global storage...');
this.setExpertModelsConfig(sessionConfig.expertModels);
return true;
}
}
return false;
}
catch (error) {
console.error('Error migrating expert models config:', error);
return false;
}
}
migrateFromAllSessions() {
try {
const homeDir = os.homedir();
const sessionsDir = path.join(homeDir, '.giga', 'sessions');
if (!fs.existsSync(sessionsDir)) {
return;
}
// Check if global config already exists
if (fs.existsSync(this.getConfigPath())) {
return; // Don't overwrite existing global config
}
const sessionDirs = fs.readdirSync(sessionsDir);
let latestConfig = null;
let latestTimestamp = 0;
// Find the most recently used session with expert models config
for (const sessionDir of sessionDirs) {
const configPath = path.join(sessionsDir, sessionDir, 'config.json');
if (fs.existsSync(configPath)) {
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (config.expertModels && config.lastUsed) {
const timestamp = new Date(config.lastUsed).getTime();
if (timestamp > latestTimestamp) {
latestTimestamp = timestamp;
latestConfig = config.expertModels;
}
}
}
catch (error) {
// Skip invalid config files
continue;
}
}
}
// If we found a config, migrate it
if (latestConfig) {
console.log('Migrating expert models config from most recent session to global storage...');
this.setExpertModelsConfig(latestConfig);
}
}
catch (error) {
console.error('Error migrating expert models from sessions:', error);
}
}
}
// Export singleton instance
exports.expertModelsManager = ExpertModelsManager.getInstance();
//# sourceMappingURL=expert-models-manager.js.map