'use client' import styles from './agents.module.css'; import Image from 'next/image'; import useSWR from 'swr'; import { useEffect, useState } from 'react'; import { useAuthenticatedData, UserProfile } from '../common/auth'; import { Button } from '@/components/ui/button'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" import { PaperPlaneTilt, Lightning, Plus, } from "@phosphor-icons/react"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTrigger } from '@/components/ui/dialog'; import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger } from '@/components/ui/drawer'; import LoginPrompt from '../components/loginPrompt/loginPrompt'; import { InlineLoading } from '../components/loading/loading'; import SidePanel from '../components/sidePanel/chatHistorySidePanel'; import NavMenu from '../components/navMenu/navMenu'; import { getIconFromIconName } from '../common/iconUtils'; import { convertColorToTextClass } from '../common/colorUtils'; import { Alert, AlertDescription } from '@/components/ui/alert'; export interface AgentData { slug: string; avatar: string; name: string; persona: string; color: string; icon: string; } async function openChat(slug: string, userData: UserProfile | null) { const unauthenticatedRedirectUrl = `/login?next=/agents?agent=${slug}`; if (!userData) { window.location.href = unauthenticatedRedirectUrl; return; } const response = await fetch(`/api/chat/sessions?agent_slug=${slug}`, { method: "POST" }); const data = await response.json(); if (response.status == 200) { window.location.href = `/chat?conversationId=${data.conversation_id}`; } else if (response.status == 403 || response.status == 401) { window.location.href = unauthenticatedRedirectUrl; } else { alert("Failed to start chat session"); } } const agentsFetcher = () => window.fetch('/api/agents').then(res => res.json()).catch(err => console.log(err)); interface AgentCardProps { data: AgentData; userProfile: UserProfile | null; isMobileWidth: boolean; } function AgentCard(props: AgentCardProps) { const searchParams = new URLSearchParams(window.location.search); const agentSlug = searchParams.get('agent'); const [showModal, setShowModal] = useState(agentSlug === props.data.slug); const [showLoginPrompt, setShowLoginPrompt] = useState(false); const userData = props.userProfile; if (showModal) { window.history.pushState({}, `Khoj AI - Agent ${props.data.slug}`, `/agents?agent=${props.data.slug}`); } const stylingString = convertColorToTextClass(props.data.color); return ( { showLoginPrompt && } { !props.isMobileWidth ? { setShowModal(!showModal); window.history.pushState({}, `Khoj AI - Agents`, `/agents`); }}> { getIconFromIconName(props.data.icon, props.data.color) || } {props.data.name} {props.userProfile ? ( openChat(props.data.slug, userData)}> ) : ( setShowLoginPrompt(true)}> )} { getIconFromIconName(props.data.icon, props.data.color) || } {props.data.name} {props.data.persona} { openChat(props.data.slug, userData); setShowModal(false); }}> Start Chatting : { setShowModal(open); window.history.pushState({}, `Khoj AI - Agents`, `/agents`); }}> { getIconFromIconName(props.data.icon, props.data.color) || } {props.data.name} {props.userProfile ? ( openChat(props.data.slug, userData)}> ) : ( setShowLoginPrompt(true)}> )} {props.data.name} Full Prompt {props.data.persona} Done } setShowModal(true)}> {props.data.persona} ) } export default function Agents() { const { data, error } = useSWR('agents', agentsFetcher, { revalidateOnFocus: false }); const authenticatedData = useAuthenticatedData(); const [isMobileWidth, setIsMobileWidth] = useState(false); const [showLoginPrompt, setShowLoginPrompt] = useState(false); useEffect(() => { if (typeof window !== 'undefined') { setIsMobileWidth(window.innerWidth < 768); } window.addEventListener('resize', () => { setIsMobileWidth(window.innerWidth < 768); }); }, []); if (error) { return ( Agents Error loading agents ); } if (!data) { return ( booting up your agents ); } return ( Agents Create Agent Coming Soon! { showLoginPrompt && } How it works Use any of these specialized personas to tune your conversation to your needs. {data.map(agent => ( ))} ); }
{props.data.name}
{props.data.persona}
Create Agent
Coming Soon!