-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkb.py
More file actions
executable file
·1069 lines (875 loc) · 41.4 KB
/
kb.py
File metadata and controls
executable file
·1069 lines (875 loc) · 41.4 KB
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
#!/usr/bin/env python3
"""
🧠 Knowledge Base CLI
Unified interface for managing the neural network battle simulation knowledge base.
Consolidates document analysis, categorization, and querying into a single tool.
"""
import os
import sys
import json
import time
import argparse
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from dataclasses import dataclass, asdict
from enum import Enum
import re
# Add scripts directory to path for imports
sys.path.append(str(Path(__file__).parent / "scripts"))
try:
from migrate_docs import (
DocumentAnalysis, DocumentCategory,
categorize_with_openrouter, analyze_file,
DOCS_DIR, KNOWLEDGE_BASE, ARCHIVE_DIR
)
from categorize_docs import DocumentCategorizer
except ImportError as e:
print(f"Error importing modules: {e}")
print("Make sure you're running from the project root directory")
sys.exit(1)
# Add this after the imports, before the KnowledgeBase class
CATEGORY_FOLDERS = {
"agents": "🤖 agents",
"training": "🧠 training",
"gameplay": "🎮 gameplay",
"development": "🔧 development",
"guides": "📖 guides",
"reference": "📚 reference",
"misc": "misc"
}
def get_target_folder(category: str) -> str:
"""Get the target folder name for a category"""
return CATEGORY_FOLDERS.get(category, category)
class KnowledgeBase:
"""Main knowledge base interface"""
def __init__(self):
self.docs_dir = DOCS_DIR
self.kb_dir = KNOWLEDGE_BASE
self.archive_dir = ARCHIVE_DIR
def status(self) -> Dict:
"""Get knowledge base statistics"""
stats = {
'total_docs': 0,
'kb_docs': 0,
'unorganized_docs': 0,
'categories': {},
'total_size': 0
}
# Count all markdown files
all_docs = list(self.docs_dir.rglob("*.md"))
stats['total_docs'] = len(all_docs)
# Count organized docs in emoji folders
organized_docs = []
for category, folder in CATEGORY_FOLDERS.items():
folder_path = self.docs_dir / folder
if folder_path.exists():
folder_docs = list(folder_path.glob("*.md"))
organized_docs.extend(folder_docs)
if folder_docs:
stats['categories'][folder] = len(folder_docs)
stats['kb_docs'] = len(organized_docs)
# Count unorganized docs (not in emoji folders or archive)
unorganized = []
for doc in all_docs:
is_organized = any(doc.is_relative_to(self.docs_dir / folder)
for folder in CATEGORY_FOLDERS.values()
if (self.docs_dir / folder).exists())
is_archived = (self.archive_dir.exists() and doc.is_relative_to(self.archive_dir))
if not is_organized and not is_archived:
unorganized.append(doc)
stats['total_size'] += doc.stat().st_size
stats['unorganized_docs'] = len(unorganized)
return stats
def cmd_status(args):
"""Show knowledge base status"""
kb = KnowledgeBase()
stats = kb.status()
print("🧠 Knowledge Base Status")
print("=" * 40)
print(f"📄 Total documents: {stats['total_docs']}")
print(f"🗂️ Organized: {stats['kb_docs']}")
print(f"📦 Unorganized: {stats['unorganized_docs']}")
if stats['categories']:
print(f"\n📊 Categories:")
for category, count in sorted(stats['categories'].items()):
print(f" {category}: {count} docs")
if stats['unorganized_docs'] > 0:
size_mb = stats['total_size'] / (1024 * 1024)
print(f"\n💡 Tip: Run 'kb analyze' to process {stats['unorganized_docs']} unorganized docs ({size_mb:.1f}MB)")
def cmd_analyze(args):
"""Analyze and categorize documents using LLM"""
from migrate_docs import process_files
import asyncio
# Find unorganized markdown files
kb = KnowledgeBase()
all_docs = list(kb.docs_dir.rglob("*.md"))
unorganized = []
for doc in all_docs:
if not (doc.is_relative_to(kb.kb_dir) or
(kb.archive_dir.exists() and doc.is_relative_to(kb.archive_dir))):
unorganized.append(doc)
if not unorganized:
print("✅ All documents are already organized!")
return
print(f"🔍 Found {len(unorganized)} unorganized documents")
if args.limit:
unorganized = unorganized[:args.limit]
print(f"📝 Processing first {len(unorganized)} documents (--limit {args.limit})")
# Process files
asyncio.run(process_files(unorganized, dry_run=args.dry_run, interactive=not args.yes))
def cmd_categorize(args):
"""Categorize documents using rule-based approach"""
rules_path = Path(__file__).parent / 'scripts' / 'doc_rules.json'
categorizer = DocumentCategorizer(str(rules_path))
# Find markdown files to categorize
source_dir = Path(args.source)
markdown_files = list(source_dir.glob('*.md'))
if not markdown_files:
print(f"No markdown files found in {source_dir}")
return
print(f"🏷️ Categorizing {len(markdown_files)} files using rule-based approach")
categorized = []
for file_path in markdown_files:
doc = categorizer.categorize_file(file_path)
categorized.append(doc)
print(f"📄 {file_path.name}")
print(f" Category: {doc.category}")
print(f" Confidence: {doc.confidence:.1%}")
print(f" Reason: {doc.reason}")
# Show summary
print(f"\n📊 Summary by category:")
by_category = {}
for doc in categorized:
by_category.setdefault(doc.category, []).append(doc)
for category, docs in sorted(by_category.items()):
print(f" {category}: {len(docs)} files")
def cmd_query(args):
"""Query the knowledge base with smart Q&A"""
if not args.question:
# Interactive mode
print("🔍 Knowledge Base Query (Interactive Mode)")
print("=" * 45)
print("Ask questions about your neural network battle simulation!")
print("Type 'exit' or 'quit' to stop, 'help' for tips.")
print()
while True:
try:
question = input("❓ Your question: ").strip()
if question.lower() in ['exit', 'quit', 'q']:
print("👋 Goodbye!")
break
elif question.lower() == 'help':
print("\n💡 Tips:")
print("• Ask about agents: 'How do agents make decisions?'")
print("• Ask about training: 'How does NEAT evolution work?'")
print("• Ask about gameplay: 'What are the combat mechanics?'")
print("• Ask about development: 'How do I set up the project?'")
print()
continue
elif not question:
continue
answer = query_knowledge_base(question)
print(f"\n🤖 Answer:\n{answer}\n")
except KeyboardInterrupt:
print("\n👋 Goodbye!")
break
else:
# Single question mode
print(f"🔍 Querying: {args.question}")
print("=" * 50)
answer = query_knowledge_base(args.question)
print(f"\n🤖 Answer:\n{answer}")
def query_knowledge_base(question: str) -> str:
"""Query the knowledge base and generate an answer"""
import os
import requests
from dotenv import load_dotenv
# Load environment variables
load_dotenv(Path(__file__).parent / '.env')
try:
# Step 1: Find relevant documents
relevant_docs = find_relevant_documents(question)
if not relevant_docs:
return "❌ I couldn't find any relevant documents for your question. Try rephrasing or asking about agents, training, gameplay, or development."
# Step 2: Extract relevant content
context = build_context_from_docs(relevant_docs, question)
# Step 3: Generate answer using Qwen3
answer = generate_answer_with_qwen(question, context)
return answer
except Exception as e:
return f"❌ Error processing your question: {str(e)}"
def find_relevant_documents(question: str) -> List[Path]:
"""Find documents relevant to the question using keyword matching"""
docs_dir = Path("docs")
relevant_docs = []
# Normalize question for searching
question_lower = question.lower()
# Category-based search hints
category_keywords = {
"agents": ["agent", "ai", "brain", "decision", "sensor", "perception", "behavior"],
"training": ["train", "neat", "evolution", "fitness", "learn", "optimize", "tournament"],
"gameplay": ["game", "combat", "battle", "physics", "simulation", "team", "fight"],
"development": ["setup", "install", "build", "dev", "contribute", "architecture", "performance"],
"guides": ["how", "tutorial", "guide", "step", "start", "begin"],
"reference": ["api", "spec", "documentation", "interface"]
}
# Find the most relevant category
best_category = None
max_matches = 0
for category, keywords in category_keywords.items():
matches = sum(1 for keyword in keywords if keyword in question_lower)
if matches > max_matches:
max_matches = matches
best_category = category
# Search in the relevant category first
if best_category:
category_folder = CATEGORY_FOLDERS.get(best_category, best_category)
category_path = docs_dir / category_folder
if category_path.exists():
for doc in category_path.glob("*.md"):
relevant_docs.append(doc)
# If no category match or need more docs, search broadly
if len(relevant_docs) < 3:
for emoji_folder in CATEGORY_FOLDERS.values():
folder_path = docs_dir / emoji_folder
if folder_path.exists():
for doc in folder_path.glob("*.md"):
if doc not in relevant_docs:
# Quick relevance check
try:
content = doc.read_text(encoding='utf-8', errors='ignore').lower()
if any(word in content for word in question_lower.split() if len(word) > 2):
relevant_docs.append(doc)
except:
continue
return relevant_docs[:5] # Limit to top 5 docs
def build_context_from_docs(docs: List[Path], question: str) -> str:
"""Build context string from relevant documents"""
context_parts = []
for doc in docs:
try:
content = doc.read_text(encoding='utf-8', errors='ignore')
# Extract title
title = doc.stem.replace('_', ' ').title()
first_line = content.split('\n')[0]
if first_line.startswith('#'):
title = first_line.strip('# ')
# Get relevant excerpt (first 1000 chars for now)
excerpt = content[:1000].strip()
if len(content) > 1000:
excerpt += "..."
context_parts.append(f"## {title}\n{excerpt}")
except Exception as e:
continue
return "\n\n".join(context_parts)
def generate_answer_with_qwen(question: str, context: str) -> str:
"""Generate answer using Qwen3 via OpenRouter"""
import os
import requests
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
return "❌ OpenRouter API key not found. Please set OPENROUTER_API_KEY in your .env file."
prompt = f"""You are a knowledgeable assistant for a neural network battle simulation project. Answer the user's question based on the provided documentation context.
Context from documentation:
{context}
User question: {question}
Instructions:
- Provide a clear, helpful answer based on the documentation
- If the documentation doesn't contain enough information, say so
- Use technical terms appropriately but explain them when needed
- Be concise but thorough
- Include relevant details from the documentation
Answer:"""
try:
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "qwen/qwen-2.5-72b-instruct", # Updated model
"messages": [
{"role": "user", "content": prompt}
],
"temperature": 0.3,
"max_tokens": 800
},
timeout=30
)
response.raise_for_status()
result = response.json()
answer = result['choices'][0]['message']['content'].strip()
# Add source information
doc_names = [doc.name for doc in find_relevant_documents(question)]
if doc_names:
answer += f"\n\n📚 Sources: {', '.join(doc_names[:3])}"
return answer
except requests.exceptions.RequestException as e:
return f"❌ API request failed: {str(e)}"
except Exception as e:
return f"❌ Error generating answer: {str(e)}"
def cmd_benchmark(args):
"""Benchmark knowledge base performance"""
print("⚡ Knowledge Base Benchmark")
print("=" * 35)
print("This feature is coming soon!")
print()
print("Planned benchmarks:")
print("• Categorization accuracy")
print("• Summary quality assessment")
print("• Query response relevance")
print("• Processing speed metrics")
def cmd_migrate(args):
"""Migrate files to the new human-readable structure"""
import shutil
# First, categorize files
rules_path = Path(__file__).parent / 'scripts' / 'doc_rules.json'
categorizer = DocumentCategorizer(str(rules_path))
# Find markdown files to migrate
source_dir = Path("docs")
markdown_files = [f for f in source_dir.glob('*.md') if not f.name.startswith('.')]
if not markdown_files:
print("No markdown files found to migrate")
return
print(f"🚚 Migrating {len(markdown_files)} files to new structure")
print()
moved_count = 0
for file_path in markdown_files:
doc = categorizer.categorize_file(file_path)
# Get target folder with emoji
target_folder = get_target_folder(doc.category)
target_dir = source_dir / target_folder
target_path = target_dir / file_path.name
# Skip if already in target location
if file_path.parent == target_dir:
continue
print(f"📄 {file_path.name}")
print(f" Moving to: {target_folder}/")
print(f" Reason: {doc.reason}")
print(f" Confidence: {doc.confidence:.1%}")
if not args.dry_run:
# Create target directory if it doesn't exist
target_dir.mkdir(parents=True, exist_ok=True)
# Move the file
shutil.move(str(file_path), str(target_path))
moved_count += 1
print(f" ✅ Moved!")
else:
print(f" 🔍 Would move (dry-run)")
print()
if args.dry_run:
print(f"🔍 Dry run complete. Would move {len(markdown_files)} files")
else:
print(f"✅ Migration complete! Moved {moved_count} files to new structure")
print("\n📁 New structure:")
for category, folder in CATEGORY_FOLDERS.items():
folder_path = source_dir / folder
if folder_path.exists():
file_count = len(list(folder_path.glob("*.md")))
if file_count > 0:
print(f" {folder}: {file_count} files")
def cmd_discover(args):
"""Discover all markdown files across the repository"""
import subprocess
print("🔍 Discovering markdown files across the repository...")
print("=" * 55)
# Find all markdown files, excluding common non-documentation directories
try:
result = subprocess.run([
'find', '.', '-name', '*.md', '-type', 'f'
], capture_output=True, text=True, check=True)
all_files = result.stdout.strip().split('\n')
# Filter out unwanted directories
excluded_patterns = [
'node_modules', '.venv', 'venv', '.git',
'__pycache__', '.cargo/registry', 'target/debug', 'target/release'
]
filtered_files = []
for file_path in all_files:
if file_path and not any(pattern in file_path for pattern in excluded_patterns):
filtered_files.append(Path(file_path))
print(f"📊 Found {len(filtered_files)} markdown files total")
# Categorize by location
categories = {
'organized': [], # Already in our emoji folders
'docs_root': [], # In docs/ but not organized
'sim_core': [], # In sim_core/
'root': [], # In project root
'other': [] # Elsewhere
}
for file_path in filtered_files:
path_str = str(file_path)
if any(emoji in path_str for emoji in ['🤖', '🧠', '🎮', '🔧', '📖', '📚']):
categories['organized'].append(file_path)
elif path_str.startswith('./docs/') and not path_str.startswith('./docs/knowledge/'):
categories['docs_root'].append(file_path)
elif path_str.startswith('./sim_core/'):
categories['sim_core'].append(file_path)
elif path_str.count('/') == 1: # Root level files
categories['root'].append(file_path)
else:
categories['other'].append(file_path)
# Display results
print(f"\n📁 File Distribution:")
print(f" ✅ Already organized: {len(categories['organized'])} files")
print(f" 📄 docs/ (unorganized): {len(categories['docs_root'])} files")
print(f" 🦀 sim_core/: {len(categories['sim_core'])} files")
print(f" 📋 root level: {len(categories['root'])} files")
print(f" 📂 other locations: {len(categories['other'])} files")
# Show unorganized files by location
if args.show_files:
for category, files in categories.items():
if category != 'organized' and files:
print(f"\n📂 {category.replace('_', ' ').title()} files:")
for file_path in sorted(files)[:10]: # Show first 10
print(f" • {file_path}")
if len(files) > 10:
print(f" ... and {len(files) - 10} more")
# Show actionable suggestions
unorganized_count = sum(len(files) for cat, files in categories.items() if cat != 'organized')
if unorganized_count > 0:
print(f"\n💡 Next steps:")
print(f" • Run 'kb harvest' to analyze and organize {unorganized_count} unorganized files")
print(f" • Use 'kb harvest --dry-run' to preview the organization")
print(f" • Add '--show-files' to see detailed file lists")
return categories
except subprocess.CalledProcessError as e:
print(f"❌ Error finding files: {e}")
return None
def cmd_harvest(args):
"""Harvest and organize markdown files from across the repository"""
import shutil
print("🌾 Harvesting documentation from across the repository...")
print("=" * 60)
# Find all markdown files manually to exclude archives
import subprocess
try:
result = subprocess.run([
'find', '.', '-name', '*.md', '-type', 'f'
], capture_output=True, text=True, check=True)
all_files = result.stdout.strip().split('\n')
# Filter out unwanted directories and files
excluded_patterns = [
'node_modules', '.venv', 'venv', '.git',
'__pycache__', '.cargo/registry', 'target/debug', 'target/release',
'docs/archive' # Exclude archive by default
]
if not args.include_archive:
excluded_patterns.append('docs/archive')
# Also exclude already organized files
emoji_patterns = ['🤖', '🧠', '🎮', '🔧', '📖', '📚']
harvestable = []
for file_path_str in all_files:
if not file_path_str:
continue
# Skip excluded patterns
if any(pattern in file_path_str for pattern in excluded_patterns):
continue
# Skip already organized files
if any(emoji in file_path_str for emoji in emoji_patterns):
continue
# Skip certain metadata files
filename = Path(file_path_str).name.lower()
if any(skip in filename for skip in ['readme', 'license', 'privacy', 'changelog']):
if not args.include_meta:
continue
harvestable.append(Path(file_path_str))
except subprocess.CalledProcessError as e:
print(f"❌ Error finding files: {e}")
return
if not harvestable:
print("✅ All relevant files are already organized!")
return
print(f"🎯 Found {len(harvestable)} files to analyze for organization...")
# Categorize files using the improved system
organized_count = 0
skipped_count = 0
for file_path in harvestable:
try:
# Skip very small files (likely not documentation)
if file_path.stat().st_size < 50:
skipped_count += 1
continue
# Categorize the file using the improved analysis
doc = analyze_file(file_path)
# Get target location
target_folder = get_target_folder(doc.category)
target_dir = Path("docs") / target_folder
target_path = target_dir / file_path.name
# Handle name conflicts
counter = 1
original_target = target_path
while target_path.exists():
stem = original_target.stem
suffix = original_target.suffix
target_path = original_target.parent / f"{stem}_{counter}{suffix}"
counter += 1
print(f"📄 {file_path}")
print(f" → {target_folder}/")
print(f" Category: {doc.category} (confidence: {doc.confidence:.1%})")
if doc.confidence < 0.3:
print(f" ⚠️ Low confidence - please review")
if args.dry_run:
print(" 🔍 Would copy (dry-run)")
else:
# Create target directory
target_dir.mkdir(parents=True, exist_ok=True)
# Copy (don't move) to preserve original structure
shutil.copy2(str(file_path), str(target_path))
organized_count += 1
print(" ✅ Copied to knowledge base!")
print()
except Exception as e:
print(f"❌ Error processing {file_path}: {e}")
skipped_count += 1
continue
# Summary
if args.dry_run:
print(f"🔍 Dry run complete:")
print(f" • Would organize: {len(harvestable) - skipped_count} files")
print(f" • Would skip: {skipped_count} files")
else:
print(f"✅ Harvest complete!")
print(f" • Organized: {organized_count} files")
print(f" • Skipped: {skipped_count} files")
print(f"\n📚 Run 'kb status' to see the updated knowledge base")
def cmd_cleanup(args):
"""Clean up duplicate and redundant files in the knowledge base"""
import hashlib
from collections import defaultdict
print("🧹 Cleaning up knowledge base duplicates and redundancy...")
print("=" * 55)
# Find all files in organized folders
kb_files = []
for category, folder in CATEGORY_FOLDERS.items():
folder_path = Path("docs") / folder
if folder_path.exists():
for file_path in folder_path.glob("*.md"):
kb_files.append(file_path)
print(f"📊 Found {len(kb_files)} files in knowledge base")
# Group files by content hash to find duplicates
file_hashes = defaultdict(list)
file_sizes = defaultdict(list)
suspicious_names = []
for file_path in kb_files:
try:
content = file_path.read_text(encoding='utf-8', errors='ignore')
content_hash = hashlib.md5(content.encode()).hexdigest()
file_hashes[content_hash].append(file_path)
size = file_path.stat().st_size
file_sizes[size].append(file_path)
# Flag suspicious file names
name = file_path.name.lower()
if any(pattern in name for pattern in ['untitled', 'copy', 'duplicate', '(1)', '_1', '_2']):
# Exclude index_* files - they have their own rename command
if not name.startswith('index_'):
suspicious_names.append(file_path)
except Exception as e:
print(f"⚠️ Error reading {file_path}: {e}")
# Report findings
duplicates = {h: files for h, files in file_hashes.items() if len(files) > 1}
size_duplicates = {s: files for s, files in file_sizes.items() if len(files) > 1}
print(f"\n🔍 Analysis Results:")
print(f" 📄 Total files: {len(kb_files)}")
print(f" 🔗 Exact duplicates: {len(duplicates)} groups ({sum(len(files)-1 for files in duplicates.values())} redundant files)")
print(f" 📏 Same-size files: {len(size_duplicates)} groups")
print(f" 🚨 Suspicious names: {len(suspicious_names)} files")
# Show duplicates
if duplicates:
print(f"\n🔗 Exact Content Duplicates:")
for content_hash, files in list(duplicates.items())[:5]: # Show first 5
print(f" Hash: {content_hash[:8]}...")
for i, file_path in enumerate(files):
marker = "🗑️" if i > 0 else "✅"
print(f" {marker} {file_path}")
if len(duplicates) > 5:
print(f" ... and {len(duplicates) - 5} more duplicate groups")
# Show suspicious names
if suspicious_names:
print(f"\n🚨 Suspicious File Names (likely auto-generated):")
for file_path in suspicious_names[:10]:
print(f" • {file_path}")
if len(suspicious_names) > 10:
print(f" ... and {len(suspicious_names) - 10} more")
# Show actions
total_removable = sum(len(files)-1 for files in duplicates.values()) + len(suspicious_names)
if total_removable > 0:
print(f"\n💡 Recommended actions:")
print(f" • Run 'kb cleanup --remove-duplicates' to remove {sum(len(files)-1 for files in duplicates.values())} exact duplicates")
print(f" • Run 'kb cleanup --remove-suspicious' to remove {len(suspicious_names)} suspicious files")
print(f" • Run 'kb cleanup --remove-all' to remove both ({total_removable} files total)")
# Execute cleanup if requested
removed_count = 0
if args.remove_duplicates or args.remove_all:
print(f"\n🗑️ Removing exact duplicates...")
for content_hash, files in duplicates.items():
# Keep the first file, remove the rest
for file_path in files[1:]:
if args.dry_run:
print(f" 🔍 Would remove: {file_path}")
else:
file_path.unlink()
print(f" ✅ Removed: {file_path}")
removed_count += 1
if args.remove_suspicious or args.remove_all:
print(f"\n🗑️ Removing suspicious files...")
for file_path in suspicious_names:
if file_path not in [f for files in duplicates.values() for f in files[1:]]: # Don't double-remove
if args.dry_run:
print(f" 🔍 Would remove: {file_path}")
else:
file_path.unlink()
print(f" ✅ Removed: {file_path}")
removed_count += 1
if removed_count > 0:
print(f"\n✅ Cleanup complete! Removed {removed_count} files")
print(f"📚 Run 'kb status' to see the cleaned knowledge base")
elif args.dry_run and total_removable > 0:
print(f"\n🔍 Dry run complete. Would remove {total_removable} files")
def cmd_archive_status(args):
"""Show status of archived documents"""
archive_dir = Path("docs/archive")
if not archive_dir.exists():
print("📂 No archive directory found")
return
archive_files = list(archive_dir.rglob("*.md"))
print(f"📦 Archive Status")
print("=" * 30)
print(f"📄 Archive files: {len(archive_files)}")
if args.show_files:
print(f"\n📂 Archive contents (first 20):")
for file_path in sorted(archive_files)[:20]:
size_kb = file_path.stat().st_size / 1024
print(f" • {file_path.name} ({size_kb:.1f}KB)")
if len(archive_files) > 20:
print(f" ... and {len(archive_files) - 20} more files")
print(f"\n💡 To include archive in harvest: kb harvest --include-archive")
def cmd_rename_index_files(args):
"""Intelligently rename index_* files to proper names"""
print("🔧 Renaming index_* files to proper names...")
print("=" * 45)
# Find all index_* files in organized folders
index_files = []
for category, folder in CATEGORY_FOLDERS.items():
folder_path = Path("docs") / folder
if folder_path.exists():
for file_path in folder_path.glob("index_*.md"):
index_files.append(file_path)
print(f"📊 Found {len(index_files)} index_* files to rename")
renamed_count = 0
for file_path in index_files:
try:
# Read content to extract title
content = file_path.read_text(encoding='utf-8', errors='ignore')
# Try to extract a good name from the content
new_name = None
# Method 1: Look for # Title at the start
title_match = re.search(r'^#\s+(.+?)$', content, re.MULTILINE)
if title_match:
title = title_match.group(1).strip()
# Clean up the title for filename
new_name = re.sub(r'[^\w\s-]', '', title).strip()
new_name = re.sub(r'\s+', '_', new_name)
new_name = new_name[:50] # Limit length
# Method 2: If no title, try to guess from content keywords
if not new_name:
content_lower = content.lower()
if 'neat' in content_lower and 'training' in content_lower:
new_name = "neat_training_guide"
elif 'agent' in content_lower and 'sensor' in content_lower:
new_name = "agent_sensor_guide"
elif 'gameplay' in content_lower and 'simulation' in content_lower:
new_name = "gameplay_simulation_guide"
elif 'development' in content_lower and 'setup' in content_lower:
new_name = "development_setup_guide"
else:
# Use first few words of content
words = content.split()[:5]
new_name = "_".join(re.sub(r'[^\w]', '', word) for word in words if word.isalpha())
new_name = new_name[:30]
# Fallback: keep as index_X but make it clear it needs manual review
if not new_name or len(new_name) < 3:
new_name = f"document_{file_path.stem}"
# Ensure .md extension
if not new_name.endswith('.md'):
new_name += '.md'
# Create new path
new_path = file_path.parent / new_name
# Handle conflicts
counter = 1
original_new_path = new_path
while new_path.exists() and new_path != file_path:
stem = original_new_path.stem
suffix = original_new_path.suffix
new_path = original_new_path.parent / f"{stem}_{counter}{suffix}"
counter += 1
print(f"📄 {file_path.name}")
print(f" → {new_path.name}")
# Show preview of content for verification
preview = content[:100].replace('\n', ' ').strip()
if len(content) > 100:
preview += "..."
print(f" Preview: {preview}")
if args.dry_run:
print(" 🔍 Would rename (dry-run)")
else:
file_path.rename(new_path)
print(" ✅ Renamed!")
renamed_count += 1
print()
except Exception as e:
print(f"❌ Error processing {file_path}: {e}")
if args.dry_run:
print(f"🔍 Dry run complete. Would rename {len(index_files)} files")
else:
print(f"✅ Rename complete! Renamed {renamed_count} files")
print(f"📚 Run 'kb status' to see the updated knowledge base")
def cmd_cleanup_old_structure(args):
"""Clean up the old knowledge/documents structure after successful harvest"""
import shutil
print("🧹 Cleaning up old knowledge base structure...")
print("=" * 50)
# Files and directories to clean up
cleanup_targets = [
"knowledge/documents", # Old nested structure
"inbox.md", # Root level files already harvested
"RAG_LEARNINGS.md",
"docs_structure_proposal.md"
]
removed_count = 0
for target in cleanup_targets:
target_path = Path(target)
if target_path.exists():
if target_path.is_dir():
# Count files in directory
file_count = len(list(target_path.rglob("*")))
print(f"📂 {target}: {file_count} items")
if args.dry_run:
print(f" 🔍 Would remove directory (dry-run)")
else:
shutil.rmtree(target_path)
print(f" ✅ Removed directory!")
removed_count += file_count
else:
# Single file
print(f"📄 {target}")
if args.dry_run:
print(f" 🔍 Would remove file (dry-run)")
else:
target_path.unlink()
print(f" ✅ Removed file!")
removed_count += 1
else:
print(f"⚠️ {target}: Not found (already cleaned?)")
if args.dry_run:
print(f"\n🔍 Dry run complete. Would remove old structure")
else:
print(f"\n✅ Cleanup complete! Removed {removed_count} items")
print(f"📚 Old knowledge structure has been cleaned up")
print(f"💡 Run 'kb status' to see the clean knowledge base")
def main():
parser = argparse.ArgumentParser(
description="🧠 Knowledge Base CLI - Unified interface for document management",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
kb status # Show knowledge base overview
kb analyze # Analyze unorganized docs with LLM
kb analyze --dry-run # Preview what would be analyzed
kb analyze --limit 5 # Process only first 5 docs
kb categorize docs/ # Rule-based categorization
kb query "NEAT training" # Search knowledge base (coming soon)
kb benchmark # Performance testing (coming soon)
"""
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Status command
status_parser = subparsers.add_parser('status', help='Show knowledge base status')
# Analyze command (LLM-powered)
analyze_parser = subparsers.add_parser('analyze', help='Analyze documents with LLM')
analyze_parser.add_argument('--dry-run', action='store_true',
help='Show what would be done without making changes')
analyze_parser.add_argument('--yes', action='store_true',
help='Skip interactive prompts')
analyze_parser.add_argument('--limit', type=int,
help='Limit number of documents to process')
# Categorize command (rule-based)
categorize_parser = subparsers.add_parser('categorize', help='Rule-based categorization')
categorize_parser.add_argument('source', nargs='?', default='docs',
help='Source directory (default: docs)')
categorize_parser.add_argument('--dry-run', action='store_true',
help='Show categorization without moving files')
# Query command (future)
query_parser = subparsers.add_parser('query', help='Query the knowledge base')
query_parser.add_argument('question', nargs='?',
help='Question to ask the knowledge base')
# Benchmark command (future)
benchmark_parser = subparsers.add_parser('benchmark', help='Benchmark performance')
# Migrate command
migrate_parser = subparsers.add_parser('migrate', help='Migrate files to the new structure')
migrate_parser.add_argument('--dry-run', action='store_true',
help='Show what would be done without making changes')
# Discover command
discover_parser = subparsers.add_parser('discover', help='Discover markdown files across the repository')