From b3b9e1dabb93cd3cc5c8dd3f53a7c9748b033291 Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 11 Mar 2024 17:33:17 +0800 Subject: [PATCH] feat: tools support run --- .../nodes/_base/hooks/use-one-step-run.ts | 1 + .../components/workflow/nodes/tool/panel.tsx | 18 ++++++ .../workflow/nodes/tool/use-config.ts | 59 ++++++++++++++++++- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts index 9ac071a78a..849244b25d 100644 --- a/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts +++ b/web/app/components/workflow/nodes/_base/hooks/use-one-step-run.ts @@ -25,6 +25,7 @@ const useOneStepRun = ({ id, data, defaultRunInputData, isInvalid = () => tru const [runningStatus, setRunningStatus] = useState('un started') const handleRun = () => { + // console.log(runInputData) if (isInvalid()) return diff --git a/web/app/components/workflow/nodes/tool/panel.tsx b/web/app/components/workflow/nodes/tool/panel.tsx index 0f1326f324..56aaae62fb 100644 --- a/web/app/components/workflow/nodes/tool/panel.tsx +++ b/web/app/components/workflow/nodes/tool/panel.tsx @@ -11,6 +11,7 @@ import type { NodePanelProps } from '@/app/components/workflow/types' import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form' import ConfigCredential from '@/app/components/tools/setting/build-in/config-credentials' import Loading from '@/app/components/base/loading' +import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form' const i18nPrefix = 'workflow.nodes.tool' @@ -35,6 +36,12 @@ const Panel: FC> = ({ hideSetAuthModal, handleSaveAuth, isLoading, + isShowSingleRun, + hideSingleRun, + singleRunForms, + runningStatus, + handleRun, + handleStop, } = useConfig(id, data) if (isLoading) { @@ -101,6 +108,17 @@ const Panel: FC> = ({ isHideRemoveBtn /> )} + + {isShowSingleRun && ( + + )} ) } diff --git a/web/app/components/workflow/nodes/tool/use-config.ts b/web/app/components/workflow/nodes/tool/use-config.ts index 64675a560b..4fc9cc92fe 100644 --- a/web/app/components/workflow/nodes/tool/use-config.ts +++ b/web/app/components/workflow/nodes/tool/use-config.ts @@ -3,20 +3,27 @@ import { useTranslation } from 'react-i18next' import produce from 'immer' import { useBoolean } from 'ahooks' import { useStore } from '../../store' -import type { ToolNodeType, ToolVarInput } from './types' +import { type ToolNodeType, type ToolVarInput } from './types' +import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import { CollectionType } from '@/app/components/tools/types' import type { Collection, Tool } from '@/app/components/tools/types' import { fetchBuiltInToolList, fetchCollectionList, fetchCustomToolList, updateBuiltInToolCredential } from '@/service/tools' import { addDefaultValue, toolParametersToFormSchemas } from '@/app/components/tools/utils/to-form-schema' import Toast from '@/app/components/base/toast' +import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form' +import { InputVarType } from '@/app/components/workflow/types' +import type { InputVar } from '@/app/components/workflow/types' +import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' const useConfig = (id: string, payload: ToolNodeType) => { const { t } = useTranslation() + const language = useLanguage() const toolsMap = useStore(s => s.toolsMap) const setToolsMap = useStore(s => s.setToolsMap) const { inputs, setInputs } = useNodeCrud(id, payload) + const toolInputs = inputs.tool_inputs const { provider_id, provider_name, provider_type, tool_name, tool_parameters } = inputs const isBuiltIn = provider_type === CollectionType.builtIn const [currCollection, setCurrCollection] = useState(null) @@ -99,6 +106,48 @@ const useConfig = (id: string, payload: ToolNodeType) => { })() }, [provider_name]) + // single run + const { + isShowSingleRun, + hideSingleRun, + runningStatus, + setRunInputData, + handleRun, + handleStop, + } = useOneStepRun({ + id, + data: inputs, + defaultRunInputData: {}, + }) + + const [inputVarValues, doSetInputVarValues] = useState>({}) + const setInputVarValues = (value: Record) => { + doSetInputVarValues(value) + setRunInputData(value) + } + + const singleRunForms = (() => { + const formInputs: InputVar[] = [] + toolInputVarSchema.forEach((item: any) => { + const targetItem = toolInputs.find(input => input.variable === item.variable) + // TODO: support selector + // if (targetItem?.variable_type === VarType.selector) { + formInputs.push({ + label: item.label[language] || item.label.en_US, + variable: item.variable, + type: InputVarType.textInput, + required: item.required, + }) + // } + }) + const forms: FormProps[] = [{ + inputs: formInputs, + values: inputVarValues, + onChange: setInputVarValues, + }] + return forms + })() + return { inputs, currTool, @@ -114,6 +163,14 @@ const useConfig = (id: string, payload: ToolNodeType) => { hideSetAuthModal, handleSaveAuth, isLoading, + isShowSingleRun, + hideSingleRun, + inputVarValues, + setInputVarValues, + singleRunForms, + runningStatus, + handleRun, + handleStop, } }