File size: 5,765 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ⏳ 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();