chore: new cortex-cpp binary - model import option and model size

This commit is contained in:
Louis 2024-11-04 20:36:04 +07:00
parent b913af9f88
commit 46d5faf59f
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
9 changed files with 47 additions and 15 deletions

View File

@ -15,7 +15,7 @@ export abstract class ModelExtension extends BaseExtension implements ModelInter
abstract getModels(): Promise<Model[]> abstract getModels(): Promise<Model[]>
abstract pullModel(model: string, id?: string, name?: string): Promise<void> abstract pullModel(model: string, id?: string, name?: string): Promise<void>
abstract cancelModelPull(modelId: string): Promise<void> abstract cancelModelPull(modelId: string): Promise<void>
abstract importModel(model: string, modePath: string, name?: string): Promise<void> abstract importModel(model: string, modePath: string, name?: string, optionType?: OptionType): Promise<void>
abstract updateModel(modelInfo: Partial<Model>): Promise<Model> abstract updateModel(modelInfo: Partial<Model>): Promise<Model>
abstract deleteModel(model: string): Promise<void> abstract deleteModel(model: string): Promise<void>
} }

View File

@ -1,4 +1,4 @@
export type OptionType = 'SYMLINK' | 'MOVE_BINARY_FILE' export type OptionType = 'symlink' | 'copy'
export type ModelImportOption = { export type ModelImportOption = {
type: OptionType type: OptionType

View File

@ -1,4 +1,5 @@
import { Model } from './modelEntity' import { Model } from './modelEntity'
import { OptionType } from './modelImport'
/** /**
* Model extension for managing models. * Model extension for managing models.
@ -43,5 +44,10 @@ export interface ModelInterface {
* @param model id of the model to import * @param model id of the model to import
* @param modelPath - path of the model file * @param modelPath - path of the model file
*/ */
importModel(model: string, modePath: string, name?: string): Promise<void> importModel(
model: string,
modePath: string,
name?: string,
optionType?: OptionType
): Promise<void>
} }

View File

@ -1 +1 @@
1.0.2-rc2 1.0.2-rc4

View File

@ -9,7 +9,7 @@ interface ICortexAPI {
getModel(model: string): Promise<Model> getModel(model: string): Promise<Model>
getModels(): Promise<Model[]> getModels(): Promise<Model[]>
pullModel(model: string, id?: string, name?: string): Promise<void> pullModel(model: string, id?: string, name?: string): Promise<void>
importModel(path: string, modelPath: string, name?: string): Promise<void> importModel(path: string, modelPath: string, name?: string, option?: string): Promise<void>
deleteModel(model: string): Promise<void> deleteModel(model: string): Promise<void>
updateModel(model: object): Promise<void> updateModel(model: object): Promise<void>
cancelModelPull(model: string): Promise<void> cancelModelPull(model: string): Promise<void>
@ -85,10 +85,17 @@ export class CortexAPI implements ICortexAPI {
* @param model * @param model
* @returns * @returns
*/ */
importModel(model: string, modelPath: string, name?: string): Promise<void> { importModel(
model: string,
modelPath: string,
name?: string,
option?: string
): Promise<void> {
return this.queue.add(() => return this.queue.add(() =>
ky ky
.post(`${API_URL}/v1/models/import`, { json: { model, modelPath, name } }) .post(`${API_URL}/v1/models/import`, {
json: { model, modelPath, name, option },
})
.json() .json()
.catch((e) => console.debug(e)) // Ignore error .catch((e) => console.debug(e)) // Ignore error
.then() .then()
@ -208,6 +215,7 @@ export class CortexAPI implements ICortexAPI {
} }
model.metadata = model.metadata ?? { model.metadata = model.metadata ?? {
tags: [], tags: [],
size: model.size ?? model.metadata?.size ?? 0
} }
return model as Model return model as Model
} }

View File

@ -9,6 +9,7 @@ import {
DownloadState, DownloadState,
events, events,
DownloadEvent, DownloadEvent,
OptionType
} from '@janhq/core' } from '@janhq/core'
import { CortexAPI } from './cortex' import { CortexAPI } from './cortex'
import { scanModelsFolder } from './legacy/model-json' import { scanModelsFolder } from './legacy/model-json'
@ -228,9 +229,10 @@ export default class JanModelExtension extends ModelExtension {
async importModel( async importModel(
model: string, model: string,
modelPath: string, modelPath: string,
name?: string name?: string,
option?: OptionType
): Promise<void> { ): Promise<void> {
return this.cortexAPI.importModel(model, modelPath, name) return this.cortexAPI.importModel(model, modelPath, name, option)
} }
/** /**

View File

@ -3,6 +3,7 @@ import { useCallback } from 'react'
import { import {
ExtensionTypeEnum, ExtensionTypeEnum,
ImportingModel, ImportingModel,
LocalImportModelEvent,
Model, Model,
ModelEvent, ModelEvent,
ModelExtension, ModelExtension,
@ -66,10 +67,13 @@ const useImportModel = () => {
addDownloadingModel(modelId) addDownloadingModel(modelId)
extensionManager extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model) .get<ModelExtension>(ExtensionTypeEnum.Model)
?.importModel(modelId, model.path, model.name) ?.importModel(modelId, model.path, model.name, optionType)
.finally(() => { .finally(() => {
removeDownloadingModel(modelId) removeDownloadingModel(modelId)
events.emit(ModelEvent.OnModelsUpdate, {}) events.emit(LocalImportModelEvent.onLocalImportModelSuccess, {
importId: model.importId,
modelId: modelId,
})
}) })
} }
}) })

View File

@ -43,7 +43,19 @@ const useModels = () => {
.models.values() .models.values()
.toArray() .toArray()
.filter((e) => !isLocalEngine(e.engine)) .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 () => { const getExtensionModels = async () => {
@ -52,7 +64,7 @@ const useModels = () => {
} }
// Fetch all data // Fetch all data
Promise.all([getDownloadedModels(), getExtensionModels()]) getExtensionModels().then(getDownloadedModels)
}, [setDownloadedModels, setExtensionModels]) }, [setDownloadedModels, setExtensionModels])
const reloadData = useDebouncedCallback(() => getData(), 300) const reloadData = useDebouncedCallback(() => getData(), 300)

View File

@ -15,13 +15,13 @@ import { importingModelsAtom } from '@/helpers/atoms/Model.atom'
const importOptions: ModelImportOption[] = [ const importOptions: ModelImportOption[] = [
{ {
type: 'SYMLINK', type: 'symlink',
title: 'Keep Original Files & Symlink', title: 'Keep Original Files & Symlink',
description: description:
'You maintain your model files outside of Jan. Keeping your files where they are, and Jan will create a smart link to them.', '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', title: 'Move model binary file',
description: description:
'Jan will move your model binary file from your current folder into Jan Data Folder.', 'Jan will move your model binary file from your current folder into Jan Data Folder.',