import React, { useState, useEffect, useRef } from 'react'; import '../App.css'; // Add Vite env type definition interface ImportMetaEnv { PROD: boolean; DEV: boolean; } interface Message { id: number; text: string; agent: string; audio_file?: string; title?: string; description?: string; category?: string; } interface DebateEntry { speaker: string; content: string; } interface ChatResponse { debate_history: DebateEntry[]; supervisor_notes: string[]; final_podcast?: { content: string; audio_file: string; title: string; description: string; }; } // Use relative URLs in production, full URLs in development const isDevelopment = window.location.hostname === 'localhost'; const API_URL = isDevelopment ? 'http://localhost:8000' : ''; const Home: React.FC = () => { const [messages, setMessages] = useState([ { id: 1, text: "Welcome! I'll help you create your own podcast content by exploring topics through an AI debate. Enter any topic of choice.", agent: "system" }, ]); const [inputMessage, setInputMessage] = useState(''); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); const audioRef = useRef(null); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); const sendMessageToServer = async (message: string): Promise => { try { const response = await fetch(`${API_URL}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ content: message, agent_type: "believer", context: { podcast_id: null, agent_chunks: [], current_agent: "believer" } }) }); if (!response.ok) { const errorData = await response.text(); console.error('Server error:', response.status, errorData); throw new Error(`Server error (${response.status}): ${errorData}`); } const data: ChatResponse = await response.json(); return data; } catch (error) { console.error('Error sending message:', error); throw error; } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!inputMessage.trim()) return; const userMessage: Message = { id: messages.length + 1, text: inputMessage, agent: "user" }; setMessages(prev => [...prev, userMessage]); setInputMessage(''); setIsLoading(true); try { const response = await sendMessageToServer(inputMessage); if (response.debate_history) { response.debate_history.forEach((entry) => { if (entry.speaker && entry.content) { const message: Message = { id: messages.length + 1, text: entry.content, agent: entry.speaker }; setMessages(prev => [...prev, message]); } }); } if (response.supervisor_notes) { response.supervisor_notes.forEach((note) => { const supervisorMessage: Message = { id: messages.length + 1, text: note, agent: "supervisor" }; setMessages(prev => [...prev, supervisorMessage]); }); } if (response.final_podcast && response.final_podcast.audio_file) { const filename = response.final_podcast.audio_file; const [queryPart, descriptionPart, categoryWithExt] = filename.split('-'); const category = categoryWithExt.replace('.mp3', ''); const podcastMessage: Message = { id: messages.length + 1, text: response.final_podcast.content || "Podcast generated successfully!", agent: "system", audio_file: `/audio-files/${filename}`, title: descriptionPart.replace(/_/g, ' ').replace(/^\w/, c => c.toUpperCase()), description: `A debate exploring ${queryPart.replace(/_/g, ' ')}`, category: category.replace(/_/g, ' ') }; setMessages(prev => [...prev, podcastMessage]); } } catch (error) { setMessages(prev => [...prev, { id: prev.length + 1, text: `Error: ${error.message}`, agent: "system" }]); } finally { setIsLoading(false); } }; return (
{messages.map((message) => (
{message.agent === "user" ? "👤" : message.agent === "system" ? "🤖" : message.agent === "believer" ? "💡" : message.agent === "skeptic" ? "🤔" : message.agent === "supervisor" ? "👀" : message.agent === "extractor" ? "🔍" : "💬"}
{message.agent}
{message.text}
{message.audio_file && (

{message.title || "Generated Podcast"}

{message.category && (
{message.category}
)}

{message.description || "An AI-generated debate podcast exploring different perspectives"}

)}
))} {isLoading && (
Debating
This might take a few moments since 2 agents are fighting over getting the best insights for you
)}
setInputMessage(e.target.value)} placeholder="Type your message..." className="chat-input" disabled={isLoading} />
); }; export default Home;