Kraft102's picture
Update backend source
34367da verified
// ⏳ THE TIME LORD: setup-graph.ts
// Point 2: Temporal Hypergraphs
// Defines the reality schema and timeline structures
import { neo4jService } from '../database/Neo4jService.js';
async function setupSchema() {
console.log('⏳ [CHRONOS] Defining Reality Schema...');
console.log('');
try {
// 1. Uniqueness Constraints (Point 1: SEGA)
console.log('πŸ“‹ Creating Uniqueness Constraints...');
const constraints = [
'CREATE CONSTRAINT file_id IF NOT EXISTS FOR (f:File) REQUIRE f.id IS UNIQUE',
'CREATE CONSTRAINT agent_id IF NOT EXISTS FOR (a:Agent) REQUIRE a.id IS UNIQUE',
'CREATE CONSTRAINT identity_email IF NOT EXISTS FOR (i:Identity) REQUIRE i.email IS UNIQUE',
'CREATE CONSTRAINT event_id IF NOT EXISTS FOR (e:Event) REQUIRE e.id IS UNIQUE',
'CREATE CONSTRAINT knowledge_id IF NOT EXISTS FOR (k:Knowledge) REQUIRE k.id IS UNIQUE'
];
for (const constraint of constraints) {
try {
await neo4jService.write(constraint);
console.log(` βœ“ ${constraint.split('FOR')[0].trim()}`);
} catch (e: any) {
if (!e.message?.includes('already exists')) {
console.warn(` ⚠ ${e.message?.slice(0, 60)}`);
}
}
}
// 2. Indexes for performance
console.log('\nπŸ“Š Creating Indexes...');
const indexes = [
'CREATE INDEX file_name_idx IF NOT EXISTS FOR (f:File) ON (f.name)',
'CREATE INDEX file_path_idx IF NOT EXISTS FOR (f:File) ON (f.path)',
'CREATE INDEX agent_role_idx IF NOT EXISTS FOR (a:Agent) ON (a.role)',
'CREATE INDEX event_timestamp_idx IF NOT EXISTS FOR (e:Event) ON (e.timestamp)',
'CREATE INDEX identity_name_idx IF NOT EXISTS FOR (i:Identity) ON (i.name)',
'CREATE INDEX knowledge_type_idx IF NOT EXISTS FOR (k:Knowledge) ON (k.type)'
];
for (const index of indexes) {
try {
await neo4jService.write(index);
console.log(` βœ“ ${index.split('FOR')[0].trim()}`);
} catch (e: any) {
if (!e.message?.includes('already exists')) {
console.warn(` ⚠ ${e.message?.slice(0, 60)}`);
}
}
}
// 3. Vector Index (Point 6: Dark Matter)
console.log('\nπŸŒ‘ Creating Vector Index (Dark Matter)...');
try {
await neo4jService.write(`
CREATE VECTOR INDEX file_embeddings IF NOT EXISTS
FOR (f:File) ON (f.embedding)
OPTIONS {indexConfig: {
\`vector.dimensions\`: 384,
\`vector.similarity_function\`: 'cosine'
}}
`);
console.log(' βœ“ Vector Index Created (384 dimensions, cosine similarity)');
} catch (e: any) {
console.warn(' ⚠ Vector index creation skipped (requires Neo4j 5.x with Vector support)');
console.warn(` ${e.message?.slice(0, 80)}`);
}
// 4. Full-text indexes for search
console.log('\nπŸ” Creating Full-Text Indexes...');
try {
await neo4jService.write(`
CREATE FULLTEXT INDEX file_content_search IF NOT EXISTS
FOR (f:File) ON EACH [f.name, f.contentPreview]
`);
console.log(' βœ“ Full-text index on File content');
} catch (e: any) {
if (!e.message?.includes('already exists')) {
console.warn(` ⚠ Full-text index skipped: ${e.message?.slice(0, 60)}`);
}
}
try {
await neo4jService.write(`
CREATE FULLTEXT INDEX knowledge_search IF NOT EXISTS
FOR (k:Knowledge) ON EACH [k.title, k.summary, k.content]
`);
console.log(' βœ“ Full-text index on Knowledge content');
} catch (e: any) {
if (!e.message?.includes('already exists')) {
console.warn(` ⚠ Full-text index skipped: ${e.message?.slice(0, 60)}`);
}
}
// 5. Create core agent nodes
console.log('\nπŸ€– Creating Core Agent Nodes...');
const coreAgents = [
{ id: 'SYSTEM_CORE', role: 'ARCHITECT', name: 'System Core' },
{ id: 'PROMETHEUS', role: 'ARCHITECT', name: 'Prometheus Engine' },
{ id: 'KNOWLEDGE_COMPILER', role: 'EXECUTOR', name: 'Knowledge Compiler' },
{ id: 'SWARM_CONTROLLER', role: 'ARCHITECT', name: 'Swarm Controller' },
{ id: 'ANGEL_PROXY', role: 'GUARDIAN', name: 'Angel Security Proxy' }
];
for (const agent of coreAgents) {
await neo4jService.write(`
MERGE (a:Agent {id: $id})
SET a.role = $role, a.name = $name, a.status = 'ACTIVE', a.createdAt = datetime()
`, agent);
console.log(` βœ“ Agent: ${agent.name} (${agent.role})`);
}
// 6. Create initial event
console.log('\nπŸ“… Recording Genesis Event...');
await neo4jService.write(`
CREATE (e:Event {
id: 'GENESIS_' + toString(timestamp()),
type: 'SYSTEM_INIT',
description: 'Neural Singularity Schema Initialized',
timestamp: datetime(),
source: 'CHRONOS'
})
`);
console.log(' βœ“ Genesis event recorded');
console.log('\n' + '='.repeat(60));
console.log('βœ… SCHEMA SETUP COMPLETE');
console.log('='.repeat(60));
console.log('\nNeural Singularity Graph is ready for:');
console.log(' β€’ Knowledge Compilation (SEGA)');
console.log(' β€’ Temporal Event Tracking (THG)');
console.log(' β€’ Swarm Consciousness (SCE)');
console.log(' β€’ Vector Similarity Search (CDMM)');
console.log(' β€’ Full-Text Search');
console.log('');
} catch (error: any) {
console.error('\n❌ Schema Setup Failed:', error.message);
process.exit(1);
} finally {
await neo4jService.close();
process.exit(0);
}
}
// Run if called directly
setupSchema();