Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void update(@NotNull AnActionEvent event) {
public void actionPerformed(@NotNull AnActionEvent event) {
var project = event.getProject();
if (project != null) {
ConversationsState.getInstance().setCurrentConversation(null);
ConversationsState.getInstance(project).setCurrentConversation(null);
var tabPanel =
project.getService(ChatToolWindowContentManager.class).createNewTabPanel();
if (tabPanel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public DeleteAllConversationsAction(Runnable onRefresh) {
public void update(@NotNull AnActionEvent event) {
var project = event.getProject();
if (project != null) {
var sortedConversations = ConversationService.getInstance().getSortedConversations();
var sortedConversations = ConversationService.getInstance(project).getSortedConversations();
event.getPresentation().setEnabled(!sortedConversations.isEmpty());
}
}
Expand All @@ -43,7 +43,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {
var project = event.getProject();
if (project != null) {
try {
ConversationService.getInstance().clearAll();
ConversationService.getInstance(project).clearAll();
project.getService(ChatToolWindowContentManager.class).resetAll();
} finally {
TelemetryAction.IDE_ACTION.createActionMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import ee.carlrobert.codegpt.actions.ActionType;
import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil;
Expand All @@ -24,7 +25,12 @@ public DeleteConversationAction(Runnable onDelete) {

@Override
public void update(@NotNull AnActionEvent event) {
event.getPresentation().setEnabled(ConversationsState.getCurrentConversation() != null);
Project project = event.getProject();
if (project == null) {
event.getPresentation().setEnabled(false);
return;
}
event.getPresentation().setEnabled(ConversationsState.getInstance(project).getCurrentConversation() != null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ protected MoveAction(String text, String description, Icon icon, Runnable onRefr

@Override
public void update(@NotNull AnActionEvent event) {
event.getPresentation().setEnabled(ConversationsState.getCurrentConversation() != null);
Project project = event.getProject();
if (project == null) {
event.getPresentation().setEnabled(false);
return;
}
event.getPresentation().setEnabled(ConversationsState.getInstance(project).getCurrentConversation() != null);
}

@Override
Expand All @@ -32,7 +37,7 @@ public void actionPerformed(@NotNull AnActionEvent event) {
if (project != null) {
getConversation(project)
.ifPresent(conversation -> {
ConversationsState.getInstance().setCurrentConversation(conversation);
ConversationsState.getInstance(project).setCurrentConversation(conversation);
onRefresh.run();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public MoveDownAction(Runnable onRefresh) {

@Override
protected Optional<Conversation> getConversation(@NotNull Project project) {
return ConversationService.getInstance().getPreviousConversation();
return ConversationService.getInstance(project).getPreviousConversation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public MoveUpAction(Runnable onRefresh) {

@Override
protected Optional<Conversation> getConversation(@NotNull Project project) {
return ConversationService.getInstance().getNextConversation();
return ConversationService.getInstance(project).getNextConversation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.testFramework.LightVirtualFile;
Expand All @@ -29,7 +30,13 @@ public OpenInEditorAction() {
@Override
public void update(@NotNull AnActionEvent event) {
super.update(event);
var currentConversation = ConversationsState.getCurrentConversation();
Project project = event.getProject();
if (project == null) {
event.getPresentation().setEnabled(false);
return;
}

var currentConversation = ConversationsState.getInstance(project).getCurrentConversation();
var isEnabled = currentConversation != null && !currentConversation.getMessages().isEmpty();
event.getPresentation().setEnabled(isEnabled);
}
Expand All @@ -38,8 +45,9 @@ public void update(@NotNull AnActionEvent event) {
public void actionPerformed(@NotNull AnActionEvent e) {
try {
var project = e.getProject();
var currentConversation = ConversationsState.getCurrentConversation();
if (project != null && currentConversation != null) {
if (project != null) {
var currentConversation = ConversationsState.getInstance(project).getCurrentConversation();
if (currentConversation != null) {
var dateTimeStamp = currentConversation.getUpdatedOn()
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
var fileName = format("proxyai_conversation_%s.md", dateTimeStamp);
Expand All @@ -55,6 +63,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
ToolWindowManager.getInstance(project).getToolWindow("ProxyAI"));
toolWindow.hide();
}
}
} finally {
TelemetryAction.IDE_ACTION.createActionMessage()
.property("action", ActionType.OPEN_CONVERSATION_IN_EDITOR.name())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ee.carlrobert.codegpt.conversations;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
Expand All @@ -12,19 +11,23 @@
import java.util.Optional;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Service
@Service(Service.Level.PROJECT)
public final class ConversationService {

private static final Logger LOG = Logger.getInstance(ConversationService.class);

private final ConversationsState conversationState = ConversationsState.getInstance();
private final Project project;
private final ConversationsState conversationState;

private ConversationService() {
public ConversationService(Project project) {
this.project = project;
this.conversationState = ConversationsState.getInstance(project);
}

public static ConversationService getInstance() {
return ApplicationManager.getApplication().getService(ConversationService.class);
public static ConversationService getInstance(@NotNull Project project) {
return project.getService(ConversationService.class);
}

public List<Conversation> getSortedConversations() {
Expand Down Expand Up @@ -75,11 +78,8 @@ public void saveConversation(Conversation conversation) {
conversationState.setCurrentConversation(conversation);
}

public Conversation startConversation(Project project) {
return startConversation(project != null ? project.getBasePath() : null);
}

private Conversation startConversation(String projectPath) {
public Conversation startConversation() {
var projectPath = project.getBasePath();
var conversation = createConversation();
conversation.setProjectPath(projectPath);
conversationState.setCurrentConversation(conversation);
Expand All @@ -102,7 +102,7 @@ public void deleteSelectedConversation() {
nextConversation = getNextConversation();
}

var currentConversation = ConversationsState.getCurrentConversation();
var currentConversation = getCurrentConversation();
if (currentConversation != null) {
deleteConversation(currentConversation);
nextConversation.ifPresent(conversationState::setCurrentConversation);
Expand All @@ -116,6 +116,10 @@ public void discardTokenLimits(Conversation conversation) {
saveConversation(conversation);
}

public @Nullable Conversation getCurrentConversation() {
return conversationState.getCurrentConversation();
}

public Optional<Conversation> getPreviousConversation() {
return tryGetNextOrPreviousConversation(true);
}
Expand All @@ -125,7 +129,7 @@ public Optional<Conversation> getNextConversation() {
}

private Optional<Conversation> tryGetNextOrPreviousConversation(boolean isPrevious) {
var currentConversation = ConversationsState.getCurrentConversation();
var currentConversation = getCurrentConversation();
if (currentConversation != null) {
var sortedConversations = getSortedConversations();
for (int i = 0; i < sortedConversations.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package ee.carlrobert.codegpt.conversations;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.intellij.util.xmlb.annotations.OptionTag;
import ee.carlrobert.codegpt.conversations.converter.ConversationConverter;
Expand All @@ -16,9 +17,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Service(Service.Level.PROJECT)
@State(
name = "ee.carlrobert.codegpt.state.conversations.ConversationsState",
storages = @Storage("ChatGPTConversations_170.xml"))
name = "ee.carlrobert.codegpt.conversations.ConversationsState",
storages = @Storage("proxyai_conversations.xml"))
public class ConversationsState implements PersistentStateComponent<ConversationsState> {

@Deprecated
Expand All @@ -33,8 +35,8 @@ public class ConversationsState implements PersistentStateComponent<Conversation

public boolean discardAllTokenLimits;

public static ConversationsState getInstance() {
return ApplicationManager.getApplication().getService(ConversationsState.class);
public static ConversationsState getInstance(@NotNull Project project) {
return project.getService(ConversationsState.class);
}

@Nullable
Expand Down Expand Up @@ -65,7 +67,8 @@ public void setCurrentConversation(@Nullable Conversation conversation) {
this.currentConversation = conversation;
}

public static @Nullable Conversation getCurrentConversation() {
return getInstance().currentConversation;
@Nullable
public Conversation getCurrentConversation() {
return currentConversation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void sendMessage(Message message, ConversationType conversationType) {
.getState()
.getChatActions()
.getStartInNewWindow();
if (startInNewWindow || ConversationsState.getCurrentConversation() == null) {
if (startInNewWindow || ConversationsState.getInstance(project).getCurrentConversation() == null) {
createNewTabPanel().sendMessage(message, conversationType);
return;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public ChatToolWindowTabPanel createNewTabPanel() {
.map(item -> {
var panel = new ChatToolWindowTabPanel(
project,
ConversationService.getInstance().startConversation(project));
ConversationService.getInstance(project).startConversation());
item.addNewTab(panel);
return panel;
})
Expand Down Expand Up @@ -117,7 +117,7 @@ public void resetAll() {
tabbedPane.clearAll();
tabbedPane.addNewTab(new ChatToolWindowTabPanel(
project,
ConversationService.getInstance().startConversation(project)));
ConversationService.getInstance(project).startConversation()));
});
}

Expand All @@ -142,4 +142,4 @@ private Optional<Content> tryFindFirstChatTabContent() {
public void clearAllTags() {
tryFindActiveChatTabPanel().ifPresent(ChatToolWindowTabPanel::clearAllTags);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ChatToolWindowPanel(
upgradePlanLink.setVisible(false);

var tabPanel = new ChatToolWindowTabPanel(project, getConversation());
tabbedPane = new ChatToolWindowTabbedPane(parentDisposable);
tabbedPane = new ChatToolWindowTabbedPane(project, parentDisposable);
tabbedPane.addNewTab(tabPanel);

initToolWindowPanel(project);
Expand All @@ -72,9 +72,9 @@ public ChatToolWindowPanel(
}

private Conversation getConversation() {
var conversation = ConversationsState.getCurrentConversation();
var conversation = ConversationsState.getInstance(project).getCurrentConversation();
if (conversation == null) {
return ConversationService.getInstance().startConversation(project);
return ConversationService.getInstance(project).startConversation();
}
return conversation;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ private void initToolWindowPanel(Project project) {
Runnable onAddNewTab = () -> {
tabbedPane.addNewTab(new ChatToolWindowTabPanel(
project,
ConversationService.getInstance().startConversation(project)));
ConversationService.getInstance(project).startConversation()));
repaint();
revalidate();
};
Expand Down Expand Up @@ -224,4 +224,4 @@ private PersonaPromptDetailsState getSelectedPersona() {
.getSelectedPersona();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ChatToolWindowTabPanel(@NotNull Project project, @NotNull Conversation co
this.project = project;
this.conversation = conversation;
this.chatSession = new ChatSession();
conversationService = ConversationService.getInstance();
conversationService = ConversationService.getInstance(project);
toolWindowScrollablePanel = new ChatToolWindowScrollablePanel();
tagManager = new TagManager();
this.psiStructureRepository = new PsiStructureRepository(
Expand Down Expand Up @@ -220,6 +220,7 @@ private List<Conversation> getHistory(List<? extends TagDetails> tags) {
.map(it -> {
if (it instanceof HistoryTagDetails tagDetails) {
return ConversationTagProcessor.Companion.getConversation(
project,
tagDetails.getConversationId());
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class ChatToolWindowTabbedPane extends JBTabbedPane {

private final Project project;
private final Map<String, ChatToolWindowTabPanel> activeTabMapping = new TreeMap<>(
(o1, o2) -> {
String nums1 = o1.replaceAll("\\D", "");
Expand All @@ -48,7 +49,8 @@ public class ChatToolWindowTabbedPane extends JBTabbedPane {
});
private final Disposable parentDisposable;

public ChatToolWindowTabbedPane(Disposable parentDisposable) {
public ChatToolWindowTabbedPane(Project project, Disposable parentDisposable) {
this.project = project;
this.parentDisposable = parentDisposable;
setTabComponentInsets(null);
setComponentPopupMenu(new TabPopupMenu());
Expand Down Expand Up @@ -180,7 +182,7 @@ private void refreshTabState() {
if (toolWindowPanel != null) {
var conversation = toolWindowPanel.getConversation();
if (conversation != null) {
ConversationsState.getInstance().setCurrentConversation(conversation);
ConversationsState.getInstance(project).setCurrentConversation(conversation);
}
}
}
Expand All @@ -192,7 +194,7 @@ public void resetCurrentlyActiveTabPanel(Project project) {
removeTabAt(getSelectedIndex());
addNewTab(new ChatToolWindowTabPanel(
project,
ConversationService.getInstance().startConversation(project)));
ConversationService.getInstance(project).startConversation()));
repaint();
revalidate();
});
Expand Down
Loading