mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-05 18:40:52 +08:00
chore: use query to manage last run to decrease the code complex
This commit is contained in:
parent
02110b4323
commit
82aca36fc1
@ -154,7 +154,6 @@ const BasePanel: FC<BasePanelProps> = ({
|
|||||||
setSingleRunParams,
|
setSingleRunParams,
|
||||||
setRunInputData,
|
setRunInputData,
|
||||||
hasLastRunData,
|
hasLastRunData,
|
||||||
isDataFromHistory,
|
|
||||||
handleRun,
|
handleRun,
|
||||||
getExistVarValuesInForms,
|
getExistVarValuesInForms,
|
||||||
getFilteredExistVarForms,
|
getFilteredExistVarForms,
|
||||||
@ -304,7 +303,7 @@ const BasePanel: FC<BasePanelProps> = ({
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{tabType === TabType.lastRun && (
|
{tabType === TabType.lastRun && (
|
||||||
<LastRun appId={appDetail?.id || ''} nodeId={id} runningStatus={runningStatus} isDataFromHistory={isDataFromHistory} />
|
<LastRun appId={appDetail?.id || ''} nodeId={id} runningStatus={runningStatus} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -1,56 +1,37 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import ResultPanel from '@/app/components/workflow/run/result-panel'
|
import ResultPanel from '@/app/components/workflow/run/result-panel'
|
||||||
import { useWorkflowStore } from '@/app/components/workflow/store'
|
|
||||||
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
import { NodeRunningStatus } from '@/app/components/workflow/types'
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import React, { useEffect, useState } from 'react'
|
import React from 'react'
|
||||||
import NoData from './no-data'
|
import NoData from './no-data'
|
||||||
import { useLastRun } from '@/service/use-workflow'
|
import { useLastRun } from '@/service/use-workflow'
|
||||||
import Loading from '@/app/components/base/loading'
|
import Loading from '@/app/components/base/loading'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
isDataFromHistory: boolean
|
|
||||||
appId: string
|
appId: string
|
||||||
nodeId: string
|
nodeId: string
|
||||||
runningStatus?: NodeRunningStatus
|
runningStatus?: NodeRunningStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
const LastRun: FC<Props> = ({
|
const LastRun: FC<Props> = ({
|
||||||
isDataFromHistory,
|
|
||||||
appId,
|
appId,
|
||||||
nodeId,
|
nodeId,
|
||||||
runningStatus,
|
runningStatus,
|
||||||
}) => {
|
}) => {
|
||||||
const workflowStore = useWorkflowStore()
|
|
||||||
|
|
||||||
const {
|
|
||||||
getLastRunNodeInfo,
|
|
||||||
} = workflowStore.getState()
|
|
||||||
const { data: runResultFromHistory, isFetching } = useLastRun(appId, nodeId, isDataFromHistory)
|
|
||||||
const [runResultFromSingleRun, setRunResult] = useState(isDataFromHistory ? getLastRunNodeInfo(nodeId) : null)
|
|
||||||
const runResult = isDataFromHistory ? runResultFromHistory : runResultFromSingleRun
|
|
||||||
const isRunning = runningStatus === NodeRunningStatus.Running
|
const isRunning = runningStatus === NodeRunningStatus.Running
|
||||||
|
const { data: runResult, isFetching } = useLastRun(appId, nodeId, !isRunning)
|
||||||
// get data from current running result
|
|
||||||
useEffect(() => {
|
|
||||||
if (isDataFromHistory)
|
|
||||||
return
|
|
||||||
|
|
||||||
setRunResult(getLastRunNodeInfo(nodeId))
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [runningStatus, isDataFromHistory])
|
|
||||||
|
|
||||||
const handleSingleRun = () => {
|
const handleSingleRun = () => {
|
||||||
console.log('run')
|
console.log('run')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDataFromHistory && isFetching)
|
if (isFetching)
|
||||||
return <Loading />
|
return <Loading />
|
||||||
|
|
||||||
if (isRunning)
|
if (isRunning)
|
||||||
return <ResultPanel status='running' showSteps={false} />
|
return <ResultPanel status='running' showSteps={false} />
|
||||||
|
|
||||||
if (!runResultFromSingleRun) {
|
if (!runResult) {
|
||||||
return (
|
return (
|
||||||
<NoData onSingleRun={handleSingleRun} />
|
<NoData onSingleRun={handleSingleRun} />
|
||||||
)
|
)
|
||||||
|
@ -29,10 +29,8 @@ const useLastRun = <T>({
|
|||||||
setSingleRunParams(childPanelRef.current?.singleRunParams)
|
setSingleRunParams(childPanelRef.current?.singleRunParams)
|
||||||
}, [doSetRunInputData])
|
}, [doSetRunInputData])
|
||||||
|
|
||||||
const [isDataFromHistory, setIsDataFromHistory] = useState(true)
|
|
||||||
const [tabType, setTabType] = useState<TabType>(TabType.settings)
|
const [tabType, setTabType] = useState<TabType>(TabType.settings)
|
||||||
const handleRun = async (data: Record<string, any>) => {
|
const handleRun = async (data: Record<string, any>) => {
|
||||||
setIsDataFromHistory(false)
|
|
||||||
setTabType(TabType.lastRun)
|
setTabType(TabType.lastRun)
|
||||||
callRunApi(data)
|
callRunApi(data)
|
||||||
hideSingleRun()
|
hideSingleRun()
|
||||||
@ -40,7 +38,6 @@ const useLastRun = <T>({
|
|||||||
|
|
||||||
const handleTabClicked = useCallback((type: TabType) => {
|
const handleTabClicked = useCallback((type: TabType) => {
|
||||||
setTabType(type)
|
setTabType(type)
|
||||||
setIsDataFromHistory(true)
|
|
||||||
}, [])
|
}, [])
|
||||||
const hasLastRunData = true // TODO: add disabled logic
|
const hasLastRunData = true // TODO: add disabled logic
|
||||||
|
|
||||||
@ -109,7 +106,6 @@ const useLastRun = <T>({
|
|||||||
setSingleRunParams,
|
setSingleRunParams,
|
||||||
setRunInputData,
|
setRunInputData,
|
||||||
hasLastRunData,
|
hasLastRunData,
|
||||||
isDataFromHistory,
|
|
||||||
handleRun,
|
handleRun,
|
||||||
getExistVarValuesInForms,
|
getExistVarValuesInForms,
|
||||||
getFilteredExistVarForms,
|
getFilteredExistVarForms,
|
||||||
|
@ -50,6 +50,7 @@ const { checkValid: checkLoopValid } = LoopDefault
|
|||||||
import {
|
import {
|
||||||
useStoreApi,
|
useStoreApi,
|
||||||
} from 'reactflow'
|
} from 'reactflow'
|
||||||
|
import { useInvalidLastRun } from '@/service/use-workflow'
|
||||||
// eslint-disable-next-line ts/no-unsafe-function-type
|
// eslint-disable-next-line ts/no-unsafe-function-type
|
||||||
const checkValidFns: Record<BlockEnum, Function> = {
|
const checkValidFns: Record<BlockEnum, Function> = {
|
||||||
[BlockEnum.LLM]: checkLLMValid,
|
[BlockEnum.LLM]: checkLLMValid,
|
||||||
@ -158,20 +159,19 @@ const useOneStepRun = <T>({
|
|||||||
const store = useStoreApi()
|
const store = useStoreApi()
|
||||||
const workflowStore = useWorkflowStore()
|
const workflowStore = useWorkflowStore()
|
||||||
const {
|
const {
|
||||||
|
|
||||||
setLastRunNodeInfo,
|
|
||||||
appendNodeInspectVars,
|
appendNodeInspectVars,
|
||||||
setShowSingleRunPanel,
|
setShowSingleRunPanel,
|
||||||
} = workflowStore.getState()
|
} = workflowStore.getState()
|
||||||
|
const invalidLastRun = useInvalidLastRun(appId!, id)
|
||||||
const [runResult, doSetRunResult] = useState<NodeRunResult | null>(null)
|
const [runResult, doSetRunResult] = useState<NodeRunResult | null>(null)
|
||||||
const setRunResult = useCallback(async (data: NodeRunResult | null) => {
|
const setRunResult = useCallback(async (data: NodeRunResult | null) => {
|
||||||
doSetRunResult(data)
|
doSetRunResult(data)
|
||||||
setLastRunNodeInfo(id, data!)
|
invalidLastRun()
|
||||||
const vars = await fetchNodeInspectVars(appId!, data!.id)
|
const vars = await fetchNodeInspectVars(appId!, data!.id)
|
||||||
const { getNodes } = store.getState()
|
const { getNodes } = store.getState()
|
||||||
const nodes = getNodes()
|
const nodes = getNodes()
|
||||||
appendNodeInspectVars(id, vars, nodes)
|
appendNodeInspectVars(id, vars, nodes)
|
||||||
}, [setLastRunNodeInfo, id, appId, store, appendNodeInspectVars])
|
}, [invalidLastRun, appId, store, appendNodeInspectVars, id])
|
||||||
|
|
||||||
const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate()
|
const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate()
|
||||||
const [canShowSingleRun, setCanShowSingleRun] = useState(false)
|
const [canShowSingleRun, setCanShowSingleRun] = useState(false)
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
import produce from 'immer'
|
|
||||||
import type { StateCreator } from 'zustand'
|
|
||||||
import type { NodeRunResult } from '@/types/workflow'
|
|
||||||
|
|
||||||
type LastRunState = {
|
|
||||||
nodes: NodeRunResult[]
|
|
||||||
}
|
|
||||||
|
|
||||||
type LastRunActions = {
|
|
||||||
setLastRunInfos: (vars: NodeRunResult[]) => void
|
|
||||||
getLastRunInfos: () => NodeRunResult[]
|
|
||||||
setLastRunNodeInfo: (nodeId: string, payload: NodeRunResult) => void
|
|
||||||
getLastRunNodeInfo: (nodeId: string) => NodeRunResult | undefined
|
|
||||||
getLastRunVar: (nodeId: string, key: string) => any
|
|
||||||
}
|
|
||||||
|
|
||||||
export type LastRunSliceShape = LastRunState & LastRunActions
|
|
||||||
|
|
||||||
export const createLastRunSlice: StateCreator<LastRunSliceShape> = (set, get) => {
|
|
||||||
return ({
|
|
||||||
nodes: [],
|
|
||||||
setLastRunInfos: (vars) => {
|
|
||||||
set(() => ({
|
|
||||||
nodes: vars,
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
getLastRunInfos: () => {
|
|
||||||
return get().nodes
|
|
||||||
},
|
|
||||||
clearVars: () => {
|
|
||||||
set(() => ({
|
|
||||||
nodes: [],
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
setLastRunNodeInfo: (nodeId, payload) => {
|
|
||||||
set((state) => {
|
|
||||||
const prevNodes = state.nodes
|
|
||||||
const nodes = produce(prevNodes, (draft) => {
|
|
||||||
const index = prevNodes.findIndex(node => node.id === nodeId)
|
|
||||||
if (index === -1)
|
|
||||||
draft.push(payload)
|
|
||||||
else
|
|
||||||
draft[index] = payload
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
nodes,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
getLastRunNodeInfo: (nodeId) => {
|
|
||||||
const nodes = get().nodes
|
|
||||||
return nodes.find(node => node.node_id === nodeId)
|
|
||||||
},
|
|
||||||
getLastRunVar: (nodeId, key) => {
|
|
||||||
const node = get().getLastRunNodeInfo(nodeId)
|
|
||||||
if (!node)
|
|
||||||
return undefined
|
|
||||||
|
|
||||||
const varItem = node
|
|
||||||
if (!varItem)
|
|
||||||
return undefined
|
|
||||||
|
|
||||||
return varItem.outputs?.[key]
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
@ -28,8 +28,6 @@ import type { WorkflowDraftSliceShape } from './workflow-draft-slice'
|
|||||||
import { createWorkflowDraftSlice } from './workflow-draft-slice'
|
import { createWorkflowDraftSlice } from './workflow-draft-slice'
|
||||||
import type { WorkflowSliceShape } from './workflow-slice'
|
import type { WorkflowSliceShape } from './workflow-slice'
|
||||||
import { createWorkflowSlice } from './workflow-slice'
|
import { createWorkflowSlice } from './workflow-slice'
|
||||||
import type { LastRunSliceShape } from './debug/last-run-slice'
|
|
||||||
import { createLastRunSlice } from './debug/last-run-slice'
|
|
||||||
import type { InspectVarsSliceShape } from './debug/inspect-vars-slice'
|
import type { InspectVarsSliceShape } from './debug/inspect-vars-slice'
|
||||||
import { createInspectVarsSlice } from './debug/inspect-vars-slice'
|
import { createInspectVarsSlice } from './debug/inspect-vars-slice'
|
||||||
|
|
||||||
@ -50,7 +48,6 @@ export type Shape =
|
|||||||
VersionSliceShape &
|
VersionSliceShape &
|
||||||
WorkflowDraftSliceShape &
|
WorkflowDraftSliceShape &
|
||||||
WorkflowSliceShape &
|
WorkflowSliceShape &
|
||||||
LastRunSliceShape &
|
|
||||||
InspectVarsSliceShape &
|
InspectVarsSliceShape &
|
||||||
LayoutSliceShape &
|
LayoutSliceShape &
|
||||||
WorkflowAppSliceShape
|
WorkflowAppSliceShape
|
||||||
@ -74,7 +71,6 @@ export const createWorkflowStore = (params: CreateWorkflowStoreParams) => {
|
|||||||
...createVersionSlice(...args),
|
...createVersionSlice(...args),
|
||||||
...createWorkflowDraftSlice(...args),
|
...createWorkflowDraftSlice(...args),
|
||||||
...createWorkflowSlice(...args),
|
...createWorkflowSlice(...args),
|
||||||
...createLastRunSlice(...args),
|
|
||||||
...createInspectVarsSlice(...args),
|
...createInspectVarsSlice(...args),
|
||||||
...createLayoutSlice(...args),
|
...createLayoutSlice(...args),
|
||||||
...(injectWorkflowStoreSliceFn?.(...args) || {} as WorkflowAppSliceShape),
|
...(injectWorkflowStoreSliceFn?.(...args) || {} as WorkflowAppSliceShape),
|
||||||
|
@ -89,11 +89,13 @@ export const usePublishWorkflow = (appId: string) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const useLastRunKey = [NAME_SPACE, 'last-run']
|
||||||
export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => {
|
export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => {
|
||||||
return useQuery<NodeTracing>({
|
return useQuery<NodeTracing>({
|
||||||
enabled,
|
enabled,
|
||||||
queryKey: [NAME_SPACE, 'last-run', appID, nodeId],
|
queryKey: [...useLastRunKey, appID, nodeId],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
|
console.log(`fetch last run : ${nodeId}`)
|
||||||
// TODO: mock data
|
// TODO: mock data
|
||||||
await sleep(1000)
|
await sleep(1000)
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
@ -125,6 +127,15 @@ export const useLastRun = (appID: string, nodeId: string, enabled: boolean) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const useInvalidLastRun = (appId: string, nodeId: string) => {
|
||||||
|
return useInvalid([NAME_SPACE, 'last-run', appId, nodeId])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rerun workflow or change the version of workflow
|
||||||
|
export const useInvalidAllLastRun = (appId: string) => {
|
||||||
|
return useInvalid([NAME_SPACE, 'last-run', appId])
|
||||||
|
}
|
||||||
|
|
||||||
const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable']
|
const useConversationVarValuesKey = [NAME_SPACE, 'conversation-variable']
|
||||||
|
|
||||||
export const useConversationVarValues = (appId: string) => {
|
export const useConversationVarValues = (appId: string) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user