File size: 28,792 Bytes
a3bdcf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import { ChevronRight, ChevronDown, Database, Code, Brain, Search, FileText, GitBranch, Layers, Workflow, Server, Cpu, ArrowRight, Zap } from 'lucide-react';

const ArchitectureViz = () => {
  const [activeTab, setActiveTab] = useState('overview');
  const [expandedSections, setExpandedSections] = useState({});

  const toggleSection = (section) => {
    setExpandedSections(prev => ({
      ...prev,
      [section]: !prev[section]
    }));
  };

  const tabs = [
    { id: 'overview', label: 'System Overview', icon: Layers },
    { id: 'rag', label: 'RAG Pipeline', icon: Search },
    { id: 'ast', label: 'AST & Graphs', icon: GitBranch },
    { id: 'chunking', label: 'Code Chunking', icon: Code },
    { id: 'agent', label: 'Agentic Workflow', icon: Brain },
    { id: 'retrieval', label: 'Retrieval System', icon: Database },
  ];

  const ComponentCard = ({ title, description, icon: Icon, color, children }) => (
    <div className={`bg-slate-800 rounded-lg p-4 border-l-4 ${color} hover:bg-slate-750 transition-all`}>
      <div className="flex items-center gap-2 mb-2">
        <Icon className="w-5 h-5 text-slate-300" />
        <h3 className="font-semibold text-white">{title}</h3>
      </div>
      <p className="text-slate-400 text-sm mb-2">{description}</p>
      {children}
    </div>
  );

  const FlowArrow = () => (
    <div className="flex justify-center py-2">
      <ArrowRight className="w-6 h-6 text-slate-500" />
    </div>
  );

  const renderOverview = () => (
    <div className="space-y-6">
      <div className="bg-gradient-to-r from-purple-900/50 to-blue-900/50 rounded-xl p-6 border border-purple-500/30">
        <h2 className="text-2xl font-bold text-white mb-2 flex items-center gap-2">
          <Zap className="w-6 h-6 text-yellow-400" />
          Code Crawler Architecture
        </h2>
        <p className="text-slate-300">
          An AI-powered codebase assistant combining RAG, AST analysis, Graph databases, and Agentic workflows.
        </p>
      </div>

      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
        <ComponentCard
          title="Data Ingestion"
          description="Universal ingestor supporting ZIP, GitHub, Local, Web"
          icon={FileText}
          color="border-green-500"
        >
          <div className="mt-2 space-y-1">
            <div className="text-xs bg-slate-700 rounded px-2 py-1">ZIPFileManager</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">GitHubRepoManager</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">LocalDirectoryManager</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">WebDocManager</div>
          </div>
        </ComponentCard>

        <ComponentCard
          title="Processing"
          description="AST parsing, chunking, embeddings, graph building"
          icon={Cpu}
          color="border-blue-500"
        >
          <div className="mt-2 space-y-1">
            <div className="text-xs bg-slate-700 rounded px-2 py-1">StructuralChunker (tree-sitter)</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">EnhancedCodeAnalyzer</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">Gemini/HuggingFace Embeddings</div>
          </div>
        </ComponentCard>

        <ComponentCard
          title="Storage"
          description="Vector DB and AST knowledge graph"
          icon={Database}
          color="border-purple-500"
        >
          <div className="mt-2 space-y-1">
            <div className="text-xs bg-slate-700 rounded px-2 py-1">Chroma / FAISS / Qdrant</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">GraphML (NetworkX)</div>
            <div className="text-xs bg-slate-700 rounded px-2 py-1">Merkle Tree Snapshots</div>
          </div>
        </ComponentCard>
      </div>

      <div className="bg-slate-800 rounded-lg p-4">
        <h3 className="font-semibold text-white mb-3 flex items-center gap-2">
          <Workflow className="w-5 h-5" />
          Data Flow
        </h3>
        <div className="flex flex-wrap items-center justify-center gap-2 text-sm">
          <span className="bg-green-600/30 text-green-300 px-3 py-1 rounded-full">Input Source</span>
          <ArrowRight className="w-4 h-4 text-slate-500" />
          <span className="bg-blue-600/30 text-blue-300 px-3 py-1 rounded-full">Ingestor</span>
          <ArrowRight className="w-4 h-4 text-slate-500" />
          <span className="bg-purple-600/30 text-purple-300 px-3 py-1 rounded-full">Chunker</span>
          <ArrowRight className="w-4 h-4 text-slate-500" />
          <span className="bg-pink-600/30 text-pink-300 px-3 py-1 rounded-full">Embeddings</span>
          <ArrowRight className="w-4 h-4 text-slate-500" />
          <span className="bg-orange-600/30 text-orange-300 px-3 py-1 rounded-full">Vector DB</span>
        </div>
      </div>

      <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
        <ComponentCard
          title="Retrieval Layer"
          description="Multi-stage retrieval with reranking"
          icon={Search}
          color="border-yellow-500"
        >
          <div className="mt-2 text-xs space-y-1">
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-yellow-500 rounded-full"></span>
              <span className="text-slate-300">Vector Retriever (60%)</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-yellow-500 rounded-full"></span>
              <span className="text-slate-300">LLM Retriever (40%)</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-yellow-500 rounded-full"></span>
              <span className="text-slate-300">Graph Enhancement</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-yellow-500 rounded-full"></span>
              <span className="text-slate-300">Cross-Encoder Reranker</span>
            </div>
          </div>
        </ComponentCard>

        <ComponentCard
          title="Chat Engine"
          description="Dual-mode: Linear RAG or Agentic"
          icon={Brain}
          color="border-red-500"
        >
          <div className="mt-2 text-xs space-y-1">
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-red-500 rounded-full"></span>
              <span className="text-slate-300">Linear RAG (simple Q&A)</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-red-500 rounded-full"></span>
              <span className="text-slate-300">Agentic Workflow (LangGraph)</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="w-2 h-2 bg-red-500 rounded-full"></span>
              <span className="text-slate-300">Tools: search, read, list, call_graph</span>
            </div>
          </div>
        </ComponentCard>
      </div>
    </div>
  );

  const renderRAG = () => (
    <div className="space-y-6">
      <div className="bg-slate-800 rounded-xl p-6">
        <h2 className="text-xl font-bold text-white mb-4">RAG Pipeline Implementation</h2>
        <p className="text-slate-400 mb-4">
          The RAG (Retrieval-Augmented Generation) system combines vector search with LLM-based file selection
          and cross-encoder reranking for high-precision code retrieval.
        </p>

        <div className="space-y-4">
          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-green-400 mb-2">1. Query Processing</h3>
            <code className="text-sm text-slate-300 block bg-slate-900 p-3 rounded">
              {`query = "How does authentication work?"
# Optionally expand with multi-query
expanded_queries = multi_query_expander(query)`}
            </code>
          </div>

          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-blue-400 mb-2">2. Hybrid Retrieval</h3>
            <code className="text-sm text-slate-300 block bg-slate-900 p-3 rounded">
              {`# Vector similarity search (60% weight)
vector_docs = chroma_db.similarity_search(query, k=10)

# LLM-based file selection (40% weight)
llm_docs = llm_retriever.select_files(query, file_tree)

# Combine with EnsembleRetriever
combined = ensemble([vector_docs, llm_docs], weights=[0.6, 0.4])`}
            </code>
          </div>

          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-purple-400 mb-2">3. Graph Enhancement</h3>
            <code className="text-sm text-slate-300 block bg-slate-900 p-3 rounded">
              {`# For each retrieved doc, find related files via AST graph
for doc in combined:
    neighbors = ast_graph.neighbors(doc.file_path)
    for neighbor in neighbors:
        if relation == "imports" or relation == "calls":
            augmented_docs.append(read_file(neighbor))`}
            </code>
          </div>

          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-yellow-400 mb-2">4. Cross-Encoder Reranking</h3>
            <code className="text-sm text-slate-300 block bg-slate-900 p-3 rounded">
              {`# Score each (query, document) pair with cross-encoder
pairs = [[query, doc.content] for doc in augmented_docs]
scores = cross_encoder.predict(pairs)

# Return top 5 by score
final_docs = sorted(zip(docs, scores), by=score)[:5]`}
            </code>
          </div>

          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-red-400 mb-2">5. Generation</h3>
            <code className="text-sm text-slate-300 block bg-slate-900 p-3 rounded">
              {`# Build context from retrieved docs
context = format_docs(final_docs)

# Generate answer with LLM
prompt = system_prompt.format(context=context)
answer = llm.invoke([SystemMessage(prompt), HumanMessage(query)])`}
            </code>
          </div>
        </div>
      </div>

      <div className="bg-slate-800 rounded-xl p-6">
        <h3 className="font-semibold text-white mb-3">Key Files</h3>
        <div className="grid grid-cols-2 gap-2 text-sm">
          <div className="bg-slate-700 rounded p-2">
            <span className="text-blue-400">code_chatbot/rag.py</span>
            <p className="text-slate-400 text-xs">ChatEngine class</p>
          </div>
          <div className="bg-slate-700 rounded p-2">
            <span className="text-blue-400">code_chatbot/retriever_wrapper.py</span>
            <p className="text-slate-400 text-xs">RerankingRetriever</p>
          </div>
          <div className="bg-slate-700 rounded p-2">
            <span className="text-blue-400">code_chatbot/llm_retriever.py</span>
            <p className="text-slate-400 text-xs">LLM-based file selection</p>
          </div>
          <div className="bg-slate-700 rounded p-2">
            <span className="text-blue-400">code_chatbot/reranker.py</span>
            <p className="text-slate-400 text-xs">Cross-encoder reranking</p>
          </div>
        </div>
      </div>
    </div>
  );

  const renderAST = () => (
    <div className="space-y-6">
      <div className="bg-slate-800 rounded-xl p-6">
        <h2 className="text-xl font-bold text-white mb-4">AST Analysis & Knowledge Graph</h2>
        <p className="text-slate-400 mb-4">
          Uses <span className="text-green-400">tree-sitter</span> to parse code into Abstract Syntax Trees,
          then builds a <span className="text-blue-400">NetworkX</span> directed graph capturing code relationships.
        </p>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-purple-400 mb-2">Node Types</h3>
            <ul className="text-sm text-slate-300 space-y-1">
              <li className="flex items-center gap-2">
                <span className="w-3 h-3 bg-green-500 rounded"></span> file
              </li>
              <li className="flex items-center gap-2">
                <span className="w-3 h-3 bg-blue-500 rounded"></span> class
              </li>
              <li className="flex items-center gap-2">
                <span className="w-3 h-3 bg-purple-500 rounded"></span> function
              </li>
              <li className="flex items-center gap-2">
                <span className="w-3 h-3 bg-yellow-500 rounded"></span> method
              </li>
            </ul>
          </div>

          <div className="bg-slate-700/50 rounded-lg p-4">
            <h3 className="font-semibold text-purple-400 mb-2">Edge Types (Relations)</h3>
            <ul className="text-sm text-slate-300 space-y-1">
              <li><span className="text-green-400">defines</span> - file β†’ class/function</li>
              <li><span className="text-blue-400">has_method</span> - class β†’ method</li>
              <li><span className="text-purple-400">calls</span> - function β†’ function</li>
              <li><span className="text-yellow-400">imports</span> - file β†’ module</li>
              <li><span className="text-red-400">inherits_from</span> - class β†’ class</li>
            </ul>
          </div>
        </div>

        <div className="bg-slate-900 rounded-lg p-4 overflow-x-auto">
          <h3 className="font-semibold text-white mb-2">Example: Parsing Python Code</h3>
          <pre className="text-sm text-slate-300">
{`# Source Code
class UserService:
    def get_user(self, user_id):
        return self.db.find(user_id)  # calls db.find

# Generated Graph
(file: user_service.py)
    β”‚
    └──defines──▢ (class: UserService)
                      β”‚
                      └──has_method──▢ (method: get_user)
                                           β”‚
                                           └──calls──▢ (function: db.find)`}
          </pre>
        </div>
      </div>

      <div className="bg-slate-800 rounded-xl p-6">
        <h3 className="font-semibold text-white mb-3">Call Graph Tools</h3>
        <div className="space-y-3">
          <div className="bg-slate-700 rounded p-3">
            <code className="text-green-400">find_callers("authenticate")</code>
            <p className="text-slate-400 text-sm mt-1">β†’ Returns all functions that call authenticate()</p>
          </div>
          <div className="bg-slate-700 rounded p-3">
            <code className="text-blue-400">find_callees("process_request")</code>
            <p className="text-slate-400 text-sm mt-1">β†’ Returns all functions called by process_request()</p>
          </div>
          <div className="bg-slate-700 rounded p-3">
            <code className="text-purple-400">find_call_chain("main", "save_to_db")</code>
            <p className="text-slate-400 text-sm mt-1">β†’ Returns execution paths from main() to save_to_db()</p>
          </div>
        </div>
      </div>
    </div>
  );

  const renderChunking = () => (
    <div className="space-y-6">
      <div className="bg-slate-800 rounded-xl p-6">
        <h2 className="text-xl font-bold text-white mb-4">Structural Code Chunking</h2>
        <p className="text-slate-400 mb-4">
          Unlike naive text splitting, this system uses <span className="text-green-400">tree-sitter</span> to
          chunk code at semantic boundaries (functions, classes) while respecting token limits.
        </p>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
          <div className="bg-red-900/30 border border-red-500/30 rounded-lg p-4">
            <h3 className="font-semibold text-red-400 mb-2">❌ Naive Text Chunking</h3>
            <pre className="text-xs text-slate-300 bg-slate-900 p-2 rounded">
{`def process_data():
    data = load()
    # ──────────────── CHUNK BREAK ────
    result = transform(data)
    return result  # Broken mid-function!`}
            </pre>
          </div>

          <div className="bg-green-900/30 border border-green-500/30 rounded-lg p-4">
            <h3 className="font-semibold text-green-400 mb-2">βœ“ Structural Chunking</h3>
            <pre className="text-xs text-slate-300 bg-slate-900 p-2 rounded">
{`# CHUNK 1 - Complete function
def process_data():
    data = load()
    result = transform(data)
    return result

# CHUNK 2 - Complete function
def another_func():
    ...`}
            </pre>
          </div>
        </div>

        <div className="bg-slate-700/50 rounded-lg p-4">
          <h3 className="font-semibold text-blue-400 mb-2">Chunking Algorithm</h3>
          <ol className="text-sm text-slate-300 space-y-2">
            <li className="flex items-start gap-2">
              <span className="bg-blue-500 text-white w-5 h-5 rounded-full flex items-center justify-center text-xs">1</span>
              <span>Parse file into AST using tree-sitter</span>
            </li>
            <li className="flex items-start gap-2">
              <span className="bg-blue-500 text-white w-5 h-5 rounded-full flex items-center justify-center text-xs">2</span>
              <span>Recursively visit nodes (functions, classes, etc.)</span>
            </li>
            <li className="flex items-start gap-2">
              <span className="bg-blue-500 text-white w-5 h-5 rounded-full flex items-center justify-center text-xs">3</span>
              <span>If node fits in max_tokens (800) β†’ return as chunk</span>
            </li>
            <li className="flex items-start gap-2">
              <span className="bg-blue-500 text-white w-5 h-5 rounded-full flex items-center justify-center text-xs">4</span>
              <span>If too large β†’ split into children, recurse</span>
            </li>
            <li className="flex items-start gap-2">
              <span className="bg-blue-500 text-white w-5 h-5 rounded-full flex items-center justify-center text-xs">5</span>
              <span>Merge neighboring small chunks to avoid fragments</span>
            </li>
          </ol>
        </div>
      </div>

      <div className="bg-slate-800 rounded-xl p-6">
        <h3 className="font-semibold text-white mb-3">Rich Chunk Metadata</h3>
        <div className="bg-slate-900 rounded-lg p-4">
          <pre className="text-sm text-slate-300">
{`FileChunk {
  file_path: "src/auth/login.py",
  start_byte: 245,
  end_byte: 892,
  line_range: "L12-L45",
  language: "python",
  chunk_type: "function_definition",
  name: "authenticate",

  // Enhanced metadata
  symbols_defined: ["authenticate", "verify_token"],
  imports_used: ["from jwt import decode"],
  complexity_score: 7,  // Cyclomatic complexity
  parent_context: "AuthService"  // Parent class
}`}
          </pre>
        </div>
      </div>
    </div>
  );

  const renderAgent = () => (
    <div className="space-y-6">
      <div className="bg-slate-800 rounded-xl p-6">
        <h2 className="text-xl font-bold text-white mb-4">Agentic Workflow (LangGraph)</h2>
        <p className="text-slate-400 mb-4">
          The agent can perform multi-step reasoning using tools, enabling complex analysis that
          simple RAG cannot handle.
        </p>

        <div className="bg-slate-900 rounded-lg p-4 mb-6">
          <h3 className="font-semibold text-purple-400 mb-3">Agent State Machine</h3>
          <div className="flex flex-col items-center space-y-2">
            <div className="bg-green-600/30 text-green-300 px-4 py-2 rounded-lg">START</div>
            <ArrowRight className="w-4 h-4 text-slate-500 rotate-90" />
            <div className="bg-blue-600/30 text-blue-300 px-6 py-3 rounded-lg text-center">
              <div className="font-semibold">AGENT NODE</div>
              <div className="text-xs mt-1">Process messages β†’ Call LLM β†’ Decide action</div>
            </div>
            <div className="flex items-center gap-4">
              <div className="flex flex-col items-center">
                <span className="text-xs text-slate-400">tool_call?</span>
                <ArrowRight className="w-4 h-4 text-slate-500 rotate-90" />
                <div className="bg-yellow-600/30 text-yellow-300 px-4 py-2 rounded-lg text-center">
                  <div className="font-semibold">TOOLS NODE</div>
                  <div className="text-xs">Execute tools</div>
                </div>
              </div>
              <div className="flex flex-col items-center">
                <span className="text-xs text-slate-400">final answer?</span>
                <ArrowRight className="w-4 h-4 text-slate-500 rotate-90" />
                <div className="bg-red-600/30 text-red-300 px-4 py-2 rounded-lg">END</div>
              </div>
            </div>
          </div>
        </div>

        <div className="grid grid-cols-2 md:grid-cols-3 gap-3">
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-green-400 text-sm">search_codebase</code>
            <p className="text-xs text-slate-400 mt-1">Vector search in codebase</p>
          </div>
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-blue-400 text-sm">read_file</code>
            <p className="text-xs text-slate-400 mt-1">Read complete file content</p>
          </div>
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-purple-400 text-sm">list_files</code>
            <p className="text-xs text-slate-400 mt-1">Directory listing</p>
          </div>
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-yellow-400 text-sm">find_callers</code>
            <p className="text-xs text-slate-400 mt-1">Who calls this function?</p>
          </div>
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-red-400 text-sm">find_callees</code>
            <p className="text-xs text-slate-400 mt-1">What does this call?</p>
          </div>
          <div className="bg-slate-700 rounded-lg p-3">
            <code className="text-pink-400 text-sm">find_call_chain</code>
            <p className="text-xs text-slate-400 mt-1">Trace execution path</p>
          </div>
        </div>
      </div>

      <div className="bg-slate-800 rounded-xl p-6">
        <h3 className="font-semibold text-white mb-3">Example Agent Execution</h3>
        <div className="space-y-3 text-sm">
          <div className="bg-slate-700 rounded p-3">
            <span className="text-blue-400">User:</span>
            <span className="text-slate-300 ml-2">"How does login handle invalid passwords?"</span>
          </div>
          <div className="bg-slate-700 rounded p-3">
            <span className="text-purple-400">Agent Thought:</span>
            <span className="text-slate-300 ml-2">I need to find the login function first.</span>
            <div className="mt-1">
              <span className="text-yellow-400">β†’ Tool Call:</span>
              <code className="text-green-300 ml-2">search_codebase("login authentication")</code>
            </div>
          </div>
          <div className="bg-slate-700 rounded p-3">
            <span className="text-purple-400">Agent Thought:</span>
            <span className="text-slate-300 ml-2">Found authenticate(). Let me see full implementation.</span>
            <div className="mt-1">
              <span className="text-yellow-400">β†’ Tool Call:</span>
              <code className="text-green-300 ml-2">read_file("src/auth/login.py")</code>
            </div>
          </div>
          <div className="bg-slate-700 rounded p-3">
            <span className="text-purple-400">Agent Thought:</span>
            <span className="text-slate-300 ml-2">It calls verify_password(). Let me check that.</span>
            <div className="mt-1">
              <span className="text-yellow-400">β†’ Tool Call:</span>
              <code className="text-green-300 ml-2">find_callees("authenticate")</code>
            </div>
          </div>
          <div className="bg-green-700/50 rounded p-3">
            <span className="text-green-400">Final Answer:</span>
            <span className="text-slate-300 ml-2">The login handles invalid passwords by...</span>
          </div>
        </div>
      </div>
    </div>
  );

  const renderRetrieval = () => (
    <div className="space-y-6">
      <div className="bg-slate-800 rounded-xl p-6">
        <h2 className="text-xl font-bold text-white mb-4">Multi-Stage Retrieval System</h2>

        <div className="space-y-4">
          <div className="bg-green-900/30 border-l-4 border-green-500 rounded-r-lg p-4">
            <h3 className="font-semibold text-green-400">Stage 1: Vector Retrieval (k=10)</h3>
            <p className="text-slate-300 text-sm">Semantic similarity search in Chroma/FAISS using embeddings</p>
          </div>

          <div className="bg-blue-900/30 border-l-4 border-blue-500 rounded-r-lg p-4">
            <h3 className="font-semibold text-blue-400">Stage 2: LLM File Selection</h3>
            <p className="text-slate-300 text-sm">LLM analyzes file tree structure and selects relevant files</p>
          </div>

          <div className="bg-purple-900/30 border-l-4 border-purple-500 rounded-r-lg p-4">
            <h3 className="font-semibold text-purple-400">Stage 3: Ensemble Combination</h3>
            <p className="text-slate-300 text-sm">Weighted merge: 60% vector + 40% LLM selection</p>
          </div>

          <div className="bg-yellow-900/30 border-l-4 border-yellow-500 rounded-r-lg p-4">
            <h3 className="font-semibold text-yellow-400">Stage 4: Graph Enhancement</h3>
            <p className="text-slate-300 text-sm">Add related files from AST graph (imports, calls)</p>
          </div>

          <div className="bg-red-900/30 border-l-4 border-red-500 rounded-r-lg p-4">
            <h3 className="font-semibold text-red-400">Stage 5: Cross-Encoder Reranking</h3>
            <p className="text-slate-300 text-sm">Score each (query, doc) pair, return top 5</p>
          </div>
        </div>
      </div>

      <div className="bg-slate-800 rounded-xl p-6">
        <h3 className="font-semibold text-white mb-3">Vector DB Support</h3>
        <div className="grid grid-cols-3 gap-4">
          <div className="bg-slate-700 rounded-lg p-4 text-center">
            <Database className="w-8 h-8 text-green-400 mx-auto mb-2" />
            <div className="font-semibold text-white">Chroma</div>
            <div className="text-xs text-slate-400">Default, local</div>
          </div>
          <div className="bg-slate-700 rounded-lg p-4 text-center">
            <Database className="w-8 h-8 text-blue-400 mx-auto mb-2" />
            <div className="font-semibold text-white">FAISS</div>
            <div className="text-xs text-slate-400">Fallback, fast</div>
          </div>
          <div className="bg-slate-700 rounded-lg p-4 text-center">
            <Database className="w-8 h-8 text-purple-400 mx-auto mb-2" />
            <div className="font-semibold text-white">Qdrant</div>
            <div className="text-xs text-slate-400">Cloud option</div>
          </div>
        </div>
      </div>
    </div>
  );

  return (
    <div className="min-h-screen bg-slate-900 text-white p-6">
      <div className="max-w-6xl mx-auto">
        <h1 className="text-3xl font-bold text-center mb-2">πŸ•·οΈ Code Crawler Architecture</h1>
        <p className="text-slate-400 text-center mb-6">Interactive System Documentation</p>

        <div className="flex flex-wrap gap-2 mb-6 justify-center">
          {tabs.map(tab => (
            <button
              key={tab.id}
              onClick={() => setActiveTab(tab.id)}
              className={`flex items-center gap-2 px-4 py-2 rounded-lg transition-all ${
                activeTab === tab.id
                  ? 'bg-purple-600 text-white'
                  : 'bg-slate-800 text-slate-400 hover:bg-slate-700'
              }`}
            >
              <tab.icon className="w-4 h-4" />
              {tab.label}
            </button>
          ))}
        </div>

        <div className="transition-all">
          {activeTab === 'overview' && renderOverview()}
          {activeTab === 'rag' && renderRAG()}
          {activeTab === 'ast' && renderAST()}
          {activeTab === 'chunking' && renderChunking()}
          {activeTab === 'agent' && renderAgent()}
          {activeTab === 'retrieval' && renderRetrieval()}
        </div>
      </div>
    </div>
  );
};

export default ArchitectureViz;