'use client' import type { AgentAppCreatePayload } from '@dify/contracts/api/console/agent/types.gen' import type { AgentFormValues, AgentIconSelection } from './agent-form' import { Button } from '@langgenius/dify-ui/button' import { Dialog, DialogCloseButton, DialogContent, DialogDescription, DialogTitle, DialogTrigger, } from '@langgenius/dify-ui/dialog' import { Form } from '@langgenius/dify-ui/form' import { toast } from '@langgenius/dify-ui/toast' import { useMutation } from '@tanstack/react-query' import { useState } from 'react' import { useTranslation } from 'react-i18next' import AppIconPicker from '@/app/components/base/app-icon-picker' import { useRouter } from '@/next/navigation' import { consoleQuery } from '@/service/client' import { getAgentDetailPath } from '../../agent-detail/routes' import { defaultAgentIcon } from './agent-form' import { AgentFormFields } from './agent-form-fields' type CreateAgentDialogProps = { open?: boolean onOpenChange?: (open: boolean) => void } export function CreateAgentDialog({ open, onOpenChange }: CreateAgentDialogProps = {}) { const { t } = useTranslation('agentV2') const { t: tCommon } = useTranslation('common') const router = useRouter() const [uncontrolledOpen, setUncontrolledOpen] = useState(false) const [formKey, setFormKey] = useState(0) const [name, setName] = useState('') const [description, setDescription] = useState('') const [role, setRole] = useState('') const [iconPickerOpen, setIconPickerOpen] = useState(false) const [agentIcon, setAgentIcon] = useState(defaultAgentIcon) const createAgentMutation = useMutation(consoleQuery.agent.post.mutationOptions()) const resetForm = () => { setFormKey((key) => key + 1) setName('') setDescription('') setRole('') setAgentIcon(defaultAgentIcon) setIconPickerOpen(false) } const handleOpenChange = (nextOpen: boolean) => { if (open === undefined) setUncontrolledOpen(nextOpen) onOpenChange?.(nextOpen) if (!nextOpen) resetForm() } const handleSubmit = (formValues: AgentFormValues) => { const trimmedName = formValues.name?.trim() ?? '' const trimmedRole = formValues.role?.trim() ?? '' if (createAgentMutation.isPending) return const body = { name: trimmedName, description: formValues.description?.trim() ?? '', role: trimmedRole, icon_type: agentIcon.type, icon: agentIcon.type === 'image' ? agentIcon.fileId : agentIcon.icon, icon_background: agentIcon.type === 'emoji' ? agentIcon.background : undefined, } satisfies AgentAppCreatePayload createAgentMutation.mutate( { body, }, { onSuccess: (createdAgent) => { toast.success(t(($) => $['roster.createSuccess'])) handleOpenChange(false) router.push(getAgentDetailPath(createdAgent.id, 'configure')) }, }, ) } return ( <> {open === undefined && ( }> {t(($) => $['roster.createAgent'])} )}
{t(($) => $['roster.createDialog.title'])} {t(($) => $['roster.createDialog.description'])}
key={formKey} className="min-h-0 flex-1" onFormSubmit={handleSubmit} > $['roster.createForm.changeIcon'])} name={name} role={role} onDescriptionChange={setDescription} onIconClick={() => setIconPickerOpen(true)} onNameChange={setName} onRoleChange={setRole} />
) }