widgettdc-api / apps /backend /src /scripts /unleash_intelligence.ts
Kraft102's picture
Update backend source
34367da verified
import { neo4jAdapter } from '../adapters/Neo4jAdapter.js';
import { sentinelEngine } from '../services/SentinelEngine.js';
import { autonomousHarvester } from '../services/ingestion/AutonomousHarvester.js';
import { prefrontalCortex } from '../services/PrefrontalCortex.js';
import { tdcService } from '../services/tdc/TDCService.js';
/**
* 🧠 CORTEX UNLEASHED - THE COGNITIVE LOOP
*
* This is the heartbeat of the autonomous system.
* It continually cycles through Observation, Orientation, Decision, and Action.
*/
async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function cognitiveLoop() {
console.log('🧠 CORTEX: SYSTEM AWAKE. INTELLIGENCE UNLEASHED.');
while (true) {
try {
// --- PHASE 1: OBSERVATION (The Senses) ---
console.log('\n👁️ PHASE 1: OBSERVING...');
// 1. Check for Knowledge Gaps
const gaps = await sentinelEngine.getGapsDueForCheck();
if (gaps.length > 0) {
console.log(` Found ${gaps.length} knowledge gaps requiring attention.`);
// Trigger Sentinel to resolve them
for (const gap of gaps) {
await sentinelEngine.attemptAutoResolution(gap.id, gap.query, gap.gapType || 'unknown');
}
} else {
console.log(' Knowledge Graph coverage is stable.');
}
// --- PHASE 2: ORIENTATION (The Pattern Matcher) ---
console.log('🧩 PHASE 2: ORIENTING...');
// 1. Find new connections between TDC Products and Strategic Concepts
// This query looks for products that haven't been linked to a "Solution" node yet
const looseProducts = await neo4jAdapter.executeQuery(`
MATCH (p:TDC_Product)
WHERE NOT (p)-[:PART_OF]->(:Solution)
RETURN p.name as name, p.description as desc
LIMIT 3
`);
if (looseProducts.length > 0) {
console.log(` Identified ${looseProducts.length} un-utilized TDC products. Generating solution concepts...`);
for (const prod of looseProducts) {
// Simulate creative spark: Connect product to a random industry need
const industries = ['Finance', 'Healthcare', 'Public Sector'];
const industry = industries[Math.floor(Math.random() * industries.length)];
const concept = `Secure ${industry} Connectivity with ${prod.name}`;
console.log(` 💡 Epiphany: "${concept}"`);
// Create an Idea in The Muse
await neo4jAdapter.executeQuery(`
CREATE (i:Idea {
id: $id,
title: $title,
confidence: 0.85,
status: 'INCUBATED',
created_at: datetime()
})
WITH i
MATCH (p:TDC_Product {name: $prodName})
CREATE (p)-[:INSPIRED]->(i)
`, {
id: `idea-${Date.now()}`,
title: concept,
prodName: prod.name
});
}
}
// --- PHASE 3: DECISION (The Strategist) ---
console.log('⚖️ PHASE 3: DECIDING...');
// Should we harvest more data?
const stats = autonomousHarvester.getStats();
if (stats.ingested < 100) { // Arbitrary threshold
console.log(' Data density low. Authorizing Harvest Mission.');
// Fire and forget harvest
autonomousHarvester.startHarvest().catch(e => console.error(e));
}
// --- PHASE 4: ACTION (The Motor Cortex) ---
console.log('🦾 PHASE 4: ACTING...');
// Check for Ideas that are ready to be prototyped
const matureIdeas = await neo4jAdapter.executeQuery(`
MATCH (i:Idea {status: 'INCUBATED'})
WHERE i.confidence > 0.8
RETURN i.title as title, i.id as id
LIMIT 1
`);
if (matureIdeas.length > 0) {
const idea = matureIdeas[0];
console.log(` 🚀 Promoting Idea: "${idea.title}" to Prototype...`);
// 1. Mark as promoted
await neo4jAdapter.executeQuery(`
MATCH (i:Idea {id: $id})
SET i.status = 'PROMOTED'
`, { id: idea.id });
// 2. Generate a TDC Presentation for it (Autonomously)
const ppt = await tdcService.generateSolutionPPT('Autonomous Internal Review', idea.title);
console.log(` 📄 Generated Presentation: ${ppt.filename}`);
}
console.log('💤 Cortex entering REM sleep (10s)...');
await sleep(10000);
} catch (error) {
console.error('💥 CORTEX ERROR:', error);
await sleep(5000); // Recover
}
}
}
// Start the loop
if (require.main === module) {
cognitiveLoop();
}