forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatMessage.atom.ts
150 lines (133 loc) · 3.91 KB
/
ChatMessage.atom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
ChatCompletionRole,
MessageStatus,
ThreadContent,
ThreadMessage,
} from '@janhq/core'
import { atom } from 'jotai'
import {
getActiveThreadIdAtom,
updateThreadStateLastMessageAtom,
} from './Thread.atom'
/**
* Stores all chat messages for all threads
*/
export const chatMessages = atom<Record<string, ThreadMessage[]>>({})
export const readyThreadsMessagesAtom = atom<Record<string, boolean>>({})
/**
* Return the chat messages for the current active conversation
*/
export const getCurrentChatMessagesAtom = atom<ThreadMessage[]>((get) => {
const activeThreadId = get(getActiveThreadIdAtom)
if (!activeThreadId) return []
const messages = get(chatMessages)[activeThreadId]
return messages ?? []
})
export const setConvoMessagesAtom = atom(
null,
(get, set, threadId: string, messages: ThreadMessage[]) => {
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[threadId] = messages
set(chatMessages, newData)
set(readyThreadsMessagesAtom, {
...get(readyThreadsMessagesAtom),
[threadId]: true,
})
}
)
/**
* Used for pagination. Add old messages to the current conversation
*/
export const addOldMessagesAtom = atom(
null,
(get, set, newMessages: ThreadMessage[]) => {
const currentConvoId = get(getActiveThreadIdAtom)
if (!currentConvoId) return
const currentMessages = get(chatMessages)[currentConvoId] ?? []
const updatedMessages = [...currentMessages, ...newMessages]
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[currentConvoId] = updatedMessages
set(chatMessages, newData)
}
)
export const addNewMessageAtom = atom(
null,
(get, set, newMessage: ThreadMessage) => {
const currentMessages = get(chatMessages)[newMessage.thread_id] ?? []
const updatedMessages = [...currentMessages, newMessage]
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[newMessage.thread_id] = updatedMessages
set(chatMessages, newData)
// Update thread last message
if (newMessage.content.length)
set(
updateThreadStateLastMessageAtom,
newMessage.thread_id,
newMessage.content
)
}
)
export const deleteChatMessageAtom = atom(
null,
(get, set, threadId: string) => {
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[threadId] = []
set(chatMessages, newData)
}
)
export const cleanChatMessageAtom = atom(null, (get, set, id: string) => {
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[id] = newData[id]?.filter((e) => e.role === ChatCompletionRole.System)
set(chatMessages, newData)
})
export const deleteMessageAtom = atom(null, (get, set, id: string) => {
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
const threadId = get(getActiveThreadIdAtom)
if (threadId) {
// Should also delete error messages to clear out the error state
newData[threadId] = newData[threadId].filter(
(e) => e.id !== id && e.status !== MessageStatus.Error
)
set(chatMessages, newData)
}
})
export const editMessageAtom = atom('')
export const updateMessageAtom = atom(
null,
(
get,
set,
id: string,
conversationId: string,
text: ThreadContent[],
status: MessageStatus
) => {
const messages = get(chatMessages)[conversationId] ?? []
const message = messages.find((e) => e.id === id)
if (message) {
message.content = text
message.status = status
const updatedMessages = [...messages]
const newData: Record<string, ThreadMessage[]> = {
...get(chatMessages),
}
newData[conversationId] = updatedMessages
set(chatMessages, newData)
// Update thread last message
if (text.length)
set(updateThreadStateLastMessageAtom, conversationId, text)
}
}
)