'use client'; import { useState, useRef, useEffect } from 'react'; type Message = { id: string; role: 'user' | 'agent'; text?: string; imageUrl?: string; videoUrl?: string; }; export default function AgentInterface() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [messages]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || loading) return; const userMsg: Message = { id: Date.now().toString(), role: 'user', text: input }; setMessages(prev => [...prev, userMsg]); setInput(''); setLoading(true); try { // Very basic command parsing for demonstrating capabilities const isImageRequest = userMsg.text?.toLowerCase().includes('generate image'); const isVideoRequest = userMsg.text?.toLowerCase().includes('generate video'); let action = 'chat'; if (isImageRequest) action = 'generate-image'; if (isVideoRequest) action = 'generate-video'; const res = await fetch('/api/gemini', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action, prompt: userMsg.text, history: messages.map(m => ({ role: m.role === 'agent' ? 'model' : 'user', text: m.text || '' })) }), }); const data = await res.json(); if (data.error) throw new Error(data.error); if (action === 'chat') { setMessages(prev => [...prev, { id: Date.now().toString(), role: 'agent', text: data.text }]); } else if (action === 'generate-image') { setMessages(prev => [...prev, { id: Date.now().toString(), role: 'agent', text: 'Generated image as requested.', imageUrl: data.imageUrl }]); } else if (action === 'generate-video') { // Mock video flow - normally we'd poll video-status but we'll simulate the response // because video takes a long time. setMessages(prev => [...prev, { id: Date.now().toString(), role: 'agent', text: `Video generation started. Operation ID: ${data.operationName}. Use dashboard logs to monitor progress.` }]); } } catch (err: any) { setMessages(prev => [...prev, { id: Date.now().toString(), role: 'agent', text: `[SYSTEM ERROR]: ${err.message}` }]); } finally { setLoading(false); } }; return (
Autonomous Agent Workflow
{messages.length === 0 && (
[Agent system initialized. Awaiting directives.]
)} {messages.map(msg => (
{msg.text && (
{msg.text}
)} {msg.imageUrl && ( Generated Asset )}
))} {loading && (
[Processing...]
)}
setInput(e.target.value)} placeholder="INPUT DIRECTIVE..." className="flex-1 bg-transparent border border-[var(--color-slate-dark)] p-[8px] font-mono text-[13px] text-[var(--color-slate-ink)] focus:outline-none focus:border-[var(--color-slate-ink)] placeholder-[var(--color-slate-dark)]" />
); }