import { useState, useCallback } from 'react'; import { Box, Typography, Button, CircularProgress, Alert, } from '@mui/material'; import OpenInNewIcon from '@mui/icons-material/OpenInNew'; import { useSessionStore } from '@/store/sessionStore'; import { useAgentStore } from '@/store/agentStore'; import { apiFetch } from '@/utils/api'; import { isInIframe, triggerLogin } from '@/hooks/useAuth'; /** HF brand orange */ const HF_ORANGE = '#FF9D00'; export default function WelcomeScreen() { const { createSession } = useSessionStore(); const { setPlan, setPanelContent, user } = useAgentStore(); const [isCreating, setIsCreating] = useState(false); const [error, setError] = useState(null); const inIframe = isInIframe(); const isAuthenticated = user?.authenticated; const isDevUser = user?.username === 'dev'; const handleStart = useCallback(async () => { if (isCreating) return; // Not authenticated and not dev → need to login if (!isAuthenticated && !isDevUser) { // In iframe: can't redirect (cookies blocked) — user needs to open in new tab // This shouldn't happen because we show a different button in iframe // But just in case: if (inIframe) return; triggerLogin(); return; } setIsCreating(true); setError(null); try { const response = await apiFetch('/api/session', { method: 'POST' }); if (response.status === 503) { const data = await response.json(); setError(data.detail || 'Server is at capacity. Please try again later.'); return; } if (response.status === 401) { triggerLogin(); return; } if (!response.ok) { setError('Failed to create session. Please try again.'); return; } const data = await response.json(); createSession(data.session_id); setPlan([]); setPanelContent(null); } catch { // Redirect may throw — ignore } finally { setIsCreating(false); } }, [isCreating, createSession, setPlan, setPanelContent, isAuthenticated, isDevUser, inIframe]); // Build the direct Space URL for the "open in new tab" link const spaceHost = typeof window !== 'undefined' ? window.location.hostname.includes('.hf.space') ? window.location.origin : `https://smolagents-ml-agent.hf.space` : ''; return ( {/* HF Logo */} {/* Title */} ML Agent {/* Description */} A general-purpose AI agent for machine learning engineering. It browses Hugging Face documentation, manages{' '} repositories, launches training jobs, and explores datasets — all through natural conversation. {/* Action button — depends on context */} {inIframe && !isAuthenticated && !isDevUser ? ( // In iframe + not logged in → link to open Space directly ) : !isAuthenticated && !isDevUser ? ( // Direct access + not logged in → sign in button ) : ( // Authenticated or dev → start session )} {/* Error */} {error && ( setError(null)} sx={{ mt: 3, maxWidth: 400, fontSize: '0.8rem', borderColor: HF_ORANGE, color: 'var(--text)', }} > {error} )} {/* Footnote */} Conversations are stored locally in your browser. ); }