Skip to content

Commit

Permalink
feat(backend): refactor model downloading and configuration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sma1lboy committed Dec 28, 2024
1 parent 97cae53 commit e579146
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 324 deletions.
19 changes: 13 additions & 6 deletions backend/src/config/common-path.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from 'path';
import { existsSync, mkdirSync, promises } from 'fs-extra';
import { existsSync, mkdirSync, promises, writeFileSync } from 'fs-extra';

// Constants for base directories
const APP_NAME = 'codefox';
// TODO: hack way to get the root directory of the workspace
const WORKSPACE_ROOT = path.resolve(__dirname, '../../../');
const WORKSPACE_ROOT = path.resolve(__dirname, '../../../../');
const ROOT_DIR = path.join(WORKSPACE_ROOT, `.${APP_NAME}`);

export const TEMPLATE_PATH = path.join(WORKSPACE_ROOT, 'backend/template');
Expand All @@ -27,10 +27,17 @@ const ensureDir = (dirPath: string): string => {
export const getRootDir = (): string => ensureDir(ROOT_DIR);

// Configuration Paths
export const getConfigDir = (): string =>
ensureDir(path.join(getRootDir(), 'config'));
export const getConfigPath = (configName: string): string =>
path.join(getConfigDir(), `${configName}.json`);
export const getConfigPath = (): string => {
const rootPath = ensureDir(path.join(getRootDir()));
return path.join(rootPath, 'config.json');
};

export const getModelStatusPath = (): string => {
const rootPath = ensureDir(getRootDir());
const modelStatusPath = path.join(rootPath, 'model-status.json');
writeFileSync(modelStatusPath, '{}');
return modelStatusPath;
};

// Models Directory
export const getModelsDir = (): string =>
Expand Down
237 changes: 0 additions & 237 deletions backend/src/config/config-loader.spec.ts

This file was deleted.

37 changes: 24 additions & 13 deletions backend/src/config/config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// config-loader.ts
import * as fs from 'fs';
import * as path from 'path';
import * as _ from 'lodash';
import { getConfigPath } from './common-path';
import { Logger } from '@nestjs/common';

export interface ChatConfig {
model: string;
Expand Down Expand Up @@ -56,29 +56,32 @@ export class ConfigLoader {
private config: AppConfig;
private readonly configPath: string;

private constructor(configPath?: string) {
this.configPath = configPath || getConfigPath('config');
private constructor() {
this.configPath = getConfigPath();
this.initConfigFile();
this.loadConfig();
}

public static getInstance(configPath?: string): ConfigLoader {
public static getInstance(): ConfigLoader {
if (!ConfigLoader.instance) {
ConfigLoader.instance = new ConfigLoader(configPath);
ConfigLoader.instance = new ConfigLoader();
}
return ConfigLoader.instance;
}

public static initConfigFile(configPath: string): void {
if (fs.existsSync(configPath)) {
throw new Error('Config file already exists');
}
public initConfigFile(): void {
Logger.log('Creating example config file', 'ConfigLoader');

const configDir = path.dirname(configPath);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
const config = getConfigPath();
if (fs.existsSync(config)) {
return;
}

fs.writeFileSync(configPath, exampleConfigContent, 'utf-8');
if (!fs.existsSync(config)) {
//make file
fs.writeFileSync(config, exampleConfigContent, 'utf-8');
}
Logger.log('Creating example config file', 'ConfigLoader');
}

public reload(): void {
Expand All @@ -87,6 +90,10 @@ export class ConfigLoader {

private loadConfig() {
try {
Logger.log(
`Loading configuration from ${this.configPath}`,
'ConfigLoader',
);
const file = fs.readFileSync(this.configPath, 'utf-8');
const jsonContent = file.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
Expand Down Expand Up @@ -234,4 +241,8 @@ export class ConfigLoader {
}
}
}

getConfig(): AppConfig {
return this.config;
}
}
4 changes: 2 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import 'reflect-metadata';
import { downloadAllModels } from './model/utils';
import { checkAndDownloadAllModels } from './model/utils';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand All @@ -17,7 +17,7 @@ async function bootstrap() {
'Access-Control-Allow-Credentials',
],
});
await downloadAllModels();
await checkAndDownloadAllModels();
await app.listen(process.env.PORT ?? 3000);
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/model/__tests__/loadAllChatsModels.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import * as fs from 'fs';
import { ConfigLoader } from '../../config/config-loader';
import { ModelDownloader } from '../model-downloader';
import { ModelDownloader } from '../downloader/downloader';
import { downloadAllModels } from '../utils';
import { getConfigDir, getConfigPath } from 'src/config/common-path';
import { getConfigPath, getConfigPath } from 'src/config/common-path';

const originalIsArray = Array.isArray;

Expand Down
Loading

0 comments on commit e579146

Please sign in to comment.