2025-06-04 15:56:47 +08:00
|
|
|
import { FileUploader } from '@/components/file-uploader';
|
|
|
|
|
import { ButtonLoading } from '@/components/ui/button';
|
2025-02-25 11:32:01 +08:00
|
|
|
import {
|
|
|
|
|
Form,
|
2025-06-04 15:56:47 +08:00
|
|
|
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 { useSetModalState } from '@/hooks/common-hooks';
|
|
|
|
|
import { useSetSelectedRecord } from '@/hooks/logic-hooks';
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2025-02-25 11:32:01 +08:00
|
|
|
import { UploadChangeParam, UploadFile } from 'antd/es/upload';
|
2025-06-04 15:56:47 +08:00
|
|
|
import React, { useCallback, useMemo, useState } from 'react';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
2025-02-25 11:32:01 +08:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-06-04 15:56:47 +08:00
|
|
|
import { z } from 'zod';
|
2025-02-25 11:32:01 +08:00
|
|
|
import { BeginQueryType } from '../constant';
|
|
|
|
|
import { BeginQuery } from '../interface';
|
|
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
parameters: BeginQuery[];
|
|
|
|
|
ok(parameters: any[]): void;
|
|
|
|
|
isNext?: boolean;
|
|
|
|
|
loading?: boolean;
|
|
|
|
|
submitButtonDisabled?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 15:56:47 +08:00
|
|
|
const values = {};
|
|
|
|
|
|
2025-02-25 11:32:01 +08:00
|
|
|
const DebugContent = ({
|
|
|
|
|
parameters,
|
|
|
|
|
ok,
|
|
|
|
|
isNext = true,
|
|
|
|
|
loading = false,
|
|
|
|
|
submitButtonDisabled = false,
|
|
|
|
|
}: IProps) => {
|
|
|
|
|
const { t } = useTranslation();
|
2025-06-04 15:56:47 +08:00
|
|
|
|
|
|
|
|
const FormSchema = useMemo(() => {
|
|
|
|
|
const obj = parameters.reduce((pre, cur, idx) => {
|
|
|
|
|
pre[idx] = z.string().optional();
|
|
|
|
|
return pre;
|
|
|
|
|
}, {});
|
|
|
|
|
return z.object(obj);
|
|
|
|
|
}, [parameters]);
|
|
|
|
|
|
|
|
|
|
const form = useForm({
|
|
|
|
|
defaultValues: values,
|
|
|
|
|
resolver: zodResolver(FormSchema),
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-25 11:32:01 +08:00
|
|
|
const {
|
|
|
|
|
visible,
|
|
|
|
|
hideModal: hidePopover,
|
|
|
|
|
switchVisible,
|
|
|
|
|
showModal: showPopover,
|
|
|
|
|
} = useSetModalState();
|
|
|
|
|
const { setRecord, currentRecord } = useSetSelectedRecord<number>();
|
2025-06-04 15:56:47 +08:00
|
|
|
// const { submittable } = useHandleSubmittable(form);
|
|
|
|
|
const submittable = true;
|
2025-02-25 11:32:01 +08:00
|
|
|
const [isUploading, setIsUploading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleShowPopover = useCallback(
|
|
|
|
|
(idx: number) => () => {
|
|
|
|
|
setRecord(idx);
|
|
|
|
|
showPopover();
|
|
|
|
|
},
|
|
|
|
|
[setRecord, showPopover],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const normFile = (e: any) => {
|
|
|
|
|
if (Array.isArray(e)) {
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
return e?.fileList;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onChange = useCallback(
|
|
|
|
|
(optional: boolean) =>
|
|
|
|
|
({ fileList }: UploadChangeParam<UploadFile>) => {
|
|
|
|
|
if (!optional) {
|
|
|
|
|
setIsUploading(fileList.some((x) => x.status === 'uploading'));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const renderWidget = useCallback(
|
2025-06-04 15:56:47 +08:00
|
|
|
(q: BeginQuery, idx: string) => {
|
|
|
|
|
const props = {
|
2025-02-25 11:32:01 +08:00
|
|
|
key: idx,
|
|
|
|
|
label: q.name ?? q.key,
|
|
|
|
|
name: idx,
|
|
|
|
|
};
|
|
|
|
|
if (q.optional === false) {
|
|
|
|
|
props.rules = [{ required: true }];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-04 15:56:47 +08:00
|
|
|
// const urlList: { url: string; result: string }[] =
|
|
|
|
|
// form.getFieldValue(idx) || [];
|
|
|
|
|
|
|
|
|
|
const urlList: { url: string; result: string }[] = [];
|
2025-02-25 11:32:01 +08:00
|
|
|
|
|
|
|
|
const BeginQueryTypeMap = {
|
|
|
|
|
[BeginQueryType.Line]: (
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={props.name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>{props.label}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input {...field}></Input>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
),
|
|
|
|
|
[BeginQueryType.Paragraph]: (
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={props.name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>{props.label}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Textarea rows={1} {...field}></Textarea>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
),
|
|
|
|
|
[BeginQueryType.Options]: (
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={props.name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>{props.label}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<RAGFlowSelect
|
|
|
|
|
allowClear
|
|
|
|
|
options={
|
|
|
|
|
q.options?.map((x) => ({ label: x, value: x })) ?? []
|
|
|
|
|
}
|
|
|
|
|
{...field}
|
|
|
|
|
></RAGFlowSelect>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
),
|
|
|
|
|
[BeginQueryType.File]: (
|
|
|
|
|
<React.Fragment key={idx}>
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={'file'}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<div className="space-y-6">
|
|
|
|
|
<FormItem className="w-full">
|
|
|
|
|
<FormLabel>{t('assistantAvatar')}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<FileUploader
|
|
|
|
|
value={field.value}
|
|
|
|
|
onValueChange={field.onChange}
|
|
|
|
|
maxFileCount={1}
|
|
|
|
|
maxSize={4 * 1024 * 1024}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
</React.Fragment>
|
|
|
|
|
),
|
|
|
|
|
[BeginQueryType.Integer]: (
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={props.name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>{props.label}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input type="number" {...field}></Input>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
),
|
|
|
|
|
[BeginQueryType.Boolean]: (
|
2025-06-04 15:56:47 +08:00
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={props.name}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex-1">
|
|
|
|
|
<FormLabel>{props.label}</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={field.value}
|
|
|
|
|
onCheckedChange={field.onChange}
|
|
|
|
|
></Switch>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2025-02-25 11:32:01 +08:00
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
BeginQueryTypeMap[q.type as BeginQueryType] ??
|
|
|
|
|
BeginQueryTypeMap[BeginQueryType.Paragraph]
|
|
|
|
|
);
|
|
|
|
|
},
|
2025-06-04 15:56:47 +08:00
|
|
|
[form, t],
|
2025-02-25 11:32:01 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onOk = useCallback(async () => {
|
2025-06-04 15:56:47 +08:00
|
|
|
// const values = await form.validateFields();
|
2025-02-25 11:32:01 +08:00
|
|
|
const nextValues = Object.entries(values).map(([key, value]) => {
|
|
|
|
|
const item = parameters[Number(key)];
|
|
|
|
|
let nextValue = value;
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
nextValue = ``;
|
|
|
|
|
|
|
|
|
|
value.forEach((x) => {
|
|
|
|
|
nextValue +=
|
|
|
|
|
x?.originFileObj instanceof File
|
|
|
|
|
? `${x.name}\n${x.response?.data}\n----\n`
|
|
|
|
|
: `${x.url}\n${x.result}\n----\n`;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return { ...item, value: nextValue };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ok(nextValues);
|
2025-06-04 15:56:47 +08:00
|
|
|
}, [ok, parameters]);
|
|
|
|
|
|
|
|
|
|
const onSubmit = useCallback(
|
|
|
|
|
(values: z.infer<typeof FormSchema>) => {
|
|
|
|
|
const nextValues = Object.entries(values).map(([key, value]) => {
|
|
|
|
|
const item = parameters[Number(key)];
|
|
|
|
|
let nextValue = value;
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
nextValue = ``;
|
|
|
|
|
|
|
|
|
|
value.forEach((x) => {
|
|
|
|
|
nextValue +=
|
|
|
|
|
x?.originFileObj instanceof File
|
|
|
|
|
? `${x.name}\n${x.response?.data}\n----\n`
|
|
|
|
|
: `${x.url}\n${x.result}\n----\n`;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return { ...item, value: nextValue };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ok(nextValues);
|
|
|
|
|
},
|
|
|
|
|
[ok, parameters],
|
|
|
|
|
);
|
2025-02-25 11:32:01 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2025-06-04 15:56:47 +08:00
|
|
|
<section>
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
2025-02-25 11:32:01 +08:00
|
|
|
{parameters.map((x, idx) => {
|
2025-06-04 15:56:47 +08:00
|
|
|
return <div key={idx}>{renderWidget(x, idx.toString())}</div>;
|
2025-02-25 11:32:01 +08:00
|
|
|
})}
|
2025-06-04 15:56:47 +08:00
|
|
|
</form>
|
|
|
|
|
</Form>
|
2025-02-25 11:32:01 +08:00
|
|
|
</section>
|
2025-06-04 15:56:47 +08:00
|
|
|
<ButtonLoading
|
2025-02-25 11:32:01 +08:00
|
|
|
onClick={onOk}
|
|
|
|
|
loading={loading}
|
|
|
|
|
disabled={!submittable || isUploading || submitButtonDisabled}
|
|
|
|
|
>
|
|
|
|
|
{t(isNext ? 'common.next' : 'flow.run')}
|
2025-06-04 15:56:47 +08:00
|
|
|
</ButtonLoading>
|
2025-02-25 11:32:01 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DebugContent;
|