-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathvisualize.py
More file actions
1509 lines (1336 loc) · 58.3 KB
/
visualize.py
File metadata and controls
1509 lines (1336 loc) · 58.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
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import json
import time
from collections import deque
from collections.abc import Awaitable, Callable
from contextlib import asynccontextmanager, suppress
from typing import TYPE_CHECKING, Any, NamedTuple, cast
if TYPE_CHECKING:
from markdown_it import MarkdownIt
import streamingjson # type: ignore[reportMissingTypeStubs]
from kosong.message import Message
from kosong.tooling import ToolError, ToolOk
from prompt_toolkit.application.run_in_terminal import run_in_terminal
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text import ANSI
from prompt_toolkit.key_binding import KeyPressEvent
from rich.console import Group, RenderableType
from rich.live import Live
from rich.panel import Panel
from rich.spinner import Spinner
from rich.style import Style
from rich.text import Text
from kimi_cli.soul import format_context_status, format_token_count
from kimi_cli.tools import extract_key_argument
from kimi_cli.ui.shell.approval_panel import (
ApprovalPromptDelegate as ApprovalPromptDelegate, # noqa: F401 — re-exported
)
from kimi_cli.ui.shell.approval_panel import (
ApprovalRequestPanel,
show_approval_in_pager,
)
from kimi_cli.ui.shell.console import console, render_to_ansi
from kimi_cli.ui.shell.echo import render_user_echo, render_user_echo_text
from kimi_cli.ui.shell.keyboard import KeyboardListener, KeyEvent
from kimi_cli.ui.shell.prompt import (
CustomPromptSession,
UserInput,
)
from kimi_cli.ui.shell.question_panel import (
QuestionPromptDelegate as QuestionPromptDelegate, # noqa: F401 — re-exported
)
from kimi_cli.ui.shell.question_panel import (
QuestionRequestPanel,
prompt_other_input,
show_question_body_in_pager,
)
from kimi_cli.ui.tps_meter import get_show_tps_meter
from kimi_cli.utils.aioqueue import Queue, QueueShutDown
from kimi_cli.utils.logging import logger
from kimi_cli.utils.rich.columns import BulletColumns
from kimi_cli.utils.rich.diff_render import (
collect_diff_hunks,
render_diff_panel,
render_diff_summary_panel,
)
from kimi_cli.utils.rich.markdown import Markdown
from kimi_cli.wire import WireUISide
from kimi_cli.wire.types import (
ApprovalRequest,
ApprovalResponse,
BackgroundTaskDisplayBlock,
BriefDisplayBlock,
CompactionBegin,
CompactionEnd,
ContentPart,
DiffDisplayBlock,
MCPLoadingBegin,
MCPLoadingEnd,
Notification,
PlanDisplay,
QuestionRequest,
StatusUpdate,
SteerInput,
StepBegin,
StepInterrupted,
SubagentEvent,
TextPart,
ThinkPart,
TodoDisplayBlock,
ToolCall,
ToolCallPart,
ToolCallRequest,
ToolResult,
ToolReturnValue,
TurnBegin,
TurnEnd,
WireMessage,
)
MAX_SUBAGENT_TOOL_CALLS_TO_SHOW = 4
MAX_LIVE_NOTIFICATIONS = 4
EXTERNAL_MESSAGE_GRACE_S = 0.1
async def visualize(
wire: WireUISide,
*,
initial_status: StatusUpdate,
cancel_event: asyncio.Event | None = None,
prompt_session: CustomPromptSession | None = None,
steer: Callable[[str | list[ContentPart]], None] | None = None,
bind_running_input: Callable[[Callable[[UserInput], None], Callable[[], None]], None]
| None = None,
unbind_running_input: Callable[[], None] | None = None,
on_view_ready: Callable[[Any], None] | None = None,
on_view_closed: Callable[[], None] | None = None,
):
"""
A loop to consume agent events and visualize the agent behavior.
Args:
wire: Communication channel with the agent
initial_status: Initial status snapshot
cancel_event: Event that can be set (e.g., by ESC key) to cancel the run
"""
if prompt_session is not None and steer is not None:
view = _PromptLiveView(
initial_status,
prompt_session=prompt_session,
steer=steer,
cancel_event=cancel_event,
)
prompt_session.attach_running_prompt(view)
def _cancel_running_input() -> None:
if cancel_event is not None:
cancel_event.set()
if bind_running_input is not None:
bind_running_input(view.handle_local_input, _cancel_running_input)
else:
view = _LiveView(initial_status, cancel_event)
if on_view_ready is not None:
on_view_ready(view)
try:
await view.visualize_loop(wire)
finally:
if prompt_session is not None and steer is not None:
if unbind_running_input is not None:
unbind_running_input()
assert isinstance(view, _PromptLiveView)
prompt_session.detach_running_prompt(view)
if on_view_closed is not None:
on_view_closed()
_THINKING_PREVIEW_LINES = 6
_PENDING_PREVIEW_LINES = 8
_SELF_CLOSING_BLOCKS = frozenset(("fence", "code_block", "hr", "html_block"))
_ELLIPSIS = "..."
def _truncate_to_display_width(line: str, max_width: int) -> str:
"""Truncate *line* so its terminal display width fits within *max_width*.
Uses ``rich.cells.cell_len`` for CJK-aware column width measurement.
"""
from rich.cells import cell_len
if cell_len(line) <= max_width:
return line
ellipsis_width = cell_len(_ELLIPSIS)
budget = max_width - ellipsis_width
width = 0
for i, ch in enumerate(line):
width += cell_len(ch)
if width > budget:
return line[:i] + _ELLIPSIS
return line
# Lazy-initialized markdown-it parser for incremental token commitment.
_md_parser: MarkdownIt | None = None
def _get_md_parser() -> MarkdownIt:
global _md_parser
if _md_parser is None:
from markdown_it import MarkdownIt
# Match the extensions used by the rendering path (utils/rich/markdown.py)
# so that block boundaries are detected consistently.
_md_parser = MarkdownIt().enable("strikethrough").enable("table")
return _md_parser
def _estimate_tokens(text: str) -> float:
"""Estimate token count for mixed CJK/Latin text.
Returns a **float** so that callers can accumulate across small chunks
without per-chunk floor truncation (e.g. a 3-char ASCII chunk would
yield 0 if truncated to int immediately, but 0.75 as float).
Heuristics based on common BPE tokenizers (cl100k, o200k):
- CJK ideographs: ~1.5 tokens per character (often split into 2-byte pieces)
- Latin / ASCII: ~1 token per 4 characters (words average ~4 chars)
"""
cjk = 0
other = 0
for ch in text:
cp = ord(ch)
if (
0x4E00 <= cp <= 0x9FFF # CJK Unified Ideographs
or 0x3400 <= cp <= 0x4DBF # CJK Extension A
or 0xF900 <= cp <= 0xFAFF # CJK Compatibility Ideographs
or 0x3000 <= cp <= 0x303F # CJK Symbols and Punctuation
or 0xFF00 <= cp <= 0xFFEF # Fullwidth Forms
):
cjk += 1
else:
other += 1
return cjk * 1.5 + other / 4
def _find_committed_boundary(text: str) -> int | None:
"""Return the character offset up to which *text* can be safely committed.
Uses the incremental token commitment algorithm: parse text into block-level
tokens via ``markdown-it-py``, confirm all blocks except the last one (which
may be incomplete due to streaming truncation).
Returns ``None`` when there are fewer than 2 blocks (nothing to confirm yet).
"""
md = _get_md_parser()
tokens = md.parse(text)
# Collect only TOP-LEVEL block boundaries by tracking nesting depth.
# Nested tokens (e.g. list_item_open inside bullet_list_open) must not be
# treated as independent blocks — otherwise lists and blockquotes get split.
block_maps: list[list[int]] = []
depth = 0
for t in tokens:
if t.nesting == 1:
if depth == 0 and t.map is not None:
block_maps.append(t.map)
depth += 1
elif t.nesting == -1:
depth -= 1
elif depth == 0 and t.type in _SELF_CLOSING_BLOCKS and t.map is not None:
block_maps.append(t.map)
if len(block_maps) < 2:
return None
# Convert end-line number to character offset by scanning newlines.
target_line = block_maps[-2][1]
offset = 0
for _ in range(target_line):
offset = text.index("\n", offset) + 1
return offset
def _tail_lines(text: str, n: int) -> str:
"""Extract the last *n* lines from *text* via reverse scanning (O(n))."""
pos = len(text)
for _ in range(n):
pos = text.rfind("\n", 0, pos)
if pos == -1:
return text
return text[pos + 1 :]
class _ContentBlock:
"""Streaming content block with incremental markdown commitment.
For **composing** (``is_think=False``), confirmed markdown blocks are flushed
to the terminal permanently via ``console.print()`` as they become complete,
giving users real-time streaming output. Only the unconfirmed tail remains
in the transient Rich Live area.
For **thinking** (``is_think=True``), content stays in the Live area as a
scrolling preview until the block is finalized.
"""
def __init__(self, is_think: bool):
self.is_think = is_think
self._spinner = Spinner("dots", "")
self.raw_text = ""
# Accumulated float estimate — avoids per-chunk int truncation.
self._token_count: float = 0.0
self._start_time = time.monotonic()
# Incremental commitment state (composing only).
self._committed_len = 0
self._has_printed_bullet = False
# -- Public API ----------------------------------------------------------
def append(self, content: str) -> None:
self.raw_text += content
self._token_count += _estimate_tokens(content)
# Block boundaries require newlines; skip parse for mid-line chunks.
if not self.is_think and "\n" in content:
self._flush_committed()
def compose(self) -> RenderableType:
"""Render the transient Live area content."""
pending = self._pending_text()
# Thinking: always show spinner + preview.
if self.is_think:
spinner = self._compose_spinner()
if not pending:
return spinner
preview = self._build_preview(pending)
return Group(spinner, Text(preview, style="grey50 italic"))
# Composing: always show spinner with elapsed time and token count.
# Committed blocks are already printed permanently above.
return self._compose_spinner()
def compose_final(self) -> RenderableType:
"""Render the remaining uncommitted content when the block ends."""
remaining = self._pending_text()
if not remaining:
return Text("")
if self.is_think:
return BulletColumns(
Markdown(remaining, style="grey50 italic"),
bullet_style="grey50",
)
return self._wrap_bullet(Markdown(remaining))
def has_pending(self) -> bool:
"""Whether there is uncommitted content to flush."""
return bool(self._pending_text())
# -- Private -------------------------------------------------------------
def _pending_text(self) -> str:
return self.raw_text[self._committed_len :]
def _wrap_bullet(self, renderable: RenderableType) -> BulletColumns:
"""First call gets the ``•`` bullet; subsequent calls get a space."""
if self._has_printed_bullet:
return BulletColumns(renderable, bullet=Text(" "))
self._has_printed_bullet = True
return BulletColumns(renderable)
def _flush_committed(self) -> None:
"""Commit confirmed markdown blocks to permanent terminal output."""
pending = self._pending_text()
if not pending:
return
boundary = _find_committed_boundary(pending)
if boundary is None:
return
committed_text = pending[:boundary]
console.print(self._wrap_bullet(Markdown(committed_text)))
self._committed_len += boundary
def _compose_spinner(self) -> Spinner:
elapsed = time.monotonic() - self._start_time
label = "Thinking..." if self.is_think else "Composing..."
elapsed_str = f"{int(elapsed)}s" if elapsed >= 1 else "<1s"
count_str = f"{format_token_count(int(self._token_count))} tokens"
self._spinner.text = Text.assemble(
(label, ""),
(f" {elapsed_str}", "grey50"),
(f" · {count_str}", "grey50"),
)
return self._spinner
def _build_preview(self, text: str) -> str:
max_lines = _THINKING_PREVIEW_LINES if self.is_think else _PENDING_PREVIEW_LINES
max_width = console.width - 2 if console.width else 78
tail_text = _tail_lines(text, max_lines)
lines = tail_text.split("\n")
return "\n".join(_truncate_to_display_width(line, max_width) for line in lines)
class _ToolCallBlock:
class FinishedSubCall(NamedTuple):
call: ToolCall
result: ToolReturnValue
def __init__(self, tool_call: ToolCall):
self._tool_name = tool_call.function.name
self._lexer = streamingjson.Lexer()
if tool_call.function.arguments is not None:
self._lexer.append_string(tool_call.function.arguments)
self._argument = extract_key_argument(self._lexer, self._tool_name)
self._full_url = self._extract_full_url(tool_call.function.arguments, self._tool_name)
self._result: ToolReturnValue | None = None
self._subagent_id: str | None = None
self._subagent_type: str | None = None
self._ongoing_subagent_tool_calls: dict[str, ToolCall] = {}
self._last_subagent_tool_call: ToolCall | None = None
self._n_finished_subagent_tool_calls = 0
self._finished_subagent_tool_calls = deque[_ToolCallBlock.FinishedSubCall](
maxlen=MAX_SUBAGENT_TOOL_CALLS_TO_SHOW
)
self._spinning_dots = Spinner("dots", text="")
self._renderable: RenderableType = self._compose()
def compose(self) -> RenderableType:
return self._renderable
@property
def finished(self) -> bool:
return self._result is not None
def append_args_part(self, args_part: str):
if self.finished:
return
self._lexer.append_string(args_part)
# TODO: maybe don't extract detail if it's already stable
argument = extract_key_argument(self._lexer, self._tool_name)
if argument and argument != self._argument:
self._argument = argument
self._full_url = self._extract_full_url(self._lexer.complete_json(), self._tool_name)
self._renderable = BulletColumns(
self._build_headline_text(),
bullet=self._spinning_dots,
)
def finish(self, result: ToolReturnValue):
self._result = result
self._renderable = self._compose()
def append_sub_tool_call(self, tool_call: ToolCall):
self._ongoing_subagent_tool_calls[tool_call.id] = tool_call
self._last_subagent_tool_call = tool_call
def append_sub_tool_call_part(self, tool_call_part: ToolCallPart):
if self._last_subagent_tool_call is None:
return
if not tool_call_part.arguments_part:
return
if self._last_subagent_tool_call.function.arguments is None:
self._last_subagent_tool_call.function.arguments = tool_call_part.arguments_part
else:
self._last_subagent_tool_call.function.arguments += tool_call_part.arguments_part
def finish_sub_tool_call(self, tool_result: ToolResult):
self._last_subagent_tool_call = None
sub_tool_call = self._ongoing_subagent_tool_calls.pop(tool_result.tool_call_id, None)
if sub_tool_call is None:
return
self._finished_subagent_tool_calls.append(
_ToolCallBlock.FinishedSubCall(
call=sub_tool_call,
result=tool_result.return_value,
)
)
self._n_finished_subagent_tool_calls += 1
self._renderable = self._compose()
def set_subagent_metadata(self, agent_id: str, subagent_type: str) -> None:
changed = (self._subagent_id, self._subagent_type) != (agent_id, subagent_type)
self._subagent_id = agent_id
self._subagent_type = subagent_type
if changed:
self._renderable = self._compose()
def _compose(self) -> RenderableType:
lines: list[RenderableType] = [
self._build_headline_text(),
]
if self._subagent_id is not None and self._subagent_type is not None:
lines.append(
BulletColumns(
Text(
f"subagent {self._subagent_type} ({self._subagent_id})",
style="grey50",
),
bullet_style="grey50",
)
)
if self._n_finished_subagent_tool_calls > MAX_SUBAGENT_TOOL_CALLS_TO_SHOW:
n_hidden = self._n_finished_subagent_tool_calls - MAX_SUBAGENT_TOOL_CALLS_TO_SHOW
lines.append(
BulletColumns(
Text(
f"{n_hidden} more tool call{'s' if n_hidden > 1 else ''} ...",
style="grey50 italic",
),
bullet_style="grey50",
)
)
for sub_call, sub_result in self._finished_subagent_tool_calls:
argument = extract_key_argument(
sub_call.function.arguments or "", sub_call.function.name
)
sub_url = self._extract_full_url(sub_call.function.arguments, sub_call.function.name)
sub_text = Text()
sub_text.append("Used ")
sub_text.append(sub_call.function.name, style="blue")
if argument:
sub_text.append(" (", style="grey50")
arg_style = Style(color="grey50", link=sub_url) if sub_url else "grey50"
sub_text.append(argument, style=arg_style)
sub_text.append(")", style="grey50")
lines.append(
BulletColumns(
sub_text,
bullet_style="green" if not sub_result.is_error else "dark_red",
)
)
if self._result is not None:
display = self._result.display
idx = 0
while idx < len(display):
block = display[idx]
if isinstance(block, DiffDisplayBlock):
# Collect consecutive same-file diff blocks
path = block.path
diff_blocks: list[DiffDisplayBlock] = []
while idx < len(display):
b = display[idx]
if not isinstance(b, DiffDisplayBlock) or b.path != path:
break
diff_blocks.append(b)
idx += 1
if any(b.is_summary for b in diff_blocks):
lines.append(render_diff_summary_panel(path, diff_blocks))
else:
hunks, added_total, removed_total = collect_diff_hunks(diff_blocks)
if hunks:
lines.append(render_diff_panel(path, hunks, added_total, removed_total))
elif isinstance(block, BriefDisplayBlock):
style = "grey50" if not self._result.is_error else "dark_red"
if block.text:
lines.append(Markdown(block.text, style=style))
idx += 1
elif isinstance(block, TodoDisplayBlock):
markdown = self._render_todo_markdown(block)
if markdown:
lines.append(Markdown(markdown, style="grey50"))
idx += 1
elif isinstance(block, BackgroundTaskDisplayBlock):
lines.append(
Markdown(
(f"`{block.task_id}` [{block.status}] {block.description}"),
style="grey50",
)
)
idx += 1
else:
idx += 1
if self.finished:
assert self._result is not None
return BulletColumns(
Group(*lines),
bullet_style="green" if not self._result.is_error else "dark_red",
)
else:
return BulletColumns(
Group(*lines),
bullet=self._spinning_dots,
)
@staticmethod
def _extract_full_url(arguments: str | None, tool_name: str) -> str | None:
"""Extract the full URL from FetchURL tool arguments."""
if tool_name != "FetchURL" or not arguments:
return None
try:
args = json.loads(arguments, strict=False)
except (json.JSONDecodeError, TypeError):
return None
if isinstance(args, dict):
url = cast(dict[str, Any], args).get("url")
if url:
return str(url)
return None
def _build_headline_text(self) -> Text:
text = Text()
text.append("Used " if self.finished else "Using ")
text.append(self._tool_name, style="blue")
if self._argument:
text.append(" (", style="grey50")
arg_style = Style(color="grey50", link=self._full_url) if self._full_url else "grey50"
text.append(self._argument, style=arg_style)
text.append(")", style="grey50")
return text
def _render_todo_markdown(self, block: TodoDisplayBlock) -> str:
lines: list[str] = []
for todo in block.items:
normalized = todo.status.replace("_", " ").lower()
match normalized:
case "pending":
lines.append(f"- {todo.title}")
case "in progress":
lines.append(f"- {todo.title} ←")
case "done":
lines.append(f"- ~~{todo.title}~~")
case _:
lines.append(f"- {todo.title}")
return "\n".join(lines)
class _NotificationBlock:
_SEVERITY_STYLE = {
"info": "cyan",
"success": "green",
"warning": "yellow",
"error": "red",
}
def __init__(self, notification: Notification):
self.notification = notification
def compose(self) -> RenderableType:
style = self._SEVERITY_STYLE.get(self.notification.severity, "cyan")
lines: list[RenderableType] = [Text(self.notification.title, style=f"bold {style}")]
body = self.notification.body.strip()
if body:
body_lines = body.splitlines()
preview = "\n".join(body_lines[:2])
if len(body_lines) > 2:
preview += "\n..."
lines.append(Text(preview, style="grey50"))
return BulletColumns(Group(*lines), bullet_style=style)
class _StatusBlock:
def __init__(self, initial: StatusUpdate) -> None:
self.text = Text("", justify="right")
self._context_usage: float = 0.0
self._context_tokens: int = 0
self._max_context_tokens: int = 0
self._tps: float = 0.0
self.update(initial)
def render(self) -> RenderableType:
return self.text
def update(self, status: StatusUpdate) -> None:
if status.context_usage is not None:
self._context_usage = status.context_usage
if status.context_tokens is not None:
self._context_tokens = status.context_tokens
if status.max_context_tokens is not None:
self._max_context_tokens = status.max_context_tokens
if status.tps is not None:
self._tps = status.tps
# Only refresh if context_usage or tps is provided (fields that affect display)
if status.context_usage is not None or status.tps is not None:
self._refresh_text()
def _refresh_text(self) -> None:
context_str = format_context_status(
self._context_usage,
self._context_tokens,
self._max_context_tokens,
)
if get_show_tps_meter() and self._tps > 0:
self.text.plain = f"{context_str} · {self._tps:.1f} tok/s"
else:
self.text.plain = context_str
@asynccontextmanager
async def _keyboard_listener(
handler: Callable[[KeyboardListener, KeyEvent], Awaitable[None]],
):
listener = KeyboardListener()
await listener.start()
async def _keyboard():
while True:
event = await listener.get()
await handler(listener, event)
task = asyncio.create_task(_keyboard())
try:
yield
finally:
task.cancel()
with suppress(asyncio.CancelledError):
await task
await listener.stop()
class _LiveView:
def __init__(self, initial_status: StatusUpdate, cancel_event: asyncio.Event | None = None):
self._cancel_event = cancel_event
self._mooning_spinner: Spinner | None = None
self._compacting_spinner: Spinner | None = None
self._mcp_loading_spinner: Spinner | None = None
self._current_content_block: _ContentBlock | None = None
self._tool_call_blocks: dict[str, _ToolCallBlock] = {}
self._last_tool_call_block: _ToolCallBlock | None = None
self._approval_request_queue = deque[ApprovalRequest]()
"""
It is possible that multiple subagents request approvals at the same time,
in which case we will have to queue them up and show them one by one.
"""
self._current_approval_request_panel: ApprovalRequestPanel | None = None
self._question_request_queue = deque[QuestionRequest]()
self._current_question_panel: QuestionRequestPanel | None = None
self._notification_blocks = deque[_NotificationBlock]()
self._live_notification_blocks = deque[_NotificationBlock](maxlen=MAX_LIVE_NOTIFICATIONS)
self._status_block = _StatusBlock(initial_status)
self._need_recompose = False
self._external_messages: Queue[WireMessage] = Queue()
def _reset_live_shape(self, live: Live) -> None:
# Rich doesn't expose a public API to clear Live's cached render height.
# After leaving the pager, stale height causes cursor restores to jump,
# so we reset the private _shape to re-anchor the next refresh.
live._live_render._shape = None # type: ignore[reportPrivateUsage]
async def _drain_external_message_after_wire_shutdown(
self,
external_task: asyncio.Task[WireMessage],
) -> tuple[WireMessage | None, asyncio.Task[WireMessage]]:
try:
msg = await asyncio.wait_for(
asyncio.shield(external_task),
timeout=EXTERNAL_MESSAGE_GRACE_S,
)
except (TimeoutError, QueueShutDown):
return None, external_task
return msg, asyncio.create_task(self._external_messages.get())
async def visualize_loop(self, wire: WireUISide):
with Live(
self.compose(),
console=console,
refresh_per_second=10,
transient=True,
vertical_overflow="visible",
) as live:
async def keyboard_handler(listener: KeyboardListener, event: KeyEvent) -> None:
# Handle Ctrl+E specially - pause Live while the pager is active
if event == KeyEvent.CTRL_E:
if self.has_expandable_panel():
await listener.pause()
live.stop()
try:
self._show_expandable_panel_content()
finally:
# Reset live render shape so the next refresh re-anchors cleanly.
self._reset_live_shape(live)
live.start()
live.update(self.compose(), refresh=True)
await listener.resume()
return
# Handle ENTER/SPACE on question panel when "Other" is selected
if self._should_prompt_question_other_for_key(event):
panel = self._current_question_panel
assert panel is not None
question_text = panel.current_question_text
await listener.pause()
live.stop()
try:
text = await prompt_other_input(question_text)
finally:
self._reset_live_shape(live)
live.start()
await listener.resume()
self._submit_question_other_text(text)
live.update(self.compose(), refresh=True)
return
self.dispatch_keyboard_event(event)
if self._need_recompose:
live.update(self.compose(), refresh=True)
self._need_recompose = False
async with _keyboard_listener(keyboard_handler):
wire_task = asyncio.create_task(wire.receive())
external_task = asyncio.create_task(self._external_messages.get())
while True:
try:
done, _ = await asyncio.wait(
[wire_task, external_task],
return_when=asyncio.FIRST_COMPLETED,
)
if wire_task in done:
msg = wire_task.result()
wire_task = asyncio.create_task(wire.receive())
else:
msg = external_task.result()
external_task = asyncio.create_task(self._external_messages.get())
except QueueShutDown:
msg, external_task = await self._drain_external_message_after_wire_shutdown(
external_task
)
if msg is not None:
self.dispatch_wire_message(msg)
if self._need_recompose:
live.update(self.compose(), refresh=True)
self._need_recompose = False
continue
self.cleanup(is_interrupt=False)
live.update(self.compose(), refresh=True)
break
if isinstance(msg, StepInterrupted):
self.cleanup(is_interrupt=True)
live.update(self.compose(), refresh=True)
break
self.dispatch_wire_message(msg)
if self._need_recompose:
live.update(self.compose(), refresh=True)
self._need_recompose = False
wire_task.cancel()
external_task.cancel()
self._external_messages.shutdown(immediate=True)
with suppress(asyncio.CancelledError, QueueShutDown):
await wire_task
with suppress(asyncio.CancelledError, QueueShutDown):
await external_task
def refresh_soon(self) -> None:
self._need_recompose = True
def _on_question_panel_state_changed(self) -> None:
"""Hook for subclasses to react when question panel visibility changes."""
return None
def enqueue_external_message(self, msg: WireMessage) -> None:
try:
self._external_messages.put_nowait(msg)
except QueueShutDown:
logger.debug("Ignoring external wire message after live view shutdown: {msg}", msg=msg)
def has_expandable_panel(self) -> bool:
return (
self._expandable_approval_panel() is not None
or self._expandable_question_panel() is not None
)
def _expandable_approval_panel(self) -> ApprovalRequestPanel | None:
panel = self._current_approval_request_panel
if panel is not None and panel.has_expandable_content:
return panel
return None
def _expandable_question_panel(self) -> QuestionRequestPanel | None:
panel = self._current_question_panel
if panel is not None and panel.has_expandable_content:
return panel
return None
def _show_expandable_panel_content(self) -> bool:
if approval_panel := self._expandable_approval_panel():
show_approval_in_pager(approval_panel)
return True
if question_panel := self._expandable_question_panel():
show_question_body_in_pager(question_panel)
return True
return False
def _should_prompt_question_other_for_key(self, key: KeyEvent) -> bool:
panel = self._current_question_panel
if panel is None or not panel.should_prompt_other_input():
return False
return key == KeyEvent.ENTER or (key == KeyEvent.SPACE and not panel.is_multi_select)
def _submit_question_other_text(self, text: str) -> None:
panel = self._current_question_panel
if panel is None:
return
all_done = panel.submit_other(text)
if all_done:
panel.request.resolve(panel.get_answers())
self.show_next_question_request()
self.refresh_soon()
def compose(self, *, include_status: bool = True) -> RenderableType:
"""Compose the live view display content.
Approval and question panels are rendered first so they remain visible
at the top of the terminal even when tool-call output is long enough
to push content beyond the visible area.
"""
blocks: list[RenderableType] = []
# Approval/question panels first — highest visual priority.
if self._current_approval_request_panel:
blocks.append(self._current_approval_request_panel.render())
if self._current_question_panel:
blocks.append(self._current_question_panel.render())
# Spinners or content + tool calls.
if self._mcp_loading_spinner is not None:
blocks.append(self._mcp_loading_spinner)
elif self._mooning_spinner is not None:
blocks.append(self._mooning_spinner)
elif self._compacting_spinner is not None:
blocks.append(self._compacting_spinner)
else:
if self._current_content_block is not None:
blocks.append(self._current_content_block.compose())
for tool_call in self._tool_call_blocks.values():
blocks.append(tool_call.compose())
for notification in self._live_notification_blocks:
blocks.append(notification.compose())
if include_status:
blocks.append(self._status_block.render())
return Group(*blocks)
def dispatch_wire_message(self, msg: WireMessage) -> None:
"""Dispatch the Wire message to UI components."""
assert not isinstance(msg, StepInterrupted) # handled in visualize_loop
if isinstance(msg, StepBegin):
self.cleanup(is_interrupt=False)
self._mcp_loading_spinner = None
self._mooning_spinner = Spinner("moon", "")
self.refresh_soon()
return
if self._mooning_spinner is not None:
# any message other than StepBegin should end the mooning state
self._mooning_spinner = None
self.refresh_soon()
match msg:
case TurnBegin():
self.flush_content()
case SteerInput(user_input=user_input):
self.cleanup(is_interrupt=False)
content: list[ContentPart]
if isinstance(user_input, list):
content = list(user_input)
else:
content = [TextPart(text=user_input)]
console.print(render_user_echo(Message(role="user", content=content)))
case TurnEnd():
pass
case CompactionBegin():
self._compacting_spinner = Spinner("balloon", "Compacting...")
self.refresh_soon()
case CompactionEnd():
self._compacting_spinner = None
self.refresh_soon()
case MCPLoadingBegin():
self._mcp_loading_spinner = Spinner("dots", "Connecting to MCP servers...")
self.refresh_soon()
case MCPLoadingEnd():
self._mcp_loading_spinner = None
self.refresh_soon()
case StatusUpdate():
self._status_block.update(msg)
case Notification():
self.append_notification(msg)
case ContentPart():
self.append_content(msg)
case ToolCall():
self.append_tool_call(msg)
case ToolCallPart():
self.append_tool_call_part(msg)
case ToolResult():
self.append_tool_result(msg)
case ApprovalResponse():
self._reconcile_approval_requests()
case SubagentEvent():
self.handle_subagent_event(msg)
case PlanDisplay():
self.display_plan(msg)
case ApprovalRequest():
self.request_approval(msg)
case QuestionRequest():
self.request_question(msg)
case ToolCallRequest():
logger.warning("Unexpected ToolCallRequest in shell UI: {msg}", msg=msg)
case _:
pass
def _try_submit_question(self) -> None:
"""Submit the current question answer; if all done, resolve and advance."""
panel = self._current_question_panel
if panel is None:
return
all_done = panel.submit()
if all_done:
panel.request.resolve(panel.get_answers())
self.show_next_question_request()
def dispatch_keyboard_event(self, event: KeyEvent) -> None:
# Handle question panel keyboard events
if self._current_question_panel is not None:
match event: