-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_parser.py
More file actions
68 lines (55 loc) · 2.08 KB
/
Copy pathcommand_parser.py
File metadata and controls
68 lines (55 loc) · 2.08 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
import re
import providers
COMMANDS = [
"chatgpt", "claude", "perplexity",
"login", "history", "clear-all",
"debug", "help", "helpme", "exit",
]
class CommandParser:
def __init__(self):
self.callbacks = {}
def bind(self, **callbacks):
self.callbacks.update(callbacks)
def is_command(self, text: str) -> bool:
return text.strip().startswith("/")
def parse(self, text: str) -> str:
match = re.match(r"/([a-zA-Z0-9_-]+)\s*(.*)", text.strip())
if not match:
return "Unknown command. Try /help"
cmd = match.group(1).lower()
args = match.group(2).strip()
if cmd not in COMMANDS:
return f"Unknown command '/{cmd}'. Try /help"
if cmd in providers.ORDER:
return self._call("switch", cmd)
if cmd == "login":
return self._call("login", args)
if cmd == "history":
return self._call("history")
if cmd == "clear-all":
return self._call("clear_all")
if cmd == "debug":
return self._call("debug")
if cmd == "exit":
return self._call("exit")
if cmd in ("help", "helpme"):
return self.help_text()
return ""
def _call(self, name, *args):
cb = self.callbacks.get(name)
return cb(*args) if cb else ""
def help_text(self) -> str:
rows = [
("/chatgpt", "switch to ChatGPT"),
("/claude", "switch to Claude"),
("/perplexity", "switch to Perplexity"),
("/login [provider]", "sign in (current provider if omitted)"),
("/history", "show recent history"),
("/clear-all", "clear output and stored history"),
("/debug", "show what extraction finds on the page"),
("/help, /helpme", "show this help"),
("/exit", "close the widget"),
]
width = max(len(c) for c, _ in rows) + 3
body = "\n".join(f"{c:<{width}}{d}" for c, d in rows)
return "commands:\n" + body + "\n(plain text is sent to the active provider)"