diff --git a/src/renderer/src/pages/home/Messages/MessageGroup.tsx b/src/renderer/src/pages/home/Messages/MessageGroup.tsx index 8b4c35e103..0f5a7e83f7 100644 --- a/src/renderer/src/pages/home/Messages/MessageGroup.tsx +++ b/src/renderer/src/pages/home/Messages/MessageGroup.tsx @@ -11,7 +11,7 @@ import type { Topic } from '@renderer/types' import type { Message } from '@renderer/types/newMessage' import { classNames } from '@renderer/utils' import { Popover } from 'antd' -import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react' +import { ComponentProps, memo, useCallback, useEffect, useMemo, useState } from 'react' import styled from 'styled-components' import { useChatMaxWidth } from '../Chat' @@ -43,9 +43,6 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => { ) const [selectedIndex, setSelectedIndex] = useState(messageLength - 1) - // Refs - const prevMessageLengthRef = useRef(messageLength) - // 对于单模型消息,采用简单的样式,避免 overflow 影响内部的 sticky 效果 const multiModelMessageStyle = useMemo( () => (messageLength < 2 ? 'fold' : _multiModelMessageStyle), @@ -83,24 +80,6 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => { }, [editMessage, selectedMessageId, setTimeoutTimer] ) - - useEffect(() => { - if (messageLength > prevMessageLengthRef.current) { - setSelectedIndex(messageLength - 1) - const lastMessage = messages[messageLength - 1] - if (lastMessage) { - setSelectedMessage(lastMessage) - } - } else { - const newIndex = messages.findIndex((msg) => msg.id === selectedMessageId) - if (newIndex !== -1) { - setSelectedIndex(newIndex) - } - } - prevMessageLengthRef.current = messageLength - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [messageLength]) - // 添加对流程图节点点击事件的监听 useEffect(() => { // 只在组件挂载和消息数组变化时添加监听器 @@ -223,7 +202,7 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => { message, topic, index: message.index - } + } satisfies ComponentProps const messageContent = ( { isGrouped, topic, multiModelMessageStyle, - messages.length, + messages, selectedMessageId, onUpdateUseful, groupContextMessageId, diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 665127e603..c9b33eea8e 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -1446,7 +1446,7 @@ export const appendAssistantResponseThunk = } // 2. Create the new assistant message stub - const newAssistantStub = createAssistantMessage(assistant.id, topicId, { + const newAssistantMessageStub = createAssistantMessage(assistant.id, topicId, { askId: askId, // Crucial: Use the original askId model: newModel, modelId: newModel.id, @@ -1459,9 +1459,14 @@ export const appendAssistantResponseThunk = const insertAtIndex = existingMessageIndex !== -1 ? existingMessageIndex + 1 : currentTopicMessageIds.length // 4. Update Database (Save the stub to the topic's message list) - await saveMessageAndBlocksToDB(newAssistantStub, [], insertAtIndex) + await saveMessageAndBlocksToDB(newAssistantMessageStub, [], insertAtIndex) - dispatch(newMessagesActions.insertMessageAtIndex({ topicId, message: newAssistantStub, index: insertAtIndex })) + dispatch( + newMessagesActions.insertMessageAtIndex({ topicId, message: newAssistantMessageStub, index: insertAtIndex }) + ) + + dispatch(updateMessageAndBlocksThunk(topicId, { id: existingAssistantMessageId, foldSelected: false }, [])) + dispatch(updateMessageAndBlocksThunk(topicId, { id: newAssistantMessageStub.id, foldSelected: true }, [])) // 5. Prepare and queue the processing task const assistantConfigForThisCall = { @@ -1475,7 +1480,7 @@ export const appendAssistantResponseThunk = getState, topicId, assistantConfigForThisCall, - newAssistantStub // Pass the newly created stub + newAssistantMessageStub // Pass the newly created stub ) }) } catch (error) {