mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-14 17:08:31 +08:00
### What problem does this PR solve? Feat: Initialize the data pipeline canvas. #9869 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
166
web/src/pages/data-flow/form/string-transform-form/index.tsx
Normal file
166
web/src/pages/data-flow/form/string-transform-form/index.tsx
Normal file
@@ -0,0 +1,166 @@
|
||||
import { FormContainer } from '@/components/form-container';
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from '@/components/ui/form';
|
||||
import { MultiSelect } from '@/components/ui/multi-select';
|
||||
import { RAGFlowSelect } from '@/components/ui/select';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { t } from 'i18next';
|
||||
import { toLower } from 'lodash';
|
||||
import { memo, useCallback, useMemo } from 'react';
|
||||
import { useForm, useWatch } from 'react-hook-form';
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
StringTransformDelimiter,
|
||||
StringTransformMethod,
|
||||
initialStringTransformValues,
|
||||
} from '../../constant';
|
||||
import { INextOperatorForm } from '../../interface';
|
||||
import { FormWrapper } from '../components/form-wrapper';
|
||||
import { Output, transferOutputs } from '../components/output';
|
||||
import { PromptEditor } from '../components/prompt-editor';
|
||||
import { QueryVariable } from '../components/query-variable';
|
||||
import { useValues } from './use-values';
|
||||
import { useWatchFormChange } from './use-watch-form-change';
|
||||
|
||||
const DelimiterOptions = Object.entries(StringTransformDelimiter).map(
|
||||
([key, val]) => ({ label: t('flow.' + toLower(key)), value: val }),
|
||||
);
|
||||
|
||||
function StringTransformForm({ node }: INextOperatorForm) {
|
||||
const values = useValues(node);
|
||||
|
||||
const FormSchema = z.object({
|
||||
method: z.string(),
|
||||
split_ref: z.string().optional(),
|
||||
script: z.string().optional(),
|
||||
delimiters: z.array(z.string()).or(z.string()),
|
||||
outputs: z.object({ result: z.object({ type: z.string() }) }).optional(),
|
||||
});
|
||||
|
||||
const form = useForm<z.infer<typeof FormSchema>>({
|
||||
defaultValues: values,
|
||||
resolver: zodResolver(FormSchema),
|
||||
});
|
||||
|
||||
const method = useWatch({ control: form.control, name: 'method' });
|
||||
|
||||
const isSplit = method === StringTransformMethod.Split;
|
||||
|
||||
const outputList = useMemo(() => {
|
||||
return transferOutputs(values.outputs);
|
||||
}, [values.outputs]);
|
||||
|
||||
const handleMethodChange = useCallback(
|
||||
(value: StringTransformMethod) => {
|
||||
const isMerge = value === StringTransformMethod.Merge;
|
||||
const outputs = {
|
||||
...initialStringTransformValues.outputs,
|
||||
result: {
|
||||
type: isMerge ? 'string' : 'Array<string>',
|
||||
},
|
||||
};
|
||||
form.setValue('outputs', outputs);
|
||||
form.setValue(
|
||||
'delimiters',
|
||||
isMerge ? StringTransformDelimiter.Comma : [],
|
||||
);
|
||||
},
|
||||
[form],
|
||||
);
|
||||
|
||||
useWatchFormChange(node?.id, form);
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<FormWrapper>
|
||||
<FormContainer>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="method"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('flow.method')}</FormLabel>
|
||||
<FormControl>
|
||||
<RAGFlowSelect
|
||||
{...field}
|
||||
options={Object.values(StringTransformMethod).map(
|
||||
(val) => ({ label: t('flow.' + val), value: val }),
|
||||
)}
|
||||
onChange={(value) => {
|
||||
handleMethodChange(value);
|
||||
field.onChange(value);
|
||||
}}
|
||||
></RAGFlowSelect>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{isSplit && (
|
||||
<QueryVariable
|
||||
label={<FormLabel>split_ref</FormLabel>}
|
||||
name="split_ref"
|
||||
></QueryVariable>
|
||||
)}
|
||||
{isSplit || (
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="script"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('flow.script')}</FormLabel>
|
||||
<FormControl>
|
||||
<PromptEditor {...field} showToolbar={false}></PromptEditor>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="delimiters"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('flow.delimiters')}</FormLabel>
|
||||
<FormControl>
|
||||
{isSplit ? (
|
||||
<MultiSelect
|
||||
options={DelimiterOptions}
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value as string[]}
|
||||
variant="inverted"
|
||||
// {...field}
|
||||
/>
|
||||
) : (
|
||||
<RAGFlowSelect
|
||||
{...field}
|
||||
options={DelimiterOptions}
|
||||
></RAGFlowSelect>
|
||||
)}
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="outputs"
|
||||
render={() => <div></div>}
|
||||
/>
|
||||
</FormContainer>
|
||||
</FormWrapper>
|
||||
<div className="p-5">
|
||||
<Output list={outputList}></Output>
|
||||
</div>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
export default memo(StringTransformForm);
|
||||
@@ -0,0 +1,33 @@
|
||||
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
||||
import { isEmpty } from 'lodash';
|
||||
import { useMemo } from 'react';
|
||||
import {
|
||||
initialStringTransformValues,
|
||||
StringTransformMethod,
|
||||
} from '../../constant';
|
||||
|
||||
function transferDelimiters(formData: typeof initialStringTransformValues) {
|
||||
return formData.method === StringTransformMethod.Merge
|
||||
? formData.delimiters[0]
|
||||
: formData.delimiters;
|
||||
}
|
||||
|
||||
export function useValues(node?: RAGFlowNodeType) {
|
||||
const values = useMemo(() => {
|
||||
const formData = node?.data?.form;
|
||||
|
||||
if (isEmpty(formData)) {
|
||||
return {
|
||||
...initialStringTransformValues,
|
||||
delimiters: transferDelimiters(formData),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...formData,
|
||||
delimiters: transferDelimiters(formData),
|
||||
};
|
||||
}, [node?.data?.form]);
|
||||
|
||||
return values;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from 'react';
|
||||
import { UseFormReturn, useWatch } from 'react-hook-form';
|
||||
import { StringTransformMethod } from '../../constant';
|
||||
import useGraphStore from '../../store';
|
||||
|
||||
export function useWatchFormChange(id?: string, form?: UseFormReturn<any>) {
|
||||
let values = useWatch({ control: form?.control });
|
||||
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
|
||||
|
||||
useEffect(() => {
|
||||
// Manually triggered form updates are synchronized to the canvas
|
||||
if (id && form?.formState.isDirty) {
|
||||
values = form?.getValues();
|
||||
let nextValues: any = values;
|
||||
|
||||
if (
|
||||
values.delimiters !== undefined &&
|
||||
values.method === StringTransformMethod.Merge
|
||||
) {
|
||||
nextValues.delimiters = [values.delimiters];
|
||||
}
|
||||
|
||||
updateNodeForm(id, nextValues);
|
||||
}
|
||||
}, [form?.formState.isDirty, id, updateNodeForm, values]);
|
||||
}
|
||||
Reference in New Issue
Block a user