Spaces:
Paused
Paused
Download frontend/apps/internal/src/components/ForensicsView.tsx from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 16.2 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/frontend/apps/internal/src/components/ForensicsView.tsx
- Command line
-
hf download hf://spaces/3SCError404/miracle/frontend/apps/internal/src/components/ForensicsView.tsx
-
curl -L -o ForensicsView.tsx https://huggingface.co/spaces/3SCError404/miracle/resolve/main/frontend/apps/internal/src/components/ForensicsView.tsx
16.2 kB
| import React, { useState, useEffect, useCallback } from 'react'; | |
| import { useAuth, api } from '@nexus/shared'; | |
| import { | |
| ShieldCheck, | |
| Activity, | |
| Clock, | |
| Filter, | |
| RefreshCcw, | |
| Fingerprint, | |
| ChevronLeft, | |
| ChevronRight, | |
| Search | |
| } from 'lucide-react'; | |
| import { motion, AnimatePresence } from 'framer-motion'; | |
| import styles from './ForensicsView.module.css'; | |
| interface AuditLog { | |
| id: string; | |
| workspace_id: string | null; | |
| entity_type: string; | |
| entity_id: string; | |
| action: string; | |
| user_id: string | null; | |
| actor_name: string; | |
| old_value: any; | |
| new_value: any; | |
| metadata_json: any; | |
| created_at: string; | |
| } | |
| interface AIInteraction { | |
| id: string; | |
| agent_name: string; | |
| model_info: string; | |
| input_data: any; | |
| output_data: any; | |
| status: string; | |
| duration_ms: any; | |
| usage_metadata: any; | |
| created_at: string; | |
| } | |
| type ForensicsTab = 'ai' | 'security'; | |
| interface ForensicsViewProps { | |
| workspaceId?: string; | |
| } | |
| export const ForensicsView: React.FC<ForensicsViewProps> = ({ workspaceId }) => { | |
| useAuth(); | |
| const [activeTab, setActiveTab] = useState<ForensicsTab>('security'); | |
| const [logs, setLogs] = useState<AuditLog[]>([]); | |
| const [aiTraces, setAiTraces] = useState<AIInteraction[]>([]); | |
| const [loading, setLoading] = useState(true); | |
| const [filterType, setFilterType] = useState<string>(''); | |
| const [page, setPage] = useState(1); | |
| const [hasMore, setHasMore] = useState(true); | |
| const [searchTerm, setSearchTerm] = useState<string>(''); | |
| const [localSearch, setLocalSearch] = useState<string>(''); | |
| const LIMIT = 20; // Smaller limit for better pagination feel | |
| const fetchAuditLogs = useCallback(async (targetPage = 1) => { | |
| setLoading(true); | |
| try { | |
| const currentOffset = (targetPage - 1) * LIMIT; | |
| const params: any = { | |
| limit: LIMIT, | |
| offset: currentOffset | |
| }; | |
| if (filterType) params.entity_type = filterType; | |
| if (workspaceId) params.workspace_id = workspaceId; | |
| if (searchTerm) params.entity_id = searchTerm; // Support searching by entity ID | |
| const res = await api.get('system/audit/', { params }); | |
| const newLogs = res.data || []; | |
| setLogs(newLogs); | |
| setPage(targetPage); | |
| setHasMore(newLogs.length === LIMIT); | |
| } catch (err) { | |
| console.error('Forensics fetch failed', err); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }, [filterType, workspaceId, searchTerm]); | |
| const fetchAIInteractions = useCallback(async (targetPage = 1) => { | |
| setLoading(true); | |
| try { | |
| const currentOffset = (targetPage - 1) * LIMIT; | |
| const params: any = { | |
| limit: LIMIT, | |
| offset: currentOffset | |
| }; | |
| if (workspaceId) params.workspace_id = workspaceId; | |
| if (searchTerm) params.ticket_id = searchTerm; // Neural traces search by ticket ID | |
| const res = await api.get('system/interactions/', { params }); | |
| const newTraces = res.data || []; | |
| setAiTraces(newTraces); | |
| setPage(targetPage); | |
| setHasMore(newTraces.length === LIMIT); | |
| } catch (err) { | |
| console.error('AI Forensics fetch failed', err); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }, [workspaceId, searchTerm]); | |
| useEffect(() => { | |
| if (activeTab === 'security') { | |
| fetchAuditLogs(page); | |
| } else { | |
| fetchAIInteractions(page); | |
| } | |
| }, [activeTab, filterType, searchTerm, workspaceId, page, fetchAuditLogs, fetchAIInteractions]); | |
| const handleSearchKeyDown = (e: React.KeyboardEvent) => { | |
| if (e.key === 'Enter') { | |
| setPage(1); | |
| setSearchTerm(localSearch); | |
| } | |
| }; | |
| const renderSecurityAudit = () => ( | |
| <div className={styles.auditContainer}> | |
| <div className={styles.auditHeader}> | |
| <div className={styles.filters}> | |
| <div className={styles.filterItem}> | |
| <Filter size={14} /> | |
| <select value={filterType} onChange={(e) => setFilterType(e.target.value)}> | |
| <option value="">ALL ENTITIES</option> | |
| <option value="user">USER MANAGEMENT</option> | |
| <option value="ticket">TICKET ACTIONS</option> | |
| <option value="workspace">WORKSPACE CONFIG</option> | |
| </select> | |
| </div> | |
| <div className={styles.filterItem}> | |
| <Search size={14} /> | |
| <input | |
| type="text" | |
| placeholder="SEARCH ENTITY ID..." | |
| value={localSearch} | |
| onChange={(e) => setLocalSearch(e.target.value)} | |
| onKeyDown={handleSearchKeyDown} | |
| style={{ background: 'transparent', border: 'none', color: 'inherit', fontSize: '11px', outline: 'none', width: '150px' }} | |
| /> | |
| </div> | |
| <button className={styles.refreshBtn} onClick={() => fetchAuditLogs(1)} disabled={loading}> | |
| <RefreshCcw size={14} className={loading ? styles.spinning : ''} /> | |
| </button> | |
| </div> | |
| <div className={styles.auditBadge}> | |
| <ShieldCheck size={14} /> | |
| <span>RECURSIVE INTEGRITY CHECK: PASSED</span> | |
| </div> | |
| </div> | |
| <div className={styles.auditTable}> | |
| <div className={styles.tableHeader}> | |
| <div className={styles.col}>TIMESTAMP</div> | |
| <div className={styles.col}>ACTOR</div> | |
| <div className={styles.col}>ACTION</div> | |
| <div className={styles.col}>ENTITY</div> | |
| <div className={styles.col}>INTELLIGENCE</div> | |
| </div> | |
| <div className={`${styles.tableBody} ${loading ? styles.loading : ''}`}> | |
| <AnimatePresence> | |
| {loading && ( | |
| <motion.div | |
| className={styles.loadingOverlay} | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| exit={{ opacity: 0 }} | |
| > | |
| <RefreshCcw size={24} className={styles.spinning} /> | |
| <span>FILTERING AUDIT TRAILS...</span> | |
| </motion.div> | |
| )} | |
| </AnimatePresence> | |
| <div className={styles.tableScroll}> | |
| <AnimatePresence mode="popLayout"> | |
| {logs.map((log, idx) => ( | |
| <motion.div | |
| key={log.id} | |
| className={styles.tableRow} | |
| initial={{ opacity: 0, x: -10 }} | |
| animate={{ opacity: 1, x: 0 }} | |
| transition={{ delay: idx * 0.03 }} | |
| > | |
| <div className={styles.col}> | |
| <div className={styles.timeWrapper}> | |
| <Clock size={12} /> | |
| {new Date(log.created_at).toLocaleTimeString()} | |
| </div> | |
| <div className={styles.dateLabel}>{new Date(log.created_at).toLocaleDateString()}</div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={styles.actorCell}> | |
| <div className={styles.actorAvatar}> | |
| {log.actor_name?.[0] || 'S'} | |
| </div> | |
| <div className={styles.actorInfo}> | |
| <span className={styles.actorName}>{log.actor_name}</span> | |
| <span className={styles.actorId}>{log.user_id?.slice(0, 8) || 'SYSTEM'}</span> | |
| </div> | |
| </div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={`${styles.actionPill} ${(log.action || '').includes('failure') ? styles.fail : styles.pass}`}> | |
| {(log.action || 'system_action').replace(/_/g, ' ').toUpperCase()} | |
| </div> | |
| </div> | |
| <div className={styles.col}> | |
| <span className={styles.entityTag}>{(log.entity_type || 'system').toUpperCase()}</span> | |
| <span className={styles.entityId}>#{log.entity_id?.slice(0, 8) || '0000'}</span> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={styles.detailBox}> | |
| {log.new_value ? ( | |
| <pre className={styles.jsonDetail}>{JSON.stringify(log.new_value, null, 2)}</pre> | |
| ) : ( | |
| <span className={styles.noData}>Stateless interaction record</span> | |
| )} | |
| </div> | |
| </div> | |
| </motion.div> | |
| ))} | |
| </AnimatePresence> | |
| {logs.length === 0 && !loading && ( | |
| <div className={styles.emptyState}> | |
| <Fingerprint size={48} opacity={0.2} /> | |
| <p>No security events found matching current filter.</p> | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| <div className={styles.paginationFooter}> | |
| <div className={styles.pagination}> | |
| <button | |
| className={styles.pageBtn} | |
| onClick={() => fetchAuditLogs(page - 1)} | |
| disabled={page === 1 || loading} | |
| > | |
| <ChevronLeft size={16} /> | |
| </button> | |
| <div className={styles.pageIndicator}> | |
| PAGE <span>{page}</span> | |
| </div> | |
| <button | |
| className={styles.pageBtn} | |
| onClick={() => fetchAuditLogs(page + 1)} | |
| disabled={!hasMore || loading} | |
| > | |
| <ChevronRight size={16} /> | |
| </button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| const renderNeuralTrace = () => ( | |
| <div className={styles.auditContainer}> | |
| <div className={styles.auditHeader}> | |
| <div className={styles.filters}> | |
| <div className={styles.filterItem}> | |
| <Search size={14} /> | |
| <input | |
| type="text" | |
| placeholder="SEARCH TICKET ID..." | |
| value={localSearch} | |
| onChange={(e) => setLocalSearch(e.target.value)} | |
| onKeyDown={handleSearchKeyDown} | |
| style={{ background: 'transparent', border: 'none', color: 'inherit', fontSize: '11px', outline: 'none', width: '180px' }} | |
| /> | |
| </div> | |
| <button className={styles.refreshBtn} onClick={() => fetchAIInteractions(1)} disabled={loading}> | |
| <RefreshCcw size={14} className={loading ? styles.spinning : ''} /> | |
| </button> | |
| </div> | |
| <div className={styles.aiBadge}> | |
| <Activity size={14} /> | |
| <span>NEURAL STREAM: ACTIVE</span> | |
| </div> | |
| </div> | |
| <div className={styles.auditTable}> | |
| <div className={styles.tableHeader}> | |
| <div className={styles.col}>TIMESTAMP</div> | |
| <div className={styles.col}>AGENT / MODEL</div> | |
| <div className={styles.col}>METRICS</div> | |
| <div className={styles.col}>STATUS</div> | |
| <div className={styles.col}>INTELLIGENCE</div> | |
| </div> | |
| <div className={`${styles.tableBody} ${loading ? styles.loading : ''}`}> | |
| <AnimatePresence> | |
| {loading && ( | |
| <motion.div | |
| className={styles.loadingOverlay} | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| exit={{ opacity: 0 }} | |
| > | |
| <Activity size={24} className={styles.spinning} /> | |
| <span>ISOLATING NEURAL TRACES...</span> | |
| </motion.div> | |
| )} | |
| </AnimatePresence> | |
| <div className={styles.tableScroll}> | |
| <AnimatePresence mode="popLayout"> | |
| {aiTraces.map((trace, idx) => ( | |
| <motion.div | |
| key={trace.id} | |
| className={styles.tableRow} | |
| initial={{ opacity: 0, y: 10 }} | |
| animate={{ opacity: 1, y: 0 }} | |
| transition={{ delay: idx * 0.02 }} | |
| > | |
| <div className={styles.col}> | |
| <div className={styles.timeWrapper}> | |
| <Clock size={12} /> | |
| {new Date(trace.created_at).toLocaleTimeString()} | |
| </div> | |
| <div className={styles.dateLabel}>{new Date(trace.created_at).toLocaleDateString()}</div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={styles.agentInfo}> | |
| <span className={styles.agentName}>{trace.agent_name.replace(/_/g, ' ').toUpperCase()}</span> | |
| <span className={styles.modelInfo}>{trace.model_info || 'NEURAL_CORE_V1'}</span> | |
| </div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={styles.metricStack}> | |
| <div className={styles.metricItem}> | |
| <span className={styles.mLabel}>LATENCY:</span> | |
| <span className={styles.mValue}>{trace.duration_ms?.total || trace.duration_ms || 0}ms</span> | |
| </div> | |
| <div className={styles.metricItem}> | |
| <span className={styles.mLabel}>TOKENS:</span> | |
| <span className={styles.mValue}>{trace.usage_metadata?.total_tokens || 0}</span> | |
| </div> | |
| </div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={`${styles.actionPill} ${trace.status === 'error' ? styles.fail : styles.pass}`}> | |
| {(trace.status || 'SUCCESS').toUpperCase()} | |
| </div> | |
| </div> | |
| <div className={styles.col}> | |
| <div className={styles.detailBox}> | |
| <div className={styles.traceSection}> | |
| <span className={styles.traceLabel}>PROMPT_GUIDE:</span> | |
| <pre className={styles.jsonDetail}>{JSON.stringify(trace.input_data, null, 2)}</pre> | |
| </div> | |
| <div className={styles.traceDivider} /> | |
| <div className={styles.traceSection}> | |
| <span className={styles.traceLabel}>RESULTANT_INFERENCE:</span> | |
| <pre className={styles.jsonDetail}>{JSON.stringify(trace.output_data, null, 2)}</pre> | |
| </div> | |
| </div> | |
| </div> | |
| </motion.div> | |
| ))} | |
| </AnimatePresence> | |
| {aiTraces.length === 0 && !loading && ( | |
| <div className={styles.emptyState}> | |
| <Activity size={48} opacity={0.2} /> | |
| <p>No neural traces found in current temporal window.</p> | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| <div className={styles.paginationFooter}> | |
| <div className={styles.pagination}> | |
| <button | |
| className={styles.pageBtn} | |
| onClick={() => fetchAIInteractions(page - 1)} | |
| disabled={page === 1 || loading} | |
| > | |
| <ChevronLeft size={16} /> | |
| </button> | |
| <div className={styles.pageIndicator}> | |
| PAGE <span>{page}</span> | |
| </div> | |
| <button | |
| className={styles.pageBtn} | |
| onClick={() => fetchAIInteractions(page + 1)} | |
| disabled={!hasMore || loading} | |
| > | |
| <ChevronRight size={16} /> | |
| </button> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| return ( | |
| <div className={styles.container}> | |
| <nav className={styles.tabNav}> | |
| <button | |
| className={`${styles.tabBtn} ${activeTab === 'security' ? styles.active : ''}`} | |
| onClick={() => { setActiveTab('security'); setLocalSearch(''); setSearchTerm(''); setPage(1); }} | |
| > | |
| <ShieldCheck size={18} /> | |
| SECURITY AUDIT LOG | |
| </button> | |
| <button | |
| className={`${styles.tabBtn} ${activeTab === 'ai' ? styles.active : ''}`} | |
| onClick={() => { setActiveTab('ai'); setLocalSearch(''); setSearchTerm(''); setPage(1); }} | |
| > | |
| <Activity size={18} /> | |
| NEURAL TRACE FORENSICS | |
| </button> | |
| </nav> | |
| <div className={styles.content}> | |
| {activeTab === 'security' ? renderSecurityAudit() : renderNeuralTrace()} | |
| </div> | |
| </div> | |
| ); | |
| }; | |