import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' import { RiArrowRightUpLine, } from '@remixicon/react' import Tooltip from '@/app/components/base/tooltip' import Switch from '@/app/components/base/switch' import Input from '@/app/components/workflow/nodes/_base/components/input-support-select-var' import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker' import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector' import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector' import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks' import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { Node } from 'reactflow' import type { NodeOutPutVar, ValueSelector, Var, } from '@/app/components/workflow/types' import type { ToolVarInputs } from '@/app/components/workflow/nodes/tool/types' import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types' import { VarType } from '@/app/components/workflow/types' import cn from '@/utils/classnames' type Props = { value: Record onChange: (val: Record) => void schemas: any[] nodeOutputVars: NodeOutPutVar[], availableNodes: Node[], nodeId: string } const ReasoningConfigForm: React.FC = ({ value, onChange, schemas, nodeOutputVars, availableNodes, nodeId, }) => { const { t } = useTranslation() const language = useLanguage() const handleAutomatic = (key: string, val: any) => { onChange({ ...value, [key]: { value: val ? null : value[key]?.value, auto: val ? 1 : 0, }, }) } const [inputsIsFocus, setInputsIsFocus] = useState>({}) const handleInputFocus = useCallback((variable: string) => { return (value: boolean) => { setInputsIsFocus((prev) => { return { ...prev, [variable]: value, } }) } }, []) const handleNotMixedTypeChange = useCallback((variable: string) => { return (varValue: ValueSelector | string, varKindType: VarKindType) => { const newValue = produce(value, (draft: ToolVarInputs) => { const target = draft[variable].value if (target) { target.type = varKindType target.value = varValue } else { draft[variable].value = { type: varKindType, value: varValue, } } }) onChange(newValue) } }, [value, onChange]) const handleMixedTypeChange = useCallback((variable: string) => { return (itemValue: string) => { const newValue = produce(value, (draft: ToolVarInputs) => { const target = draft[variable].value if (target) { target.value = itemValue } else { draft[variable].value = { type: VarKindType.mixed, value: itemValue, } } }) onChange(newValue) } }, [value, onChange]) const handleFileChange = useCallback((variable: string) => { return (varValue: ValueSelector | string) => { const newValue = produce(value, (draft: ToolVarInputs) => { draft[variable].value = { type: VarKindType.variable, value: varValue, } }) onChange(newValue) } }, [value, onChange]) const handleAppChange = useCallback((variable: string) => { return (app: { app_id: string inputs: Record files?: any[] }) => { const newValue = produce(value, (draft: ToolVarInputs) => { draft[variable].value = app as any }) onChange(newValue) } }, [onChange, value]) const handleModelChange = useCallback((variable: string) => { return (model: any) => { const newValue = produce(value, (draft: ToolVarInputs) => { draft[variable].value = { ...draft[variable].value, ...model, } as any }) onChange(newValue) } }, [onChange, value]) const renderField = (schema: any) => { const { variable, label, required, tooltip, type, scope, url, } = schema const auto = value[variable]?.auto const tooltipContent = (tooltip && ( {tooltip[language] || tooltip.en_US} } triggerClassName='ml-1 w-4 h-4' asChild={false} /> )) const varInput = value[variable].value const isNumber = type === FormTypeEnum.textNumber const isSelect = type === FormTypeEnum.select const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files const isAppSelector = type === FormTypeEnum.appSelector const isModelSelector = type === FormTypeEnum.modelSelector // const isToolSelector = type === FormTypeEnum.toolSelector const isString = !isNumber && !isSelect && !isFile && !isAppSelector && !isModelSelector return (
{label[language] || label.en_US} {required && ( * )} {tooltipContent}
handleAutomatic(variable, !auto)}> {t('plugin.detailPanel.toolSelector.auto')} handleAutomatic(variable, val)} />
{auto === 0 && ( <> {isString && ( )} {/* {isString && ( varPayload.type === VarType.number || varPayload.type === VarType.secret || varPayload.type === VarType.string} /> )} */} {(isNumber || isSelect) && ( varPayload.type === schema._type : undefined} availableVars={isSelect ? nodeOutputVars : undefined} schema={schema} /> )} {isFile && ( varPayload.type === VarType.file || varPayload.type === VarType.arrayFile} /> )} {isAppSelector && ( )} {isModelSelector && ( )} )} {url && ( {t('tools.howToGet')} )}
) } return (
{schemas.map(schema => renderField(schema))}
) } export default ReasoningConfigForm