Kraft102's picture
Initial deployment - WidgeTDC Cortex Backend v2.1.0
529090e
/**
* Cognitive Error Intelligence API Routes
* ========================================
* Advanced AI-powered error handling endpoints
*/
import express from 'express';
import { cognitiveErrorIntelligence } from '../services/CognitiveErrorIntelligence.js';
const router = express.Router();
// ═══════════════════════════════════════════════════════════════════════════
// INTELLIGENT ERROR PROCESSING
// ═══════════════════════════════════════════════════════════════════════════
/**
* POST /api/intelligence/process
* Process an error with full cognitive analysis
*/
router.post('/process', async (req, res) => {
try {
const { error, service, context, stackTrace } = req.body;
if (!error || typeof error !== 'string') {
return res.status(400).json({ error: 'error message is required' });
}
const result = await cognitiveErrorIntelligence.processError(
error,
service || 'unknown',
context || {},
stackTrace
);
res.json({
success: true,
...result,
solutionCount: result.solutions.length,
correlationCount: result.correlatedErrors.length,
hasAutoRemediation: result.autoRemediation?.queued || false
});
} catch (error) {
console.error('Error processing with CEI:', error);
res.status(500).json({ error: 'Failed to process error' });
}
});
/**
* GET /api/intelligence/stats
* Get CEI system statistics
*/
router.get('/stats', (_req, res) => {
try {
const stats = cognitiveErrorIntelligence.getStats();
res.json(stats);
} catch (error) {
console.error('Error getting CEI stats:', error);
res.status(500).json({ error: 'Failed to get stats' });
}
});
/**
* GET /api/intelligence/correlations
* Get learned error correlations
*/
router.get('/correlations', (_req, res) => {
try {
const correlations = cognitiveErrorIntelligence.getCorrelations();
res.json({
total: correlations.length,
correlations: correlations.slice(0, 50) // Limit to top 50
});
} catch (error) {
console.error('Error getting correlations:', error);
res.status(500).json({ error: 'Failed to get correlations' });
}
});
/**
* POST /api/intelligence/metric
* Record a metric for predictive analysis
*/
router.post('/metric', (req, res) => {
try {
const { metric, value } = req.body;
if (!metric || typeof value !== 'number') {
return res.status(400).json({ error: 'metric and numeric value required' });
}
cognitiveErrorIntelligence.recordMetric(metric, value);
res.json({ success: true, metric, value });
} catch (error) {
console.error('Error recording metric:', error);
res.status(500).json({ error: 'Failed to record metric' });
}
});
/**
* POST /api/intelligence/persist-correlations
* Persist learned correlations to Neo4j
*/
router.post('/persist-correlations', async (_req, res) => {
try {
const count = await cognitiveErrorIntelligence.persistCorrelationsToNeo4j();
res.json({
success: true,
persisted: count,
message: `Persisted ${count} correlations to Neo4j`
});
} catch (error) {
console.error('Error persisting correlations:', error);
res.status(500).json({ error: 'Failed to persist correlations' });
}
});
/**
* POST /api/intelligence/approve-remediation/:actionId
* Approve a pending remediation action
*/
router.post('/approve-remediation/:actionId', (req, res) => {
try {
const { actionId } = req.params;
const approved = cognitiveErrorIntelligence.approveRemediation(actionId);
if (approved) {
res.json({ success: true, message: `Remediation ${actionId} approved` });
} else {
res.status(404).json({ error: 'Remediation action not found' });
}
} catch (error) {
console.error('Error approving remediation:', error);
res.status(500).json({ error: 'Failed to approve remediation' });
}
});
/**
* POST /api/intelligence/context
* Update system context for better recommendations
*/
router.post('/context', (req, res) => {
try {
const { load, services } = req.body;
const updates: any = {};
if (load) updates.load = load;
if (services) {
updates.services = new Map(Object.entries(services));
}
cognitiveErrorIntelligence.updateSystemContext(updates);
res.json({ success: true, message: 'Context updated' });
} catch (error) {
console.error('Error updating context:', error);
res.status(500).json({ error: 'Failed to update context' });
}
});
export default router;