import { useState } from 'react'; interface Message { text: string; isUser: boolean; } function App() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const handleSend = async () => { if (input.trim() === '' || isLoading) return; const userMessage: Message = { text: input, isUser: true }; setMessages(prevMessages => [...prevMessages, userMessage]); setInput(''); setIsLoading(true); try { const response = await fetch('/api', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ message: input }), }); if (!response.body) { throw new Error('No response body'); } const reader = response.body.getReader(); const decoder = new TextDecoder(); let aiResponse = ''; const aiMessage: Message = { text: '', isUser: false }; setMessages(prevMessages => [...prevMessages, aiMessage]); while (true) { const { done, value } = await reader.read(); if (done) { break; } aiResponse += decoder.decode(value, { stream: true }); setMessages(prevMessages => prevMessages.map((msg, index) => index === prevMessages.length - 1 ? { ...msg, text: aiResponse } : msg ) ); } } catch (error) { console.error('Error sending message:', error); const errorMessage: Message = { text: 'Sorry, something went wrong.', isUser: false }; setMessages(prevMessages => [...prevMessages, errorMessage]); } finally { setIsLoading(false); } }; return (

AI Coding Agent

{messages.map((message, index) => (

{message.text}

))} {isLoading && (

Jules is thinking...

)}
); } export default App;