import React from "react"; import Image from "next/image"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Pencil } from "lucide-react"; import { formatDate } from "@/lib/utils"; import { Tables } from "@/utils/supabase/database.types"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Badge } from "@/components/ui/badge"; import Spinner from "@/components/spinner"; import { Textarea } from "@/components/ui/textarea"; import clsx from "clsx"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import axios, { AxiosError } from "axios"; import { toast } from "sonner"; type AgentCardProps = { data: Tables<"agents"> }; export default function AgentCard(props: AgentCardProps) { const { id, image_url, name, description, model_type, created_at } = props.data; const [openDialogUpdate, setOpenDialogUpdate] = React.useState(false); const formSchema = z.object({ question: z .string({ required_error: "Question is required", }) .min(2, { message: "Minimum Question is 2 characters.", }), }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { question: "", }, mode: "onChange", }); const queryClient = useQueryClient(); const mutation = useMutation({ mutationFn: async (values: z.infer) => { const url = encodeURI( `https://ai-endpoint-one.dev3vds1.link/deepinfra-ai/${name}/${model_type}/${description}/${values.question}` ); const json = await axios.get(url); return json.data; }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["agents"], }); }, onError: (error) => { if (error instanceof AxiosError) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx const errResponse = error.response.data; console.log({ errResponse }); // if (errResponse.errors && Array.isArray(errResponse.errors)) { // errResponse.errors.forEach( // (inputErr: { field: string; message: string }) => { // toast.error(`Error field : ${inputErr.field}`, { // description: inputErr.message, // }); // } // ); // } // if (errResponse.code) { // toast.error(errResponse?.code, { // description: errResponse?.message, // }); // } } else if (error.request) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js toast.error("No response from the server", { description: "Failed to fetch the data, server returns null.", }); } else { // Something happened in setting up the request that triggered an Error toast.error("Failed to set up the request", { description: "There is something wrong when setting up the request", }); } } // toast.error(err.message); }, }); const onSubmit = (values: z.infer) => { mutation.mutate(values); }; const { formState: { errors }, } = form; return (
  • {name}
    {model_type}

    {name}

    {description}

    { // form.reset(); setOpenDialogUpdate(val); }} > Try Agent Try to ask a question to our agent
    {name}
    {model_type}
    • Name:

      {name}

    • Description:

      {description}

    ( Question: