-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathllm-response.html
More file actions
1029 lines (910 loc) · 37.1 KB
/
Copy pathllm-response.html
File metadata and controls
1029 lines (910 loc) · 37.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenCluely</title>
<script src="lib/markdown.js"></script>
<style>
.text-white { color: white; }
.text-gray-300 { color: #d1d5db; }
.text-sm { font-size: 0.875rem; }
.font-medium { font-weight: 500; }
.mr-2 { margin-right: 0.5rem; }
body {
background: linear-gradient(135deg, rgba(0, 0, 0, 0.4) 0%, rgba(20, 20, 20, 0.5) 100%);;
-webkit-app-region: no-drag;
color: white;
margin: 0;
padding: 0;
overflow: hidden;
height: 100vh;
width: 100vw;
}
.content-area {
-webkit-app-region: no-drag;
}
.markdown-content {
line-height: 1.6;
color: white;
font-size: 0.875rem;
}
.markdown-content h1, .markdown-content h2, .markdown-content h3 {
margin-top: 1.2rem;
margin-bottom: 0.8rem;
font-weight: bold;
color: white;
}
.markdown-content h1 { font-size: 1.2rem; }
.markdown-content h2 { font-size: 1.1rem; }
.markdown-content h3 { font-size: 1rem; }
.markdown-content p { margin-bottom: 0.8rem; color: white; font-size: 0.875rem; }
.markdown-content ul, .markdown-content ol { margin-bottom: 0.8rem; padding-left: 1.2rem; font-size: 0.875rem; }
.markdown-content li { margin-bottom: 0.4rem; color: white; font-size: 0.875rem; }
.markdown-content code {
background-color: rgba(0, 0, 0, 0.4);
padding: 0.2rem 0.4rem;
border-radius: 0.25rem;
font-family: 'Courier New', monospace;
color: #e5e7eb;
}
.markdown-content pre {
background-color: rgba(0, 0, 0, 0.4);
padding: 1rem;
border-radius: 0.5rem;
margin: 1rem 0;
overflow-x: auto;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.markdown-content pre code {
background-color: transparent;
padding: 0;
color: #e5e7eb;
}
.markdown-content blockquote {
border-left: 4px solid #6b7280;
padding-left: 1rem;
margin: 1rem 0;
font-style: italic;
color: #d1d5db;
}
/* Main container - ensures full viewport usage */
.main-container {
height: 100vh;
width: 100vw;
overflow: hidden;
position: relative;
background: transparent;
}
/* Split layout improvements */
.split-layout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.75rem;
height: 100vh;
padding: 0;
box-sizing: border-box;
overflow: hidden;
}
/* Panel improvements with proper scrolling */
.text-panel, .code-panel {
background: transparent;
overflow: hidden;
position: relative;
min-height: 0; /* Important for grid items */
}
.panel-content {
height: 100%;
overflow-y: auto;
overflow-x: auto;
padding: 1rem;
box-sizing: border-box;
scroll-behavior: smooth;
}
/* Full content layout */
.full-content {
height: 100vh;
padding: 0;
box-sizing: border-box;
overflow: hidden;
position: relative;
}
.full-content-inner {
height: 100%;
background-color: rgba(255, 255, 255, 0.05);
backdrop-filter: blur(10px);
border-radius: 0.5rem;
border: 1px solid rgba(255, 255, 255, 0.1);
overflow-y: auto;
overflow-x: auto;
padding: 1rem;
box-sizing: border-box;
scroll-behavior: smooth;
}
/* Hide scrollbars completely while maintaining scrollability */
.panel-content::-webkit-scrollbar,
.full-content-inner::-webkit-scrollbar {
width: 0px;
height: 0px;
display: none;
}
/* For Firefox and other browsers */
.panel-content,
.full-content-inner {
scrollbar-width: none;
-ms-overflow-style: none;
}
/* Code block styling */
.code-block {
background-color: rgba(0, 0, 0, 0.3);
margin-bottom: 1rem;
border-radius: 0.5rem;
border: 1px solid rgba(255, 255, 255, 0.1);
}
.code-header {
background-color: rgba(0, 0, 0, 0.4);
padding: 0.5rem 0.75rem;
border-radius: 0.5rem 0.5rem 0 0;
font-family: 'Courier New', monospace;
font-size: 0.75rem;
color: #d1d5db;
font-weight: bold;
}
.code-content {
padding: 1rem;
overflow-x: auto;
background-color: rgba(0, 0, 0, 0.2);
border-radius: 0 0 0.5rem 0.5rem;
}
.code-content pre {
background-color: transparent;
border: none;
margin: 0;
padding: 0;
}
.code-content code {
background-color: transparent;
color: #e5e7eb;
}
.no-code {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: #9ca3af;
font-style: italic;
}
/* Loading state */
.loading {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
background-color: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(8px);
}
.loading-compact {
display: flex;
align-items: center;
justify-content: center;
width: 200px;
height: 60px;
background: transparent;
}
.dots-container {
display: inline-flex;
align-items: center;
gap: 0.3rem;
}
.bouncing-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background-color: #4CAF50;
animation: bounce 1.4s infinite ease-in-out;
}
.bouncing-dot:nth-child(1) { animation-delay: -0.32s; }
.bouncing-dot:nth-child(2) { animation-delay: -0.16s; }
.bouncing-dot:nth-child(3) { animation-delay: 0s; }
@keyframes bounce {
0%, 80%, 100% {
transform: scale(0);
opacity: 0.5;
}
40% {
transform: scale(1);
opacity: 1;
}
}
/* Interactive scrolling enhancements */
.interactive-scrolling .panel-content,
.interactive-scrolling .full-content-inner {
cursor: grab;
}
.interactive-scrolling .panel-content:active,
.interactive-scrolling .full-content-inner:active {
cursor: grabbing;
}
/* Remove hover effects for clean content appearance */
/* Override Prism theme for better visibility */
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: #6a737d;
}
.token.punctuation {
color: #e1e4e8;
}
.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
color: #79b8ff;
}
.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #85e89d;
}
.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #f97583;
}
.token.atrule,
.token.attr-value,
.token.keyword {
color: #f97583;
}
.token.function,
.token.class-name {
color: #b392f0;
}
.token.regex,
.token.important,
.token.variable {
color: #ffab70;
}
.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
.token.entity {
cursor: help;
}
/* Ensure content is always visible */
.hidden { display: none !important; }
.visible { display: block !important; }
/* Draggable header bar */
.window-header {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 32px;
-webkit-app-region: drag;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 10px;
box-sizing: border-box;
background: linear-gradient(180deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.04) 100%);
backdrop-filter: blur(10px);
border-bottom: 1px solid rgba(255,255,255,0.12);
z-index: 50;
color: #e5e7eb;
font-size: 12px;
letter-spacing: 0.3px;
}
.window-title {
pointer-events: none;
user-select: none;
opacity: 0.9;
}
.window-controls {
display: flex;
gap: 6px;
-webkit-app-region: no-drag;
}
.window-btn {
width: 22px;
height: 22px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 4px;
background: rgba(255,255,255,0.06);
color: #e5e7eb;
border: 1px solid rgba(255,255,255,0.12);
cursor: pointer;
transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
font-weight: 600;
line-height: 1;
}
.window-btn:hover {
background: rgba(255,255,255,0.12);
border-color: rgba(255,255,255,0.2);
}
.window-btn-close:hover {
background: rgba(239, 68, 68, 0.25);
border-color: rgba(239, 68, 68, 0.45);
color: #fff;
}
/* Push content below header */
.has-header #response-content,
.has-header #loading {
margin-top: 32px;
}
.has-header .split-layout,
.has-header .full-content,
.has-header .loading {
height: calc(100vh - 32px);
}
.copy-btn {
position: absolute;
top: 6px;
right: 6px;
background: rgba(0,0,0,0.5);
color: rgba(255,255,255,0.9);
border: 1px solid rgba(255,255,255,0.2);
padding: 4px 8px;
font-size: 11px;
border-radius: 4px;
cursor: pointer;
-webkit-app-region: no-drag;
transition: background 0.2s ease, border-color 0.2s ease, transform 0.12s ease;
}
.copy-btn:hover { background: rgba(0,0,0,0.7); }
.copy-btn.copied {
background: rgba(76,175,80,0.85);
border-color: rgba(76,175,80,0.95);
transform: scale(0.98);
}
</style>
</head>
<body class="text-white">
<div class="main-container has-header">
<!-- Draggable Header -->
<div class="window-header">
<div class="window-title">AI Response</div>
<div class="window-controls">
<button id="close-window-btn" class="window-btn window-btn-close" title="Close">×</button>
</div>
</div>
<!-- Loading State -->
<div id="loading" class="loading">
<div class="loading-compact">
<span class="text-gray-300 text-sm font-medium mr-2">Analyzing</span>
<div class="dots-container">
<div class="bouncing-dot"></div>
<div class="bouncing-dot"></div>
<div class="bouncing-dot"></div>
</div>
</div>
</div>
<!-- Response Content -->
<div id="response-content" class="content-area hidden">
<!-- Split Layout -->
<div id="split-layout" class="split-layout hidden">
<!-- Text Panel -->
<div class="text-panel">
<div class="panel-content" tabindex="0">
<div id="text-content" class="markdown-content"></div>
</div>
</div>
<!-- Code Panel -->
<div class="code-panel">
<div class="panel-content" tabindex="0">
<div id="code-content"></div>
</div>
</div>
</div>
<!-- Full Content (when no code detected) -->
<div id="full-content" class="full-content hidden">
<div class="full-content-inner" tabindex="0">
<div id="full-markdown" class="markdown-content"></div>
</div>
</div>
</div>
</div>
<script>
// Global variables
let currentLayout = 'split';
let hasCode = false;
let currentSkill = 'dsa';
let isInteractive = false;
let scrollableElements = [];
let initialized = false;
// Immediate logging to verify script execution
console.log('[LLM-RESPONSE] Script tag executed');
window.onerror = function(msg, url, line, col, error) {
console.error('[LLM-RESPONSE] Window error:', msg, 'at line', line);
return false;
};
document.addEventListener('DOMContentLoaded', function() {
console.log('[LLM-RESPONSE] DOMContentLoaded fired');
initLLMResponseWindow();
});
// Also try immediate init in case DOMContentLoaded already fired
if (document.readyState === 'complete' || document.readyState === 'interactive') {
console.log('[LLM-RESPONSE] Document already ready, initializing now');
setTimeout(initLLMResponseWindow, 0);
}
function initLLMResponseWindow() {
if (initialized) return;
initialized = true;
console.log('[LLM-RESPONSE] Initializing window...');
// Prefer marked when available, but fall back to the bundled markdown.js parser.
if (typeof marked !== 'undefined') {
marked.setOptions({
highlight: function(code, lang) {
if (typeof Prism !== 'undefined' && Prism.languages[lang]) {
return Prism.highlight(code, Prism.languages[lang], lang);
}
return code;
},
breaks: true,
gfm: true
});
console.log('[LLM-RESPONSE] marked configured');
} else if (typeof markdown !== 'undefined' && typeof markdown.toHTML === 'function') {
console.log('[LLM-RESPONSE] using bundled markdown.js renderer');
} else {
console.error('[LLM-RESPONSE] no markdown renderer available, falling back to plain text');
}
console.log('[LLM-RESPONSE] electronAPI exists:', !!window.electronAPI);
console.log('[LLM-RESPONSE] onDisplayLlmResponse method:', typeof window.electronAPI?.onDisplayLlmResponse);
// Set up event listeners using electronAPI from preload script
if (window.electronAPI) {
console.log('[LLM-RESPONSE] Setting up IPC listeners...');
// Set up show-loading event listener
window.electronAPI.onShowLoading(() => {
console.log('[LLM-RESPONSE] show-loading received');
showLoadingState();
});
// Listen for LLM response
window.electronAPI.onDisplayLlmResponse(function() {
console.log('[LLM-RESPONSE] ===== display-llm-response received! =====');
console.log('[LLM-RESPONSE] arguments count:', arguments.length);
try {
// Get the data - could be first or second argument
let actualData = null;
if (arguments.length >= 2 && arguments[1] && typeof arguments[1] === 'object') {
actualData = arguments[1];
} else if (arguments.length >= 1 && arguments[0] && typeof arguments[0] === 'object') {
actualData = arguments[0];
}
console.log('[LLM-RESPONSE] actualData keys:', actualData ? Object.keys(actualData) : 'null');
const response = actualData?.content || actualData?.response;
console.log('[LLM-RESPONSE] response found:', !!response, 'length:', response?.length);
if (!response) {
console.error('[LLM-RESPONSE] No response content found!');
hideLoadingState();
return;
}
hideLoadingState();
displayResponse(actualData);
setupScrolling();
setTimeout(verifyDisplayState, 500);
console.log('[LLM-RESPONSE] Handler completed successfully');
} catch (err) {
console.error('[LLM-RESPONSE] Error in handler:', err.message, err.stack);
hideLoadingState();
}
});
console.log('[LLM-RESPONSE] IPC listeners set up successfully');
} else {
console.error('[LLM-RESPONSE] electronAPI not available!');
}
} // end initLLMResponseWindow
function showLoadingState() {
const loadingElement = document.getElementById('loading');
const responseElement = document.getElementById('response-content');
if (loadingElement) {
loadingElement.classList.remove('hidden');
loadingElement.style.display = ''; // Remove any forced display styles
}
if (responseElement) {
responseElement.classList.add('hidden');
}
console.log('Loading state shown - should see Analyzing...');
}
function hideLoadingState() {
const loadingElement = document.getElementById('loading');
if (loadingElement) {
loadingElement.classList.add('hidden');
loadingElement.style.display = 'none'; // Force hide
console.log('Loading state hidden');
} else {
console.error('Loading element not found!');
}
}
function renderMarkdown(text) {
if (typeof marked !== 'undefined') {
return marked.parse ? marked.parse(text) : marked(text);
}
if (typeof markdown !== 'undefined' && typeof markdown.toHTML === 'function') {
return markdown.toHTML(text);
}
return escapeHtml(text).replace(/\n/g, '<br>');
}
function calculateContentMetrics(response, codeBlocks) {
// Count lines in the response
const lineCount = response.split('\n').length;
// Check for long lines (over 80 characters)
const hasLongLines = response.split('\n').some(line => line.length > 80);
// Count code blocks
const codeBlockCount = codeBlocks.length;
// Estimate content complexity
const hasCode = codeBlockCount > 0;
const isLongContent = lineCount > 30;
const hasMultipleCodeBlocks = codeBlockCount > 2;
return {
lineCount,
hasLongLines,
codeBlocks: codeBlockCount,
hasCode,
isLongContent,
hasMultipleCodeBlocks,
complexity: isLongContent || hasMultipleCodeBlocks ? 'high' : hasCode ? 'medium' : 'low'
};
}
function displayResponse(data) {
// Show content
const responseElement = document.getElementById('response-content');
if (responseElement) {
responseElement.classList.remove('hidden');
}
// Parse the response - check both content and response properties for compatibility
const response = data.content || data.response;
if (!response) {
console.error('No response data received', {
dataKeys: Object.keys(data),
hasContent: !!data.content,
hasResponse: !!data.response
});
return;
}
// Check if response contains code blocks
const codeBlocks = extractCodeBlocks(response);
hasCode = codeBlocks.length > 0;
// Calculate content metrics for dynamic sizing
const contentMetrics = calculateContentMetrics(response, codeBlocks);
if (hasCode) {
// Split layout: text on left, code on right
displaySplitLayout(response, codeBlocks);
} else {
// Full layout: all content in one panel
displayFullLayout(response);
}
// Request dynamic window sizing based on content
setTimeout(() => {
window.electronAPI.resizeLlmWindowForContent(contentMetrics)
.then(result => {
})
.catch(error => {
console.error('Failed to resize window:', error);
});
}, 200);
}
function setupScrolling() {
// Set up scrollable elements based on current layout
setTimeout(() => {
if (hasCode) {
scrollableElements = [
document.querySelector('.text-panel .panel-content'),
document.querySelector('.code-panel .panel-content')
].filter(el => el !== null);
} else {
scrollableElements = [
document.querySelector('.full-content-inner')
].filter(el => el !== null);
}
// Reset scroll positions and ensure elements are focusable
scrollableElements.forEach(element => {
element.scrollTop = 0;
element.scrollLeft = 0;
element.setAttribute('tabindex', '0');
});
// Enable scrolling if interactive mode is on
if (isInteractive) {
enableScrolling();
}
}, 100);
}
function enableScrolling() {
scrollableElements.forEach(element => {
if (element) {
// Enable mouse/trackpad scrolling with smooth behavior
element.style.scrollBehavior = 'smooth';
// Add mouse enter handler to ensure focus for scrolling
element.addEventListener('mouseenter', handleMouseEnter, { passive: true });
element.addEventListener('mouseleave', handleMouseLeave, { passive: true });
// Add wheel event for enhanced scrolling
element.addEventListener('wheel', handleWheelScroll, { passive: false });
}
});
}
function disableScrolling() {
scrollableElements.forEach(element => {
if (element) {
element.removeEventListener('mouseenter', handleMouseEnter);
element.removeEventListener('mouseleave', handleMouseLeave);
element.removeEventListener('wheel', handleWheelScroll);
}
});
}
function handleMouseEnter(e) {
// Focus the element for keyboard scrolling and ensure it's ready for mouse scrolling
e.target.focus({ preventScroll: true });
e.target.style.cursor = 'grab';
}
function handleMouseLeave(e) {
e.target.style.cursor = 'default';
}
function handleWheelScroll(e) {
// Allow natural scrolling behavior
// Don't prevent default to maintain smooth native scrolling
console.log('Wheel scroll detected on element');
}
function extractCodeBlocks(text) {
const blocks = [];
if (!text) return blocks;
const regex = /(```|~~~)([^\n]*)\n([\s\S]*?)\1/g; // supports ```lang and ~~~lang
let match;
while ((match = regex.exec(text)) !== null) {
const info = (match[2] || '').trim();
const langToken = info.split(/\s+/)[0] || 'text';
blocks.push({ language: langToken, code: match[3], fullMatch: match[0] });
}
return blocks;
}
function attachCopy(btn, text) {
btn.addEventListener('click', async (e) => {
e.stopPropagation();
try {
let ok = false;
if (window.electronAPI && typeof window.electronAPI.copyToClipboard === 'function') {
ok = await window.electronAPI.copyToClipboard(text);
}
if (!ok) {
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
ok = true;
} else {
const ta = document.createElement('textarea');
ta.value = text;
document.body.appendChild(ta);
ta.select();
document.execCommand('copy');
ta.remove();
ok = true;
}
}
if (ok) {
btn.classList.add('copied');
const original = btn.textContent;
btn.textContent = 'Copied';
setTimeout(() => { btn.classList.remove('copied'); btn.textContent = original || 'Copy'; }, 1200);
}
} catch (err) { console.warn('Copy failed', err); }
})
}
function renderCopyButtonsInFullMarkdown(container) {
// Add copy buttons to all code blocks in full markdown view
const pres = container.querySelectorAll('pre');
pres.forEach(pre => {
// Avoid duplicate buttons
if (pre.querySelector('.copy-btn')) return;
const code = pre.querySelector('code');
const text = code ? code.textContent : pre.textContent;
const btn = document.createElement('button');
btn.className = 'copy-btn';
btn.textContent = 'Copy';
attachCopy(btn, text || '');
pre.style.position = 'relative';
pre.appendChild(btn);
});
}
function displaySplitLayout(response, codeBlocks) {
// Show split layout, hide full layout
document.getElementById('split-layout').classList.remove('hidden');
document.getElementById('full-content').classList.add('hidden');
// Remove code blocks from text content
let textContent = response;
codeBlocks.forEach(block => {
textContent = textContent.replace(block.fullMatch, '');
});
// Clean up text content
textContent = textContent.replace(/\n\s*\n\s*\n/g, '\n\n').trim();
// Render text content
const textHtml = renderMarkdown(textContent);
document.getElementById('text-content').innerHTML = textHtml;
// Render code blocks
const codeContainer = document.getElementById('code-content');
codeContainer.innerHTML = '';
if (codeBlocks.length === 0) {
codeContainer.innerHTML = '<div class="no-code">No code examples found</div>';
} else {
codeBlocks.forEach((block, index) => {
const codeBlock = document.createElement('div');
codeBlock.className = 'code-block';
codeBlock.innerHTML = `
<div class="code-header">${block.language.toUpperCase()}</div>
<div class="code-content">
<pre><code class="language-${block.language}">${escapeHtml(block.code)}</code></pre>
</div>
`;
const pre = codeBlock.querySelector('pre');
pre.style.position = 'relative';
const btn = document.createElement('button');
btn.className = 'copy-btn';
btn.textContent = 'Copy';
attachCopy(btn, block.code || '');
pre.appendChild(btn);
codeContainer.appendChild(codeBlock);
});
}
// Highlight code if Prism is available
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
}
function displayFullLayout(response) {
// Show full layout, hide split layout
document.getElementById('split-layout').classList.add('hidden');
document.getElementById('full-content').classList.remove('hidden');
// Render full markdown
const html = renderMarkdown(response);
const full = document.getElementById('full-markdown');
full.innerHTML = html;
// Highlight any code if Prism is available
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
// Add copy buttons to code blocks in full view
const pres = full.querySelectorAll('pre');
pres.forEach(pre => {
if (pre.querySelector('.copy-btn')) return;
const code = pre.querySelector('code');
const text = code ? code.textContent : pre.textContent;
const btn = document.createElement('button');
btn.className = 'copy-btn';
btn.textContent = 'Copy';
attachCopy(btn, text || '');
pre.style.position = 'relative';
pre.appendChild(btn);
});
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Handle window focus
window.addEventListener('focus', () => {
console.log('LLM Response window focused');
});
// Handle window close
window.addEventListener('beforeunload', () => {
console.log('LLM Response window closing');
});
// Add keyboard scrolling support for when interactive
document.addEventListener('keydown', (e) => {
if (!isInteractive) return;
const activeElement = document.activeElement;
let targetElement = null;
// Find the currently focused scrollable element
for (let element of scrollableElements) {
if (element === activeElement || element.contains(activeElement)) {
targetElement = element;
break;
}
}
// Default to first scrollable element if none focused
if (!targetElement && scrollableElements.length > 0) {
targetElement = scrollableElements[0];
}
if (!targetElement) return;
const scrollAmount = 50; // Pixels to scroll
switch(e.key) {
case 'ArrowUp':
e.preventDefault();
targetElement.scrollBy({ top: -scrollAmount, behavior: 'smooth' });
break;
case 'ArrowDown':
e.preventDefault();
targetElement.scrollBy({ top: scrollAmount, behavior: 'smooth' });
break;
case 'ArrowLeft':
e.preventDefault();
targetElement.scrollBy({ left: -scrollAmount, behavior: 'smooth' });
break;
case 'ArrowRight':
e.preventDefault();
targetElement.scrollBy({ left: scrollAmount, behavior: 'smooth' });
break;
case 'PageUp':
e.preventDefault();
targetElement.scrollBy({ top: -targetElement.clientHeight * 0.8, behavior: 'smooth' });
break;
case 'PageDown':
e.preventDefault();
targetElement.scrollBy({ top: targetElement.clientHeight * 0.8, behavior: 'smooth' });
break;
case 'Home':
e.preventDefault();
targetElement.scrollTo({ top: 0, behavior: 'smooth' });
break;
case 'End':
e.preventDefault();
targetElement.scrollTo({ top: targetElement.scrollHeight, behavior: 'smooth' });
break;
}
});
// Add verification function to ensure content is properly displayed
function verifyDisplayState() {
const loadingElement = document.getElementById('loading');
const responseElement = document.getElementById('response-content');
const isLoadingHidden = loadingElement ? loadingElement.classList.contains('hidden') : true;
const isContentVisible = responseElement ? !responseElement.classList.contains('hidden') : false;
console.log('Display state verification:', {
loadingHidden: isLoadingHidden,
contentVisible: isContentVisible,
windowVisible: !document.hidden
});
if (!isLoadingHidden || !isContentVisible) {
console.warn('Display state inconsistent - forcing correction');
if (loadingElement) {
loadingElement.classList.add('hidden');
loadingElement.style.display = 'none';
}
if (responseElement) {
responseElement.classList.remove('hidden');
}
}
}
// Add global debugging function
window.debugLLMWindow = function() {
console.log('=== LLM Window Debug Info ===');
console.log('Current layout:', currentLayout);
console.log('Has code:', hasCode);