import { Collapse } from '@/components/collapse'; import { LargeModelFilterFormSchema, LargeModelFormField, } from '@/components/large-model-form-field'; import { LlmSettingSchema } from '@/components/llm-setting-items/next'; import { MessageHistoryWindowSizeFormField } from '@/components/message-history-window-size-item'; import { SelectWithSearch } from '@/components/originui/select-with-search'; import { RAGFlowFormItem } from '@/components/ragflow-form'; import { Button } from '@/components/ui/button'; import { Form, FormControl, FormField, FormItem, FormLabel, } from '@/components/ui/form'; import { Input, NumberInput } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Separator } from '@/components/ui/separator'; import { Switch } from '@/components/ui/switch'; import { LlmModelType } from '@/constants/knowledge'; import { useFindLlmByUuid } from '@/hooks/use-llm-request'; import { zodResolver } from '@hookform/resolvers/zod'; import { memo, useEffect, useMemo } from 'react'; import { useForm, useWatch } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { z } from 'zod'; import { AgentExceptionMethod, AgentStructuredOutputField, NodeHandleId, VariableType, } from '../../constant'; import { INextOperatorForm } from '../../interface'; import useGraphStore from '../../store'; import { hasSubAgentOrTool, isBottomSubAgent } from '../../utils'; import { buildOutputList } from '../../utils/build-output-list'; import { DescriptionField } from '../components/description-field'; import { FormWrapper } from '../components/form-wrapper'; import { Output } from '../components/output'; import { PromptEditor } from '../components/prompt-editor'; import { QueryVariable } from '../components/query-variable'; import { AgentTools, Agents } from './agent-tools'; import { StructuredOutputDialog } from './structured-output-dialog'; import { StructuredOutputPanel } from './structured-output-panel'; import { useBuildPromptExtraPromptOptions } from './use-build-prompt-options'; import { useShowStructuredOutputDialog } from './use-show-structured-output-dialog'; import { useValues } from './use-values'; import { useWatchFormChange } from './use-watch-change'; const FormSchema = z.object({ sys_prompt: z.string(), description: z.string().optional(), user_prompt: z.string().optional(), prompts: z.string().optional(), // prompts: z // .array( // z.object({ // role: z.string(), // content: z.string(), // }), // ) // .optional(), message_history_window_size: z.coerce.number(), ...LlmSettingSchema, max_retries: z.coerce.number(), delay_after_error: z.coerce.number().optional(), visual_files_var: z.string().optional(), max_rounds: z.coerce.number().optional(), exception_method: z.string().optional(), exception_goto: z.array(z.string()).optional(), exception_default_value: z.string().optional(), ...LargeModelFilterFormSchema, cite: z.boolean().optional(), showStructuredOutput: z.boolean().optional(), [AgentStructuredOutputField]: z.record(z.any()), }); export type AgentFormSchemaType = z.infer; function AgentForm({ node }: INextOperatorForm) { const { t } = useTranslation(); const { edges, deleteEdgesBySourceAndSourceHandle } = useGraphStore( (state) => state, ); const outputList = buildOutputList(node?.data.form.outputs); const defaultValues = useValues(node); const { extraOptions } = useBuildPromptExtraPromptOptions(edges, node?.id); const ExceptionMethodOptions = Object.values(AgentExceptionMethod).map( (x) => ({ label: t(`flow.${x}`), value: x, }), ); const isSubAgent = useMemo(() => { return isBottomSubAgent(edges, node?.id); }, [edges, node?.id]); const form = useForm({ defaultValues: defaultValues, resolver: zodResolver(FormSchema), }); const llmId = useWatch({ control: form.control, name: 'llm_id' }); const findLlmByUuid = useFindLlmByUuid(); const exceptionMethod = useWatch({ control: form.control, name: 'exception_method', }); const showStructuredOutput = useWatch({ control: form.control, name: 'showStructuredOutput', }); const { initialStructuredOutput, showStructuredOutputDialog, structuredOutputDialogVisible, hideStructuredOutputDialog, handleStructuredOutputDialogOk, } = useShowStructuredOutputDialog(form); useEffect(() => { if (exceptionMethod !== AgentExceptionMethod.Goto) { if (node?.id) { deleteEdgesBySourceAndSourceHandle( node?.id, NodeHandleId.AgentException, ); } } }, [deleteEdgesBySourceAndSourceHandle, exceptionMethod, node?.id]); useWatchFormChange(node?.id, form); return ( <>
{isSubAgent && } {findLlmByUuid(llmId)?.model_type === LlmModelType.Image2text && ( )} ( {t('flow.systemPrompt')} )} /> {isSubAgent || ( ( {t('flow.userPrompt')}
)} /> )} {t('flow.advancedSettings')}}>
( {t('flow.cite')} )} /> ( {t('flow.maxRetries')} )} /> ( {t('flow.delayEfterError')} )} /> {hasSubAgentOrTool(edges, node?.id) && ( ( {t('flow.maxRounds')} )} /> )} ( {t('flow.exceptionMethod')} )} /> {exceptionMethod === AgentExceptionMethod.Comment && ( ( {t('flow.ExceptionDefaultValue')} )} /> )}
{(field) => (
)}
{showStructuredOutput && (
{t('flow.structuredOutput.structuredOutput')}
)}
{structuredOutputDialogVisible && ( )} ); } export default memo(AgentForm);