Skip to content

Commit d473f2c

Browse files
committed
refactor: Improve message handling in useMessageOperations by optimizing message history retrieval and enhancing AI message sending logic
1 parent a089006 commit d473f2c

1 file changed

Lines changed: 54 additions & 41 deletions

File tree

src/renderer/src/components/pages/chat/hooks/useMessageOperations.ts

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export interface UseMessageOperationsProps {
1919
}
2020

2121
export interface UseMessageOperationsReturn {
22-
handleSendMessage: (content: string, customModelId?: string, customParentId?: string) => Promise<void>
22+
handleSendMessage: (
23+
content: string,
24+
customModelId?: string,
25+
customParentId?: string
26+
) => Promise<void>
2327
handleRetryMessage: (messageId: string) => Promise<void>
2428
handleEditMessage: (messageId: string, newContent: string) => Promise<void>
2529
handleEditAndResendMessage: (messageId: string, newContent: string) => Promise<void>
@@ -88,9 +92,16 @@ export function useMessageOperations({
8892
setIsLoading(true)
8993

9094
try {
91-
// Use the messages we just calculated instead of relying on state
92-
const allMessages = [...currentMessages, userMessageWithParent]
93-
await aiService.sendAIMessage(allMessages, llmConfig, userMessageWithParent.id, 'chat', {
95+
// 获取当前分支路径上的消息,而不是全部消息
96+
const currentPath = chat?.currentPath || messageTree.getCurrentPath()
97+
const currentPathMessages = currentPath
98+
.map((id: string) => chat.messages.find((msg: any) => msg.id === id))
99+
.filter(Boolean) as ChatMessage[]
100+
101+
// 构建要发送给AI的消息历史(当前分支 + 新用户消息)
102+
const messagesToSend = [...currentPathMessages, userMessageWithParent]
103+
104+
await aiService.sendAIMessage(messagesToSend, llmConfig, userMessageWithParent.id, 'chat', {
94105
chat: {
95106
messageContent: content.trim(),
96107
parentMessageId: parentId
@@ -103,15 +114,7 @@ export function useMessageOperations({
103114
setIsLoading(false)
104115
}
105116
},
106-
[
107-
isLoading,
108-
aiService,
109-
chatId,
110-
chat,
111-
addMessageToParent,
112-
messageTree,
113-
setIsLoading
114-
]
117+
[isLoading, aiService, chatId, chat, addMessageToParent, messageTree, setIsLoading]
115118
)
116119

117120
const handleRetryMessage = useCallback(
@@ -235,12 +238,18 @@ export function useMessageOperations({
235238
}
236239

237240
// 生成新的AI回复
238-
await aiService.sendAIMessage(messagesToSend, llmConfig, editedUserMessage.id, 'edit_resend', {
239-
editResend: {
240-
originalMessageId: messageId,
241-
newContent: newContent
241+
await aiService.sendAIMessage(
242+
messagesToSend,
243+
llmConfig,
244+
editedUserMessage.id,
245+
'edit_resend',
246+
{
247+
editResend: {
248+
originalMessageId: messageId,
249+
newContent: newContent
250+
}
242251
}
243-
})
252+
)
244253
} else {
245254
// 对于AI消息,从其父消息重新生成
246255
const currentPath = chat.currentPath || messageTree.getCurrentPath()
@@ -259,12 +268,18 @@ export function useMessageOperations({
259268
}
260269

261270
// 生成新的AI回复作为兄弟分支
262-
await aiService.sendAIMessage(messagesToSend, llmConfig, targetMessage.parentId, 'edit_resend', {
263-
editResend: {
264-
originalMessageId: messageId,
265-
newContent: newContent
271+
await aiService.sendAIMessage(
272+
messagesToSend,
273+
llmConfig,
274+
targetMessage.parentId,
275+
'edit_resend',
276+
{
277+
editResend: {
278+
originalMessageId: messageId,
279+
newContent: newContent
280+
}
266281
}
267-
})
282+
)
268283
}
269284
} catch (error) {
270285
console.error('Edit and resend failed:', error)
@@ -327,28 +342,26 @@ export function useMessageOperations({
327342

328343
try {
329344
// 使用新模型生成AI回复作为兄弟分支
330-
await aiService.sendAIMessage(messagesToSend, llmConfig, targetMessage.parentId, 'model_change', {
331-
modelChange: {
332-
originalMessageId: messageId,
333-
newModelId: newModelId
345+
await aiService.sendAIMessage(
346+
messagesToSend,
347+
llmConfig,
348+
targetMessage.parentId,
349+
'model_change',
350+
{
351+
modelChange: {
352+
originalMessageId: messageId,
353+
newModelId: newModelId
354+
}
334355
}
335-
})
356+
)
336357
} catch (error) {
337358
console.error('Model change failed:', error)
338359
message.error('切换模型失败,请检查网络连接和配置')
339360
} finally {
340361
setIsLoading(false)
341362
}
342363
},
343-
[
344-
chat,
345-
isLoading,
346-
chatId,
347-
aiService,
348-
messageTree,
349-
setIsLoading,
350-
settings
351-
]
364+
[chat, isLoading, chatId, aiService, messageTree, setIsLoading, settings]
352365
)
353366

354367
const handleDeleteMessage = useCallback(
@@ -375,10 +388,10 @@ export function useMessageOperations({
375388
const hasChildren = totalCount > 1
376389

377390
// 显示确认对话框
378-
const confirmText = hasChildren
379-
? `确定要删除这条消息及其所有子分支吗?(共 ${totalCount} 条消息)`
391+
const confirmText = hasChildren
392+
? `确定要删除这条消息及其所有子分支吗?(共 ${totalCount} 条消息)`
380393
: '确定要删除这条消息吗?'
381-
394+
382395
const confirmed = await new Promise<boolean>((resolve) => {
383396
modal.confirm({
384397
title: '删除消息',
@@ -414,4 +427,4 @@ export function useMessageOperations({
414427
handleModelChangeForMessage,
415428
handleDeleteMessage
416429
}
417-
}
430+
}

0 commit comments

Comments
 (0)