File size: 7,454 Bytes
1a12d36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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<string | null>(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 (
    <Box

      sx={{

        width: '100%',

        height: '100%',

        display: 'flex',

        flexDirection: 'column',

        alignItems: 'center',

        justifyContent: 'center',

        background: 'var(--body-gradient)',

        py: 8,

      }}

    >

      {/* HF Logo */}

      <Box

        component="img"

        src="https://huggingface.co/front/assets/huggingface_logo-noborder.svg"

        alt="Hugging Face"

        sx={{ width: 96, height: 96, mb: 3, display: 'block' }}

      />



      {/* Title */}

      <Typography

        variant="h2"

        sx={{

          fontWeight: 800,

          color: 'var(--text)',

          mb: 1.5,

          letterSpacing: '-0.02em',

          fontSize: { xs: '2rem', md: '2.8rem' },

        }}

      >

        ML Agent

      </Typography>



      {/* Description */}

      <Typography

        variant="body1"

        sx={{

          color: 'var(--muted-text)',

          maxWidth: 520,

          mb: 5,

          lineHeight: 1.8,

          fontSize: '0.95rem',

          textAlign: 'center',

          px: 2,

          '& strong': { color: 'var(--text)', fontWeight: 600 },

        }}

      >

        A general-purpose AI agent for <strong>machine learning engineering</strong>.

        It browses <strong>Hugging Face documentation</strong>, manages{' '}

        <strong>repositories</strong>, launches <strong>training jobs</strong>,

        and explores <strong>datasets</strong> β€” all through natural conversation.

      </Typography>



      {/* Action button β€” depends on context */}

      {inIframe && !isAuthenticated && !isDevUser ? (

        // In iframe + not logged in β†’ link to open Space directly

        <Button

          variant="contained"

          size="large"

          component="a"

          href={spaceHost}

          target="_blank"

          rel="noopener noreferrer"

          endIcon={<OpenInNewIcon />}

          sx={{

            px: 5,

            py: 1.5,

            fontSize: '1rem',

            fontWeight: 700,

            textTransform: 'none',

            borderRadius: '12px',

            bgcolor: HF_ORANGE,

            color: '#000',

            boxShadow: '0 4px 24px rgba(255, 157, 0, 0.3)',

            textDecoration: 'none',

            '&:hover': {

              bgcolor: '#FFB340',

              boxShadow: '0 6px 32px rgba(255, 157, 0, 0.45)',

            },

          }}

        >

          Open ML Agent

        </Button>

      ) : !isAuthenticated && !isDevUser ? (

        // Direct access + not logged in β†’ sign in button

        <Button

          variant="contained"

          size="large"

          onClick={() => triggerLogin()}

          sx={{

            px: 5,

            py: 1.5,

            fontSize: '1rem',

            fontWeight: 700,

            textTransform: 'none',

            borderRadius: '12px',

            bgcolor: HF_ORANGE,

            color: '#000',

            boxShadow: '0 4px 24px rgba(255, 157, 0, 0.3)',

            '&:hover': {

              bgcolor: '#FFB340',

              boxShadow: '0 6px 32px rgba(255, 157, 0, 0.45)',

            },

          }}

        >

          Sign in with Hugging Face

        </Button>

      ) : (

        // Authenticated or dev β†’ start session

        <Button

          variant="contained"

          size="large"

          onClick={handleStart}

          disabled={isCreating}

          startIcon={

            isCreating ? <CircularProgress size={20} color="inherit" /> : null

          }

          sx={{

            px: 5,

            py: 1.5,

            fontSize: '1rem',

            fontWeight: 700,

            textTransform: 'none',

            borderRadius: '12px',

            bgcolor: HF_ORANGE,

            color: '#000',

            boxShadow: '0 4px 24px rgba(255, 157, 0, 0.3)',

            '&:hover': {

              bgcolor: '#FFB340',

              boxShadow: '0 6px 32px rgba(255, 157, 0, 0.45)',

            },

            '&.Mui-disabled': {

              bgcolor: 'rgba(255, 157, 0, 0.35)',

              color: 'rgba(0,0,0,0.45)',

            },

          }}

        >

          {isCreating ? 'Initializing...' : 'Start Session'}

        </Button>

      )}



      {/* Error */}

      {error && (

        <Alert

          severity="warning"

          variant="outlined"

          onClose={() => setError(null)}

          sx={{

            mt: 3,

            maxWidth: 400,

            fontSize: '0.8rem',

            borderColor: HF_ORANGE,

            color: 'var(--text)',

          }}

        >

          {error}

        </Alert>

      )}



      {/* Footnote */}

      <Typography

        variant="caption"

        sx={{ mt: 5, color: 'var(--muted-text)', opacity: 0.5, fontSize: '0.7rem' }}

      >

        Conversations are stored locally in your browser.

      </Typography>

    </Box>
  );
}