Skip to content

Commit

Permalink
tui: Tweak markdown renderer to match chatgpt
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler committed Mar 16, 2023
1 parent ee46f37 commit 1c54988
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/chap/commands/tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys

import click
from markdown_it import MarkdownIt
from textual.app import App
from textual.binding import Binding
from textual.containers import Container
Expand All @@ -16,14 +17,21 @@
from ..session import Assistant, Session, User


def parser_factory():
parser = MarkdownIt()
parser.options["html"] = False
return parser


class Markdown(Markdown, can_focus=True): # pylint: disable=function-redefined
pass


def markdown_for_step(step):
return Markdown(
step.content.strip().replace("<", "&lt;"),
step.content.strip() or "…",
classes="role_" + step.role,
parser_factory=parser_factory,
)


Expand Down Expand Up @@ -73,7 +81,7 @@ async def on_input_submitted(self, event) -> None:
async def render_fun():
while await update.get():
if tokens:
await output.update("".join(tokens).replace("<", "&lt;"))
await output.update("".join(tokens).strip())
self.container.scroll_end()
await asyncio.sleep(0.1)

Expand All @@ -90,10 +98,10 @@ async def get_token_fun():
await asyncio.gather(render_fun(), get_token_fun())
self.input.value = ""
finally:
all_output = self.session.session[-1].content.replace("<", "&lt;")
all_output = self.session.session[-1].content
await output.update(all_output)
self.container.scroll_end()
output._markdown = all_output # pylint: disable=protected-access
self.container.scroll_end()
self.input.disabled = False

def scroll_end(self):
Expand Down

0 comments on commit 1c54988

Please sign in to comment.