| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- import { Collapse } from '@/components/collapse';
- import { FormContainer } from '@/components/form-container';
- 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 {
- Form,
- FormControl,
- FormField,
- FormItem,
- FormLabel,
- } from '@/components/ui/form';
- import { Input, NumberInput } from '@/components/ui/input';
- 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,
- NodeHandleId,
- VariableType,
- initialAgentValues,
- } from '../../constant';
- import { INextOperatorForm } from '../../interface';
- import useGraphStore from '../../store';
- import { 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 { 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(),
- tools: z
- .array(
- z.object({
- component_name: z.string(),
- }),
- )
- .optional(),
- ...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(),
- });
-
- const outputList = buildOutputList(initialAgentValues.outputs);
-
- function AgentForm({ node }: INextOperatorForm) {
- const { t } = useTranslation();
- const { edges, deleteEdgesBySourceAndSourceHandle } = useGraphStore(
- (state) => state,
- );
-
- const defaultValues = useValues(node);
-
- 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<z.infer<typeof FormSchema>>({
- 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',
- });
-
- useEffect(() => {
- if (exceptionMethod !== AgentExceptionMethod.Goto) {
- if (node?.id) {
- deleteEdgesBySourceAndSourceHandle(
- node?.id,
- NodeHandleId.AgentException,
- );
- }
- }
- }, [deleteEdgesBySourceAndSourceHandle, exceptionMethod, node?.id]);
-
- useWatchFormChange(node?.id, form);
-
- return (
- <Form {...form}>
- <FormWrapper>
- <FormContainer>
- {isSubAgent && <DescriptionField></DescriptionField>}
- <LargeModelFormField></LargeModelFormField>
- {findLlmByUuid(llmId)?.model_type === LlmModelType.Image2text && (
- <QueryVariable
- name="visual_files_var"
- label="Visual Input File"
- type={VariableType.File}
- ></QueryVariable>
- )}
- </FormContainer>
-
- <FormContainer>
- <FormField
- control={form.control}
- name={`sys_prompt`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>System Prompt</FormLabel>
- <FormControl>
- <PromptEditor
- {...field}
- placeholder={t('flow.messagePlaceholder')}
- showToolbar={false}
- ></PromptEditor>
- </FormControl>
- </FormItem>
- )}
- />
- </FormContainer>
- {isSubAgent || (
- <FormContainer>
- {/* <DynamicPrompt></DynamicPrompt> */}
- <FormField
- control={form.control}
- name={`prompts`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>User Prompt</FormLabel>
- <FormControl>
- <section>
- <PromptEditor
- {...field}
- showToolbar={false}
- ></PromptEditor>
- </section>
- </FormControl>
- </FormItem>
- )}
- />
- </FormContainer>
- )}
-
- <FormContainer>
- <AgentTools></AgentTools>
- <Agents node={node}></Agents>
- </FormContainer>
- <Collapse title={<div>Advanced Settings</div>}>
- <FormContainer>
- <MessageHistoryWindowSizeFormField></MessageHistoryWindowSizeFormField>
- <FormField
- control={form.control}
- name={`cite`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel tooltip={t('flow.citeTip')}>
- {t('flow.cite')}
- </FormLabel>
- <FormControl>
- <Switch
- checked={field.value}
- onCheckedChange={field.onChange}
- ></Switch>
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name={`max_retries`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Max retries</FormLabel>
- <FormControl>
- <NumberInput {...field} max={8}></NumberInput>
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name={`delay_after_error`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Delay after error</FormLabel>
- <FormControl>
- <NumberInput {...field} max={5} step={0.1}></NumberInput>
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name={`max_rounds`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Max rounds</FormLabel>
- <FormControl>
- <NumberInput {...field}></NumberInput>
- </FormControl>
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name={`exception_method`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Exception method</FormLabel>
- <FormControl>
- <SelectWithSearch
- {...field}
- options={ExceptionMethodOptions}
- allowClear
- />
- </FormControl>
- </FormItem>
- )}
- />
- {exceptionMethod === AgentExceptionMethod.Comment && (
- <FormField
- control={form.control}
- name={`exception_default_value`}
- render={({ field }) => (
- <FormItem className="flex-1">
- <FormLabel>Exception default value</FormLabel>
- <FormControl>
- <Input {...field} />
- </FormControl>
- </FormItem>
- )}
- />
- )}
- </FormContainer>
- </Collapse>
- <Output list={outputList}></Output>
- </FormWrapper>
- </Form>
- );
- }
-
- export default memo(AgentForm);
|