-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathChatToolWindowTabPanel.java
More file actions
542 lines (469 loc) · 19.7 KB
/
ChatToolWindowTabPanel.java
File metadata and controls
542 lines (469 loc) · 19.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package ee.carlrobert.codegpt.toolwindow.chat;
import static ee.carlrobert.codegpt.ui.UIUtil.createScrollPaneWithSmartScroller;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.JBColor;
import com.intellij.util.concurrency.AppExecutorUtil;
import com.intellij.util.ui.JBUI;
import ee.carlrobert.codegpt.CodeGPTKeys;
import ee.carlrobert.codegpt.EncodingManager;
import ee.carlrobert.codegpt.ReferencedFile;
import ee.carlrobert.codegpt.actions.ActionType;
import ee.carlrobert.codegpt.completions.ChatCompletionParameters;
import ee.carlrobert.codegpt.completions.CompletionRequestService;
import ee.carlrobert.codegpt.completions.CompletionRequestUtil;
import ee.carlrobert.codegpt.completions.ConversationType;
import ee.carlrobert.codegpt.completions.ToolwindowChatCompletionRequestHandler;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.ConversationService;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.psistructure.PsiStructureProvider;
import ee.carlrobert.codegpt.psistructure.models.ClassStructure;
import ee.carlrobert.codegpt.settings.service.FeatureType;
import ee.carlrobert.codegpt.telemetry.TelemetryAction;
import ee.carlrobert.codegpt.toolwindow.chat.editor.actions.CopyAction;
import ee.carlrobert.codegpt.toolwindow.chat.structure.data.PsiStructureRepository;
import ee.carlrobert.codegpt.toolwindow.chat.structure.data.PsiStructureState;
import ee.carlrobert.codegpt.toolwindow.chat.ui.ChatMessageResponseBody;
import ee.carlrobert.codegpt.toolwindow.chat.ui.ChatToolWindowScrollablePanel;
import ee.carlrobert.codegpt.toolwindow.chat.ui.textarea.TotalTokensDetails;
import ee.carlrobert.codegpt.toolwindow.chat.ui.textarea.TotalTokensPanel;
import ee.carlrobert.codegpt.toolwindow.ui.ChatToolWindowLandingPanel;
import ee.carlrobert.codegpt.toolwindow.ui.ResponseMessagePanel;
import ee.carlrobert.codegpt.toolwindow.ui.UserMessagePanel;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import ee.carlrobert.codegpt.ui.textarea.ConversationTagProcessor;
import ee.carlrobert.codegpt.ui.textarea.UserInputPanel;
import ee.carlrobert.codegpt.ui.textarea.header.tag.EditorTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.FileTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.FolderTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.GitCommitTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.HistoryTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.PersonaTagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.TagDetails;
import ee.carlrobert.codegpt.ui.textarea.header.tag.TagManager;
import ee.carlrobert.codegpt.util.EditorUtil;
import ee.carlrobert.codegpt.util.coroutines.CoroutineDispatchers;
import git4idea.GitCommit;
import java.awt.BorderLayout;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.swing.JComponent;
import javax.swing.JPanel;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ChatToolWindowTabPanel implements Disposable {
private static final Logger LOG = Logger.getInstance(ChatToolWindowTabPanel.class);
private final ChatSession chatSession;
private final Project project;
private final JPanel rootPanel;
private final Conversation conversation;
private final UserInputPanel userInputPanel;
private final ConversationService conversationService;
private final TotalTokensPanel totalTokensPanel;
private final ChatToolWindowScrollablePanel toolWindowScrollablePanel;
private final PsiStructureRepository psiStructureRepository;
private final TagManager tagManager;
private @Nullable ToolwindowChatCompletionRequestHandler requestHandler;
public ChatToolWindowTabPanel(@NotNull Project project, @NotNull Conversation conversation) {
this.project = project;
this.conversation = conversation;
this.chatSession = new ChatSession();
conversationService = ConversationService.getInstance(project);
toolWindowScrollablePanel = new ChatToolWindowScrollablePanel();
tagManager = new TagManager();
this.psiStructureRepository = new PsiStructureRepository(
this,
project,
tagManager,
new PsiStructureProvider(),
new CoroutineDispatchers()
);
totalTokensPanel = new TotalTokensPanel(
conversation,
EditorUtil.getSelectedEditorSelectedText(project),
this,
psiStructureRepository);
userInputPanel = new UserInputPanel(
project,
totalTokensPanel,
this,
FeatureType.CHAT,
tagManager,
this::handleSubmit,
this::handleCancel,
true,
true);
userInputPanel.requestFocus();
rootPanel = createRootPanel();
if (conversation.getMessages().isEmpty()) {
displayLandingView();
} else {
displayConversation();
}
}
public void dispose() {
LOG.info("Disposing BaseChatToolWindowTabPanel component");
}
public JComponent getContent() {
return rootPanel;
}
public Conversation getConversation() {
return conversation;
}
public ChatSession getChatSession() {
return chatSession;
}
public TotalTokensDetails getTokenDetails() {
return totalTokensPanel.getTokenDetails();
}
public void requestFocusForTextArea() {
userInputPanel.requestFocus();
}
public void displayLandingView() {
toolWindowScrollablePanel.displayLandingView(getLandingView());
totalTokensPanel.updateConversationTokens(conversation);
}
public void addSelection(VirtualFile editorFile, SelectionModel selectionModel) {
userInputPanel.addSelection(editorFile, selectionModel);
}
public void addCommitReferences(List<GitCommit> gitCommits) {
userInputPanel.addCommitReferences(gitCommits);
}
public List<TagDetails> getSelectedTags() {
return userInputPanel.getSelectedTags();
}
private ChatCompletionParameters getCallParameters(
Message message,
ConversationType conversationType,
Set<ClassStructure> psiStructure
) {
final var selectedTags = tagManager.getTags().stream()
.filter(TagDetails::getSelected)
.collect(Collectors.toList());
var builder = ChatCompletionParameters.builder(conversation, message)
.sessionId(chatSession.getId())
.conversationType(conversationType)
.imageDetailsFromPath(CodeGPTKeys.IMAGE_ATTACHMENT_FILE_PATH.get(project))
.referencedFiles(getReferencedFiles(selectedTags))
.history(getHistory(getSelectedTags()))
.psiStructure(psiStructure)
.project(project)
.chatMode(userInputPanel.getChatMode());
findTagOfType(selectedTags, PersonaTagDetails.class)
.ifPresent(tag -> builder.personaDetails(tag.getPersonaDetails()));
findTagOfType(selectedTags, GitCommitTagDetails.class)
.ifPresent(tag -> builder.gitDiff(tag.getGitCommit().getFullMessage()));
return builder.build();
}
private List<ReferencedFile> getReferencedFiles(List<? extends TagDetails> tags) {
return tags.stream()
.map(this::getVirtualFile)
.filter(Objects::nonNull)
.distinct()
.map(ReferencedFile::from)
.toList();
}
private List<UUID> getConversationHistoryIds(List<? extends TagDetails> tags) {
return tags.stream()
.map(it -> {
if (it instanceof HistoryTagDetails tagDetails) {
return tagDetails.getConversationId();
}
return null;
})
.filter(Objects::nonNull)
.toList();
}
private List<Conversation> getHistory(List<? extends TagDetails> tags) {
return tags.stream()
.map(it -> {
if (it instanceof HistoryTagDetails tagDetails) {
return ConversationTagProcessor.Companion.getConversation(
project,
tagDetails.getConversationId());
}
return null;
})
.filter(Objects::nonNull)
.distinct()
.toList();
}
private VirtualFile getVirtualFile(TagDetails tag) {
VirtualFile virtualFile = null;
if (tag.getSelected()) {
if (tag instanceof FileTagDetails) {
virtualFile = ((FileTagDetails) tag).getVirtualFile();
} else if (tag instanceof EditorTagDetails) {
virtualFile = ((EditorTagDetails) tag).getVirtualFile();
} else if (tag instanceof FolderTagDetails) {
virtualFile = ((FolderTagDetails) tag).getFolder();
}
}
return virtualFile;
}
private <T extends TagDetails> Optional<T> findTagOfType(
List<? extends TagDetails> tags,
Class<T> tagClass) {
return tags.stream()
.filter(tagClass::isInstance)
.map(tagClass::cast)
.findFirst();
}
public void sendMessage(Message message, ConversationType conversationType) {
sendMessage(message, conversationType, new HashSet<>());
}
public void sendMessage(
Message message,
ConversationType conversationType,
Set<ClassStructure> psiStructure
) {
var callParameters = getCallParameters(message, conversationType, psiStructure);
if (callParameters.getImageDetails() != null) {
project.getService(ChatToolWindowContentManager.class)
.tryFindChatToolWindowPanel()
.ifPresent(panel -> panel.clearImageNotifications(project));
}
totalTokensPanel.updateConversationTokens(conversation);
if (callParameters.getReferencedFiles() != null) {
totalTokensPanel.updateReferencedFilesTokens(
callParameters.getReferencedFiles().stream().map(ReferencedFile::fileContent).toList());
}
var userMessagePanel = createUserMessagePanel(message, callParameters);
var responseMessagePanel = createResponseMessagePanel(callParameters);
var messagePanel = toolWindowScrollablePanel.addMessage(message.getId());
messagePanel.add(userMessagePanel);
messagePanel.add(responseMessagePanel);
call(callParameters, responseMessagePanel, userMessagePanel);
}
public void clearAllTags() {
tagManager.clear();
}
public void includeFiles(List<VirtualFile> referencedFiles) {
userInputPanel.includeFiles(referencedFiles);
ReadAction.nonBlocking(() -> {
var encodingManager = EncodingManager.getInstance();
return referencedFiles.stream()
.mapToInt(it -> encodingManager.countTokens(ReferencedFile.from(it).fileContent()))
.sum();
}
)
.inSmartMode(project)
.expireWith(project)
.finishOnUiThread(ModalityState.any(), totalTokensPanel::updateReferencedFilesTokens)
.submit(AppExecutorUtil.getAppExecutorService());
}
private boolean hasReferencedFilePaths(Message message) {
return message.getReferencedFilePaths() != null && !message.getReferencedFilePaths().isEmpty();
}
private boolean hasReferencedFilePaths(Conversation conversation) {
return conversation.getMessages().stream()
.anyMatch(
it -> it.getReferencedFilePaths() != null && !it.getReferencedFilePaths().isEmpty());
}
private UserMessagePanel createUserMessagePanel(
Message message,
ChatCompletionParameters callParameters) {
var panel = new UserMessagePanel(project, message, this);
panel.addCopyAction(() -> CopyAction.copyToClipboard(message.getPrompt()));
panel.addReloadAction(() -> reloadMessage(callParameters, panel));
panel.addDeleteAction(() -> removeMessage(message.getId(), conversation));
return panel;
}
private ResponseMessagePanel createResponseMessagePanel(ChatCompletionParameters callParameters) {
var message = callParameters.getMessage();
var fileContextIncluded =
hasReferencedFilePaths(message) || hasReferencedFilePaths(conversation);
var panel = new ResponseMessagePanel();
panel.addCopyAction(() -> CopyAction.copyToClipboard(message.getResponse()));
panel.addContent(new ChatMessageResponseBody(
project,
false,
message.isWebSearchIncluded(),
fileContextIncluded || message.getDocumentationDetails() != null,
true,
this));
return panel;
}
private void reloadMessage(
ChatCompletionParameters prevParameters,
UserMessagePanel userMessagePanel) {
var prevMessage = prevParameters.getMessage();
ResponseMessagePanel responsePanel = null;
try {
responsePanel = toolWindowScrollablePanel.getResponseMessagePanel(prevMessage.getId());
((ChatMessageResponseBody) responsePanel.getContent()).clear();
toolWindowScrollablePanel.update();
} catch (Exception e) {
throw new RuntimeException("Could not delete the existing message component", e);
} finally {
LOG.debug("Reloading message: " + prevMessage.getId());
if (responsePanel != null) {
prevMessage.setResponse("");
conversationService.saveMessage(conversation, prevMessage);
call(prevParameters.toBuilder().retry(true).build(), responsePanel, userMessagePanel);
}
totalTokensPanel.updateConversationTokens(conversation);
TelemetryAction.IDE_ACTION.createActionMessage()
.property("action", ActionType.RELOAD_MESSAGE.name())
.send();
}
}
private void removeMessage(UUID messageId, Conversation conversation) {
toolWindowScrollablePanel.removeMessage(messageId);
conversation.removeMessage(messageId);
conversationService.saveConversation(conversation);
totalTokensPanel.updateConversationTokens(conversation);
if (conversation.getMessages().isEmpty()) {
displayLandingView();
}
}
private void clearWindow() {
toolWindowScrollablePanel.clearAll();
totalTokensPanel.updateConversationTokens(conversation);
}
private void call(
ChatCompletionParameters callParameters,
ResponseMessagePanel responseMessagePanel,
UserMessagePanel userMessagePanel) {
var responseContainer = (ChatMessageResponseBody) responseMessagePanel.getContent();
if (!CompletionRequestService.isRequestAllowed(FeatureType.CHAT)) {
responseContainer.displayMissingCredential();
return;
}
userInputPanel.setSubmitEnabled(false);
userMessagePanel.disableActions(List.of("RELOAD", "DELETE"));
responseMessagePanel.disableActions(List.of("COPY"));
requestHandler = new ToolwindowChatCompletionRequestHandler(
project,
new ToolWindowCompletionResponseEventListener(
project,
userMessagePanel,
responseMessagePanel,
totalTokensPanel,
userInputPanel) {
@Override
public void handleTokensExceededPolicyAccepted() {
call(callParameters, responseMessagePanel, userMessagePanel);
}
});
ApplicationManager.getApplication()
.executeOnPooledThread(() -> requestHandler.call(callParameters));
}
private Unit handleSubmit(String text) {
toolWindowScrollablePanel.scrollToBottom();
var application = ApplicationManager.getApplication();
application.executeOnPooledThread(() -> {
final Set<ClassStructure> psiStructure;
if (psiStructureRepository.getStructureState().getValue()
instanceof PsiStructureState.Content content) {
psiStructure = content.getElements();
} else {
psiStructure = new HashSet<>();
}
final var appliedTags = tagManager.getTags().stream()
.filter(TagDetails::getSelected)
.collect(Collectors.toList());
var messageBuilder = new MessageBuilder(project, text).withInlays(appliedTags);
List<ReferencedFile> referencedFiles = getReferencedFiles(appliedTags);
if (!referencedFiles.isEmpty()) {
messageBuilder.withReferencedFiles(referencedFiles);
}
List<UUID> conversationHistoryIds = getConversationHistoryIds(appliedTags);
if (!conversationHistoryIds.isEmpty()) {
messageBuilder.withConversationHistoryIds(conversationHistoryIds);
}
String attachedImagePath = CodeGPTKeys.IMAGE_ATTACHMENT_FILE_PATH.get(project);
if (attachedImagePath != null) {
messageBuilder.withImage(attachedImagePath);
}
application.invokeLater(() -> {
sendMessage(messageBuilder.build(), ConversationType.DEFAULT, psiStructure);
});
});
return Unit.INSTANCE;
}
private Unit handleCancel() {
if (requestHandler != null) {
requestHandler.cancel();
}
return Unit.INSTANCE;
}
private JPanel createUserPromptPanel() {
var panel = new JPanel(new BorderLayout());
panel.setBorder(JBUI.Borders.compound(
JBUI.Borders.customLine(JBColor.border(), 1, 0, 0, 0),
JBUI.Borders.empty(8)));
panel.add(JBUI.Panels.simplePanel(totalTokensPanel)
.withBorder(JBUI.Borders.emptyBottom(8)), BorderLayout.NORTH);
panel.add(userInputPanel, BorderLayout.CENTER);
return panel;
}
private JComponent getLandingView() {
return new ChatToolWindowLandingPanel((action, locationOnScreen) -> {
var editor = EditorUtil.getSelectedEditor(project);
if (editor == null || !editor.getSelectionModel().hasSelection()) {
OverlayUtil.showWarningBalloon(
editor == null ? "Unable to locate a selected editor"
: "Please select a target code before proceeding",
locationOnScreen);
return Unit.INSTANCE;
}
var formattedCode = CompletionRequestUtil.formatCode(
editor.getSelectionModel().getSelectedText(),
editor.getVirtualFile().getPath());
var message = new Message(action.getPrompt().replace("{SELECTION}", formattedCode));
sendMessage(message, ConversationType.DEFAULT);
return Unit.INSTANCE;
});
}
private void displayConversation() {
clearWindow();
conversation.getMessages().forEach(message -> {
var messagePanel = toolWindowScrollablePanel.addMessage(message.getId());
messagePanel.add(getUserMessagePanel(message));
messagePanel.add(getResponseMessagePanel(message));
});
}
private UserMessagePanel getUserMessagePanel(Message message) {
var userMessagePanel = new UserMessagePanel(project, message, this);
userMessagePanel.addCopyAction(() -> CopyAction.copyToClipboard(message.getPrompt()));
userMessagePanel.addReloadAction(() -> reloadMessage(
ChatCompletionParameters.builder(conversation, message)
.conversationType(ConversationType.DEFAULT)
.project(project)
.chatMode(userInputPanel.getChatMode())
.build(),
userMessagePanel));
userMessagePanel.addDeleteAction(() -> removeMessage(message.getId(), conversation));
return userMessagePanel;
}
private ResponseMessagePanel getResponseMessagePanel(Message message) {
var response = message.getResponse() == null ? "" : message.getResponse();
var messageResponseBody =
new ChatMessageResponseBody(project, this).withResponse(response);
var responseMessagePanel = new ResponseMessagePanel();
responseMessagePanel.addContent(messageResponseBody);
responseMessagePanel.addCopyAction(() -> CopyAction.copyToClipboard(message.getResponse()));
return responseMessagePanel;
}
private JPanel createRootPanel() {
var rootPanel = new JPanel(new BorderLayout());
rootPanel.add(createScrollPaneWithSmartScroller(toolWindowScrollablePanel),
BorderLayout.CENTER);
rootPanel.add(createUserPromptPanel(), BorderLayout.SOUTH);
return rootPanel;
}
}