File size: 14,547 Bytes
b0b150b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import React, { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import {
    Box,
    Typography,
    Card,
    CardContent,
    LinearProgress,
    Button,
    Alert,
    Stepper,
    Step,
    StepLabel,
    StepContent,
    CircularProgress,
} from '@mui/material';
import {
    Check as CheckIcon,
    Error as ErrorIcon,
    Refresh as RefreshIcon,
} from '@mui/icons-material';
import client from '../api/client';

// Compilation steps
const COMPILATION_STEPS = [
    { label: 'Initializing', description: 'Setting up agent environment' },
    { label: 'Analyzing Prompt', description: 'Extracting domain and configuration' },
    { label: 'Compiling Knowledge', description: 'Building knowledge base' },
    { label: 'Generating Embeddings', description: 'Creating vector store' },
    { label: 'Finalizing', description: 'Saving agent artifacts' },
];

function CompilationProgress() {
    const { agentName } = useParams();
    const navigate = useNavigate();

    const [status, setStatus] = useState({
        status: 'starting',
        percentage: 0,
        current_step: 'Initializing...',
        error: null,
    });
    const [activeStep, setActiveStep] = useState(0);

    useEffect(() => {
        const pollStatus = async () => {
            try {
                // Use Phase 2 API
                const response = await client.get(`/api/compile/${agentName}/status`);
                const data = response.data;

                // Map to expected format
                const mappedStatus = {
                    status: data.agent_status === 'ready' ? 'complete' :
                        data.agent_status === 'failed' ? 'error' : 'processing',
                    percentage: data.job?.progress || 0,
                    current_step: data.job?.current_step || 'Processing...',
                    error: data.job?.error_message,
                    stats: null
                };

                setStatus(mappedStatus);

                // Update active step based on percentage
                if (mappedStatus.percentage < 20) setActiveStep(0);
                else if (mappedStatus.percentage < 40) setActiveStep(1);
                else if (mappedStatus.percentage < 60) setActiveStep(2);
                else if (mappedStatus.percentage < 90) setActiveStep(3);
                else setActiveStep(4);

                // Continue polling if not complete
                if (mappedStatus.status !== 'complete' && mappedStatus.status !== 'error') {
                    setTimeout(pollStatus, 2000);
                }
            } catch (err) {
                console.error('Failed to get status:', err);
                // Fallback to Phase 1 API
                try {
                    const fallbackResponse = await client.get(`/api/compile-status/${agentName}`);
                    setStatus(fallbackResponse.data);
                } catch {
                    setStatus({
                        status: 'error',
                        percentage: 0,
                        current_step: 'Failed to get status',
                        error: err.message,
                    });
                }
            }
        };

        pollStatus();
    }, [agentName]);

    const getStatusColor = () => {
        switch (status.status) {
            case 'complete': return 'success';
            case 'error': return 'error';
            default: return 'primary';
        }
    };

    const handleContinue = () => {
        navigate(`/ready/${agentName}`);
    };

    const handleRetry = () => {
        navigate('/create');
    };

    return (
        <Box
            className="fade-in"
            sx={{
                maxWidth: 800,
                mx: 'auto',
                mt: 4,
            }}
        >
            {/* Header */}
            <Box sx={{ textAlign: 'center', mb: 4 }}>
                <Typography variant="h4" sx={{ fontWeight: 700, mb: 1 }}>
                    {status.status === 'complete'
                        ? '🎉 Agent Ready!'
                        : status.status === 'error'
                            ? '❌ Compilation Failed'
                            : '⚙️ Compiling Agent'}
                </Typography>
                <Typography color="text.secondary" sx={{ fontWeight: 600, letterSpacing: '1px' }}>
                    MEXAR <span style={{ color: 'var(--primary)' }}>ULTIMATE</span> | {agentName.replace(/_/g, ' ').toUpperCase()}
                </Typography>
            </Box>

            {/* Progress Card */}
            <Card sx={{ mb: 4 }}>
                <CardContent>
                    {/* Progress Bar */}
                    <Box sx={{ mb: 3 }}>
                        <Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 1 }}>
                            <Typography variant="body2" color="text.secondary">
                                {status.current_step}
                            </Typography>
                            <Typography variant="body2" color="text.secondary">
                                {status.percentage}%
                            </Typography>
                        </Box>
                        <LinearProgress
                            variant="determinate"
                            value={status.percentage}
                            color={getStatusColor()}
                            sx={{
                                height: 12,
                                borderRadius: 6,
                                backgroundColor: 'background.paper',
                                '& .MuiLinearProgress-bar': {
                                    borderRadius: 6,
                                    background: status.status === 'complete'
                                        ? 'linear-gradient(90deg, #22c55e 0%, #06b6d4 100%)'
                                        : status.status === 'error'
                                            ? '#ef4444'
                                            : 'linear-gradient(90deg, #8b5cf6 0%, #06b6d4 100%)',
                                },
                            }}
                        />
                    </Box>

                    {/* Status Steps */}
                    <Stepper activeStep={activeStep} orientation="vertical">
                        {COMPILATION_STEPS.map((step, index) => (
                            <Step key={step.label}>
                                <StepLabel
                                    optional={
                                        <Typography variant="caption" color="text.secondary">
                                            {step.description}
                                        </Typography>
                                    }
                                    StepIconComponent={() => {
                                        if (status.status === 'error' && index === activeStep) {
                                            return <ErrorIcon color="error" />;
                                        }
                                        if (index < activeStep || status.status === 'complete') {
                                            return <CheckIcon color="success" />;
                                        }
                                        if (index === activeStep) {
                                            return <CircularProgress size={24} />;
                                        }
                                        return (
                                            <Box
                                                sx={{
                                                    width: 24,
                                                    height: 24,
                                                    borderRadius: '50%',
                                                    border: '2px solid',
                                                    borderColor: 'divider',
                                                    display: 'flex',
                                                    alignItems: 'center',
                                                    justifyContent: 'center',
                                                }}
                                            >
                                                <Typography variant="caption">{index + 1}</Typography>
                                            </Box>
                                        );
                                    }}
                                >
                                    {step.label}
                                </StepLabel>
                                <StepContent>
                                    {index === activeStep && status.status !== 'complete' && status.status !== 'error' && (
                                        <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mt: 1 }}>
                                            <CircularProgress size={16} />
                                            <Typography variant="body2" color="text.secondary">
                                                Processing...
                                            </Typography>
                                        </Box>
                                    )}
                                </StepContent>
                            </Step>
                        ))}
                    </Stepper>
                </CardContent>
            </Card>

            {/* Error Message */}
            {status.status === 'error' && (
                <Alert severity="error" sx={{ mb: 3 }}>
                    <Typography variant="subtitle2" gutterBottom>
                        Compilation Error
                    </Typography>
                    <Typography variant="body2">
                        {status.error || 'An unexpected error occurred during compilation.'}
                    </Typography>
                </Alert>
            )}

            {/* Statistics Preview (when complete) */}
            {status.status === 'complete' && status.stats && (
                <Card sx={{ mb: 3, borderColor: 'success.main', borderWidth: 2 }}>
                    <CardContent>
                        <Typography variant="h6" gutterBottom>
                            📊 Compilation Statistics
                        </Typography>
                        <Box sx={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 2 }}>
                            <Box>
                                <Typography variant="h4" color="primary.main" sx={{ fontWeight: 700 }}>
                                    {status.stats.nodes_count}
                                </Typography>
                                <Typography variant="body2" color="text.secondary">
                                    Knowledge Nodes
                                </Typography>
                            </Box>
                            <Box>
                                <Typography variant="h4" color="secondary.main" sx={{ fontWeight: 700 }}>
                                    {status.stats.edges_count}
                                </Typography>
                                <Typography variant="body2" color="text.secondary">
                                    Relationships
                                </Typography>
                            </Box>
                            <Box>
                                <Typography variant="h4" color="success.main" sx={{ fontWeight: 700 }}>
                                    Ready
                                </Typography>
                                <Typography variant="body2" color="text.secondary">
                                    Status
                                </Typography>
                            </Box>
                        </Box>
                    </CardContent>
                </Card>
            )}

            {/* Action Buttons */}
            <Box sx={{ display: 'flex', gap: 2, justifyContent: 'center' }}>
                {status.status === 'complete' && (
                    <Button
                        variant="contained"
                        size="large"
                        onClick={handleContinue}
                        sx={{
                            px: 6,
                            py: 1.5,
                            background: 'linear-gradient(135deg, #22c55e 0%, #06b6d4 100%)',
                        }}
                    >
                        Continue to Chat
                    </Button>
                )}

                {status.status === 'error' && (
                    <>
                        <Button
                            variant="outlined"
                            onClick={() => navigate('/')}
                        >
                            Back to Agents
                        </Button>
                        <Button
                            variant="contained"
                            startIcon={<RefreshIcon />}
                            onClick={handleRetry}
                        >
                            Try Again
                        </Button>
                    </>
                )}

                {status.status === 'processing' && (
                    <Button
                        variant="text"
                        onClick={() => navigate('/dashboard')}
                        sx={{ color: 'text.secondary' }}
                    >
                        Go to Dashboard
                    </Button>
                )}
            </Box>

            {/* Tips while waiting */}
            {status.status !== 'complete' && status.status !== 'error' && (
                <Card sx={{ mt: 4, background: 'rgba(139, 92, 246, 0.1)' }}>
                    <CardContent>
                        <Typography variant="subtitle2" gutterBottom>
                            💡 Did you know?
                        </Typography>
                        <Typography variant="body2" color="text.secondary">
                            MEXAR uses a high-performance Vector Database architecture. The semantic search
                            context enables fast responses, while the retrieval mechanism provides explainable
                            reasoning paths for every answer.
                        </Typography>
                    </CardContent>
                </Card>
            )}
        </Box>
    );
}

export default CompilationProgress;