-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathChatToolWindowContentManager.java
More file actions
145 lines (124 loc) · 5.32 KB
/
ChatToolWindowContentManager.java
File metadata and controls
145 lines (124 loc) · 5.32 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
package ee.carlrobert.codegpt.toolwindow.chat;
import static java.util.Objects.requireNonNull;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComponentContainer;
import com.intellij.openapi.wm.RegisterToolWindowTask;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import ee.carlrobert.codegpt.CodeGPTBundle;
import ee.carlrobert.codegpt.Icons;
import ee.carlrobert.codegpt.completions.ConversationType;
import ee.carlrobert.codegpt.conversations.Conversation;
import ee.carlrobert.codegpt.conversations.ConversationService;
import ee.carlrobert.codegpt.conversations.ConversationsState;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.prompts.PromptsSettings;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;
@Service(Service.Level.PROJECT)
public final class ChatToolWindowContentManager {
private final Project project;
public ChatToolWindowContentManager(Project project) {
this.project = project;
}
public void sendMessageInNewTab(Message message, ConversationType conversationType) {
createNewTabPanel().sendMessage(message, conversationType);
}
public void sendMessage(Message message) {
sendMessage(message, ConversationType.DEFAULT);
}
public void sendMessage(Message message, ConversationType conversationType) {
getToolWindow().show();
var startInNewWindow = ApplicationManager.getApplication().getService(PromptsSettings.class)
.getState()
.getChatActions()
.getStartInNewWindow();
if (startInNewWindow || ConversationsState.getInstance(project).getCurrentConversation() == null) {
createNewTabPanel().sendMessage(message, conversationType);
return;
}
tryFindChatTabbedPane()
.map(tabbedPane -> tabbedPane.tryFindActiveTabPanel().orElseGet(this::createNewTabPanel))
.orElseGet(this::createNewTabPanel)
.sendMessage(message, conversationType);
}
public Optional<ChatToolWindowTabPanel> tryFindActiveChatTabPanel() {
return tryFindChatTabbedPane().flatMap(ChatToolWindowTabbedPane::tryFindActiveTabPanel);
}
public void displayConversation(@NotNull Conversation conversation) {
displayChatTab();
tryFindChatTabbedPane()
.ifPresent(tabbedPane -> tabbedPane.tryFindTabTitle(conversation.getId())
.ifPresentOrElse(
title -> tabbedPane.setSelectedIndex(tabbedPane.indexOfTab(title)),
() -> tabbedPane.addNewTab(new ChatToolWindowTabPanel(project, conversation))));
}
public ChatToolWindowTabPanel createNewTabPanel() {
displayChatTab();
return tryFindChatTabbedPane()
.map(item -> {
var panel = new ChatToolWindowTabPanel(
project,
ConversationService.getInstance(project).startConversation());
item.addNewTab(panel);
return panel;
})
.orElseThrow();
}
public void displayChatTab() {
var toolWindow = getToolWindow();
toolWindow.show();
var contentManager = toolWindow.getContentManager();
tryFindFirstChatTabContent().ifPresentOrElse(
contentManager::setSelectedContent,
() -> contentManager.setSelectedContent(requireNonNull(contentManager.getContent(0)))
);
}
public Optional<ChatToolWindowTabbedPane> tryFindChatTabbedPane() {
var chatTabContent = tryFindFirstChatTabContent();
if (chatTabContent.isPresent()) {
var chatToolWindowPanel = (ChatToolWindowPanel) chatTabContent.get().getComponent();
return Optional.of(chatToolWindowPanel.getChatTabbedPane());
}
return Optional.empty();
}
public Optional<ChatToolWindowPanel> tryFindChatToolWindowPanel() {
return tryFindFirstChatTabContent()
.map(ComponentContainer::getComponent)
.filter(component -> component instanceof ChatToolWindowPanel)
.map(component -> (ChatToolWindowPanel) component);
}
public void resetAll() {
tryFindChatTabbedPane().ifPresent(tabbedPane -> {
tabbedPane.clearAll();
tabbedPane.addNewTab(new ChatToolWindowTabPanel(
project,
ConversationService.getInstance(project).startConversation()));
});
}
public @NotNull ToolWindow getToolWindow() {
var toolWindowManager = ToolWindowManager.getInstance(project);
var toolWindow = toolWindowManager.getToolWindow("ProxyAI");
// https://intellij-support.jetbrains.com/hc/en-us/community/posts/11533368171026/comments/11538403084562
return Objects.requireNonNullElseGet(toolWindow, () -> toolWindowManager
.registerToolWindow(RegisterToolWindowTask.closable(
"ProxyAI",
() -> CodeGPTBundle.get("project.label"),
Icons.DefaultSmall,
ToolWindowAnchor.RIGHT)));
}
private Optional<Content> tryFindFirstChatTabContent() {
return Arrays.stream(getToolWindow().getContentManager().getContents())
.filter(content -> "Chat".equals(content.getTabName()))
.findFirst();
}
public void clearAllTags() {
tryFindActiveChatTabPanel().ifPresent(ChatToolWindowTabPanel::clearAllTags);
}
}