diff --git a/web/app/components/workflow/index.tsx b/web/app/components/workflow/index.tsx index f748aceff5..30635bb255 100644 --- a/web/app/components/workflow/index.tsx +++ b/web/app/components/workflow/index.tsx @@ -102,6 +102,7 @@ import Confirm from '@/app/components/base/confirm' import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants' import { fetchFileUploadConfig } from '@/service/common' import DatasetsDetailProvider from './datasets-detail-store/provider' +import LastRunProvider from './last-run-store/provider' import CurrentVarsProvider from './current-vars-store/provider' const nodeTypes = { @@ -454,13 +455,15 @@ const WorkflowWrap = memo(() => { edges={edgesData} > - - - + + + + + diff --git a/web/app/components/workflow/last-run-store/provider.tsx b/web/app/components/workflow/last-run-store/provider.tsx new file mode 100644 index 0000000000..7f2ef16923 --- /dev/null +++ b/web/app/components/workflow/last-run-store/provider.tsx @@ -0,0 +1,30 @@ +import type { FC } from 'react' +import { createContext, useRef } from 'react' +import { createLastRunStore } from './store' + +type LastRunStoreApi = ReturnType + +type LastRunContextType = LastRunStoreApi | undefined + +export const LastRunContext = createContext(undefined) + +type LastRunProviderProps = { + children: React.ReactNode +} + +const LastRunProvider: FC = ({ + children, +}) => { + const storeRef = useRef() + + if (!storeRef.current) + storeRef.current = createLastRunStore() + + return ( + + {children} + + ) +} + +export default LastRunProvider diff --git a/web/app/components/workflow/last-run-store/store.ts b/web/app/components/workflow/last-run-store/store.ts new file mode 100644 index 0000000000..c21ff7b61c --- /dev/null +++ b/web/app/components/workflow/last-run-store/store.ts @@ -0,0 +1,67 @@ +import { useContext } from 'react' +import { createStore, useStore } from 'zustand' +import { LastRunContext } from './provider' + +type NodeInfo = { + id: string + name: string + type: string + vars: { + key: string + type: string + value: any + }[] +} & { + input: Record + output: Record +} + +type LastRunState = { + nodes: NodeInfo[] +} + +type LastRunActions = { + setInfos: (vars: NodeInfo[]) => void + getInfos: () => NodeInfo[] + getNodeInfo: (nodeId: string) => NodeInfo | undefined +} + +type LastRunStore = LastRunState & LastRunActions + +export const createLastRunStore = () => { + return createStore((set, get) => ({ + nodes: [{ + id: '', + name: '', + type: '', + vars: [], + input: {}, + output: {}, + }], + setInfos: (vars) => { + set(() => ({ + nodes: vars, + })) + }, + getInfos: () => { + return get().nodes + }, + clearVars: () => { + set(() => ({ + nodes: [], + })) + }, + getNodeInfo: (nodeId) => { + const nodes = get().nodes + return nodes.find(node => node.id === nodeId) + }, + })) +} + +export const useLastRunStore = (selector: (state: LastRunStore) => T): T => { + const store = useContext(LastRunContext) + if (!store) + throw new Error('Missing LastRunContext.Provider in the tree') + + return useStore(store, selector) +} diff --git a/web/app/components/workflow/nodes/llm/panel.tsx b/web/app/components/workflow/nodes/llm/panel.tsx index edffa65047..023d91e631 100644 --- a/web/app/components/workflow/nodes/llm/panel.tsx +++ b/web/app/components/workflow/nodes/llm/panel.tsx @@ -21,6 +21,7 @@ import ResultPanel from '@/app/components/workflow/run/result-panel' import Tooltip from '@/app/components/base/tooltip' import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor' import { useCurrentVarsStore } from '../../current-vars-store/store' +import { useLastRunStore } from '../../last-run-store/store' const i18nPrefix = 'workflow.nodes.llm' @@ -30,7 +31,8 @@ const Panel: FC> = ({ }) => { const { t } = useTranslation() const currentVars = useCurrentVarsStore(state => state.getVars()) - console.log(currentVars) + const lastRunInfo = useLastRunStore(state => state.getInfos()) + console.log(currentVars, lastRunInfo) const { readOnly, inputs,