'use client' import styles from './agents.module.css'; import Image from 'next/image'; import Link from 'next/link'; import useSWR from 'swr'; import { useEffect, useState } from 'react'; import { useAuthenticatedData, UserProfile } from '../common/auth'; import { Button } from '@/components/ui/button'; export interface AgentData { slug: string; avatar: string; name: string; personality: 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" }); if (response.status == 200) { window.location.href = `/chat`; } 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 AgentModalProps { data: AgentData; setShowModal: (show: boolean) => void; userData: UserProfile | null; } interface AgentCardProps { data: AgentData; userProfile: UserProfile | null; } function AgentModal(props: AgentModalProps) { const [copiedToClipboard, setCopiedToClipboard] = useState(false); useEffect(() => { if (copiedToClipboard) { setTimeout(() => setCopiedToClipboard(false), 3000); } }, [copiedToClipboard]); return (
{props.data.name}

{props.data.name}

{props.data.personality}

); } 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 userData = props.userProfile; if (showModal) { window.history.pushState({}, `Khoj AI - Agent ${props.data.slug}`, `/agents?agent=${props.data.slug}`); } return (
{ showModal && }
{props.data.name}
); } export default function Agents() { const { data, error } = useSWR('agents', agentsFetcher, { revalidateOnFocus: false }); const userData = useAuthenticatedData(); if (error) { return (
Talk to a Specialized Agent
Error loading agents
); } if (!data) { return (
Talk to a Specialized Agent
Loading agents...
); } return (
Talk to a Specialized Agent
{data.map(agent => ( ))}
); }