Commit 30ea3cb7 authored by Joel's avatar Joel

feat: can run code node

parent a5147a38
...@@ -2,45 +2,56 @@ ...@@ -2,45 +2,56 @@
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import type { OutputVar } from '../../../code/types' import type { OutputVar, OutputVarType } from '../../../code/types'
import RemoveButton from '../remove-button' import RemoveButton from '../remove-button'
import VarTypePicker from './var-type-picker' import VarTypePicker from './var-type-picker'
type Props = { type Props = {
readonly: boolean readonly: boolean
list: OutputVar[] outputs: OutputVar
onChange: (list: OutputVar[]) => void onChange: (payload: OutputVar) => void
} }
const OutputVarList: FC<Props> = ({ const OutputVarList: FC<Props> = ({
readonly, readonly,
list, outputs,
onChange, onChange,
}) => { }) => {
const list = (Object.keys(outputs)).map((key) => {
return {
variable: key,
variable_type: outputs[key].type,
}
})
const handleVarNameChange = useCallback((index: number) => { const handleVarNameChange = useCallback((index: number) => {
return (e: React.ChangeEvent<HTMLInputElement>) => { return (e: React.ChangeEvent<HTMLInputElement>) => {
const newList = produce(list, (draft) => { const oldKey = list[index].variable
draft[index].variable = e.target.value const newOutputs = produce(outputs, (draft) => {
const newKey = e.target.value
draft[newKey] = draft[oldKey]
delete draft[oldKey]
}) })
onChange(newList) onChange(newOutputs)
} }
}, [list, onChange]) }, [list, onChange])
const handleVarChange = useCallback((index: number) => { const handleVarTypeChange = useCallback((index: number) => {
return (value: string) => { return (value: string) => {
const newList = produce(list, (draft) => { const key = list[index].variable
draft[index].variable_type = value const newOutputs = produce(outputs, (draft) => {
draft[key].type = value as OutputVarType
}) })
onChange(newList) onChange(newOutputs)
} }
}, [list, onChange]) }, [list, onChange])
const handleVarRemove = useCallback((index: number) => { const handleVarRemove = useCallback((index: number) => {
return () => { return () => {
const newList = produce(list, (draft) => { const key = list[index].variable
draft.splice(index, 1) const newOutputs = produce(outputs, (draft) => {
delete draft[key]
}) })
onChange(newList) onChange(newOutputs)
} }
}, [list, onChange]) }, [list, onChange])
...@@ -57,7 +68,7 @@ const OutputVarList: FC<Props> = ({ ...@@ -57,7 +68,7 @@ const OutputVarList: FC<Props> = ({
<VarTypePicker <VarTypePicker
readonly={readonly} readonly={readonly}
value={item.variable_type} value={item.variable_type}
onChange={handleVarChange(index)} onChange={handleVarTypeChange(index)}
/> />
<RemoveButton <RemoveButton
className='!p-2 !bg-gray-100 hover:!bg-gray-200' className='!p-2 !bg-gray-100 hover:!bg-gray-200'
......
import { useState } from 'react' import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useWorkflow } from '@/app/components/workflow/hooks' import { useWorkflow } from '@/app/components/workflow/hooks'
import type { CommonNodeType, InputVar, Variable } from '@/app/components/workflow/types' import type { CommonNodeType, InputVar, Variable } from '@/app/components/workflow/types'
import { InputVarType, NodeRunningStatus } from '@/app/components/workflow/types' import { InputVarType, NodeRunningStatus } from '@/app/components/workflow/types'
import { useStore as useAppStore } from '@/app/components/app/store' import { useStore as useAppStore } from '@/app/components/app/store'
import { singleNodeRun } from '@/service/workflow' import { singleNodeRun } from '@/service/workflow'
import Toast from '@/app/components/base/toast'
type Params<T> = { type Params<T> = {
id: string id: string
...@@ -13,6 +15,8 @@ type Params<T> = { ...@@ -13,6 +15,8 @@ type Params<T> = {
} }
const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => true }: Params<T>) => { const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => true }: Params<T>) => {
const { t } = useTranslation()
const appId = useAppStore.getState().appDetail?.id const appId = useAppStore.getState().appDetail?.id
const [runInputData, setRunInputData] = useState<Record<string, any>>(defaultRunInputData || {}) const [runInputData, setRunInputData] = useState<Record<string, any>>(defaultRunInputData || {})
...@@ -38,9 +42,30 @@ const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => tru ...@@ -38,9 +42,30 @@ const useOneStepRun = <T>({ id, data, defaultRunInputData, isInvalid = () => tru
_singleRunningStatus: NodeRunningStatus.Running, _singleRunningStatus: NodeRunningStatus.Running,
}, },
}) })
try {
const res = await singleNodeRun(appId!, id, { inputs: runInputData }) const res = await singleNodeRun(appId!, id, { inputs: runInputData })
console.log(res) }
catch (e) {
handleNodeDataUpdate({
id,
data: {
...data,
_singleRunningStatus: NodeRunningStatus.Failed,
},
})
return false
}
handleNodeDataUpdate({
id,
data: {
...data,
_singleRunningStatus: NodeRunningStatus.Succeeded,
},
})
Toast.notify({
type: 'success',
message: t('common.api.success'),
})
} }
const handleStop = () => { const handleStop = () => {
......
import { useCallback } from 'react' import { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import type { OutputVar } from '../../code/types' import { type OutputVar, OutputVarType } from '../../code/types'
type Params<T> = { type Params<T> = {
inputs: T inputs: T
setInputs: (newInputs: T) => void setInputs: (newInputs: T) => void
...@@ -11,25 +11,28 @@ function useOutputVarList<T>({ ...@@ -11,25 +11,28 @@ function useOutputVarList<T>({
setInputs, setInputs,
varKey = 'outputs', varKey = 'outputs',
}: Params<T>) { }: Params<T>) {
const handleVarListChange = useCallback((newList: OutputVar[]) => { const handleVarsChange = useCallback((newVars: OutputVar) => {
const newInputs = produce(inputs, (draft: any) => { const newInputs = produce(inputs, (draft: any) => {
draft[varKey] = newList draft[varKey] = newVars
}) })
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs, varKey]) }, [inputs, setInputs, varKey])
const handleAddVariable = useCallback(() => { const handleAddVariable = useCallback(() => {
const newInputs = produce(inputs, (draft: any) => { const newInputs = produce(inputs, (draft: any) => {
draft[varKey].push({ draft[varKey] = {
variable: '', ...draft[varKey],
variable_type: 'string', [`var-${Object.keys(draft[varKey]).length + 1}`]: {
}) type: OutputVarType.string,
children: null,
},
}
}) })
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs, varKey]) }, [inputs, setInputs, varKey])
return { return {
handleVarListChange, handleVarsChange,
handleAddVariable, handleAddVariable,
} }
} }
......
...@@ -39,7 +39,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({ ...@@ -39,7 +39,7 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
handleAddVariable, handleAddVariable,
handleCodeChange, handleCodeChange,
handleCodeLanguageChange, handleCodeLanguageChange,
handleOutputVarListChange, handleVarsChange,
handleAddOutputVariable, handleAddOutputVariable,
// single run // single run
isShowSingleRun, isShowSingleRun,
...@@ -91,8 +91,8 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({ ...@@ -91,8 +91,8 @@ const Panel: FC<NodePanelProps<CodeNodeType>> = ({
> >
<OutputVarList <OutputVarList
readonly={readOnly} readonly={readOnly}
list={inputs.outputs} outputs={inputs.outputs}
onChange={handleOutputVarListChange} onChange={handleVarsChange}
/> />
</Field> </Field>
</div> </div>
......
...@@ -6,14 +6,21 @@ export enum CodeLanguage { ...@@ -6,14 +6,21 @@ export enum CodeLanguage {
json = 'json', json = 'json',
} }
export type OutputVar = { export enum OutputVarType {
variable: string string = 'string',
variable_type: string number = 'number',
boolean = 'boolean',
object = 'object',
} }
export type OutputVar = Record<string, {
type: OutputVarType
children: null // support nest in the future,
}>
export type CodeNodeType = CommonNodeType & { export type CodeNodeType = CommonNodeType & {
variables: Variable[] variables: Variable[]
code_language: CodeLanguage code_language: CodeLanguage
code: string code: string
outputs: OutputVar[] outputs: OutputVar
} }
...@@ -27,7 +27,7 @@ const useConfig = (id: string, payload: CodeNodeType) => { ...@@ -27,7 +27,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
setInputs(newInputs) setInputs(newInputs)
}, [inputs, setInputs]) }, [inputs, setInputs])
const { handleVarListChange: handleOutputVarListChange, handleAddVariable: handleAddOutputVariable } = useOutputVarList<CodeNodeType>({ const { handleVarsChange, handleAddVariable: handleAddOutputVariable } = useOutputVarList<CodeNodeType>({
inputs, inputs,
setInputs, setInputs,
}) })
...@@ -68,7 +68,7 @@ const useConfig = (id: string, payload: CodeNodeType) => { ...@@ -68,7 +68,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
handleAddVariable, handleAddVariable,
handleCodeChange, handleCodeChange,
handleCodeLanguageChange, handleCodeLanguageChange,
handleOutputVarListChange, handleVarsChange,
handleAddOutputVariable, handleAddOutputVariable,
// single run // single run
isShowSingleRun, isShowSingleRun,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment