Skip to content

Commit 6866e2b

Browse files
committed
refactor(chat): Improve error handling and configuration retrieval for AI models across chat components
1 parent 1257ace commit 6866e2b

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

components/ai-chat/ai-chat-new.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ export default function Chat() {
104104
}
105105
}, [reloadModel, aiModel])
106106

107-
const { getConfigByModel, hasAvailableModels } = useAiConfig()
107+
const { getConfigByModel } = useAiConfig()
108+
const config = useMemo(() => {
109+
try {
110+
return getConfigByModel(aiModel)
111+
} catch (error) {
112+
return {}
113+
}
114+
}, [aiModel, getConfigByModel])
115+
108116
const { messages, setMessages, reload, append, isLoading, stop } = useChat({
109117
onToolCall: async ({ toolCall }) => {
110118
const res = await handleToolsCall(toolCall.toolName, toolCall.args)
@@ -123,7 +131,7 @@ export default function Chat() {
123131
})
124132
},
125133
body: {
126-
...getConfigByModel(aiModel),
134+
...config,
127135
systemPrompt,
128136
model: aiModel,
129137
useTools: enableTools, // 使用 enableTools 状态控制

components/doc/plugins/AIToolsPlugin/ai-tools.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ export function AITools({
132132
}
133133

134134
const { addScript } = useScript()
135+
const config = useMemo(() => {
136+
try {
137+
return getConfigByModel(currentModel)
138+
} catch (error) {
139+
return {}
140+
}
141+
}, [currentModel, getConfigByModel])
135142
const { messages, setMessages, reload, isLoading, stop } = useChat({
136143
onError(error) {
137144
console.log("error:", error)
@@ -161,7 +168,7 @@ export function AITools({
161168
setActionOpen(true)
162169
},
163170
body: {
164-
...getConfigByModel(currentModel),
171+
...config,
165172
model: currentModel,
166173
useTools: false,
167174
// use word chunking for

components/remix-chat/chat.tsx

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client"
22

3-
import { useCallback, useEffect, useState } from "react"
3+
import { useCallback, useEffect, useMemo, useState } from "react"
44
import type { Attachment, ChatRequestOptions, Message } from "ai"
55
import { useChat } from "ai/react"
66
import { AnimatePresence } from "framer-motion"
@@ -78,8 +78,27 @@ export function Chat({
7878
codingModel ?? findFirstAvailableModel()
7979
)
8080

81+
const textModelConfig = useMemo(() => {
82+
const textModel = findAvailableModel(TaskType.Translation)
83+
if (textModel) {
84+
try {
85+
return getConfigByModel(textModel)
86+
} catch (error) {
87+
return undefined
88+
}
89+
}
90+
return undefined
91+
}, [findAvailableModel, getConfigByModel])
92+
93+
const config = useMemo(() => {
94+
try {
95+
return getConfigByModel(aiModel)
96+
} catch (error) {
97+
return {}
98+
}
99+
}, [aiModel, getConfigByModel])
100+
81101
const { space } = useCurrentPathInfo()
82-
const textModel = findAvailableModel(TaskType.Translation)
83102
const {
84103
messages,
85104
setMessages,
@@ -93,16 +112,14 @@ export function Chat({
93112
reload,
94113
} = useChat({
95114
body: {
96-
...getConfigByModel(aiModel),
115+
...config,
97116
systemPrompt: remixPrompt,
98117
id,
99118
model: aiModel,
100119
space,
101120
projectId: scriptId,
102121
useTools: false,
103-
textModel: textModel
104-
? getConfigByModel(findAvailableModel(TaskType.Translation))
105-
: undefined,
122+
textModel: textModelConfig,
106123
},
107124
initialMessages,
108125
onFinish: () => {

0 commit comments

Comments
 (0)