From 0fa497499e1f49c919175b3b2a69979d14b73b93 Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 24 Apr 2025 18:27:32 +0800 Subject: [PATCH] chore: move get exist var to hooks --- .../components/before-run-form/index.tsx | 64 ++----------------- .../_base/components/workflow-panel/index.tsx | 18 +++++- .../workflow-panel/last-run/use-last-run.ts | 60 +++++++++++++++++ .../components/workflow/nodes/_base/node.tsx | 8 ++- 4 files changed, 88 insertions(+), 62 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/components/before-run-form/index.tsx b/web/app/components/workflow/nodes/_base/components/before-run-form/index.tsx index 2d81691c3c..94a8b45129 100644 --- a/web/app/components/workflow/nodes/_base/components/before-run-form/index.tsx +++ b/web/app/components/workflow/nodes/_base/components/before-run-form/index.tsx @@ -1,6 +1,6 @@ 'use client' import type { FC } from 'react' -import React, { useMemo } from 'react' +import React from 'react' import { useTranslation } from 'react-i18next' import { RiCloseLine, @@ -21,7 +21,6 @@ import type { BlockEnum } from '@/app/components/workflow/types' import type { Emoji } from '@/app/components/tools/types' import type { SpecialResultPanelProps } from '@/app/components/workflow/run/special-result-panel' import SpecialResultPanel from '@/app/components/workflow/run/special-result-panel' -import { useWorkflowStore } from '@/app/components/workflow/store' const i18nPrefix = 'workflow.singleRun' @@ -36,6 +35,8 @@ export type BeforeRunFormProps = { result?: React.JSX.Element forms: FormProps[] showSpecialResultPanel?: boolean + existVarValuesInForms: Record[] + filteredExistVarForms: FormProps[] } & Partial function formatValue(value: string | any, type: InputVarType) { @@ -70,64 +71,11 @@ const BeforeRunForm: FC = ({ result, forms, showSpecialResultPanel, + filteredExistVarForms, + existVarValuesInForms, ...restResultPanelParams }) => { const { t } = useTranslation() - console.log(forms) - - const workflowStore = useWorkflowStore() - const { - getInspectVar, - } = workflowStore.getState() - - const existVarValuesInForms = useMemo(() => { - const valuesArr = forms.map((form) => { - const values: Record = {} - form.inputs.forEach(({ variable }) => { - // #nodeId.path1?.path2?...# => [nodeId, path1] - // TODO: conversation vars and envs - const selector = variable.slice(1, -1).split('.') - const [nodeId, varName] = selector.slice(0, 2) - const inspectVarValue = getInspectVar(nodeId, varName) - if (inspectVarValue !== undefined) { - const subPathArr = selector.slice(2) - if (subPathArr.length > 0) { - let current = inspectVarValue.value - let invalid = false - subPathArr.forEach((subPath) => { - if (invalid) - return - - if (current && typeof current === 'object' && subPath in current) { - current = current[subPath] - return - } - invalid = true - }) - values[variable] = current - } - else { - values[variable] = inspectVarValue - } - } - }) - return values - }) - return valuesArr - }, [forms, getInspectVar]) - - const filteredExistVarForms = useMemo(() => { - const res = forms.map((form, i) => { - const existVarValuesInForm = existVarValuesInForms[i] - const newForm = { ...form } - const inputs = form.inputs.filter((input) => { - return !(input.variable in existVarValuesInForm) - }) - newForm.inputs = inputs - return newForm - }).filter(form => form.inputs.length > 0) - return res - }, [forms, existVarValuesInForms]) const isFinished = runningStatus === NodeRunningStatus.Succeeded || runningStatus === NodeRunningStatus.Failed || runningStatus === NodeRunningStatus.Exception const isRunning = runningStatus === NodeRunningStatus.Running @@ -180,7 +128,7 @@ const BeforeRunForm: FC = ({ form.inputs.forEach((input) => { if (input.variable in existVarValuesInForm) { - // TODO: if is the big value, should fetch value from server + // TODO: wait for api if need to pass exist var values submitData[input.variable] = existVarValuesInForm[input.variable] return } diff --git a/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx b/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx index 8bf536fb90..a378d51108 100644 --- a/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx +++ b/web/app/components/workflow/nodes/_base/components/workflow-panel/index.tsx @@ -156,6 +156,8 @@ const BasePanel: FC = ({ hasLastRunData, isDataFromHistory, handleRun, + getExistVarValuesInForms, + getFilteredExistVarForms, } = useLastRun({ id, data, @@ -206,8 +208,16 @@ const BasePanel: FC = ({ console.error('childPanelRef is not set') return } - setSingleRunParams(childPanelRef.current?.singleRunParams) - showSingleRun() + const filteredExistVarForms = getFilteredExistVarForms(childPanelRef.current?.singleRunParams.forms) + if (filteredExistVarForms.length > 0) { + setSingleRunParams(childPanelRef.current?.singleRunParams) + showSingleRun() + } + else { + // TODO: check valid + // TODO: all value is setted. wait for api if need to pass exist var values + handleRun({}) + } handleSyncWorkflowDraft(true) }} > @@ -307,7 +317,9 @@ const BasePanel: FC = ({ onRun={handleRun} onStop={handleStop} {...singleRunParams!} - result={
xxx
} + existVarValuesInForms={getExistVarValuesInForms(singleRunParams?.forms as any)} + filteredExistVarForms={getFilteredExistVarForms(singleRunParams?.forms as any)} + result={<>} /> ) } diff --git a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts index 08b1e494a8..8a6443ea50 100644 --- a/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts +++ b/web/app/components/workflow/nodes/_base/components/workflow-panel/last-run/use-last-run.ts @@ -4,6 +4,8 @@ import { useCallback, useRef, useState } from 'react' import type { PanelExposedType } from '@/types/workflow' import { TabType } from '../tab' import { sleep } from '@/utils' +import { useWorkflowStore } from '@/app/components/workflow/store' +import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form' type Params = OneStepRunParams const useLastRun = ({ @@ -42,6 +44,62 @@ const useLastRun = ({ }, []) const hasLastRunData = true // TODO: add disabled logic + const workflowStore = useWorkflowStore() + const { + getInspectVar, + } = workflowStore.getState() + const getExistVarValuesInForms = (forms: FormProps[]) => { + // if (!singleRunParams) + const valuesArr = forms.map((form) => { + const values: Record = {} + form.inputs.forEach(({ variable }) => { + // #nodeId.path1?.path2?...# => [nodeId, path1] + // TODO: conversation vars and envs + const selector = variable.slice(1, -1).split('.') + const [nodeId, varName] = selector.slice(0, 2) + const inspectVarValue = getInspectVar(nodeId, varName) + if (inspectVarValue !== undefined) { + const subPathArr = selector.slice(2) + if (subPathArr.length > 0) { + let current = inspectVarValue.value + let invalid = false + subPathArr.forEach((subPath) => { + if (invalid) + return + + if (current && typeof current === 'object' && subPath in current) { + current = current[subPath] + return + } + invalid = true + }) + values[variable] = current + } + else { + values[variable] = inspectVarValue + } + } + }) + return values + }) + return valuesArr + } + + const getFilteredExistVarForms = (forms: FormProps[]) => { + const existVarValuesInForms = getExistVarValuesInForms(forms) + + const res = forms.map((form, i) => { + const existVarValuesInForm = existVarValuesInForms[i] + const newForm = { ...form } + const inputs = form.inputs.filter((input) => { + return !(input.variable in existVarValuesInForm) + }) + newForm.inputs = inputs + return newForm + }).filter(form => form.inputs.length > 0) + return res + } + return { ...oneStepRunRes, childPanelRef, @@ -53,6 +111,8 @@ const useLastRun = ({ hasLastRunData, isDataFromHistory, handleRun, + getExistVarValuesInForms, + getFilteredExistVarForms, } } diff --git a/web/app/components/workflow/nodes/_base/node.tsx b/web/app/components/workflow/nodes/_base/node.tsx index 527b2f094d..6899bf53a7 100644 --- a/web/app/components/workflow/nodes/_base/node.tsx +++ b/web/app/components/workflow/nodes/_base/node.tsx @@ -44,6 +44,7 @@ import AddVariablePopupWithPosition from './components/add-variable-popup-with-p import cn from '@/utils/classnames' import BlockIcon from '@/app/components/workflow/block-icon' import Tooltip from '@/app/components/base/tooltip' +import { useWorkflowStore } from '../../store' type BaseNodeProps = { children: ReactElement @@ -128,6 +129,11 @@ const BaseNode: FC = ({ return null }, [data._loopIndex, data._runningStatus, t]) + const workflowStore = useWorkflowStore() + const { + hasNodeInspectVars, + } = workflowStore.getState() + return (
= ({ ) } { - data._runningStatus === NodeRunningStatus.Succeeded && ( + (data._runningStatus === NodeRunningStatus.Succeeded || hasNodeInspectVars(id)) && ( ) }