Spaces:
Runtime error
Runtime error
File size: 23,378 Bytes
3c8d218 ef865f2 3c8d218 ef865f2 3c8d218 ef865f2 3c8d218 ef865f2 3c8d218 ef865f2 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 ef865f2 3c8d218 7457a6f 3c8d218 ef865f2 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 7457a6f 3c8d218 | 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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | import React, { useState, useEffect } from 'react';
import { History, Clock, CheckCircle, XCircle, ChevronRight, Activity, Search, Filter, Trash2, ArrowRight, Zap, Download } from 'lucide-react';
import { HistoryService } from '../services/api';
import { useAppContext } from './AppContext';
const HistoryPage = () => {
const { importSession, addToast } = useAppContext();
const [history, setHistory] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [searchQuery, setSearchQuery] = useState('');
const [expandedSessionId, setExpandedSessionId] = useState(null);
const [expandedStepId, setExpandedStepId] = useState(null);
useEffect(() => {
fetchHistory();
}, []);
const handleImport = (session) => {
importSession(session);
// Navigate to testing page
const event = new CustomEvent('nav-change', { detail: 'testing' });
window.dispatchEvent(event);
};
const fetchHistory = async () => {
setIsLoading(true);
try {
const data = await HistoryService.getHistory();
setHistory(data);
} catch (e) {
console.error("Failed to fetch history", e);
} finally {
setIsLoading(false);
}
};
const handleExportSession = async (sessionId) => {
try {
await HistoryService.exportSession(sessionId);
} catch (e) {
console.error("Export failed", e);
addToast('error', "Failed to export session.");
}
};
const handleDeleteSession = async (sessionId) => {
if (!window.confirm("Are you sure you want to delete this test session?")) return;
try {
await HistoryService.deleteHistorySession(sessionId);
setHistory(prev => prev.filter(item => item.session_id !== sessionId));
if (expandedSessionId === sessionId) setExpandedSessionId(null);
} catch (e) {
console.error("Delete failed", e);
addToast('error', "Failed to delete session.");
}
};
const handleClearAll = async () => {
if (!window.confirm("CRITICAL: This will permanently wipe ALL test history. Proceed?")) return;
try {
await HistoryService.clearAllHistory();
setHistory([]);
setExpandedSessionId(null);
} catch (e) {
console.error("Clear failed", e);
addToast('error', "Failed to clear history.");
}
};
// Grouping logic: Create a map of sessions, then convert to array
const sessionsMap = (history || []).reduce((acc, curr) => {
if (!curr) return acc;
const sid = curr.session_id || 'unknown';
if (!acc[sid]) {
acc[sid] = {
session_id: sid,
spec_id: curr.spec_id,
spec_name: curr.spec_name || 'Unknown Spec',
scenario: curr.scenario || 'No scenario text',
timestamp: curr.timestamp,
steps: [],
status: 'success'
};
}
acc[sid].steps.push(curr);
if (curr.analysis?.status === 'failure') acc[sid].status = 'failure';
// Capture context variables from the session metadata
if (curr.context_variables && Object.keys(curr.context_variables).length > 0) {
if (!acc[sid].context_variables) acc[sid].context_variables = {};
Object.assign(acc[sid].context_variables, curr.context_variables);
}
// Capture step definition if it exists (for restoration)
if (curr.step_definition) {
if (!acc[sid].plan_steps) acc[sid].plan_steps = {};
acc[sid].plan_steps[curr.step] = curr.step_definition;
}
// Keep the most recent timestamp as session timestamp
if (new Date(curr.timestamp) > new Date(acc[sid].timestamp)) {
acc[sid].timestamp = curr.timestamp;
}
return acc;
}, {});
// Sort steps within sessions and convert map to sorted array
const groupedSessions = Object.values(sessionsMap)
.map(s => {
// Reconstruct the plan from plan_steps or fallback to minimal info from steps
const stepsForImport = Object.keys(s.plan_steps || {}).length > 0
? Object.values(s.plan_steps).sort((a,b) => a.step - b.step)
: s.steps.map(st => ({
step: st.step,
endpoint: st.request.method + ' ' + st.request.url.split('/').pop(),
purpose: st.analysis?.response_summary?.slice(0, 50) || 'Step execution'
})).sort((a,b) => a.step - b.step);
return {
...s,
stepsForImport,
steps: s.steps.sort((a, b) => a.step - b.step)
};
})
.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
const filteredSessions = groupedSessions.filter(s =>
s.spec_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
s.scenario.toLowerCase().includes(searchQuery.toLowerCase()) ||
s.steps.some(st => (st.analysis?.response_summary || '').toLowerCase().includes(searchQuery.toLowerCase()))
);
const formatDate = (dateStr) => {
const d = new Date(dateStr);
return d.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
};
return (
<div className="flex-1 flex flex-col bg-[var(--bg-primary)] overflow-hidden animate-in fade-in slide-in-from-bottom-2 duration-300">
{/* Header */}
<div className="h-14 border-b border-[var(--border-subtle)] flex items-center justify-between px-6 bg-[var(--bg-secondary)] shrink-0">
<div className="flex items-center space-x-4">
<h1 className="text-lg font-medium text-[var(--text-primary)] flex items-center space-x-2">
<History size={20} className="text-[var(--accent-primary)]" />
<span>Execution History</span>
</h1>
<span className="text-xs bg-[var(--bg-tertiary)] text-[var(--text-secondary)] px-2 py-0.5 rounded-full font-mono border border-[var(--border-subtle)]">
{groupedSessions.length} sessions
</span>
</div>
<div className="flex items-center space-x-3">
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--text-secondary)]" size={14} />
<input
type="text"
placeholder="Search specs, scenarios..."
value={searchQuery}
onChange={e => setSearchQuery(e.target.value)}
className="bg-[var(--bg-tertiary)] border border-[var(--border-subtle)] text-[var(--text-primary)] pl-9 pr-4 py-1.5 text-xs rounded-lg outline-none focus:border-[var(--accent-primary)] w-64 transition-all"
/>
</div>
<button
onClick={fetchHistory}
className="p-2 text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-colors"
title="Refresh History"
>
<Activity size={18} />
</button>
{groupedSessions.length > 0 && (
<button
onClick={handleClearAll}
className="px-3 py-1.5 bg-red-500/10 hover:bg-red-500/20 text-red-500 text-[10px] font-bold uppercase tracking-wider rounded-lg border border-red-500/20 transition-all flex items-center space-x-2"
title="Clear All History"
>
<Trash2 size={14} />
<span>Clear All</span>
</button>
)}
</div>
</div>
{isLoading ? (
<div className="flex-1 flex flex-col items-center justify-center space-y-4 opacity-50">
<Activity size={32} className="text-[var(--accent-primary)] animate-pulse" />
<p className="text-sm font-light text-[var(--text-secondary)]">Loading history records...</p>
</div>
) : filteredSessions.length === 0 ? (
<div className="flex-1 flex flex-col items-center justify-center p-12 text-center">
<div className="w-16 h-16 bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded-2xl flex items-center justify-center mb-6">
<History size={32} className="text-[var(--text-secondary)] opacity-30" />
</div>
<h3 className="text-lg font-light text-[var(--text-primary)]">No History Found</h3>
<p className="text-sm text-[var(--text-secondary)] max-w-xs mt-2">
Run some test scenarios on the Workflow page to see execution logs appear here.
</p>
</div>
) : (
<div className="flex-1 overflow-y-auto p-6">
<div className="max-w-5xl mx-auto space-y-4">
{filteredSessions.map((session) => (
<div
key={session.session_id}
className={`group border transition-all duration-200 overflow-hidden rounded-xl ${
expandedSessionId === session.session_id
? 'bg-[var(--bg-secondary)] border-[var(--accent-primary)]/50 shadow-2xl'
: 'bg-[var(--bg-secondary)]/50 border-[var(--border-subtle)] hover:border-[var(--text-secondary)]/30'
}`}
>
{/* Session Header (Collapsible) */}
<div
onClick={() => setExpandedSessionId(expandedSessionId === session.session_id ? null : session.session_id)}
className="p-4 flex items-center cursor-pointer select-none"
>
<div className={`mr-4 p-2 rounded-lg ${
session.status === 'success' ? 'bg-emerald-500/10' : 'bg-red-500/10'
}`}>
{session.status === 'success'
? <Zap size={20} className="text-emerald-500" />
: <XCircle size={20} className="text-red-500" />
}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center space-x-2 text-[10px] font-bold uppercase tracking-wider text-[var(--text-secondary)] mb-1">
<span className="text-[var(--accent-primary)]">{session.spec_name}</span>
<span>•</span>
<span>{session.steps.length} steps</span>
</div>
<div className="text-sm text-[var(--text-primary)] font-medium truncate">
{session.scenario}
</div>
</div>
<div className="flex items-center shrink-0 ml-6 space-x-4">
<button
onClick={(e) => { e.stopPropagation(); handleDeleteSession(session.session_id); }}
className="p-2 text-[var(--text-secondary)] hover:text-red-500 hover:bg-red-500/10 rounded-lg transition-all"
title="Delete Session"
>
<Trash2 size={16} />
</button>
<button
onClick={(e) => { e.stopPropagation(); handleExportSession(session.session_id); }}
className="p-2 text-[var(--text-secondary)] hover:text-[var(--accent-secondary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-all flex items-center space-x-2 group/btn"
title="Export Session as JSON"
>
<Download size={16} className="group-hover/btn:-translate-y-0.5 transition-transform" />
<span className="text-[10px] uppercase font-bold tracking-tight hidden group-hover/btn:inline">Export</span>
</button>
<button
onClick={(e) => { e.stopPropagation(); handleImport(session); }}
className="p-2 text-[var(--text-secondary)] hover:text-[var(--accent-primary)] hover:bg-[var(--bg-tertiary)] rounded-lg transition-all flex items-center space-x-2 group/btn"
title="Import to Testing Workspace"
>
<ArrowRight size={16} className="group-hover/btn:translate-x-0.5 transition-transform" />
<span className="text-[10px] uppercase font-bold tracking-tight hidden group-hover/btn:inline">Import</span>
</button>
<div className="flex flex-col items-end">
<div className="flex items-center text-xs text-[var(--text-secondary)] font-mono mb-1">
<Clock size={12} className="mr-1" />
{formatDate(session.timestamp)}
</div>
<ChevronRight
size={16}
className={`text-[var(--text-secondary)] transition-transform duration-200 ${expandedSessionId === session.session_id ? 'rotate-90' : ''}`}
/>
</div>
</div>
</div>
{/* Session Steps (Expanded View) */}
{expandedSessionId === session.session_id && (
<div className="px-4 pb-6 overflow-hidden animate-in slide-in-from-top-2 duration-200">
{/* Session Extraction History */}
{session.context_variables && Object.keys(session.context_variables).length > 0 && (
<div className="mb-6 bg-[var(--bg-primary)] border border-[var(--border-subtle)] rounded-lg p-4">
<h4 className="text-[10px] font-bold uppercase text-[var(--text-secondary)] mb-3 tracking-wider flex items-center">
<Activity size={12} className="mr-2 text-[var(--accent-primary)]" />
Session Extraction History
</h4>
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-3">
{Object.entries(session.context_variables).map(([k, v]) => (
<div key={k} className="flex flex-col space-y-1 bg-[var(--bg-tertiary)] p-2 rounded border border-[var(--border-subtle)]">
<span className="text-[9px] text-[var(--accent-primary)] font-mono">{k}</span>
<span className="text-xs text-[var(--accent-secondary)] truncate">"{String(v)}"</span>
</div>
))}
</div>
</div>
)}
<div className="pt-4 border-t border-[var(--border-subtle)] space-y-3">
{session.steps.map((item) => (
<div key={item.id} className="border border-[var(--border-subtle)] rounded-lg overflow-hidden bg-[var(--bg-primary)]">
<div
onClick={() => setExpandedStepId(expandedStepId === item.id ? null : item.id)}
className="p-3 flex items-center justify-between cursor-pointer hover:bg-[var(--bg-tertiary)] transition-colors"
>
<div className="flex items-center space-x-3">
<span className={`px-2 py-0.5 rounded text-[10px] font-mono ${
item.analysis?.status === 'success' ? 'bg-green-500/20 text-green-400' : 'bg-red-500/20 text-red-400'
}`}>
Step {item.step}
</span>
<span className="text-xs text-[var(--text-primary)] font-medium">{item.request.method} {item.request.url.split('/').pop()}</span>
</div>
<ChevronRight
size={14}
className={`text-[var(--text-secondary)] transition-transform duration-200 ${expandedStepId === item.id ? 'rotate-90' : ''}`}
/>
</div>
{/* Step Details */}
{expandedStepId === item.id && (
<div className="p-4 bg-[var(--bg-primary)] border-t border-[var(--border-subtle)] space-y-4 animate-in fade-in duration-200">
{/* AI Analysis Result */}
<div className={`p-4 rounded-lg border-l-4 ${
item.analysis?.status === 'success' ? 'border-emerald-500 bg-emerald-500/5' : 'border-red-500 bg-red-500/5'
}`}>
<div className="text-[10px] font-bold uppercase text-[var(--text-secondary)] mb-2">AI Step Analysis</div>
<p className="text-xs text-[var(--text-primary)] leading-relaxed">
{item.analysis?.response_summary || "Summary not available."}
</p>
{item.analysis?.extracted_variables && Object.keys(item.analysis.extracted_variables).length > 0 && (
<div className="mt-3 flex flex-wrap gap-2">
{Object.entries(item.analysis.extracted_variables).map(([k, v]) => (
<div key={k} className="px-2 py-1 bg-[var(--bg-tertiary)] rounded text-[9px] text-[var(--accent-primary)] font-mono border border-[var(--border-subtle)]">
{k}: <span className="text-[var(--accent-secondary)]">"{String(v)}"</span>
</div>
))}
</div>
)}
</div>
{/* Request/Response Grid */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="space-y-2">
<div className="text-[9px] uppercase font-bold text-[var(--text-secondary)] flex items-center">
<ArrowRight size={10} className="mr-1" />
Request Details
</div>
<div className="bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded p-3 text-[10px] font-mono overflow-auto max-h-48 space-y-3">
<div className="text-[var(--accent-primary)] font-bold mb-2 break-all">{item.request.method} {item.request.url}</div>
{item.request.headers && Object.keys(item.request.headers).length > 0 && (
<div>
<div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Headers</div>
{Object.entries(item.request.headers).map(([k,v]) => (
<div key={k} className="text-[var(--accent-primary)]"><span className="text-[var(--text-secondary)]">{k}:</span> {v}</div>
))}
</div>
)}
{item.request.query_params && Object.keys(item.request.query_params).length > 0 && (
<div>
<div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Query Params</div>
{Object.entries(item.request.query_params).map(([k,v]) => (
<div key={k} className="text-[var(--accent-primary)]"><span className="text-[var(--text-secondary)]">{k}=</span>{v}</div>
))}
</div>
)}
{item.request.body && <div>
<div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Body</div>
<pre className="text-[var(--text-secondary)]">{JSON.stringify(item.request.body, null, 2)}</pre>
</div>}
{item.request.form_data && Object.keys(item.request.form_data).length > 0 && (
<div>
<div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Form Data</div>
<pre className="text-[var(--text-secondary)]">{JSON.stringify(item.request.form_data, null, 2)}</pre>
</div>
)}
{item.request.files && Object.keys(item.request.files).length > 0 && (
<div>
<div className="text-[var(--text-secondary)] font-bold uppercase mb-1">Files</div>
{Object.entries(item.request.files).map(([k,v]) => (
<div key={k} className="text-[var(--accent-secondary)]"><span className="text-[var(--text-secondary)]">{k}:</span> {v}</div>
))}
</div>
)}
</div>
</div>
<div className="space-y-2">
<div className="text-[9px] uppercase font-bold text-[var(--text-secondary)] flex items-center">
<Zap size={10} className="mr-1" />
Response Body ({item.response.status_code})
</div>
<div className="bg-[var(--bg-secondary)] border border-[var(--border-subtle)] rounded p-3 text-[10px] font-mono overflow-auto max-h-48">
<pre className="text-[var(--accent-secondary)]">
{(() => {
try { return JSON.stringify(JSON.parse(item.response.body), null, 2); }
catch { return item.response.body; }
})()}
</pre>
</div>
</div>
</div>
</div>
)}
</div>
))}
</div>
</div>
)}
</div>
))}
</div>
</div>
)}
</div>
);
};
export default HistoryPage;
|