Skip to content

Commit 97024cb

Browse files
committed
refactor: Enhance useMessageOperations to incorporate selectedModel for improved message retry and editing functionality
1 parent 696ed15 commit 97024cb

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/renderer/src/components/pages/chat/ChatLogic.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ export default function ChatLogic({
104104
aiService: enhancedAIService,
105105
messageTree,
106106
isLoading,
107-
setIsLoading
107+
setIsLoading,
108+
selectedModel
108109
})
109110

110111
// 设置 sendMessage 引用,确保自动追问也使用增强的消息发送

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface UseMessageOperationsProps {
1515
messageTree: MessageTree
1616
isLoading: boolean
1717
setIsLoading: (loading: boolean) => void
18+
selectedModel?: string
1819
}
1920

2021
export interface UseMessageOperationsReturn {
@@ -33,7 +34,8 @@ export function useMessageOperations({
3334
aiService,
3435
messageTree,
3536
isLoading,
36-
setIsLoading
37+
setIsLoading,
38+
selectedModel
3739
}: UseMessageOperationsProps): UseMessageOperationsReturn {
3840
const { addMessageToParent, toggleMessageFavorite, deleteMessageAndChildren } = useMessagesStore()
3941
const { settings } = useSettingsStore()
@@ -116,16 +118,18 @@ export function useMessageOperations({
116118
async (messageId: string) => {
117119
if (!chat || isLoading) return
118120

119-
const llmConfig = aiService.getLLMConfig()
121+
// 找到要重试的消息
122+
const messageToRetry = chat.messages.find((msg: any) => msg.id === messageId)
123+
if (!messageToRetry) return
124+
125+
// 使用原消息的模型ID
126+
const modelIdToUse = messageToRetry.modelId
127+
const llmConfig = aiService.getLLMConfig(modelIdToUse)
120128
if (!llmConfig) {
121129
message.error('请先在设置中配置LLM')
122130
return
123131
}
124132

125-
// 找到要重试的消息
126-
const messageToRetry = chat.messages.find((msg: any) => msg.id === messageId)
127-
if (!messageToRetry) return
128-
129133
// 在树状结构中,重试意味着从父消息重新生成一个新的分支
130134
const currentPath = chat.currentPath || messageTree.getCurrentPath()
131135
const currentPathMessages = currentPath
@@ -182,16 +186,18 @@ export function useMessageOperations({
182186
async (messageId: string, newContent: string) => {
183187
if (!chat || isLoading) return
184188

185-
const llmConfig = aiService.getLLMConfig()
189+
// 找到要编辑的消息
190+
const targetMessage = chat.messages.find((msg: any) => msg.id === messageId)
191+
if (!targetMessage) return
192+
193+
// 如果是用户消息,使用当前选中的模型;如果是AI消息,使用原消息的模型
194+
const modelIdToUse = targetMessage.role === 'user' ? selectedModel : targetMessage.modelId
195+
const llmConfig = aiService.getLLMConfig(modelIdToUse)
186196
if (!llmConfig) {
187197
message.error('请先在设置中配置LLM')
188198
return
189199
}
190200

191-
// 找到要编辑的消息
192-
const targetMessage = chat.messages.find((msg: any) => msg.id === messageId)
193-
if (!targetMessage) return
194-
195201
setIsLoading(true)
196202

197203
try {
@@ -274,7 +280,8 @@ export function useMessageOperations({
274280
chatId,
275281
messageTree,
276282
addMessageToParent,
277-
setIsLoading
283+
setIsLoading,
284+
selectedModel
278285
]
279286
)
280287

0 commit comments

Comments
 (0)