Spaces:
Paused
Paused
| import { Request, Response } from 'express'; | |
| import { logger } from '../utils/logger.js'; | |
| import { selfHealing, SelfHealingAdapter } from '../services/SelfHealingAdapter.js'; | |
| import { RedisService } from '../services/RedisService.js'; | |
| const log = logger.child({ module: 'CortexController' }); | |
| const immuneSystem = selfHealing; | |
| const redis = RedisService.getInstance(); | |
| export class CortexController { | |
| /** | |
| * GET /api/cortex/graph | |
| * Retrieves the active neural map from Redis or seeds it. | |
| */ | |
| static async getGraph(req: Request, res: Response) { | |
| try { | |
| // 1. Try to recall from Collective Memory (Redis) | |
| let graph = await redis.getGraphState(); | |
| // 2. If Amnesia (Empty), generate Seed Data | |
| if (!graph) { | |
| log.info('🌱 Generating Synaptic Seed Data...'); | |
| graph = { | |
| nodes: [ | |
| { | |
| id: "CORE", | |
| label: "WidgeTDC Core", | |
| type: "System", | |
| x: 0, y: 0, | |
| radius: 45, | |
| data: { | |
| "CPU Load": "12%", | |
| "Uptime": "99.99%", | |
| "Active Agents": 8, | |
| "Energy Index": "Optimal", | |
| "Network Latency": "2ms", | |
| "Security Integrity": "100%", | |
| "Optimization Level": "High" | |
| } | |
| }, | |
| { | |
| id: "OUTLOOK", | |
| label: "Outlook Pipe", | |
| type: "Ingestion", | |
| x: -150, y: -100, | |
| radius: 35, | |
| data: { | |
| "Email Count": 14205, | |
| "Total Size": "4.2 GB", | |
| "Daily Growth": "+150 MB", | |
| "Learning Contribution": "High", | |
| "Last Sync": "Just now", | |
| "Sentiment Avg": "Neutral", | |
| "Top Topics": ["Project X", "Budget", "HR"], | |
| "Security Flags": 0 | |
| } | |
| }, | |
| { | |
| id: "FILES", | |
| label: "File Watcher", | |
| type: "Ingestion", | |
| x: 150, y: -100, | |
| radius: 35, | |
| data: { | |
| "File Count": 8503, | |
| "Storage Usage": "1.5 TB", | |
| "Indexing Status": "Active", | |
| "Knowledge Extraction": "92%", | |
| "MIME Types": "PDF, DOCX, XLSX", | |
| "Duplicate Ratio": "4%", | |
| "OCR Success": "98%", | |
| "Vector Embeddings": "1.2M" | |
| } | |
| }, | |
| { | |
| id: "HYPER", | |
| label: "HyperLog Vector", | |
| type: "Memory", | |
| x: 0, y: 150, | |
| radius: 40, | |
| data: { | |
| "Vector Dimensions": 1536, | |
| "Memory Density": "85%", | |
| "Recall Accuracy": "94.5%", | |
| "Forgetting Curve": "Stable", | |
| "Association Strength": "Strong", | |
| "Active Contexts": 12, | |
| "Pattern Confidence": "High" | |
| } | |
| }, | |
| { | |
| id: "GEMINI", | |
| label: "Architect Agent", | |
| type: "Agent", | |
| x: 200, y: 50, | |
| radius: 30, | |
| data: { | |
| "Tokens Processed": "45M", | |
| "Goal Completion": "88%", | |
| "Adaptation Score": "9.2/10", | |
| "Tool Usage": "High", | |
| "Creativity Index": "85", | |
| "Current Focus": "Optimization", | |
| "Ethical Alignment": "100%" | |
| } | |
| } | |
| ], | |
| links: [ | |
| { source: "CORE", target: "OUTLOOK" }, | |
| { source: "CORE", target: "FILES" }, | |
| { source: "CORE", target: "HYPER" }, | |
| { source: "CORE", target: "GEMINI" } | |
| ] | |
| }; | |
| await redis.saveGraphState(graph); | |
| } | |
| res.json({ success: true, graph }); | |
| } catch (error: any) { | |
| await immuneSystem.handleError(error, 'CortexScan'); | |
| res.status(500).json({ success: false, error: 'Synaptic Failure' }); | |
| } | |
| } | |
| /** | |
| * POST /api/cortex/nudge | |
| * Handles haptic impulses and triggers reflexes. | |
| */ | |
| static async processNudge(req: Request, res: Response) { | |
| const { nodeId } = req.body; | |
| try { | |
| log.info(`⚡ SYNAPTIC IMPULSE: Node [${nodeId}]`); | |
| // Logic Bindings (The Reflexes) | |
| let reaction = "Impulse Propagated"; | |
| if (nodeId === 'OUTLOOK') reaction = "Syncing Inbox..."; | |
| if (nodeId === 'HYPER') reaction = "Re-indexing Vectors..."; | |
| if (nodeId === 'GEMINI') reaction = "Architect is listening."; | |
| // Telepathy: Inform other clients | |
| await redis.publishImpulse('NUDGE', { nodeId, reaction }); | |
| res.json({ success: true, reaction, timestamp: new Date().toISOString() }); | |
| } catch (error: any) { | |
| await immuneSystem.handleError(error, `NudgeNode:${nodeId}`); | |
| res.status(400).json({ success: false, message: "Impulse Rejected" }); | |
| } | |
| } | |
| /** | |
| * POST /api/cortex/inject | |
| * Allows external injection of nodes (Files, Emails, Thoughts). | |
| */ | |
| static async injectNode(req: Request, res: Response) { | |
| const { label, type, data } = req.body; | |
| try { | |
| if (label.includes('<script>')) throw new Error("Malicious Payload"); | |
| const graph = await redis.getGraphState() || { nodes: [], links: [] }; | |
| const newNode = { | |
| id: Math.random().toString(36).substr(2, 9).toUpperCase(), | |
| label, | |
| type, | |
| x: (Math.random() - 0.5) * 400, | |
| y: (Math.random() - 0.5) * 400, | |
| vx: 0, vy: 0, | |
| data: data || {}, | |
| radius: 15 | |
| }; | |
| graph.nodes.push(newNode); | |
| if (graph.nodes.length > 1) { | |
| graph.links.push({ source: "CORE", target: newNode.id }); | |
| } | |
| await redis.saveGraphState(graph); | |
| log.info(`💉 INJECTION: [${type}] ${label}`); | |
| res.json({ success: true, id: newNode.id }); | |
| } catch (error: any) { | |
| res.status(403).json({ success: false, error: "Injection Blocked" }); | |
| } | |
| } | |
| } | |