Skip to content

Commit

Permalink
fix: creating db
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-balfe committed Nov 12, 2024
1 parent 13a7202 commit 68a457c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ai-console-agent",
"version": "0.2.5",
"version": "0.2.8",
"engines": {
"bun": "1.1.34"
},
Expand Down
89 changes: 44 additions & 45 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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
Expand All @@ -146,13 +145,13 @@ export function saveConfig(appConfig: Partial<AppConfig>, userPrefs: Partial<Use
writeConfigFile(APP_CONFIG_FILE_PATH, newAppConfig);
writeConfigFile(USER_PREFS_FILE_PATH, newUserPrefs);

if (commitChanges("Update config and preferences")) {
return true;
} else {
console.warn("Failed to commit changes. Rolling back...");
revertLastChange();
return false;
}
// if (commitChanges("Update config and preferences")) {
return true;
// } else {
// console.warn("Failed to commit changes. Rolling back...");
// revertLastChange();
// return false;
// }
} catch (error) {
console.error("Error saving config:", error);
return false;
Expand Down
41 changes: 30 additions & 11 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Database } from "bun:sqlite";
import { existsSync } from "node:fs";
import { mkdir } from "node:fs/promises";
import path from "path";
import { MessageRole } from "../constants";
import { AgentMessage, ToolCall } from "./interface";
Expand All @@ -23,9 +25,20 @@ export interface AgentStep {
const LATEST_DB_VERSION = 6;

export async function initializeDatabase(): Promise<Database> {
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
);
Expand Down Expand Up @@ -70,18 +83,24 @@ export async function initializeDatabase(): Promise<Database> {
);
`);

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 {
Expand Down

0 comments on commit 68a457c

Please sign in to comment.