Merge pull request #3976 from janhq/chore/should-keep-model-running-on-message-error

chore: retrieves the exact model running status upon message error
This commit is contained in:
Louis 2024-11-08 16:42:26 +07:00 committed by GitHub
commit 1dc2b4d9ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 5 deletions

View File

@ -15,7 +15,13 @@ 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, optionType?: OptionType): 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>
abstract isModelLoaded(model: string): Promise<boolean>
} }

View File

@ -9,7 +9,12 @@ 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, option?: 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>
@ -141,6 +146,17 @@ export class CortexAPI implements ICortexAPI {
) )
} }
/**
* Check model status
* @param model
*/
async getModelStatus(model: string): Promise<boolean> {
return this.queue
.add(() => ky.get(`${API_URL}/models/status/${model}`))
.then((e) => true)
.catch(() => false)
}
/** /**
* Do health check on cortex.cpp * Do health check on cortex.cpp
* @returns * @returns
@ -215,7 +231,7 @@ export class CortexAPI implements ICortexAPI {
} }
model.metadata = model.metadata ?? { model.metadata = model.metadata ?? {
tags: [], tags: [],
size: model.size ?? model.metadata?.size ?? 0 size: model.size ?? model.metadata?.size ?? 0,
} }
return model as Model return model as Model
} }

View File

@ -238,6 +238,14 @@ export default class JanModelExtension extends ModelExtension {
return this.cortexAPI.importModel(model, modelPath, name, option) return this.cortexAPI.importModel(model, modelPath, name, option)
} }
/**
* Check model status
* @param model
*/
async isModelLoaded(model: string): Promise<boolean> {
return this.cortexAPI.getModelStatus(model)
}
/** /**
* Handle download state from main app * Handle download state from main app
*/ */

View File

@ -16,6 +16,7 @@ import {
EngineManager, EngineManager,
InferenceEngine, InferenceEngine,
extractInferenceParams, extractInferenceParams,
ModelExtension,
} from '@janhq/core' } from '@janhq/core'
import { useAtomValue, useSetAtom } from 'jotai' import { useAtomValue, useSetAtom } from 'jotai'
import { ulid } from 'ulidx' import { ulid } from 'ulidx'
@ -180,8 +181,16 @@ export default function EventHandler({ children }: { children: ReactNode }) {
} }
return return
} else if (message.status === MessageStatus.Error) { } else if (message.status === MessageStatus.Error) {
setActiveModel(undefined) ;(async () => {
setStateModel({ state: 'start', loading: false, model: undefined }) if (
!(await extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.isModelLoaded(activeModelRef.current?.id as string))
) {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
}
})()
} }
// Mark the thread as not waiting for response // Mark the thread as not waiting for response
updateThreadWaiting(message.thread_id, false) updateThreadWaiting(message.thread_id, false)

View File

@ -35,6 +35,10 @@ const useModels = () => {
const localModels = (await getModels()).map((e) => ({ const localModels = (await getModels()).map((e) => ({
...e, ...e,
name: ModelManager.instance().models.get(e.id)?.name ?? e.id, name: ModelManager.instance().models.get(e.id)?.name ?? e.id,
settings:
ModelManager.instance().models.get(e.id)?.settings ?? e.settings,
parameters:
ModelManager.instance().models.get(e.id)?.parameters ?? e.parameters,
metadata: metadata:
ModelManager.instance().models.get(e.id)?.metadata ?? e.metadata, ModelManager.instance().models.get(e.id)?.metadata ?? e.metadata,
})) }))