Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function initApi(key: KeyConfig) {
return new OpenAI(clientOptions)
}

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

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

const { message, uploadFileKeys, parentMessageId, process, systemMessage, temperature, top_p } = options
const { message, uploadFileKeys, parentMessageId, process, systemMessage, temperature, top_p, chatUuid } = options

try {
// Initialize OpenAI client
const openai = await initApi(key)

// Create abort controller for cancellation
const abort = new AbortController()
processThreads.push({ userId, abort, messageId })
processThreads.push({ userId, chatUuid, abort })

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

export function abortChatProcess(userId: string) {
const index = processThreads.findIndex(d => d.userId === userId)
export function abortChatProcess(userId: string, chatUuid: number) {
const index = processThreads.findIndex(d => d.userId === userId && d.chatUuid === chatUuid)
if (index <= -1)
return
const messageId = processThreads[index].messageId
processThreads[index].abort.abort()
processThreads.splice(index, 1)
return messageId
}

export function initAuditService(audit: AuditConfig) {
Expand Down
1 change: 1 addition & 0 deletions service/src/chatgpt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface RequestOptions {
user: UserInfo
messageId: string
room: ChatRoom
chatUuid: number
}

export interface BalanceResponse {
Expand Down
8 changes: 4 additions & 4 deletions service/src/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
user,
messageId: message._id.toString(),
room,
chatUuid: uuid,
})
// return the whole response including usage
res.write(`\n${JSON.stringify(result.data)}`)
Expand Down Expand Up @@ -329,12 +330,11 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
router.post('/chat-abort', [auth, limiter], async (req, res) => {
try {
const userId = req.headers.userId.toString()
const { text, messageId, conversationId } = req.body as { text: string, messageId: string, conversationId: string }
const msgId = abortChatProcess(userId)
await updateChat(msgId, text, messageId, conversationId, null, null)
const { chatUuid } = req.body as { chatUuid: number }
abortChatProcess(userId, chatUuid)
res.send({ status: 'Success', message: 'OK', data: null })
}
catch {
res.send({ status: 'Fail', message: '重置邮件已发送 | Reset email has been sent', data: null })
res.send({ status: 'Fail', message: '中止会话失败 | Chat abort error', data: null })
}
})
4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export function fetchChatAPIProcess<T = any>(
})
}

export function fetchChatStopResponding<T = any>(text: string, messageId: string, conversationId: string) {
export function fetchChatStopResponding<T = any>(chatUuid: number) {
return post<T>({
url: '/chat-abort',
data: { text, messageId, conversationId },
data: { chatUuid },
})
}

Expand Down
19 changes: 10 additions & 9 deletions src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,19 @@ export const useChatStore = defineStore('chat-store', () => {
return chatIndex !== -1 ? state.chat[chatIndex].data[index] : null
}

const addChatByUuid = async (uuid: number, chatItem: Chat.Chat) => {
const targetUuid = getCurrentUuid(uuid)
const chatIndex = findChatIndex(targetUuid)
const addChatMessage = async (roomId: number, chatItem: Chat.Chat) => {
const chatIndex = findChatIndex(roomId)

state.chat[chatIndex].data.push(chatItem)

if (state.chatRooms[chatIndex]?.title === 'New Chat') {
state.chatRooms[chatIndex].title = chatItem.text
await fetchRenameChatRoom(chatItem.text, state.chatRooms[chatIndex].roomId)
}
}

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

if (chatIndex !== -1 && state.chat[chatIndex].data[index]) {
const existingUuid = state.chat[chatIndex].data[index].uuid
Expand Down Expand Up @@ -274,9 +274,10 @@ export const useChatStore = defineStore('chat-store', () => {
deleteChatRoom,
setActive,
getChatByUuidAndIndex,
addChatByUuid,
updateChatByUuid,
updateChatSomeByUuid: updateChatByUuid,
addChatMessage,
updateChatMessage,
updateChatByUuid: updateChatMessage,
updateChatSomeByUuid: updateChatMessage,
deleteChatByUuid,
clearChatByUuid,
clearLocalChat,
Expand Down
8 changes: 4 additions & 4 deletions src/views/chat/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export function useChat() {
return chatStore.getChatByUuidAndIndex(uuid, index)
}

const addChat = (uuid: number, chat: Chat.Chat) => {
chatStore.addChatByUuid(uuid, chat)
const addChat = async (roomId: number, chat: Chat.Chat) => {
await chatStore.addChatMessage(roomId, chat)
}

const updateChat = (uuid: number, index: number, chat: Chat.Chat) => {
chatStore.updateChatByUuid(uuid, index, chat)
const updateChat = (roomId: number, index: number, chat: Chat.Chat) => {
chatStore.updateChatByUuid(roomId, index, chat)
}

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