From 7656048e64f1ddac56caca1f63e0bf4c5089df35 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 6 Dec 2023 11:18:05 +0700 Subject: [PATCH] fix: can not download multiple models at once (#867) --- web/containers/Providers/EventListener.tsx | 4 +++- web/helpers/atoms/Model.atom.ts | 20 ++++++++++++++++++++ web/hooks/useDownloadModel.ts | 11 +++++------ web/hooks/useDownloadState.ts | 2 +- web/hooks/useSendChatMessage.ts | 9 ++++++++- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/web/containers/Providers/EventListener.tsx b/web/containers/Providers/EventListener.tsx index 686368e36b..d73e5732de 100644 --- a/web/containers/Providers/EventListener.tsx +++ b/web/containers/Providers/EventListener.tsx @@ -57,9 +57,11 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) { (_event: string, callback: any) => { if (callback && callback.fileName) { const modelId = callback.fileName.split('/').pop() ?? '' - setDownloadStateSuccess(modelId) const model = modelsRef.current.find((e) => e.id === modelId) + + setDownloadStateSuccess(modelId) + if (model) extensionManager .get(ExtensionType.Model) diff --git a/web/helpers/atoms/Model.atom.ts b/web/helpers/atoms/Model.atom.ts index 6b142221da..6eb7f2ad68 100644 --- a/web/helpers/atoms/Model.atom.ts +++ b/web/helpers/atoms/Model.atom.ts @@ -3,4 +3,24 @@ import { atom } from 'jotai' export const stateModel = atom({ state: 'start', loading: false, model: '' }) export const activeAssistantModelAtom = atom(undefined) + export const downloadingModelsAtom = atom([]) + +export const addNewDownloadingModelAtom = atom( + null, + (get, set, model: Model) => { + const currentModels = get(downloadingModelsAtom) + set(downloadingModelsAtom, [...currentModels, model]) + } +) + +export const removeDownloadingModelAtom = atom( + null, + (get, set, modelId: string) => { + const currentModels = get(downloadingModelsAtom) + set( + downloadingModelsAtom, + currentModels.filter((e) => e.id !== modelId) + ) + } +) diff --git a/web/hooks/useDownloadModel.ts b/web/hooks/useDownloadModel.ts index b91ac2a57d..7d5b2d1bdf 100644 --- a/web/hooks/useDownloadModel.ts +++ b/web/hooks/useDownloadModel.ts @@ -1,17 +1,15 @@ import { Model, ExtensionType, ModelExtension } from '@janhq/core' -import { useAtom } from 'jotai' +import { useSetAtom } from 'jotai' import { useDownloadState } from './useDownloadState' import { extensionManager } from '@/extension/ExtensionManager' -import { downloadingModelsAtom } from '@/helpers/atoms/Model.atom' +import { addNewDownloadingModelAtom } from '@/helpers/atoms/Model.atom' export default function useDownloadModel() { const { setDownloadState } = useDownloadState() - const [downloadingModels, setDownloadingModels] = useAtom( - downloadingModelsAtom - ) + const addNewDownloadingModel = useSetAtom(addNewDownloadingModelAtom) const downloadModel = async (model: Model) => { // set an initial download state @@ -29,7 +27,8 @@ export default function useDownloadModel() { }, }) - setDownloadingModels([...downloadingModels, model]) + addNewDownloadingModel(model) + await extensionManager .get(ExtensionType.Model) ?.downloadModel(model) diff --git a/web/hooks/useDownloadState.ts b/web/hooks/useDownloadState.ts index 527d6ab336..e1dd7163f5 100644 --- a/web/hooks/useDownloadState.ts +++ b/web/hooks/useDownloadState.ts @@ -18,7 +18,7 @@ const setDownloadStateSuccessAtom = atom(null, (get, set, modelId: string) => { const currentState = { ...get(modelDownloadStateAtom) } const state = currentState[modelId] if (!state) { - console.error(`Cannot find download state for ${modelId}`) + console.debug(`Cannot find download state for ${modelId}`) return } delete currentState[modelId] diff --git a/web/hooks/useSendChatMessage.ts b/web/hooks/useSendChatMessage.ts index 3e4a8f0261..11dd2c781f 100644 --- a/web/hooks/useSendChatMessage.ts +++ b/web/hooks/useSendChatMessage.ts @@ -1,4 +1,4 @@ -import { useState } from 'react' +import { useEffect, useRef, useState } from 'react' import { ChatCompletionMessage, @@ -11,6 +11,7 @@ import { Thread, ThreadMessage, events, + Model, } from '@janhq/core' import { ConversationalExtension } from '@janhq/core' import { useAtom, useAtomValue, useSetAtom } from 'jotai' @@ -48,6 +49,12 @@ export default function useSendChatMessage() { const { startModel } = useActiveModel() const [queuedMessage, setQueuedMessage] = useState(false) + const modelRef = useRef() + + useEffect(() => { + modelRef.current = activeModel + }, [activeModel]) + const resendChatMessage = async () => { if (!activeThread) { console.error('No active thread')