Skip to content

Commit f81a24b

Browse files
committed
fix: add chat message when submit new conversation
Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 7c39e1f commit f81a24b

File tree

7 files changed

+63
-61
lines changed

7 files changed

+63
-61
lines changed

service/src/chatgpt/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function initApi(key: KeyConfig) {
5757
return new OpenAI(clientOptions)
5858
}
5959

60-
const processThreads: { userId: string, abort: AbortController, messageId: string }[] = []
60+
const processThreads: { userId: string, chatUuid: number, abort: AbortController }[] = []
6161

6262
async function chatReplyProcess(options: RequestOptions) {
6363
const globalConfig = await getCacheConfig()
@@ -70,15 +70,15 @@ async function chatReplyProcess(options: RequestOptions) {
7070
if (key == null || key === undefined)
7171
throw new Error('没有对应的apikeys配置。请再试一次 | No available apikeys configuration. Please try again.')
7272

73-
const { message, uploadFileKeys, parentMessageId, process, systemMessage, temperature, top_p } = options
73+
const { message, uploadFileKeys, parentMessageId, process, systemMessage, temperature, top_p, chatUuid } = options
7474

7575
try {
7676
// Initialize OpenAI client
7777
const openai = await initApi(key)
7878

7979
// Create abort controller for cancellation
8080
const abort = new AbortController()
81-
processThreads.push({ userId, abort, messageId })
81+
processThreads.push({ userId, chatUuid, abort })
8282

8383
// Prepare messages array for the chat completion
8484
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = []
@@ -262,14 +262,12 @@ search result: <search_result>${searchResultContent}</search_result>`,
262262
}
263263
}
264264

265-
export function abortChatProcess(userId: string) {
266-
const index = processThreads.findIndex(d => d.userId === userId)
265+
export function abortChatProcess(userId: string, chatUuid: number) {
266+
const index = processThreads.findIndex(d => d.userId === userId && d.chatUuid === chatUuid)
267267
if (index <= -1)
268268
return
269-
const messageId = processThreads[index].messageId
270269
processThreads[index].abort.abort()
271270
processThreads.splice(index, 1)
272-
return messageId
273271
}
274272

275273
export function initAuditService(audit: AuditConfig) {

service/src/chatgpt/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface RequestOptions {
3333
user: UserInfo
3434
messageId: string
3535
room: ChatRoom
36+
chatUuid: number
3637
}
3738

3839
export interface BalanceResponse {

service/src/routes/chat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
281281
user,
282282
messageId: message._id.toString(),
283283
room,
284+
chatUuid: uuid,
284285
})
285286
// return the whole response including usage
286287
res.write(`\n${JSON.stringify(result.data)}`)
@@ -329,12 +330,11 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
329330
router.post('/chat-abort', [auth, limiter], async (req, res) => {
330331
try {
331332
const userId = req.headers.userId.toString()
332-
const { text, messageId, conversationId } = req.body as { text: string, messageId: string, conversationId: string }
333-
const msgId = abortChatProcess(userId)
334-
await updateChat(msgId, text, messageId, conversationId, null, null)
333+
const { chatUuid } = req.body as { chatUuid: number }
334+
abortChatProcess(userId, chatUuid)
335335
res.send({ status: 'Success', message: 'OK', data: null })
336336
}
337337
catch {
338-
res.send({ status: 'Fail', message: '重置邮件已发送 | Reset email has been sent', data: null })
338+
res.send({ status: 'Fail', message: '中止会话失败 | Chat abort error', data: null })
339339
}
340340
})

src/api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ export function fetchChatAPIProcess<T = any>(
5050
})
5151
}
5252

53-
export function fetchChatStopResponding<T = any>(text: string, messageId: string, conversationId: string) {
53+
export function fetchChatStopResponding<T = any>(chatUuid: number) {
5454
return post<T>({
5555
url: '/chat-abort',
56-
data: { text, messageId, conversationId },
56+
data: { chatUuid },
5757
})
5858
}
5959

src/store/modules/chat/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,19 @@ export const useChatStore = defineStore('chat-store', () => {
206206
return chatIndex !== -1 ? state.chat[chatIndex].data[index] : null
207207
}
208208

209-
const addChatByUuid = async (uuid: number, chatItem: Chat.Chat) => {
210-
const targetUuid = getCurrentUuid(uuid)
211-
const chatIndex = findChatIndex(targetUuid)
209+
const addChatMessage = async (roomId: number, chatItem: Chat.Chat) => {
210+
const chatIndex = findChatIndex(roomId)
211+
212+
state.chat[chatIndex].data.push(chatItem)
212213

213214
if (state.chatRooms[chatIndex]?.title === 'New Chat') {
214215
state.chatRooms[chatIndex].title = chatItem.text
215216
await fetchRenameChatRoom(chatItem.text, state.chatRooms[chatIndex].roomId)
216217
}
217218
}
218219

219-
const updateChatByUuid = (uuid: number, index: number, chatItem: Chat.Chat | Partial<Chat.Chat>) => {
220-
const targetUuid = getCurrentUuid(uuid)
221-
const chatIndex = findChatIndex(targetUuid)
220+
const updateChatMessage = async (roomId: number, index: number, chatItem: Chat.Chat | Partial<Chat.Chat>) => {
221+
const chatIndex = findChatIndex(roomId)
222222

223223
if (chatIndex !== -1 && state.chat[chatIndex].data[index]) {
224224
const existingUuid = state.chat[chatIndex].data[index].uuid
@@ -274,9 +274,10 @@ export const useChatStore = defineStore('chat-store', () => {
274274
deleteChatRoom,
275275
setActive,
276276
getChatByUuidAndIndex,
277-
addChatByUuid,
278-
updateChatByUuid,
279-
updateChatSomeByUuid: updateChatByUuid,
277+
addChatMessage,
278+
updateChatMessage,
279+
updateChatByUuid: updateChatMessage,
280+
updateChatSomeByUuid: updateChatMessage,
280281
deleteChatByUuid,
281282
clearChatByUuid,
282283
clearLocalChat,

src/views/chat/hooks/useChat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export function useChat() {
77
return chatStore.getChatByUuidAndIndex(uuid, index)
88
}
99

10-
const addChat = (uuid: number, chat: Chat.Chat) => {
11-
chatStore.addChatByUuid(uuid, chat)
10+
const addChat = async (roomId: number, chat: Chat.Chat) => {
11+
await chatStore.addChatMessage(roomId, chat)
1212
}
1313

14-
const updateChat = (uuid: number, index: number, chat: Chat.Chat) => {
15-
chatStore.updateChatByUuid(uuid, index, chat)
14+
const updateChat = (roomId: number, index: number, chat: Chat.Chat) => {
15+
chatStore.updateChatByUuid(roomId, index, chat)
1616
}
1717

1818
const updateChatSome = (uuid: number, index: number, chat: Partial<Chat.Chat>) => {

0 commit comments

Comments
 (0)