Files
EvoScientist-Multi/EvoScientist/cli/widgets/assistant_message.py
T
X-iZhang 03f4134190 feat: add UserMessage widget and UI backend selection during onboarding
- Introduced UserMessage widget for displaying user input with a styled prompt.
- Updated onboarding steps to include UI backend selection (Rich CLI or Textual TUI).
- Modified EvoScientistConfig to store selected UI backend.
- Enhanced configuration handling to support UI backend environment variable.
- Updated README with new UI backend options and commands.
- Added tests for new UI backend functionality and UserMessage widget.
- Removed obsolete test files and ensured existing tests are updated accordingly.
2026-02-21 01:18:33 +00:00

51 lines
1.5 KiB
Python

"""Assistant message widget with incremental Markdown rendering."""
from __future__ import annotations
from textual.containers import Vertical
from textual.widgets import Markdown
class AssistantMessage(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
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 re-render the Markdown widget."""
self._content += text
self.query_one(Markdown).update(self._content)
async def stop_stream(self) -> None:
"""Finalize the stream — ensure final content is rendered."""
if self._content:
self.query_one(Markdown).update(self._content)