File size: 9,406 Bytes
bdb3c28
bdbcdab
 
 
 
 
 
 
 
1d590c5
bdbcdab
 
 
 
 
 
 
 
71196c0
bdb3c28
 
 
 
 
 
 
 
 
71196c0
bdbcdab
 
 
 
 
bdb3c28
 
bdbcdab
 
 
 
 
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
1d590c5
bdbcdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d590c5
 
 
 
 
 
 
 
 
 
 
 
 
bdbcdab
 
 
 
 
 
 
 
71196c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdbcdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdb3c28
 
71196c0
1d590c5
71196c0
1d590c5
 
71196c0
 
 
 
1d590c5
71196c0
1d590c5
 
 
9ea884b
1d590c5
71196c0
1d590c5
 
 
 
9ea884b
659c746
 
bdb3c28
1d590c5
71196c0
 
 
 
 
 
 
bdb3c28
 
71196c0
bdb3c28
9ea884b
 
 
659c746
 
 
 
 
9ea884b
 
659c746
1d590c5
71196c0
 
 
659c746
bdb3c28
9ea884b
bdb3c28
9ea884b
 
 
659c746
9ea884b
 
659c746
9ea884b
659c746
 
 
 
 
 
 
 
 
 
 
 
 
 
9ea884b
 
 
659c746
bdb3c28
71196c0
bdb3c28
71196c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdbcdab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { useState, useCallback, useEffect, useRef } from 'react';
import {
  Box,
  Typography,
  Button,
  CircularProgress,
  Alert,
} from '@mui/material';
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
import GroupAddIcon from '@mui/icons-material/GroupAdd';
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';

const ORG_JOIN_URL = 'https://huggingface.co/organizations/ml-agent-explorers/share/GzPMJUivoFPlfkvFtIqEouZKSytatKQSZT';
const ORG_JOINED_KEY = 'hf-agent-org-joined';

function hasJoinedOrg(): boolean {
  try { return localStorage.getItem(ORG_JOINED_KEY) === '1'; } catch { return false; }
}

function markOrgJoined(): void {
  try { localStorage.setItem(ORG_JOINED_KEY, '1'); } catch { /* ignore */ }
}

export default function WelcomeScreen() {
  const { createSession } = useSessionStore();
  const { setPlan, clearPanel, user } = useAgentStore();
  const [isCreating, setIsCreating] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [orgJoined, setOrgJoined] = useState(hasJoinedOrg);
  const joinLinkOpened = useRef(false);

  const inIframe = isInIframe();
  const isAuthenticated = user?.authenticated;
  const isDevUser = user?.username === 'dev';

  // Auto-advance when user returns from the join link
  useEffect(() => {
    const handleVisibility = () => {
      if (document.visibilityState !== 'visible' || !joinLinkOpened.current) return;
      joinLinkOpened.current = false;
      markOrgJoined();
      setOrgJoined(true);
    };

    document.addEventListener('visibilitychange', handleVisibility);
    return () => document.removeEventListener('visibilitychange', handleVisibility);
  }, []);

  const tryCreateSession = useCallback(async () => {
    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([]);
      clearPanel();
    } catch {
      // Redirect may throw β€” ignore
    } finally {
      setIsCreating(false);
    }
  }, [createSession, setPlan, clearPanel]);

  const handleStart = useCallback(async () => {
    if (isCreating) return;

    if (!isAuthenticated && !isDevUser) {
      if (inIframe) return;
      triggerLogin();
      return;
    }

    await tryCreateSession();
  }, [isCreating, isAuthenticated, isDevUser, inIframe, tryCreateSession]);

  // 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`
    : '';

  // Shared button style
  const primaryBtnSx = {
    px: 5,
    py: 1.5,
    fontSize: '1rem',
    fontWeight: 700,
    textTransform: 'none' as const,
    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)',
    },
  };

  // Description block (reused across screens)
  const 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>
  );

  // Which screen to show
  const needsJoin = inIframe && !orgJoined;
  const showOpenAgent = inIframe && orgJoined;
  const showSignin = !inIframe && !isAuthenticated && !isDevUser;
  const showReady = !inIframe && (isAuthenticated || isDevUser);

  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' },
        }}
      >
        HF Agent
      </Typography>

      {/* ── Iframe: join org (first visit only) ──────────────────── */}
      {needsJoin && (
        <>
          <Typography
            variant="body1"
            sx={{
              color: 'var(--muted-text)',
              maxWidth: 480,
              mb: 4,
              lineHeight: 1.8,
              fontSize: '0.95rem',
              textAlign: 'center',
              px: 2,
              '& strong': { color: 'var(--text)', fontWeight: 600 },
            }}
          >
            Under the hood, this agent uses GPUs, inference APIs, and other paid Hub goodies β€” but we made them all free for you. Just join <strong>ML Agent Explorers</strong> to get started!
          </Typography>

          <Button
            variant="contained"
            size="large"
            component="a"
            href={ORG_JOIN_URL}
            target="_blank"
            rel="noopener noreferrer"
            onClick={() => { joinLinkOpened.current = true; }}
            startIcon={<GroupAddIcon />}
            sx={primaryBtnSx}
          >
            Join ML Agent Explorers
          </Button>
        </>
      )}

      {/* ── Iframe: already joined β†’ open Space ──────────────────── */}
      {showOpenAgent && (
        <>
          {description}
          <Button
            variant="contained"
            size="large"
            component="a"
            href={spaceHost}
            target="_blank"
            rel="noopener noreferrer"
            endIcon={<OpenInNewIcon />}
            sx={primaryBtnSx}
          >
            Open HF Agent
          </Button>
        </>
      )}

      {/* ── Direct: not logged in β†’ sign in ──────────────────────── */}
      {showSignin && (
        <>
          {description}
          <Button
            variant="contained"
            size="large"
            onClick={() => triggerLogin()}
            sx={primaryBtnSx}
          >
            Sign in with Hugging Face
          </Button>

          <Typography
            variant="caption"
            sx={{
              mt: 2.5,
              color: 'var(--muted-text)',
              fontSize: '0.78rem',
              textAlign: 'center',
              maxWidth: 360,
              lineHeight: 1.6,
            }}
          >
            Make sure to enable access to the <strong>ml-agent-explorers</strong> org when prompted.
          </Typography>
        </>
      )}

      {/* ── Direct: authenticated β†’ start session ────────────────── */}
      {showReady && (
        <>
          {description}
          <Button
            variant="contained"
            size="large"
            onClick={handleStart}
            disabled={isCreating}
            startIcon={
              isCreating ? <CircularProgress size={20} color="inherit" /> : null
            }
            sx={{
              ...primaryBtnSx,
              '&.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>
  );
}