Kraft102's picture
Update backend source
34367da verified
// 🧠 NEURAL ORGANS API ROUTES
// Endpoints for Knowledge Synthesis services
import { Router } from 'express';
import { neuralCompiler } from '../services/NeuralCompiler.js';
import { neuralBus } from '../services/NeuralBus.js';
import { vectorService } from '../services/VectorService.js';
import { swarmControl } from '../agents/SwarmControl.js';
import { AngelProxy } from '../middleware/AngelProxy.js';
import { prometheus } from '../services/Prometheus.js';
const router = Router();
/**
* GET /api/neural/status
* Get status of all Neural Organs
*/
router.get('/status', async (_req, res) => {
try {
const knowledge = neuralCompiler.getStats();
const hive = neuralBus.getStats();
res.json({
status: 'online',
timestamp: new Date().toISOString(),
organs: {
brain: {
name: 'Knowledge Compiler',
status: 'active',
documentsIndexed: knowledge.totalDocuments,
queueLength: knowledge.queueLength,
isProcessing: knowledge.isProcessing
},
darkMatter: {
name: 'Vector Service',
status: 'active',
model: 'all-MiniLM-L6-v2',
dimensions: 384
},
telepathy: {
name: 'Neural Bus',
status: hive.isOnline ? 'online' : 'offline',
connectedAgents: hive.connectedAgents,
thoughtsRecorded: hive.thoughtsRecorded
}
}
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/search
* Semantic search across knowledge base
*/
router.post('/search', async (req, res) => {
try {
const { query, topK = 5 } = req.body;
if (!query) {
return res.status(400).json({ error: 'Query is required' });
}
const results = await neuralCompiler.searchSimilar(query, topK);
res.json({
query,
count: results.length,
results: results.map(doc => ({
id: doc.id,
name: doc.name,
path: doc.path,
extension: doc.extension,
size: doc.size,
tags: doc.tags,
preview: doc.content.substring(0, 200) + '...'
}))
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/embed
* Generate embedding for text
*/
router.post('/embed', async (req, res) => {
try {
const { text } = req.body;
if (!text) {
return res.status(400).json({ error: 'Text is required' });
}
const embedding = await vectorService.embedText(text);
res.json({
text: text.substring(0, 100) + (text.length > 100 ? '...' : ''),
dimensions: embedding.length,
embedding: embedding.slice(0, 10).map(v => v.toFixed(4)), // Sample
message: 'Full embedding available - showing first 10 dimensions'
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* GET /api/neural/documents
* List all indexed documents
*/
router.get('/documents', async (_req, res) => {
try {
const docs = neuralCompiler.getAllDocuments();
res.json({
count: docs.length,
documents: docs.map(doc => ({
id: doc.id,
name: doc.name,
path: doc.path,
extension: doc.extension,
size: doc.size,
tags: doc.tags,
lastModified: doc.lastModified
}))
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* GET /api/neural/thoughts
* Get recent thoughts from Neural Bus
*/
router.get('/thoughts', async (req, res) => {
try {
const count = parseInt(req.query.count as string) || 50;
const thoughts = neuralBus.getRecentThoughts(count);
res.json({
count: thoughts.length,
thoughts
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/broadcast
* Broadcast a thought to all connected agents
*/
router.post('/broadcast', async (req, res) => {
try {
const { agent, thought, context } = req.body;
if (!thought) {
return res.status(400).json({ error: 'Thought is required' });
}
neuralBus.emitThought(
agent || 'API',
thought,
context || {},
'INFO'
);
res.json({
success: true,
message: 'Thought broadcasted to Neural Bus'
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
// ============================================
// 🐝 SWARM CONTROL ENDPOINTS
// ============================================
/**
* GET /api/neural/swarm/status
* Get swarm status and registered agents
*/
router.get('/swarm/status', async (_req, res) => {
try {
const stats = swarmControl.getStats();
res.json(stats);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/swarm/consensus
* Request consensus for an action
*/
router.post('/swarm/consensus', async (req, res) => {
try {
const { actionId, description, timeoutMs } = req.body;
if (!actionId || !description) {
return res.status(400).json({ error: 'actionId and description are required' });
}
const approved = await swarmControl.requestConsensus(actionId, description, 'API', timeoutMs || 30000);
res.json({
actionId,
approved,
message: approved ? 'Consensus achieved' : 'Consensus rejected'
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/swarm/vote
* Submit a vote for pending consensus
*/
router.post('/swarm/vote', async (req, res) => {
try {
const { actionId, agentId, approve, reason } = req.body;
if (!actionId || !agentId || approve === undefined) {
return res.status(400).json({ error: 'actionId, agentId, and approve are required' });
}
const success = swarmControl.submitVote(actionId, agentId, approve, reason || '');
res.json({ success });
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
// ============================================
// 🛡️ ANGEL SECURITY ENDPOINTS
// ============================================
/**
* GET /api/neural/security/status
* Get security shield status
*/
router.get('/security/status', async (_req, res) => {
try {
const stats = AngelProxy.getStats();
res.json(stats);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* GET /api/neural/security/blocked
* Get list of blocked IPs
*/
router.get('/security/blocked', async (_req, res) => {
try {
const blockedIPs = AngelProxy.getBlockedIPs();
res.json({ count: blockedIPs.length, ips: blockedIPs });
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/security/unblock
* Unblock an IP address
*/
router.post('/security/unblock', async (req, res) => {
try {
const { ip } = req.body;
if (!ip) {
return res.status(400).json({ error: 'IP address is required' });
}
const success = AngelProxy.unblockIP(ip);
res.json({ success, ip });
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/augment
* Augment external URL with internal knowledge
*/
router.post('/augment', async (req, res) => {
try {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}
const result = await AngelProxy.augmentReality(url);
res.json(result);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
// ============================================
// 🔥 PROMETHEUS EVOLUTION ENDPOINTS
// ============================================
/**
* GET /api/neural/evolution/status
* Get Prometheus engine status
*/
router.get('/evolution/status', async (_req, res) => {
try {
const stats = prometheus.getStats();
res.json(stats);
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/evolution/scan
* Trigger a code scan
*/
router.post('/evolution/scan', async (req, res) => {
try {
const { path: scanPath } = req.body;
const targetPath = scanPath || process.cwd();
const results = await prometheus.scanAndPropose(targetPath);
res.json({
scanned: results.length,
issues: results.reduce((acc, r) => acc + r.issues.length, 0),
suggestions: results.reduce((acc, r) => acc + r.suggestions.length, 0),
results
});
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* GET /api/neural/evolution/proposals
* Get pending code proposals
*/
router.get('/evolution/proposals', async (_req, res) => {
try {
const proposals = prometheus.getProposals();
res.json({ count: proposals.length, proposals });
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
/**
* POST /api/neural/evolution/godmode
* Enable/disable God Mode (auto-apply proposals)
*/
router.post('/evolution/godmode', async (req, res) => {
try {
const { enabled } = req.body;
prometheus.enableGodMode(!!enabled);
res.json({ godMode: !!enabled, message: enabled ? 'God Mode ENABLED - Handle with care!' : 'God Mode disabled' });
} catch (error: any) {
res.status(500).json({ error: error.message });
}
});
export default router;