From ce3556c24037d2a96fa315b767aff2104cdd0ab9 Mon Sep 17 00:00:00 2001 From: heheer Date: Sat, 27 Sep 2025 19:24:12 +0800 Subject: [PATCH] fix: react-hook-form Controller crash with special characters in field names (#5715) --- .../core/chat/components/AIResponseBox.tsx | 27 +++++++++++++------ .../Interactive/InteractiveComponents.tsx | 12 ++++----- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/projects/app/src/components/core/chat/components/AIResponseBox.tsx b/projects/app/src/components/core/chat/components/AIResponseBox.tsx index 685548e40..7bfe7d91d 100644 --- a/projects/app/src/components/core/chat/components/AIResponseBox.tsx +++ b/projects/app/src/components/core/chat/components/AIResponseBox.tsx @@ -217,20 +217,31 @@ const RenderUserFormInteractive = React.memo(function RenderFormInput({ const defaultValues = useMemo(() => { if (interactive.type === 'userInput') { - return interactive.params.inputForm?.reduce((acc: Record, item) => { - acc[item.label] = !!item.value ? item.value : item.defaultValue; + return interactive.params.inputForm?.reduce((acc: Record, item, index) => { + acc[`field_${index}`] = !!item.value ? item.value : item.defaultValue; return acc; }, {}); } return {}; }, [interactive]); - const handleFormSubmit = useCallback((data: Record) => { - onSendPrompt({ - text: JSON.stringify(data), - isInteractivePrompt: true - }); - }, []); + const handleFormSubmit = useCallback( + (data: Record) => { + const finalData: Record = {}; + interactive.params.inputForm?.forEach((item, index) => { + const fieldName = `field_${index}`; + if (fieldName in data) { + finalData[item.label] = data[fieldName]; + } + }); + + onSendPrompt({ + text: JSON.stringify(finalData), + isInteractivePrompt: true + }); + }, + [interactive.params.inputForm] + ); return ( diff --git a/projects/app/src/components/core/chat/components/Interactive/InteractiveComponents.tsx b/projects/app/src/components/core/chat/components/Interactive/InteractiveComponents.tsx index b9c090469..56881a504 100644 --- a/projects/app/src/components/core/chat/components/Interactive/InteractiveComponents.tsx +++ b/projects/app/src/components/core/chat/components/Interactive/InteractiveComponents.tsx @@ -1,5 +1,5 @@ import React, { useCallback } from 'react'; -import { Box, Button, Flex } from '@chakra-ui/react'; +import { Box, Flex } from '@chakra-ui/react'; import { Controller, useForm, type UseFormHandleSubmit } from 'react-hook-form'; import Markdown from '@/components/Markdown'; import QuestionTip from '@fastgpt/web/components/common/MyTooltip/QuestionTip'; @@ -79,12 +79,12 @@ export const FormInputComponent = React.memo(function FormInputComponent({ }); const RenderFormInput = useCallback( - ({ input }: { input: UserInputFormItemType }) => { + ({ input, index }: { input: UserInputFormItemType; index: number }) => { return ( { const inputType = nodeInputTypeToInputType([input.type]); @@ -94,7 +94,7 @@ export const FormInputComponent = React.memo(function FormInputComponent({ inputType={inputType} value={value} onChange={onChange} - placeholder={input.description} + placeholder={input.label} isDisabled={submitted} isInvalid={!!error} maxLength={input.maxLength} @@ -114,14 +114,14 @@ export const FormInputComponent = React.memo(function FormInputComponent({ - {inputForm.map((input) => ( + {inputForm.map((input, index) => ( {input.required && *} {input.label} {input.description && } - + ))}