Commit d58a1b13 authored by Joel's avatar Joel

feat: code support vars

parent bb87a350
......@@ -46,8 +46,9 @@ const Page: FC = () => {
/*
* TODO: for debug.
* 2 directAnswer 3: llm 5: questionClassifier
* 7 Code
*/
selectedNodeId='5'
selectedNodeId='7'
/>
</div>
)
......
import { useCallback } from 'react'
import produce from 'immer'
import type { Variable } from '@/app/components/workflow/types'
type Params<T> = {
inputs: T
setInputs: (newInputs: T) => void
varKey?: string
}
function useVarList<T>({
inputs,
setInputs,
varKey = 'variables',
}: Params<T>) {
const handleVarListChange = useCallback((newList: Variable[]) => {
const newInputs = produce(inputs, (draft: any) => {
draft[varKey] = newList
})
setInputs(newInputs)
}, [inputs, setInputs, varKey])
const handleAddVariable = useCallback(() => {
const newInputs = produce(inputs, (draft: any) => {
draft[varKey].push({
variable: '',
value_selector: [],
})
})
setInputs(newInputs)
}, [inputs, setInputs, varKey])
return {
handleVarListChange,
handleAddVariable,
}
}
export default useVarList
import { CodeLanguage } from './types'
import type { CodeNodeType } from './types'
export const mockData: CodeNodeType = {
title: 'Test',
desc: 'Test',
type: 'Test',
variables: [
{
variable: 'name',
value_selector: ['aaa', 'name'],
},
{
variable: 'age',
value_selector: ['bbb', 'b', 'c'],
},
],
code_language: CodeLanguage.python3,
code: 'print("hello world")',
outputs: [
{
variable: 'output',
variable_type: 'string',
},
],
}
......@@ -2,7 +2,8 @@ import type { FC } from 'react'
const Node: FC = () => {
return (
<div>code</div>
// No summary content
<div></div>
)
}
......
import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
import useConfig from './use-config'
import { mockData } from './mock'
import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
import AddButton from '@/app/components/base/button/add-button'
import Field from '@/app/components/workflow/nodes/_base/components/field'
const i18nPrefix = 'workflow.nodes.code'
const Panel: FC = () => {
const { t } = useTranslation()
const readOnly = false
const {
inputs,
handleVarListChange,
handleAddVariable,
} = useConfig(mockData)
return (
<div>start panel inputs</div>
<div className='mt-2 px-4 space-y-4'>
<Field
title={t(`${i18nPrefix}.inputVars`)}
operations={
<AddButton onClick={handleAddVariable} />
}
>
<VarList
readonly={readOnly}
list={inputs.variables}
onChange={handleVarListChange}
/>
</Field>
</div>
)
}
......
import type { CommonNodeType, Variable } from '@/app/components/workflow/types'
export enum CodeLanguage {
python3 = 'python3',
javascript = 'javascript',
}
export type CodeNodeType = CommonNodeType & {
variables: Variable[]
code_language: CodeLanguage
code: string
outputs: {
variable: string
variable_type: string
}[]
}
import { useState } from 'react'
import useVarList from '../_base/hooks/use-var-list'
import type { CodeNodeType } from './types'
const useConfig = (initInputs: CodeNodeType) => {
const [inputs, setInputs] = useState<CodeNodeType>(initInputs)
const { handleVarListChange, handleAddVariable } = useVarList<CodeNodeType>({
inputs,
setInputs,
})
return {
inputs,
handleVarListChange,
handleAddVariable,
}
}
export default useConfig
import { useCallback, useState } from 'react'
import produce from 'immer'
import type { Variable } from '../../types'
import useVarList from '../_base/hooks/use-var-list'
import type { LLMNodeType } from './types'
const useConfig = (initInputs: LLMNodeType) => {
......@@ -24,22 +24,10 @@ const useConfig = (initInputs: LLMNodeType) => {
}, [inputs, setInputs])
// variables
const handleVarListChange = useCallback((newList: Variable[]) => {
const newInputs = produce(inputs, (draft) => {
draft.variables = newList
})
setInputs(newInputs)
}, [inputs, setInputs])
const handleAddVariable = useCallback(() => {
const newInputs = produce(inputs, (draft) => {
draft.variables.push({
variable: '',
value_selector: [],
})
})
setInputs(newInputs)
}, [inputs, setInputs])
const { handleVarListChange, handleAddVariable } = useVarList<LLMNodeType>({
inputs,
setInputs,
})
// context
const toggleContextEnabled = useCallback(() => {
......
import type { CommonNodeType, Variable } from '@/app/components/workflow/types'
export type TemplateTransformNodeType = CommonNodeType & {
variables: Variable[]
template: string
}
......@@ -19,6 +19,9 @@ const translation = {
usage: 'Model Usage Information',
},
},
code: {
inputVars: 'Input Variables',
},
},
}
......
......@@ -18,6 +18,9 @@ const translation = {
output: '生成内容',
usage: '模型用量信息',
},
code: {
inputVars: '输入变量',
},
},
},
}
......
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