File size: 8,130 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Test script for The Dreaming Mind (Level 3 HyperLog)
 *
 * Creates thoughts WITH embeddings and tests semantic search
 */

import { config } from 'dotenv';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import neo4j from 'neo4j-driver';
import { v4 as uuidv4 } from 'uuid';
import { pipeline } from '@xenova/transformers';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
config({ path: resolve(__dirname, '../../.env') });

const NEO4J_URI = process.env.NEO4J_URI!;
const NEO4J_USERNAME = process.env.NEO4J_USERNAME!;
const NEO4J_PASSWORD = process.env.NEO4J_PASSWORD!;

// Embedding model
let embedder: any = null;

async function getEmbedding(text: string): Promise<number[]> {
    if (!embedder) {
        console.log('πŸ”„ Loading embedding model (first time)...');
        embedder = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', { quantized: true });
        console.log('βœ… Embedding model ready');
    }
    const output = await embedder(text, { pooling: 'mean', normalize: true });
    return Array.from(output.data) as number[];
}

async function testDreamingMind() {
    console.log('🧠 The Dreaming Mind - Level 3 Test');
    console.log('====================================');
    console.log(`Connecting to: ${NEO4J_URI}`);

    const driver = neo4j.driver(
        NEO4J_URI,
        neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
    );

    await driver.verifyConnectivity();
    console.log('βœ… Connected to Neo4j Aura\n');

    const session = driver.session();
    const correlationId = uuidv4();

    try {
        // 1. Create Vector Index
        console.log('πŸ“‡ Creating vector index...');
        try {
            await session.run(`
                CREATE VECTOR INDEX hyper_thought_vectors IF NOT EXISTS
                FOR (e:HyperEvent) ON (e.embedding)
                OPTIONS {indexConfig: {
                    \`vector.dimensions\`: 384,
                    \`vector.similarity_function\`: 'cosine'
                }}
            `);
            console.log('βœ… Vector index ready (384D cosine)\n');
        } catch (e: any) {
            if (e.message.includes('already exists')) {
                console.log('βœ… Vector index already exists\n');
            } else {
                console.warn('⚠️  Vector index issue:', e.message);
            }
        }

        // 2. Create thoughts WITH embeddings
        console.log('🧠 Creating thoughts with embeddings...\n');

        const thoughts = [
            { type: 'USER_INTENT', agent: 'GraphRAG', content: 'Analyze network security threats from firewall logs' },
            { type: 'THOUGHT', agent: 'GraphRAG', content: 'I should search for blocked connection attempts and failed authentications' },
            { type: 'DATA_RETRIEVAL', agent: 'GraphRAG', content: 'Found 47 blocked SSH attempts from IP 192.168.1.105 in the last hour' },
            { type: 'INSIGHT', agent: 'GraphRAG', content: 'This IP appears to be conducting a brute force attack against our SSH servers' },
            { type: 'THOUGHT', agent: 'ThreatHunter', content: 'Need to check if this IP has been flagged in threat intelligence databases' },
            { type: 'DATA_RETRIEVAL', agent: 'ThreatHunter', content: 'IP 192.168.1.105 is associated with known botnet infrastructure' },
            { type: 'CRITICAL_DECISION', agent: 'ThreatHunter', content: 'Recommending immediate IP block and security team notification' },
            { type: 'USER_INTENT', agent: 'Analyst', content: 'Generate a compliance report for GDPR data processing activities' },
            { type: 'THOUGHT', agent: 'Analyst', content: 'I need to gather all data processing records and retention policies' },
            { type: 'INSIGHT', agent: 'Analyst', content: 'Several data processing activities are missing proper consent documentation' },
        ];

        let prevId: string | null = null;

        for (const thought of thoughts) {
            const eventId = uuidv4();
            const timestamp = Date.now();

            // Generate embedding
            const embedding = await getEmbedding(thought.content);
            console.log(`  βœ“ ${thought.type}: ${thought.content.substring(0, 45)}... [${embedding.length}D vector]`);

            // Store in Neo4j with embedding
            await session.run(`
                CREATE (e:HyperEvent {
                    id: $id,
                    type: $type,
                    agent: $agent,
                    content: $content,
                    timestamp: $timestamp,
                    correlationId: $correlationId,
                    embedding: $embedding,
                    metadata: '{}'
                })
            `, {
                id: eventId,
                type: thought.type,
                agent: thought.agent,
                content: thought.content,
                timestamp,
                correlationId,
                embedding
            });

            // Create causal chain
            if (prevId) {
                await session.run(`
                    MATCH (prev:HyperEvent {id: $prevId})
                    MATCH (curr:HyperEvent {id: $currId})
                    CREATE (prev)-[:LED_TO]->(curr)
                `, { prevId, currId: eventId });
            }

            prevId = eventId;
            await new Promise(r => setTimeout(r, 100));
        }

        console.log('\nβœ… All thoughts created with embeddings\n');

        // 3. Wait for index to populate
        console.log('⏳ Waiting for vector index to populate...');
        await new Promise(r => setTimeout(r, 3000));

        // 4. Test semantic search - "Dream Mode"
        console.log('\nπŸŒ™ DREAM MODE: Testing semantic search\n');

        const searchQueries = [
            'cybersecurity attack detection',
            'GDPR compliance issues',
            'network intrusion',
        ];

        for (const query of searchQueries) {
            console.log(`\nπŸ” Searching: "${query}"`);
            const queryVector = await getEmbedding(query);

            const result = await session.run(`
                CALL db.index.vector.queryNodes('hyper_thought_vectors', 3, $queryVector)
                YIELD node, score
                RETURN node.content AS content, node.agent AS agent, node.type AS type, score
                ORDER BY score DESC
            `, { queryVector });

            if (result.records.length === 0) {
                console.log('   No results found');
            } else {
                result.records.forEach((r, i) => {
                    const score = r.get('score').toFixed(3);
                    const content = r.get('content').substring(0, 60);
                    const agent = r.get('agent');
                    console.log(`   ${i + 1}. [${score}] (${agent}) ${content}...`);
                });
            }
        }

        // 5. Verify embeddings stored
        console.log('\n\nπŸ“Š Verification:');

        const countResult = await session.run(`
            MATCH (e:HyperEvent)
            WHERE e.embedding IS NOT NULL
            RETURN count(e) as count, avg(size(e.embedding)) as avgDim
        `);

        const count = countResult.records[0].get('count').toNumber();
        const avgDim = countResult.records[0].get('avgDim');
        console.log(`  Thoughts with embeddings: ${count}`);
        console.log(`  Average embedding dimension: ${avgDim}`);

        const totalResult = await session.run('MATCH (e:HyperEvent) RETURN count(e) as total');
        console.log(`  Total HyperEvents: ${totalResult.records[0].get('total').toNumber()}`);

        console.log('\nβœ… The Dreaming Mind is ACTIVE!');
        console.log('\nπŸ“‹ Neo4j Browser query to visualize:');
        console.log('   MATCH (e:HyperEvent) WHERE e.embedding IS NOT NULL RETURN e.content, size(e.embedding) LIMIT 10');

    } finally {
        await session.close();
        await driver.close();
    }
}

testDreamingMind()
    .then(() => process.exit(0))
    .catch(e => {
        console.error('❌ Error:', e);
        process.exit(1);
    });