forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinplace_chat_handler.py
More file actions
337 lines (298 loc) · 11.7 KB
/
inplace_chat_handler.py
File metadata and controls
337 lines (298 loc) · 11.7 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import CopilotChat.prompts as system_prompts
from CopilotChat.handlers.chat_handler import ChatHandler
from CopilotChat.mypynvim.core.buffer import MyBuffer
from CopilotChat.mypynvim.core.nvim import MyNvim
from CopilotChat.mypynvim.ui_components.layout import Box, Layout
from CopilotChat.mypynvim.ui_components.popup import PopUp
# Define constants for the models
MODEL_GPT4 = "gpt-4"
MODEL_GPT35_TURBO = "gpt-3.5-turbo"
# TODO: change the layout, e.g: move to right side of the screen
class InPlaceChatHandler:
"""This class handles in-place chat functionality."""
def __init__(self, nvim: MyNvim):
"""Initialize the InPlaceChatHandler with the given nvim instance."""
self.nvim: MyNvim = nvim
self.diff_mode: bool = False
self.model: str = MODEL_GPT4
self.system_prompt: str = "SENIOR_DEVELOPER_PROMPT"
self.language = self.nvim.eval("g:copilot_chat_language")
# Add user prompts collection
self.user_prompts = self.nvim.eval("g:copilot_chat_user_prompts")
self.current_user_prompt = 0
# Initialize popups
self.original_popup = PopUp(nvim, title="Original")
self.copilot_popup = PopUp(
nvim,
title=f"Copilot ({self.model}, {self.system_prompt})",
opts={"wrap": True, "linebreak": True},
)
self.prompt_popup = PopUp(
nvim, title="Prompt", enter=True, padding={"left": 1, "right": 1}
)
self.help_popup = PopUp(nvim, title="Help")
self.popups = [
self.original_popup,
self.copilot_popup,
self.prompt_popup,
]
# Initialize layout base on help text option
self.help_popup_visible = self.nvim.eval("g:copilot_chat_show_help") == "yes"
if self.help_popup_visible:
self.layout = self._create_layout()
self.popups.append(self.help_popup)
else:
self.layout = self._create_layout_without_help()
# Initialize chat handler
self.chat_handler = ChatHandler(nvim, self.copilot_popup.buffer)
# Set keymaps and help content
self._set_keymaps()
self._set_help_content()
def _create_layout(self):
"""Create the layout for the chat handler."""
return Layout(
self.nvim,
Box(
[
Box(
[
Box([self.original_popup]),
Box([self.copilot_popup]),
],
size=["50%", "50%"],
direction="row",
),
Box(
[Box([self.prompt_popup]), Box([self.help_popup])],
size=["50%", "50%"],
direction="row",
),
],
size=["80%", "20%"],
direction="col",
),
width="80%",
height="60%",
relative="editor",
row="50%",
col="50%",
)
def _create_layout_without_help(self):
"""Create the layout with help for the chat handler."""
return Layout(
self.nvim,
Box(
[
Box(
[
Box([self.original_popup]),
Box([self.copilot_popup]),
],
size=["50%", "50%"],
direction="row",
),
Box(
[Box([self.prompt_popup]), Box([])],
size=["100%", "0%"],
direction="row",
),
],
size=["80%", "20%"],
direction="col",
),
width="80%",
height="60%",
relative="editor",
row="50%",
col="50%",
)
def mount(
self, original_code: str, filetype: str, range: list[int], user_buffer: MyBuffer
):
"""Mount the chat handler with the given parameters."""
self.original_code = original_code
self.filetype = filetype
self.range = [range[0] - 1, range[1]]
self.user_buffer = user_buffer
self.original_popup.buffer.lines(original_code)
self.original_popup.buffer.options["filetype"] = filetype
self.copilot_popup.buffer.options["filetype"] = "markdown"
self.layout.mount()
def _replace_original_code(self):
"""Replace the original code with the new code."""
new_lines = self.copilot_popup.buffer.lines()
if new_lines[0].startswith("```"):
new_lines = new_lines[1:-1]
self.user_buffer.lines(new_lines, self.range[0], self.range[1])
self.layout.unmount()
self.nvim.command("norm! ^")
def _diff(self):
"""Show the difference between the original code and the new code."""
if not self.diff_mode:
self.original_popup.window.command("diffthis")
self.copilot_popup.window.command("diffthis")
self.diff_mode = True
else:
self.original_popup.window.command("diffoff")
self.diff_mode = False
def _chat(self):
"""Start a chat session."""
self.copilot_popup.buffer.lines("")
self.copilot_popup.window.command("norm! gg")
prompt_lines = self.prompt_popup.buffer.lines()
prompt = "\n".join(prompt_lines)
self.chat_handler.chat(
prompt,
self.filetype,
self.original_code,
self.copilot_popup.window.handle,
system_prompt=system_prompts.__dict__[self.system_prompt],
disable_start_separator=True,
disable_end_separator=True,
model=self.model,
)
def _set_prompt(self, prompt: str):
self.prompt_popup.buffer.lines(prompt)
def _set_next_user_prompt(self):
self.current_user_prompt = (self.current_user_prompt + 1) % len(
self.user_prompts
)
prompt = list(self.user_prompts.keys())[self.current_user_prompt]
self.prompt_popup.buffer.lines(self.user_prompts[prompt])
def _set_previous_user_prompt(self):
self.current_user_prompt = (self.current_user_prompt - 1) % len(
self.user_prompts
)
prompt = list(self.user_prompts.keys())[self.current_user_prompt]
self.prompt_popup.buffer.lines(self.user_prompts[prompt])
def _toggle_model(self):
if self.model == MODEL_GPT4:
self.model = MODEL_GPT35_TURBO
else:
self.model = MODEL_GPT4
self.copilot_popup.original_config.title = (
f"Copilot ({self.model}, {self.system_prompt})"
)
self.copilot_popup.config.title = (
f"Copilot ({self.model}, {self.system_prompt})"
)
self.copilot_popup.unmount()
self.copilot_popup.mount(controlled=True)
def _toggle_system_model(self):
# Create a list of all system prompts and add the current system prompt
system_prompts = [
"SENIOR_DEVELOPER_PROMPT",
"COPILOT_EXPLAIN",
"COPILOT_TESTS",
"COPILOT_FIX",
"COPILOT_WORKSPACE",
"TEST_SHORTCUT",
"EXPLAIN_SHORTCUT",
"FIX_SHORTCUT",
]
# Get the index of the current system prompt
current_system_prompt_index = system_prompts.index(self.system_prompt)
# Set the next system prompt
self.system_prompt = system_prompts[
(current_system_prompt_index + 1) % len(system_prompts)
]
self.copilot_popup.original_config.title = (
f"Copilot ({self.model}, {self.system_prompt})"
)
self.copilot_popup.config.title = (
f"Copilot ({self.model}, {self.system_prompt})"
)
self.copilot_popup.unmount()
self.copilot_popup.mount(controlled=True)
# TODO: Add custom keymaps for in-place chat as suggestion here https://discord.com/channels/1200633211236122665/1200633212041449606/1208065809285382164
def _set_keymaps(self):
"""Set the keymaps for the chat handler."""
self.prompt_popup.map("n", "<CR>", lambda: self._chat())
self.prompt_popup.map("n", "<C-CR>", lambda: self._replace_original_code())
self.prompt_popup.map("n", "<C-d>", lambda: self._diff())
self.prompt_popup.map("n", "<C-g>", lambda cb=self._toggle_model: cb())
self.prompt_popup.map("n", "<C-m>", lambda cb=self._toggle_system_model: cb())
self.prompt_popup.map(
"n", "'", lambda: self._set_prompt(system_prompts.PROMPT_SIMPLE_DOCSTRING)
)
self.prompt_popup.map(
"n", "s", lambda: self._set_prompt(system_prompts.PROMPT_SEPARATE)
)
self.prompt_popup.map(
"i", "<C-s>", lambda: (self.nvim.feed("<Esc>"), self._chat())
)
self.prompt_popup.map(
"n",
"<C-n>",
lambda: self._set_next_user_prompt(),
)
self.prompt_popup.map(
"n",
"<C-p>",
lambda: self._set_previous_user_prompt(),
)
for i, popup in enumerate(self.popups):
popup.buffer.map("n", "q", lambda: self.layout.unmount())
popup.buffer.map("n", "<C-l>", lambda: self._clear_chat_history())
popup.buffer.map("n", "?", lambda: self._toggle_help())
popup.buffer.map(
"n",
"<Tab>",
lambda i=i: self.popups[(i + 1) % len(self.popups)].focus(),
)
def _clear_chat_history(self):
"""Clear the chat history in the copilot popup."""
self.copilot_popup.buffer.lines([])
def _set_help_content(self):
"""Set the content for the help popup."""
help_content = [
"Navigation:",
" <Tab>: Switch focus between popups",
" q: Close layout",
" ?: Toggle help content",
"",
"Chat in Normal Mode:",
" <CR>: Submit prompt to Copilot",
" <C-CR>: Replace old code with new",
" <C-d>: Show code differences",
" <C-l>: Clear chat history",
"",
"Chat in Insert Mode:",
" <C-s>: Start chat and submit prompt to Copilot",
"",
"Prompt Binding:",
" ': Set prompt to SIMPLE_DOCSTRING",
" s: Set prompt to SEPARATE",
" <C-p>: Get the previous user prompt",
" <C-n>: Set prompt to next item in user prompts",
"",
"Model:",
" <C-g>: Toggle AI model",
" <C-m>: Set system prompt to next item in system prompts",
"",
"User prompts:",
]
for prompt in self.user_prompts:
help_content.append(f" {prompt}: {self.user_prompts[prompt]}")
self.help_popup.buffer.lines(help_content)
def _toggle_help(self):
"""Toggle the visibility of the help popup."""
self.layout.unmount()
if self.help_popup_visible:
self.popups = [
self.original_popup,
self.copilot_popup,
self.prompt_popup,
]
self.layout = self._create_layout_without_help()
else:
self.popups = [
self.original_popup,
self.copilot_popup,
self.prompt_popup,
self.help_popup,
]
self.layout = self._create_layout()
self.help_popup_visible = not self.help_popup_visible
self._set_keymaps()
self.layout.mount()