'use client' /** * Agents Page Client * Main client component for the agents page. */ import { useState, useCallback, useMemo } from 'react' import { Plus, Bot, LifeBuoy, Search } from 'lucide-react' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import { AgentCard } from '@/components/agents/agent-card' import { AgentForm } from '@/components/agents/agent-form' import { AgentTemplates } from '@/components/agents/agent-templates' import { AgentRunLog } from '@/components/agents/agent-run-log' import { AgentHelp } from '@/components/agents/agent-help' import { createAgent, updateAgent, getAgents, } from '@/app/actions/agent-actions' // --- Types --- interface Notebook { id: string name: string icon?: string | null } interface AgentItem { id: string name: string description?: string | null type?: string | null role: string sourceUrls?: string | null sourceNotebookId?: string | null targetNotebookId?: string | null frequency: string isEnabled: boolean lastRun: string | Date | null createdAt: string | Date updatedAt: string | Date tools?: string | null maxSteps?: number notifyEmail?: boolean _count: { actions: number } actions: { id: string; status: string; createdAt: string | Date }[] notebook?: { id: string; name: string; icon?: string | null } | null } interface AgentsPageClientProps { agents: AgentItem[] notebooks: Notebook[] } const typeFilterOptions = [ { value: '', labelKey: 'agents.filterAll' }, { value: 'scraper', labelKey: 'agents.types.scraper' }, { value: 'researcher', labelKey: 'agents.types.researcher' }, { value: 'monitor', labelKey: 'agents.types.monitor' }, { value: 'custom', labelKey: 'agents.types.custom' }, ] as const // --- Component --- export function AgentsPageClient({ agents: initialAgents, notebooks, }: AgentsPageClientProps) { const { t } = useLanguage() const [agents, setAgents] = useState(initialAgents) const [showForm, setShowForm] = useState(false) const [editingAgent, setEditingAgent] = useState(null) const [logAgent, setLogAgent] = useState<{ id: string; name: string } | null>(null) const [showHelp, setShowHelp] = useState(false) const [searchQuery, setSearchQuery] = useState('') const [typeFilter, setTypeFilter] = useState('') const refreshAgents = useCallback(async () => { try { const updated = await getAgents() setAgents(updated) } catch { // Silent } }, []) const handleToggle = useCallback((id: string, isEnabled: boolean) => { setAgents(prev => prev.map(a => a.id === id ? { ...a, isEnabled } : a)) }, []) const handleCreate = useCallback(() => { setEditingAgent(null) setShowForm(true) }, []) const handleEdit = useCallback((id: string) => { const agent = agents.find(a => a.id === id) if (agent) { setEditingAgent(agent) setShowForm(true) } }, [agents]) const handleSave = useCallback(async (formData: FormData) => { const data = { name: formData.get('name') as string, description: (formData.get('description') as string) || undefined, type: formData.get('type') as string, role: formData.get('role') as string, sourceUrls: formData.get('sourceUrls') ? JSON.parse(formData.get('sourceUrls') as string) : undefined, sourceNotebookId: (formData.get('sourceNotebookId') as string) || undefined, targetNotebookId: (formData.get('targetNotebookId') as string) || undefined, frequency: formData.get('frequency') as string, tools: formData.get('tools') ? JSON.parse(formData.get('tools') as string) : undefined, maxSteps: formData.get('maxSteps') ? Number(formData.get('maxSteps')) : undefined, notifyEmail: formData.get('notifyEmail') === 'true', } if (editingAgent) { await updateAgent(editingAgent.id, data) toast.success(t('agents.toasts.updated')) } else { await createAgent(data) toast.success(t('agents.toasts.created')) } setShowForm(false) setEditingAgent(null) await refreshAgents() }, [editingAgent, refreshAgents, t]) const filteredAgents = useMemo(() => { return agents.filter(agent => { const matchesType = !typeFilter || (agent.type || 'scraper') === typeFilter if (!searchQuery.trim()) return matchesType const q = searchQuery.toLowerCase() const matchesSearch = (agent.name || '').toLowerCase().includes(q) || (agent.description && agent.description.toLowerCase().includes(q)) return matchesType && matchesSearch }) }, [agents, searchQuery, typeFilter]) const existingAgentNames = useMemo(() => agents.map(a => a.name), [agents]) return ( <> {/* Header */}

{t('agents.title')}

{t('agents.subtitle')}

{/* Action buttons */}
{/* Agents grid */} {agents.length > 0 && (

{t('agents.myAgents')}

{/* Search and filter */}
setSearchQuery(e.target.value)} placeholder={t('agents.searchPlaceholder')} className="w-full pl-9 pr-3 py-2 text-sm bg-white border border-slate-200 rounded-lg outline-none focus:border-primary/40 focus:ring-2 focus:ring-primary/10 transition-all" />
{typeFilterOptions.map(opt => ( ))}
{filteredAgents.length > 0 ? (
{filteredAgents.map(agent => ( ))}
) : (

{t('agents.noResults')}

)}
)} {/* Empty state */} {agents.length === 0 && (

{t('agents.noAgents')}

{t('agents.noAgentsDescription')}

)} {/* Templates */} {/* Form modal */} {showForm && ( { setShowForm(false); setEditingAgent(null) }} /> )} {/* Run log modal */} {logAgent && ( setLogAgent(null)} /> )} {/* Help modal */} {showHelp && ( setShowHelp(false)} /> )} ) }