Repository navigation
Expand file tree
/
Copy pathmessage_widgets.py
More file actions
1099 lines (990 loc) · 37.9 KB
/
Copy pathmessage_widgets.py
File metadata and controls
1099 lines (990 loc) · 37.9 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
"""Markdown message rendering for ChickenButt (native GTK widgets).
Rendering strategy (picked for us after reading Alpaca + Newelle source):
* **Prose** — Alpaca-style *commit on completed line*: incomplete lines stay in a
carry buffer; on ``\\n`` we format Markdown (bold/code/lists) into a stable
TextView via TextTags. No full-message reparse every token.
* **Code fences** — ChickenButt hybrid: open a capped CodeBlock as soon as the
opening fence line completes (avoids Alpaca’s tall raw-generating growth),
then TextBuffer.insert into that card. Height is content-based with max 320px.
* **Finish** — flush carry + close open fence; do not destroy/rebuild the bubble.
* **Not chosen** — Newelle full reparse + widget-diff (good, heavier); WebKitGTK
(fallback only if native stays too janky).
Same-stack references (read-only, GPL — reimplement shapes, don’t paste):
Alpaca: GeneratingText.process_content, text_to_block_list
Newelle: get_message_chunks, widgets_map, _render_serial
"""
from __future__ import annotations
import os
import sys
from typing import Any
import gi
gi.require_version("Gtk", "4.0")
gi.require_version("Gdk", "4.0")
from gi.repository import Gdk, Gio, GLib, GObject, Gtk, Pango
# Vendored pure-Python markdown
_VENDOR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vendor")
if _VENDOR not in sys.path:
sys.path.insert(0, _VENDOR)
import mistune # noqa: E402
# Optional syntax highlighting
_GtkSource = None
try:
gi.require_version("GtkSource", "5")
from gi.repository import GtkSource as _GtkSource # type: ignore
except (ValueError, ImportError):
_GtkSource = None
_MD = mistune.create_markdown(renderer="ast")
MD_CSS = b"""
/* Base scale for assistant markdown (slightly larger than UI chrome) */
.md-view {
font-size: 15px;
}
.md-block {
margin-bottom: 6px;
font-size: 15px;
}
.md-h1 {
font-size: 1.35em;
font-weight: 700;
margin-top: 6px;
margin-bottom: 4px;
}
.md-h2 {
font-size: 1.22em;
font-weight: 700;
margin-top: 6px;
}
.md-h3 {
font-size: 1.1em;
font-weight: 700;
}
.md-paragraph {
font-size: 15px;
line-height: 1.5;
}
.md-list {
margin-left: 4px;
font-size: 15px;
}
.md-li {
margin-bottom: 4px;
font-size: 15px;
line-height: 1.45;
}
.md-quote {
border-left: 3px solid alpha(@window_fg_color, 0.25);
padding-left: 10px;
opacity: 0.9;
font-style: italic;
font-size: 15px;
line-height: 1.45;
}
.md-hr {
min-height: 1px;
background-color: alpha(@window_fg_color, 0.15);
margin: 10px 0;
}
.md-inline-code {
font-family: monospace;
font-size: 0.95em;
background-color: alpha(@window_fg_color, 0.10);
border-radius: 4px;
padding: 1px 5px;
}
.code-block {
border-radius: 10px;
/* Theme-aware fill so light mode is not stuck on dark code chrome */
background-color: alpha(@window_fg_color, 0.06);
border: 1px solid alpha(@window_fg_color, 0.12);
margin: 8px 0 6px 0;
}
.code-block-header {
padding: 6px 10px;
background-color: alpha(@window_fg_color, 0.08);
border-bottom: 1px solid alpha(@window_fg_color, 0.10);
}
.code-lang {
font-size: 0.85em;
font-weight: 600;
opacity: 0.7;
font-family: monospace;
}
.code-view {
font-family: monospace;
font-size: 13.5px;
padding: 10px 12px;
background: transparent;
}
.code-view textview,
.code-view text {
background: transparent;
font-family: monospace;
font-size: 13.5px;
}
.copy-toast {
opacity: 0.8;
font-size: 0.85em;
}
.stream-body {
font-size: 15px;
line-height: 1.5;
}
.message-body {
font-size: 15px;
}
/* Streaming surface: never animate size/opacity while tokens arrive */
.message-body,
.message-body *,
.chat-bubble,
.stream-view,
.stream-view textview,
.stream-view text {
transition: none;
animation: none;
}
.stream-view {
font-size: 15px;
background: transparent;
border: none;
box-shadow: none;
padding: 0;
}
.stream-view textview,
.stream-view text {
background: transparent;
color: inherit;
font-size: 15px;
}
"""
_css_installed = False
def ensure_md_css() -> None:
global _css_installed
if _css_installed:
return
provider = Gtk.CssProvider()
provider.load_from_data(MD_CSS)
display = Gdk.Display.get_default()
if display is not None:
Gtk.StyleContext.add_provider_for_display(
display, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
_css_installed = True
def _inline_to_markup(nodes: list[dict[str, Any]] | None) -> str:
"""Convert inline AST nodes to Pango markup (escaped)."""
if not nodes:
return ""
parts: list[str] = []
for node in nodes:
t = node.get("type")
if t == "text":
parts.append(GLib.markup_escape_text(node.get("raw") or ""))
elif t == "strong":
parts.append(f"<b>{_inline_to_markup(node.get('children'))}</b>")
elif t == "emphasis":
parts.append(f"<i>{_inline_to_markup(node.get('children'))}</i>")
elif t == "codespan":
raw = GLib.markup_escape_text(node.get("raw") or "")
parts.append(
f'<span font_family="monospace" bgcolor="#00000030">{raw}</span>'
)
elif t == "link":
children = _inline_to_markup(node.get("children"))
href = GLib.markup_escape_text((node.get("attrs") or {}).get("url") or "")
# Show label; URL as tooltip isn't available on markup alone
if href:
parts.append(f'<a href="{href}">{children or href}</a>')
else:
parts.append(children)
elif t == "strikethrough":
parts.append(f"<s>{_inline_to_markup(node.get('children'))}</s>")
elif t == "linebreak":
parts.append("\n")
elif t == "softbreak":
parts.append(" ")
elif t == "image":
alt = (node.get("attrs") or {}).get("alt") or "image"
parts.append(GLib.markup_escape_text(f"[image: {alt}]"))
else:
if node.get("children"):
parts.append(_inline_to_markup(node.get("children")))
elif node.get("raw"):
parts.append(GLib.markup_escape_text(node.get("raw") or ""))
return "".join(parts)
def _make_label(markup: str, *css_classes: str) -> Gtk.Label:
lab = Gtk.Label()
lab.set_markup(markup)
lab.set_wrap(True)
lab.set_wrap_mode(Pango.WrapMode.WORD_CHAR)
lab.set_xalign(0.0)
lab.set_selectable(True)
lab.set_hexpand(True)
lab.set_halign(Gtk.Align.FILL)
lab.set_max_width_chars(-1)
lab.add_css_class("md-block")
for c in css_classes:
lab.add_css_class(c)
return lab
_LANG_ALIASES = {
"js": "javascript",
"ts": "typescript",
"py": "python",
"sh": "sh",
"bash": "sh",
"shell": "sh",
"c++": "cpp",
"rs": "rust",
"html": "html",
"css": "css",
}
# Natural height up to this cap; long snippets scroll inside the card.
CODE_BLOCK_MIN_HEIGHT = 100
CODE_BLOCK_MAX_HEIGHT = 320
class CodeBlock(Gtk.Box):
"""Fenced code card — content-sized height, never steals vertical space from the window."""
def __init__(self, code: str = "", language: str = "", *, show_run: bool = False):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=0)
ensure_md_css()
self.add_css_class("code-block")
self._language = (language or "").strip()
# Grow horizontally with the bubble; do not expand vertically with the window
self.set_hexpand(True)
self.set_halign(Gtk.Align.FILL)
self.set_vexpand(False)
self.set_valign(Gtk.Align.START)
header = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
header.add_css_class("code-block-header")
header.set_hexpand(True)
header.set_vexpand(False)
header.set_valign(Gtk.Align.START)
self._lang_label = Gtk.Label(label=self._language or "code")
self._lang_label.add_css_class("code-lang")
self._lang_label.set_halign(Gtk.Align.START)
self._lang_label.set_hexpand(True)
header.append(self._lang_label)
self._copy_btn = Gtk.Button.new_from_icon_name("edit-copy-symbolic")
self._copy_btn.add_css_class("flat")
self._copy_btn.set_has_frame(False)
self._copy_btn.set_tooltip_text("Copy code")
self._copy_btn.set_size_request(32, 32)
self._copy_btn.connect("clicked", self._on_copy)
self._copy_btn.set_cursor_from_name("pointer")
header.append(self._copy_btn)
self._expand_btn = Gtk.Button.new_from_icon_name("view-fullscreen-symbolic")
self._expand_btn.add_css_class("flat")
self._expand_btn.set_has_frame(False)
self._expand_btn.set_tooltip_text("Expand code")
self._expand_btn.set_size_request(32, 32)
self._expand_btn.set_visible(False)
self._expand_btn.connect("clicked", self._on_expand)
self._expand_btn.set_cursor_from_name("pointer")
header.append(self._expand_btn)
self._expanded = False
self.append(header)
# Collapsed: cap height. Expanded: natural height, outer chat scrolls.
self._scroll = Gtk.ScrolledWindow()
self._scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER)
self._scroll.set_propagate_natural_height(True)
self._scroll.set_min_content_height(0)
self._scroll.set_max_content_height(CODE_BLOCK_MAX_HEIGHT)
self._scroll.set_hexpand(True)
self._scroll.set_vexpand(False)
self._scroll.set_valign(Gtk.Align.START)
self._scroll.set_halign(Gtk.Align.FILL)
if _GtkSource is not None:
self._buffer = _GtkSource.Buffer()
self._apply_language(self._language)
scheme_mgr = _GtkSource.StyleSchemeManager.get_default()
for name in ("Adwaita-dark", "Adwaita", "classic"):
scheme = scheme_mgr.get_scheme(name)
if scheme is not None:
self._buffer.set_style_scheme(scheme)
break
self._view = _GtkSource.View.new_with_buffer(self._buffer)
self._view.set_show_line_numbers(False)
else:
self._buffer = Gtk.TextBuffer()
self._view = Gtk.TextView.new_with_buffer(self._buffer)
self._view.set_editable(False)
self._view.set_cursor_visible(False)
self._view.set_monospace(True)
self._view.set_wrap_mode(Gtk.WrapMode.NONE)
self._view.set_vexpand(False)
self._view.set_hexpand(True)
self._view.set_valign(Gtk.Align.START)
self._view.set_top_margin(8)
self._view.set_bottom_margin(8)
self._view.set_left_margin(10)
self._view.set_right_margin(10)
self._view.add_css_class("code-view")
self._scroll.set_child(self._view)
self.append(self._scroll)
end = self._buffer.get_end_iter()
self._end_mark = self._buffer.create_mark("code-end", end, False)
if code:
self.append_text(code if code.endswith("\n") else code)
def _apply_language(self, language: str) -> None:
if _GtkSource is None or not isinstance(self._buffer, _GtkSource.Buffer):
return
language = (language or "").strip()
if not language:
return
lm = _GtkSource.LanguageManager.get_default()
lang = lm.get_language(language.lower())
if lang is None:
lang = lm.get_language(_LANG_ALIASES.get(language.lower(), ""))
if lang is not None:
self._buffer.set_language(lang)
def set_language(self, language: str) -> None:
self._language = (language or "").strip()
self._lang_label.set_text(self._language or "code")
self._apply_language(self._language)
def append_text(self, text: str) -> None:
if not text:
return
end = self._buffer.get_end_iter()
self._buffer.insert(end, text)
end = self._buffer.get_end_iter()
self._buffer.move_mark(self._end_mark, end)
self._view.scroll_to_mark(self._end_mark, 0.0, False, 0.0, 1.0)
def get_code(self) -> str:
return self._buffer.get_text(
self._buffer.get_start_iter(), self._buffer.get_end_iter(), False
)
def finalize(self) -> None:
"""Post-stream polish — do not recreate the widget."""
self._apply_language(self._language)
self._copy_btn.set_sensitive(True)
GLib.idle_add(self._maybe_enable_expand)
def _line_count(self) -> int:
return self.get_code().count("\n") + (1 if self.get_code() else 0)
def _maybe_enable_expand(self) -> bool:
# Long blocks: offer expand (no nested vertical scroll when expanded)
long = self._line_count() > 14 or len(self.get_code()) > 600
self._expand_btn.set_visible(long)
if long and not self._expanded:
self._scroll.set_max_content_height(CODE_BLOCK_MAX_HEIGHT)
self._expand_btn.set_icon_name("view-fullscreen-symbolic")
self._expand_btn.set_tooltip_text("Expand code")
elif not long:
self._scroll.set_max_content_height(10000)
return False
def _on_expand(self, _btn: Gtk.Button) -> None:
self._expanded = not self._expanded
if self._expanded:
# Full height — outer transcript scrolls
self._scroll.set_max_content_height(100000)
self._scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER)
self._expand_btn.set_icon_name("view-restore-symbolic")
self._expand_btn.set_tooltip_text("Collapse code")
else:
self._scroll.set_max_content_height(CODE_BLOCK_MAX_HEIGHT)
self._scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER)
self._expand_btn.set_icon_name("view-fullscreen-symbolic")
self._expand_btn.set_tooltip_text("Expand code")
def _on_copy(self, _btn: Gtk.Button) -> None:
display = Gdk.Display.get_default()
if display is None:
return
display.get_clipboard().set(self.get_code().rstrip("\n"))
# Prefer checkmark symbolic; fall back if theme lacks it
check = "object-select-symbolic"
try:
theme = Gtk.IconTheme.get_for_display(display)
if theme is not None and not theme.has_icon(check):
if theme.has_icon("emblem-ok-symbolic"):
check = "emblem-ok-symbolic"
elif theme.has_icon("checkbox-checked-symbolic"):
check = "checkbox-checked-symbolic"
except Exception: # noqa: BLE001
pass
self._copy_btn.set_icon_name(check)
self._copy_btn.set_tooltip_text("Copied")
GLib.timeout_add(1500, self._reset_copy_label)
def _reset_copy_label(self) -> bool:
self._copy_btn.set_icon_name("edit-copy-symbolic")
self._copy_btn.set_tooltip_text("Copy code")
return False
def _append_blocks(parent: Gtk.Box, nodes: list[dict[str, Any]] | None) -> None:
if not nodes:
return
for node in nodes:
t = node.get("type")
if t == "blank_line":
continue
if t == "heading":
level = (node.get("attrs") or {}).get("level") or 1
cls = {1: "md-h1", 2: "md-h2"}.get(level, "md-h3")
parent.append(
_make_label(_inline_to_markup(node.get("children")), "md-block", cls)
)
elif t == "paragraph":
parent.append(
_make_label(
_inline_to_markup(node.get("children")), "md-block", "md-paragraph"
)
)
elif t == "block_code":
info = ((node.get("attrs") or {}).get("info") or "").split()
lang = info[0] if info else ""
parent.append(CodeBlock(node.get("raw") or "", lang))
elif t == "block_quote":
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
box.add_css_class("md-quote")
box.add_css_class("md-block")
_append_blocks(box, node.get("children"))
parent.append(box)
elif t == "list":
ordered = (node.get("attrs") or {}).get("ordered")
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
box.add_css_class("md-list")
box.add_css_class("md-block")
for i, item in enumerate(node.get("children") or [], start=1):
if item.get("type") != "list_item":
continue
row = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
bullet = Gtk.Label(label=f"{i}." if ordered else "•")
bullet.set_valign(Gtk.Align.START)
bullet.set_xalign(0.0)
body = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=2)
body.set_hexpand(True)
_append_blocks(body, item.get("children"))
# list_item often wraps block_text
if not body.get_first_child():
body.append(
_make_label(
_inline_to_markup(item.get("children")), "md-li"
)
)
row.append(bullet)
row.append(body)
box.append(row)
parent.append(box)
elif t == "block_text":
parent.append(
_make_label(_inline_to_markup(node.get("children")), "md-li")
)
elif t == "thematic_break":
sep = Gtk.Box()
sep.add_css_class("md-hr")
sep.add_css_class("md-block")
sep.set_size_request(-1, 1)
parent.append(sep)
elif t == "table":
# Flatten tables to monospace text for now
parent.append(
_make_label(
GLib.markup_escape_text(_table_to_text(node)),
"md-block",
"md-paragraph",
)
)
else:
if node.get("children"):
_append_blocks(parent, node.get("children"))
elif node.get("raw"):
parent.append(
_make_label(
GLib.markup_escape_text(node.get("raw") or ""),
"md-block",
"md-paragraph",
)
)
def _table_to_text(node: dict[str, Any]) -> str:
import re
rows: list[str] = []
for child in node.get("children") or []:
cells: list[str] = []
for cell in child.get("children") or []:
markup = _inline_to_markup(cell.get("children"))
cells.append(re.sub(r"<[^>]+>", "", markup))
rows.append(" | ".join(cells))
return "\n".join(rows)
def prepare_partial_markdown(text: str) -> str:
"""Close open fences so incomplete streams still render as code blocks."""
if not text:
return text
# Count fenced openers (``` or ~~~) on their own line-ish starts
fence_count = 0
for line in text.splitlines():
stripped = line.lstrip()
if stripped.startswith("```") or stripped.startswith("~~~"):
fence_count += 1
if fence_count % 2 == 1:
# Prefer matching the open fence style
last = "```"
for line in reversed(text.splitlines()):
s = line.lstrip()
if s.startswith("```"):
last = "```"
break
if s.startswith("~~~"):
last = "~~~"
break
if not text.endswith("\n"):
text += "\n"
text += last
return text
class MarkdownView(Gtk.Box):
"""Renders a full markdown string as native widgets."""
def __init__(self, text: str = ""):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=4)
ensure_md_css()
self.add_css_class("md-view")
self.set_hexpand(True)
self.set_halign(Gtk.Align.FILL)
self.set_valign(Gtk.Align.START)
self._last_src = ""
if text:
self.set_markdown(text)
def clear(self) -> None:
while child := self.get_first_child():
self.remove(child)
self._last_src = ""
def set_markdown(self, text: str, *, partial: bool = False) -> None:
src = prepare_partial_markdown(text) if partial else text
# Skip no-op rebuilds (live stream can re-fire same snapshot)
if src == self._last_src and self.get_first_child() is not None:
return
# clear() wipes _last_src — rebuild then remember
while child := self.get_first_child():
self.remove(child)
self._last_src = src
if not (src or "").strip():
return
try:
ast = _MD(src)
except Exception:
self.append(
_make_label(GLib.markup_escape_text(text), "md-block", "md-paragraph")
)
return
if not isinstance(ast, list):
self.append(
_make_label(GLib.markup_escape_text(text), "md-block", "md-paragraph")
)
return
_append_blocks(self, ast)
def _install_prose_tags(buf: Gtk.TextBuffer) -> dict[str, Gtk.TextTag]:
"""Create TextTags for lightweight streaming Markdown."""
table = buf.get_tag_table()
tags: dict[str, Gtk.TextTag] = {}
def ensure(name: str, **props) -> Gtk.TextTag:
existing = table.lookup(name)
if existing is not None:
return existing
return buf.create_tag(name, **props)
tags["bold"] = ensure("bold", weight=Pango.Weight.BOLD)
tags["italic"] = ensure("italic", style=Pango.Style.ITALIC)
tags["inline-code"] = ensure(
"inline-code",
family="monospace",
background="rgba(0,0,0,0.28)",
)
tags["list-item"] = ensure(
"list-item",
left_margin=18,
indent=-12,
)
tags["heading"] = ensure(
"heading",
weight=Pango.Weight.BOLD,
scale=1.08,
)
return tags
def _new_prose_view() -> tuple[Gtk.TextView, Gtk.TextBuffer, Gtk.TextMark, dict]:
buf = Gtk.TextBuffer()
tags = _install_prose_tags(buf)
view = Gtk.TextView.new_with_buffer(buf)
view.set_editable(False)
view.set_cursor_visible(False)
view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
view.set_accepts_tab(False)
view.set_hexpand(True)
view.set_halign(Gtk.Align.FILL)
view.set_vexpand(False)
view.set_valign(Gtk.Align.START)
view.set_top_margin(0)
view.set_bottom_margin(2)
view.set_left_margin(0)
view.set_right_margin(0)
view.set_pixels_above_lines(1)
view.set_pixels_below_lines(1)
view.add_css_class("stream-view")
view.add_css_class("stream-body")
view.add_css_class("chat-body")
end = buf.get_end_iter()
mark = buf.create_mark("end", end, False)
return view, buf, mark, tags
class MessageBody(Gtk.Box):
"""
Stream-safe message body (see module docstring for strategy).
Structure while generating::
[prose TextView]* → [CodeBlock]? → [prose]* → …
"""
def __init__(self, *, role: str = "assistant"):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=4)
ensure_md_css()
self.add_css_class("message-body")
self._role = role
self._raw_text = ""
self._mode = "empty" # empty | structural | markdown | plain
self.set_hexpand(True)
self.set_halign(Gtk.Align.FILL)
self.set_vexpand(False)
self.set_valign(Gtk.Align.START)
# Stream state machine (line-carry + fence mode)
self._in_code = False
self._carry = "" # incomplete line between chunks
self._prose_view: Gtk.TextView | None = None
self._prose_buf: Gtk.TextBuffer | None = None
self._prose_mark: Gtk.TextMark | None = None
self._prose_tags: dict[str, Gtk.TextTag] = {}
self._code_block: CodeBlock | None = None
self._typing = False
# Newelle-style: ignore stale main-loop renders if a newer update exists
self._render_serial = 0
self._user_label = Gtk.Label(label="")
self._user_label.add_css_class("stream-body")
self._user_label.add_css_class("chat-body")
self._user_label.set_wrap(True)
self._user_label.set_wrap_mode(Pango.WrapMode.WORD_CHAR)
self._user_label.set_xalign(0.0)
self._user_label.set_selectable(True)
self._user_label.set_hexpand(True)
if role == "user":
self._user_label.set_max_width_chars(48)
# Optional reasoning panel (sibling of answer content, not merged).
self._reasoning_text = ""
self._reasoning_expander: Gtk.Expander | None = None
self._reasoning_label: Gtk.Label | None = None
def clear_reasoning(self) -> None:
self._reasoning_text = ""
if self._reasoning_expander is not None:
try:
self.remove(self._reasoning_expander)
except Exception: # noqa: BLE001
pass
self._reasoning_expander = None
self._reasoning_label = None
def _ensure_reasoning_ui(self) -> None:
if self._reasoning_expander is not None:
return
exp = Gtk.Expander(label="Thinking…")
exp.add_css_class("reasoning-expander")
exp.set_expanded(True)
lab = Gtk.Label(label="")
lab.set_wrap(True)
lab.set_wrap_mode(Pango.WrapMode.WORD_CHAR)
lab.set_xalign(0.0)
lab.set_selectable(True)
lab.add_css_class("dim-label")
lab.add_css_class("reasoning-body")
exp.set_child(lab)
# Keep reasoning above answer content children.
self.prepend(exp)
self._reasoning_expander = exp
self._reasoning_label = lab
def set_reasoning(self, text: str, *, streaming: bool = False) -> None:
text = text or ""
if not text.strip():
self.clear_reasoning()
return
self._reasoning_text = text
self._ensure_reasoning_ui()
assert self._reasoning_label is not None
assert self._reasoning_expander is not None
self._reasoning_label.set_text(text)
self._reasoning_expander.set_label(
"Thinking…" if streaming else "Reasoning"
)
self._reasoning_expander.set_expanded(bool(streaming))
def append_reasoning(self, chunk: str) -> None:
if not chunk:
return
self._reasoning_text = (self._reasoning_text or "") + chunk
self.set_reasoning(self._reasoning_text, streaming=True)
def _clear_children(self) -> None:
while child := self.get_first_child():
self.remove(child)
self._reasoning_expander = None
self._reasoning_label = None
# Keep _reasoning_text so set_reasoning can reattach after clear.
def set_typing(self) -> None:
self._clear_children()
self._raw_text = ""
self._in_code = False
self._carry = ""
self._code_block = None
self._prose_view = None
self._prose_buf = None
self._prose_mark = None
self._prose_tags = {}
self._typing = True
self._mode = "structural"
view, buf, mark, tags = _new_prose_view()
buf.set_text("···")
view.add_css_class("typing-dots")
self._prose_view = view
self._prose_buf = buf
self._prose_mark = mark
self._prose_tags = tags
self.append(view)
def begin_stream(self) -> None:
self.set_typing()
def _clear_typing_placeholder(self) -> None:
if not self._typing:
return
self._typing = False
if self._prose_view is not None:
self._prose_view.remove_css_class("typing-dots")
if self._prose_buf is not None:
text = self._prose_buf.get_text(
self._prose_buf.get_start_iter(),
self._prose_buf.get_end_iter(),
False,
)
if text == "···":
self._prose_buf.set_text("")
def _ensure_prose(self) -> None:
if self._prose_buf is not None and not self._in_code:
return
view, buf, mark, tags = _new_prose_view()
self._prose_view = view
self._prose_buf = buf
self._prose_mark = mark
self._prose_tags = tags
self.append(view)
def _insert_text(self, text: str, *tags: Gtk.TextTag | None) -> None:
if not text or self._prose_buf is None:
return
start_mark = self._prose_buf.create_mark(
None, self._prose_buf.get_end_iter(), True
)
self._prose_buf.insert(self._prose_buf.get_end_iter(), text)
start = self._prose_buf.get_iter_at_mark(start_mark)
end = self._prose_buf.get_end_iter()
for tag in tags:
if tag is not None:
self._prose_buf.apply_tag(tag, start, end)
self._prose_buf.delete_mark(start_mark)
if self._prose_mark is not None:
self._prose_buf.move_mark(self._prose_mark, end)
def _insert_inline_markdown(
self, text: str, base_tag: Gtk.TextTag | None = None
) -> None:
"""Parse **bold**, *italic*, `code` into TextTags (markers stripped)."""
position = 0
n = len(text)
bold = self._prose_tags.get("bold")
italic = self._prose_tags.get("italic")
code = self._prose_tags.get("inline-code")
while position < n:
if text.startswith("**", position):
closing = text.find("**", position + 2)
if closing != -1:
value = text[position + 2 : closing]
self._insert_text(value, base_tag, bold)
position = closing + 2
continue
if text.startswith("`", position):
closing = text.find("`", position + 1)
if closing != -1:
value = text[position + 1 : closing]
self._insert_text(value, base_tag, code)
position = closing + 1
continue
# Single *italic* (not part of **)
if text.startswith("*", position) and not text.startswith("**", position):
closing = text.find("*", position + 1)
if closing != -1 and not text.startswith("**", closing):
value = text[position + 1 : closing]
self._insert_text(value, base_tag, italic)
position = closing + 1
continue
candidates = [
i
for i in (
text.find("**", position),
text.find("`", position),
text.find("*", position),
)
if i != -1
]
next_marker = min(candidates) if candidates else n
self._insert_text(text[position:next_marker], base_tag)
position = next_marker
def append_markdown_line(self, line: str) -> None:
"""Process one completed prose line into the current TextView with tags."""
self._ensure_prose()
assert self._prose_buf is not None
# Preserve whether caller included newline
has_nl = line.endswith("\n")
stripped = line.rstrip("\n\r")
if not stripped.strip():
self._insert_text("\n")
return
list_tag = self._prose_tags.get("list-item")
heading_tag = self._prose_tags.get("heading")
# Unordered list: - item / * item (not **bold)
if stripped.startswith("- ") or (
stripped.startswith("* ") and not stripped.startswith("**")
):
body = stripped[2:]
self._insert_text("• ", list_tag)
self._insert_inline_markdown(body, list_tag)
self._insert_text("\n")
return
# Ordered list: 1. item
import re
m = re.match(r"^(\d+)\.\s+(.*)$", stripped)
if m:
self._insert_text(f"{m.group(1)}. ", list_tag)
self._insert_inline_markdown(m.group(2), list_tag)
self._insert_text("\n")
return
# ATX headings: ### Title
hm = re.match(r"^(#{1,6})\s+(.*)$", stripped)
if hm:
self._insert_inline_markdown(hm.group(2), heading_tag)
self._insert_text("\n")
return
self._insert_inline_markdown(stripped)
self._insert_text("\n")
def _open_code(self, language: str) -> None:
self._in_code = True
self._prose_view = None
self._prose_buf = None
self._prose_mark = None
self._prose_tags = {}
block = CodeBlock("", language=language)
self._code_block = block
self.append(block)
def _append_code(self, text: str) -> None:
if self._code_block is None:
self._open_code("")
assert self._code_block is not None
self._code_block.append_text(text)
def _close_code(self) -> None:
if self._code_block is not None:
self._code_block.finalize()
self._code_block = None
self._in_code = False
# Next prose goes into a new TextView after the code card
self._prose_view = None
self._prose_buf = None
self._prose_mark = None
self._prose_tags = {}
def _handle_complete_line(self, line: str) -> None:
"""line includes trailing \\n when from split, or full line at finalize."""
stripped = line.strip()
if not self._in_code:
if stripped.startswith("```"):
lang = stripped[3:].strip()
# opening fence — not shown as prose
self._open_code(lang)
else:
self.append_markdown_line(line if line.endswith("\n") else line + "\n")
else:
# Closing fence: line is ``` or ```something
if stripped.startswith("```"):
self._close_code()
else:
# Keep newline inside code for correct formatting
self._append_code(line if line.endswith("\n") else line + "\n")
def append_stream(self, chunk: str) -> None:
"""Route tokens into prose (Markdown tags) or a live CodeBlock."""
if not chunk:
return
if self._mode != "structural":
self.set_typing()