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
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
|
|
import {
|
|
userConversationsAtom,
|
|
setActiveConvoIdAtom,
|
|
addNewConversationStateAtom,
|
|
} from '@helpers/atoms/Conversation.atom'
|
|
import { Model } from '@janhq/core/lib/types'
|
|
import { downloadedModelAtom } from '@helpers/atoms/DownloadedModel.atom'
|
|
|
|
const useCreateConversation = () => {
|
|
const [userConversations, setUserConversations] = useAtom(
|
|
userConversationsAtom
|
|
)
|
|
const setActiveConvoId = useSetAtom(setActiveConvoIdAtom)
|
|
const addNewConvoState = useSetAtom(addNewConversationStateAtom)
|
|
const models = useAtomValue(downloadedModelAtom)
|
|
|
|
const createConvoByBot = async (bot: Bot) => {
|
|
const model = models.find((e) => e._id === bot.modelId)
|
|
|
|
if (!model) {
|
|
alert(
|
|
`Model ${bot.modelId} not found! Please re-download the model first.`
|
|
)
|
|
return
|
|
}
|
|
|
|
return requestCreateConvo(model, bot)
|
|
}
|
|
|
|
const requestCreateConvo = async (model: Model, bot?: Bot) => {
|
|
const conversationName = model.name
|
|
const mappedConvo: Conversation = {
|
|
_id: `jan-${Date.now()}`,
|
|
modelId: model._id,
|
|
name: conversationName,
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
botId: bot?._id ?? undefined,
|
|
}
|
|
|
|
addNewConvoState(mappedConvo._id, {
|
|
hasMore: true,
|
|
waitingForResponse: false,
|
|
})
|
|
setUserConversations([mappedConvo, ...userConversations])
|
|
setActiveConvoId(mappedConvo._id)
|
|
}
|
|
|
|
return {
|
|
createConvoByBot,
|
|
requestCreateConvo,
|
|
}
|
|
}
|
|
|
|
export default useCreateConversation
|