Spaces:
Paused
Paused
File size: 6,037 Bytes
529090e | 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 | 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" });
}
}
}
|