fix(workflow): restore fetchInspectVars and invalidAllLastRun calls lost during refactor

The previous commit (a86765e2b6) accidentally dropped these calls when splitting
useChat hook into modules. Also add race condition protection using runId to
prevent stale callbacks from updating state after a new run has started.
This commit is contained in:
yyh
2026-01-26 21:55:16 +08:00
parent a86765e2b6
commit fb9a6bbc9f
2 changed files with 21 additions and 3 deletions

View File

@ -3,13 +3,15 @@ import type { SendCallback, SendParams, UpdateCurrentQAParams } from './types'
import type { InputForm } from '@/app/components/base/chat/chat/type'
import type { ChatItem, ChatItemInTree, Inputs } from '@/app/components/base/chat/types'
import { uniqBy } from 'es-toolkit/compat'
import { useCallback } from 'react'
import { useCallback, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { getProcessedInputs } from '@/app/components/base/chat/chat/utils'
import { getProcessedFiles, getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
import { useToastContext } from '@/app/components/base/toast'
import { useInvalidAllLastRun } from '@/service/use-workflow'
import { TransferMethod } from '@/types/app'
import { useWorkflowRun } from '../../../hooks'
import { useSetWorkflowVarsWithValue, useWorkflowRun } from '../../../hooks'
import { useHooksStore } from '../../../hooks-store'
import { useStore, useWorkflowStore } from '../../../store'
import { createWorkflowEventHandlers } from './use-workflow-event-handlers'
@ -46,10 +48,15 @@ export function useChatMessageSender({
const { handleRun } = useWorkflowRun()
const workflowStore = useWorkflowStore()
const configsMap = useHooksStore(s => s.configsMap)
const invalidAllLastRun = useInvalidAllLastRun(configsMap?.flowType, configsMap?.flowId)
const { fetchInspectVars } = useSetWorkflowVarsWithValue()
const setConversationId = useStore(s => s.setConversationId)
const setTargetMessageId = useStore(s => s.setTargetMessageId)
const setSuggestedQuestions = useStore(s => s.setSuggestedQuestions)
const activeRunIdRef = useRef(0)
const handleSend = useCallback((
params: SendParams,
{ onGetSuggestedQuestions }: SendCallback,
@ -59,6 +66,9 @@ export function useChatMessageSender({
return false
}
const runId = ++activeRunIdRef.current
const isCurrentRun = () => runId === activeRunIdRef.current
const parentMessage = threadMessages.find(item => item.id === params.parent_message_id)
const placeholderQuestionId = `question-${Date.now()}`
@ -158,7 +168,11 @@ export function useChatMessageSender({
})
},
async onCompleted(hasError?: boolean, errorMessage?: string) {
if (!isCurrentRun())
return
handleResponding(false)
fetchInspectVars({})
invalidAllLastRun()
if (hasError) {
if (errorMessage) {
@ -203,6 +217,8 @@ export function useChatMessageSender({
responseItem.content = messageReplace.answer
},
onError() {
if (!isCurrentRun())
return
handleResponding(false)
},
onWorkflowStarted: (event) => {
@ -231,6 +247,8 @@ export function useChatMessageSender({
setTargetMessageId,
setConversationId,
setSuggestedQuestions,
fetchInspectVars,
invalidAllLastRun,
workflowStore,
hasStopResponded,
taskIdRef,

View File

@ -19,11 +19,11 @@ export function useChat(
stopChat?: (taskId: string) => void,
) {
const chatTree = useStore(s => s.chatTree)
const updateChatTree = useStore(s => s.updateChatTree)
const conversationId = useStore(s => s.conversationId)
const isResponding = useStore(s => s.isResponding)
const suggestedQuestions = useStore(s => s.suggestedQuestions)
const targetMessageId = useStore(s => s.targetMessageId)
const updateChatTree = useStore(s => s.updateChatTree)
const setTargetMessageId = useStore(s => s.setTargetMessageId)
const initialChatTreeRef = useRef(prevChatTree)