| 75 | |
| 76 | |
| 77 | class RichChatIO(ChatIO): |
| 78 | bindings = KeyBindings() |
| 79 | |
| 80 | @bindings.add("escape", "enter") |
| 81 | def _(event): |
| 82 | event.app.current_buffer.newline() |
| 83 | |
| 84 | def __init__(self, multiline: bool = False, mouse: bool = False): |
| 85 | self._prompt_session = PromptSession(history=InMemoryHistory()) |
| 86 | self._completer = WordCompleter( |
| 87 | words=["!!exit", "!!reset", "!!remove", "!!regen", "!!save", "!!load"], |
| 88 | pattern=re.compile("$"), |
| 89 | ) |
| 90 | self._console = Console() |
| 91 | self._multiline = multiline |
| 92 | self._mouse = mouse |
| 93 | |
| 94 | def prompt_for_input(self, role) -> str: |
| 95 | self._console.print(f"[bold]{role}:") |
| 96 | # TODO(suquark): multiline input has some issues. fix it later. |
| 97 | prompt_input = self._prompt_session.prompt( |
| 98 | completer=self._completer, |
| 99 | multiline=False, |
| 100 | mouse_support=self._mouse, |
| 101 | auto_suggest=AutoSuggestFromHistory(), |
| 102 | key_bindings=self.bindings if self._multiline else None, |
| 103 | ) |
| 104 | self._console.print() |
| 105 | return prompt_input |
| 106 | |
| 107 | def prompt_for_output(self, role: str): |
| 108 | self._console.print(f"[bold]{role.replace('/', '|')}:") |
| 109 | |
| 110 | def stream_output(self, output_stream): |
| 111 | """Stream output from a role.""" |
| 112 | # TODO(suquark): the console flickers when there is a code block |
| 113 | # above it. We need to cut off "live" when a code block is done. |
| 114 | |
| 115 | # Create a Live context for updating the console output |
| 116 | with Live(console=self._console, refresh_per_second=4) as live: |
| 117 | # Read lines from the stream |
| 118 | for outputs in output_stream: |
| 119 | if not outputs: |
| 120 | continue |
| 121 | text = outputs["text"] |
| 122 | # Render the accumulated text as Markdown |
| 123 | # NOTE: this is a workaround for the rendering "unstandard markdown" |
| 124 | # in rich. The chatbots output treat "\n" as a new line for |
| 125 | # better compatibility with real-world text. However, rendering |
| 126 | # in markdown would break the format. It is because standard markdown |
| 127 | # treat a single "\n" in normal text as a space. |
| 128 | # Our workaround is adding two spaces at the end of each line. |
| 129 | # This is not a perfect solution, as it would |
| 130 | # introduce trailing spaces (only) in code block, but it works well |
| 131 | # especially for console output, because in general the console does not |
| 132 | # care about trailing spaces. |
| 133 | lines = [] |
| 134 | for line in text.splitlines(): |
no outgoing calls
no test coverage detected
searching dependent graphs…