Files
EvoScientist-Multi/EvoScientist/cli/widgets/assistant_message.py
T
Xi Zhang 3503142af6 feat(tui): UX polish — multi-line input, timestamps, update check & v0.0.3 (#85)
* feat: implement background update check and startup notifications

* feat: enhance user experience with timestamp notifications and UI polish

* feat: implement multi-line chat input with Enter-to-submit and modifier+Enter newline

* update

* update

* v0.0.3

* feat: improve code readability with consistent formatting in TUI and test files

* feat: update PyPI badge version to v0.0.3 in README files

* feat: add docstrings for test classes in test_update_check.py
2026-03-20 19:57:35 +00:00

62 lines
1.9 KiB
Python

"""Assistant message widget with incremental Markdown rendering."""
from __future__ import annotations
from textual.containers import Vertical
from textual.widgets import Markdown
from .timestamp_mixin import TimestampClickMixin
class AssistantMessage(TimestampClickMixin, Vertical):
"""Displays the assistant's final Markdown response.
Mount once, then call :meth:`append_content` for each text chunk.
When streaming finishes, call :meth:`stop_stream`.
Each ``append_content`` call re-renders only *this* widget's Markdown —
not the entire chat history — which is the core improvement over the
old "rebuild Rich Group every 100 ms" approach.
"""
DEFAULT_CSS = """
AssistantMessage {
height: auto;
margin: 1 0 0 0;
}
AssistantMessage Markdown {
margin: 0;
padding: 0;
}
"""
def __init__(self, initial_content: str = "") -> None:
super().__init__()
self._content = initial_content
self._flush_pending = False
def compose(self):
yield Markdown("")
def on_mount(self) -> None:
if self._content:
self.query_one(Markdown).update(self._content)
async def append_content(self, text: str) -> None:
"""Append text and schedule a debounced Markdown re-render."""
self._content += text
if not self._flush_pending:
self._flush_pending = True
self.set_timer(0.1, self._flush_markdown)
def _flush_markdown(self) -> None:
"""Flush accumulated content to the Markdown widget."""
self._flush_pending = False
self.query_one(Markdown).update(self._content)
async def stop_stream(self) -> None:
"""Finalize the stream — ensure final content is rendered."""
self._flush_pending = False
if self._content:
self.query_one(Markdown).update(self._content)