diff --git a/chatui/src/app/libs/agents/use-agents.mutation.ts b/chatui/src/app/libs/agents/use-agents.mutation.ts index 48d00d4337..f37b45e110 100644 --- a/chatui/src/app/libs/agents/use-agents.mutation.ts +++ b/chatui/src/app/libs/agents/use-agents.mutation.ts @@ -5,12 +5,21 @@ import { Agent } from './agent'; export const useAgentsCreateMutation = (userId: string | null | undefined) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn: async (params: { name: string; human: string; persona: string; model: string }) => - (await fetch(API_BASE_URL + '/agents', { + mutationFn: async (params: { name: string; human: string; persona: string; model: string }) => { + const response = await fetch(API_BASE_URL + '/agents', { method: 'POST', headers: { 'Content-Type': ' application/json' }, body: JSON.stringify({ config: params, user_id: userId }), - }).then((res) => res.json())) as Promise, + }); + + if (!response.ok) { + // Throw an error if the response is not OK + const errorBody = await response.text(); + throw new Error(errorBody || 'Error creating agent'); + } + + return response.json() as Promise; + }, onSuccess: () => queryClient.invalidateQueries({ queryKey: [userId, 'agents', 'list'] }), }); }; diff --git a/chatui/src/app/modules/home/create-agent-dialog.tsx b/chatui/src/app/modules/home/create-agent-dialog.tsx index c7360ef489..e6412919f0 100644 --- a/chatui/src/app/modules/home/create-agent-dialog.tsx +++ b/chatui/src/app/modules/home/create-agent-dialog.tsx @@ -10,7 +10,9 @@ import { import { Input } from '@memgpt/components/input'; import { Label } from '@memgpt/components/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@memgpt/components/select'; +import { useToast } from '@memgpt/components/toast'; import { Loader2 } from 'lucide-react'; +import { useEffect } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { useAgentsCreateMutation } from '../../libs/agents/use-agents.mutation'; import { useAuthStoreState } from '../../libs/auth/auth.store'; @@ -25,19 +27,27 @@ interface FormData { model: string; } -const CreateAgentDialog = (props: { open: boolean; onOpenChange: (open: boolean) => void }) => { +const CreateAgentDialog = ({ open, onOpenChange }: { open: boolean; onOpenChange: (open: boolean) => void }) => { const auth = useAuthStoreState(); const createAgent = useAgentsCreateMutation(auth.uuid); + const { toast } = useToast(); - const { register, handleSubmit, control } = useForm({ - defaultValues: { - name: 'James Bond', - human: 'cs_phd', - persona: 'sam_pov', - model: 'gpt-4', - }, + const { + register, + handleSubmit, + control, + formState: { errors, isSubmitting, isValid }, + reset, + } = useForm({ + mode: 'onBlur', }); + useEffect(() => { + if (!open) { + reset(); // Resets form on dialog close + } + }, [open, reset]); + // Fetch models, humans, and personas using the custom hooks const { data: modelsData } = useModelsQuery(auth.uuid); const { data: humansData } = useHumansQuery(auth.uuid); @@ -45,34 +55,67 @@ const CreateAgentDialog = (props: { open: boolean; onOpenChange: (open: boolean) const onSubmit = (data: FormData) => { if (!auth.uuid) return; - // The data parameter already contains the values from the form - createAgent.mutate( - { + + createAgent + .mutateAsync({ name: data.name, human: data.human, persona: data.persona, model: data.model, - }, - { - onSuccess: () => props.onOpenChange(false), - } - ); + }) + .then(() => { + onOpenChange(false); + toast({ title: 'Agent created successfully!', duration: 5000 }); + }) + .catch((error) => { + let errorMessage = 'Error creating agent'; + const unknownErrorMessage = `${errorMessage}: Unspecified error.`; + + try { + const errorData = JSON.parse(error.message); + if (errorData.detail) { + // TODO: Modify this once the backend is changed: Backend should not return 'None' as the detail. + // This will eventually just be: + // errorMessage = `${errorMessage}: ${errorData.detail}`; + if (errorData.detail == 'None') { + errorMessage = unknownErrorMessage; + } else { + errorMessage = `${errorMessage}: ${errorData.detail}`; + } + } + } catch (parseError) { + errorMessage = unknownErrorMessage; + } + + toast({ + title: errorMessage, + duration: 5000, + }); + }); }; return ( - +
Create Agent - Add a new agent here. Click create when you're done. + + Add a new agent here. Click create when you're done. Human, Persona, and Model can be left blank to use + default values. +
- +
-