import MarkdownContent from '@/components/next-markdown-content'; import { ButtonLoading } from '@/components/ui/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { RAGFlowSelect } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import { Textarea } from '@/components/ui/textarea'; import { IMessage } from '@/interfaces/database/chat'; import { zodResolver } from '@hookform/resolvers/zod'; import React, { ReactNode, useCallback, useMemo } from 'react'; import { useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { z } from 'zod'; import { BeginQueryType } from '../constant'; import { BeginQuery } from '../interface'; import { FileUploadDirectUpload } from './uploader'; const StringFields = [ BeginQueryType.Line, BeginQueryType.Paragraph, BeginQueryType.Options, ]; interface IProps { parameters: BeginQuery[]; message?: IMessage; ok(parameters: any[]): void; isNext?: boolean; loading?: boolean; submitButtonDisabled?: boolean; btnText?: ReactNode; } const DebugContent = ({ parameters, message, ok, isNext = true, loading = false, submitButtonDisabled = false, btnText, }: IProps) => { const { t } = useTranslation(); const formSchemaValues = useMemo(() => { const obj = parameters.reduce<{ schema: Record; values: Record; }>( (pre, cur, idx) => { const type = cur.type; let fieldSchema; let value; if (StringFields.some((x) => x === type)) { fieldSchema = z.string().trim().min(1); } else if (type === BeginQueryType.Boolean) { fieldSchema = z.boolean(); value = false; } else if (type === BeginQueryType.Integer || type === 'float') { fieldSchema = z.coerce.number(); } else { fieldSchema = z.record(z.any()); } if (cur.optional) { fieldSchema = fieldSchema.optional(); } const index = idx.toString(); pre.schema[index] = fieldSchema; pre.values[index] = value; return pre; }, { schema: {}, values: {} }, ); return { schema: z.object(obj.schema), values: obj.values }; }, [parameters]); const form = useForm>({ defaultValues: formSchemaValues.values, resolver: zodResolver(formSchemaValues.schema), }); const submittable = true; const renderWidget = useCallback( (q: BeginQuery, idx: string) => { const props = { key: idx, label: q.name ?? q.key, name: idx, }; const BeginQueryTypeMap = { [BeginQueryType.Line]: ( ( {props.label} )} /> ), [BeginQueryType.Paragraph]: ( ( {props.label} )} /> ), [BeginQueryType.Options]: ( ( {props.label} ({ label: x, value: x as string, })) ?? [] } {...field} > )} /> ), [BeginQueryType.File]: ( (
{t('assistantAvatar')}
)} />
), [BeginQueryType.Integer]: ( ( {props.label} )} /> ), [BeginQueryType.Boolean]: ( ( {props.label} )} /> ), }; return ( BeginQueryTypeMap[q.type as BeginQueryType] ?? BeginQueryTypeMap[BeginQueryType.Paragraph] ); }, [form, t], ); const onSubmit = useCallback( (values: z.infer) => { const nextValues = Object.entries(values).map(([key, value]) => { const item = parameters[Number(key)]; return { ...item, value }; }); ok(nextValues); }, [formSchemaValues, ok, parameters], ); return ( <>
{message?.data?.tips && (
)}
{parameters.map((x, idx) => { return
{renderWidget(x, idx.toString())}
; })}
{btnText || t(isNext ? 'common.next' : 'flow.run')}
); }; export default DebugContent;