Skip to content

StreamingMarkdown

Sub classes

Name Children Inherits
AgentMessageView
acptui.ui.widgets.blocks
A message of the agent, streamed as Markdown.

    Base classes

    Name Children Inherits
    Markdown
    textual.widgets._markdown

    ⋔ Inheritance diagram

    graph TD
      107846722155648["blocks.StreamingMarkdown"]
      107846719929408["_markdown.Markdown"]
      107846718477920["widget.Widget"]
      107846718489888["dom.DOMNode"]
      107846718374032["message_pump.MessagePump"]
      137699722799616["builtins.object"]
      107846719929408 --> 107846722155648
      107846718477920 --> 107846719929408
      107846718489888 --> 107846718477920
      107846718374032 --> 107846718489888
      137699722799616 --> 107846718374032

    🛈 DocStrings

    Bases: Markdown

    Markdown that streams appended text, starting only after its initial content is shown.

    Markdown renders its initial content in an async mount handler; text streamed before that finished would be overwritten.

    Source code in src/acptui/ui/widgets/blocks.py
    class StreamingMarkdown(Markdown):
        """Markdown that streams appended text, starting only after its initial content is shown.
    
        ``Markdown`` renders its initial content in an async mount handler; text streamed
        before that finished would be overwritten.
        """
    
        def __init__(self, markdown: str, *, classes: str | None = None) -> None:
            super().__init__(markdown, classes=classes)
            self._ready = asyncio.Event()
            self._stream: MarkdownStream | None = None
            self._shown = markdown
    
        async def sync(self, markdown: str) -> None:
            """Show ``markdown``: stream the new part if it extends what is shown, else replace.
    
            Rendering lags behind the session state (several chunks may have been applied
            before the view exists), so views sync to the full text instead of appending
            each chunk.
            """
            if markdown == self._shown:
                return
            if markdown.startswith(self._shown):
                delta = markdown[len(self._shown) :]
                self._shown = markdown
                await self.append_text(delta)
            else:
                self._shown = markdown
                await self.replace_text(markdown)
    
        def on_mount(self) -> None:
            # Markdown's own mount handler (which shows the initial content) runs after
            # this one, in the same message: become ready once the message is processed.
            self.call_next(self._ready.set)
    
        async def append_text(self, text: str) -> None:
            """Append streamed text."""
            await self._ready.wait()
            if self._stream is None:
                self._stream = Markdown.get_stream(self)
            await self._stream.write(text)
    
        async def replace_text(self, text: str) -> None:
            """Replace all text."""
            await self._ready.wait()
            await self.finish()
            await self.update(text)
    
        async def finish(self) -> None:
            """Flush streamed text."""
            if self._stream is not None:
                stream, self._stream = self._stream, None
                await stream.stop()
    

    append_text async

    append_text(text: str) -> None
    

    Append streamed text.

    Source code in src/acptui/ui/widgets/blocks.py
    async def append_text(self, text: str) -> None:
        """Append streamed text."""
        await self._ready.wait()
        if self._stream is None:
            self._stream = Markdown.get_stream(self)
        await self._stream.write(text)
    

    finish async

    finish() -> None
    

    Flush streamed text.

    Source code in src/acptui/ui/widgets/blocks.py
    async def finish(self) -> None:
        """Flush streamed text."""
        if self._stream is not None:
            stream, self._stream = self._stream, None
            await stream.stop()
    

    replace_text async

    replace_text(text: str) -> None
    

    Replace all text.

    Source code in src/acptui/ui/widgets/blocks.py
    async def replace_text(self, text: str) -> None:
        """Replace all text."""
        await self._ready.wait()
        await self.finish()
        await self.update(text)
    

    sync async

    sync(markdown: str) -> None
    

    Show markdown: stream the new part if it extends what is shown, else replace.

    Rendering lags behind the session state (several chunks may have been applied before the view exists), so views sync to the full text instead of appending each chunk.

    Source code in src/acptui/ui/widgets/blocks.py
    async def sync(self, markdown: str) -> None:
        """Show ``markdown``: stream the new part if it extends what is shown, else replace.
    
        Rendering lags behind the session state (several chunks may have been applied
        before the view exists), so views sync to the full text instead of appending
        each chunk.
        """
        if markdown == self._shown:
            return
        if markdown.startswith(self._shown):
            delta = markdown[len(self._shown) :]
            self._shown = markdown
            await self.append_text(delta)
        else:
            self._shown = markdown
            await self.replace_text(markdown)
    

    :fa-brands-github: Show source on GitHub