* feat: adding create bot functionality Signed-off-by: James <james@jan.ai> * update the temperature progress bar Signed-off-by: James <james@jan.ai> * WIP baselayout * Mapping plugins with available preferences * Added loader component * WIP working another screen * Cleanup types and avoid import one by one * Prepare bottom bar * Add css variables colors to enable user select the accent * Enable change accent color * Seperate css variable * Fix conflict * Add blank state of my model empty * Restyle explore models page * Enable user config left sidebar * Restyle my models page * WIP styling chat page * Restyling chat message * Fix conflict * Adde form preferences setting plugins * Fixed form bot info * Sidebar bot chat * Showing rightbar for both setting when user created bot * Fix style bot info * Using overflow auto intead of scroll * Remove script built UI from root package * Fix missig import * Resolve error linter * fix e2e tests Signed-off-by: James <james@jan.ai> --------- Signed-off-by: James <james@jan.ai> Co-authored-by: James <james@jan.ai>
105 lines
2.9 KiB
TypeScript
105 lines
2.9 KiB
TypeScript
import { atom } from 'jotai'
|
|
import { getActiveConvoIdAtom } from './Conversation.atom'
|
|
|
|
/**
|
|
* Stores all chat messages for all conversations
|
|
*/
|
|
export const chatMessages = atom<Record<string, ChatMessage[]>>({})
|
|
|
|
export const currentChatMessagesAtom = atom<ChatMessage[]>((get) => {
|
|
const activeConversationId = get(getActiveConvoIdAtom)
|
|
if (!activeConversationId) return []
|
|
return get(chatMessages)[activeConversationId] ?? []
|
|
})
|
|
|
|
export const addOldMessagesAtom = atom(
|
|
null,
|
|
(get, set, newMessages: ChatMessage[]) => {
|
|
const currentConvoId = get(getActiveConvoIdAtom)
|
|
if (!currentConvoId) return
|
|
|
|
const currentMessages = get(chatMessages)[currentConvoId] ?? []
|
|
const updatedMessages = [...currentMessages, ...newMessages]
|
|
|
|
const newData: Record<string, ChatMessage[]> = {
|
|
...get(chatMessages),
|
|
}
|
|
newData[currentConvoId] = updatedMessages
|
|
set(chatMessages, newData)
|
|
}
|
|
)
|
|
|
|
export const addNewMessageAtom = atom(
|
|
null,
|
|
(get, set, newMessage: ChatMessage) => {
|
|
const currentConvoId = get(getActiveConvoIdAtom)
|
|
if (!currentConvoId) return
|
|
|
|
const currentMessages = get(chatMessages)[currentConvoId] ?? []
|
|
const updatedMessages = [newMessage, ...currentMessages]
|
|
|
|
const newData: Record<string, ChatMessage[]> = {
|
|
...get(chatMessages),
|
|
}
|
|
newData[currentConvoId] = updatedMessages
|
|
set(chatMessages, newData)
|
|
}
|
|
)
|
|
|
|
export const deleteConversationMessage = atom(null, (get, set, id: string) => {
|
|
const newData: Record<string, ChatMessage[]> = {
|
|
...get(chatMessages),
|
|
}
|
|
newData[id] = []
|
|
set(chatMessages, newData)
|
|
})
|
|
|
|
export const updateMessageAtom = atom(
|
|
null,
|
|
(get, set, id: string, conversationId: string, text: string) => {
|
|
const messages = get(chatMessages)[conversationId] ?? []
|
|
const message = messages.find((e) => e.id === id)
|
|
if (message) {
|
|
message.text = text
|
|
const updatedMessages = [...messages]
|
|
|
|
const newData: Record<string, ChatMessage[]> = {
|
|
...get(chatMessages),
|
|
}
|
|
newData[conversationId] = updatedMessages
|
|
set(chatMessages, newData)
|
|
}
|
|
}
|
|
)
|
|
|
|
/**
|
|
* For updating the status of the last AI message that is pending
|
|
*/
|
|
export const updateLastMessageAsReadyAtom = atom(
|
|
null,
|
|
(get, set, id, text: string) => {
|
|
const currentConvoId = get(getActiveConvoIdAtom)
|
|
if (!currentConvoId) return
|
|
|
|
const currentMessages = get(chatMessages)[currentConvoId] ?? []
|
|
const messageToUpdate = currentMessages.find((e) => e.id === id)
|
|
|
|
// if message is not found, do nothing
|
|
if (!messageToUpdate) return
|
|
|
|
const index = currentMessages.indexOf(messageToUpdate)
|
|
const updatedMsg: ChatMessage = {
|
|
...messageToUpdate,
|
|
status: MessageStatus.Ready,
|
|
text: text,
|
|
}
|
|
|
|
currentMessages[index] = updatedMsg
|
|
const newData: Record<string, ChatMessage[]> = {
|
|
...get(chatMessages),
|
|
}
|
|
newData[currentConvoId] = currentMessages
|
|
set(chatMessages, newData)
|
|
}
|
|
)
|