Skip to content

Commit 7c39e1f

Browse files
authored
refactor: chat store remove use local storage (#626)
* refactor: chat store remove use local storage Signed-off-by: Bob Du <i@bobdu.cc> * refactor: chat store api Signed-off-by: Bob Du <i@bobdu.cc> * refactor: remove updateChatRoom from store and handle edit logic in component Signed-off-by: Bob Du <i@bobdu.cc> --------- Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 0ad2d7c commit 7c39e1f

File tree

6 files changed

+146
-215
lines changed

6 files changed

+146
-215
lines changed

src/store/modules/chat/helper.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/store/modules/chat/index.ts

Lines changed: 100 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@ import {
77
fetchGetChatRooms,
88
fetchRenameChatRoom,
99
fetchUpdateChatRoomChatModel,
10+
fetchUpdateChatRoomMaxContextCount,
1011
fetchUpdateChatRoomSearchEnabled,
1112
fetchUpdateChatRoomThinkEnabled,
1213
fetchUpdateChatRoomUsingContext,
1314
fetchUpdateUserChatModel,
15+
fetchUpdateUserMaxContextCount,
1416
} from '@/api'
1517
import { router } from '@/router'
1618
import { useUserStore } from '../user'
17-
import { getLocalState, setLocalState } from './helper'
1819

19-
export const useChatStore = defineStore('chat-store', () => {
20-
const initialState = getLocalState()
21-
const chat = ref(initialState.chat)
22-
const chatRooms = ref(initialState.chatRooms)
23-
const active = ref(initialState.active)
20+
class ChatState {
21+
active: number | null = null
22+
chatRooms: Chat.ChatRoom[] = []
23+
chat: { roomId: number, data: Chat.Chat[] }[] = []
24+
}
2425

25-
// Helpers
26-
const recordState = () => setLocalState({ chat: chat.value, chatRooms: chatRooms.value, active: active.value, usingContext: true })
26+
export const useChatStore = defineStore('chat-store', () => {
27+
const state = reactive<ChatState>(new ChatState())
2728

2829
const reloadRoute = async (roomId?: number) => {
29-
recordState()
3030
await router.push({ name: 'Chat', params: { uuid: roomId } })
3131
}
3232

33-
const findChatIndex = (uuid: number) => chat.value.findIndex(item => item.roomId === uuid)
34-
const findRoomIndex = (uuid: number) => chatRooms.value.findIndex(item => item.roomId === uuid)
35-
const getCurrentUuid = (uuid?: number) => uuid || active.value || chat.value[0]?.roomId
33+
const findRoomIndex = (roomId: number | null) => state.chatRooms.findIndex(item => item.roomId === roomId)
34+
const findChatIndex = (roomId: number) => state.chat.findIndex(item => item.roomId === roomId)
35+
const getCurrentUuid = (uuid?: number) => uuid || state.active || state.chat[0]?.roomId
3636

3737
// Getters
3838
const getChatRoomByCurrentActive = computed(() =>
39-
chatRooms.value.find(item => item.roomId === active.value) || null,
39+
state.chatRooms.find(item => item.roomId === state.active) || null,
4040
)
4141

4242
const getChatByUuid = computed(() => (uuid?: number) => {
4343
const targetUuid = getCurrentUuid(uuid)
44-
return chat.value.find(item => item.roomId === targetUuid)?.data ?? []
44+
return state.chat.find(item => item.roomId === targetUuid)?.data ?? []
4545
})
4646

4747
// Actions
@@ -50,7 +50,7 @@ export const useChatStore = defineStore('chat-store', () => {
5050
const roomId = Date.now()
5151
const result = await fetchCreateChatRoom(title, roomId)
5252

53-
chatRooms.value.unshift({
53+
state.chatRooms.unshift({
5454
title,
5555
roomId,
5656
isEdit: false,
@@ -61,8 +61,8 @@ export const useChatStore = defineStore('chat-store', () => {
6161
thinkEnabled: result.data?.thinkEnabled,
6262
})
6363

64-
chat.value.unshift({ roomId, data: [] })
65-
active.value = roomId
64+
state.chat.unshift({ roomId, data: [] })
65+
state.active = roomId
6666
await reloadRoute(roomId)
6767
}
6868

@@ -72,16 +72,16 @@ export const useChatStore = defineStore('chat-store', () => {
7272
return await addNewChatRoom()
7373
}
7474

75-
chatRooms.value = []
76-
chat.value = []
75+
state.chatRooms = []
76+
state.chat = []
7777

7878
rooms.forEach((r: Chat.ChatRoom) => {
79-
chatRooms.value.unshift(r)
80-
chat.value.unshift({ roomId: r.roomId, data: [] })
79+
state.chatRooms.unshift(r)
80+
state.chat.unshift({ roomId: r.roomId, data: [] })
8181
})
82-
if (!rooms.find((item: Chat.ChatRoom) => item.roomId === active.value)) {
83-
active.value = chatRooms.value[0].roomId
84-
await reloadRoute(active.value!)
82+
if (!rooms.find((item: Chat.ChatRoom) => item.roomId === state.active)) {
83+
state.active = state.chatRooms[0].roomId
84+
await reloadRoute(state.active!)
8585
}
8686
}
8787

@@ -90,159 +90,150 @@ export const useChatStore = defineStore('chat-store', () => {
9090
return callback?.()
9191

9292
const roomIndex = findRoomIndex(room.roomId)
93-
if (roomIndex === -1 || chatRooms.value[roomIndex].loading || chatRooms.value[roomIndex].all) {
93+
if (roomIndex === -1 || state.chatRooms[roomIndex].loading || state.chatRooms[roomIndex].all) {
9494
if (lastId === undefined)
9595
callback?.()
96-
if (chatRooms.value[roomIndex]?.all)
96+
if (state.chatRooms[roomIndex]?.all)
9797
onEmpty?.()
9898
return
9999
}
100100

101101
try {
102-
chatRooms.value[roomIndex].loading = true
102+
state.chatRooms[roomIndex].loading = true
103103
const chatIndex = findChatIndex(room.roomId)
104104

105-
if (chatIndex === -1 || chat.value[chatIndex].data.length === 0 || lastId !== undefined) {
105+
if (chatIndex === -1 || state.chat[chatIndex].data.length === 0 || lastId !== undefined) {
106106
onStart?.()
107107
const chatData = (await fetchGetChatHistory(room.roomId, lastId)).data
108108

109109
if (chatData.length === 0) {
110-
chatRooms.value[roomIndex].all = true
110+
state.chatRooms[roomIndex].all = true
111111
}
112112
else {
113113
if (chatIndex === -1) {
114-
chat.value.unshift({ roomId: room.roomId, data: chatData })
114+
state.chat.unshift({ roomId: room.roomId, data: chatData })
115115
}
116116
else {
117-
chat.value[chatIndex].data.unshift(...chatData)
117+
state.chat[chatIndex].data.unshift(...chatData)
118118
}
119119
}
120120
}
121121
}
122122
finally {
123-
chatRooms.value[roomIndex].loading = false
124-
if (chatRooms.value[roomIndex].all)
123+
state.chatRooms[roomIndex].loading = false
124+
if (state.chatRooms[roomIndex].all)
125125
onEmpty?.()
126-
recordState()
127126
callback?.()
128127
}
129128
}
130129

131-
const setUsingContext = async (context: boolean, roomId: number) => {
132-
await fetchUpdateChatRoomUsingContext(context, roomId)
133-
recordState()
134-
}
130+
const setUsingContext = async (usingContext: boolean) => {
131+
const index = findRoomIndex(state.active)
132+
if (index === -1)
133+
return
135134

136-
const setChatModel = async (chatModel: string, roomId: number) => {
137-
const index = findRoomIndex(active.value!)
138-
if (index !== -1) {
139-
chatRooms.value[index].chatModel = chatModel
140-
await fetchUpdateChatRoomChatModel(chatModel, roomId)
141-
const userStore = useUserStore()
142-
userStore.userInfo.config.chatModel = chatModel
143-
await fetchUpdateUserChatModel(chatModel)
144-
}
135+
state.chatRooms[index].usingContext = usingContext
136+
await fetchUpdateChatRoomUsingContext(usingContext, state.active!)
145137
}
146138

147-
const setChatSearchEnabled = async (searchEnabled: boolean, roomId: number) => {
148-
const index = findRoomIndex(active.value!)
149-
if (index !== -1) {
150-
chatRooms.value[index].searchEnabled = searchEnabled
151-
await fetchUpdateChatRoomSearchEnabled(searchEnabled, roomId)
152-
recordState()
153-
}
139+
const setMaxContextCount = async (maxContextCount: number) => {
140+
const index = findRoomIndex(state.active)
141+
if (index === -1)
142+
return
143+
144+
state.chatRooms[index].maxContextCount = maxContextCount
145+
await fetchUpdateChatRoomMaxContextCount(maxContextCount, state.active!)
146+
147+
const userStore = useUserStore()
148+
userStore.userInfo.config.maxContextCount = maxContextCount
149+
await fetchUpdateUserMaxContextCount(maxContextCount)
154150
}
155151

156-
const setChatThinkEnabled = async (thinkEnabled: boolean, roomId: number) => {
157-
const index = findRoomIndex(active.value!)
158-
if (index !== -1) {
159-
chatRooms.value[index].thinkEnabled = thinkEnabled
160-
await fetchUpdateChatRoomThinkEnabled(thinkEnabled, roomId)
161-
recordState()
162-
}
152+
const setChatModel = async (chatModel: string) => {
153+
const index = findRoomIndex(state.active)
154+
if (index === -1)
155+
return
156+
157+
state.chatRooms[index].chatModel = chatModel
158+
await fetchUpdateChatRoomChatModel(chatModel, state.active!)
159+
160+
const userStore = useUserStore()
161+
userStore.userInfo.config.chatModel = chatModel
162+
await fetchUpdateUserChatModel(chatModel)
163163
}
164164

165-
const updateChatRoom = async (uuid: number, edit: Partial<Chat.ChatRoom>) => {
166-
const index = findRoomIndex(uuid)
165+
const setChatSearchEnabled = async (searchEnabled: boolean) => {
166+
const index = findRoomIndex(state.active)
167167
if (index === -1)
168168
return
169169

170-
chatRooms.value[index] = { ...chatRooms.value[index], ...edit }
171-
recordState()
170+
state.chatRooms[index].searchEnabled = searchEnabled
171+
await fetchUpdateChatRoomSearchEnabled(searchEnabled, state.active!)
172+
}
173+
174+
const setChatThinkEnabled = async (thinkEnabled: boolean) => {
175+
const index = findRoomIndex(state.active)
176+
if (index === -1) {
177+
return
178+
}
172179

173-
if (!edit.isEdit)
174-
await fetchRenameChatRoom(chatRooms.value[index].title, uuid)
180+
state.chatRooms[index].thinkEnabled = thinkEnabled
181+
await fetchUpdateChatRoomThinkEnabled(thinkEnabled, state.active!)
175182
}
176183

177184
const deleteChatRoom = async (index: number) => {
178-
await fetchDeleteChatRoom(chatRooms.value[index].roomId)
179-
chatRooms.value.splice(index, 1)
180-
chat.value.splice(index, 1)
185+
await fetchDeleteChatRoom(state.chatRooms[index].roomId)
186+
state.chatRooms.splice(index, 1)
187+
state.chat.splice(index, 1)
181188

182-
if (chatRooms.value.length === 0)
189+
if (state.chatRooms.length === 0)
183190
return await addNewChatRoom()
184191

185-
const nextIndex = Math.min(index, chatRooms.value.length - 1)
186-
const roomId = chatRooms.value[nextIndex].roomId
187-
active.value = roomId
192+
const nextIndex = Math.min(index, state.chatRooms.length - 1)
193+
const roomId = state.chatRooms[nextIndex].roomId
194+
state.active = roomId
188195
await reloadRoute(roomId)
189196
}
190197

191198
const setActive = async (roomId: number) => {
192-
active.value = roomId
199+
state.active = roomId
193200
return await reloadRoute(roomId)
194201
}
195202

196203
const getChatByUuidAndIndex = (uuid: number, index: number) => {
197204
const targetUuid = getCurrentUuid(uuid)
198205
const chatIndex = findChatIndex(targetUuid)
199-
return chatIndex !== -1 ? chat.value[chatIndex].data[index] : null
206+
return chatIndex !== -1 ? state.chat[chatIndex].data[index] : null
200207
}
201208

202209
const addChatByUuid = async (uuid: number, chatItem: Chat.Chat) => {
203210
const targetUuid = getCurrentUuid(uuid)
204-
let chatIndex = findChatIndex(targetUuid)
205-
206-
if (chatIndex === -1 && chatRooms.value.length === 0) {
207-
const newUuid = Date.now()
208-
await fetchCreateChatRoom(chatItem.text, newUuid)
209-
chatRooms.value.push({ roomId: newUuid, title: chatItem.text, isEdit: false, usingContext: true, maxContextCount: 10 })
210-
chat.value.push({ roomId: newUuid, data: [chatItem] })
211-
active.value = newUuid
212-
}
213-
else {
214-
if (chatIndex === -1)
215-
chatIndex = 0
216-
chat.value[chatIndex].data.push(chatItem)
217-
218-
if (chatRooms.value[chatIndex]?.title === 'New Chat') {
219-
chatRooms.value[chatIndex].title = chatItem.text
220-
await fetchRenameChatRoom(chatItem.text, chatRooms.value[chatIndex].roomId)
221-
}
211+
const chatIndex = findChatIndex(targetUuid)
212+
213+
if (state.chatRooms[chatIndex]?.title === 'New Chat') {
214+
state.chatRooms[chatIndex].title = chatItem.text
215+
await fetchRenameChatRoom(chatItem.text, state.chatRooms[chatIndex].roomId)
222216
}
223-
recordState()
224217
}
225218

226219
const updateChatByUuid = (uuid: number, index: number, chatItem: Chat.Chat | Partial<Chat.Chat>) => {
227220
const targetUuid = getCurrentUuid(uuid)
228221
const chatIndex = findChatIndex(targetUuid)
229222

230-
if (chatIndex !== -1 && chat.value[chatIndex].data[index]) {
231-
const existingUuid = chat.value[chatIndex].data[index].uuid
232-
chat.value[chatIndex].data[index] = { ...chat.value[chatIndex].data[index], ...chatItem, uuid: existingUuid }
233-
recordState()
223+
if (chatIndex !== -1 && state.chat[chatIndex].data[index]) {
224+
const existingUuid = state.chat[chatIndex].data[index].uuid
225+
state.chat[chatIndex].data[index] = { ...state.chat[chatIndex].data[index], ...chatItem, uuid: existingUuid }
234226
}
235227
}
236228

237229
const deleteChatByUuid = async (uuid: number, index: number) => {
238230
const targetUuid = getCurrentUuid(uuid)
239231
const chatIndex = findChatIndex(targetUuid)
240232

241-
if (chatIndex !== -1 && chat.value[chatIndex].data[index]) {
242-
const chatData = chat.value[chatIndex].data[index]
233+
if (chatIndex !== -1 && state.chat[chatIndex].data[index]) {
234+
const chatData = state.chat[chatIndex].data[index]
243235
await fetchDeleteChat(targetUuid, chatData.uuid || 0, chatData.inversion)
244-
chat.value[chatIndex].data.splice(index, 1)
245-
recordState()
236+
state.chat[chatIndex].data.splice(index, 1)
246237
}
247238
}
248239

@@ -252,24 +243,20 @@ export const useChatStore = defineStore('chat-store', () => {
252243

253244
if (chatIndex !== -1) {
254245
await fetchClearChat(targetUuid)
255-
chat.value[chatIndex].data = []
256-
recordState()
246+
state.chat[chatIndex].data = []
257247
}
258248
}
259249

260250
const clearLocalChat = async () => {
261-
chat.value = []
262-
chatRooms.value = []
263-
active.value = null
264-
recordState()
251+
state.chat = []
252+
state.chatRooms = []
253+
state.active = null
265254
await router.push({ name: 'Chat' })
266255
}
267256

268257
return {
269258
// State
270-
chat,
271-
chatRooms,
272-
active,
259+
...toRefs(state),
273260

274261
// Getters
275262
getChatRoomByCurrentActive,
@@ -279,11 +266,11 @@ export const useChatStore = defineStore('chat-store', () => {
279266
syncHistory,
280267
syncChat,
281268
setUsingContext,
269+
setMaxContextCount,
282270
setChatModel,
283271
setChatSearchEnabled,
284272
setChatThinkEnabled,
285273
addNewChatRoom,
286-
updateChatRoom,
287274
deleteChatRoom,
288275
setActive,
289276
getChatByUuidAndIndex,

0 commit comments

Comments
 (0)