From 46d5faf59fbe159f4a9d1648b53d44b88e9c3c84 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 4 Nov 2024 20:36:04 +0700 Subject: [PATCH] chore: new cortex-cpp binary - model import option and model size --- core/src/browser/extensions/model.ts | 2 +- core/src/types/model/modelImport.ts | 2 +- core/src/types/model/modelInterface.ts | 8 +++++++- .../inference-cortex-extension/bin/version.txt | 2 +- extensions/model-extension/src/cortex.ts | 14 +++++++++++--- extensions/model-extension/src/index.ts | 6 ++++-- web/hooks/useImportModel.ts | 8 ++++++-- web/hooks/useModels.ts | 16 ++++++++++++++-- .../Settings/ImportModelOptionModal/index.tsx | 4 ++-- 9 files changed, 47 insertions(+), 15 deletions(-) diff --git a/core/src/browser/extensions/model.ts b/core/src/browser/extensions/model.ts index b237fad9d..1fb94fba3 100644 --- a/core/src/browser/extensions/model.ts +++ b/core/src/browser/extensions/model.ts @@ -15,7 +15,7 @@ export abstract class ModelExtension extends BaseExtension implements ModelInter abstract getModels(): Promise abstract pullModel(model: string, id?: string, name?: string): Promise abstract cancelModelPull(modelId: string): Promise - abstract importModel(model: string, modePath: string, name?: string): Promise + abstract importModel(model: string, modePath: string, name?: string, optionType?: OptionType): Promise abstract updateModel(modelInfo: Partial): Promise abstract deleteModel(model: string): Promise } diff --git a/core/src/types/model/modelImport.ts b/core/src/types/model/modelImport.ts index 7c72a691b..3f0ddab10 100644 --- a/core/src/types/model/modelImport.ts +++ b/core/src/types/model/modelImport.ts @@ -1,4 +1,4 @@ -export type OptionType = 'SYMLINK' | 'MOVE_BINARY_FILE' +export type OptionType = 'symlink' | 'copy' export type ModelImportOption = { type: OptionType diff --git a/core/src/types/model/modelInterface.ts b/core/src/types/model/modelInterface.ts index c35bae9ce..7ad1b136c 100644 --- a/core/src/types/model/modelInterface.ts +++ b/core/src/types/model/modelInterface.ts @@ -1,4 +1,5 @@ import { Model } from './modelEntity' +import { OptionType } from './modelImport' /** * Model extension for managing models. @@ -43,5 +44,10 @@ export interface ModelInterface { * @param model id of the model to import * @param modelPath - path of the model file */ - importModel(model: string, modePath: string, name?: string): Promise + importModel( + model: string, + modePath: string, + name?: string, + optionType?: OptionType + ): Promise } diff --git a/extensions/inference-cortex-extension/bin/version.txt b/extensions/inference-cortex-extension/bin/version.txt index 57d77db55..c89636bcf 100644 --- a/extensions/inference-cortex-extension/bin/version.txt +++ b/extensions/inference-cortex-extension/bin/version.txt @@ -1 +1 @@ -1.0.2-rc2 \ No newline at end of file +1.0.2-rc4 \ No newline at end of file diff --git a/extensions/model-extension/src/cortex.ts b/extensions/model-extension/src/cortex.ts index 50eace5e5..024aa2223 100644 --- a/extensions/model-extension/src/cortex.ts +++ b/extensions/model-extension/src/cortex.ts @@ -9,7 +9,7 @@ interface ICortexAPI { getModel(model: string): Promise getModels(): Promise pullModel(model: string, id?: string, name?: string): Promise - importModel(path: string, modelPath: string, name?: string): Promise + importModel(path: string, modelPath: string, name?: string, option?: string): Promise deleteModel(model: string): Promise updateModel(model: object): Promise cancelModelPull(model: string): Promise @@ -85,10 +85,17 @@ export class CortexAPI implements ICortexAPI { * @param model * @returns */ - importModel(model: string, modelPath: string, name?: string): Promise { + importModel( + model: string, + modelPath: string, + name?: string, + option?: string + ): Promise { return this.queue.add(() => ky - .post(`${API_URL}/v1/models/import`, { json: { model, modelPath, name } }) + .post(`${API_URL}/v1/models/import`, { + json: { model, modelPath, name, option }, + }) .json() .catch((e) => console.debug(e)) // Ignore error .then() @@ -208,6 +215,7 @@ export class CortexAPI implements ICortexAPI { } model.metadata = model.metadata ?? { tags: [], + size: model.size ?? model.metadata?.size ?? 0 } return model as Model } diff --git a/extensions/model-extension/src/index.ts b/extensions/model-extension/src/index.ts index 17c00263d..e62e5b2ee 100644 --- a/extensions/model-extension/src/index.ts +++ b/extensions/model-extension/src/index.ts @@ -9,6 +9,7 @@ import { DownloadState, events, DownloadEvent, + OptionType } from '@janhq/core' import { CortexAPI } from './cortex' import { scanModelsFolder } from './legacy/model-json' @@ -228,9 +229,10 @@ export default class JanModelExtension extends ModelExtension { async importModel( model: string, modelPath: string, - name?: string + name?: string, + option?: OptionType ): Promise { - return this.cortexAPI.importModel(model, modelPath, name) + return this.cortexAPI.importModel(model, modelPath, name, option) } /** diff --git a/web/hooks/useImportModel.ts b/web/hooks/useImportModel.ts index d84690715..093385f0d 100644 --- a/web/hooks/useImportModel.ts +++ b/web/hooks/useImportModel.ts @@ -3,6 +3,7 @@ import { useCallback } from 'react' import { ExtensionTypeEnum, ImportingModel, + LocalImportModelEvent, Model, ModelEvent, ModelExtension, @@ -66,10 +67,13 @@ const useImportModel = () => { addDownloadingModel(modelId) extensionManager .get(ExtensionTypeEnum.Model) - ?.importModel(modelId, model.path, model.name) + ?.importModel(modelId, model.path, model.name, optionType) .finally(() => { removeDownloadingModel(modelId) - events.emit(ModelEvent.OnModelsUpdate, {}) + events.emit(LocalImportModelEvent.onLocalImportModelSuccess, { + importId: model.importId, + modelId: modelId, + }) }) } }) diff --git a/web/hooks/useModels.ts b/web/hooks/useModels.ts index 0c898119c..f3004f823 100644 --- a/web/hooks/useModels.ts +++ b/web/hooks/useModels.ts @@ -43,7 +43,19 @@ const useModels = () => { .models.values() .toArray() .filter((e) => !isLocalEngine(e.engine)) - setDownloadedModels([...localModels, ...remoteModels]) + const toUpdate = [...localModels, ...remoteModels] + setDownloadedModels(toUpdate) + + let isUpdated = false + toUpdate.forEach((model) => { + if (!ModelManager.instance().models.has(model.id)) { + ModelManager.instance().models.set(model.id, model) + isUpdated = true + } + }) + if (isUpdated) { + getExtensionModels() + } } const getExtensionModels = async () => { @@ -52,7 +64,7 @@ const useModels = () => { } // Fetch all data - Promise.all([getDownloadedModels(), getExtensionModels()]) + getExtensionModels().then(getDownloadedModels) }, [setDownloadedModels, setExtensionModels]) const reloadData = useDebouncedCallback(() => getData(), 300) diff --git a/web/screens/Settings/ImportModelOptionModal/index.tsx b/web/screens/Settings/ImportModelOptionModal/index.tsx index 5a2af2335..f185b9015 100644 --- a/web/screens/Settings/ImportModelOptionModal/index.tsx +++ b/web/screens/Settings/ImportModelOptionModal/index.tsx @@ -15,13 +15,13 @@ import { importingModelsAtom } from '@/helpers/atoms/Model.atom' const importOptions: ModelImportOption[] = [ { - type: 'SYMLINK', + type: 'symlink', title: 'Keep Original Files & Symlink', description: 'You maintain your model files outside of Jan. Keeping your files where they are, and Jan will create a smart link to them.', }, { - type: 'MOVE_BINARY_FILE', + type: 'copy', title: 'Move model binary file', description: 'Jan will move your model binary file from your current folder into Jan Data Folder.',