* refactor: model plugin to follow new specs Signed-off-by: James <james@jan.ai> * chore: rebase main chore: rebase main --------- Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai> Co-authored-by: Louis <louis@jan.ai>
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { PluginType, Thread } from '@janhq/core'
|
|
|
|
import { ConversationalPlugin } from '@janhq/core/lib/plugins'
|
|
|
|
import { useAtomValue, useSetAtom } from 'jotai'
|
|
|
|
import { setConvoMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'
|
|
import {
|
|
getActiveThreadIdAtom,
|
|
setActiveThreadIdAtom,
|
|
} from '@/helpers/atoms/Conversation.atom'
|
|
import { pluginManager } from '@/plugin'
|
|
|
|
export default function useSetActiveThread() {
|
|
const activeThreadId = useAtomValue(getActiveThreadIdAtom)
|
|
const setActiveThreadId = useSetAtom(setActiveThreadIdAtom)
|
|
const setThreadMessage = useSetAtom(setConvoMessagesAtom)
|
|
|
|
const setActiveThread = async (thread: Thread) => {
|
|
if (activeThreadId === thread.id) {
|
|
console.debug('Thread already active')
|
|
return
|
|
}
|
|
|
|
if (!thread.isFinishInit) {
|
|
console.debug('Thread not finish init')
|
|
return
|
|
}
|
|
|
|
// load the corresponding messages
|
|
const messages = await pluginManager
|
|
.get<ConversationalPlugin>(PluginType.Conversational)
|
|
?.getAllMessages(thread.id)
|
|
setThreadMessage(thread.id, messages ?? [])
|
|
|
|
setActiveThreadId(thread.id)
|
|
}
|
|
|
|
return { activeThreadId, setActiveThread }
|
|
}
|