-
-
Notifications
You must be signed in to change notification settings - Fork 947
Expand file tree
/
Copy pathrepl_handler.py
More file actions
69 lines (60 loc) · 2.72 KB
/
repl_handler.py
File metadata and controls
69 lines (60 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from typing import Any
import typer
from rich import print as rich_print
from rich.rule import Rule
from ..role import DefaultRoles, SystemRole
from ..utils import run_command, get_fixed_prompt, extract_command_from_completion
from .chat_handler import ChatHandler
from .default_handler import DefaultHandler
class ReplHandler(ChatHandler):
def __init__(self, chat_id: str, role: SystemRole, markdown: bool) -> None:
super().__init__(chat_id, role, markdown)
@classmethod
def _get_multiline_input(cls) -> str:
multiline_input = ""
while (user_input := typer.prompt("...", prompt_suffix="")) != '"""':
multiline_input += user_input + "\n"
return multiline_input
def handle(self, init_prompt: str, **kwargs: Any) -> None: # type: ignore
if self.initiated:
rich_print(Rule(title="Chat History", style="bold magenta"))
self.show_messages(self.chat_id, self.markdown)
rich_print(Rule(style="bold magenta"))
info_message = (
"Entering REPL mode, press Ctrl+C to exit."
if not self.role.name == DefaultRoles.SHELL.value
else (
"Entering shell REPL mode, type [e] to execute commands "
"or [d] to describe the commands, or [f] to fix the last command, press Ctrl+C to exit."
)
)
typer.secho(info_message, fg="yellow")
if init_prompt:
rich_print(Rule(title="Input", style="bold purple"))
typer.echo(init_prompt)
rich_print(Rule(style="bold purple"))
full_completion = ""
while True:
# Infinite loop until user exits with Ctrl+C.
prompt = typer.prompt(">>>", prompt_suffix=" ")
if prompt == '"""':
prompt = self._get_multiline_input()
if prompt == "exit()":
raise typer.Exit()
if init_prompt:
prompt = f"{init_prompt}\n\n\n{prompt}"
init_prompt = ""
if self.role.name == DefaultRoles.SHELL.value and prompt == "f":
prompt = get_fixed_prompt()
command = extract_command_from_completion(full_completion)
if self.role.name == DefaultRoles.SHELL.value and prompt == "e":
typer.echo()
run_command(command)
typer.echo()
rich_print(Rule(style="bold magenta"))
elif self.role.name == DefaultRoles.SHELL.value and prompt == "d":
DefaultHandler(
DefaultRoles.DESCRIBE_SHELL.get_role(), self.markdown
).handle(prompt=command, **kwargs)
else:
full_completion = super().handle(prompt=prompt, **kwargs)