Commit cf202c6a by liimin

refactor(chat): 优化预设问题自动发送逻辑

- 使用新状态 autoSendQuestion 替代直接使用 presuppositionQuestion
- 在重置会话时更新 autoSendQuestion 和 isAutoSend
- 优化了消息发送的条件判断
1 parent 3e85bc4f
......@@ -4,7 +4,7 @@
* @Author: limin
* @Date: 2025-04-16 18:17:38
* @LastEditors: limin
* @LastEditTime: 2025-07-25 19:24:40
* @LastEditTime: 2025-07-28 17:41:15
* @FilePath: \ai-assistant\app\components\base\chat\chat-with-history\chat-wrapper.tsx
*/
......@@ -19,7 +19,6 @@ import {
ChatRespondingProvider,
} from '@/app/components/base/chat/chat-with-history/context'
import React from 'react'
enum MessageListenerType {
RESET_CHAT = 'reset_chat',
}
......@@ -42,6 +41,7 @@ type Props = {
}
const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) => {
const [isAutoSend, setAutoSend] = useState<boolean>(true)
const [autoSendQuestion, setAutoSendQuestion] = useState<string | undefined>(presuppositionQuestion)
const {
config,
chatList,
......@@ -56,21 +56,23 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) =
handleNewConversation,
} = useChat(inputs)
useEffect(() => {
if (presuppositionQuestion && config.initial_message !== undefined && isAutoSend) {
if (autoSendQuestion && config.initial_message !== undefined && isAutoSend) {
const timer = setTimeout(() => {
sendMessage(presuppositionQuestion, InputMessageType.BY_AUTO)
sendMessage(autoSendQuestion, InputMessageType.BY_AUTO)
setAutoSend(false)
}, 0)
return () => clearTimeout(timer)
}
}, [presuppositionQuestion, config.initial_message, isAutoSend])
}, [autoSendQuestion, config.initial_message, isAutoSend])
const handleGlobalMessage = useCallback((data: MessageListenerProps) => {
try {
switch (data.type) {
switch (data?.type) {
case MessageListenerType.RESET_CHAT:
handleNewConversation(undefined, false)
data.payload.message && sendMessage(data.payload.message, InputMessageType.BY_AUTO)
handleNewConversation(undefined, false) // 重新开启一次会话
const message = data?.payload?.message
setAutoSendQuestion(message) //可能没有预设消息,但是也要更新 重置对话的时候也要保持没有预设消息
message && setAutoSend(true) // 有预设消息才自动发送
break
}
} catch (error) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!