Commit aa90f3a8 by Liu

feat:存在历史记录时则不自动调用提问接口

parent 7bc5496b
......@@ -34,6 +34,8 @@ export const TacticsChat: React.FC = () => {
const [historyDividerTime, setHistoryDividerTime] = useState<string | null>(null)
const [hasCleared, setHasCleared] = useState(false)
const [showClearConfirm, setShowClearConfirm] = useState(false)
// 标记当前会话是否已有历史记录;null 表示尚未完成查询
const [hasHistory, setHasHistory] = useState<boolean | null>(null)
const dispatch = useAppDispatch()
const {
shouldSendQuestion: shouldSendQuestionFromState,
......@@ -484,11 +486,11 @@ export const TacticsChat: React.FC = () => {
try {
const res = await fetchTacticsQaRecordPage(conversationId)
const qaRecords = res.data || []
const hasHistoryFlag = qaRecords.length > 0
// 始终添加 system 角色作为欢迎语
const messages = [{ role: 'system' } as ChatRecord, ...processApiResponse(qaRecords)]
const hasHistory = qaRecords.length > 0
setHistoryDividerIndex(hasHistory ? messages.length : null)
setHistoryDividerTime(hasHistory ? formatCurrentTime() : null)
setHistoryDividerIndex(hasHistoryFlag ? messages.length : null)
setHistoryDividerTime(hasHistoryFlag ? formatCurrentTime() : null)
// 处理历史记录中的参考文档标记
const processedMessages = messages.map((item) => {
if (item.role === 'ai' && item.answerList?.[0]?.answer) {
......@@ -520,6 +522,7 @@ export const TacticsChat: React.FC = () => {
return processedMessages
})
setHasCleared(false)
setHasHistory(hasHistoryFlag)
}
catch {
// 如果获取失败,至少显示欢迎语
......@@ -537,6 +540,8 @@ export const TacticsChat: React.FC = () => {
setHasCleared(false)
setHistoryDividerIndex(null)
setHistoryDividerTime(null)
// 拉取失败时视为无历史记录,后续可按需触发自动提问
setHasHistory(false)
}
finally {
setIsLoading(false)
......@@ -625,6 +630,8 @@ export const TacticsChat: React.FC = () => {
currentIdRef.current = id
lastSentQuestionRef.current = ''
// 每次切换会话时重置历史标记,等待新会话的历史查询结果
setHasHistory(null)
getUserQaRecordPage(id)
}
else {
......@@ -633,6 +640,7 @@ export const TacticsChat: React.FC = () => {
setHistoryDividerIndex(null)
setHistoryDividerTime(null)
setIsLoading(false)
setHasHistory(false)
}
}, [id, getUserQaRecordPage, dispatch])
......@@ -650,7 +658,8 @@ export const TacticsChat: React.FC = () => {
}
}, [id])
useEffect(() => {
if (currentIdRef.current && !isLoading && !hasAutoSubmittedRef.current) {
// 仅在历史记录查询完成且为空时,才自动触发一次提交
if (currentIdRef.current && !isLoading && hasHistory === false && !hasAutoSubmittedRef.current) {
hasAutoSubmittedRef.current = true
// 进入会话后自动触发一次提交:
// - 无 userMeta:沿用原逻辑,recordType=A02,携带 tacticsMeta
......@@ -685,7 +694,7 @@ export const TacticsChat: React.FC = () => {
)
}
}
}, [isLoading, handleSubmitQuestion, id, userMeta, getNumberTypeWithUserMeta])
}, [isLoading, handleSubmitQuestion, id, userMeta, getNumberTypeWithUserMeta, hasHistory])
// 创建新会话成功后跳转到新会话页面
useEffect(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment