-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_input.py
More file actions
188 lines (151 loc) · 5.92 KB
/
user_input.py
File metadata and controls
188 lines (151 loc) · 5.92 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from textual.widget import Widget
from textual.app import ComposeResult
from textual.widgets import TextArea, OptionList
from textual.binding import Binding
from textual.events import DescendantBlur
from agent_chat_cli.components.caret import Caret
from agent_chat_cli.components.flex import Flex
from agent_chat_cli.components.slash_command_menu import SlashCommandMenu
from agent_chat_cli.components.model_selection_menu import ModelSelectionMenu
from agent_chat_cli.core.actions import Actions
from agent_chat_cli.utils.enums import Key
class UserInput(Widget):
BINDINGS = [
Binding(Key.ENTER.value, "submit", "Submit", priority=True),
]
def __init__(self, actions: Actions) -> None:
super().__init__()
self.actions = actions
self.message_history: list[str] = []
self.history_index: int | None = None
self.draft_message: str = ""
def compose(self) -> ComposeResult:
with Flex():
yield Caret()
yield TextArea(
"",
show_line_numbers=False,
soft_wrap=True,
)
yield SlashCommandMenu(
actions=self.actions, on_filter_change=self._on_filter_change
)
yield ModelSelectionMenu(actions=self.actions)
def _on_filter_change(self, char: str) -> None:
text_area = self.query_one(TextArea)
if char == Key.BACKSPACE.value:
text_area.action_delete_left()
else:
text_area.insert(char)
def on_mount(self) -> None:
input_widget = self.query_one(TextArea)
input_widget.focus()
def on_descendant_blur(self, event: DescendantBlur) -> None:
if not self.display:
return
menu = self._get_visible_menu()
if isinstance(event.widget, TextArea) and not menu:
event.widget.focus(scroll_visible=False)
elif isinstance(event.widget, OptionList) and menu:
menu_option_list = menu.query_one(OptionList)
if event.widget == menu_option_list:
menu.hide()
self.query_one(TextArea).focus(scroll_visible=False)
def on_text_area_changed(self, event: TextArea.Changed) -> None:
menu = self.query_one(SlashCommandMenu)
text = event.text_area.text
if text == Key.SLASH.value:
event.text_area.clear()
menu.show()
async def on_key(self, event) -> None:
menu = self._get_visible_menu()
if menu:
self._close_menu(event, menu)
return
if event.key == "up":
await self._navigate_history(event, direction=-1)
return
if event.key == "down":
await self._navigate_history(event, direction=1)
return
if event.key == Key.CTRL_J.value:
self._insert_newline(event)
return
def _insert_newline(self, event) -> None:
event.stop()
event.prevent_default()
input_widget = self.query_one(TextArea)
input_widget.insert("\n")
def _close_menu(self, event, menu: SlashCommandMenu | ModelSelectionMenu) -> None:
if event.key == Key.ESCAPE.value:
event.stop()
event.prevent_default()
menu.hide()
input_widget = self.query_one(TextArea)
input_widget.clear()
input_widget.focus()
return
if isinstance(menu, SlashCommandMenu) and event.key in (
Key.BACKSPACE.value,
Key.DELETE.value,
):
if menu.filter_text:
menu.filter_text = menu.filter_text[:-1]
menu._refresh_options()
self.query_one(TextArea).action_delete_left()
else:
event.stop()
event.prevent_default()
menu.hide()
input_widget = self.query_one(TextArea)
input_widget.clear()
input_widget.focus()
async def _navigate_history(self, event, direction: int) -> None:
event.stop()
event.prevent_default()
input_widget = self.query_one(TextArea)
if direction < 0:
if not self.message_history:
return
if self.history_index is None:
self.draft_message = input_widget.text
self.history_index = len(self.message_history) - 1
elif self.history_index > 0:
self.history_index -= 1
else:
if self.history_index is None:
return
self.history_index += 1
if self.history_index >= len(self.message_history):
self.history_index = None
input_widget.text = self.draft_message
input_widget.move_cursor_relative(rows=999, columns=999)
return
input_widget.text = self.message_history[self.history_index]
input_widget.move_cursor_relative(rows=999, columns=999)
def _get_visible_menu(self) -> SlashCommandMenu | ModelSelectionMenu | None:
slash_menu = self.query_one(SlashCommandMenu)
if slash_menu.is_visible:
return slash_menu
model_menu = self.query_one(ModelSelectionMenu)
if model_menu.is_visible:
return model_menu
return None
async def action_submit(self) -> None:
menu = self._get_visible_menu()
if menu:
option_list = menu.query_one(OptionList)
option_list.action_select()
input_widget = self.query_one(TextArea)
input_widget.clear()
input_widget.focus()
return
input_widget = self.query_one(TextArea)
user_message = input_widget.text.strip()
if not user_message:
return
self.message_history.append(user_message)
self.history_index = None
self.draft_message = ""
input_widget.clear()
await self.actions.post_user_message(user_message)