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
|
"""Terminal presentation.
Everything that decides how Agentux *looks* lives here rather than in the CLI,
so the same rendering can be reused by a test or another front end.
Three ideas carry the interface:
* **Gradients, sparingly.** A gradient on the wordmark and on nothing else.
Colour used as decoration everywhere is noise, and it disappears entirely
when the output is not a terminal. The wordmark's gradient runs down the
rows and its extruded side is a second, darker colour -- the two together
are what make the letters read as solid rather than as flat blocks.
* **One live region.** Tool activity scrolls permanently; the answer streams
into a single live region that re-renders markdown as it arrives. Two
competing live regions fight each other and flicker.
* **Numbers are labelled.** Tokens come from the provider when it reports them
and from an estimate when it does not, and the display says which.
"""
from __future__ import annotations
import time
from typing import Any, List, Optional, Tuple, Union
from rich.console import Console, Group, RenderableType
from rich.live import Live
from rich.markdown import Markdown
from rich.panel import Panel
from rich.text import Text
from .theme import (
ACCENT_DIM,
BORDER,
BORDER_SUBTLE,
CYAN,
EMERALD,
PINK,
PURPLE,
TEXT_LIGHT,
TEXT_MUTED,
WORDMARK_DEPTH,
WORDMARK_END,
WORDMARK_SHADOW,
WORDMARK_START,
YELLOW,
)
from .usage import (
DEFAULT_CONTEXT_WINDOW,
Usage,
context_bar,
conversation_tokens,
format_duration,
format_tokens,
)
#: The accent ramp. Emerald -> cyan, from the brand palette.
ACCENT_START = WORDMARK_START
ACCENT_END = WORDMARK_END
#: The colour of the extruded side of each letter. Dim enough to read as depth
#: rather than as a second letter, bright enough to be visible at all.
ACCENT_DEPTH = WORDMARK_DEPTH
#: Letterforms, sharp-cornered, each exactly ``LETTER_WIDTH`` columns by
#: ``LETTER_HEIGHT`` rows. Declared as a grid rather than written out as one
#: banner because a hand-typed banner silently goes ragged -- and ragged rows
#: shear, which is what once made this read as "AOSNTUK" instead of AGENTUX.
#:
#: ``G`` carries a crossbar on its fourth row. Without it ``G`` is a closed
#: ring and reads as ``O``. ``U`` closes across both legs on its last row, so
#: the bowl is actually connected.
LETTER_HEIGHT = 6
LETTER_WIDTH = 8
_LETTERS: Dict[str, List[str]] = {
"A": [
" ████ ",
" ██ ██ ",
"████████",
"██ ██",
"██ ██",
"██ ██",
],
"G": [
" ██████ ",
"██ ██",
"██ ",
"██ ████ ",
"██ ██",
" ██████ ",
],
"E": [
"████████",
"██ ",
"███████ ",
"██ ",
"██ ",
"████████",
],
"N": [
"██ ██",
"███ ██",
"████ ██",
"██ ██ ██",
"██ ████",
"██ ███",
],
"T": [
"████████",
" ████ ",
" ████ ",
" ████ ",
" ████ ",
" ████ ",
],
"U": [
"██ ██",
"██ ██",
"██ ██",
"██ ██",
"██ ██",
" ██████ ",
],
"X": [
"██ ██",
"██ ██",
" ██ ██ ",
" ████ ",
" ██ ██ ",
"██ ██",
],
}
#: How far the extruded side is offset from the face, in (columns, rows).
#: One column and one row. Anything wider reaches across the gap between
#: neighbouring strokes and fills it in, which fuses the letters together --
#: the depth then reads as fill rather than as an edge.
DEPTH_X = 1
DEPTH_Y = 1
def _wordmark_layers() -> Tuple[List[str], List[str]]:
"""The wordmark as two aligned layers: the face and the extruded side.
Returned separately rather than composited, because the two want different
colours. Compositing first would mean re-deriving which cells are shadow,
and the depth colour is most of what makes the letters read as solid.
"""
face = [
[" "] * (len(_LETTERS) * LETTER_WIDTH + (len(_LETTERS) - 1))
for _ in range(LETTER_HEIGHT)
]
for index, char in enumerate(_WORDMARK_TEXT):
glyph = _LETTERS[char]
offset = index * (LETTER_WIDTH + 1)
for row in range(LETTER_HEIGHT):
for col, pixel in enumerate(glyph[row]):
face[row][offset + col] = pixel
height, width = LETTER_HEIGHT, len(face[0])
rows, cols = height + DEPTH_Y, width + DEPTH_X
# The silhouette shifted down-right by (DEPTH_Y, DEPTH_X). Anything drawn
# from this is "what the letters look like one step further back".
shifted = [[" "] * cols for _ in range(rows)]
for row in range(height):
for col in range(width):
shifted[row + DEPTH_Y][col + DEPTH_X] = face[row][col]
def ink(grid: List[List[str]], row: int, col: int) -> bool:
return 0 <= row < len(grid) and 0 <= col < len(grid[row]) and grid[row][col] != " "
# Only the *rim* of the shifted silhouette is drawn. Two things are
# subtracted, and both are load-bearing:
#
# 1. The face itself. Painting depth over the face smears the letters.
# 2. The interior of the shifted silhouette -- the cells a letter's
# counter encloses, such as the triangle inside "A". Those are inside
# the solid, not on its edge, so filling them turns the counter into
# a filled wedge and the letter stops reading.
#
# What survives is the one-cell edge along the lower-right of each stroke,
# which is the only part of an extruded face you would actually see.
depth = [[" "] * cols for _ in range(rows)]
for row in range(rows):
for col in range(cols):
if not ink(shifted, row, col) or ink(face, row, col):
continue
enclosed = all(
ink(shifted, row + dy, col + dx)
for dy, dx in ((-1, 0), (1, 0), (0, -1), (0, 1))
)
if not enclosed:
depth[row][col] = shifted[row][col]
return (
["".join(row) for row in face],
["".join(row) for row in depth],
)
#: The word, spelled once. `_LETTERS` is the only place the shapes live.
_WORDMARK_TEXT = "AGENTUX"
def interpolate(start: Tuple[int, int, int], end: Tuple[int, int, int], t: float) -> str:
"""A colour between two RGB points, as a rich hex string."""
t = max(0.0, min(1.0, t))
return "#{:02x}{:02x}{:02x}".format(
round(start[0] + (end[0] - start[0]) * t),
round(start[1] + (end[1] - start[1]) * t),
round(start[2] + (end[2] - start[2]) * t),
)
def gradient_text(text: str, start=ACCENT_START, end=ACCENT_END) -> Text:
"""Paint a string with a horizontal colour ramp.
The ramp runs across the whole string, not per line, so a multi-line
wordmark reads as one gradient rather than several restarting ones.
"""
total = len(text)
if total == 0:
return Text("")
lines = text.split("\n")
styled = Text()
seen = 0
for line_index, line in enumerate(lines):
if line_index:
styled.append("\n")
for char in line:
styled.append(char, style=interpolate(start, end, seen / max(1, total - 1)))
seen += 1
seen += 1 # the newline counts toward the ramp
return styled
def gradient_text_vertical(
text: str, start=ACCENT_START, end=ACCENT_END
) -> Text:
"""Paint a string with a vertical colour ramp, one colour per line.
The wordmark needs this rather than a horizontal ramp. Its glyphs are solid
block characters, so ramping across each line colours adjacent columns of
the same letter differently -- the letter reads as vertical stripes instead
of one shape. Ramping down the lines instead gives each row a single colour,
which keeps every letter solid and still reads as one gradient overall.
"""
lines = text.split("\n")
if not lines:
return Text("")
styled = Text()
last = max(1, len(lines) - 1)
for index, line in enumerate(lines):
if index:
styled.append("\n")
if not line:
continue
# Half a step in: the first row fully at `start` and the last fully at
# `end` both look washed out next to their neighbours.
styled.append(line, style=interpolate(start, end, (index + 0.5) / (last + 1)))
return styled
def wordmark() -> Text:
"""The Agentux wordmark: a gradient face with an extruded dark side.
The two layers are composited onto one grid before any colour is applied,
because a cell can hold only one glyph. Drawing the depth row and then the
face row as separate strings would interleave them wrongly -- the rows are
different lengths, so the face would land in the wrong columns and smear
sideways across the banner.
Each row is padded to the full grid width and never stripped: the trailing
blanks are what keep the letters from shearing against each other.
"""
face, depth = _wordmark_layers()
width = max(len(row) for row in depth)
# Composite: start from the depth, then stamp the face over it.
grid = [list(row.ljust(width)) for row in depth]
for index, row in enumerate(face):
target = grid[index]
for col, char in enumerate(row):
if char != " ":
target[col] = char
# Which cells are face and which are depth, after compositing. The depth
# layer is taller than the face (that is the extrusion), so the extra rows
# have no face to mark and are depth throughout.
face_mask = [
[
index < len(face) and col < len(face[index]) and face[index][col] != " "
for col in range(width)
]
for index in range(len(grid))
]
last = max(1, len(grid) - 1)
styled = Text()
for index in range(len(grid)):
if index:
styled.append("\n")
shade = interpolate(ACCENT_START, ACCENT_END, (index + 0.5) / (last + 1))
side = interpolate(ACCENT_DEPTH, WORDMARK_SHADOW, index / last)
for col in range(width):
char = grid[index][col]
if char == " ":
styled.append(" ")
elif face_mask[index][col]:
styled.append(char, style=shade)
else:
styled.append(char, style=side)
return styled
#: The face layer only, for tests and for anything that wants plain text.
WORDMARK = "\n".join(_wordmark_layers()[0])
def context_bar_text(fraction: float, width: int = 16) -> Text:
"""The context bar as a styled ``Text``.
``usage.context_bar`` returns markup, which is only safe to print with
markup parsing on. Callers here build a ``Text`` with ``append``, and
``Text.append`` treats its argument literally -- so the tags would appear
on screen as ``[green]...[/green]``. Converting here keeps the style.
"""
return Text.from_markup(context_bar(fraction, width))
# ----------------------------------------------------------------------
# Rendering into a buffer instead of onto the terminal
# ----------------------------------------------------------------------
# The full-screen TUI owns the terminal, so rich must not print to stdout.
# These render to an in-memory console and return the ANSI text, which the TUI
# hands to prompt_toolkit as styled text. Rich stays the only renderer; it just
# writes somewhere that is not the screen.
def render_markdown_ansi(text: str, width: int = 80) -> str:
"""Render markdown to ANSI text without touching stdout."""
return _render_ansi(Markdown(text) if text.strip() else Text(""), width)
def render_text_ansi(text: str, width: int = 80) -> str:
"""Render plain text to ANSI, with no markup interpretation."""
return _render_ansi(Text(text), width)
def render_tool_ansi(text: str, width: int = 100) -> str:
"""Render a verbose tool result as an indented, dimmed block."""
body = Text()
for index, line in enumerate(text.rstrip("\n").split("\n")):
if index:
body.append("\n")
body.append(" " + line, style="dim")
return _render_ansi(body, width)
def _render_ansi(renderable: RenderableType, width: int) -> str:
from io import StringIO
console = Console(
file=StringIO(),
width=max(20, width),
force_terminal=True,
color_system="truecolor",
legacy_windows=False,
highlight=False,
soft_wrap=False,
)
console.print(renderable)
# `print` terminates its last line. Callers embed this in a larger
# document, so the extra newline shows up as a stray blank row there.
raw = console.file.getvalue().rstrip("\n") # type: ignore[union-attr]
return "\n".join(line.rstrip() for line in raw.split("\n"))
def wordmark_to_ansi(rows: Optional[int] = None) -> str:
"""The wordmark as ANSI text, optionally only its first N rows.
Truncating has to keep the spans. The gradient lives in them, so slicing
the plain string and re-rendering would give a flat grey block instead of
the ramp.
"""
# Split the Text itself, not its plain form: the spans carry the gradient,
# and re-joining plain strings would flatten every row to one colour.
lines = wordmark().split("\n", allow_blank=True)
# The rendered Text keeps its trailing newline, which would show up as a
# stray blank line inside the transcript.
while lines and not lines[-1].plain.strip():
lines.pop()
if rows is not None:
lines = lines[:rows]
trimmed = Text()
for index, line in enumerate(lines):
if index:
trimmed.append("\n")
trimmed.append_text(line)
return _render_ansi(trimmed, 200)
# ----------------------------------------------------------------------
# Streaming
# ----------------------------------------------------------------------
class StreamRenderer:
"""Renders an answer into one live region, markdown and all.
Markdown cannot be appended to incrementally -- a half-typed code fence is
not the same document as a closed one -- so the accumulated text is
re-rendered on each delta. For answer-sized text that is cheap, and it buys
correctly formatted output the instant it is complete.
"""
def __init__(self, console: Console, markdown: bool = True, transient: bool = False) -> None:
self.console = console
self.markdown = markdown
self.buffer: List[str] = []
self._live: Optional[Live] = None
self.started = time.monotonic()
self.elapsed: float = 0.0
def _renderable(self) -> RenderableType:
text = "".join(self.buffer)
if not text:
return Text("")
if self.markdown:
return Markdown(text)
return Text(text)
def start(self) -> None:
if self._live is not None:
return
self.started = time.monotonic()
self._live = Live(
self._renderable(),
console=self.console,
refresh_per_second=12,
transient=False,
vertical_overflow="visible",
)
self._live.start()
def feed(self, piece: str) -> None:
"""Append a delta and repaint."""
self.buffer.append(piece)
if self._live is not None:
self._live.update(self._renderable())
def stop(self) -> str:
"""Finish the region, leaving the rendered answer on screen."""
self.elapsed = time.monotonic() - self.started
if self._live is not None:
self._live.update(self._renderable())
self._live.stop()
self._live = None
return "".join(self.buffer)
# ----------------------------------------------------------------------
# Session chrome
# ----------------------------------------------------------------------
class SessionView:
"""Renders the header, tool activity, status, and footer for one session."""
def __init__(self, console: Console, agent: Any, mcp_manager: Any = None) -> None:
self.console = console
self.agent = agent
self.mcp_manager = mcp_manager
self.started = time.monotonic()
self.usage = Usage()
self._spinner_cycle = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
# -- header ---------------------------------------------------------
def banner(self) -> None:
agent = self.agent
mcp_tools = len(self.mcp_manager.tools) if self.mcp_manager else 0
built_in = len(agent.registry) - mcp_tools
servers = len(self.mcp_manager.servers) if self.mcp_manager else 0
tools_line = f"{built_in} built-in"
if servers:
tools_line += f" · {mcp_tools} from {servers} MCP server{'s' if servers != 1 else ''}"
stats = self.stats_line()
body = Group(
wordmark(),
Text(""),
Text.from_markup(f"[dim]v{agentux_version()}[/dim]"),
Text(""),
_rule(),
_field("model", agent.provider.get_model_name()),
_field("workspace", agent.config.working_dir),
_field("tools", tools_line),
_field("context", stats),
Text(""),
_rule(),
Text.from_markup(
"[dim]/help[/dim] [dim]/model[/dim] [dim]/set[/dim] "
"[dim]/tools[/dim] [dim]/mcp[/dim] [dim]/context[/dim] "
"[dim]/reset[/dim] [dim]/exit[/dim]"
),
)
self.console.print()
self.console.print(
Panel(body, border_style=ACCENT_START_HEX, padding=(1, 2), title=None)
)
self.console.print()
# -- activity -------------------------------------------------------
def narration(self, text: str) -> None:
self.console.print(f" [dim italic]{_shorten(text, 160)}[/dim italic]")
def tool_call(self, text: str) -> None:
name = text.split("(", 1)[0]
arguments = text[len(name):]
self.console.print(
Text(" ⏺ ", style=ACCENT_START_HEX)
+ Text(name, style=f"bold {ACCENT_START_HEX}")
+ Text(_shorten(arguments, 100), style="dim")
)
def tool_error(self, text: str) -> None:
self.console.print(f" [red]✗[/red] [red]{_shorten(text, 300)}[/red]")
def tool_output(self, text: str, verbose: bool) -> None:
if not verbose:
return
self.console.print(
Panel(_shorten(text, 6000), title="result", border_style="dim", padding=(0, 1))
)
def reflection(self, text: str) -> None:
self.console.print(f" [yellow]⟳[/yellow] [dim yellow]{_shorten(text, 120)}[/dim yellow]")
# -- status ---------------------------------------------------------
def status(self) -> str:
"""The line shown while the agent is working.
Deliberately one line: a status that reflows the terminal is a status
the user has to wait for. Everything here is short enough to stay put.
"""
frame = self._spinner_cycle[int(time.monotonic() * 10) % len(self._spinner_cycle)]
elapsed = time.monotonic() - self.started
tools = len(self.agent.step_log)
parts = [
f"[{ACCENT_START_HEX}]{frame}[/{ACCENT_START_HEX}] [bold]working[/bold]",
f"[dim]{_format(elapsed)}[/dim]",
f"[dim]{tools} tool{'s' if tools != 1 else ''}[/dim]",
]
tokens = self.usage.total_tokens
if tokens:
# A leading ~ marks a figure the provider did not report.
source = "" if self.usage.reported else "~"
parts.append(f"[dim]{source}{format_tokens(tokens)} tok[/dim]")
return " " + " [dim]·[/dim] ".join(parts)
def stats_line(self) -> Text:
"""Context occupancy, for the banner and ``/context``."""
return self.context_stats()
def context_stats(self) -> Text:
"""The occupancy bar and its numbers, as a ``Text``.
A ``Text`` and not a markup string: every caller appends this into an
existing ``Text``, and ``Text.append`` does not parse markup, so a
returned ``"[green]...[/green]"`` would be shown to the user verbatim.
"""
history = getattr(self.agent, "history", [])
system = history[0].content if history and history[0].role == "system" else ""
tools = self.agent.registry.definitions()
used = conversation_tokens(history, system or "", tools)
window = DEFAULT_CONTEXT_WINDOW
fraction = min(1.0, used / window) if window else 0.0
text = Text()
text.append_text(context_bar_text(fraction, 16))
text.append(f" {fraction * 100:.0f}%", style="dim")
text.append(f" {format_tokens(used)} / {format_tokens(window)}", style="dim")
return text
# -- footer ---------------------------------------------------------
def summary(self, steps: int) -> None:
elapsed = time.monotonic() - self.started
pieces = [
f"{len(self.agent.step_log)} tool call{'s' if len(self.agent.step_log) != 1 else ''}",
f"{steps} step{'s' if steps != 1 else ''}",
_format(elapsed),
]
if self.usage.total_tokens:
pieces.append(f"{'~' if not self.usage.reported else ''}"
f"{format_tokens(self.usage.total_tokens)} tokens")
failures = sum(1 for entry in self.agent.step_log if entry[2])
if failures:
pieces.append(f"{failures} failed")
self.console.print()
self.console.print(
Text(" " + " · ".join(pieces), style="dim")
)
self.console.print(_rule())
self.started = time.monotonic()
# ----------------------------------------------------------------------
ACCENT_START_HEX = interpolate(ACCENT_START, ACCENT_END, 0.15)
ACCENT_END_HEX = interpolate(ACCENT_START, ACCENT_END, 0.85)
def _rule() -> Text:
return Text("─" * 60, style="dim")
def _field(label: str, value: Union[str, Text]) -> Text:
"""One ``label value`` row of the banner.
A ``Text`` value keeps its own styles. The context row passes one so the
bar can stay green/yellow/red instead of being flattened to cyan, and so
its markup tags are never shown literally.
"""
text = Text()
text.append(f"{label:<11}", style="dim")
if isinstance(value, Text):
text.append_text(value.copy())
else:
text.append(value, style="cyan")
return text
def _shorten(text: str, limit: int) -> str:
text = text.strip()
if len(text) <= limit:
return text
return text[:limit] + "…"
def _format(seconds: float) -> str:
return format_duration(seconds)
def agentux_version() -> str:
from . import __version__
return __version__
__all__ = [
"StreamRenderer",
"SessionView",
"gradient_text",
"wordmark",
"interpolate",
"ACCENT_START",
"ACCENT_END",
]
|