mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-06-30 15:45:09 +08:00
### What problem does this PR solve? Feat: Add return value widget to CodeForm #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
parent
558b252c5a
commit
ac2643700b
@ -1,3 +1,4 @@
|
|||||||
|
import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { Operator } from '../constant';
|
import { Operator } from '../constant';
|
||||||
@ -133,13 +134,17 @@ export function useFormConfigMap() {
|
|||||||
},
|
},
|
||||||
[Operator.Code]: {
|
[Operator.Code]: {
|
||||||
component: CodeForm,
|
component: CodeForm,
|
||||||
defaultValues: { arguments: [] },
|
defaultValues: {
|
||||||
|
lang: ProgrammingLanguage.Python,
|
||||||
|
script: CodeTemplateStrMap[ProgrammingLanguage.Python],
|
||||||
|
arguments: [],
|
||||||
|
},
|
||||||
schema: z.object({
|
schema: z.object({
|
||||||
|
lang: z.string(),
|
||||||
script: z.string(),
|
script: z.string(),
|
||||||
arguments: z.array(
|
arguments: z.array(
|
||||||
z.object({ name: z.string(), component_id: z.string() }),
|
z.object({ name: z.string(), component_id: z.string() }),
|
||||||
),
|
),
|
||||||
lang: z.string(),
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
[Operator.Baidu]: {
|
[Operator.Baidu]: {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Editor, { loader } from '@monaco-editor/react';
|
import Editor, { loader } from '@monaco-editor/react';
|
||||||
import { INextOperatorForm } from '../../interface';
|
import { INextOperatorForm } from '../../interface';
|
||||||
|
|
||||||
|
import { FormContainer } from '@/components/form-container';
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
FormControl,
|
FormControl,
|
||||||
@ -9,11 +10,17 @@ import {
|
|||||||
FormLabel,
|
FormLabel,
|
||||||
FormMessage,
|
FormMessage,
|
||||||
} from '@/components/ui/form';
|
} from '@/components/ui/form';
|
||||||
|
import { Input } from '@/components/ui/input';
|
||||||
import { RAGFlowSelect } from '@/components/ui/select';
|
import { RAGFlowSelect } from '@/components/ui/select';
|
||||||
import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
|
import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
|
||||||
import { ICodeForm } from '@/interfaces/database/flow';
|
import { ICodeForm } from '@/interfaces/database/flow';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { DynamicInputVariable } from './next-variable';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import {
|
||||||
|
DynamicInputVariable,
|
||||||
|
TypeOptions,
|
||||||
|
VariableTitle,
|
||||||
|
} from './next-variable';
|
||||||
|
|
||||||
loader.config({ paths: { vs: '/vs' } });
|
loader.config({ paths: { vs: '/vs' } });
|
||||||
|
|
||||||
@ -24,6 +31,7 @@ const options = [
|
|||||||
|
|
||||||
const CodeForm = ({ form, node }: INextOperatorForm) => {
|
const CodeForm = ({ form, node }: INextOperatorForm) => {
|
||||||
const formData = node?.data.form as ICodeForm;
|
const formData = node?.data.form as ICodeForm;
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// TODO: Direct operation zustand is more elegant
|
// TODO: Direct operation zustand is more elegant
|
||||||
@ -35,13 +43,23 @@ const CodeForm = ({ form, node }: INextOperatorForm) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<DynamicInputVariable node={node}></DynamicInputVariable>
|
<form
|
||||||
|
className="p-5 space-y-5"
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DynamicInputVariable
|
||||||
|
node={node}
|
||||||
|
title={t('flow.input')}
|
||||||
|
></DynamicInputVariable>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="script"
|
name="script"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>
|
<FormLabel className="flex items-center justify-between">
|
||||||
|
Code
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="lang"
|
name="lang"
|
||||||
@ -57,7 +75,7 @@ const CodeForm = ({ form, node }: INextOperatorForm) => {
|
|||||||
</FormLabel>
|
</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Editor
|
<Editor
|
||||||
height={600}
|
height={300}
|
||||||
theme="vs-dark"
|
theme="vs-dark"
|
||||||
language={formData.lang}
|
language={formData.lang}
|
||||||
options={{
|
options={{
|
||||||
@ -71,6 +89,54 @@ const CodeForm = ({ form, node }: INextOperatorForm) => {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{formData.lang === ProgrammingLanguage.Python ? (
|
||||||
|
<DynamicInputVariable
|
||||||
|
node={node}
|
||||||
|
title={'Return Values'}
|
||||||
|
name={'return'}
|
||||||
|
></DynamicInputVariable>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
<VariableTitle title={'Return Values'}></VariableTitle>
|
||||||
|
<FormContainer className="space-y-5">
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name={'return.name'}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Name</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
{...field}
|
||||||
|
placeholder={t('common.pleaseInput')}
|
||||||
|
></Input>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name={`return.component_id`}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="flex-1">
|
||||||
|
<FormLabel>Type</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<RAGFlowSelect
|
||||||
|
placeholder={t('common.pleaseSelect')}
|
||||||
|
options={TypeOptions}
|
||||||
|
{...field}
|
||||||
|
></RAGFlowSelect>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</FormContainer>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,23 +1,19 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { SideDown } from '@/assets/icon/Icon';
|
import { FormContainer } from '@/components/form-container';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import {
|
|
||||||
Collapsible,
|
|
||||||
CollapsibleContent,
|
|
||||||
CollapsibleTrigger,
|
|
||||||
} from '@/components/ui/collapsible';
|
|
||||||
import {
|
import {
|
||||||
FormControl,
|
FormControl,
|
||||||
FormDescription,
|
|
||||||
FormField,
|
FormField,
|
||||||
FormItem,
|
FormItem,
|
||||||
FormMessage,
|
FormMessage,
|
||||||
} from '@/components/ui/form';
|
} from '@/components/ui/form';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { RAGFlowSelect } from '@/components/ui/select';
|
import { RAGFlowSelect } from '@/components/ui/select';
|
||||||
|
import { Separator } from '@/components/ui/separator';
|
||||||
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
||||||
import { Plus, Trash2 } from 'lucide-react';
|
import { Plus, X } from 'lucide-react';
|
||||||
|
import { ReactNode } from 'react';
|
||||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
|
import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
|
||||||
@ -27,6 +23,15 @@ interface IProps {
|
|||||||
name?: string;
|
name?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const TypeOptions = [
|
||||||
|
'String',
|
||||||
|
'Number',
|
||||||
|
'Boolean',
|
||||||
|
'Array[String]',
|
||||||
|
'Array[Number]',
|
||||||
|
'Object',
|
||||||
|
].map((x) => ({ label: x, value: x }));
|
||||||
|
|
||||||
export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const form = useFormContext();
|
const form = useFormContext();
|
||||||
@ -42,17 +47,16 @@ export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="space-y-5">
|
||||||
{fields.map((field, index) => {
|
{fields.map((field, index) => {
|
||||||
const typeField = `${name}.${index}.name`;
|
const typeField = `${name}.${index}.name`;
|
||||||
return (
|
return (
|
||||||
<div key={field.id} className="flex items-center gap-1">
|
<div key={field.id} className="flex items-center gap-2">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name={typeField}
|
name={typeField}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="w-2/5">
|
<FormItem className="w-2/5">
|
||||||
<FormDescription />
|
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input
|
||||||
{...field}
|
{...field}
|
||||||
@ -63,16 +67,18 @@ export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
<Separator className="w-3 text-text-sub-title" />
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name={`${name}.${index}.component_id`}
|
name={`${name}.${index}.component_id`}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex-1">
|
<FormItem className="flex-1">
|
||||||
<FormDescription />
|
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<RAGFlowSelect
|
<RAGFlowSelect
|
||||||
placeholder={t('common.pleaseSelect')}
|
placeholder={t('common.pleaseSelect')}
|
||||||
options={valueOptions}
|
options={
|
||||||
|
name === 'arguments' ? valueOptions : TypeOptions
|
||||||
|
}
|
||||||
{...field}
|
{...field}
|
||||||
></RAGFlowSelect>
|
></RAGFlowSelect>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
@ -80,18 +86,16 @@ export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
<Trash2
|
<Button variant={'ghost'} onClick={() => remove(index)}>
|
||||||
className="cursor-pointer mx-3 size-4 text-colors-text-functional-danger"
|
<X className="text-text-sub-title-invert " />
|
||||||
onClick={() => remove(index)}
|
</Button>
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
<Button
|
<Button
|
||||||
onClick={() => append({ name: '', component_id: undefined })}
|
onClick={() => append({ name: '', component_id: undefined })}
|
||||||
className="mt-4"
|
className="mt-4 border-dashed w-full"
|
||||||
variant={'outline'}
|
variant={'outline'}
|
||||||
size={'sm'}
|
|
||||||
>
|
>
|
||||||
<Plus />
|
<Plus />
|
||||||
{t('flow.addVariable')}
|
{t('flow.addVariable')}
|
||||||
@ -100,22 +104,21 @@ export function DynamicVariableForm({ node, name = 'arguments' }: IProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function DynamicInputVariable({ node }: IProps) {
|
export function VariableTitle({ title }: { title: ReactNode }) {
|
||||||
const { t } = useTranslation();
|
return <div className="font-medium text-text-title pb-2">{title}</div>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DynamicInputVariable({
|
||||||
|
node,
|
||||||
|
name,
|
||||||
|
title,
|
||||||
|
}: IProps & { title: ReactNode }) {
|
||||||
return (
|
return (
|
||||||
<Collapsible defaultOpen className="group/collapsible">
|
<section>
|
||||||
<CollapsibleTrigger className="flex justify-between w-full pb-2">
|
<VariableTitle title={title}></VariableTitle>
|
||||||
<span className="font-bold text-2xl text-colors-text-neutral-strong">
|
<FormContainer>
|
||||||
{t('flow.input')}
|
<DynamicVariableForm node={node} name={name}></DynamicVariableForm>
|
||||||
</span>
|
</FormContainer>
|
||||||
<Button variant={'icon'} size={'icon'}>
|
</section>
|
||||||
<SideDown />
|
|
||||||
</Button>
|
|
||||||
</CollapsibleTrigger>
|
|
||||||
<CollapsibleContent>
|
|
||||||
<DynamicVariableForm node={node}></DynamicVariableForm>
|
|
||||||
</CollapsibleContent>
|
|
||||||
</Collapsible>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user