File size: 13,367 Bytes
3a2d933
27cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const API_URL = window.location.origin;

// Global state
let currentEpisodes = [];
let chartInstance = null;

// DOM Elements
const statusIndicator = document.getElementById('api-status');
const statusText = document.getElementById('api-status-text');

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    initNavigation();
    checkApiStatus();
    setInterval(checkApiStatus, 10000);
});

// Navigation
function initNavigation() {
    document.querySelectorAll('.nav-btn').forEach(btn => {
        btn.addEventListener('click', (e) => {
            document.querySelectorAll('.nav-btn').forEach(b => b.classList.remove('active'));
            document.querySelectorAll('.tab-pane').forEach(p => p.classList.remove('active'));
            
            const target = e.currentTarget;
            target.classList.add('active');
            
            const tabId = target.getAttribute('data-tab');
            document.getElementById(`tab-${tabId}`).classList.add('active');
            
            // Tab specific logic
            if (tabId === 'dashboard') loadStats();
            if (tabId === 'runner') loadScenariosForRunner();
            if (tabId === 'replay') loadRecentReplays();
            if (tabId === 'scenarios') loadScenarios();
        });
    });
}

// API Health
async function checkApiStatus() {
    try {
        const res = await fetch(`${API_URL}/`);
        if (res.ok) {
            statusIndicator.className = 'status-indicator online';
            statusText.textContent = 'API Online';
            if (currentEpisodes.length === 0) {
                // Initial load
                loadStats();
                loadScenariosForRunner();
            }
        } else {
            throw new Error('Bad response');
        }
    } catch (e) {
        statusIndicator.className = 'status-indicator error';
        statusText.textContent = 'API Offline';
    }
}

async function apiGet(path) {
    try {
        const res = await fetch(`${API_URL}${path}`);
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        return await res.json();
    } catch (err) {
        console.error(`API GET ${path} failed:`, err);
        return null;
    }
}

// ==========================================
// DASHBOARD (Chart.js & Stats)
// ==========================================
async function loadStats() {
    try {
        const stats = await apiGet('/stats');
        if (!stats) return;
        
        document.getElementById('stat-total-episodes').textContent = stats.total_episodes || 0;
        
        let totalEps = 0;
        let totalSolved = 0;
        let totalReward = 0;
        
        Object.values(stats.levels || {}).forEach(l => {
            totalEps += l.count;
            totalSolved += l.solve_rate * l.count;
            totalReward += l.mean_reward * l.count;
        });

        if (totalEps > 0) {
            document.getElementById('stat-overall-solve-rate').textContent = `${((totalSolved / totalEps) * 100).toFixed(1)}%`;
            document.getElementById('stat-mean-reward').textContent = (totalReward / totalEps).toFixed(1);
        } else {
            document.getElementById('stat-overall-solve-rate').textContent = `0%`;
            document.getElementById('stat-mean-reward').textContent = `0.0`;
        }

        const recentData = await apiGet('/recent?n=100');
        if (recentData && recentData.episodes && recentData.episodes.length > 0) {
            currentEpisodes = recentData.episodes;
            const rev = [...recentData.episodes].reverse();
            let sumSolved = 0;
            let chartLabels = [];
            let rewardData = [];
            let solveData = [];

            rev.forEach((ep, i) => {
                if (ep.solved) sumSolved++;
                chartLabels.push(`Ep ${ep.episode_id.slice(0,4)}`);
                rewardData.push(ep.total_reward);
                solveData.push((sumSolved / (i+1)) * 100);
            });
            
            renderChart(chartLabels, rewardData, solveData);
        }

        renderLevelCards(stats.levels || {});

    } catch (e) {
        console.error('Failed to load stats', e);
    }
}

function renderChart(labels, rewardData, solveData) {
    const ctx = document.getElementById('rewardChart').getContext('2d');
    
    if (chartInstance) chartInstance.destroy();
    
    chartInstance = new Chart(ctx, {
        type: 'line',
        data: {
            labels: labels,
            datasets: [
                {
                    label: 'Total Reward',
                    data: rewardData,
                    borderColor: '#00f0ff',
                    backgroundColor: 'rgba(0, 240, 255, 0.1)',
                    borderWidth: 2,
                    tension: 0.3,
                    fill: true,
                    yAxisID: 'y'
                },
                {
                    label: 'Solve Rate (%)',
                    data: solveData,
                    borderColor: '#00ff9d',
                    borderWidth: 2,
                    borderDash: [5, 5],
                    tension: 0.3,
                    yAxisID: 'y1'
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            interaction: { mode: 'index', intersect: false },
            scales: {
                x: { display: false },
                y: { type: 'linear', display: true, position: 'left', grid: { color: 'rgba(255,255,255,0.05)' } },
                y1: { type: 'linear', display: true, position: 'right', grid: { drawOnChartArea: false }, min: 0, max: 100 }
            },
            plugins: {
                legend: { labels: { color: '#e2e8f0', font: { family: 'JetBrains Mono' } } }
            }
        }
    });
}

function renderLevelCards(levels) {
    const container = document.getElementById('level-cards-container');
    container.innerHTML = '';
    
    [1, 2, 3].forEach(level => {
        const stats = levels[level] || { solve_rate: 0, count: 0 };
        const pct = (stats.solve_rate * 100).toFixed(1);
        
        const div = document.createElement('div');
        div.className = 'level-stat-row';
        div.innerHTML = `
            <div class="top">
                <span class="name">Level ${level}</span>
                <span class="rate">${pct}% (${stats.count} runs)</span>
            </div>
            <div class="progress-track">
                <div class="progress-fill" style="width: ${pct}%"></div>
            </div>
        `;
        container.appendChild(div);
    });
}

// ==========================================
// RUNNER & TERMINAL
// ==========================================
async function loadScenariosForRunner() {
    const data = await apiGet('/scenarios');
    if (!data) return;
    
    const select = document.getElementById('scenario-select');
    select.innerHTML = '<option value="">(Random Scenario)</option>';
    
    data.scenarios.forEach(scen => {
        const opt = document.createElement('option');
        opt.value = scen.id;
        opt.textContent = `${scen.id} (Level ${scen.level})`;
        select.appendChild(opt);
    });
}

document.getElementById('run-episode-btn').addEventListener('click', async (e) => {
    const btn = e.currentTarget;
    btn.disabled = true;
    btn.innerHTML = '<span class="btn-text">EXECUTING...</span><span class="btn-icon spinner"></span>';
    
    const sid = document.getElementById('scenario-select').value;
    const lvl = document.getElementById('level-select').value;
    
    const output = document.getElementById('runner-output');
    output.innerHTML = '<div class="term-empty-state"><span class="blink">_</span> Initializing Docker sandbox...</div>';
    
    try {
        const body = {};
        if (sid) body.scenario_id = sid;
        if (lvl) body.level = parseInt(lvl);
        
        const res = await fetch(`${API_URL}/episode/run`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(body)
        });
        const data = await res.json();
        
        if (!res.ok) throw new Error("Failed to run episode.");
        
        renderTerminalExecution(output, data);
        loadStats();
    } catch (err) {
        output.innerHTML = `<div class="term-error">Execution Failed: ${err.message}</div>`;
    } finally {
        btn.disabled = false;
        btn.innerHTML = '<span class="btn-text">INITIALIZE RUN</span><span class="btn-icon">⚡</span>';
    }
});

function renderTerminalExecution(container, data) {
    container.innerHTML = '';
    
    const title = document.createElement('div');
    title.style.marginBottom = '1rem';
    title.innerHTML = `<strong>Scenario:</strong> <span style="color:#00f0ff">${data.scenario_id}</span>`;
    container.appendChild(title);
    
    data.steps.forEach(step => {
        const block = document.createElement('div');
        block.className = 'term-step';
        
        let statusClass = 'term-success';
        if (step.execution_result?.blocked) statusClass = 'term-blocked';
        else if (step.execution_result?.exit_code !== 0) statusClass = 'term-error';
        
        const rewardClass = step.reward >= 0 ? 'pos' : 'neg';
        const rewardSign = step.reward >= 0 ? '+' : '';
        
        let statusText = step.execution_result?.blocked ? '⚠ BLOCKED' : 
                         step.solved ? '✓ SOLVED' :
                         step.execution_result?.exit_code === 0 ? 'ok (exit 0)' : `failed (exit ${step.execution_result?.exit_code})`;

        block.innerHTML = `
            <div class="term-cmd">${step.action}</div>
            <div class="term-result ${statusClass}">
${statusText} 
                <span class="term-reward ${rewardClass}">${rewardSign}${step.reward.toFixed(1)}</span>
            </div>
        `;
        container.appendChild(block);
    });
    
    const sum = document.createElement('div');
    sum.className = 'term-summary';
    const finalStatus = data.solved ? '<span class="term-success">SOLVED ✓</span>' : '<span class="term-error">FAILED ✗</span>';
    sum.innerHTML = `
        <strong>Result:</strong> ${finalStatus}<br>
        <strong>Steps:</strong> ${data.total_steps}<br>
        <strong>Total Reward:</strong> ${data.total_reward.toFixed(1)}
    `;
    container.appendChild(sum);
}

// ==========================================
// REPLAY VIEWER
// ==========================================
async function loadRecentReplays() {
    const data = await apiGet('/recent?n=20');
    if (!data) return;
    
    const list = document.getElementById('replay-recent-list');
    list.innerHTML = '';
    
    data.episodes.forEach(ep => {
        const item = document.createElement('div');
        item.className = 'history-item';
        
        const rClass = ep.total_reward >= 0 ? 'pos' : 'neg';
        const rSign = ep.total_reward >= 0 ? '+' : '';
        const bClass = ep.solved ? 'win' : '';
        const bText = ep.solved ? 'SOLVED' : 'FAILED';
        
        item.innerHTML = `
            <div class="hi-top">
                <span class="hi-id">${ep.episode_id.split('-')[0]}</span>
                <span class="hi-reward ${rClass}">${rSign}${ep.total_reward.toFixed(1)}</span>
            </div>
            <div class="hi-scenario">${ep.scenario_id} <span class="hi-badge ${bClass}">${bText}</span></div>
        `;
        
        item.addEventListener('click', async () => {
            const term = document.getElementById('replay-viewer');
            document.getElementById('replay-title').textContent = `replay_${ep.episode_id.split('-')[0]}.sh`;
            
            try {
                const epData = await apiGet(`/replay/${ep.episode_id}`);
                renderTerminalExecution(term, epData);
            } catch (e) {
                term.innerHTML = '<div class="term-error">Failed to load replay data</div>';
            }
        });
        
        list.appendChild(item);
    });
}

// ==========================================
// SCENARIOS REGISTRY
// ==========================================
async function loadScenarios() {
    const scData = await apiGet('/scenarios');
    const stData = await apiGet('/stats');
    if (!scData || !stData) return;
    
    const list = document.getElementById('scenarios-list');
    list.innerHTML = '';
    
    scData.scenarios.forEach(sc => {
        const scStats = (stData.scenarios && stData.scenarios[sc.id]) || { count: 0, solve_rate: 0 };
        
        const card = document.createElement('div');
        card.className = 'glass-panel scenario-card';
        
        card.innerHTML = `
            <div class="sc-name">${sc.id}</div>
            <div class="sc-desc">${sc.description}</div>
            <div class="sc-hints">
                ${(sc.hint_commands||[]).map(h => `<span>${h}</span>`).join('')}
            </div>
            <div class="sc-stats">
                <span>Level ${sc.level}</span>
                <span style="color:var(--success)">${(scStats.solve_rate * 100).toFixed(1)}% Solved (${scStats.count})</span>
            </div>
        `;
        list.appendChild(card);
    });
}

function escapeHtml(unsafe) {
    return (unsafe||'').replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}