File size: 22,112 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/**
 * 🧬 DNA SPLICER - Intelligence Pattern Extractor
 *
 * Analyzes leaked System Prompts and Reference Code to extract
 * intelligence patterns for WidgeTDC's Knowledge Graph.
 *
 * Node Types Created:
 * - (:Persona) - AI personalities (Claude, GPT variants, custom GPTs)
 * - (:Directive) - Rules/instructions from system prompts
 * - (:Capability) - Identified capabilities (CodeGeneration, WebBrowsing, etc.)
 * - (:Hotkey) - Interaction patterns
 * - (:Pattern) - Behavioral patterns extracted
 */

import fs from 'fs/promises';
import path from 'path';
import crypto from 'crypto';
import neo4j, { Driver, Session } from 'neo4j-driver';

// ============================================
// TYPES
// ============================================
export interface PersonaNode {
  name: string;
  source: string;        // filename
  sourceUrl?: string;    // original URL if available
  author?: string;
  description?: string;
  hash: string;
}

export interface DirectiveNode {
  id: string;
  content: string;
  type: 'rule' | 'behavior' | 'restriction' | 'capability' | 'tone' | 'format';
  priority: number;      // 1-10, how important
}

export interface CapabilityNode {
  name: string;
  description: string;
  codeSnippet?: string;
  sourceFile?: string;
}

export interface HotkeyPattern {
  key: string;
  action: string;
  description: string;
}

export interface SpliceResult {
  personas: number;
  directives: number;
  capabilities: number;
  hotkeys: number;
  duration: number;
  errors: string[];
}

// ============================================
// DIRECTIVE PATTERNS (Regex)
// ============================================
const DIRECTIVE_PATTERNS = {
  rule: /(?:you (?:must|should|always|never)|rule:|important:|note:)/i,
  behavior: /(?:when|if|respond|reply|act|behave|personality|tone)/i,
  restriction: /(?:never|don't|do not|cannot|must not|forbidden|under no circumstances)/i,
  capability: /(?:can|able to|capable|support|feature|tool|function)/i,
  tone: /(?:tone|voice|style|manner|personality|character|persona)/i,
  format: /(?:format|structure|output|respond with|use|markdown|code block)/i,
};

// ============================================
// CAPABILITY KEYWORDS
// ============================================
const CAPABILITY_KEYWORDS = [
  { keyword: 'code', capability: 'CodeGeneration' },
  { keyword: 'write code', capability: 'CodeGeneration' },
  { keyword: 'programming', capability: 'CodeGeneration' },
  { keyword: 'file', capability: 'FileOperations' },
  { keyword: 'read file', capability: 'FileReading' },
  { keyword: 'write file', capability: 'FileWriting' },
  { keyword: 'browse', capability: 'WebBrowsing' },
  { keyword: 'search', capability: 'WebSearch' },
  { keyword: 'image', capability: 'ImageGeneration' },
  { keyword: 'dalle', capability: 'ImageGeneration' },
  { keyword: 'python', capability: 'PythonExecution' },
  { keyword: 'jupyter', capability: 'PythonExecution' },
  { keyword: 'analyze', capability: 'DataAnalysis' },
  { keyword: 'math', capability: 'MathematicalReasoning' },
  { keyword: 'reason', capability: 'LogicalReasoning' },
  { keyword: 'step-by-step', capability: 'ChainOfThought' },
  { keyword: 'task', capability: 'TaskManagement' },
  { keyword: 'memory', capability: 'ContextMemory' },
  { keyword: 'remember', capability: 'ContextMemory' },
  { keyword: 'tool', capability: 'ToolUse' },
  { keyword: 'function', capability: 'FunctionCalling' },
  { keyword: 'api', capability: 'APIIntegration' },
  { keyword: 'autonomous', capability: 'AutonomousOperation' },
  { keyword: 'self-heal', capability: 'SelfHealing' },
  { keyword: 'debug', capability: 'Debugging' },
];

// ============================================
// DNA SPLICER CLASS
// ============================================
export class DNASplicer {
  private driver: Driver;
  private intelPath: string;

  // Premium prompt directories (real model system prompts)
  private static PREMIUM_PATHS = [
    'system_prompts_leaks-main/system_prompts_leaks-main/Anthropic',
    'system_prompts_leaks-main/system_prompts_leaks-main/OpenAI',
    'system_prompts_leaks-main/system_prompts_leaks-main/Google',
    'system_prompts_leaks-main/system_prompts_leaks-main/xAI',
    'system_prompts_leaks-main/system_prompts_leaks-main/Perplexity',
    'system_prompts_leaks-main/system_prompts_leaks-main/Misc',
  ];

  constructor(intelPath: string = '/intel') {
    const uri = process.env.NEO4J_URI || 'bolt://neo4j:7687';
    const user = process.env.NEO4J_USER || 'neo4j';
    const password = process.env.NEO4J_PASSWORD || 'password';

    this.driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
    this.intelPath = intelPath;

    console.log('🧬 DNA Splicer initialized');
  }

  /**
   * 🔬 PREMIUM SPLICE - Only real model prompts (Claude, GPT, Gemini, Grok)
   */
  async splicePremium(): Promise<SpliceResult> {
    const startTime = Date.now();
    const errors: string[] = [];
    let personas = 0;
    let directives = 0;
    let capabilities = 0;
    let hotkeys = 0;

    console.log('🧬 DNA SPLICER [PREMIUM MODE]: Extracting real model system prompts');

    const session = this.driver.session();

    try {
      await this.createIndexes(session);

      // Only scan premium directories
      for (const premiumPath of DNASplicer.PREMIUM_PATHS) {
        const fullPath = path.join(this.intelPath, 'Leaked_x', premiumPath);
        console.log(`📂 Scanning premium path: ${fullPath}`);

        try {
          const files = await this.findPromptFiles(fullPath);
          console.log(`   Found ${files.length} files`);

          for (const filePath of files) {
            try {
              const result = await this.processPromptFile(session, filePath, true); // premium flag
              personas += result.personas;
              directives += result.directives;
              capabilities += result.capabilities;
              hotkeys += result.hotkeys;
            } catch (err) {
              errors.push(`Failed: ${path.basename(filePath)}: ${err}`);
            }
          }
        } catch (err) {
          console.warn(`⚠️ Could not access ${premiumPath}`);
        }
      }

      await this.createPersonaRelationships(session);

    } finally {
      await session.close();
    }

    const duration = Date.now() - startTime;
    console.log(`✅ PREMIUM SPLICE COMPLETE: ${personas} personas, ${directives} directives in ${duration}ms`);

    return { personas, directives, capabilities, hotkeys, duration, errors };
  }

  /**
   * 🔬 MAIN SPLICE OPERATION
   * Scans intel directory and extracts all patterns
   */
  async splice(): Promise<SpliceResult> {
    const startTime = Date.now();
    const errors: string[] = [];
    let personas = 0;
    let directives = 0;
    let capabilities = 0;
    let hotkeys = 0;

    console.log(`🧬 DNA SPLICER: Starting intelligence extraction from ${this.intelPath}`);

    try {
      // Find all markdown files (system prompts)
      const promptFiles = await this.findPromptFiles(this.intelPath);
      console.log(`📁 Found ${promptFiles.length} prompt files`);

      const session = this.driver.session();

      try {
        // Create indexes for performance
        await this.createIndexes(session);

        // Process each prompt file
        for (const filePath of promptFiles) {
          try {
            const result = await this.processPromptFile(session, filePath);
            personas += result.personas;
            directives += result.directives;
            capabilities += result.capabilities;
            hotkeys += result.hotkeys;
          } catch (err) {
            const error = `Failed to process ${filePath}: ${err}`;
            console.error(`❌ ${error}`);
            errors.push(error);
          }
        }

        // Create inter-persona relationships
        await this.createPersonaRelationships(session);

      } finally {
        await session.close();
      }

    } catch (err) {
      errors.push(`Splice operation failed: ${err}`);
      console.error('❌ Splice failed:', err);
    }

    const duration = Date.now() - startTime;

    console.log(`✅ DNA SPLICE COMPLETE: ${personas} personas, ${directives} directives, ${capabilities} capabilities in ${duration}ms`);

    return {
      personas,
      directives,
      capabilities,
      hotkeys,
      duration,
      errors,
    };
  }

  /**
   * Recursively find all .md files
   */
  private async findPromptFiles(dir: string): Promise<string[]> {
    const files: string[] = [];

    try {
      const entries = await fs.readdir(dir, { withFileTypes: true });

      for (const entry of entries) {
        const fullPath = path.join(dir, entry.name);

        if (entry.isDirectory()) {
          const subFiles = await this.findPromptFiles(fullPath);
          files.push(...subFiles);
        } else if (entry.name.endsWith('.md') || entry.name.endsWith('.txt')) {
          files.push(fullPath);
        }
      }
    } catch (err) {
      console.warn(`⚠️ Could not read directory ${dir}:`, err);
    }

    return files;
  }

  /**
   * Create Neo4j indexes
   */
  private async createIndexes(session: Session): Promise<void> {
    const indexes = [
      'CREATE INDEX persona_name IF NOT EXISTS FOR (p:Persona) ON (p.name)',
      'CREATE INDEX directive_type IF NOT EXISTS FOR (d:Directive) ON (d.type)',
      'CREATE INDEX capability_name IF NOT EXISTS FOR (c:Capability) ON (c.name)',
    ];

    for (const idx of indexes) {
      try {
        await session.run(idx);
      } catch {
        // Index might already exist
      }
    }
  }

  /**
   * Process a single prompt file
   */
  private async processPromptFile(session: Session, filePath: string, isPremium: boolean = false): Promise<{
    personas: number;
    directives: number;
    capabilities: number;
    hotkeys: number;
  }> {
    const content = await fs.readFile(filePath, 'utf-8');
    const fileName = path.basename(filePath, path.extname(filePath));

    // Extract metadata from markdown
    const metadata = this.extractMetadata(content, fileName);

    // Extract the actual prompt content (inside code blocks)
    const promptContent = this.extractPromptContent(content);

    if (!promptContent) {
      return { personas: 0, directives: 0, capabilities: 0, hotkeys: 0 };
    }

    // Create Persona node
    const personaHash = crypto.createHash('md5').update(promptContent).digest('hex');

    // Determine vendor from path
    let vendor = 'Unknown';
    if (filePath.includes('/Anthropic/') || filePath.includes('claude')) vendor = 'Anthropic';
    else if (filePath.includes('/OpenAI/') || filePath.includes('gpt')) vendor = 'OpenAI';
    else if (filePath.includes('/Google/') || filePath.includes('gemini')) vendor = 'Google';
    else if (filePath.includes('/xAI/') || filePath.includes('grok')) vendor = 'xAI';
    else if (filePath.includes('/Perplexity/')) vendor = 'Perplexity';
    else if (filePath.includes('/Misc/')) vendor = 'Misc';

    await session.run(`
      MERGE (p:Persona {name: $name})
      ON CREATE SET
        p.source = $source,
        p.sourceUrl = $sourceUrl,
        p.author = $author,
        p.description = $description,
        p.hash = $hash,
        p.rawPrompt = $rawPrompt,
        p.vendor = $vendor,
        p.isPremium = $isPremium,
        p.createdAt = datetime()
      ON MATCH SET
        p.hash = $hash,
        p.rawPrompt = $rawPrompt,
        p.vendor = $vendor,
        p.isPremium = $isPremium,
        p.updatedAt = datetime()
    `, {
      name: metadata.name,
      source: filePath,
      sourceUrl: metadata.url || null,
      author: metadata.author || null,
      description: metadata.description || null,
      hash: personaHash,
      rawPrompt: promptContent.substring(0, 50000),
      vendor,
      isPremium,
    });

    // Extract and create Directives
    const extractedDirectives = this.extractDirectives(promptContent);
    let directiveCount = 0;

    for (const directive of extractedDirectives) {
      await session.run(`
        MATCH (p:Persona {name: $personaName})
        MERGE (d:Directive {id: $id})
        ON CREATE SET
          d.content = $content,
          d.type = $type,
          d.priority = $priority,
          d.createdAt = datetime()
        MERGE (p)-[:HAS_DIRECTIVE]->(d)
      `, {
        personaName: metadata.name,
        id: directive.id,
        content: directive.content,
        type: directive.type,
        priority: directive.priority,
      });
      directiveCount++;
    }

    // Extract and create Capabilities
    const extractedCapabilities = this.extractCapabilities(promptContent);
    let capabilityCount = 0;

    for (const capability of extractedCapabilities) {
      await session.run(`
        MATCH (p:Persona {name: $personaName})
        MERGE (c:Capability {name: $name})
        ON CREATE SET
          c.description = $description,
          c.createdAt = datetime()
        MERGE (p)-[:HAS_CAPABILITY]->(c)
      `, {
        personaName: metadata.name,
        name: capability.name,
        description: capability.description,
      });
      capabilityCount++;
    }

    // Extract Hotkeys
    const extractedHotkeys = this.extractHotkeys(promptContent);
    let hotkeyCount = 0;

    for (const hotkey of extractedHotkeys) {
      await session.run(`
        MATCH (p:Persona {name: $personaName})
        MERGE (h:Hotkey {key: $key, personaName: $personaName})
        ON CREATE SET
          h.action = $action,
          h.description = $description,
          h.createdAt = datetime()
        MERGE (p)-[:USES_HOTKEY]->(h)
      `, {
        personaName: metadata.name,
        key: hotkey.key,
        action: hotkey.action,
        description: hotkey.description,
      });
      hotkeyCount++;
    }

    console.log(`  ✓ ${metadata.name}: ${directiveCount} directives, ${capabilityCount} capabilities, ${hotkeyCount} hotkeys`);

    return {
      personas: 1,
      directives: directiveCount,
      capabilities: capabilityCount,
      hotkeys: hotkeyCount,
    };
  }

  /**
   * Extract metadata from markdown header
   */
  private extractMetadata(content: string, fileName: string): {
    name: string;
    description?: string;
    author?: string;
    url?: string;
  } {
    const lines = content.split('\n');
    let name = fileName;
    let description: string | undefined;
    let author: string | undefined;
    let url: string | undefined;

    for (const line of lines.slice(0, 15)) {
      // ## Title
      if (line.startsWith('## ') && !name) {
        name = line.replace('## ', '').trim();
      }
      // Description line (usually after title)
      else if (!description && line.trim() && !line.startsWith('#') && !line.startsWith('By ') && !line.startsWith('http')) {
        description = line.trim();
      }
      // By Author
      else if (line.startsWith('By ')) {
        author = line.replace('By ', '').trim();
      }
      // URL
      else if (line.includes('chat.openai.com/g/') || line.includes('chatgpt.com/g/')) {
        url = line.trim();
      }
    }

    return { name, description, author, url };
  }

  /**
   * Extract prompt content from code blocks
   */
  private extractPromptContent(content: string): string | null {
    // Match content inside ```markdown ... ``` or ``` ... ```
    const codeBlockMatch = content.match(/```(?:markdown)?\s*([\s\S]*?)```/);
    if (codeBlockMatch) {
      return codeBlockMatch[1].trim();
    }

    // If no code block, try to get content after metadata
    const lines = content.split('\n');
    const contentStart = lines.findIndex((l, i) => i > 5 && l.trim() && !l.startsWith('#') && !l.startsWith('By ') && !l.includes('http'));

    if (contentStart > 0) {
      return lines.slice(contentStart).join('\n').trim();
    }

    return null;
  }

  /**
   * Extract directives from prompt content
   */
  private extractDirectives(content: string): DirectiveNode[] {
    const directives: DirectiveNode[] = [];
    const sentences = content.split(/[.!?\n]/).filter(s => s.trim().length > 10);

    for (const sentence of sentences) {
      const trimmed = sentence.trim();

      // Determine directive type
      let type: DirectiveNode['type'] = 'rule';
      let priority = 5;

      if (DIRECTIVE_PATTERNS.restriction.test(trimmed)) {
        type = 'restriction';
        priority = 9; // High priority for restrictions
      } else if (DIRECTIVE_PATTERNS.capability.test(trimmed)) {
        type = 'capability';
        priority = 6;
      } else if (DIRECTIVE_PATTERNS.behavior.test(trimmed)) {
        type = 'behavior';
        priority = 7;
      } else if (DIRECTIVE_PATTERNS.tone.test(trimmed)) {
        type = 'tone';
        priority = 5;
      } else if (DIRECTIVE_PATTERNS.format.test(trimmed)) {
        type = 'format';
        priority = 4;
      } else if (DIRECTIVE_PATTERNS.rule.test(trimmed)) {
        type = 'rule';
        priority = 8;
      } else {
        continue; // Skip if no pattern matches
      }

      // Check for emphasis markers
      if (trimmed.toUpperCase() === trimmed || trimmed.includes('IMPORTANT') || trimmed.includes('MUST')) {
        priority = Math.min(10, priority + 2);
      }

      const id = crypto.createHash('md5').update(trimmed).digest('hex').substring(0, 12);

      directives.push({
        id,
        content: trimmed.substring(0, 500),
        type,
        priority,
      });
    }

    return directives;
  }

  /**
   * Extract capabilities from content
   */
  private extractCapabilities(content: string): CapabilityNode[] {
    const capabilities: CapabilityNode[] = [];
    const found = new Set<string>();
    const lowerContent = content.toLowerCase();

    for (const { keyword, capability } of CAPABILITY_KEYWORDS) {
      if (lowerContent.includes(keyword) && !found.has(capability)) {
        found.add(capability);
        capabilities.push({
          name: capability,
          description: `Detected via keyword: "${keyword}"`,
        });
      }
    }

    return capabilities;
  }

  /**
   * Extract hotkey patterns
   */
  private extractHotkeys(content: string): HotkeyPattern[] {
    const hotkeys: HotkeyPattern[] = [];

    // Match patterns like "W: action" or "- W: action"
    const hotkeyRegex = /[-•*]?\s*([A-Z]{1,3})\s*[::]\s*(.+?)(?:\n|$)/g;
    let match;

    while ((match = hotkeyRegex.exec(content)) !== null) {
      const key = match[1].trim();
      const description = match[2].trim();

      // Skip if too long (probably not a hotkey)
      if (description.length > 200) continue;

      hotkeys.push({
        key,
        action: key,
        description,
      });
    }

    return hotkeys;
  }

  /**
   * Create relationships between similar personas
   */
  private async createPersonaRelationships(session: Session): Promise<void> {
    // Connect personas with shared capabilities
    await session.run(`
      MATCH (p1:Persona)-[:HAS_CAPABILITY]->(c:Capability)<-[:HAS_CAPABILITY]-(p2:Persona)
      WHERE p1 <> p2
      MERGE (p1)-[:SHARES_CAPABILITY {capability: c.name}]->(p2)
    `);

    // Connect personas with similar directives (by type)
    await session.run(`
      MATCH (p1:Persona)-[:HAS_DIRECTIVE]->(d1:Directive)
      MATCH (p2:Persona)-[:HAS_DIRECTIVE]->(d2:Directive)
      WHERE p1 <> p2 AND d1.type = d2.type AND d1.priority >= 8 AND d2.priority >= 8
      WITH p1, p2, count(*) AS sharedCount
      WHERE sharedCount >= 3
      MERGE (p1)-[:SIMILAR_PERSONA {sharedDirectives: sharedCount}]->(p2)
    `);
  }

  /**
   * Get splice statistics
   */
  async getStats(): Promise<{
    totalPersonas: number;
    totalDirectives: number;
    totalCapabilities: number;
    topCapabilities: { name: string; count: number }[];
  }> {
    const session = this.driver.session();

    try {
      const result = await session.run(`
        MATCH (p:Persona) WITH count(p) AS personas
        MATCH (d:Directive) WITH personas, count(d) AS directives
        MATCH (c:Capability) WITH personas, directives, count(c) AS capabilities
        RETURN personas, directives, capabilities
      `);

      const topCaps = await session.run(`
        MATCH (p:Persona)-[:HAS_CAPABILITY]->(c:Capability)
        RETURN c.name AS name, count(p) AS count
        ORDER BY count DESC
        LIMIT 10
      `);

      const record = result.records[0];

      return {
        totalPersonas: record?.get('personas')?.toNumber() || 0,
        totalDirectives: record?.get('directives')?.toNumber() || 0,
        totalCapabilities: record?.get('capabilities')?.toNumber() || 0,
        topCapabilities: topCaps.records.map(r => ({
          name: r.get('name'),
          count: r.get('count').toNumber(),
        })),
      };
    } finally {
      await session.close();
    }
  }

  /**
   * Query personas by capability
   */
  async findPersonasByCapability(capability: string): Promise<string[]> {
    const session = this.driver.session();

    try {
      const result = await session.run(`
        MATCH (p:Persona)-[:HAS_CAPABILITY]->(c:Capability {name: $capability})
        RETURN p.name AS name, p.description AS description
      `, { capability });

      return result.records.map(r => r.get('name'));
    } finally {
      await session.close();
    }
  }

  /**
   * Get directives for a persona
   */
  async getPersonaDirectives(personaName: string): Promise<DirectiveNode[]> {
    const session = this.driver.session();

    try {
      const result = await session.run(`
        MATCH (p:Persona {name: $name})-[:HAS_DIRECTIVE]->(d:Directive)
        RETURN d.id AS id, d.content AS content, d.type AS type, d.priority AS priority
        ORDER BY d.priority DESC
      `, { name: personaName });

      return result.records.map(r => ({
        id: r.get('id'),
        content: r.get('content'),
        type: r.get('type'),
        priority: r.get('priority'),
      }));
    } finally {
      await session.close();
    }
  }

  async close(): Promise<void> {
    await this.driver.close();
  }
}

export default DNASplicer;