feat: more params for tools params support

This commit is contained in:
Joel 2025-05-07 14:21:02 +08:00
parent 47ecf856fb
commit 17f0dde2c2
11 changed files with 201 additions and 158 deletions

View File

@ -79,8 +79,6 @@ const BeforeRunForm: FC<BeforeRunFormProps> = ({
}) => {
const { t } = useTranslation()
// useWhyDidYouUpdate('BeforeRunForm', { nodeName, nodeType, toolIcon, onHide, onRun, onStop, runningStatus, result, forms, showSpecialResultPanel, filteredExistVarForms, existVarValuesInForms, ...restResultPanelParams })
const isFinished = runningStatus === NodeRunningStatus.Succeeded || runningStatus === NodeRunningStatus.Failed || runningStatus === NodeRunningStatus.Exception
const isRunning = runningStatus === NodeRunningStatus.Running
const isFileLoaded = (() => {

View File

@ -44,7 +44,7 @@ import {
hasRetryNode,
} from '@/app/components/workflow/utils'
import Tooltip from '@/app/components/base/tooltip'
import type { Node } from '@/app/components/workflow/types'
import { BlockEnum, type Node } from '@/app/components/workflow/types'
import { useStore as useAppStore } from '@/app/components/app/store'
import { useStore } from '@/app/components/workflow/store'
import Tab, { TabType } from './tab'
@ -53,6 +53,7 @@ import useLastRun from './last-run/use-last-run'
import BeforeRunForm from '../before-run-form'
import { debounce } from 'lodash-es'
import { NODES_EXTRA_DATA } from '@/app/components/workflow/constants'
import { useLogs } from '@/app/components/workflow/run/hooks'
type BasePanelProps = {
children: ReactNode
@ -150,6 +151,7 @@ const BasePanel: FC<BasePanelProps> = ({
tabType,
setTabType,
singleRunParams,
nodeInfo,
setRunInputData,
hasLastRunData,
handleSingleRun,
@ -162,6 +164,14 @@ const BasePanel: FC<BasePanelProps> = ({
defaultRunInputData: NODES_EXTRA_DATA[data.type]?.defaultRunInputData || {},
})
const logParams = useLogs()
const passedLogParams = (() => {
if ([BlockEnum.Tool, BlockEnum.Agent, BlockEnum.Iteration, BlockEnum.Loop].includes(data.type))
return logParams
return {}
})()
if (isShowSingleRun) {
return (
<div className={cn(
@ -182,6 +192,7 @@ const BasePanel: FC<BasePanelProps> = ({
onRun={handleRunWithParams}
onStop={handleStop}
{...singleRunParams!}
{...passedLogParams}
existVarValuesInForms={getExistVarValuesInForms(singleRunParams?.forms as any)}
filteredExistVarForms={getFilteredExistVarForms(singleRunParams?.forms as any)}
result={<></>}
@ -319,6 +330,8 @@ const BasePanel: FC<BasePanelProps> = ({
canSingleRun={isSupportSingleRun}
runningStatus={runningStatus}
onSingleRunClicked={handleSingleRun}
nodeInfo={nodeInfo}
{...passedLogParams}
/>
)}
</div>

View File

@ -1,4 +1,5 @@
'use client'
import type { ResultPanelProps } from '@/app/components/workflow/run/result-panel'
import ResultPanel from '@/app/components/workflow/run/result-panel'
import { NodeRunningStatus } from '@/app/components/workflow/types'
import type { FC } from 'react'
@ -6,21 +7,25 @@ import React from 'react'
import NoData from './no-data'
import { useLastRun } from '@/service/use-workflow'
import { RiLoader2Line } from '@remixicon/react'
import type { NodeTracing } from '@/types/workflow'
type Props = {
appId: string
nodeId: string
canSingleRun: boolean
nodeInfo?: NodeTracing
runningStatus?: NodeRunningStatus
onSingleRunClicked: () => void
}
} & Partial<ResultPanelProps>
const LastRun: FC<Props> = ({
appId,
nodeId,
canSingleRun,
nodeInfo,
runningStatus,
onSingleRunClicked,
...otherResultPanelProps
}) => {
const isRunning = runningStatus === NodeRunningStatus.Running
const { data: runResult, isFetching } = useLastRun(appId, nodeId, !isRunning)
@ -44,6 +49,8 @@ const LastRun: FC<Props> = ({
<div>
<ResultPanel
{...runResult as any}
{...otherResultPanelProps}
nodeInfo={nodeInfo}
showSteps={false}
/>
</div>

View File

@ -12,6 +12,7 @@ import useTemplateTransformSingleRunFormParams from '@/app/components/workflow/n
import useQuestionClassifierSingleRunFormParams from '@/app/components/workflow/nodes/question-classifier/use-single-run-form-params'
import useParameterExtractorSingleRunFormParams from '@/app/components/workflow/nodes/parameter-extractor/use-single-run-form-params'
import useHttpRequestSingleRunFormParams from '@/app/components/workflow/nodes/http/use-single-run-form-params'
import useToolSingleRunFormParams from '@/app/components/workflow/nodes/tool/use-single-run-form-params'
import useIterationSingleRunFormParams from '@/app/components/workflow/nodes/iteration/use-single-run-form-params'
import useAgentSingleRunFormParams from '@/app/components/workflow/nodes/agent/use-single-run-form-params'
import useDocExtractorSingleRunFormParams from '@/app/components/workflow/nodes/document-extractor/use-single-run-form-params'
@ -19,6 +20,10 @@ import useLoopSingleRunFormParams from '@/app/components/workflow/nodes/loop/use
import useIfElseSingleRunFormParams from '@/app/components/workflow/nodes/if-else/use-single-run-form-params'
import useVariableAggregatorSingleRunFormParams from '@/app/components/workflow/nodes/variable-assigner/use-single-run-form-params'
import useToolGetDataForCheckMore from '@/app/components/workflow/nodes/tool/use-get-data-for-check-more'
// import
import type { CommonNodeType } from '@/app/components/workflow/types'
import { BlockEnum } from '@/app/components/workflow/types'
import {
useNodesSyncDraft,
@ -31,7 +36,7 @@ const singleRunFormParamsHooks: Record<BlockEnum, any> = {
[BlockEnum.TemplateTransform]: useTemplateTransformSingleRunFormParams,
[BlockEnum.QuestionClassifier]: useQuestionClassifierSingleRunFormParams,
[BlockEnum.HttpRequest]: useHttpRequestSingleRunFormParams,
[BlockEnum.Tool]: undefined,
[BlockEnum.Tool]: useToolSingleRunFormParams,
[BlockEnum.ParameterExtractor]: useParameterExtractorSingleRunFormParams,
[BlockEnum.Iteration]: useIterationSingleRunFormParams,
[BlockEnum.Agent]: useAgentSingleRunFormParams,
@ -56,17 +61,59 @@ const useSingleRunFormParamsHooks = (nodeType: BlockEnum) => {
}
}
const getDataForCheckMoreHooks: Record<BlockEnum, any> = {
[BlockEnum.Tool]: useToolGetDataForCheckMore,
[BlockEnum.LLM]: undefined,
[BlockEnum.KnowledgeRetrieval]: undefined,
[BlockEnum.Code]: undefined,
[BlockEnum.TemplateTransform]: undefined,
[BlockEnum.QuestionClassifier]: undefined,
[BlockEnum.HttpRequest]: undefined,
[BlockEnum.ParameterExtractor]: undefined,
[BlockEnum.Iteration]: undefined,
[BlockEnum.Agent]: undefined,
[BlockEnum.DocExtractor]: undefined,
[BlockEnum.Loop]: undefined,
[BlockEnum.Start]: undefined,
[BlockEnum.IfElse]: undefined,
[BlockEnum.VariableAggregator]: undefined,
[BlockEnum.End]: undefined,
[BlockEnum.Answer]: undefined,
[BlockEnum.VariableAssigner]: undefined,
[BlockEnum.ListFilter]: undefined,
[BlockEnum.IterationStart]: undefined,
[BlockEnum.Assigner]: undefined,
[BlockEnum.LoopStart]: undefined,
[BlockEnum.LoopEnd]: undefined,
}
const useGetDataForCheckMoreHooks = <T>(nodeType: BlockEnum) => {
return (id: string, payload: CommonNodeType<T>) => {
return getDataForCheckMoreHooks[nodeType]?.({ id, payload }) || {
getData: () => {
return {}
},
}
}
}
type Params<T> = OneStepRunParams<T>
const useLastRun = <T>({
...oneStepRunParams
}: Params<T>) => {
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
const {
getData: getDataForCheckMore,
} = useGetDataForCheckMoreHooks<T>(oneStepRunParams.data.type)(oneStepRunParams.id, oneStepRunParams.data)
const {
id,
data,
} = oneStepRunParams
const oneStepRunRes = useOneStepRun(oneStepRunParams)
const oneStepRunRes = useOneStepRun({
...oneStepRunParams,
moreDataForCheckValid: getDataForCheckMore(),
})
const {
hideSingleRun,
handleRun: doCallRunApi,
@ -77,9 +124,13 @@ const useLastRun = <T>({
runInputDataRef,
setRunInputData,
showSingleRun,
runResult,
} = oneStepRunRes
const singleRunParams = useSingleRunFormParamsHooks(data.type)({
const {
nodeInfo,
...singleRunParams
} = useSingleRunFormParamsHooks(data.type)({
id,
payload: data,
runInputData,
@ -88,6 +139,7 @@ const useLastRun = <T>({
setRunInputData,
toVarInputs,
varSelectorsToVarInputs,
runResult,
})
const callRunApi = async (data: Record<string, any>) => {
@ -115,7 +167,6 @@ const useLastRun = <T>({
if (!forms || forms.length === 0)
return []
// if (!singleRunParams)
const valuesArr = forms.map((form) => {
const values: Record<string, any> = {}
form.inputs.forEach(({ variable }) => {
@ -181,6 +232,7 @@ const useLastRun = <T>({
tabType,
setTabType: handleTabClicked,
singleRunParams,
nodeInfo,
setRunInputData,
hasLastRunData,
handleSingleRun,

View File

@ -521,24 +521,6 @@ const useOneStepRun = <T>({
return varInputs
}
const varSelectorsToVarInputs = (valueSelectors: ValueSelector[]): InputVar[] => {
return valueSelectors.map((item) => {
const varInfo = getNodeInfoById(availableNodesIncludeParent, item[0])?.data
return {
type: InputVarType.textInput,
required: true,
label: {
nodeType: varInfo?.type,
nodeName: varInfo?.title || availableNodesIncludeParent[0]?.data.title, // default start node title
variable: isSystemVar(item) ? item.join('.') : item[item.length - 1],
isChatVar: isConversationVar(item),
},
variable: isSystemVar(item) ? `#${item.join('.')}#` : `${item.join('.')}`,
value_selector: item,
}
})
}
const getInputVars = (textList: string[]) => {
const valueSelectors: ValueSelector[] = []
textList.forEach((text) => {
@ -564,6 +546,12 @@ const useOneStepRun = <T>({
return varInputs
}
const varSelectorsToVarInputs = (valueSelectors: ValueSelector[] | string[]): InputVar[] => {
return valueSelectors.map((item) => {
return getInputVars([`{{#${typeof item === 'string' ? item : item.join('.')}#}}`])[0]
})
}
return {
isShowSingleRun,
hideSingleRun,

View File

@ -3,7 +3,6 @@ import type { InputVar, Variable } from '@/app/components/workflow/types'
import { useMemo } from 'react'
import useNodeCrud from '../_base/hooks/use-node-crud'
import type { AgentNodeType } from './types'
import { useLogs } from '../../run/hooks'
import { useTranslation } from 'react-i18next'
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
import { useStrategyInfo } from './use-config'
@ -28,7 +27,6 @@ const useSingleRunFormParams = ({
}: Params) => {
const { t } = useTranslation()
const { inputs } = useNodeCrud<AgentNodeType>(id, payload)
const logsParams = useLogs()
const formData = useMemo(() => {
return Object.fromEntries(
@ -72,7 +70,6 @@ const useSingleRunFormParams = ({
return {
forms,
logsParams,
}
}

View File

@ -1,5 +1,5 @@
import type { FC } from 'react'
import React, { useMemo } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import Split from '../_base/components/split'
import type { ToolNodeType } from './types'
@ -11,12 +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'
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
import ResultPanel from '@/app/components/workflow/run/result-panel'
import { useToolIcon } from '@/app/components/workflow/hooks'
import { useLogs } from '@/app/components/workflow/run/hooks'
import formatToTracingNodeList from '@/app/components/workflow/run/utils/format-log'
import StructureOutputItem from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/show'
import { Type } from '../llm/types'
@ -45,23 +40,9 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
hideSetAuthModal,
handleSaveAuth,
isLoading,
isShowSingleRun,
hideSingleRun,
singleRunForms,
runningStatus,
handleRun,
handleStop,
runResult,
outputSchema,
hasObjectOutput,
} = useConfig(id, data)
const toolIcon = useToolIcon(data)
const logsParams = useLogs()
const nodeInfo = useMemo(() => {
if (!runResult)
return null
return formatToTracingNodeList([runResult], t)[0]
}, [runResult, t])
if (isLoading) {
return <div className='flex h-[200px] items-center justify-center'>
@ -180,21 +161,6 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
</>
</OutputVars>
</div>
{isShowSingleRun && (
<BeforeRunForm
nodeName={inputs.title}
nodeType={inputs.type}
toolIcon={toolIcon}
onHide={hideSingleRun}
forms={singleRunForms}
runningStatus={runningStatus}
onRun={handleRun}
onStop={handleStop}
{...logsParams}
result={<ResultPanel {...runResult} showSteps={false} {...logsParams} nodeInfo={nodeInfo} />}
/>
)}
</div>
)
}

View File

@ -3,17 +3,15 @@ import { useTranslation } from 'react-i18next'
import produce from 'immer'
import { useBoolean } from 'ahooks'
import { useStore } from '../../store'
import { type ToolNodeType, type ToolVarInputs, VarType } from './types'
import type { ToolNodeType, ToolVarInputs } 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 { 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 { VarType as VarVarType } from '@/app/components/workflow/types'
import type { InputVar, ValueSelector, Var } from '@/app/components/workflow/types'
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
import type { InputVar, Var } from '@/app/components/workflow/types'
import {
useFetchToolsData,
useNodesReadOnly,
@ -160,39 +158,8 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const isLoading = currTool && (isBuiltIn ? !currCollection : false)
// single run
const [inputVarValues, doSetInputVarValues] = useState<Record<string, any>>({})
const setInputVarValues = (value: Record<string, any>) => {
doSetInputVarValues(value)
// eslint-disable-next-line ts/no-use-before-define
setRunInputData(value)
}
// fill single run form variable with constant value first time
const inputVarValuesWithConstantValue = () => {
const res = produce(inputVarValues, (draft) => {
Object.keys(inputs.tool_parameters).forEach((key: string) => {
const { type, value } = inputs.tool_parameters[key]
if (type === VarType.constant && (value === undefined || value === null))
draft.tool_parameters[key].value = value
})
})
return res
}
const {
isShowSingleRun,
hideSingleRun,
getInputVars,
runningStatus,
setRunInputData,
handleRun: doHandleRun,
handleStop,
runResult,
} = useOneStepRun<ToolNodeType>({
id,
data: inputs,
defaultRunInputData: {},
moreDataForCheckValid: {
const getMoreDataForCheckValid = () => {
return {
toolInputsSchema: (() => {
const formInputs: InputVar[] = []
toolInputVarSchema.forEach((item: any) => {
@ -208,52 +175,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
notAuthed: isShowAuthBtn,
toolSettingSchema,
language,
},
})
const hadVarParams = Object.keys(inputs.tool_parameters)
.filter(key => inputs.tool_parameters[key].type !== VarType.constant)
.map(k => inputs.tool_parameters[k])
const varInputs = getInputVars(hadVarParams.map((p) => {
if (p.type === VarType.variable) {
// handle the old wrong value not crash the page
if (!(p.value as any).join)
return `{{#${p.value}#}}`
return `{{#${(p.value as ValueSelector).join('.')}#}}`
}
return p.value as string
}))
const singleRunForms = (() => {
const forms: FormProps[] = [{
inputs: varInputs,
values: inputVarValuesWithConstantValue(),
onChange: setInputVarValues,
}]
return forms
})()
const handleRun = (submitData: Record<string, any>) => {
const varTypeInputKeys = Object.keys(inputs.tool_parameters)
.filter(key => inputs.tool_parameters[key].type === VarType.variable)
const shouldAdd = varTypeInputKeys.length > 0
if (!shouldAdd) {
doHandleRun(submitData)
return
}
const addMissedVarData = { ...submitData }
Object.keys(submitData).forEach((key) => {
const value = submitData[key]
varTypeInputKeys.forEach((inputKey) => {
const inputValue = inputs.tool_parameters[inputKey].value as ValueSelector
if (`#${inputValue.join('.')}#` === key)
addMissedVarData[inputKey] = value
})
})
doHandleRun(addMissedVarData)
}
const outputSchema = useMemo(() => {
@ -307,18 +229,9 @@ const useConfig = (id: string, payload: ToolNodeType) => {
hideSetAuthModal,
handleSaveAuth,
isLoading,
isShowSingleRun,
hideSingleRun,
inputVarValues,
varInputs,
setInputVarValues,
singleRunForms,
runningStatus,
handleRun,
handleStop,
runResult,
outputSchema,
hasObjectOutput,
getMoreDataForCheckValid,
}
}

View File

@ -0,0 +1,20 @@
import type { ToolNodeType } from './types'
import useConfig from './use-config'
type Params = {
id: string
payload: ToolNodeType,
}
const useGetDataForCheckMore = ({
id,
payload,
}: Params) => {
const { getMoreDataForCheckValid } = useConfig(id, payload)
return {
getData: getMoreDataForCheckValid,
}
}
export default useGetDataForCheckMore

View File

@ -0,0 +1,89 @@
import type { MutableRefObject } from 'react'
import type { InputVar, Variable } from '@/app/components/workflow/types'
import { useCallback, useMemo, useState } from 'react'
import useNodeCrud from '../_base/hooks/use-node-crud'
import { type ToolNodeType, VarType } from './types'
import type { ValueSelector } from '@/app/components/workflow/types'
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
import produce from 'immer'
import type { NodeTracing } from '@/types/workflow'
import { useTranslation } from 'react-i18next'
import formatToTracingNodeList from '@/app/components/workflow/run/utils/format-log'
import { useToolIcon } from '../../hooks'
type Params = {
id: string,
payload: ToolNodeType,
runInputData: Record<string, any>
runInputDataRef: MutableRefObject<Record<string, any>>
getInputVars: (textList: string[]) => InputVar[]
setRunInputData: (data: Record<string, any>) => void
toVarInputs: (variables: Variable[]) => InputVar[]
runResult: NodeTracing
}
const useSingleRunFormParams = ({
id,
payload,
getInputVars,
setRunInputData,
runResult,
}: Params) => {
const { t } = useTranslation()
const { inputs } = useNodeCrud<ToolNodeType>(id, payload)
const hadVarParams = Object.keys(inputs.tool_parameters)
.filter(key => inputs.tool_parameters[key].type !== VarType.constant)
.map(k => inputs.tool_parameters[k])
const varInputs = getInputVars(hadVarParams.map((p) => {
if (p.type === VarType.variable) {
// handle the old wrong value not crash the page
if (!(p.value as any).join)
return `{{#${p.value}#}}`
return `{{#${(p.value as ValueSelector).join('.')}#}}`
}
return p.value as string
}))
const [inputVarValues, doSetInputVarValues] = useState<Record<string, any>>({})
const setInputVarValues = useCallback((value: Record<string, any>) => {
doSetInputVarValues(value)
setRunInputData(value)
}, [setRunInputData])
const inputVarValuesWithConstantValue = useCallback(() => {
const res = produce(inputVarValues, (draft) => {
Object.keys(inputs.tool_parameters).forEach((key: string) => {
const { type, value } = inputs.tool_parameters[key]
if (type === VarType.constant && (value === undefined || value === null))
draft.tool_parameters[key].value = value
})
})
return res
}, [inputs.tool_parameters, inputVarValues])
const forms = useMemo(() => {
const forms: FormProps[] = [{
inputs: varInputs,
values: inputVarValuesWithConstantValue(),
onChange: setInputVarValues,
}]
return forms
}, [inputVarValuesWithConstantValue, setInputVarValues, varInputs])
const nodeInfo = useMemo(() => {
if (!runResult)
return null
return formatToTracingNodeList([runResult], t)[0]
}, [runResult, t])
const toolIcon = useToolIcon(payload)
return {
forms,
nodeInfo,
toolIcon,
}
}
export default useSingleRunFormParams

View File

@ -17,7 +17,7 @@ import { LoopLogTrigger } from '@/app/components/workflow/run/loop-log'
import { RetryLogTrigger } from '@/app/components/workflow/run/retry-log'
import { AgentLogTrigger } from '@/app/components/workflow/run/agent-log'
type ResultPanelProps = {
export type ResultPanelProps = {
nodeInfo?: NodeTracing
inputs?: string
process_data?: string