Skip to content

Commit

Permalink
fix: can not download multiple models at once (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Dec 6, 2023
1 parent 8fbd5fc commit 7656048
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
4 changes: 3 additions & 1 deletion web/containers/Providers/EventListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModelExtension>(ExtensionType.Model)
Expand Down
20 changes: 20 additions & 0 deletions web/helpers/atoms/Model.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,24 @@ import { atom } from 'jotai'

export const stateModel = atom({ state: 'start', loading: false, model: '' })
export const activeAssistantModelAtom = atom<Model | undefined>(undefined)

export const downloadingModelsAtom = atom<Model[]>([])

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)
)
}
)
11 changes: 5 additions & 6 deletions web/hooks/useDownloadModel.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,7 +27,8 @@ export default function useDownloadModel() {
},
})

setDownloadingModels([...downloadingModels, model])
addNewDownloadingModel(model)

await extensionManager
.get<ModelExtension>(ExtensionType.Model)
?.downloadModel(model)
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useDownloadState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useRef, useState } from 'react'

import {
ChatCompletionMessage,
Expand All @@ -11,6 +11,7 @@ import {
Thread,
ThreadMessage,
events,
Model,
} from '@janhq/core'
import { ConversationalExtension } from '@janhq/core'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
Expand Down Expand Up @@ -48,6 +49,12 @@ export default function useSendChatMessage() {
const { startModel } = useActiveModel()
const [queuedMessage, setQueuedMessage] = useState(false)

const modelRef = useRef<Model | undefined>()

useEffect(() => {
modelRef.current = activeModel
}, [activeModel])

const resendChatMessage = async () => {
if (!activeThread) {
console.error('No active thread')
Expand Down

0 comments on commit 7656048

Please sign in to comment.