Commit 21db8e3b authored by Joel's avatar Joel

feat: add var struct

parent e05bbec8
...@@ -14,7 +14,16 @@ export const mockLLMNodeData: LLMNodeData = { ...@@ -14,7 +14,16 @@ export const mockLLMNodeData: LLMNodeData = {
temperature: 0.7, temperature: 0.7,
}, },
}, },
variables: [], variables: [
{
variable: 'name',
value_selector: ['start', 'name'],
},
{
variable: 'age',
value_selector: ['a', 'b', 'c'],
},
],
prompt: [], prompt: [],
memory: { memory: {
role_prefix: MemoryRole.assistant, role_prefix: MemoryRole.assistant,
......
import type { FC } from 'react' import type { FC } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import BasePanel from '../_base/panel' import BasePanel from '../_base/panel'
import VarList from '../_base/components/var/var-list'
import useInput from './use-input' import useInput from './use-input'
import { mockLLMNodeData } from './mock' import { mockLLMNodeData } from './mock'
import Field from '@/app/components/workflow/nodes/_base/components/field' import Field from '@/app/components/workflow/nodes/_base/components/field'
import AddButton from '@/app/components/base/button/add-button' import AddButton from '@/app/components/base/button/add-button'
import Split from '@/app/components/workflow/nodes/_base/components/split' import Split from '@/app/components/workflow/nodes/_base/components/split'
import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal' import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
import Switch from '@/app/components/base/switch' import Switch from '@/app/components/base/switch'
const i18nPrefix = 'workflow.nodes.llm' const i18nPrefix = 'workflow.nodes.llm'
const Panel: FC = () => { const Panel: FC = () => {
...@@ -16,16 +17,15 @@ const Panel: FC = () => { ...@@ -16,16 +17,15 @@ const Panel: FC = () => {
const { const {
inputs, inputs,
handleModelChanged, handleModelChanged,
toggleContextEnabled,
handleCompletionParamsChange, handleCompletionParamsChange,
handleVarListChange,
handleAddVariable,
toggleContextEnabled,
} = useInput(mockLLMNodeData) } = useInput(mockLLMNodeData)
const model = inputs.model const model = inputs.model
const modelMode = inputs.model?.mode const modelMode = inputs.model?.mode
const isChatMode = modelMode === 'chat' const isChatMode = modelMode === 'chat'
const handleAddVariable = () => {
console.log('add variable')
}
return ( return (
<BasePanel <BasePanel
inputsElement={ inputsElement={
...@@ -53,7 +53,10 @@ const Panel: FC = () => { ...@@ -53,7 +53,10 @@ const Panel: FC = () => {
<AddButton onClick={handleAddVariable} /> <AddButton onClick={handleAddVariable} />
} }
> >
Var Selector <VarList
list={inputs.variables}
onChange={handleVarListChange}
/>
</Field> </Field>
<Field <Field
......
/* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable react-hooks/exhaustive-deps */
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import produce from 'immer' import produce from 'immer'
import type { LLMNodeData } from '../../types' import type { LLMNodeData, Variable } from '../../types'
const useInput = (initInputs: LLMNodeData) => { const useInput = (initInputs: LLMNodeData) => {
const [inputs, setInputs] = useState<LLMNodeData>(initInputs) const [inputs, setInputs] = useState<LLMNodeData>(initInputs)
// model
const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => { const handleModelChanged = useCallback((model: { provider: string; modelId: string; mode?: string }) => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
draft.model.provider = model.provider draft.model.provider = model.provider
...@@ -22,6 +23,25 @@ const useInput = (initInputs: LLMNodeData) => { ...@@ -22,6 +23,25 @@ const useInput = (initInputs: LLMNodeData) => {
setInputs(newInputs) setInputs(newInputs)
}, [inputs.model]) }, [inputs.model])
// variables
const handleVarListChange = useCallback((newList: Variable[]) => {
const newInputs = produce(inputs, (draft) => {
draft.variables = newList
})
setInputs(newInputs)
}, [inputs.variables])
const handleAddVariable = useCallback(() => {
const newInputs = produce(inputs, (draft) => {
draft.variables.push({
variable: '',
value_selector: [],
})
})
setInputs(newInputs)
}, [inputs.variables])
// context
const toggleContextEnabled = useCallback(() => { const toggleContextEnabled = useCallback(() => {
const newInputs = produce(inputs, (draft) => { const newInputs = produce(inputs, (draft) => {
draft.context.enabled = !draft.context.enabled draft.context.enabled = !draft.context.enabled
...@@ -30,9 +50,11 @@ const useInput = (initInputs: LLMNodeData) => { ...@@ -30,9 +50,11 @@ const useInput = (initInputs: LLMNodeData) => {
}, [inputs.context.enabled]) }, [inputs.context.enabled])
return { return {
handleCompletionParamsChange,
inputs, inputs,
handleModelChanged, handleModelChanged,
handleCompletionParamsChange,
handleVarListChange,
handleAddVariable,
toggleContextEnabled, toggleContextEnabled,
} }
} }
......
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