feat: tools vars limit

This commit is contained in:
Joel 2024-03-15 22:15:36 +08:00
parent 338dd1c714
commit 5ee7fc4fde
3 changed files with 21 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import React, { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import type { ToolVarInput } from '../types' import type { ToolVarInput } from '../types'
import { VarType as VarKindType } from '../types' import { VarType as VarKindType } from '../types'
import { type ValueSelector } from '@/app/components/workflow/types' import type { ValueSelector, Var } from '@/app/components/workflow/types'
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
@ -17,6 +17,7 @@ type Props = {
value: ToolVarInput[] value: ToolVarInput[]
onChange: (value: ToolVarInput[]) => void onChange: (value: ToolVarInput[]) => void
isSupportConstantValue?: boolean isSupportConstantValue?: boolean
filterVar?: (payload: Var, valueSelector: ValueSelector) => boolean
} }
const InputVarList: FC<Props> = ({ const InputVarList: FC<Props> = ({
@ -26,6 +27,7 @@ const InputVarList: FC<Props> = ({
value, value,
onChange, onChange,
isSupportConstantValue, isSupportConstantValue,
filterVar,
}) => { }) => {
const language = useLanguage() const language = useLanguage()
@ -92,6 +94,7 @@ const InputVarList: FC<Props> = ({
onChange={handleChange(variable)} onChange={handleChange(variable)}
isSupportConstantValue={isSupportConstantValue} isSupportConstantValue={isSupportConstantValue}
defaultVarKindType={varInput?.variable_type} defaultVarKindType={varInput?.variable_type}
filterVar={filterVar}
/> />
{tooltip && <div className='leading-[18px] text-xs font-normal text-gray-600'>{tooltip[language] || tooltip.en_US}</div>} {tooltip && <div className='leading-[18px] text-xs font-normal text-gray-600'>{tooltip[language] || tooltip.en_US}</div>}
</div> </div>

View File

@ -28,6 +28,7 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
inputs, inputs,
toolInputVarSchema, toolInputVarSchema,
setInputVar, setInputVar,
filterVar,
toolSettingSchema, toolSettingSchema,
toolSettingValue, toolSettingValue,
setToolSettingValue, setToolSettingValue,
@ -80,6 +81,7 @@ const Panel: FC<NodePanelProps<ToolNodeType>> = ({
schema={toolInputVarSchema as any} schema={toolInputVarSchema as any}
value={inputs.tool_parameters} value={inputs.tool_parameters}
onChange={setInputVar} onChange={setInputVar}
filterVar={filterVar}
isSupportConstantValue isSupportConstantValue
/> />
</Field> </Field>

View File

@ -12,10 +12,9 @@ import { fetchBuiltInToolList, fetchCollectionList, fetchCustomToolList, updateB
import { addDefaultValue, toolParametersToFormSchemas } from '@/app/components/tools/utils/to-form-schema' import { addDefaultValue, toolParametersToFormSchemas } from '@/app/components/tools/utils/to-form-schema'
import Toast from '@/app/components/base/toast' import Toast from '@/app/components/base/toast'
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 { InputVarType } from '@/app/components/workflow/types' import { InputVarType, VarType as VarVarType } from '@/app/components/workflow/types'
import type { InputVar } from '@/app/components/workflow/types' import type { InputVar, Var } from '@/app/components/workflow/types'
import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run' import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
const useConfig = (id: string, payload: ToolNodeType) => { const useConfig = (id: string, payload: ToolNodeType) => {
const { t } = useTranslation() const { t } = useTranslation()
const language = useLanguage() const language = useLanguage()
@ -27,7 +26,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
* tool_configurations: tool setting, not dynamic setting * tool_configurations: tool setting, not dynamic setting
* tool_parameters: tool dynamic setting(by user) * tool_parameters: tool dynamic setting(by user)
*/ */
const { provider_id, provider_name, provider_type, tool_name, tool_configurations, tool_parameters: toolInputs } = inputs const { provider_id, provider_name, provider_type, tool_name, tool_configurations } = inputs
const isBuiltIn = provider_type === CollectionType.builtIn const isBuiltIn = provider_type === CollectionType.builtIn
const [currCollection, setCurrCollection] = useState<Collection | null | undefined>(null) const [currCollection, setCurrCollection] = useState<Collection | null | undefined>(null)
const fetchCurrCollection = useCallback(async () => { const fetchCurrCollection = useCallback(async () => {
@ -43,6 +42,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
return return
fetchCurrCollection() fetchCurrCollection()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [provider_id]) }, [provider_id])
// Auth // Auth
@ -63,7 +63,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
await fetchCurrCollection() await fetchCurrCollection()
hideSetAuthModal() hideSetAuthModal()
}, [currCollection]) }, [currCollection?.name, fetchCurrCollection, hideSetAuthModal, t])
const [currTool, setCurrTool] = useState<Tool | null>(null) const [currTool, setCurrTool] = useState<Tool | null>(null)
const formSchemas = currTool ? toolParametersToFormSchemas(currTool.parameters) : [] const formSchemas = currTool ? toolParametersToFormSchemas(currTool.parameters) : []
@ -94,6 +94,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
}) })
setInputs(inputsWithDefaultValue) setInputs(inputsWithDefaultValue)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currTool]) }, [currTool])
// setting when call // setting when call
@ -104,6 +105,11 @@ const useConfig = (id: string, payload: ToolNodeType) => {
}) })
}, [inputs, setInputs]) }, [inputs, setInputs])
// TODO: dynamic setting as the current var type
const filterVar = useCallback((varPayload: Var) => {
return varPayload.type !== VarVarType.arrayFile
}, [])
const isLoading = currTool && (isBuiltIn ? !currCollection : false) const isLoading = currTool && (isBuiltIn ? !currCollection : false)
useEffect(() => { useEffect(() => {
@ -123,6 +129,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
if (currTool) if (currTool)
setCurrTool(currTool) setCurrTool(currTool)
})() })()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [provider_name]) }, [provider_name])
// single run // single run
@ -149,13 +156,13 @@ const useConfig = (id: string, payload: ToolNodeType) => {
const singleRunForms = (() => { const singleRunForms = (() => {
const formInputs: InputVar[] = [] const formInputs: InputVar[] = []
toolInputVarSchema.forEach((item: any) => { toolInputVarSchema.forEach((item: any) => {
const targetItem = toolInputs.find(input => input.variable === item.variable) // const targetItem = toolInputs.find(input => input.variable === item.variable)
// TODO: support selector // TODO: support selector
// if (targetItem?.variable_type === VarType.selector) { // if (targetItem?.variable_type === VarType.selector) {
formInputs.push({ formInputs.push({
label: item.label[language] || item.label.en_US, label: item.label[language] || item.label.en_US,
variable: item.variable, variable: item.variable,
type: InputVarType.textInput, type: InputVarType.textInput, // TODO: to form input
required: item.required, required: item.required,
}) })
// } // }
@ -176,6 +183,7 @@ const useConfig = (id: string, payload: ToolNodeType) => {
setToolSettingValue, setToolSettingValue,
toolInputVarSchema, toolInputVarSchema,
setInputVar, setInputVar,
filterVar,
currCollection, currCollection,
isShowAuthBtn, isShowAuthBtn,
showSetAuth, showSetAuth,