feat: code node single run

This commit is contained in:
Joel 2025-04-30 15:33:45 +08:00
parent b875a48f80
commit 739ee05446
4 changed files with 56 additions and 77 deletions

View File

@ -6,6 +6,7 @@ import { useWorkflowStore } from '@/app/components/workflow/store'
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form' import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
import useLLMSingleRunFormParams from '@/app/components/workflow/nodes/llm/use-single-run-form-params' import useLLMSingleRunFormParams from '@/app/components/workflow/nodes/llm/use-single-run-form-params'
import useKnowledgeRetrievalSingleRunFormParams from '../../../../knowledge-retrieval/use-single-run-form-params' import useKnowledgeRetrievalSingleRunFormParams from '../../../../knowledge-retrieval/use-single-run-form-params'
import useCodeSingleRunFormParams from '../../../../code/use-single-run-form-params'
import { BlockEnum } from '@/app/components/workflow/types' import { BlockEnum } from '@/app/components/workflow/types'
import { import {
useNodesSyncDraft, useNodesSyncDraft,
@ -14,12 +15,12 @@ import {
const singleRunFormParamsHooks: Record<BlockEnum, any> = { const singleRunFormParamsHooks: Record<BlockEnum, any> = {
[BlockEnum.LLM]: useLLMSingleRunFormParams, [BlockEnum.LLM]: useLLMSingleRunFormParams,
[BlockEnum.KnowledgeRetrieval]: useKnowledgeRetrievalSingleRunFormParams, [BlockEnum.KnowledgeRetrieval]: useKnowledgeRetrievalSingleRunFormParams,
[BlockEnum.Code]: useCodeSingleRunFormParams,
[BlockEnum.Start]: undefined, [BlockEnum.Start]: undefined,
[BlockEnum.End]: undefined, [BlockEnum.End]: undefined,
[BlockEnum.Answer]: undefined, [BlockEnum.Answer]: undefined,
[BlockEnum.QuestionClassifier]: undefined, [BlockEnum.QuestionClassifier]: undefined,
[BlockEnum.IfElse]: undefined, [BlockEnum.IfElse]: undefined,
[BlockEnum.Code]: undefined,
[BlockEnum.TemplateTransform]: undefined, [BlockEnum.TemplateTransform]: undefined,
[BlockEnum.HttpRequest]: undefined, [BlockEnum.HttpRequest]: undefined,
[BlockEnum.VariableAssigner]: undefined, [BlockEnum.VariableAssigner]: undefined,

View File

@ -14,8 +14,6 @@ import Split from '@/app/components/workflow/nodes/_base/components/split'
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor' import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector' import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
import type { NodePanelProps } from '@/app/components/workflow/types' import type { NodePanelProps } from '@/app/components/workflow/types'
import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
import ResultPanel from '@/app/components/workflow/run/result-panel'
const i18nPrefix = 'workflow.nodes.code' const i18nPrefix = 'workflow.nodes.code'
const codeLanguages = [ const codeLanguages = [
@ -50,16 +48,6 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
isShowRemoveVarConfirm, isShowRemoveVarConfirm,
hideRemoveVarConfirm, hideRemoveVarConfirm,
onRemoveVarConfirm, onRemoveVarConfirm,
// single run
isShowSingleRun,
hideSingleRun,
runningStatus,
handleRun,
handleStop,
runResult,
varInputs,
inputVarValues,
setInputVarValues,
} = useConfig(id, data) } = useConfig(id, data)
const handleGeneratedCode = (value: string) => { const handleGeneratedCode = (value: string) => {
@ -128,25 +116,6 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
/> />
</Field> </Field>
</div> </div>
{
isShowSingleRun && (
<BeforeRunForm
nodeName={inputs.title}
onHide={hideSingleRun}
forms={[
{
inputs: varInputs,
values: inputVarValues,
onChange: setInputVarValues,
},
]}
runningStatus={runningStatus}
onRun={handleRun}
onStop={handleStop}
result={<ResultPanel {...runResult} showSteps={false} />}
/>
)
}
<RemoveEffectVarConfirm <RemoveEffectVarConfirm
isShow={isShowRemoveVarConfirm} isShow={isShowRemoveVarConfirm}
onCancel={hideRemoveVarConfirm} onCancel={hideRemoveVarConfirm}

View File

@ -8,7 +8,6 @@ import { useStore } from '../../store'
import type { CodeNodeType, OutputVar } from './types' import type { CodeNodeType, OutputVar } from './types'
import { CodeLanguage } from './types' import { CodeLanguage } from './types'
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud' import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
import { fetchNodeDefault } from '@/service/workflow' import { fetchNodeDefault } from '@/service/workflow'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import { import {
@ -104,38 +103,6 @@ const useConfig = (id: string, payload: CodeNodeType) => {
return [VarType.string, VarType.number, VarType.secret, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject, VarType.file, VarType.arrayFile].includes(varPayload.type) return [VarType.string, VarType.number, VarType.secret, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject, VarType.file, VarType.arrayFile].includes(varPayload.type)
}, []) }, [])
// single run
const {
isShowSingleRun,
hideSingleRun,
toVarInputs,
runningStatus,
isCompleted,
handleRun,
handleStop,
runInputData,
setRunInputData,
runResult,
} = useOneStepRun<CodeNodeType>({
id,
data: inputs,
defaultRunInputData: {},
})
const varInputs = toVarInputs(inputs.variables)
const inputVarValues = (() => {
const vars: Record<string, any> = {}
Object.keys(runInputData)
.forEach((key) => {
vars[key] = runInputData[key]
})
return vars
})()
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
setRunInputData(newPayload)
}, [setRunInputData])
const handleCodeAndVarsChange = useCallback((code: string, inputVariables: Variable[], outputVariables: OutputVar) => { const handleCodeAndVarsChange = useCallback((code: string, inputVariables: Variable[], outputVariables: OutputVar) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
draft.code = code draft.code = code
@ -160,17 +127,6 @@ const useConfig = (id: string, payload: CodeNodeType) => {
isShowRemoveVarConfirm, isShowRemoveVarConfirm,
hideRemoveVarConfirm, hideRemoveVarConfirm,
onRemoveVarConfirm, onRemoveVarConfirm,
// single run
isShowSingleRun,
hideSingleRun,
runningStatus,
isCompleted,
handleRun,
handleStop,
varInputs,
inputVarValues,
setInputVarValues,
runResult,
handleCodeAndVarsChange, handleCodeAndVarsChange,
} }
} }

View File

@ -0,0 +1,53 @@
import type { MutableRefObject } from 'react'
import type { InputVar, Variable } from '@/app/components/workflow/types'
import { useCallback, useMemo } from 'react'
import useNodeCrud from '../_base/hooks/use-node-crud'
import type { CodeNodeType } from './types'
type Params = {
id: string,
payload: CodeNodeType,
runInputData: Record<string, any>
runInputDataRef: MutableRefObject<Record<string, any>>
getInputVars: (textList: string[]) => InputVar[]
setRunInputData: (data: Record<string, any>) => void
toVarInputs: (variables: Variable[]) => InputVar[]
}
const useSingleRunFormParams = ({
id,
payload,
runInputData,
toVarInputs,
setRunInputData,
}: Params) => {
const { inputs } = useNodeCrud<CodeNodeType>(id, payload)
const varInputs = toVarInputs(inputs.variables)
const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
setRunInputData(newPayload)
}, [setRunInputData])
const inputVarValues = (() => {
const vars: Record<string, any> = {}
Object.keys(runInputData)
.forEach((key) => {
vars[key] = runInputData[key]
})
return vars
})()
const forms = useMemo(() => {
return [
{
inputs: varInputs,
values: inputVarValues,
onChange: setInputVarValues,
},
]
}, [inputVarValues, setInputVarValues, varInputs])
return {
forms,
}
}
export default useSingleRunFormParams