mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-23 15:06:50 +08:00
### What problem does this PR solve? Feat: Add DynamicPrompt component #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
97
web/src/pages/agent/form/agent-form/dynamic-prompt.tsx
Normal file
97
web/src/pages/agent/form/agent-form/dynamic-prompt.tsx
Normal file
@ -0,0 +1,97 @@
|
||||
import { PromptEditor } from '@/components/prompt-editor';
|
||||
import { BlockButton, Button } from '@/components/ui/button';
|
||||
import {
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from '@/components/ui/form';
|
||||
import { RAGFlowSelect } from '@/components/ui/select';
|
||||
import { X } from 'lucide-react';
|
||||
import { memo } from 'react';
|
||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
export enum PromptRole {
|
||||
User = 'user',
|
||||
Assistant = 'assistant',
|
||||
}
|
||||
|
||||
const options = [
|
||||
{ label: 'User', value: PromptRole.User },
|
||||
{ label: 'Assistant', value: PromptRole.Assistant },
|
||||
];
|
||||
|
||||
const DynamicPrompt = () => {
|
||||
const { t } = useTranslation();
|
||||
const form = useFormContext();
|
||||
const name = 'prompts';
|
||||
|
||||
const { fields, append, remove } = useFieldArray({
|
||||
name: name,
|
||||
control: form.control,
|
||||
});
|
||||
|
||||
return (
|
||||
<FormItem>
|
||||
<FormLabel tooltip={t('flow.msgTip')}>{t('flow.msg')}</FormLabel>
|
||||
<div className="space-y-4">
|
||||
{fields.map((field, index) => (
|
||||
<div key={field.id} className="flex">
|
||||
<div className="space-y-2 flex-1">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`${name}.${index}.role`}
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-1/3">
|
||||
<FormLabel />
|
||||
<FormControl>
|
||||
<RAGFlowSelect
|
||||
{...field}
|
||||
options={options}
|
||||
></RAGFlowSelect>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`${name}.${index}.content`}
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex-1">
|
||||
<FormControl>
|
||||
<section>
|
||||
<PromptEditor
|
||||
{...field}
|
||||
showToolbar={false}
|
||||
></PromptEditor>
|
||||
</section>
|
||||
</FormControl>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant={'ghost'}
|
||||
onClick={() => remove(index)}
|
||||
>
|
||||
<X />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<FormMessage />
|
||||
<BlockButton
|
||||
onClick={() => append({ content: '', role: PromptRole.User })}
|
||||
>
|
||||
Add
|
||||
</BlockButton>
|
||||
</FormItem>
|
||||
);
|
||||
};
|
||||
|
||||
export default memo(DynamicPrompt);
|
||||
Reference in New Issue
Block a user