Commit cf202c6a by liimin

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

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