-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslash_command_menu.py
More file actions
91 lines (71 loc) · 2.66 KB
/
slash_command_menu.py
File metadata and controls
91 lines (71 loc) · 2.66 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from typing import Callable
from textual.widget import Widget
from textual.app import ComposeResult
from textual.containers import VerticalScroll
from textual.widgets import OptionList
from textual.widgets.option_list import Option
from agent_chat_cli.core.actions import Actions
COMMANDS = [
{"id": "new", "label": "/new - Start new conversation"},
{"id": "clear", "label": "/clear - Clear chat history"},
{"id": "model", "label": "/model - Change model"},
{"id": "save", "label": "/save - Save conversation to markdown"},
{"id": "exit", "label": "/exit - Exit"},
]
class SlashCommandMenu(Widget):
def __init__(
self, actions: Actions, on_filter_change: Callable[[str], None] | None = None
) -> None:
super().__init__()
self.actions = actions
self.filter_text = ""
self.on_filter_change = on_filter_change
def compose(self) -> ComposeResult:
yield OptionList(*[Option(cmd["label"], id=cmd["id"]) for cmd in COMMANDS])
def show(self) -> None:
self.filter_text = ""
self.add_class("visible")
self._refresh_options()
scroll_containers = self.app.query(VerticalScroll)
if scroll_containers:
scroll_containers.first().scroll_end(animate=False)
def hide(self) -> None:
self.remove_class("visible")
self.filter_text = ""
@property
def is_visible(self) -> bool:
return self.has_class("visible")
def _refresh_options(self) -> None:
option_list = self.query_one(OptionList)
option_list.clear_options()
filtered = [
cmd for cmd in COMMANDS if self.filter_text.lower() in cmd["id"].lower()
]
for cmd in filtered:
option_list.add_option(Option(cmd["label"], id=cmd["id"]))
if filtered:
option_list.highlighted = 0
option_list.focus()
def on_key(self, event) -> None:
if not self.is_visible:
return
if event.is_printable and event.character:
self.filter_text += event.character
self._refresh_options()
if self.on_filter_change:
self.on_filter_change(event.character)
async def on_option_list_option_selected(
self, event: OptionList.OptionSelected
) -> None:
self.hide()
match event.option_id:
case "exit":
self.actions.quit()
case "clear":
await self.actions.clear()
case "new":
await self.actions.new()
case "model":
self.actions.show_model_menu()
case "save":
await self.actions.save()