File size: 46,168 Bytes
7f7972d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
#!/usr/bin/env node
/**
 * Stack 2.9 - Enhanced Training Data Extractor
 * Extracts training examples from OpenClaw codebase
 * 
 * Features:
 * 1. Parse code patterns: function+comment pairs, error messages, test files
 * 2. Real conversation parsing (JSON, JSONL, Markdown formats)
 * 3. Synthetic examples (50+ per tool)
 * 4. JSONL output
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import os from 'os';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Paths
const SRC_DIR = path.join(__dirname, 'src');
const OUTPUT_DIR = path.join(__dirname, 'training-data');
const SYNTHETIC_DIR = path.join(OUTPUT_DIR, 'synthetic');
const TOOLS_SCHEMA_DIR = path.join(OUTPUT_DIR, 'tools');
const CODE_PAIRS_DIR = path.join(OUTPUT_DIR, 'code-pairs');
const CONVERSATIONS_DIR = path.join(OUTPUT_DIR, 'conversations');

// Ensure directories exist
for (const dir of [OUTPUT_DIR, SYNTHETIC_DIR, TOOLS_SCHEMA_DIR, CODE_PAIRS_DIR, CONVERSATIONS_DIR]) {
  fs.mkdirSync(dir, { recursive: true });
}

// ============================================================================
// 1. EXTRACT TOOL SCHEMAS FROM src/tools/
// ============================================================================

function extractToolSchemas() {
  const toolsDir = path.join(SRC_DIR, 'tools');
  if (!fs.existsSync(toolsDir)) {
    console.log('⚠️  Tools directory not found, skipping...');
    return [];
  }

  const schemas = [];
  const toolDirs = fs.readdirSync(toolsDir).filter(name => {
    const stat = fs.statSync(path.join(toolsDir, name));
    return stat.isDirectory();
  });

  for (const toolDir of toolDirs) {
    const toolPath = path.join(toolsDir, toolDir);
    const promptFile = path.join(toolPath, 'prompt.ts');
    const toolFile = path.join(toolPath, toolDir + '.tsx') || path.join(toolPath, toolDir + '.ts');

    if (fs.existsSync(promptFile) || fs.existsSync(toolFile)) {
      try {
        const promptContent = fs.existsSync(promptFile) ? fs.readFileSync(promptFile, 'utf-8') : '';
        const toolContent = fs.existsSync(toolFile) ? fs.readFileSync(toolFile, 'utf-8') : '';

        // Extract tool description from JSDoc
        const descMatch = promptContent.match(/\/\*\*([\s\S]*?)\*\//);
        let description = '';
        if (descMatch) {
          description = descMatch[1]
            .replace(/^\s*\* ?/gm, '')
            .replace(/^\s*\*/g, '')
            .replace(/\*\/$/, '')
            .trim()
            .substring(0, 300);
        }

        // Extract input interface from tool file
        let inputSchema = {};
        const interfaceMatch = toolContent.match(/interface\s+(\w+Input\w*)\s*\{([\s\S]*?)\}/);
        if (interfaceMatch) {
          const fields = interfaceMatch[2].match(/(\w+)(\??):\s*([^;]+);/g) || [];
          for (const field of fields) {
            const match = field.match(/(\w+)(\??):\s*([^;]+);/);
            if (match) {
              inputSchema[match[1]] = { type: match[3].trim(), optional: match[2] === '?' };
            }
          }
        }

        schemas.push({
          tool: toolDir,
          description,
          hasPrompt: !!promptContent,
          hasImplementation: !!toolContent,
          inputSchema
        });
      } catch (e) {
        console.log(`⚠️  Error parsing ${toolDir}: ${e.message}`);
      }
    }
  }

  // Write tools catalog
  fs.writeFileSync(
    path.join(TOOLS_SCHEMA_DIR, 'catalog.json'),
    JSON.stringify(schemas, null, 2)
  );

  console.log(`βœ… Extracted ${schemas.length} tool schemas`);
  return schemas;
}

// ============================================================================
// 2. EXTRACT CODE-COMMENT PAIRS FROM src/
// ============================================================================

function extractCodeCommentPairs() {
  console.log('πŸ” Extracting code-comment pairs...');
  const pairs = [];
  
  // Patterns for JSDoc comments
  const jsdocPattern = /\/\*\*([\s\S]*?)\*\/\s*\n(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*(?::\s*([^{]+))?\{([\s\S]*?)\n\}/g;
  const methodPattern = /\/\*\*([\s\S]*?)\*\/\s*\n\s*(?:async\s+)?(\w+)\s*\([^)]*\)[^:]*\{([\s\S]*?)\n\s*\}/g;
  
  // Error message patterns
  const errorPattern = /(?:throw\s+new\s+Error|logger\.error|console\.error)\s*\(\s*[`"']([^`'"]+)[`'"]/g;
  const errorClassPattern = /class\s+(\w+Error\w*)\s+extends\s+Error\s*\{([^}]*)\}/g;

  function processFile(filePath) {
    try {
      const content = fs.readFileSync(filePath, 'utf-8');
      const relativePath = path.relative(SRC_DIR, filePath);
      
      // Skip test files and mock files for now
      if (filePath.includes('__tests__') || filePath.includes('mocks')) return;

      // Extract function + JSDoc pairs
      let match;
      const funcRegex = /\/\*\*([\s\S]*?)\*\/\s*\n\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(([^)]*)\)\s*(?::\s*([^;\n]+))?[^{]*\{([\s\S]*?)\n\}/g;
      
      while ((match = funcRegex.exec(content)) !== null) {
        const jsdoc = match[1].replace(/^\s*\*\s*/gm, '').trim();
        const funcName = match[2];
        const params = match[3].trim();
        const returnType = match[4]?.trim() || 'void';
        const body = match[5].trim();
        
        // Only include if meaningful (not too short, has actual logic)
        if (body.length > 50 && jsdoc.length > 10) {
          pairs.push({
            type: 'function',
            name: funcName,
            path: relativePath,
            code: `function ${funcName}(${params})${returnType ? `: ${returnType}` : ''} { ... }`,
            fullBody: body.substring(0, 500),
            comment: jsdoc.substring(0, 300),
            commentType: 'jsdoc'
          });
        }
      }

      // Extract error messages and patterns
      const errorRegex = /(?:throw\s+new\s+Error|logger\.error|console\.error)\s*\(\s*[`"']([^`'"]+)[`'"]/g;
      let errorMatch;
      while ((errorMatch = errorRegex.exec(content)) !== null) {
        const errorMsg = errorMatch[1];
        // Categorize error type
        let category = 'general';
        if (errorMsg.includes('not found') || errorMsg.includes('No such')) category = 'not_found';
        else if (errorMsg.includes('permission') || errorMsg.includes('denied')) category = 'permission';
        else if (errorMsg.includes('invalid') || errorMsg.includes('malformed')) category = 'validation';
        else if (errorMsg.includes('timeout')) category = 'timeout';
        else if (errorMsg.includes('already')) category = 'conflict';
        
        pairs.push({
          type: 'error_message',
          path: relativePath,
          message: errorMsg,
          category,
          fixSuggestion: generateFixSuggestion(errorMsg, category)
        });
      }

      // Extract class with error handling
      const classRegex = /class\s+(\w+)\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/g;
      let classMatch;
      while ((classMatch = classRegex.exec(content)) !== null) {
        const className = match[1];
        const classBody = match[2];
        // Look for try-catch patterns
        if (classBody.includes('try') && classBody.includes('catch')) {
          pairs.push({
            type: 'error_handling_class',
            name: className,
            path: relativePath,
            pattern: 'try-catch',
            example: classBody.substring(0, 400)
          });
        }
      }

    } catch (e) {
      // Skip files that can't be read
    }
  }

  function walkDir(dir, extensions = ['.ts', '.tsx']) {
    if (!fs.existsSync(dir)) return;
    
    const entries = fs.readdirSync(dir, { withFileTypes: true });
    for (const entry of entries) {
      const fullPath = path.join(dir, entry.name);
      if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
        walkDir(fullPath, extensions);
      } else if (entry.isFile() && extensions.some(ext => entry.name.endsWith(ext))) {
        processFile(fullPath);
      }
    }
  }

  walkDir(SRC_DIR);

  // Save code-comment pairs
  fs.writeFileSync(
    path.join(CODE_PAIRS_DIR, 'pairs.json'),
    JSON.stringify(pairs, null, 2)
  );
  
  console.log(`βœ… Extracted ${pairs.length} code-comment pairs`);
  return pairs;
}

function generateFixSuggestion(message, category) {
  const suggestions = {
    not_found: 'Check if the resource exists or provide the correct path',
    permission: 'Ensure you have the necessary permissions for this operation',
    validation: 'Verify the input format and required fields',
    timeout: 'Increase timeout duration or check network connectivity',
    conflict: 'Check if the resource already exists or needs to be deleted first',
    general: 'Review the error message and correct the underlying issue'
  };
  return suggestions[category] || suggestions.general;
}

// ============================================================================
// 3. PARSE TEST FILES FOR TEST-GENERATION EXAMPLES
// ============================================================================

function extractTestExamples() {
  console.log('πŸ§ͺ Extracting test examples...');
  const testExamples = [];
  
  const testPattern = /describe\s*\(\s*['"]([^'"]+)['"](?:\s*,\s*)?\(\s*\)\s*=>\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}\s*\)/g;
  const itPattern = /it\s*\(\s*['"]([^'"]+)['"](?:\s*,\s*)?(?:async\s+)?\(\s*\)\s*(?:=>\s*)?\{([\s\S]*?)\n\s*\}/g;
  const expectPattern = /expect\s*\(([^)]+)\)\.(to[^;(]+)\s*\(([^)]+)\)/g;
  
  function processTestFile(filePath) {
    try {
      const content = fs.readFileSync(filePath, 'utf-8');
      const relativePath = path.relative(SRC_DIR, filePath);
      
      let match;
      while ((match = testPattern.exec(content)) !== null) {
        const testSuite = match[1];
        const testBody = match[2];
        
        // Extract individual it() blocks
        const itRegex = /it\s*\(\s*['"]([^'"]+)['"](?:\s*,\s*)?(?:async\s+)?\(\s*\)\s*(?:=>\s*)?\{([\s\S]*?)\n\s*\}/g;
        let itMatch;
        
        while ((itMatch = itRegex.exec(testBody)) !== null) {
          const testName = itMatch[1];
          const testCode = itMatch[2].trim();
          
          // Extract assertions
          const assertions = [];
          const expectRegex = /expect\s*\(([^)]+)\)\.(\w+)\s*\(([^)]*)\)/g;
          let expectMatch;
          
          while ((expectMatch = expectRegex.exec(testCode)) !== null) {
            assertions.push({
              actual: expectMatch[1],
              matcher: expectMatch[2],
              expected: expectMatch[3]
            });
          }
          
          if (assertions.length > 0) {
            testExamples.push({
              type: 'test_example',
              suite: testSuite,
              name: testName,
              path: relativePath,
              code: testCode.substring(0, 400),
              assertions,
              isAsync: testCode.includes('await')
            });
          }
        }
      }
    } catch (e) {
      // Skip files that can't be read
    }
  }
  
  function walkDir(dir) {
    if (!fs.existsSync(dir)) return;
    
    const entries = fs.readdirSync(dir, { withFileTypes: true });
    for (const entry of entries) {
      const fullPath = path.join(dir, entry.name);
      if (entry.isDirectory()) {
        walkDir(fullPath);
      } else if (entry.isFile() && (entry.name.endsWith('.test.ts') || entry.name.endsWith('.test.tsx'))) {
        processTestFile(fullPath);
      }
    }
  }
  
  // Look for test files in __tests__ directories
  walkDir(SRC_DIR);
  
  // Save test examples
  fs.writeFileSync(
    path.join(CODE_PAIRS_DIR, 'test-examples.json'),
    JSON.stringify(testExamples, null, 2)
  );
  
  console.log(`βœ… Extracted ${testExamples.length} test examples`);
  return testExamples;
}

// ============================================================================
// 4. PARSE REAL CONVERSATIONS FROM SESSION LOGS
// ============================================================================

function parseConversations() {
  console.log('πŸ’¬ Parsing conversations from session logs...');
  const conversations = [];
  
  // Common session log locations
  const sessionLogPaths = [
    path.join(os.homedir(), '.claude', 'sessions'),
    path.join(os.homedir(), '.openclaw', 'sessions'),
    path.join(os.homedir(), '.claude', 'conversations'),
    path.join(os.homedir(), '.openclaw', 'conversations'),
    path.join(os.homedir(), '.config', 'claude', 'sessions')
  ];
  
  function parseJsonFormat(content, source) {
    try {
      const data = JSON.parse(content);
      if (data.messages && Array.isArray(data.messages)) {
        return {
          format: 'json',
          source,
          messages: data.messages,
          metadata: data.metadata || {}
        };
      }
      if (data.conversation && data.conversation.messages) {
        return {
          format: 'json',
          source,
          messages: data.conversation.messages,
          metadata: data.metadata || {}
        };
      }
    } catch (e) {}
    return null;
  }
  
  function parseJsonlFormat(content, source) {
    const lines = content.trim().split('\n');
    const conversations = [];
    
    for (const line of lines) {
      try {
        const obj = JSON.parse(line);
        if (obj.messages || obj.conversation) {
          conversations.push({
            format: 'jsonl',
            source,
            messages: obj.messages || obj.conversation?.messages || [],
            metadata: obj.metadata || {}
          });
        }
      } catch (e) {}
    }
    
    return conversations;
  }
  
  function parseMarkdownFormat(content, source) {
    const messages = [];
    const blocks = content.split(/(?=^##?\s+(?:User|Assistant|System|Human|AI))/m);
    
    let currentRole = null;
    let currentContent = [];
    
    for (const block of blocks) {
      const roleMatch = block.match(/^##?\s+(User|Assistant|System|Human|AI|Assistant \(tool\))/im);
      if (roleMatch) {
        if (currentRole && currentContent.length > 0) {
          messages.push({
            role: currentRole,
            content: currentContent.join('\n').trim()
          });
        }
        currentRole = roleMatch[1].toLowerCase().replace('assistant (tool)', 'tool');
        currentContent = [block.replace(/^##?\s+.*$/m, '').trim()];
      } else if (currentRole) {
        currentContent.push(block.trim());
      }
    }
    
    if (currentRole && currentContent.length > 0) {
      messages.push({
        role: currentRole,
        content: currentContent.join('\n').trim()
      });
    }
    
    if (messages.length > 0) {
      return {
        format: 'markdown',
        source,
        messages,
        metadata: {}
      };
    }
    return null;
  }
  
  function processLogFile(filePath) {
    try {
      const content = fs.readFileSync(filePath, 'utf-8');
      const source = path.relative(os.homedir(), filePath);
      
      // Try JSON format
      if (filePath.endsWith('.json')) {
        const parsed = parseJsonFormat(content, source);
        if (parsed) {
          conversations.push(parsed);
          return;
        }
      }
      
      // Try JSONL format
      if (filePath.endsWith('.jsonl')) {
        const parsed = parseJsonlFormat(content, source);
        conversations.push(...parsed);
        return;
      }
      
      // Try Markdown format
      if (filePath.endsWith('.md') || filePath.endsWith('.mdx')) {
        const parsed = parseMarkdownFormat(content, source);
        if (parsed) {
          conversations.push(parsed);
        }
      }
    } catch (e) {
      // Skip files that can't be read
    }
  }
  
  function walkDir(dir) {
    if (!fs.existsSync(dir)) return;
    
    try {
      const entries = fs.readdirSync(dir, { withFileTypes: true });
      for (const entry of entries) {
        const fullPath = path.join(dir, entry.name);
        if (entry.isDirectory()) {
          walkDir(fullPath);
        } else if (entry.isFile() && 
                   (entry.name.endsWith('.json') || 
                    entry.name.endsWith('.jsonl') || 
                    entry.name.endsWith('.md') ||
                    entry.name.endsWith('.mdx'))) {
          processLogFile(fullPath);
        }
      }
    } catch (e) {
      // Skip directories that can't be accessed
    }
  }
  
  // Try all known session log locations
  for (const logPath of sessionLogPaths) {
    walkDir(logPath);
  }
  
  // Save parsed conversations
  fs.writeFileSync(
    path.join(CONVERSATIONS_DIR, 'parsed.json'),
    JSON.stringify(conversations, null, 2)
  );
  
  console.log(`βœ… Parsed ${conversations.length} conversations`);
  return conversations;
}

// ============================================================================
// 5. GENERATE SYNTHETIC EXAMPLES (50+ per tool)
// ============================================================================

function generateSyntheticExamples(tools) {
  console.log('🎭 Generating synthetic examples...');
  const examples = [];
  
  // Base system prompts
  const systemPrompts = [
    'You are Stack, an AI coding assistant. You help with programming tasks, answer questions, and use tools when needed.',
    'You are Stack, an expert programmer specializing in TypeScript, React, and Node.js.',
    'You are Stack, a helpful AI assistant that can read and write files, run commands, and search code.',
    'You are Stack, an AI coding assistant with access to file system tools, search tools, and more.'
  ];

  // =========================================================================
  // EXAMPLES: Basic coding questions and answers
  // =========================================================================
  
  const basicQAPairs = [
    {
      q: 'How do I read a file in Node.js?',
      a: 'You can use the `fs` module. Here are the main approaches:\n\n1. Synchronous (simple but blocks):\n```js\nconst fs = require(\'fs\');\nconst content = fs.readFileSync(\'file.txt\', \'utf8\');\n```\n\n2. Async with callbacks:\n```js\nfs.readFile(\'file.txt\', \'utf8\', (err, data) => {\n  if (err) throw err;\n  console.log(data);\n});\n```\n\n3. Promise-based (recommended):\n```js\nconst fs = require(\'fs\').promises;\nconst data = await fs.readFile(\'file.txt\', \'utf8\');\n```'
    },
    {
      q: 'How do I write to a file in Node.js?',
      a: 'Use the `fs` module:\n\n1. Sync (blocks):\n```js\nfs.writeFileSync(\'output.txt\', \'Hello!\', \'utf8\');\n```\n\n2. Async with promises:\n```js\nawait fs.writeFile(\'output.txt\', \'Hello!\', \'utf8\');\n```\n\n3. Append to existing:\n```js\nawait fs.appendFile(\'log.txt\', \'new entry\\n\', \'utf8\');\n```'
    },
    {
      q: 'How do I list files in a directory?',
      a: 'Use `fs.readdir()`:\n\n```js\nconst files = await fs.readdir(\'./src\', { withFileTypes: true });\n\nfor (const file of files) {\n  if (file.isDirectory()) {\n    console.log(`πŸ“ ${file.name}/`);\n  } else {\n    console.log(`πŸ“„ ${file.name}`);\n  }\n}\n```'
    },
    {
      q: 'What is the difference between let, const, and var?',
      a: 'Key differences:\n\n**const** - Block-scoped, cannot be reassigned (but object properties can change)\n**let** - Block-scoped, can be reassigned\n**var** - Function-scoped (or global), hoisted (avoid in modern JS)\n\n```js\nconst PI = 3.14;        // Cannot reassign\nlet count = 0;          // Can reassign\ncount = count + 1;      // βœ“\nvar oldWay = \'hoisted\'; // Avoid - function scoped\n```'
    },
    {
      q: 'How do I use async/await properly?',
      a: 'Async/await is syntactic sugar over promises:\n\n```js\n// Basic usage\nasync function fetchData() {\n  try {\n    const response = await fetch(url);\n    const data = await response.json();\n    return data;\n  } catch (error) {\n    console.error(\'Fetch failed:\', error);\n    throw error;\n  }\n}\n\n// Parallel execution\nconst [users, posts] = await Promise.all([\n  fetch(\'/api/users\').then(r => r.json()),\n  fetch(\'/api/posts\').then(r => r.json())\n]);\n```'
    }
  ];

  for (const pair of basicQAPairs) {
    examples.push({
      messages: [
        { role: 'system', content: systemPrompts[0] },
        { role: 'user', content: pair.q },
        { role: 'assistant', content: pair.a }
      ]
    });
  }

  // =========================================================================
  // EXAMPLES: Tool use patterns (per tool)
  // =========================================================================
  
  const toolUsePatterns = [
    {
      tool: 'FileReadTool',
      scenarios: [
        {
          user: 'Show me the contents of package.json',
          assistant: 'I\'ll read the package.json file for you.',
          toolName: 'FileReadTool',
          toolInput: { file_path: 'package.json' },
          resultContent: '{\n  "name": "my-project",\n  "version": "1.0.0",\n  "scripts": {\n    "dev": "next dev",\n    "build": "next build"\n  }\n}',
          summary: 'Here\'s what\'s in your package.json:\n\n- **Project name**: my-project\n- **Version**: 1.0.0\n- **Scripts**: dev (next dev), build (next build)'
        },
        {
          user: 'Read the config.ts file from the src directory',
          assistant: 'Let me read that config file.',
          toolName: 'FileReadTool',
          toolInput: { file_path: 'src/config.ts' },
          resultContent: 'export const config = {\n  apiUrl: \'https://api.example.com\',\n  timeout: 5000,\n  retries: 3\n};',
          summary: 'The config file exports a configuration object with:\n\n- **API URL**: https://api.example.com\n- **Timeout**: 5000ms\n- **Retries**: 3'
        },
        {
          user: 'What\'s in the README.md?',
          assistant: 'Reading the README...',
          toolName: 'FileReadTool',
          toolInput: { file_path: 'README.md' },
          resultContent: '# My Project\n\nA brief description of the project.\n\n## Installation\n\nnpm install\n\n## Usage\n\nnpm start',
          summary: 'The README contains:\n\n- Project title and description\n- Installation instructions\n- Usage guide'
        }
      ]
    },
    {
      tool: 'GlobTool',
      scenarios: [
        {
          user: 'Find all TypeScript files in src',
          assistant: 'Searching for TypeScript files...',
          toolName: 'GlobTool',
          toolInput: { pattern: 'src/**/*.ts' },
          resultContent: 'src/index.ts\nsrc/utils.ts\nsrc/components/Button.tsx\nsrc/services/api.ts',
          summary: 'Found 4 TypeScript files in src/'
        },
        {
          user: 'List all test files',
          assistant: 'Looking for test files...',
          toolName: 'GlobTool',
          toolInput: { pattern: '**/*.test.{ts,tsx}' },
          resultContent: '__tests__/sample.test.ts\nsrc/components/__tests__/Button.test.tsx\nsrc/utils/__tests__/helpers.test.ts',
          summary: 'Found 3 test files'
        },
        {
          user: 'Find all JSON config files',
          assistant: 'Searching for JSON config files...',
          toolName: 'GlobTool',
          toolInput: { pattern: '**/*.json' },
          resultContent: 'package.json\ntsconfig.json\njest.config.json\nsrc/config.json',
          summary: 'Found 4 JSON files'
        }
      ]
    },
    {
      tool: 'GrepTool',
      scenarios: [
        {
          user: 'Find all uses of "TODO" in the code',
          assistant: 'Searching for TODO comments...',
          toolName: 'GrepTool',
          toolInput: { pattern: 'TODO', path: '.' },
          resultContent: 'src/utils.ts: // TODO: implement error handling\nsrc/components/Form.tsx: // TODO: add validation\nsrc/api/client.ts: // TODO: add retry logic',
          summary: 'Found 3 TODO comments in:\n- src/utils.ts\n- src/components/Form.tsx\n- src/api/client.ts'
        },
        {
          user: 'Search for "async function" in the src',
          assistant: 'Finding async functions...',
          toolName: 'GrepTool',
          toolInput: { pattern: 'async function', path: 'src' },
          resultContent: 'src/api/client.ts: async function fetchData() {\nsrc/utils/parser.ts: async function parseJSON() {',
          summary: 'Found 2 async functions'
        }
      ]
    },
    {
      tool: 'BashTool',
      scenarios: [
        {
          user: 'Run npm install',
          assistant: 'Installing dependencies...',
          toolName: 'BashTool',
          toolInput: { command: 'npm install' },
          resultContent: 'added 245 packages in 12s\n\n12 packages are looking for funding\n  run `npm fund` for details',
          summary: 'Successfully installed 245 packages'
        },
        {
          user: 'Check git status',
          assistant: 'Checking git status...',
          toolName: 'BashTool',
          toolInput: { command: 'git status' },
          resultContent: 'On branch main\nChanges not staged for commit:\n  modified:   src/index.ts\n  modified:   package.json',
          summary: 'On branch main with 2 modified files'
        },
        {
          user: 'Run the tests',
          assistant: 'Running tests...',
          toolName: 'BashTool',
          toolInput: { command: 'npm test' },
          resultContent: 'PASS  __tests__/sample.test.ts\nPASS  src/components/__tests__/Button.test.tsx\n\nTest Suites: 2 passed, 2 total\nTests:       5 passed, 5 total',
          summary: 'All tests passed! 2 test suites, 5 tests total'
        }
      ]
    },
    {
      tool: 'FileWriteTool',
      scenarios: [
        {
          user: 'Create a new config file',
          assistant: 'Creating the config file...',
          toolName: 'FileWriteTool',
          toolInput: { file_path: 'config/local.json', content: '{\n  "name": "local-config",\n  "debug": true\n}' },
          resultContent: '',
          summary: 'Created config/local.json'
        },
        {
          user: 'Write a simple test file',
          assistant: 'Writing the test file...',
          toolName: 'FileWriteTool',
          toolInput: { file_path: 'test/example.test.ts', content: 'describe(\'Example\', () => {\n  it(\'should pass\', () => {\n    expect(1 + 1).toBe(2);\n  });\n});' },
          resultContent: '',
          summary: 'Created test/example.test.ts'
        }
      ]
    },
    {
      tool: 'FileEditTool',
      scenarios: [
        {
          user: 'Add a new export to the index file',
          assistant: 'Editing the index file...',
          toolName: 'FileEditTool',
          toolInput: { 
            file_path: 'src/index.ts',
            old_string: 'export { Config } from \'./config\';',
            new_string: 'export { Config } from \'./config\';\nexport { Logger } from \'./logger\';'
          },
          resultContent: '',
          summary: 'Added Logger export to src/index.ts'
        }
      ]
    },
    {
      tool: 'WebSearchTool',
      scenarios: [
        {
          user: 'Search for the latest React documentation',
          assistant: 'Searching the web...',
          toolName: 'WebSearchTool',
          toolInput: { query: 'React 18 documentation' },
          resultContent: 'Found results for React documentation...',
          summary: 'Found relevant documentation resources'
        }
      ]
    },
    {
      tool: 'WebFetchTool',
      scenarios: [
        {
          user: 'Fetch the content of a GitHub README',
          assistant: 'Fetching the README...',
          toolName: 'WebFetchTool',
          toolInput: { url: 'https://github.com/facebook/react' },
          resultContent: 'README content...',
          summary: 'Successfully fetched the README'
        }
      ]
    }
  ];

  // Generate tool use examples
  for (const toolPattern of toolUsePatterns) {
    for (const scenario of toolPattern.scenarios) {
      examples.push({
        messages: [
          { role: 'system', content: systemPrompts[0] },
          { role: 'user', content: scenario.user },
          { role: 'assistant', content: scenario.assistant, tool_use: { name: scenario.toolName, input: scenario.toolInput } },
          { role: 'user', content: '', tool_result: { tool_use_id: 'tool_1', content: scenario.resultContent } },
          { role: 'assistant', content: scenario.summary }
        ]
      });
    }
  }

  // =========================================================================
  // EXAMPLES: Error handling patterns
  // =========================================================================
  
  const errorHandlingExamples = [
    {
      user: 'Why did my file write fail?',
      context: 'Error: ENOENT: no such file or directory, open \'missing/dir/file.txt\'',
      response: 'The error indicates the directory doesn\'t exist. You need to create the parent directory first:\n\n```js\nimport fs from \'fs\'.promises;\nimport path from \'path\';\n\nasync function ensureDir(dirPath) {\n  await fs.mkdir(path.dirname(dirPath), { recursive: true });\n}\n\n// Then write\nawait ensureDir(\'missing/dir/file.txt\');\nawait fs.writeFile(\'missing/dir/file.txt\', \'content\');\n```\n\nOr use `fs.mkdirSync(path.dirname(filePath), { recursive: true })` synchronously.'
    },
    {
      user: 'My async function is throwing an error',
      context: 'Error: Cannot read property \'map\' of undefined',
      response: 'This usually means the data you\'re trying to map over isn\'t defined yet. Here\'s how to handle it:\n\n```js\nasync function getItems() {\n  try {\n    const data = await fetchData();\n    // Check before using\n    return data?.items?.map(item => item.id) || [];\n  } catch (error) {\n    console.error(\'Failed to fetch:\', error);\n    return []; // Return fallback\n  }\n}\n```\n\nUse optional chaining (`?.`) to safely access nested properties.'
    }
  ];

  for (const error of errorHandlingExamples) {
    examples.push({
      messages: [
        { role: 'system', content: systemPrompts[0] },
        { role: 'user', content: error.user + '\n\nError context: ' + error.context },
        { role: 'assistant', content: error.response }
      ]
    });
  }

  // =========================================================================
  // EXAMPLES: Multi-step workflows
  // =========================================================================
  
  const multiStepWorkflows = [
    {
      name: 'Create and test a new component',
      steps: [
        { role: 'user', content: 'Create a new React component called UserCard' },
        { role: 'assistant', content: 'I\'ll create the UserCard component for you.', tool_use: { name: 'FileWriteTool', input: { file_path: 'src/components/UserCard.tsx', content: 'import React from \'react\';\n\ninterface UserCardProps {\n  name: string;\n  email: string;\n}\n\nexport const UserCard: React.FC<UserCardProps> = ({ name, email }) => {\n  return (\n    <div className="user-card">\n      <h3>{name}</h3>\n      <p>{email}</p>\n    </div>\n  );\n};' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_1', content: 'File created successfully' } },
        { role: 'assistant', content: 'Created the component. Now let me write a test for it.', tool_use: { name: 'FileWriteTool', input: { file_path: 'src/components/__tests__/UserCard.test.tsx', content: 'import React from \'react\';\nimport { render, screen } from \'@testing-library/react\';\nimport { UserCard } from \'../UserCard\';\n\ndescribe(\'UserCard\', () => {\n  it(\'renders user name and email\', () => {\n    render(<UserCard name="John" email="john@example.com" />);\n    expect(screen.getByText(\'John\')).toBeInTheDocument();\n    expect(screen.getByText(\'john@example.com\')).toBeInTheDocument();\n  });\n});' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_2', content: 'Test file created' } },
        { role: 'assistant', content: 'Now let me run the tests to verify it works.', tool_use: { name: 'BashTool', input: { command: 'npm test -- --testPathPattern=UserCard' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_3', content: 'PASS src/components/__tests__/UserCard.test.tsx\n\n  UserCard\n    βœ“ renders user name and email' } },
        { role: 'assistant', content: 'All tests pass! The UserCard component is ready to use.\n\n```tsx\nimport { UserCard } from \'./components/UserCard\';\n\n<UserCard name="John" email="john@example.com" />\n```' }
      ]
    },
    {
      name: 'Debug and fix a failing test',
      steps: [
        { role: 'user', content: 'Run the tests - I think something is broken' },
        { role: 'assistant', content: 'Running tests...', tool_use: { name: 'BashTool', input: { command: 'npm test 2>&1' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_1', content: 'FAIL src/utils/__tests__/format.test.ts\n\n  Format Date\n    βœ• should format date as YYYY-MM-DD (5ms)\n\n    expect(received).toBe(expected)\n\n    Expected: "2024-01-15"\n    Received: "15-01-2024"' } },
        { role: 'assistant', content: 'I see the test is failing. Let me look at the formatDate function to understand the issue.', tool_use: { name: 'GrepTool', input: { pattern: 'formatDate', path: 'src/utils' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_2', content: 'src/utils/format.ts:export function formatDate(date: Date): string {\n  const d = new Date(date);\n  return `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear()}`;\n}' } },
        { role: 'assistant', content: 'Found it! The function returns DD-MM-YYYY but the test expects YYYY-MM-DD. Let me fix it.', tool_use: { name: 'FileEditTool', input: { file_path: 'src/utils/format.ts', old_string: 'return `${d.getDate()}-${d.getMonth() + 1}-${d.getFullYear()}`;', new_string: 'return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, \'0\')}-${String(d.getDate()).padStart(2, \'0\')}`;' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_3', content: 'File updated' } },
        { role: 'assistant', content: 'Fixed the format. Let me run the tests again to confirm.', tool_use: { name: 'BashTool', input: { command: 'npm test -- --testPathPattern=format.test' } } },
        { role: 'user', content: '', tool_result: { tool_use_id: 'tool_4', content: 'PASS src/utils/__tests__/format.test.ts\n\n  Format Date\n    βœ“ should format date as YYYY-MM-DD' } },
        { role: 'assistant', content: 'All tests pass now! The date is correctly formatted as YYYY-MM-DD.' }
      ]
    }
  ];

  for (const workflow of multiStepWorkflows) {
    examples.push({
      messages: [
        { role: 'system', content: systemPrompts[2] },
        ...workflow.steps
      ]
    });
  }

  // =========================================================================
  // EXAMPLES: Edge cases
  // =========================================================================
  
  const edgeCases = [
    {
      user: 'Handle an empty array in my reduce function',
      context: 'My reduce throws when the array is empty',
      response: 'Here are ways to handle empty arrays with reduce:\n\n```js\n// 1. Provide initial value\nconst sum = [].reduce((acc, val) => acc + val, 0); // Returns 0\n\n// 2. Check first\nif (arr.length === 0) return defaultValue;\n\n// 3. Use reduce with optional initial value\nfunction safeReduce(arr, reducer, initial) {\n  if (arr.length === 0 && initial === undefined) {\n    throw new Error(\'Cannot reduce empty array without initial value\');\n  }\n  return arr.reduce(reducer, initial);\n}\n```'
    },
    {
      user: 'Deal with optional function parameters',
      context: 'How do I make some parameters optional?',
      response: 'You can use optional parameters in several ways:\n\n```ts\n// TypeScript optional parameter\nfunction greet(name: string, greeting?: string) {\n  return `${greeting || \'Hello\'}, ${name}!`;\n}\n\n// Default parameter\nfunction createUser(name: string, role = \'user\') {\n  return { name, role };\n}\n\n// Nullish coalescing for defaults\nfunction process(value: string | null) {\n  const input = value ?? \'default\';\n}\n```'
    },
    {
      user: 'Handle API rate limiting',
      context: 'Getting 429 errors from the API',
      response: 'Here\'s a pattern for handling rate limits:\n\n```js\nasync function fetchWithRetry(url, retries = 3) {\n  for (let i = 0; i < retries; i++) {\n    const response = await fetch(url);\n    \n    if (response.status === 429) {\n      const retryAfter = response.headers.get(\'Retry-After\') || 60;\n      console.log(`Rate limited. Waiting ${retryAfter}s...`);\n      await new Promise(r => setTimeout(r, retryAfter * 1000));\n      continue;\n    }\n    \n    return response.json();\n  }\n  throw new Error(\'Max retries exceeded\');\n}\n```\n\nUse exponential backoff for more aggressive retrying.'
    }
  ];

  for (const edge of edgeCases) {
    examples.push({
      messages: [
        { role: 'system', content: systemPrompts[1] },
        { role: 'user', content: edge.user + '\n\nContext: ' + edge.context },
        { role: 'assistant', content: edge.response }
      ]
    });
  }

  // =========================================================================
  // GENERATE 50+ EXAMPLES PER TOOL (tool-specific variations)
  // =========================================================================
  
  const toolNames = tools.map(t => t.tool);
  const variationsPerTool = {
    FileReadTool: [
      'Read the first 100 lines of a large log file',
      'Show me the contents of .env.example',
      'What\'s in the tsconfig.json?',
      'Read the package-lock.json to check versions',
      'Show me the gitignore file'
    ],
    FileWriteTool: [
      'Create a .gitignore file with common ignores',
      'Write a new entry to the changelog',
      'Create a simple JSON config file',
      'Write the test results to output.txt'
    ],
    GlobTool: [
      'Find all .test.ts files',
      'List all files in src/ directory',
      'Find all files with "helper" in the name',
      'Search for *.config.js files',
      'Find all files in any __tests__ directory'
    ],
    GrepTool: [
      'Find all console.log statements',
      'Search for "export default"',
      'Find all imports from "react"',
      'Search for password or secret patterns',
      'Find all unused imports'
    ],
    BashTool: [
      'Initialize a new git repository',
      'Show the last 10 commits',
      'List all npm scripts available',
      'Check the current directory',
      'Show the difference between branches'
    ]
  };

  // Generate 50+ examples by varying prompts for each tool
  let exampleCount = examples.length;
  
  for (const tool of tools) {
    const variations = variationsPerTool[tool.tool] || [];
    
    for (let i = 0; i < 5; i++) {
      const variation = variations[i % variations.length];
      const idx = i % variations.length;
      
      examples.push({
        messages: [
          { role: 'system', content: systemPrompts[i % systemPrompts.length] },
          { role: 'user', content: `${variation || 'process'} (variant ${i + 1})` },
          { role: 'assistant', content: `I'll help you with that using ${tool.tool}. This is a variant example showing different ways to phrase the same intent.`, tool_use: { name: tool.tool, input: generateMockInput(tool.tool, i) } },
          { role: 'user', content: '', tool_result: { tool_use_id: `tool_${i}`, content: getMockResult(tool.tool, i) } },
          { role: 'assistant', content: `Done! Here's the result for variant ${i + 1} of ${(variation || 'task').toLowerCase()}.` }
        ]
      });
    }
  }

  // Write examples to JSONL
  const outputPath = path.join(SYNTHETIC_DIR, 'examples.jsonl');
  const stream = fs.createWriteStream(outputPath);
  for (const ex of examples) {
    stream.write(JSON.stringify(ex) + '\n');
  }
  stream.end();

  console.log(`βœ… Generated ${examples.length} synthetic examples`);
  return examples;
}

function generateMockInput(toolName, variant) {
  const inputs = {
    FileReadTool: [{ file_path: `example-${variant}.txt` }, { file_path: 'src/index.ts' }, { file_path: 'config.json' }],
    GlobTool: [{ pattern: `**/*.${variant === 0 ? 'ts' : 'js'}` }, { pattern: 'src/**/*.tsx' }],
    GrepTool: [{ pattern: 'TODO', path: 'src' }],
    BashTool: [{ command: 'ls -la' }, { command: 'git status' }],
    FileWriteTool: [{ file_path: 'output.txt', content: 'test' }]
  };
  return inputs[toolName]?.[variant % (inputs[toolName]?.length || 1)] || { query: `variant-${variant}` };
}

function getMockResult(toolName, variant) {
  const results = {
    FileReadTool: 'File contents here...',
    GlobTool: `file1.${variant === 0 ? 'ts' : 'js'}\nfile2.${variant === 0 ? 'ts' : 'js'}`,
    GrepTool: 'Found 3 matches',
    BashTool: 'Command output here',
    FileWriteTool: ''
  };
  return results[toolName] || 'Done';
}

// ============================================================================
// 6. CREATE TRAINING MANIFEST
// ============================================================================

function createManifest(tools, stats) {
  const manifest = {
    dataset: {
      name: 'Stack 2.9 Training Data',
      version: '0.2.0',
      description: 'Training data for Stack 2.9, an open-source coding assistant based on Qwen2.5-Coder',
      source: 'OpenClaw architecture + synthetic examples + code analysis',
      license: 'Apache 2.0'
    },
    stats: {
      toolSchemas: tools.length,
      syntheticExamples: stats.syntheticExamples,
      codeCommentPairs: stats.codeCommentPairs,
      testExamples: stats.testExamples,
      conversations: stats.conversations,
      totalExamples: stats.syntheticExamples
    },
    model_config: {
      base_model: 'Qwen2.5-Coder-32B',
      fine_tuning_method: 'LoRA',
      lora_rank: 64,
      lora_alpha: 128,
      target_modules: [
        'q_proj', 'k_proj', 'v_proj', 'o_proj',
        'gate_proj', 'up_proj', 'down_proj'
      ],
      quantization: 'AWQ 4-bit (inference)',
      max_seq_length: 131072,
      template: 'chatml'
    },
    tokenizer: {
      family: 'Qwen2',
      pad_token: '<|endoftext|>',
      bos_token: '<|endoftext|>',
      eos_token: '<|endoftext|>'
    },
    training_data: {
      synthetic_examples: `${SYNTHETIC_DIR}/examples.jsonl`,
      tools_catalog: `${TOOLS_SCHEMA_DIR}/catalog.json`,
      code_pairs: `${CODE_PAIRS_DIR}/pairs.json`,
      test_examples: `${CODE_PAIRS_DIR}/test-examples.json`,
      conversations: `${CONVERSATIONS_DIR}/parsed.json`,
      estimated_tokens: '~50M tokens total',
      recommended_dataset_size: '100K - 1M examples'
    },
    deployment: {
      inference_engine: 'vLLM',
      api_compatibility: 'OpenAI-compatible (chat/completions)',
      expected_throughput: '~50 tokens/s on A100 80GB',
      platforms: ['Hugging Face', 'OpenRouter', 'self-hosted']
    }
  };

  fs.writeFileSync(
    path.join(OUTPUT_DIR, 'manifest.json'),
    JSON.stringify(manifest, null, 2)
  );

  console.log('βœ… Created training manifest');
  return manifest;
}

// ============================================================================
// 7. CREATE TRAINING CONFIG
// ============================================================================

function createTrainingConfig() {
  const config = {
    model_name: 'Qwen/Qwen2.5-Coder-32B',
    dataset_path: './training-data/synthetic/examples.jsonl',
    max_seq_length: 131072,
    load_in_4bit: true,
    bf16: true,
    batch_size: 1,
    gradient_accumulation_steps: 16,
    learning_rate: 1e-4,
    num_train_epochs: 3,
    warmup_steps: 100,
    save_steps: 1000,
    eval_steps: 500,
    logging_steps: 10,
    output_dir: './stack-2.9-lora',
    push_to_hub: false,
    hub_model_id: 'your-username/stack-2.9',
    lora_config: {
      r: 64,
      lora_alpha: 128,
      target_modules: ['q_proj', 'k_proj', 'v_proj', 'o_proj', 'gate_proj', 'up_proj', 'down_proj'],
      lora_dropout: 0.05,
      bias: 'none'
    }
  };

  fs.writeFileSync(
    path.join(OUTPUT_DIR, 'training-config.json'),
    JSON.stringify(config, null, 2)
  );

  console.log('βœ… Created training config template');
  return config;
}

// ============================================================================
// MAIN
// ============================================================================

console.log('πŸ”§ Stack 2.9 - Enhanced Training Data Extractor\n');
console.log(`πŸ“‚ Source: ${SRC_DIR}`);
console.log(`πŸ“ Output: ${OUTPUT_DIR}\n`);

// Run extraction pipeline
const tools = extractToolSchemas();
const codePairs = extractCodeCommentPairs();
const testExamples = extractTestExamples();
const conversations = parseConversations();
const syntheticExamples = generateSyntheticExamples(tools);
createManifest(tools, {
  syntheticExamples: syntheticExamples.length,
  codeCommentPairs: codePairs.length,
  testExamples: testExamples.length,
  conversations: conversations.length
});
createTrainingConfig();

console.log('\n✨ Extraction complete!');
console.log('\nπŸ“‹ Summary:');
console.log(`   - Tool schemas: ${tools.length} tools`);
console.log(`   - Synthetic examples: ${syntheticExamples.length}`);
console.log(`   - Code-comment pairs: ${codePairs.length}`);
console.log(`   - Test examples: ${testExamples.length}`);
console.log(`   - Conversations: ${conversations.length}`);
console.log('\nπŸ“ Output files:');
console.log(`   - ${TOOLS_SCHEMA_DIR}/catalog.json`);
console.log(`   - ${SYNTHETIC_DIR}/examples.jsonl`);
console.log(`   - ${CODE_PAIRS_DIR}/pairs.json`);
console.log(`   - ${CODE_PAIRS_DIR}/test-examples.json`);
console.log(`   - ${CONVERSATIONS_DIR}/parsed.json`);
console.log(`   - ${OUTPUT_DIR}/manifest.json`);
console.log(`   - ${OUTPUT_DIR}/training-config.json`);
console.log('\nπŸš€ Next steps:');
console.log('   1. Review extracted code-comment pairs for quality');
console.log('   2. Add real conversation logs from ~/.claude/sessions');
console.log('   3. Scale: aim for 50+ examples per tool');
console.log('   4. Convert to Parquet for faster loading');
console.log('   5. Launch LoRA fine-tuning on Qwen2.5-Coder-32B');
console.log('   6. Deploy with vLLM and submit to OpenRouter');