File size: 35,493 Bytes
45fa202
d231444
 
f6ad957
da206c8
 
 
 
 
 
 
 
 
 
 
d231444
 
 
da206c8
 
d242052
 
 
da206c8
 
 
45fa202
 
 
 
 
d231444
da206c8
 
 
 
 
 
 
 
45fa202
 
 
 
d242052
 
 
 
 
 
 
45fa202
 
d242052
 
 
 
 
da206c8
d231444
da206c8
 
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d242052
d231444
d242052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05251fc
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d242052
 
 
d231444
 
 
 
 
 
 
d242052
 
 
 
 
 
 
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05251fc
 
d231444
 
 
05251fc
d231444
05251fc
da206c8
d231444
da206c8
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da206c8
05251fc
da206c8
d231444
 
05251fc
da206c8
d231444
05251fc
 
da206c8
d231444
05251fc
d231444
05251fc
 
 
da206c8
 
d231444
 
da206c8
 
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d231444
da206c8
 
 
 
 
 
d231444
da206c8
 
d242052
 
 
 
 
da206c8
45fa202
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
d231444
da206c8
d231444
45fa202
d231444
d242052
45fa202
 
d231444
 
 
 
45fa202
 
05251fc
da206c8
d231444
da206c8
d231444
da206c8
d231444
33a4fd2
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d231444
33a4fd2
 
 
d231444
33a4fd2
05251fc
33a4fd2
 
 
 
 
 
 
 
 
 
 
 
 
05251fc
da206c8
d231444
da206c8
33a4fd2
 
 
 
da206c8
d231444
da206c8
 
d231444
45fa202
 
 
 
d231444
 
 
 
 
 
 
45fa202
da206c8
d231444
da206c8
 
45fa202
d231444
45fa202
 
d231444
 
d242052
d231444
45fa202
d242052
45fa202
 
 
da206c8
45fa202
d242052
d231444
45fa202
da206c8
 
 
d231444
45fa202
 
da206c8
 
 
 
 
 
 
 
 
 
 
d231444
da206c8
 
 
 
 
 
 
 
 
 
 
45fa202
 
 
 
 
 
 
 
da206c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45fa202
da206c8
 
d242052
da206c8
 
 
 
 
 
 
 
 
 
 
d242052
 
 
 
 
 
 
 
 
 
 
da206c8
 
 
d242052
45fa202
 
 
d242052
 
 
 
 
 
 
 
 
 
 
 
 
d231444
 
 
 
 
 
 
 
d242052
d231444
 
 
 
 
778e84b
d231444
 
 
 
 
 
45fa202
 
d231444
45fa202
 
 
 
 
 
d231444
 
 
 
 
 
 
 
 
 
45fa202
 
d231444
 
 
d242052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d231444
 
 
 
45fa202
 
d231444
 
 
 
 
 
 
 
 
 
 
 
 
da206c8
45fa202
d231444
45fa202
d231444
45fa202
05251fc
d231444
 
da206c8
45fa202
d231444
 
45fa202
 
 
d231444
da206c8
d231444
 
 
 
 
 
da206c8
d231444
 
da206c8
 
 
 
d231444
 
 
05251fc
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da206c8
d231444
 
 
da206c8
 
d231444
da206c8
 
 
 
 
d231444
da206c8
 
 
 
 
 
 
 
 
 
 
 
 
 
d231444
 
da206c8
d231444
da206c8
 
 
d231444
da206c8
d231444
 
da206c8
 
d231444
 
 
 
 
 
 
 
 
 
 
da206c8
 
d231444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05251fc
da206c8
 
 
 
 
 
 
05251fc
da206c8
 
 
45fa202
da206c8
45fa202
d231444
da206c8
d231444
45fa202
d231444
 
 
45fa202
05251fc
d231444
45fa202
d231444
 
 
 
45fa202
 
 
da206c8
 
d231444
da206c8
 
 
 
 
33a4fd2
45fa202
33a4fd2
da206c8
33a4fd2
 
 
 
 
 
 
 
 
 
 
 
da206c8
 
 
33a4fd2
da206c8
 
33a4fd2
d231444
 
da206c8
 
 
 
c34a74f
d231444
da206c8
d231444
 
 
 
 
da206c8
 
 
 
45fa202
 
 
 
 
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
import React, { useState, useRef, useEffect } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { apiUrl } from '../../lib/api';
import {
  Send,
  MessageSquareCode,
  Sparkles,
  Bot,
  User,
  RefreshCw,
  FileCode,
  AlertTriangle,
  ChevronDown,
  ChevronUp,
  Copy,
  CheckCircle2,
  StopCircle,
} from 'lucide-react';

import { tokenizeCode, type Token } from '../../lib/tokenizer';
import { sanitizeMarkdown } from '../../lib/sanitizer';

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

interface Message {
  id: string;
  sender: 'user' | 'assistant';
  text: string;
  timestamp?: string;
  /** Sources attached to this assistant message (from the done event). */
  sources?: string[];
  /** 0-100 confidence score attached to this assistant message. */
  confidence?: number;
  /** True when the LLM was unavailable and we fell back to retrieval-only. */
  fallbackMode?: boolean;
  /** True when this is an inline error (no_repo_selected, etc.). */
  isError?: boolean;
}

interface ChatInterfaceProps {
  repoName: string;
  /**
   * A question pushed in from elsewhere in the dashboard (e.g. "Ask about this
   * file" in the reading path). The `token` allows the same text to be sent
   * again. Consumed once, then reported back via `onPendingPromptConsumed`.
   */
  pendingPrompt?: { text: string; token: number } | null;
  onPendingPromptConsumed?: () => void;
}

const createChatSessionId = (): string =>
  typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
    ? crypto.randomUUID()
    : `${Date.now()}-${Math.random().toString(36).slice(2)}`;

// ---------------------------------------------------------------------------
// Custom Sub-components for Markdown & Layout
// ---------------------------------------------------------------------------

/** Custom helper to inject a blinking cursor element where %%CURSOR%% is found */
const renderChildrenWithCursor = (children: React.ReactNode): React.ReactNode => {
  if (typeof children === 'string' && children.includes('%%CURSOR%%')) {
    const parts = children.split('%%CURSOR%%');
    return (
      <>
        {parts[0]}
        <span className="inline-block h-3.5 w-1.5 ml-1 bg-primary animate-blink align-middle" />
        {parts[1]}
      </>
    );
  }
  if (Array.isArray(children)) {
    return React.Children.map(children, (child) => renderChildrenWithCursor(child));
  }
  return children;
};

/** Safe Token Syntax Renderer - Converts AST tokens into native React elements */
const SafeSyntaxHighlighter: React.FC<{ codeText: string }> = ({ codeText }) => {
  const cleanText = codeText.includes('%%CURSOR%%') ? codeText.split('%%CURSOR%%')[0] : codeText;
  const hasCursor = codeText.includes('%%CURSOR%%');

  const tokens = tokenizeCode(cleanText);

  const getTokenClassName = (type: Token['type']): string => {
    switch (type) {
      case 'comment':
        return 'text-text-subtle italic';
      case 'keyword':
        return 'text-indigo-400 font-semibold';
      case 'string':
        return 'text-emerald-400';
      case 'number':
        return 'text-amber-400';
      case 'operator':
        return 'text-primary/80';
      default:
        return '';
    }
  };

  return (
    <>
      {tokens.map((t, idx) => (
        <span key={idx} className={getTokenClassName(t.type)}>
          {t.text}
        </span>
      ))}
      {hasCursor && <span className="inline-block h-3.5 w-1.5 ml-1 bg-primary animate-blink align-middle" />}
    </>
  );
};

/** Premium CodeBlock with safe syntax highlighting, copy button, and wrap options */
const CodeBlock: React.FC<{ inline?: boolean; className?: string; children: React.ReactNode }> = ({
  inline,
  className,
  children,
}) => {
  const [copied, setCopied] = useState(false);
  const [wrapLines, setWrapLines] = useState(false);
  const match = /language-(\w+)/.exec(className || '');
  const lang = match ? match[1] : '';
  const codeText = String(children).replace(/\n$/, '');

  if (inline) {
    return (
      <code className="bg-surface-3 px-1.5 py-0.5 rounded text-xs text-primary font-mono select-all">
        {children}
      </code>
    );
  }

  const handleCopy = () => {
    navigator.clipboard.writeText(codeText.includes('%%CURSOR%%') ? codeText.split('%%CURSOR%%')[0] : codeText);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <div className="my-3 border border-border rounded-xl overflow-hidden bg-surface-1 shadow-card flex flex-col w-full fade-up">
      <div className="flex items-center justify-between px-4 py-2.5 bg-surface-2 border-b border-border/80 text-[10px] font-mono text-text-muted select-none shrink-0">
        <div className="flex items-center gap-2">
          <span className="h-2 w-2 rounded-full bg-primary/70"></span>
          <span>{lang || 'code'}</span>
        </div>
        <div className="flex items-center gap-3.5">
          <button
            type="button"
            onClick={() => setWrapLines((v) => !v)}
            className="hover:text-text transition-colors"
          >
            {wrapLines ? 'unwrap' : 'wrap'}
          </button>
          <button
            type="button"
            onClick={handleCopy}
            className="flex items-center gap-1 hover:text-text transition-colors font-semibold"
          >
            {copied ? (
              <>
                <CheckCircle2 className="h-3 w-3 text-emerald-500" />
                <span className="text-emerald-500">Copied!</span>
              </>
            ) : (
              <>
                <Copy className="h-3 w-3" />
                <span>Copy</span>
              </>
            )}
          </button>
        </div>
      </div>
      <pre className={`p-4 overflow-x-auto text-xs font-mono leading-relaxed bg-surface-1/40 ${wrapLines ? 'whitespace-pre-wrap break-all' : 'whitespace-pre'}`}>
        <code>
          <SafeSyntaxHighlighter codeText={codeText} />
        </code>
      </pre>
    </div>
  );
};

/** Markdown Component mapping GFM styles to Tailwind classes */
const Markdown: React.FC<{ content: string; isStreaming: boolean }> = ({ content, isStreaming }) => {
  const sanitized = sanitizeMarkdown(content);
  const textWithCursor = isStreaming ? sanitized + ' %%CURSOR%%' : sanitized;

  // Debug logging when DEBUG_RENDERING is set
  if (typeof window !== 'undefined' && (window as any).DEBUG_RENDERING) {
    console.log('[DEBUG_RENDERING]', { raw: content, sanitized, textWithCursor });
  }

  return (
    <ReactMarkdown
      remarkPlugins={[remarkGfm]}
      components={{
        code: ({ className, children }) => {
          const match = /language-(\w+)/.exec(className || '');
          const isInline = !match;
          return (
            <CodeBlock inline={isInline} className={className}>
              {children}
            </CodeBlock>
          );
        },
        table: ({ children }) => (
          <div className="overflow-x-auto my-3 border border-border rounded-xl bg-surface-1/25 shadow-card max-w-full">
            <table className="w-full text-left text-xs border-collapse font-sans leading-relaxed">{children}</table>
          </div>
        ),
        thead: ({ children }) => <thead className="bg-surface-2 border-b border-border text-[9px] font-mono font-bold text-text-muted uppercase tracking-wider select-none">{children}</thead>,
        tbody: ({ children }) => <tbody className="divide-y divide-border/40 bg-surface-1/10">{children}</tbody>,
        tr: ({ children }) => <tr className="hover:bg-primary/5 transition-colors">{children}</tr>,
        th: ({ children }) => <th className="p-3 font-semibold text-text-muted">{children}</th>,
        td: ({ children }) => <td className="p-3 text-text/90 font-mono break-all">{children}</td>,
        ul: ({ children }) => <ul className="list-disc pl-5 my-2 space-y-1.5 font-sans">{children}</ul>,
        ol: ({ children }) => <ol className="list-decimal pl-5 my-2 space-y-1.5 font-sans">{children}</ol>,
        li: ({ children }) => <li className="leading-relaxed">{renderChildrenWithCursor(children)}</li>,
        p: ({ children }) => <p className="leading-relaxed mb-3.5 break-words text-text/95 last:mb-0">{renderChildrenWithCursor(children)}</p>,
        h1: ({ children }) => <h1 className="text-base font-bold border-b border-border/50 pb-1.5 mt-5 mb-2.5 text-text font-mono uppercase tracking-wider">{children}</h1>,
        h2: ({ children }) => <h2 className="text-sm font-bold mt-4.5 mb-2 text-text font-mono">{children}</h2>,
        h3: ({ children }) => <h3 className="text-xs font-bold mt-4 mb-1.5 text-text-muted font-mono">{children}</h3>,
        blockquote: ({ children }) => <blockquote className="border-l-4 border-primary/40 bg-primary/5 pl-4 py-2.5 my-3 rounded-r-lg italic text-text-muted">{children}</blockquote>,
        a: ({ href, children }) => <a href={href} target="_blank" rel="noopener noreferrer" className="text-primary hover:underline font-semibold">{children}</a>,
      }}
    >
      {textWithCursor}
    </ReactMarkdown>
  );
};

/** Streaming Typing indicator */
const TypingIndicator: React.FC = () => (
  <div className="flex items-center gap-1 py-1 select-none text-text-muted shrink-0" aria-label="Thinking">
    <span className="h-1.5 w-1.5 rounded-full bg-primary animate-bounce-slow" />
    <span className="h-1.5 w-1.5 rounded-full bg-primary animate-bounce-slow [animation-delay:0.2s]" />
    <span className="h-1.5 w-1.5 rounded-full bg-primary animate-bounce-slow [animation-delay:0.4s]" />
  </div>
);

/** Citations, Grounded Evidence, and Structural Sources container */
const SourcesPanel: React.FC<{ sources: string[]; confidence: number; fallbackMode: boolean; activeRepo?: string }> = ({
  sources,
  confidence,
  fallbackMode,
  activeRepo,
}) => {
  const [open, setOpen] = useState(true);

  const displayConfidence = Math.min(Math.max(Math.round(confidence), 0), 100);

  const handleNavigateToGraph = (filePath: string) => {
    if (typeof window !== 'undefined') {
      const [owner, repo] = (activeRepo || '').split('/');
      window.dispatchEvent(
        new CustomEvent('aria-open-graph', {
          detail: {
            owner: owner || undefined,
            repo: repo || undefined,
            file: filePath,
            path: filePath,
            source: 'chat',
          },
        })
      );
      // If we are on standalone /chat, offer direct link
      if (window.location.pathname === '/chat' && activeRepo) {
        if (owner && repo) {
          window.location.href = `/analysis?owner=${owner}&repo=${repo}&tab=graph&file=${encodeURIComponent(filePath)}`;
        }
      }
    }
  };

  return (
    <div className="border border-white/[0.08] rounded-xl overflow-hidden bg-surface-1 text-[11px] font-sans shadow-card fade-up select-none">
      <button
        type="button"
        onClick={() => setOpen((o) => !o)}
        className="w-full flex items-center justify-between px-3.5 py-2.5 bg-surface-2/70 hover:bg-surface-2 transition-colors focus-visible:outline-none border-b border-white/[0.04]"
      >
        <div className="flex items-center gap-3">
          <span className="font-mono text-xs font-bold text-text flex items-center gap-1.5">
            <FileCode className="h-3.5 w-3.5 text-primary" /> Grounded Evidence ({sources.length})
          </span>
          {fallbackMode ? (
            <span className="text-amber-400 font-mono text-[10px] font-semibold">Fallback Mode</span>
          ) : (
            <span className="text-text-subtle text-[10px] font-mono">
              Retrieval Grounding:{' '}
              <span className="font-bold text-emerald-400">{displayConfidence}% Confidence</span>
            </span>
          )}
        </div>
        {open ? <ChevronUp className="h-3.5 w-3.5 text-text-muted" /> : <ChevronDown className="h-3.5 w-3.5 text-text-muted" />}
      </button>

      {open && (
        <div className="p-3 bg-canvas/60 space-y-2 max-h-56 overflow-y-auto">
          <div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
            {sources.map((src, i) => {
              const fileName = src.split('/').pop() || src;
              const dir = src.includes('/') ? src.substring(0, src.lastIndexOf('/')) : '';
              return (
                <div
                  key={i}
                  className="flex items-center justify-between gap-2 p-2 rounded-lg border border-white/[0.06] bg-surface-1/90 hover:border-primary/40 transition-all group"
                >
                  <div className="min-w-0 pr-1 flex items-center gap-2">
                    <FileCode className="h-3.5 w-3.5 text-primary/70 shrink-0 group-hover:text-primary transition-colors" />
                    <div className="truncate">
                      <p className="font-mono text-[11px] font-semibold text-text truncate" title={src}>
                        {fileName}
                      </p>
                      {dir && (
                        <p className="font-mono text-[9px] text-text-subtle truncate" title={dir}>
                          {dir}/
                        </p>
                      )}
                    </div>
                  </div>
                  <button
                    type="button"
                    onClick={() => handleNavigateToGraph(src)}
                    className="shrink-0 text-[9px] font-mono font-semibold px-2 py-1 rounded bg-primary/10 border border-primary/20 text-primary hover:bg-primary hover:text-white transition-colors"
                    title={`View ${fileName} in Structural Graph`}
                  >
                    View in Graph →
                  </button>
                </div>
              );
            })}
          </div>
        </div>
      )}
    </div>
  );
};

// ---------------------------------------------------------------------------
// Main Component
// ---------------------------------------------------------------------------

export const ChatInterface: React.FC<ChatInterfaceProps> = ({
  repoName,
  pendingPrompt,
  onPendingPromptConsumed,
}) => {
  const [messages, setMessages] = useState<Message[]>([]);
  const [input, setInput] = useState('');
  const [isStreaming, setIsStreaming] = useState(false);
  const [activeRepo, setActiveRepo] = useState<string>(() => {
    if (repoName) return repoName;
    if (typeof window !== 'undefined') {
      const urlParams = new URLSearchParams(window.location.search);
      const owner = urlParams.get('owner');
      const repo = urlParams.get('repo');
      if (owner && repo) return `${owner}/${repo}`;
      if (urlParams.get('repo')) return urlParams.get('repo')!;
      const stored = localStorage.getItem('activeRepo');
      if (stored) return stored;
    }
    return '';
  });
  const [copiedAnswerId, setCopiedAnswerId] = useState<string | null>(null);
  const [noRepoError, setNoRepoError] = useState(false);

  const bottomRef = useRef<HTMLDivElement>(null);
  const abortControllerRef = useRef<AbortController | null>(null);
  const sessionIdRef = useRef<string>(createChatSessionId());

  const suggestedQuestions = [
    'What does this codebase do, and what are its main entry points?',
    'Show me any circular dependencies in this project.',
    'Are there any dead/unused functions, and where are they located?',
    'What is the architectural overview of this repository?',
  ];

  // Sync active repo changes from prop
  useEffect(() => {
    if (repoName) {
      setActiveRepo(repoName);
      setNoRepoError(false);
    }
  }, [repoName]);

  // Fallback: If still no activeRepo on mount, look up recent analyzed repos from the backend
  useEffect(() => {
    if (!activeRepo) {
      fetch(apiUrl('/api/v1/repos/recent'))
        .then((res) => res.json())
        .then((data) => {
          if (Array.isArray(data) && data.length > 0 && data[0].name) {
            const foundRepo = data[0].name;
            setActiveRepo(foundRepo);
            if (typeof window !== 'undefined') {
              localStorage.setItem('activeRepo', foundRepo);
              window.dispatchEvent(new CustomEvent('active-repo-changed', { detail: foundRepo }));
            }
          } else {
            setNoRepoError(true);
          }
        })
        .catch(() => {
          setNoRepoError(true);
        });
    }
  }, [activeRepo]);

  // Window events
  useEffect(() => {
    const handleRepoChanged = (e: Event) => {
      const customEvent = e as CustomEvent<string>;
      if (customEvent.detail) {
        setActiveRepo(customEvent.detail);
        setNoRepoError(false);
      }
    };
    const handleRepoCleared = () => {
      setActiveRepo('');
      setNoRepoError(true);
    };

    window.addEventListener('active-repo-changed', handleRepoChanged);
    window.addEventListener('active-repo-cleared', handleRepoCleared);
    return () => {
      window.removeEventListener('active-repo-changed', handleRepoChanged);
      window.removeEventListener('active-repo-cleared', handleRepoCleared);
    };
  }, []);

  // Reset conversation on active repo change
  useEffect(() => {
    if (!activeRepo) {
      setMessages([]);
      return;
    }
    setNoRepoError(false);
    setMessages([]);
  }, [activeRepo]);

  // Scroll bottom
  useEffect(() => {
    bottomRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [messages]);

  const handleCopyAnswer = (text: string, id: string) => {
    navigator.clipboard.writeText(text);
    setCopiedAnswerId(id);
    setTimeout(() => setCopiedAnswerId(null), 2000);
  };

  const triggerStreamResponse = async (userPrompt: string, updatedHistory: Message[]) => {
    setIsStreaming(true);
    const assistantId = Math.random().toString();
    const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });

    setMessages((prev) => [
      ...prev,
      { id: assistantId, sender: 'assistant', text: '', timestamp },
    ]);

    const controller = new AbortController();
    abortControllerRef.current = controller;
    let accumulatedText = '';

    try {
      const response = await fetch(apiUrl('/api/v1/chat'), {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          repo: activeRepo,
          message: userPrompt,
          session_id: sessionIdRef.current,
          history: updatedHistory.map((m) => ({
            role: m.sender === 'user' ? 'user' : 'model',
            parts: [m.text],
          })),
        }),
        signal: controller.signal,
      });

      if (!response.ok) {
        let detail = `Backend error ${response.status}`;
        try {
          const errBody = await response.json();
          if (errBody?.detail) {
            if (Array.isArray(errBody.detail)) {
              detail = errBody.detail.map((d: any) => d.msg).join('; ');
            } else {
              detail = String(errBody.detail);
            }
          }
        } catch { /* ignore */ }
        setMessages((prev) =>
          prev.map((msg) =>
            msg.id === assistantId
              ? { ...msg, text: detail, isError: true }
              : msg,
          ),
        );
        return;
      }

      if (!response.body) throw new Error('ReadableStream not available');

      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let finished = false;

      while (!finished) {
        const { value, done } = await reader.read();
        finished = done;
        if (!value) continue;

        const raw = decoder.decode(value);
        const lines = raw.split('\n');

        for (const line of lines) {
          if (!line.startsWith('data: ')) continue;
          try {
            const data = JSON.parse(line.slice(6));

            if (data.error) {
              setMessages((prev) =>
                prev.map((msg) =>
                  msg.id === assistantId
                    ? { ...msg, text: data.message ?? data.error, isError: true }
                    : msg,
                ),
              );
              continue;
            }

            if (data.text) {
              accumulatedText += data.text;
              setMessages((prev) =>
                prev.map((msg) =>
                  msg.id === assistantId
                    ? { ...msg, text: msg.text + data.text }
                    : msg,
                ),
              );
            }

            if (data.status === 'done') {
              setMessages((prev) =>
                prev.map((msg) => {
                  if (msg.id !== assistantId) return msg;
                  const finalTxt = msg.text.trim() || accumulatedText.trim() || '⚠️ The AI provider returned an empty response. Repository intelligence fallback applied.';
                  return {
                    ...msg,
                    text: finalTxt,
                    sources: data.sources ?? [],
                    confidence: data.confidence ?? 0,
                    fallbackMode: data.fallback_mode ?? false,
                  };
                }),
              );
            }
          } catch {
            /* ignore JSON parse errors */
          }
        }
      }

      // Final check: if stream finished cleanly but accumulatedText is empty
      if (!accumulatedText.trim()) {
        setMessages((prev) =>
          prev.map((msg) =>
            msg.id === assistantId && !msg.text.trim()
              ? { ...msg, text: '⚠️ The AI provider returned an empty response. Repository intelligence fallback applied.', isError: true }
              : msg,
          ),
        );
      }
    } catch (err: any) {
      if (err.name === 'AbortError') {
        setMessages((prev) =>
          prev.map((msg) =>
            msg.id === assistantId
              ? { ...msg, text: msg.text + '\n\n*Stream generation stopped.*' }
              : msg,
          ),
        );
      } else {
        console.error('Chat stream error:', err);
        setMessages((prev) =>
          prev.map((msg) =>
            msg.id === assistantId
              ? {
                  ...msg,
                  text: 'Could not reach the agent backend. Please verify that the backend server is running and reachable.',
                  isError: true,
                }
              : msg,
          ),
        );
      }
    } finally {
      setIsStreaming(false);
      abortControllerRef.current = null;
    }
  };

  const handleSend = async (textToSend: string) => {
    if (!textToSend.trim() || isStreaming) return;

    const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
    const userMessage: Message = {
      id: Math.random().toString(),
      sender: 'user',
      text: textToSend,
      timestamp,
    };
    const nextHistory = [...messages, userMessage];
    
    setMessages(nextHistory);
    setInput('');

    await triggerStreamResponse(textToSend, nextHistory);
  };

  /**
   * Dispatches a question handed in from another panel. Kept as a thin trigger
   * around the existing send path so request/streaming behaviour is unchanged.
   */
  const consumedPromptTokenRef = useRef<number | null>(null);
  useEffect(() => {
    if (!pendingPrompt?.text) return;
    if (consumedPromptTokenRef.current === pendingPrompt.token) return;
    if (isStreaming) return;

    consumedPromptTokenRef.current = pendingPrompt.token;
    onPendingPromptConsumed?.();
    void handleSend(pendingPrompt.text);
    // handleSend is recreated each render; depending on it would re-fire the
    // effect, so the token guard above is what makes this run exactly once.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [pendingPrompt?.text, pendingPrompt?.token, isStreaming]);

  const handleStop = () => {
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
    }
  };

  const handleRegenerate = async () => {
    const userMsgs = messages.filter((m) => m.sender === 'user');
    if (userMsgs.length === 0) return;
    const lastUserMsg = userMsgs[userMsgs.length - 1];

    let nextMsgs = [...messages];
    if (nextMsgs[nextMsgs.length - 1].sender === 'assistant') {
      nextMsgs.pop();
    }

    setMessages(nextMsgs);
    await triggerStreamResponse(lastUserMsg.text, nextMsgs);
  };

  return (
    <div className="flex flex-col min-h-[550px] h-full border border-border bg-card/10 rounded-xl overflow-hidden w-full shadow-float">
      {/* Header */}
      <div className="flex items-center justify-between px-5 py-3.5 border-b border-border bg-card/45 shrink-0 select-none">
        <div className="flex items-center gap-2">
          <MessageSquareCode className="h-4 w-4 text-primary" />
          <span className="font-mono text-xs font-bold text-text uppercase tracking-wider">
            Codebase Companion
          </span>
        </div>
        <span className="text-[10px] font-mono border border-primary/25 text-primary px-2.5 py-0.5 rounded-md bg-primary/5 truncate max-w-[200px]">
          {activeRepo || 'no repo'}
        </span>
      </div>

      {/* No-repo state */}
      {noRepoError && (
        <div className="flex-grow flex flex-col items-center justify-center p-8 gap-3.5 text-center fade-up">
          <div className="h-12 w-12 rounded-full bg-amber-500/10 border border-amber-500/25 flex items-center justify-center text-amber-500 select-none">
            <AlertTriangle className="h-6 w-6" />
          </div>
          <p className="text-sm font-semibold text-text">
            No active repository found
          </p>
          <p className="text-xs text-text-muted max-w-xs leading-relaxed font-sans">
            Please analyze a repository on the homepage to start asking questions about its codebase.
          </p>
        </div>
      )}

      {/* Empty State Welcome Dashboard */}
      {!noRepoError && messages.length === 0 && (
        <div className="flex-grow flex flex-col items-center justify-center p-6 gap-6 text-center max-w-xl mx-auto my-auto fade-up">
          <div className="h-12 w-12 rounded-full bg-primary/10 border border-primary/20 flex items-center justify-center text-primary select-none">
            <Sparkles className="h-6 w-6" />
          </div>
          <div className="space-y-2">
            <h2 className="text-base font-bold text-text font-mono">Chat with {activeRepo}</h2>
            <p className="text-xs text-text-muted leading-relaxed font-sans max-w-sm">
              Ask about function callers, class inheritances, endpoints, or planned code refactors.
            </p>
          </div>

          <div className="w-full text-left mt-3 select-none">
            <p className="text-[10px] uppercase font-mono tracking-widest text-text-subtle font-semibold mb-3 flex items-center gap-1.5 justify-center">
              Suggested Prompts
            </p>
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
              {suggestedQuestions.map((q) => (
                <button
                  key={q}
                  onClick={() => handleSend(q)}
                  className="card p-3 text-left hover:border-primary/40 hover:bg-primary/5 transition-all text-xs font-sans text-text-muted hover:text-text duration-200 group flex items-start gap-2.5"
                >
                  <MessageSquareCode className="h-4 w-4 text-primary/70 shrink-0 mt-0.5 group-hover:text-primary transition-colors" />
                  <span className="leading-relaxed">{q}</span>
                </button>
              ))}
            </div>
          </div>
        </div>
      )}

      {/* Message thread */}
      {!noRepoError && messages.length > 0 && (
        <div className="flex-grow overflow-y-auto p-5 space-y-5 font-sans text-xs">
          {messages.map((msg, idx) => (
            <div
              key={msg.id}
              className={`flex gap-3 fade-up ${
                msg.sender === 'user' ? 'ml-auto flex-row-reverse max-w-[85%]' : 'max-w-[92%]'
              }`}
            >
              {/* Avatar */}
              <div
                className={`h-8 w-8 rounded-full flex items-center justify-center shrink-0 border select-none ${
                  msg.sender === 'user'
                    ? 'bg-primary/10 border-primary/30 text-primary'
                    : msg.isError
                    ? 'bg-red-500/10 border-red-500/30 text-red-400'
                    : 'bg-card border-border text-text-muted'
                }`}
              >
                {msg.sender === 'user' ? (
                  <User className="h-4 w-4" />
                ) : (
                  <Bot className="h-4 w-4" />
                )}
              </div>

              {/* Bubble + Citations */}
              <div className="flex flex-col gap-1.5 min-w-0">
                <div
                  className={`p-3.5 rounded-xl border leading-relaxed break-words shadow-card ${
                    msg.sender === 'user'
                      ? 'bg-primary/10 border-primary/20 text-text'
                      : msg.isError
                      ? 'bg-red-500/5 border-red-500/20 text-red-400 font-mono'
                      : msg.fallbackMode
                      ? 'bg-amber-500/5 border-amber-500/20 text-text'
                      : 'bg-card/40 border-border text-text'
                  }`}
                >
                  {msg.sender === 'user' ? (
                    <p className="whitespace-pre-wrap leading-relaxed break-words text-text/95">
                      {msg.text}
                    </p>
                  ) : msg.text === '' ? (
                    <TypingIndicator />
                  ) : (
                    <div className="markdown-body font-sans text-[12px] overflow-hidden">
                      <Markdown content={msg.text} isStreaming={isStreaming && idx === messages.length - 1} />
                    </div>
                  )}
                </div>

                {/* Message Timestamp */}
                {msg.timestamp && (
                  <span className={`text-[8px] font-mono text-text-muted mt-0.5 px-1 select-none ${
                    msg.sender === 'user' ? 'self-end' : 'self-start'
                  }`}>
                    {msg.timestamp}
                  </span>
                )}

                {/* Toolbar under assistant bubble */}
                {msg.sender === 'assistant' && msg.text !== '' && (
                  <div className="flex items-center gap-3.5 mt-0.5 pl-1.5 text-[10px] text-text-muted select-none">
                    <button
                      type="button"
                      onClick={() => handleCopyAnswer(msg.text, msg.id)}
                      className="flex items-center gap-1 hover:text-text transition-colors"
                    >
                      {copiedAnswerId === msg.id ? (
                        <>
                          <CheckCircle2 className="h-3.5 w-3.5 text-emerald-500" />
                          <span className="text-emerald-500 font-semibold">Copied answer</span>
                        </>
                      ) : (
                        <>
                          <Copy className="h-3.5 w-3.5" />
                          <span>Copy answer</span>
                        </>
                      )}
                    </button>
                    
                    {idx === messages.length - 1 && !isStreaming && (
                      <button
                        type="button"
                        onClick={handleRegenerate}
                        className="flex items-center gap-1 hover:text-text transition-colors font-semibold"
                      >
                        <RefreshCw className="h-3.5 w-3.5" />
                        <span>Regenerate</span>
                      </button>
                    )}
                  </div>
                )}

                {/* Collapsible citations & grounded evidence panel */}
                {msg.sender === 'assistant' &&
                  msg.sources &&
                  msg.sources.length > 0 && (
                    <SourcesPanel
                      sources={msg.sources}
                      confidence={msg.confidence ?? 0}
                      fallbackMode={msg.fallbackMode ?? false}
                      activeRepo={activeRepo}
                    />
                  )}
              </div>
            </div>
          ))}

          {/* Inline Stop button while streaming */}
          {isStreaming && (
            <div className="flex items-center justify-center py-2 fade-up select-none">
              <button
                type="button"
                onClick={handleStop}
                className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-red-500/25 bg-red-500/5 text-red-500 hover:bg-red-500/10 text-[10px] font-mono uppercase tracking-wider font-bold transition-all focus-visible:outline-none"
              >
                <StopCircle className="h-3.5 w-3.5" />
                Stop Generation
              </button>
            </div>
          )}

          <div ref={bottomRef} />
        </div>
      )}

      {/* Input bar */}
      {!noRepoError && (
        <div className="p-3.5 border-t border-border bg-card/25 shrink-0">
          <form
            onSubmit={(e) => {
              e.preventDefault();
              handleSend(input);
            }}
            className="flex gap-2 items-end"
          >
            <textarea
              value={input}
              rows={1}
              onChange={(e) => {
                setInput(e.target.value);
                e.target.style.height = 'auto';
                e.target.style.height = Math.min(e.target.scrollHeight, 160) + 'px';
              }}
              onKeyDown={(e) => {
                if (e.key === 'Enter' && !e.shiftKey) {
                  e.preventDefault();
                  handleSend(input);
                }
              }}
              disabled={isStreaming || !activeRepo}
              placeholder={
                activeRepo
                  ? `Ask a question about ${activeRepo}… (Enter to send)`
                  : 'Waiting for repository...'
              }
              aria-label="Chat message"
              className="flex-grow bg-canvas border border-border rounded-lg px-3.5 py-2.5 text-xs focus:outline-none focus:border-primary focus:ring-1 focus:ring-primary font-sans text-text placeholder:text-text-muted/40 resize-none overflow-hidden leading-relaxed"
              style={{ minHeight: '40px' }}
            />
            <button
              type="submit"
              disabled={isStreaming || !input.trim() || !activeRepo}
              aria-label="Send message"
              className="bg-primary hover:bg-primary-hover text-text font-medium px-4 py-2.5 rounded-lg flex items-center gap-1.5 text-xs transition-all disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:shadow-ring shrink-0 hover:scale-[1.02] active:scale-[0.98] duration-150"
            >
              {isStreaming ? (
                <RefreshCw className="h-4 w-4 animate-spin text-text" aria-hidden="true" />
              ) : (
                <Send className="h-4 w-4" aria-hidden="true" />
              )}
            </button>
          </form>
        </div>
      )}
    </div>
  );
};

export default ChatInterface;