-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathOutputToolWindowControl.xaml.cs
More file actions
992 lines (858 loc) · 38.3 KB
/
OutputToolWindowControl.xaml.cs
File metadata and controls
992 lines (858 loc) · 38.3 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
using AI_Studio.Helpers;
using Microsoft.Extensions.AI;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell.Interop;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.Threading;
namespace AI_Studio
{
public partial class OutputToolWindowControl : UserControl
{
private enum ViewState { Empty, Content }
private readonly List<ChatMessage> _conversation = new List<ChatMessage>();
private bool _isSending;
private bool _showLoadingBubble;
private Border _streamingBubble;
private DateTime _lastStreamRenderTime = DateTime.MinValue;
private const int StreamRenderThrottleMs = 80;
// Cancels the follow-up chat request started from this tool window.
// (Command-initiated streams are cancelled via RequestCancellationManager.)
private CancellationTokenSource _streamingCts;
// VS theme brushes — resolved once after the control is loaded
private Brush _textBrush;
private Brush _windowBrush;
public OutputToolWindowControl()
{
InitializeComponent();
Loaded += OnLoaded;
SendButton.Click += OnSendButtonClick;
PromptInput.KeyDown += PromptInput_KeyDown;
PreviewKeyDown += OnPreviewKeyDown;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
RefreshThemeBrushes();
VSColorTheme.ThemeChanged += OnVsThemeChanged;
Unloaded += OnUnloaded;
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
VSColorTheme.ThemeChanged -= OnVsThemeChanged;
}
private void RefreshThemeBrushes()
{
_textBrush = TryFindResource("VsBrush.WindowText") as Brush ?? Brushes.WhiteSmoke;
_windowBrush = TryFindResource("VsBrush.Window") as Brush ?? Brushes.Transparent;
}
private void OnVsThemeChanged(ThemeChangedEventArgs e)
{
_ = ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
RefreshThemeBrushes();
RebuildPanel();
});
}
private bool IsDarkTheme()
{
if (_windowBrush is SolidColorBrush scb)
{
var c = scb.Color;
double lum = 0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B;
return lum < 128;
}
return true; // default to dark
}
// ── Public API (called from AIBaseCommand) ────────────────────────────
public async Task BeginStreamingAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
_conversation.Clear();
_showLoadingBubble = true;
_streamingBubble = null;
SetStopButtonMode();
RebuildPanel();
}
public async Task UpdateContentAsync(string content, bool isStreaming = false)
{
if (isStreaming)
{
var elapsed = (DateTime.UtcNow - _lastStreamRenderTime).TotalMilliseconds;
if (elapsed < StreamRenderThrottleMs)
return;
_lastStreamRenderTime = DateTime.UtcNow;
}
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
_showLoadingBubble = false;
var safeContent = string.IsNullOrWhiteSpace(content) ? "(No response returned.)" : content;
_conversation.Clear();
_conversation.Add(new ChatMessage(ChatRole.Assistant, safeContent));
_streamingBubble = null;
if (!isStreaming)
SetSendButtonMode();
RebuildPanel();
}
// ── Chat send ─────────────────────────────────────────────────────────
private void OnSendButtonClick(object sender, RoutedEventArgs e)
{
if (_isSending || RequestCancellationManager.IsActive)
{
CancelActiveRequest();
return;
}
_ = ThreadHelper.JoinableTaskFactory.RunAsync(HandleSendAsync);
}
private void PromptInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && Keyboard.Modifiers == ModifierKeys.None)
{
e.Handled = true;
if (!_isSending && !RequestCancellationManager.IsActive)
_ = ThreadHelper.JoinableTaskFactory.RunAsync(HandleSendAsync);
}
}
private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape && (_isSending || RequestCancellationManager.IsActive))
{
CancelActiveRequest();
e.Handled = true;
}
}
private void CancelActiveRequest()
{
// Cancel follow-up chat if this control owns the stream,
// otherwise cancel a command-initiated stream.
if (_streamingCts != null)
_streamingCts.Cancel();
else
RequestCancellationManager.Cancel();
}
private async Task HandleSendAsync()
{
if (_isSending)
return;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var userMessage = PromptInput.Text.Trim();
if (string.IsNullOrWhiteSpace(userMessage))
return;
_isSending = true;
_streamingCts = new CancellationTokenSource();
PromptInput.Text = string.Empty;
PromptInput.IsEnabled = false;
SetStopButtonMode();
_conversation.Add(new ChatMessage(ChatRole.User, userMessage));
_showLoadingBubble = true;
_streamingBubble = null;
RebuildPanel();
var responseBuilder = new StringBuilder();
var wasCancelled = false;
try
{
var generalOptions = await General.GetLiveInstanceAsync();
if (ChatClientFactory.RequiresApiKey(generalOptions.Provider) && string.IsNullOrWhiteSpace(generalOptions.ApiKey))
{
await VS.MessageBox.ShowAsync(
$"Add an API key for {generalOptions.Provider} in Tools > Options > AI Studio > General.",
buttons: OLEMSGBUTTON.OLEMSGBUTTON_OK);
return;
}
using IChatClient client = ChatClientFactory.Create(generalOptions);
var requestMessages = new List<ChatMessage>
{
new ChatMessage(ChatRole.System,
"You are AI Studio inside Visual Studio. Respond with concise, markdown-formatted answers suited for developers.")
};
requestMessages.AddRange(_conversation);
try
{
await foreach (var update in client.GetStreamingResponseAsync(requestMessages, cancellationToken: _streamingCts.Token))
{
if (string.IsNullOrEmpty(update?.Text))
continue;
responseBuilder.Append(update.Text);
await UpdateStreamingBubbleAsync(responseBuilder.ToString());
}
}
catch (OperationCanceledException) when (_streamingCts.IsCancellationRequested)
{
wasCancelled = true;
}
var assistantMessage = responseBuilder.Length == 0
? (wasCancelled ? "_(stopped)_" : "(No response returned.)")
: (wasCancelled ? responseBuilder.ToString() + "\n\n_(stopped)_" : responseBuilder.ToString());
_conversation.Add(new ChatMessage(ChatRole.Assistant, assistantMessage));
_streamingBubble = null;
_showLoadingBubble = false;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
RebuildPanel();
}
catch (Exception ex)
{
await VS.MessageBox.ShowAsync(ex.Message, buttons: OLEMSGBUTTON.OLEMSGBUTTON_OK);
}
finally
{
_streamingCts?.Dispose();
_streamingCts = null;
_isSending = false;
_showLoadingBubble = false;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
SetViewState(_conversation.Count > 0 ? ViewState.Content : ViewState.Empty);
PromptInput.IsEnabled = true;
SetSendButtonMode();
PromptInput.Focus();
}
}
private void SetStopButtonMode()
{
SendButton.Content = "Stop";
SendButton.IsEnabled = true;
SendButton.ToolTip = "Stop the current request (Esc)";
}
private void SetSendButtonMode()
{
SendButton.Content = "Send";
SendButton.IsEnabled = true;
SendButton.ToolTip = null;
}
// ── Streaming bubble ──────────────────────────────────────────────────
private async Task UpdateStreamingBubbleAsync(string markdown)
{
var elapsed = (DateTime.UtcNow - _lastStreamRenderTime).TotalMilliseconds;
if (elapsed < StreamRenderThrottleMs)
return;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
_lastStreamRenderTime = DateTime.UtcNow;
_showLoadingBubble = false;
if (_streamingBubble == null)
{
RebuildPanel(streamingMarkdown: markdown);
}
else
{
UpdateStreamingBubbleContent(markdown);
ScrollToBottom();
}
}
private void UpdateStreamingBubbleContent(string markdown)
{
if (_streamingBubble?.Child is StackPanel sp && sp.Children.Count >= 2)
{
var contentPanel = sp.Children[1] as StackPanel;
if (contentPanel != null)
{
contentPanel.Children.Clear();
RenderMarkdownInto(contentPanel, markdown);
}
}
}
// ── Panel rendering ───────────────────────────────────────────────────
private void RebuildPanel(string streamingMarkdown = null)
{
ConversationPanel.Children.Clear();
_streamingBubble = null;
foreach (var msg in _conversation)
{
var bubble = CreateMessageBubble(msg.Role == ChatRole.User, msg.Text ?? string.Empty);
ConversationPanel.Children.Add(bubble);
}
if (!string.IsNullOrEmpty(streamingMarkdown))
{
_showLoadingBubble = false;
_streamingBubble = CreateMessageBubble(isUser: false, markdown: streamingMarkdown, isStreaming: true);
ConversationPanel.Children.Add(_streamingBubble);
SetViewState(ViewState.Content);
}
else if (_showLoadingBubble)
{
ConversationPanel.Children.Add(CreateLoadingBubble());
SetViewState(ViewState.Content);
}
else
{
SetViewState(_conversation.Count > 0 ? ViewState.Content : ViewState.Empty);
}
ScrollToBottom();
}
private Border CreateMessageBubble(bool isUser, string markdown, bool isStreaming = false)
{
bool dark = IsDarkTheme();
var mutedColor = dark ? Color.FromRgb(0x9c, 0xa3, 0xaf) : Color.FromRgb(0x6b, 0x72, 0x80);
var copyBorderColor = dark ? Color.FromRgb(0x55, 0x55, 0x55) : Color.FromRgb(0xd1, 0xd5, 0xdb);
var roleLabel = new TextBlock
{
Text = isUser ? "You" : "AI",
FontSize = 10,
FontWeight = FontWeights.SemiBold,
Foreground = new SolidColorBrush(mutedColor),
VerticalAlignment = VerticalAlignment.Center,
};
// Copy button next to the role label
var copyButton = new Button
{
Content = "Copy",
FontSize = 10,
Padding = new Thickness(6, 1, 6, 1),
Cursor = Cursors.Hand,
Background = Brushes.Transparent,
BorderThickness = new Thickness(1),
BorderBrush = new SolidColorBrush(copyBorderColor),
Foreground = new SolidColorBrush(mutedColor),
VerticalAlignment = VerticalAlignment.Center,
Focusable = false,
};
var capturedMarkdown = markdown;
copyButton.Click += (s, e) => Clipboard.SetText(capturedMarkdown);
// Header row: role label on the left, copy button on the right
var headerGrid = new Grid { Margin = new Thickness(0, 0, 0, 5) };
headerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
headerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
Grid.SetColumn(roleLabel, 0);
Grid.SetColumn(copyButton, 1);
headerGrid.Children.Add(roleLabel);
headerGrid.Children.Add(copyButton);
var contentPanel = new StackPanel();
RenderMarkdownInto(contentPanel, markdown);
var innerStack = new StackPanel();
innerStack.Children.Add(headerGrid);
innerStack.Children.Add(contentPanel);
// Right-click context menu
var contextMenu = new ContextMenu();
var copyMenuItem = new MenuItem { Header = "Copy" };
copyMenuItem.Click += (s, e) => Clipboard.SetText(capturedMarkdown);
contextMenu.Items.Add(copyMenuItem);
return new Border
{
Child = innerStack,
CornerRadius = new CornerRadius(8),
Background = isUser
? new SolidColorBrush(dark ? Color.FromRgb(0x1c, 0x45, 0x6b) : Color.FromRgb(0xdb, 0xe4, 0xfc))
: new SolidColorBrush(dark ? Color.FromRgb(0x2d, 0x2d, 0x30) : Color.FromRgb(0xf3, 0xf4, 0xf6)),
BorderBrush = isUser
? new SolidColorBrush(dark ? Color.FromRgb(0x2d, 0x5f, 0x8a) : Color.FromRgb(0x93, 0xc5, 0xfd))
: new SolidColorBrush(dark ? Color.FromRgb(0x3c, 0x3c, 0x3c) : Color.FromRgb(0xd1, 0xd5, 0xdb)),
BorderThickness = new Thickness(1),
Padding = new Thickness(12, 10, 12, 10),
Margin = new Thickness(0, 0, 0, 10),
Opacity = isStreaming ? 0.9 : 1.0,
ContextMenu = contextMenu,
};
}
private UIElement CreateLoadingBubble()
{
bool dark = IsDarkTheme();
var mutedColor = dark ? Color.FromRgb(0x9c, 0xa3, 0xaf) : Color.FromRgb(0x6b, 0x72, 0x80);
var dotsPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 2, 0, 2) };
for (int i = 0; i < 3; i++)
{
var dot = new Ellipse
{
Width = 7,
Height = 7,
Fill = new SolidColorBrush(mutedColor),
Margin = new Thickness(2, 0, 2, 0),
Opacity = 0.3,
};
var anim = new DoubleAnimation
{
From = 0.3,
To = 1.0,
Duration = TimeSpan.FromMilliseconds(600),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever,
BeginTime = TimeSpan.FromMilliseconds(i * 160),
};
dot.BeginAnimation(OpacityProperty, anim);
dotsPanel.Children.Add(dot);
}
var roleLabel = new TextBlock
{
Text = "AI",
FontSize = 10,
FontWeight = FontWeights.SemiBold,
Foreground = new SolidColorBrush(mutedColor),
Margin = new Thickness(0, 0, 0, 5),
};
var inner = new StackPanel();
inner.Children.Add(roleLabel);
inner.Children.Add(dotsPanel);
return new Border
{
Child = inner,
CornerRadius = new CornerRadius(8),
Background = new SolidColorBrush(dark ? Color.FromRgb(0x2d, 0x2d, 0x30) : Color.FromRgb(0xf3, 0xf4, 0xf6)),
BorderBrush = new SolidColorBrush(dark ? Color.FromRgb(0x3c, 0x3c, 0x3c) : Color.FromRgb(0xd1, 0xd5, 0xdb)),
BorderThickness = new Thickness(1),
Padding = new Thickness(12, 10, 12, 10),
Margin = new Thickness(0, 0, 0, 10),
};
}
// ── Markdown → WPF ────────────────────────────────────────────────────
// Matches fenced code blocks: ```[lang]\n...\n```
private static readonly Regex _codeBlockRegex = new Regex(
@"```[^\n]*\n([\s\S]*?)```", RegexOptions.Compiled);
// Matches **bold**, `inline code`, ~~strikethrough~~, [link](url), *italic*
private static readonly Regex _inlineRegex = new Regex(
@"\*\*(.+?)\*\*|`([^`]+)`|~~(.+?)~~|\[([^\]]+)\]\(([^)]+)\)|\*(.+?)\*", RegexOptions.Compiled);
private static readonly Regex _numberedListRegex = new Regex(
@"^(\s*)(\d+)\.\s+(.*)", RegexOptions.Compiled);
private static readonly Regex _nestedBulletRegex = new Regex(
@"^(\s{2,})[-*]\s+(.*)", RegexOptions.Compiled);
private static readonly Regex _horizontalRuleRegex = new Regex(
@"^\s*(?:---+|\*\*\*+|___+)\s*$", RegexOptions.Compiled);
private static readonly Regex _tableSeparatorRegex = new Regex(
@"^\|[\s:]*-+[\s:]*(\|[\s:]*-+[\s:]*)*\|?\s*$", RegexOptions.Compiled);
private void RenderMarkdownInto(StackPanel panel, string markdown)
{
if (string.IsNullOrEmpty(markdown)) return;
// Split on fenced code blocks; capture groups are interleaved in result
var parts = _codeBlockRegex.Split(markdown);
for (int i = 0; i < parts.Length; i++)
{
if (i % 2 == 0)
RenderTextSection(panel, parts[i]);
else
RenderCodeBlock(panel, parts[i]);
}
}
private void RenderTextSection(StackPanel panel, string text)
{
if (string.IsNullOrWhiteSpace(text)) return;
var lines = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
var buffer = new List<string>();
var tableBuffer = new List<string>();
var blockquoteBuffer = new List<string>();
foreach (var line in lines)
{
// Table lines: start and end with |
var trimmed = line.Trim();
if (trimmed.StartsWith("|") && trimmed.EndsWith("|"))
{
FlushBuffer(panel, buffer);
FlushBlockquoteBuffer(panel, blockquoteBuffer);
tableBuffer.Add(line);
continue;
}
if (tableBuffer.Count > 0)
{
RenderTable(panel, tableBuffer);
tableBuffer.Clear();
}
// Blockquote lines
if (line.StartsWith("> ") || line == ">")
{
FlushBuffer(panel, buffer);
blockquoteBuffer.Add(line.Length > 2 ? line.Substring(2) : "");
continue;
}
if (blockquoteBuffer.Count > 0)
{
FlushBlockquoteBuffer(panel, blockquoteBuffer);
}
// Horizontal rule
if (_horizontalRuleRegex.IsMatch(line))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeHorizontalRule());
}
// Headings
else if (line.StartsWith("#### "))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeHeading(line.Substring(5), 13, FontWeights.SemiBold));
}
else if (line.StartsWith("### "))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeHeading(line.Substring(4), 14, FontWeights.SemiBold));
}
else if (line.StartsWith("## "))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeHeading(line.Substring(3), 16, FontWeights.Bold));
}
else if (line.StartsWith("# "))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeHeading(line.Substring(2), 18, FontWeights.Bold));
}
// Unordered list (top level)
else if (line.StartsWith("- ") || line.StartsWith("* "))
{
FlushBuffer(panel, buffer);
panel.Children.Add(MakeBulletRow(line.Substring(2)));
}
// Nested unordered list
else if (_nestedBulletRegex.IsMatch(line))
{
FlushBuffer(panel, buffer);
var match = _nestedBulletRegex.Match(line);
var indent = match.Groups[1].Value.Length / 2;
panel.Children.Add(MakeBulletRow(match.Groups[2].Value, indent));
}
// Numbered list
else if (_numberedListRegex.IsMatch(line))
{
FlushBuffer(panel, buffer);
var match = _numberedListRegex.Match(line);
var indent = match.Groups[1].Value.Length >= 2 ? match.Groups[1].Value.Length / 2 : 0;
panel.Children.Add(MakeNumberedRow(match.Groups[2].Value, match.Groups[3].Value, indent));
}
else
{
buffer.Add(line);
}
}
// Flush remaining buffers
if (tableBuffer.Count > 0) RenderTable(panel, tableBuffer);
FlushBlockquoteBuffer(panel, blockquoteBuffer);
FlushBuffer(panel, buffer);
}
private void FlushBuffer(StackPanel panel, List<string> buffer)
{
if (buffer.Count == 0) return;
// Trim surrounding blank lines
int s = 0, e = buffer.Count - 1;
while (s <= e && string.IsNullOrWhiteSpace(buffer[s])) s++;
while (e >= s && string.IsNullOrWhiteSpace(buffer[e])) e--;
if (s <= e)
{
var joined = string.Join("\n", buffer.GetRange(s, e - s + 1));
if (!string.IsNullOrWhiteSpace(joined))
{
var tb = MakeInlineTextBlock(joined);
tb.Margin = new Thickness(0, 0, 0, 6);
panel.Children.Add(tb);
}
}
buffer.Clear();
}
private void FlushBlockquoteBuffer(StackPanel panel, List<string> blockquoteBuffer)
{
if (blockquoteBuffer.Count == 0) return;
bool dark = IsDarkTheme();
var borderColor = dark ? Color.FromRgb(0x4a, 0x4a, 0x4a) : Color.FromRgb(0xd1, 0xd5, 0xdb);
var bgColor = dark ? Color.FromArgb(30, 255, 255, 255) : Color.FromArgb(20, 0, 0, 0);
var innerPanel = new StackPanel();
var joinedText = string.Join("\n", blockquoteBuffer);
RenderTextSection(innerPanel, joinedText);
panel.Children.Add(new Border
{
Child = innerPanel,
BorderBrush = new SolidColorBrush(borderColor),
BorderThickness = new Thickness(3, 0, 0, 0),
Background = new SolidColorBrush(bgColor),
Padding = new Thickness(12, 6, 12, 6),
Margin = new Thickness(0, 4, 0, 6),
});
blockquoteBuffer.Clear();
}
private FrameworkElement MakeHeading(string text, double size, FontWeight weight)
{
return MakeSelectableRtb(text, fontSize: size, fontWeight: weight,
margin: new Thickness(0, 8, 0, 4));
}
private UIElement MakeBulletRow(string text, int indent = 0)
{
var grid = new Grid { Margin = new Thickness(indent * 16, 0, 0, 2) };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(16) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
var bullet = new TextBlock
{
Text = "•",
Foreground = _textBrush ?? Brushes.WhiteSmoke,
VerticalAlignment = VerticalAlignment.Top,
};
Grid.SetColumn(bullet, 0);
var content = MakeInlineTextBlock(text);
Grid.SetColumn(content, 1);
grid.Children.Add(bullet);
grid.Children.Add(content);
return grid;
}
private UIElement MakeNumberedRow(string number, string text, int indent = 0)
{
var grid = new Grid { Margin = new Thickness(indent * 16, 0, 0, 2) };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(24) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
var numberBlock = new TextBlock
{
Text = number + ".",
Foreground = _textBrush ?? Brushes.WhiteSmoke,
VerticalAlignment = VerticalAlignment.Top,
};
Grid.SetColumn(numberBlock, 0);
var content = MakeInlineTextBlock(text);
Grid.SetColumn(content, 1);
grid.Children.Add(numberBlock);
grid.Children.Add(content);
return grid;
}
private UIElement MakeHorizontalRule()
{
bool dark = IsDarkTheme();
return new Border
{
Height = 1,
Background = new SolidColorBrush(dark
? Color.FromRgb(0x4a, 0x4a, 0x4a)
: Color.FromRgb(0xd1, 0xd5, 0xdb)),
Margin = new Thickness(0, 8, 0, 8),
};
}
private void RenderCodeBlock(StackPanel panel, string code)
{
bool dark = IsDarkTheme();
var codeTextColor = dark ? Color.FromRgb(0xf3, 0xf3, 0xf3) : Color.FromRgb(0x1f, 0x29, 0x37);
var codeBgColor = dark ? Color.FromRgb(0x1e, 0x1e, 0x1e) : Color.FromRgb(0xf0, 0xf0, 0xf0);
var copyBgColor = dark ? Color.FromArgb(200, 0x3c, 0x3c, 0x3c) : Color.FromArgb(200, 0xe5, 0xe7, 0xeb);
var copyBorderColor = dark ? Color.FromRgb(0x55, 0x55, 0x55) : Color.FromRgb(0xd1, 0xd5, 0xdb);
var mutedColor = dark ? Color.FromRgb(0x9c, 0xa3, 0xaf) : Color.FromRgb(0x6b, 0x72, 0x80);
var codeText = code.TrimEnd('\r', '\n');
var tb = new TextBox
{
Text = codeText,
FontFamily = new FontFamily("Consolas, Courier New"),
FontSize = 12,
Foreground = new SolidColorBrush(codeTextColor),
TextWrapping = TextWrapping.Wrap,
Padding = new Thickness(10),
IsReadOnly = true,
Background = Brushes.Transparent,
BorderThickness = new Thickness(0),
IsTabStop = false,
};
var copyBtn = new Button
{
Content = "Copy",
FontSize = 10,
Padding = new Thickness(6, 2, 6, 2),
Cursor = Cursors.Hand,
Background = new SolidColorBrush(copyBgColor),
BorderThickness = new Thickness(1),
BorderBrush = new SolidColorBrush(copyBorderColor),
Foreground = new SolidColorBrush(mutedColor),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(0, 6, 6, 0),
Focusable = false,
};
copyBtn.Click += (s, e) =>
{
_ = ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
Clipboard.SetText(codeText);
copyBtn.Content = "Copied!";
await Task.Delay(1500);
copyBtn.Content = "Copy";
});
};
var overlay = new Grid();
overlay.Children.Add(tb);
overlay.Children.Add(copyBtn);
panel.Children.Add(new Border
{
Child = overlay,
Background = new SolidColorBrush(codeBgColor),
CornerRadius = new CornerRadius(4),
Margin = new Thickness(0, 4, 0, 6),
});
}
private void RenderTable(StackPanel panel, List<string> tableLines)
{
if (tableLines.Count < 2)
{
// Not enough lines for a table — render as plain text
foreach (var line in tableLines)
{
var tb = MakeInlineTextBlock(line);
tb.Margin = new Thickness(0, 0, 0, 6);
panel.Children.Add(tb);
}
return;
}
var rows = new List<string[]>();
int separatorIndex = -1;
for (int i = 0; i < tableLines.Count; i++)
{
var line = tableLines[i].Trim();
if (_tableSeparatorRegex.IsMatch(line))
{
separatorIndex = i;
continue;
}
var cells = ParseTableCells(line);
if (cells.Length > 0)
rows.Add(cells);
}
if (rows.Count == 0) return;
int columnCount = rows.Max(r => r.Length);
bool hasHeader = separatorIndex == 1;
bool dark = IsDarkTheme();
var borderColor = dark ? Color.FromRgb(0x4a, 0x4a, 0x4a) : Color.FromRgb(0xd1, 0xd5, 0xdb);
var headerBg = dark ? Color.FromArgb(40, 255, 255, 255) : Color.FromArgb(30, 0, 0, 0);
var grid = new Grid();
for (int c = 0; c < columnCount; c++)
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
for (int r = 0; r < rows.Count; r++)
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
for (int r = 0; r < rows.Count; r++)
{
for (int c = 0; c < columnCount; c++)
{
var cellText = c < rows[r].Length ? rows[r][c] : "";
var cellContent = MakeInlineTextBlock(cellText);
var cellBorder = new Border
{
Child = cellContent,
BorderBrush = new SolidColorBrush(borderColor),
BorderThickness = new Thickness(
c == 0 ? 1 : 0,
r == 0 ? 1 : 0,
1,
1),
Padding = new Thickness(8, 4, 8, 4),
};
if (hasHeader && r == 0)
{
cellBorder.Background = new SolidColorBrush(headerBg);
var rtb = cellContent as RichTextBox;
if (rtb != null)
rtb.FontWeight = FontWeights.SemiBold;
}
Grid.SetRow(cellBorder, r);
Grid.SetColumn(cellBorder, c);
grid.Children.Add(cellBorder);
}
}
panel.Children.Add(new Border
{
Child = grid,
Margin = new Thickness(0, 4, 0, 6),
});
}
private static string[] ParseTableCells(string line)
{
var trimmed = line.Trim();
if (!trimmed.StartsWith("|")) return new string[0];
trimmed = trimmed.Substring(1);
if (trimmed.EndsWith("|"))
trimmed = trimmed.Substring(0, trimmed.Length - 1);
return trimmed.Split('|').Select(c => c.Trim()).ToArray();
}
private FrameworkElement MakeInlineTextBlock(string text)
{
return MakeSelectableRtb(text, fontSize: 13);
}
// Creates a read-only, transparent RichTextBox that supports text selection
// while preserving inline formatting (bold, italic, inline code).
private RichTextBox MakeSelectableRtb(string text, double fontSize = 13,
FontWeight? fontWeight = null,
Thickness? margin = null)
{
var para = new Paragraph { Margin = new Thickness(0) };
AddInlines(para.Inlines, text);
var doc = new FlowDocument(para) { PagePadding = new Thickness(0) };
var rtb = new RichTextBox(doc)
{
IsReadOnly = true,
Background = Brushes.Transparent,
BorderThickness = new Thickness(0),
Padding = new Thickness(0),
FontSize = fontSize,
Foreground = _textBrush ?? Brushes.WhiteSmoke,
VerticalScrollBarVisibility = ScrollBarVisibility.Disabled,
HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
IsTabStop = false,
Margin = margin ?? new Thickness(0),
};
if (fontWeight.HasValue)
rtb.FontWeight = fontWeight.Value;
return rtb;
}
private void AddInlines(InlineCollection inlines, string text)
{
int last = 0;
foreach (Match m in _inlineRegex.Matches(text))
{
if (m.Index > last)
inlines.Add(new Run(text.Substring(last, m.Index - last)));
if (m.Groups[1].Success) // **bold**
{
inlines.Add(new Bold(new Run(m.Groups[1].Value)));
}
else if (m.Groups[2].Success) // `inline code`
{
inlines.Add(new Run(m.Groups[2].Value)
{
FontFamily = new FontFamily("Consolas, Courier New"),
FontSize = 12,
Background = IsDarkTheme()
? new SolidColorBrush(Color.FromArgb(50, 255, 255, 255))
: new SolidColorBrush(Color.FromArgb(40, 0, 0, 0)),
});
}
else if (m.Groups[3].Success) // ~~strikethrough~~
{
inlines.Add(new Run(m.Groups[3].Value)
{
TextDecorations = TextDecorations.Strikethrough,
});
}
else if (m.Groups[4].Success) // [link](url)
{
var hyperlink = new Hyperlink(new Run(m.Groups[4].Value))
{
Foreground = new SolidColorBrush(IsDarkTheme()
? Color.FromRgb(0x58, 0xa6, 0xff)
: Color.FromRgb(0x09, 0x69, 0xda)),
};
Uri uri;
if (Uri.TryCreate(m.Groups[5].Value, UriKind.Absolute, out uri))
{
hyperlink.NavigateUri = uri;
hyperlink.RequestNavigate += (s, e) =>
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = e.Uri.AbsoluteUri,
UseShellExecute = true,
});
e.Handled = true;
};
}
inlines.Add(hyperlink);
}
else if (m.Groups[6].Success) // *italic*
{
inlines.Add(new Italic(new Run(m.Groups[6].Value)));
}
last = m.Index + m.Length;
}
if (last < text.Length)
inlines.Add(new Run(text.Substring(last)));
}
// ── Helpers ───────────────────────────────────────────────────────────
private void SetViewState(ViewState state)
{
EmptyStatePanel.Visibility = state == ViewState.Empty ? Visibility.Visible : Visibility.Collapsed;
}
private void ScrollToBottom()
{
ConversationScroll.UpdateLayout();
ConversationScroll.ScrollToBottom();
}
}
}