ensure the Live is stopped if exceptions occur

This commit is contained in:
Paul Gauthier 2023-05-11 20:40:07 -07:00
parent d44f732d3b
commit f29eaf451b

View file

@ -343,34 +343,36 @@ class Coder:
return self.resp, interrupted return self.resp, interrupted
def show_send_output(self, completion, silent): def show_send_output(self, completion, silent):
live = None
if self.pretty and not silent: if self.pretty and not silent:
live = Live(vertical_overflow="scroll") live = Live(vertical_overflow="scroll")
live.start()
else:
live = None
for chunk in completion: try:
if chunk.choices[0].finish_reason not in (None, "stop"): if live:
assert False, "Exceeded context window!" live.start()
try: for chunk in completion:
text = chunk.choices[0].delta.content if chunk.choices[0].finish_reason not in (None, "stop"):
self.resp += text assert False, "Exceeded context window!"
except AttributeError:
continue
if silent: try:
continue text = chunk.choices[0].delta.content
self.resp += text
except AttributeError:
continue
if self.pretty: if silent:
md = Markdown(self.resp, style="blue", code_theme="default") continue
live.update(md)
else:
sys.stdout.write(text)
sys.stdout.flush()
if live: if self.pretty:
live.stop() md = Markdown(self.resp, style="blue", code_theme="default")
live.update(md)
else:
sys.stdout.write(text)
sys.stdout.flush()
finally:
if live:
live.stop()
pattern = re.compile( pattern = re.compile(
# Optional: Matches the start of a code block (e.g., ```python) and any following whitespace # Optional: Matches the start of a code block (e.g., ```python) and any following whitespace
@ -517,7 +519,6 @@ class Coder:
res = self.prompt_ask( res = self.prompt_ask(
"[bright_black]Commit before the chat proceeds? \[y/n/commit message]", # noqa: W605 E501 "[bright_black]Commit before the chat proceeds? \[y/n/commit message]", # noqa: W605 E501
console=self.console,
default="y", default="y",
).strip() ).strip()
self.console.print() self.console.print()