File size: 39,653 Bytes
5f0d937
8a55c98
 
3b28871
3bb9213
5f0d937
 
3b28871
5f0d937
3b28871
5f0d937
3b28871
3bb9213
3b28871
3bb9213
3b28871
 
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
3bb9213
3b28871
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
 
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
 
 
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
 
 
5f0d937
 
 
 
 
 
 
 
 
8a55c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
3bb9213
5f0d937
 
3bb9213
3b28871
 
 
3bb9213
 
 
5f0d937
 
3b28871
5f0d937
 
 
3bb9213
5f0d937
 
 
8a55c98
3b28871
 
5f0d937
 
 
 
 
 
 
3bb9213
 
5f0d937
 
 
 
 
 
 
 
8a55c98
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
5f0d937
 
8a55c98
 
 
 
 
 
 
5f0d937
 
3b28871
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a55c98
 
 
 
 
 
 
 
 
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
3bb9213
 
 
3b28871
3bb9213
 
 
 
 
 
 
 
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bb9213
 
 
 
 
 
 
3b28871
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
3bb9213
3b28871
 
 
3bb9213
3b28871
3bb9213
 
 
3b28871
 
 
 
 
 
 
3bb9213
3b28871
 
 
 
 
 
 
 
 
 
 
3bb9213
 
 
 
 
3b28871
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
3bb9213
 
 
 
 
 
3b28871
 
 
 
 
 
 
 
 
3bb9213
3b28871
3bb9213
3b28871
 
 
 
3bb9213
 
 
 
3b28871
 
 
3bb9213
 
 
 
 
 
 
 
 
3b28871
 
 
 
 
 
 
3bb9213
3b28871
 
 
 
 
 
 
 
 
 
 
 
3bb9213
3b28871
 
3bb9213
 
 
 
 
 
3b28871
 
 
 
 
3bb9213
 
3b28871
 
3bb9213
3b28871
 
 
 
3bb9213
3b28871
 
 
3bb9213
3b28871
 
 
 
 
 
5f0d937
3b28871
 
 
 
3bb9213
3b28871
 
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
 
3bb9213
 
3b28871
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bb9213
 
 
 
 
 
 
3b28871
 
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
3b28871
1feab6b
 
3bb9213
 
1feab6b
 
 
 
3bb9213
1feab6b
3bb9213
1feab6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
 
3bb9213
 
 
 
 
 
 
5f0d937
3bb9213
 
3b28871
3bb9213
3b28871
 
5f0d937
3bb9213
3b28871
 
 
 
3bb9213
 
 
 
 
 
8a55c98
 
 
 
 
 
 
 
5f0d937
 
8a55c98
 
 
 
 
 
1feab6b
3b28871
 
8a55c98
 
 
 
 
 
 
 
 
 
3b28871
 
5f0d937
 
 
 
 
 
3b28871
8a55c98
 
 
 
3b28871
 
3bb9213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
 
5f0d937
3b28871
 
5f0d937
 
 
 
 
 
3b28871
1feab6b
3b28871
3bb9213
3b28871
 
 
 
 
3bb9213
3b28871
 
 
 
5f0d937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b28871
 
5f0d937
 
 
 
 
8a55c98
 
 
5f0d937
 
 
 
3b28871
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import {
  AlertTriangle,
  ChevronDown,
  Download,
  MessageSquarePlus,
  Share2,
  RefreshCw,
  Search,
  Send,
  Settings,
  Square,
  Trash2,
} from 'lucide-react'

const STORAGE_PERSONA = 'askjerry_extra_persona'
const CONTEXT_WINDOW = 8192
const MAX_REPLY_TOKENS = 4096
const SUMMARIZE_THRESHOLD = 0.55

const DEFAULT_EXTRA_PERSONA = `You are Tawish Jerry Huaute, a Chumash man who spent 14+ years at Microsoft as a Senior Field Technical Account Manager starting in 1994. You grew up in Pomona, California. Your grandfather Semu Huaute (1908–2004) was a legendary Chumash medicine man, activist, and founder of the Red Wind Foundation who participated in the 1969 Alcatraz occupation. You are a member of Native Americans at Microsoft, mentoring Indigenous youth in tech careers.
Your core beliefs: "Don't let an opportunity pass you by. You need to invest in yourself." Keep learning, seize chances, and build your value through education and hands-on experience. You are proud of your Chumash heritage and see it as inseparable from your professional identity — not a contradiction.
Your tone is direct, warm, unpretentious, and encouraging. You speak from lived experience, not theory. You naturally mentor younger people, especially Native youth who may not see representation in tech. You know enterprise IT deeply — deployment, troubleshooting, account management, customer relationships, Microsoft technologies. You don't lead with credentials; you lead with real talk.
When discussing heritage, speak with quiet pride about your family and Chumash culture. When giving career advice, be practical and honest — you believe in people but expect them to put in the work.`

function initialExtraPersona() {
  try {
    const s = sessionStorage.getItem(STORAGE_PERSONA)
    if (s !== null) return s
  } catch { /* */ }
  return DEFAULT_EXTRA_PERSONA
}

function estimateTokens(text) {
  return Math.max(1, Math.ceil((text || '').length / 4))
}

function estimateMessagesTokens(msgs, systemPrompt, summary) {
  let total = estimateTokens(systemPrompt) + 4
  if (summary) total += estimateTokens(summary) + 8
  for (const m of msgs) total += estimateTokens(m.content) + 4
  return total
}

function parseSseBuffer(buf, onEvent) {
  const normalized = buf.replace(/\r\n/g, '\n')
  const idx = normalized.lastIndexOf('\n')
  if (idx === -1) return normalized
  const complete = normalized.slice(0, idx + 1)
  const remainder = normalized.slice(idx + 1)
  for (const line of complete.split('\n')) {
    if (!line.startsWith('data: ')) continue
    const raw = line.slice(6).trim()
    if (raw === '[DONE]') { onEvent({ type: 'done' }); continue }
    if (!raw) continue
    try { onEvent(JSON.parse(raw)) } catch { /* */ }
  }
  return remainder
}

function appendTokenContent(acc, ev) {
  if (ev.type !== 'token' || ev.content == null) return acc
  let piece = ev.content
  if (Array.isArray(piece)) {
    piece = piece.map((p) => (typeof p === 'string' ? p : p?.text ?? '')).join('')
  } else if (typeof piece !== 'string') {
    piece = String(piece)
  }
  return piece ? acc + piece : acc
}

function AssistantSearchBar({ content, show }) {
  const [open, setOpen] = useState(false)
  const [draft, setDraft] = useState('')
  const [loading, setLoading] = useState(false)
  const [copied, setCopied] = useState(false)
  const [placement, setPlacement] = useState('below')
  const [popoverStyle, setPopoverStyle] = useState(null)
  const wrapRef = useRef(null)

  const measurePopoverPosition = useCallback(() => {
    if (!open) {
      setPopoverStyle(null)
      return
    }
    const bubble = wrapRef.current?.closest('.aj-bubble-wrap')
    if (!bubble) return
    const br = bubble.getBoundingClientRect()
    const vw = window.visualViewport?.width ?? window.innerWidth
    const margin = 12
    const gap = 8
    const popoverW = Math.min(360, vw - margin * 2)
    const spaceRight = vw - br.right - margin
    const canPlaceRight = spaceRight >= popoverW + gap

    const vh = window.visualViewport?.height ?? window.innerHeight
    let style
    if (canPlaceRight) {
      setPlacement('right')
      const w = Math.min(popoverW, vw - br.right - gap - margin)
      style = { position: 'fixed', visibility: 'visible', bottom: vh - br.bottom, left: br.right + gap, width: w }
    } else {
      setPlacement('below')
      const w = Math.min(br.width, popoverW, vw - 2 * margin)
      const left = Math.max(margin, Math.min(br.left, vw - w - margin))
      style = { position: 'fixed', visibility: 'visible', top: br.bottom + gap, left, width: w }
    }
    setPopoverStyle(style)
  }, [open])

  useLayoutEffect(() => {
    measurePopoverPosition()
  }, [open, loading, draft, measurePopoverPosition])

  useEffect(() => {
    if (!open) return
    const onMove = () => measurePopoverPosition()
    window.addEventListener('resize', onMove)
    window.addEventListener('scroll', onMove, true)
    return () => {
      window.removeEventListener('resize', onMove)
      window.removeEventListener('scroll', onMove, true)
    }
  }, [open, measurePopoverPosition])

  useEffect(() => {
    if (!open) return
    const onDoc = (e) => {
      if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false)
    }
    document.addEventListener('mousedown', onDoc)
    return () => document.removeEventListener('mousedown', onDoc)
  }, [open])

  const openPanel = async () => {
    if (open) {
      setOpen(false)
      return
    }
    setOpen(true)
    setCopied(false)
    setLoading(true)
    setDraft('')
    const text = (content || '').toString().slice(0, 4000)
    try {
      const res = await fetch('/api/search-references', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ statement: text }),
      })
      const data = await res.json().catch(() => ({}))
      const q = (data.search_query || '').trim() || text.slice(0, 100)
      setDraft(`Provide citations for ${q}`)
    } catch {
      setDraft(`Provide citations for ${text.slice(0, 100)}`)
    } finally {
      setLoading(false)
    }
  }

  const copyDraft = () => {
    navigator.clipboard.writeText(draft).then(() => {
      setCopied(true)
      setTimeout(() => setCopied(false), 2000)
    }).catch(() => {})
  }

  const openPerplexity = () => {
    window.open(`https://www.perplexity.ai/?q=${encodeURIComponent(draft)}`, '_blank', 'noopener,noreferrer')
  }

  const openWebSearch = () => {
    window.open(`https://www.google.com/search?q=${encodeURIComponent(draft)}`, '_blank', 'noopener,noreferrer')
  }

  if (!show || !(content || '').trim()) return null

  return (
    <div className="aj-msg-search-anchor" ref={wrapRef}>
      <div className="aj-msg-search-wrap">
        <button
          type="button"
          className="aj-msg-search-btn"
          onClick={openPanel}
          title="Search"
        >
          <Search size={14} aria-hidden />
        </button>
      </div>
      {open && (
        <div
          className={`aj-msg-search-popover aj-msg-search-popover--${placement}`}
          style={popoverStyle ?? undefined}
          role="dialog"
          aria-label="Web search for citations and resources"
        >
          <div className="aj-msg-search-popover-head">
            <span>Web search for citations and resources</span>
            <button type="button" className="aj-msg-search-close" onClick={() => setOpen(false)} aria-label="Close">
              ×
            </button>
          </div>
          {loading ? (
            <div className="aj-msg-search-loading">Generating search query…</div>
          ) : (
            <>
              <textarea
                className="aj-msg-search-input"
                value={draft}
                onChange={(e) => setDraft(e.target.value)}
                rows={3}
                aria-label="Edit search query"
              />
              <div className="aj-msg-search-actions">
                <button type="button" className="aj-btn primary aj-msg-search-action" onClick={openPerplexity}>
                  Open in Perplexity
                </button>
                <button type="button" className="aj-btn aj-msg-search-action" onClick={copyDraft}>
                  {copied ? 'Copied!' : 'Copy'}
                </button>
                <button type="button" className="aj-btn aj-msg-search-action" onClick={openWebSearch}>
                  Search
                </button>
              </div>
            </>
          )}
        </div>
      )}
    </div>
  )
}

function AssistantBubbleContent({ content }) {
  return (
    <div className="aj-bubble-md">
      <ReactMarkdown
        remarkPlugins={[remarkGfm]}
        components={{
          a: ({ href, children, ...rest }) => (
            <a href={href} target="_blank" rel="noopener noreferrer" {...rest}>
              {children}
            </a>
          ),
        }}
      >
        {content}
      </ReactMarkdown>
    </div>
  )
}

export default function App() {
  const [optionsOpen, setOptionsOpen] = useState(false)
  const [shareOpen, setShareOpen] = useState(false)
  const [exportOpen, setExportOpen] = useState(false)
  const [extraPersona, setExtraPersona] = useState(initialExtraPersona)
  const [messages, setMessages] = useState([])
  const [input, setInput] = useState('')
  const [streaming, setStreaming] = useState(false)
  const [summary, setSummary] = useState(null)
  const [summarizing, setSummarizing] = useState(false)
  const [contextOverflow, setContextOverflow] = useState(null)
  const [hideTypingCursor, setHideTypingCursor] = useState(false)
  const [showJumpToBottom, setShowJumpToBottom] = useState(false)
  const abortRef = useRef(null)
  const streamingAssistantRowRef = useRef(null)
  const didScrollForStreamRef = useRef(false)
  const chatScrollRef = useRef(null)
  const optionsRef = useRef(null)
  const shareRef = useRef(null)
  const exportRef = useRef(null)
  const composerRef = useRef(null)
  const lastUserRowRef = useRef(null)

  useEffect(() => {
    const el = document.documentElement
    if (hideTypingCursor) el.classList.add('aj-hide-pointer')
    else el.classList.remove('aj-hide-pointer')
    return () => {
      el.classList.remove('aj-hide-pointer')
    }
  }, [hideTypingCursor])

  useEffect(() => {
    if (!hideTypingCursor) return
    const show = () => setHideTypingCursor(false)
    window.addEventListener('mousemove', show, { passive: true })
    return () => window.removeEventListener('mousemove', show)
  }, [hideTypingCursor])

  const updateJumpToBottomVisibility = useCallback(() => {
    const el = chatScrollRef.current
    if (!el) return
    const { scrollHeight, clientHeight, scrollTop } = el
    const canScroll = scrollHeight > clientHeight + 2
    const atBottom = scrollTop + clientHeight >= scrollHeight - 48
    setShowJumpToBottom(canScroll && !atBottom)
  }, [])

  useLayoutEffect(() => {
    updateJumpToBottomVisibility()
  }, [messages, updateJumpToBottomVisibility])

  useLayoutEffect(() => {
    if (!streaming) {
      didScrollForStreamRef.current = false
      return
    }
    if (didScrollForStreamRef.current) return
    const doScroll = () => {
      const container = chatScrollRef.current
      const target = lastUserRowRef.current || streamingAssistantRowRef.current
      if (!container || !target) return false
      const canScroll = container.scrollHeight > container.clientHeight + 2
      if (!canScroll) return false
      const offset = target.offsetTop - container.offsetTop
      container.scrollTop = offset
      const delta = Math.abs(
        target.getBoundingClientRect().top - container.getBoundingClientRect().top,
      )
      if (delta < 12) didScrollForStreamRef.current = true
      updateJumpToBottomVisibility()
      return true
    }
    if (doScroll()) return undefined
    const id = requestAnimationFrame(() => {
      if (!didScrollForStreamRef.current) doScroll()
    })
    return () => cancelAnimationFrame(id)
  }, [streaming, messages, updateJumpToBottomVisibility])

  useEffect(() => {
    const el = chatScrollRef.current
    if (!el) return
    const onScroll = () => updateJumpToBottomVisibility()
    el.addEventListener('scroll', onScroll, { passive: true })
    window.addEventListener('resize', onScroll)
    return () => {
      el.removeEventListener('scroll', onScroll)
      window.removeEventListener('resize', onScroll)
    }
  }, [updateJumpToBottomVisibility])

  const scrollToLatestQuestion = useCallback(() => {
    const container = chatScrollRef.current
    if (!container) return
    const userRow = lastUserRowRef.current
    if (userRow) {
      const offset = userRow.offsetTop - container.offsetTop
      container.scrollTo({ top: offset, behavior: 'smooth' })
    } else {
      container.scrollTo({ top: container.scrollHeight, behavior: 'smooth' })
    }
    requestAnimationFrame(() => {
      updateJumpToBottomVisibility()
      composerRef.current?.focus()
    })
  }, [updateJumpToBottomVisibility])

  const adjustComposerHeight = useCallback(() => {
    const el = composerRef.current
    if (!el) return
    const maxPx = window.innerHeight * 0.5
    el.style.height = 'auto'
    const sh = el.scrollHeight
    if (sh <= maxPx) {
      el.style.height = `${sh}px`
      el.style.overflowY = 'hidden'
    } else {
      el.style.height = `${maxPx}px`
      el.style.overflowY = 'auto'
    }
  }, [])

  useLayoutEffect(() => {
    adjustComposerHeight()
  }, [input, adjustComposerHeight])

  useEffect(() => {
    const onResize = () => adjustComposerHeight()
    window.addEventListener('resize', onResize)
    return () => window.removeEventListener('resize', onResize)
  }, [adjustComposerHeight])

  useEffect(() => {
    try { sessionStorage.setItem(STORAGE_PERSONA, extraPersona) } catch { /* */ }
  }, [extraPersona])

  useEffect(() => {
    if (!optionsOpen) return
    const onDown = (e) => {
      if (optionsRef.current && !optionsRef.current.contains(e.target)) setOptionsOpen(false)
    }
    const onKey = (e) => { if (e.key === 'Escape') setOptionsOpen(false) }
    document.addEventListener('mousedown', onDown)
    document.addEventListener('keydown', onKey)
    return () => {
      document.removeEventListener('mousedown', onDown)
      document.removeEventListener('keydown', onKey)
    }
  }, [optionsOpen])

  useEffect(() => {
    if (!shareOpen) return
    const onDown = (e) => {
      if (shareRef.current && !shareRef.current.contains(e.target)) setShareOpen(false)
    }
    const onKey = (e) => { if (e.key === 'Escape') setShareOpen(false) }
    document.addEventListener('mousedown', onDown)
    document.addEventListener('keydown', onKey)
    return () => {
      document.removeEventListener('mousedown', onDown)
      document.removeEventListener('keydown', onKey)
    }
  }, [shareOpen])

  useEffect(() => {
    if (!exportOpen) return
    const onDown = (e) => {
      if (exportRef.current && !exportRef.current.contains(e.target)) setExportOpen(false)
    }
    const onKey = (e) => { if (e.key === 'Escape') setExportOpen(false) }
    document.addEventListener('mousedown', onDown)
    document.addEventListener('keydown', onKey)
    return () => {
      document.removeEventListener('mousedown', onDown)
      document.removeEventListener('keydown', onKey)
    }
  }, [exportOpen])

  const buildSystemPrompt = useCallback(() => {
    const base =
      'You are AI Jerry, a cybersecurity-focused assistant running on the BrainForge Security model. ' +
      'You give clear, practical guidance; you distinguish facts from speculation; you flag risks and ' +
      'compliance considerations when relevant. You are friendly and professional.'
    const extra = (extraPersona || '').trim()
    return extra ? `${base}\n\nAdditional instructions from the user:\n${extra}` : base
  }, [extraPersona])

  const triggerBackgroundSummarize = useCallback(
    async (allMessages) => {
      const sys = buildSystemPrompt()
      const est = estimateMessagesTokens(
        allMessages.map(({ role, content }) => ({ role, content })),
        sys,
        null,
      )
      const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
      const ratio = est / inputBudget
      if (ratio < SUMMARIZE_THRESHOLD) return

      setSummarizing(true)
      try {
        const res = await fetch('/api/chat/summarize', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            messages: allMessages.map(({ role, content }) => ({ role, content })),
            extra_persona: extraPersona,
          }),
        })
        if (!res.ok) return
        const data = await res.json()
        if (data.summary) {
          setSummary(data.summary)
        }
      } catch {
        /* silent — summarization is best-effort */
      } finally {
        setSummarizing(false)
      }
    },
    [buildSystemPrompt, extraPersona],
  )

  const buildApiPayload = useCallback(
    (rawMsgs) => {
      if (!summary) {
        return { messages: rawMsgs, summary: null }
      }
      const sys = buildSystemPrompt()
      const fullEst = estimateMessagesTokens(rawMsgs, sys, null)
      const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
      if (fullEst < inputBudget * 0.7) {
        return { messages: rawMsgs, summary: null }
      }
      const KEEP_RECENT = 4
      const recent = rawMsgs.slice(-KEEP_RECENT)
      return { messages: recent, summary }
    },
    [summary, buildSystemPrompt],
  )

  const runStream = useCallback(
    async (rawApiMsgs) => {
      const ac = new AbortController()
      abortRef.current = ac
      setStreaming(true)
      setContextOverflow(null)
      let acc = ''

      const { messages: apiMsgs, summary: useSummary } = buildApiPayload(rawApiMsgs)

      try {
        const res = await fetch('/api/chat/stream', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            messages: apiMsgs,
            extra_persona: extraPersona,
            summary: useSummary || undefined,
          }),
          signal: ac.signal,
        })
        if (!res.ok) {
          const t = await res.text()
          throw new Error(t || res.statusText)
        }
        const reader = res.body.getReader()
        const decoder = new TextDecoder()
        let buffer = ''
        const handleEv = (ev) => {
          if (ev.type === 'context_overflow') {
            setContextOverflow({
              detail: ev.detail || 'The conversation is too long for the model context window.',
              inputTokens: ev.input_tokens,
              contextWindow: ev.context_window || CONTEXT_WINDOW,
            })
            return
          }
          if (ev.type === 'token') {
            const nextAcc = appendTokenContent(acc, ev)
            if (nextAcc !== acc) {
              acc = nextAcc
              setMessages((prev) => {
                const next = [...prev]
                const last = next[next.length - 1]
                if (last?.role === 'assistant') {
                  next[next.length - 1] = { ...last, content: acc }
                }
                return next
              })
            }
          }
          if (ev.type === 'error') {
            const detail = ev.detail || ''
            if (/context.length|max_tokens.*too large|too many tokens/i.test(detail)) {
              setContextOverflow({ detail })
            } else {
              throw new Error(detail || 'Stream error')
            }
          }
        }
        while (true) {
          const { done, value } = await reader.read()
          if (done) break
          buffer += decoder.decode(value, { stream: true })
          buffer = parseSseBuffer(buffer, handleEv)
        }
        if (buffer.trim()) {
          parseSseBuffer(`${buffer}\n`, handleEv)
        }
        if (!acc.trim() && !contextOverflow) {
          setMessages((prev) => {
            const next = [...prev]
            const last = next[next.length - 1]
            if (last?.role === 'assistant' && !last.content.trim()) {
              next[next.length - 1] = {
                role: 'assistant',
                content: '[No response] The stream finished with no text.',
              }
            }
            return next
          })
        }
      } catch (e) {
        if (e.name === 'AbortError') {
          setMessages((prev) => {
            const next = [...prev]
            const last = next[next.length - 1]
            if (last?.role === 'assistant' && !last.content.trim()) next.pop()
            return next
          })
        } else {
          setMessages((prev) => {
            const next = [...prev]
            const last = next[next.length - 1]
            if (last?.role === 'assistant') {
              next[next.length - 1] = {
                ...last,
                content: (last.content ? `${last.content}\n\n` : '') + `[Error] ${e.message}`,
              }
            } else {
              next.push({ role: 'assistant', content: `[Error] ${e.message}` })
            }
            return next
          })
        }
      } finally {
        setStreaming(false)
        abortRef.current = null
      }
    },
    [extraPersona, buildApiPayload, contextOverflow],
  )

  useEffect(() => {
    if (streaming || messages.length < 4) return
    const last = messages[messages.length - 1]
    if (last.role !== 'assistant' || !last.content.trim()) return
    triggerBackgroundSummarize(messages)
  }, [streaming, messages, triggerBackgroundSummarize])

  const handleNewChat = () => {
    abortRef.current?.abort()
    setMessages([])
    setInput('')
    setSummary(null)
    setContextOverflow(null)
  }

  const handleStop = () => { abortRef.current?.abort() }

  const handleRefresh = () => {
    if (streaming) return
    let base = [...messages]
    if (base.length && base[base.length - 1].role === 'assistant') base = base.slice(0, -1)
    if (!base.length || base[base.length - 1].role !== 'user') return
    const apiMsgs = base.map(({ role, content }) => ({ role, content }))
    setMessages([...base, { role: 'assistant', content: '' }])
    setContextOverflow(null)
    runStream(apiMsgs)
  }

  const handleSend = () => {
    const text = input.trim()
    if (!text || streaming) return
    setHideTypingCursor(false)
    const userMsg = { role: 'user', content: text }
    const apiMsgs = [...messages, userMsg].map(({ role, content }) => ({ role, content }))
    setMessages([...messages, userMsg, { role: 'assistant', content: '' }])
    setInput('')
    setContextOverflow(null)
    runStream(apiMsgs)
  }

  const handleOverflowRetry = () => {
    setContextOverflow(null)
    setMessages((prev) => {
      const next = [...prev]
      const last = next[next.length - 1]
      if (last?.role === 'assistant' && !last.content.trim()) next.pop()
      return next
    })
  }

  const handleOverflowClear = () => {
    setContextOverflow(null)
    setMessages([])
    setSummary(null)
  }

  const handleOverflowSummarize = async () => {
    setContextOverflow(null)
    setSummarizing(true)
    try {
      const res = await fetch('/api/chat/summarize', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          messages: messages.map(({ role, content }) => ({ role, content })),
          extra_persona: extraPersona,
        }),
      })
      if (res.ok) {
        const data = await res.json()
        if (data.summary) {
          setSummary(data.summary)
          setMessages([])
          return
        }
      }
      setMessages([])
      setSummary(null)
    } catch {
      setMessages([])
      setSummary(null)
    } finally {
      setSummarizing(false)
    }
  }

  const onKeyDown = (e) => {
    if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend() }
  }

  const buildExportText = useCallback(() => {
    if (!messages.length) return ''
    const lines = messages.map((m) => {
      const label = m.role === 'user' ? 'You' : 'AI Jerry'
      return `${label}:\n${m.content}\n`
    })
    return `Ask Jerry — Chat Export\n${new Date().toLocaleString()}\n${'─'.repeat(40)}\n\n${lines.join('\n')}`
  }, [messages])

  const buildExportMarkdown = useCallback(() => {
    if (!messages.length) return ''
    const ts = new Date().toLocaleString()
    const header = `# Ask Jerry — Chat Export\n\n_Exported: ${ts}_\n\n---\n\n`
    const blocks = messages.map((m) => {
      const heading = m.role === 'user' ? '### You' : '### AI Jerry'
      return `${heading}\n\n${m.content}`
    })
    return header + blocks.join('\n\n---\n\n')
  }, [messages])

  const copyToClipboard = async (text) => {
    try {
      await navigator.clipboard.writeText(text)
    } catch {
      const ta = document.createElement('textarea')
      ta.value = text
      ta.setAttribute('readonly', '')
      ta.style.position = 'fixed'
      ta.style.left = '-9999px'
      document.body.appendChild(ta)
      ta.select()
      try {
        document.execCommand('copy')
      } finally {
        document.body.removeChild(ta)
      }
    }
  }

  const handleExportDownload = (format) => {
    const text = format === 'md' ? buildExportMarkdown() : buildExportText()
    if (!text) return
    const mime =
      format === 'md' ? 'text/markdown;charset=utf-8' : 'text/plain;charset=utf-8'
    const ext = format === 'md' ? 'md' : 'txt'
    const blob = new Blob([text], { type: mime })
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = `ask-jerry-chat-${Date.now()}.${ext}`
    a.click()
    URL.revokeObjectURL(url)
    setExportOpen(false)
  }

  const handleShareCopyClipboard = async () => {
    const text = buildExportText()
    if (!text) return
    await copyToClipboard(text)
    setShareOpen(false)
  }

  const handleShareEmail = () => {
    const text = buildExportText()
    if (!text) return
    const subject = encodeURIComponent('Ask Jerry chat')
    const body = encodeURIComponent(text.length > 16000 ? `${text.slice(0, 16000)}\n\n… (truncated)` : text)
    const a = document.createElement('a')
    a.href = `mailto:?subject=${subject}&body=${body}`
    a.rel = 'noopener noreferrer'
    a.click()
    setShareOpen(false)
  }

  const handleShareLinkedIn = async () => {
    const text = buildExportText()
    if (!text) return
    await copyToClipboard(text)
    window.open('https://www.linkedin.com/feed/?shareActive=true', '_blank', 'noopener,noreferrer')
    setShareOpen(false)
  }

  const handleShareFacebook = async () => {
    const text = buildExportText()
    if (!text) return
    await copyToClipboard(text)
    const quote = encodeURIComponent(text.slice(0, 2000))
    const fb = `https://www.facebook.com/sharer/sharer.php?quote=${quote}`
    if (fb.length > 2048) {
      window.open('https://www.facebook.com/', '_blank', 'noopener,noreferrer')
    } else {
      window.open(fb, '_blank', 'noopener,noreferrer')
    }
    setShareOpen(false)
  }

  const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
  const currentTokens = estimateMessagesTokens(
    messages.map(({ role, content }) => ({ role, content })),
    buildSystemPrompt(),
    summary && messages.length > 4 ? summary : null,
  )
  const usagePct = Math.min(100, Math.round((currentTokens / inputBudget) * 100))
  return (
    <div className="aj-app">
      <header className="aj-header">
        <a
          className="aj-header-brand-link"
          href="https://www.neon.ai/"
          target="_blank"
          rel="noopener noreferrer"
          title="Neon.ai"
        >
          <img src="/neon-logo.png" alt="Neon.ai" className="aj-header-logo" />
          <span className="aj-header-brand-name">Neon.ai</span>
        </a>
        <div className="aj-header-title-wrap">
          <h1 className="aj-header-title">Ask Jerry About Cyber Security</h1>
        </div>
        <div className="aj-options-wrap" ref={optionsRef}>
          {streaming && (
            <button
              type="button"
              className="aj-btn danger"
              onClick={handleStop}
              title="Stop"
              aria-label="Stop"
            >
              <Square size={16} aria-hidden />
            </button>
          )}
          <button
            type="button"
            className="aj-btn"
            onClick={handleRefresh}
            disabled={streaming || messages.length === 0}
            title="Refresh"
            aria-label="Refresh"
          >
            <RefreshCw size={16} aria-hidden />
          </button>
          <button
            type="button"
            className="aj-btn"
            onClick={handleNewChat}
            title="New chat"
            aria-label="New chat"
          >
            <MessageSquarePlus size={16} aria-hidden />
          </button>
          <button
            type="button"
            className="aj-btn"
            onClick={() => setOptionsOpen((o) => !o)}
            aria-expanded={optionsOpen}
            aria-haspopup="dialog"
            aria-controls="aj-options-panel"
            title="Options"
            aria-label="Options"
          >
            <Settings size={16} aria-hidden />
          </button>
          {optionsOpen && (
            <div id="aj-options-panel" className="aj-options-panel" role="dialog" aria-label="Options" onMouseDown={(e) => e.stopPropagation()}>
              <label className="aj-field">
                <span>Additional persona instructions</span>
                <textarea
                  value={extraPersona}
                  onChange={(e) => setExtraPersona(e.target.value)}
                  placeholder="Optional — style, tone, domain focus… The assistant stays AI Jerry."
                  rows={10}
                  disabled={streaming}
                />
              </label>
              <p className="aj-options-hint">
                Merged into the system prompt for this session. The chat label remains <strong>AI Jerry</strong>.
              </p>
            </div>
          )}
        </div>
      </header>

      {messages.length > 0 && (
        <div className="aj-context-bar">
          <div className="aj-context-meter">
            <div className="aj-context-fill" style={{ width: `${usagePct}%` }} data-hot={usagePct > 80 ? '' : undefined} />
          </div>
          <span className="aj-context-label">
            Context: ~{currentTokens.toLocaleString()} / {inputBudget.toLocaleString()} tokens ({usagePct}%)
            {summary && ' · chat history is being summarized'}
            {summarizing && ' · summarizing…'}
          </span>
        </div>
      )}

      <div className="aj-chat-wrap">
        <div className="aj-messages" ref={chatScrollRef}>
          {messages.length === 0 && !summary && (
            <p className="aj-sub" style={{ textAlign: 'center', marginTop: 24 }}>
              Message AI Jerry to begin.
            </p>
          )}
          {messages.length === 0 && summary && (
            <div className="aj-summary-banner">
              <p><strong>Continuing from a summary of your previous conversation.</strong></p>
              <p className="aj-summary-text">{summary}</p>
            </div>
          )}
          {(() => {
            const lastUserIdx = messages.reduce((acc, msg, idx) => msg.role === 'user' ? idx : acc, -1)
            return messages.map((m, i) => {
              const isUser = m.role === 'user'
              const rowRef =
                i === lastUserIdx
                  ? lastUserRowRef
                  : i === messages.length - 1 && m.role === 'assistant'
                    ? streamingAssistantRowRef
                    : undefined
              return (
                <div
                  key={i}
                  ref={rowRef}
                  className={`aj-msg-row ${isUser ? 'user' : ''}`}
                >
                {!isUser && <div className="aj-shape" aria-hidden />}
                <div className="aj-bubble-wrap">
                  <div className="aj-bubble-name">{isUser ? 'You' : 'AI Jerry'}</div>
                  <div className={`aj-bubble ${!isUser ? 'aj-bubble--assistant-md' : ''}`}>
                    {isUser ? (
                      m.content
                    ) : (
                      <>
                        {m.content ? <AssistantBubbleContent content={m.content} /> : null}
                        {streaming && i === messages.length - 1 && !m.content && (
                          <span className="aj-typing" />
                        )}
                      </>
                    )}
                  </div>
                  {!isUser && (
                    <AssistantSearchBar
                      content={m.content}
                      show={!(streaming && i === messages.length - 1)}
                    />
                  )}
                </div>
                </div>
              )
            })
          })()}
        </div>

        {contextOverflow && (
          <div className="aj-overflow-banner">
            <div className="aj-overflow-icon"><AlertTriangle size={22} /></div>
            <div className="aj-overflow-body">
              <p className="aj-overflow-title">Context window exceeded</p>
              <p className="aj-overflow-detail">
                The conversation history is too long for this model's {CONTEXT_WINDOW.toLocaleString()}-token context window.
              </p>
              <div className="aj-overflow-actions">
                <button type="button" className="aj-btn" onClick={handleOverflowRetry}>
                  <RefreshCw size={14} aria-hidden />
                  Try again (shorter request)
                </button>
                <button type="button" className="aj-btn" onClick={handleOverflowClear}>
                  <Trash2 size={14} aria-hidden />
                  Clear chat &amp; start fresh
                </button>
                <button type="button" className="aj-btn primary" onClick={handleOverflowSummarize} disabled={summarizing}>
                  <MessageSquarePlus size={14} aria-hidden />
                  {summarizing ? 'Summarizing…' : 'New chat with summary'}
                </button>
              </div>
            </div>
          </div>
        )}

        <div className="aj-composer">
          <div className="aj-composer-inner">
            <textarea
              ref={composerRef}
              value={input}
              onChange={(e) => setInput(e.target.value)}
              onInput={(e) => {
                const ne = e.nativeEvent
                if (ne.inputType?.startsWith('insert') || (ne.data != null && ne.data !== '')) {
                  setHideTypingCursor(true)
                }
              }}
              onKeyDown={onKeyDown}
              placeholder="Ask Jerry about cybersecurity…"
              rows={1}
              disabled={streaming || !!contextOverflow}
            />
            <button
              type="button"
              className="aj-btn primary"
              onClick={handleSend}
              disabled={streaming || !input.trim() || !!contextOverflow}
              title="Send"
            >
              <Send size={18} aria-hidden />
            </button>
            <div className="aj-share-wrap" ref={exportRef}>
              <button
                type="button"
                className="aj-btn"
                onClick={() => {
                  setExportOpen((o) => !o)
                  setShareOpen(false)
                }}
                disabled={!messages.length || streaming}
                title="Export chat"
                aria-expanded={exportOpen}
                aria-haspopup="menu"
                aria-controls="aj-export-panel"
              >
                <Download size={18} aria-hidden />
              </button>
              {exportOpen && (
                <div
                  id="aj-export-panel"
                  className="aj-share-panel"
                  role="menu"
                  aria-label="Export format"
                  onMouseDown={(e) => e.stopPropagation()}
                >
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={() => handleExportDownload('md')}
                  >
                    Markdown (.md)
                  </button>
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={() => handleExportDownload('txt')}
                  >
                    Plain text (.txt)
                  </button>
                </div>
              )}
            </div>
            <div className="aj-share-wrap" ref={shareRef}>
              <button
                type="button"
                className="aj-btn"
                onClick={() => {
                  setShareOpen((o) => !o)
                  setExportOpen(false)
                }}
                disabled={!messages.length || streaming}
                title="Share chat"
                aria-expanded={shareOpen}
                aria-haspopup="menu"
                aria-controls="aj-share-panel"
              >
                <Share2 size={18} aria-hidden />
              </button>
              {shareOpen && (
                <div
                  id="aj-share-panel"
                  className="aj-share-panel"
                  role="menu"
                  aria-label="Share chat"
                  onMouseDown={(e) => e.stopPropagation()}
                >
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={handleShareLinkedIn}
                  >
                    LinkedIn
                  </button>
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={handleShareFacebook}
                  >
                    Facebook
                  </button>
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={handleShareEmail}
                  >
                    Email
                  </button>
                  <button
                    type="button"
                    className="aj-share-item"
                    role="menuitem"
                    onClick={handleShareCopyClipboard}
                  >
                    Copy to clipboard
                  </button>
                  <p className="aj-share-hint">
                    LinkedIn and Facebook copy the conversation to your clipboard, then open the site so you can paste into a post.
                  </p>
                </div>
              )}
            </div>
          </div>
        </div>

        {showJumpToBottom && (
          <button
            type="button"
            className="aj-jump-bottom-btn"
            onClick={scrollToLatestQuestion}
            title="Jump to latest question"
            aria-label="Jump to latest question"
          >
            <ChevronDown size={22} aria-hidden />
          </button>
        )}
      </div>
    </div>
  )
}