import React from 'react'; const EpisodeEndOverlay = ({ isOpen, onClose, metrics, gameState }) => { if (!isOpen) return null; const handleDownload = () => { if (!gameState) return; const sc = gameState.scenario || {}; const allAgents = gameState.agents || {}; const allMessages = Object.values(allAgents).flatMap(a => a?.messages || []); const unifiedSummary = generateUnifiedSummary(); let report = `=================================================================\n`; report += ` NEXUS INCIDENT INVESTIGATION REPORT \n`; report += `=================================================================\n\n`; report += `[ SCENARIO METADATA ]\n`; report += `Title: ${sc.id || 'N/A'}\n`; report += `Domain: ${sc.domain || 'N/A'}\n`; report += `Difficulty: ${sc.difficulty || 'N/A'}\n`; report += `Final Score: ${Number(gameState?.cumulativeReward || metrics?.score || 0).toFixed(4)} / 1.00\n`; report += `Total Steps: ${gameState?.step || metrics?.steps || 'N/A'}\n`; report += `Active Agents: ${Object.keys(allAgents).length}\n`; report += `Status: ${unifiedSummary?.isSuccess ? 'SUCCESS' : 'INCONCLUSIVE'}\n\n`; report += `[ AGENTS DEPLOYED ]\n`; Object.entries(allAgents).forEach(([agentId, agentData], idx) => { const msgs = agentData?.messages || []; const msgCount = msgs.filter(m => m.type === 'message').length; const toolCount = msgs.filter(m => m.type === 'tool_call').length; report += `${idx + 1}. ${agentId}: ${msgCount} messages, ${toolCount} tool calls\n`; }); report += `\n`; // UNIFIED SUMMARY SECTION if (unifiedSummary) { report += `=================================================================\n`; report += `[ UNIFIED INVESTIGATION SUMMARY ]\n`; report += `=================================================================\n\n`; report += `## Combined Agent Conclusions\n`; report += `${unifiedSummary.conclusionText || 'No conclusions recorded.'}\n\n`; report += `## Key Findings & Clues\n`; report += `${unifiedSummary.keyFindings || 'None recorded.'}\n\n`; report += `## Key Tool Results\n`; report += `${unifiedSummary.toolSummary}\n\n`; } report += `[ STEP REWARDS ]\n`; if (gameState?.rewardHistory && gameState.rewardHistory.length > 0) { gameState.rewardHistory.forEach((r, i) => { report += `Step ${i + 1}: ${r.toFixed(4)}\n`; }); report += `Average: ${(gameState.rewardHistory.reduce((a, b) => a + b, 0) / gameState.rewardHistory.length).toFixed(4)}\n`; report += `Final Score: ${Number(gameState.cumulativeReward || 0).toFixed(4)}\n\n`; } else { report += `No step rewards recorded.\n\n`; } report += `[ REWARD BREAKDOWN ]\n`; if (gameState?.rewardBreakdown && Object.keys(gameState.rewardBreakdown).length > 0) { Object.entries(gameState.rewardBreakdown).forEach(([key, val]) => { report += `${key}: ${typeof val === 'number' ? val.toFixed(4) : val}\n`; }); report += `\n`; } report += `[ INCIDENT DESCRIPTION & PROBLEM ]\n`; report += `${sc.description || 'No description provided.'}\n\n`; report += `[ CONTEXT & ROOT CAUSE ]\n`; report += `${sc.context || 'No context provided.'}\n`; report += `Root Cause Validation: ${metrics?.rootCause || 'N/A'}\n\n`; report += `=================================================================\n`; report += `[ INVESTIGATION LOG & DETAILED TRACE ]\n`; report += `=================================================================\n\n`; const allErrors = []; const allTools = []; allMessages.forEach(msg => { if (msg.type === 'tool_call') { allTools.push(`- ${msg.tool_name}(${JSON.stringify(msg.params)})`); } if (msg.type === 'tool_result' && !msg.success) { allErrors.push(`- Error from ${msg.tool_name}: ${msg.result}`); } if (msg.type === 'tool_result' && msg.result?.toLowerCase().includes('error')) { allErrors.push(`- Log/Cmd Error: ${msg.result}`); } }); report += `> EXECUTED TOOLS & COMMANDS:\n`; if (allTools.length > 0) { allTools.forEach(t => report += `${t}\n`); } else { report += `None.\n`; } report += `\n`; report += `> SYSTEMS ERRORS DETECTED:\n`; if (allErrors.length > 0) { [...new Set(allErrors)].forEach(err => report += `${err}\n`); } else { report += `No significant system errors found.\n`; } report += `\n`; report += `=================================================================\n`; report += `[ SOLUTION & FIX VERIFICATION ]\n`; report += `=================================================================\n\n`; const resCall = gameState?.tool_calls_made?.find(c => c.tool_name === 'submit_resolution'); if (resCall?.params) { report += `Root Cause Service: ${resCall.params.root_cause_service || 'UNKNOWN'}\n`; report += `Root Cause Description: ${resCall.params.root_cause_description || 'None'}\n`; report += `Fix Applied: ${resCall.params.fix_applied || 'None'}\n`; } else { report += `No resolution submitted.\n`; } report += `\n`; report += `=================================================================\n`; report += `[ RECOMMENDATIONS ]\n`; report += `=================================================================\n\n`; if (allTools.length > 15) { report += `1. EFFICIENCY: ${allTools.length} tool calls made. Consider refining hypotheses.\n`; } else { report += `1. EFFICIENCY: Tool usage was concise (${allTools.length} calls).\n`; } if (allErrors.length > 5) { report += `2. ACCURACY: Multiple errors encountered. Verify tool syntax.\n`; } report += `3. CAUSE-ANALYSIS: Check error logs before database queries.\n`; report += `4. REMEDIATION: Establish better alerting for ${sc.domain || 'general'} domain.\n`; const blob = new Blob([report], { type: 'text/plain;charset=utf-8' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `nexus_investigation_report_${sc.id || 'export'}.txt`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; const generateUnifiedSummary = () => { const allAgents = gameState?.agents || {}; const agentEntries = Object.entries(allAgents); if (agentEntries.length === 0) return null; const allConclusions = []; const allToolResults = []; const allClues = gameState?.clues_found || []; agentEntries.forEach(([agentId, agentData]) => { const msgs = agentData?.messages || []; const textMsgs = msgs.filter(m => m.type === 'message'); const lastMsg = textMsgs[textMsgs.length - 1]; if (lastMsg) { allConclusions.push({ agentId, content: lastMsg.content || lastMsg.text || lastMsg.message || '', role: agentData.role || agentId }); } const toolResults = msgs.filter(m => m.type === 'tool_result'); toolResults.forEach(tr => { if (tr.result && !tr.result.toLowerCase().includes('error')) { allToolResults.push({ agentId, tool: tr.tool_name || tr.tool, result: tr.result }); } }); }); const conclusionText = allConclusions.map(c => c.content).join('\n\n'); const keyFindings = allClues.slice(0, 5).join('\n• '); const toolSummary = allToolResults.length > 0 ? allToolResults.slice(0, 3).map(t => `• ${t.tool}: ${t.result.substring(0, 100)}...`).join('\n') : 'No tool results recorded.'; const isSuccess = Number(gameState?.cumulativeReward || metrics?.score || 0) >= 0.5; return { conclusionText, keyFindings: keyFindings || 'No clues recorded.', toolSummary, agentCount: agentEntries.length, isSuccess }; }; const unifiedSummary = generateUnifiedSummary(); return (
{p.root_cause_description || 'No description provided.'}
{p.fix_applied || 'No fix described.'}
{unifiedSummary.conclusionText || 'No conclusions recorded.'}
{unifiedSummary.toolSummary}