Skip to content

Commit

Permalink
fix: better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed May 10, 2024
1 parent fd86b5d commit bce9a3f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 132 deletions.
126 changes: 0 additions & 126 deletions server/.eslintrc.json

This file was deleted.

3 changes: 3 additions & 0 deletions server/src/errors/CatAIError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class CatAIError extends Error {

}
2 changes: 2 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import FetchModels from './manage-models/about-models/fetch-models.js';
import createChat, {getModelPath} from './manage-models/bind-class/bind-class.js';
import CatAIDB from './storage/app-db.js';
import ENV_CONFIG from './storage/config.js';
import {CatAIError} from './errors/CatAIError.js';

const downloadModel = FetchModels.simpleDownload;

export {
CatAIError,
RemoteCatAI,
FetchModels,
createChat,
Expand Down
21 changes: 15 additions & 6 deletions server/src/manage-models/bind-class/bind-class.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import BaseBindClass, {CreateChatOptions} from './binds/base-bind-class.js';
import AppDb, {ModelSettings} from '../../storage/app-db.js';
import NodeLlamaCppV2 from './binds/node-llama-cpp/node-llama-cpp-v2/node-llama-cpp-v2.js';
import {withLock} from "lifecycle-utils";
import {withLock} from 'lifecycle-utils';
import {ModelNotInstalledError} from './errors/ModelNotInstalledError.js';
import {NoActiveModelError} from './errors/NoActiveModelError.js';
import {NoModelBindError} from './errors/NoModelBindError.js';
import {BindNotFoundError} from './errors/BindNotFoundError.js';

export const ALL_BINDS = [NodeLlamaCppV2];
const cachedBinds: { [key: string]: InstanceType<typeof BaseBindClass> } = {};

export function findLocalModel(modelName?: string) {
const modelDetails = AppDb.db.models[modelName || AppDb.db.activeModel!];

if (!modelDetails)
throw new Error('No active model');
if (!modelDetails) {
if (modelName) {
throw new ModelNotInstalledError(`Model ${modelName} not installed`);
}
throw new NoActiveModelError('No active model');
}


if (!modelDetails.settings.bind)
throw new Error('No bind class');
throw new NoModelBindError('No bind class');

return modelDetails;
}
Expand All @@ -40,7 +49,7 @@ export default async function createChat(options?: CreateChatOptions) {

const bindClass = ALL_BINDS.find(x => x.shortName === bind);
if (!bindClass)
throw new Error(`Bind class "${bind}" not found. Try to update the model/CatAI`);
throw new BindNotFoundError(`Bind class "${bind}" not found. Try to update the model/CatAI`);

const bindClassInstance = cachedBinds[bind] ??= new bindClass(modelDetails);
await bindClassInstance.initialize();
Expand All @@ -50,4 +59,4 @@ export default async function createChat(options?: CreateChatOptions) {

export function getModelPath(name: string) {
return findLocalModel(name).downloadedFiles?.model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {CatAIError} from '../../../errors/CatAIError.js';

export class BindNotFoundError extends CatAIError {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {CatAIError} from '../../../errors/CatAIError.js';

export class ModelNotInstalledError extends CatAIError {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {CatAIError} from '../../../errors/CatAIError.js';

export class NoActiveModelError extends CatAIError {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {CatAIError} from '../../../errors/CatAIError.js';

export class NoModelBindError extends CatAIError {

}

0 comments on commit bce9a3f

Please sign in to comment.