diff --git a/package.json b/package.json index 98b2491..42c8968 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ai-console-agent", - "version": "0.2.5", + "version": "0.2.8", "engines": { "bun": "1.1.34" }, diff --git a/src/utils/config.ts b/src/utils/config.ts index 4e1e17a..a10b5e1 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -1,6 +1,5 @@ import { execSync } from "child_process"; import fs from "fs"; -import path from "path"; import { APP_CONFIG_FILE_PATH, AVAILABLE_MODELS, CONFIG_DIR_PATH, USER_PREFS_FILE_PATH } from "../constants"; import { LogLevel, LogLevelType } from "./logger"; @@ -60,43 +59,43 @@ function execCommand(command: string, cwd: string): string { } } -function initGitRepo() { - if (!fs.existsSync(path.join(CONFIG_DIR_PATH, ".git"))) { - execCommand("git init", CONFIG_DIR_PATH); - execCommand("git add APP_CONFIG_FILE_PATH", CONFIG_DIR_PATH); - execCommand("git add USER_PREFS_FILE_PATH", CONFIG_DIR_PATH); - execCommand('git commit -m "Initial commit"', CONFIG_DIR_PATH); - } -} - -function commitChanges(message: string): boolean { - try { - execCommand("git add APP_CONFIG_FILE_PATH", CONFIG_DIR_PATH); - execCommand("git add USER_PREFS_FILE_PATH", CONFIG_DIR_PATH); - execCommand(`git commit -m "${message}"`, CONFIG_DIR_PATH); - return true; - } catch (error) { - console.error("Failed to commit changes:", error); - return false; - } -} - -function getLastCommitHash(): string { - return execCommand("git rev-parse HEAD", CONFIG_DIR_PATH).trim(); -} +// function initGitRepo() { +// if (!fs.existsSync(path.join(CONFIG_DIR_PATH, ".git"))) { +// execCommand("git init", CONFIG_DIR_PATH); +// execCommand("git add APP_CONFIG_FILE_PATH", CONFIG_DIR_PATH); +// execCommand("git add USER_PREFS_FILE_PATH", CONFIG_DIR_PATH); +// execCommand('git commit -m "Initial commit"', CONFIG_DIR_PATH); +// } +// } + +// function commitChanges(message: string): boolean { +// try { +// execCommand("git add APP_CONFIG_FILE_PATH", CONFIG_DIR_PATH); +// execCommand("git add USER_PREFS_FILE_PATH", CONFIG_DIR_PATH); +// execCommand(`git commit -m "${message}"`, CONFIG_DIR_PATH); +// return true; +// } catch (error) { +// console.error("Failed to commit changes:", error); +// return false; +// } +// } + +// function getLastCommitHash(): string { +// return execCommand("git rev-parse HEAD", CONFIG_DIR_PATH).trim(); +// } + +// export function revertLastChange(): boolean { +// try { +// const lastCommitHash = getLastCommitHash(); +// execCommand(`git revert --no-commit ${lastCommitHash}`, CONFIG_DIR_PATH); +// execCommand('git commit -m "Revert last change due to config issue"', CONFIG_DIR_PATH); +// return true; +// } catch (error) { +// console.error("Failed to revert last change:", error); +// return false; +// } +// } -export function revertLastChange(): boolean { - try { - const lastCommitHash = getLastCommitHash(); - execCommand(`git revert --no-commit ${lastCommitHash}`, CONFIG_DIR_PATH); - execCommand('git commit -m "Revert last change due to config issue"', CONFIG_DIR_PATH); - return true; - } catch (error) { - console.error("Failed to revert last change:", error); - return false; - } -} -loadConfig export function loadConfig(): { appConfig: AppConfig; userPrefs: UserPreferences } { if (!fs.existsSync(CONFIG_DIR_PATH)) { fs.mkdirSync(CONFIG_DIR_PATH, { recursive: true }); @@ -128,7 +127,7 @@ export function loadConfig(): { appConfig: AppConfig; userPrefs: UserPreferences writeConfigFile(USER_PREFS_FILE_PATH, DEFAULT_USER_PREFS); } - initGitRepo(); + // initGitRepo(); } catch (error) { console.error("Error loading config:", error); // If there's an error, use default configs @@ -146,13 +145,13 @@ export function saveConfig(appConfig: Partial, userPrefs: Partial { - const db = new Database(DB_PATH, { create: true }); + const dbDir = path.dirname(DB_PATH); + if (!existsSync(dbDir)) { + try { + await mkdir(dbDir, { recursive: true }); + } catch (error) { + logger.error(`Failed to create database directory: ${error}`); + throw error; + } + } + + try { + const db = new Database(DB_PATH, { create: true }); - db.exec(` + db.exec(` CREATE TABLE IF NOT EXISTS db_version ( version INTEGER DEFAULT ${LATEST_DB_VERSION} PRIMARY KEY ); @@ -70,18 +83,24 @@ export async function initializeDatabase(): Promise { ); `); - const currentVersion = db.query("SELECT version FROM db_version").get() as { version: number } | undefined; + const currentVersion = db.query("SELECT version FROM db_version").get() as + | { version: number } + | undefined; - if (currentVersion) { - logger.debug(`Current database version: ${currentVersion.version}`); - } else { - logger.debug("No database version found"); - } + if (currentVersion) { + logger.debug(`Current database version: ${currentVersion.version}`); + } else { + logger.debug("No database version found"); + } - // await migrateDatabase(db); + // await migrateDatabase(db); - logger.debug(`Database initialized successfully at ${DB_PATH}`); - return db; + logger.debug(`Database initialized successfully at ${DB_PATH}`); + return db; + } catch (error) { + logger.error(`Failed to initialize database: ${error}`); + throw error; + } } export interface Conversation extends ConversationMetadata {