chore: add facades refactor: core module export refactor: inference plugin - deprecate function registering (#537) * refactor: revamp inference plugin as class - deprecate function registering * refactor: monitoring plugin - deprecate service registering (#538) refactor: revamp inference plugin as class - deprecate function registering chore: update import refactor: plugin revamp - model management chore: update build steps and remove experimental plugins refactor: remove pluggable electron chore: add sorting for conversations chore: build plugins for testing chore: consistent plugin directory name chore: docs chore: fix CI chore: update conversation prefix
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { useSetAtom } from 'jotai'
|
|
import {
|
|
conversationStatesAtom,
|
|
userConversationsAtom,
|
|
} from '@helpers/atoms/Conversation.atom'
|
|
import { pluginManager } from '../plugin/PluginManager'
|
|
import { PluginType } from '@janhq/core'
|
|
import { setConvoMessagesAtom } from '@helpers/atoms/ChatMessage.atom'
|
|
import { toChatMessage } from '@models/ChatMessage'
|
|
import { ConversationalPlugin } from '@janhq/core/lib/plugins'
|
|
import { Conversation } from "@janhq/core/lib/types"
|
|
|
|
const useGetUserConversations = () => {
|
|
const setConversationStates = useSetAtom(conversationStatesAtom)
|
|
const setConversations = useSetAtom(userConversationsAtom)
|
|
const setConvoMessages = useSetAtom(setConvoMessagesAtom)
|
|
|
|
const getUserConversations = async () => {
|
|
try {
|
|
const convos: Conversation[] | undefined = await pluginManager
|
|
.get<ConversationalPlugin>(PluginType.Conversational)
|
|
?.getConversations()
|
|
const convoStates: Record<string, ConversationState> = {}
|
|
convos?.forEach((convo) => {
|
|
convoStates[convo._id ?? ''] = {
|
|
hasMore: true,
|
|
waitingForResponse: false,
|
|
}
|
|
setConvoMessages(
|
|
convo.messages.map<ChatMessage>((msg) => toChatMessage(msg)),
|
|
convo._id ?? ''
|
|
)
|
|
})
|
|
setConversationStates(convoStates)
|
|
setConversations(convos ?? [])
|
|
} catch (ex) {
|
|
console.log(ex)
|
|
}
|
|
}
|
|
|
|
return {
|
|
getUserConversations,
|
|
}
|
|
}
|
|
|
|
export default useGetUserConversations
|