From 5b1ac69706689f51ff8f4f534b87691ba3b64f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 3 Apr 2024 13:51:46 +0200 Subject: [PATCH 1/6] Rename HistoryTree.kt to ChatHistoryPanel.kt --- .../kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt | 8 ++++---- .../com/sourcegraph/cody/config/SettingsMigration.kt | 2 +- .../config/notification/AccountSettingChangeListener.kt | 2 +- .../cody/history/{HistoryTree.kt => ChatHistoryPanel.kt} | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) rename src/main/kotlin/com/sourcegraph/cody/history/{HistoryTree.kt => ChatHistoryPanel.kt} (99%) diff --git a/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt b/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt index f1ecb94f3..aee874363 100644 --- a/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt +++ b/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt @@ -17,8 +17,8 @@ import com.sourcegraph.cody.commands.ui.CommandsTabPanel import com.sourcegraph.cody.config.CodyAccount import com.sourcegraph.cody.config.CodyApplicationSettings import com.sourcegraph.cody.config.CodyAuthenticationManager +import com.sourcegraph.cody.history.ChatHistoryPanel import com.sourcegraph.cody.history.HistoryService -import com.sourcegraph.cody.history.HistoryTree import com.sourcegraph.cody.history.state.ChatState import java.awt.CardLayout import javax.swing.JComponent @@ -32,7 +32,7 @@ class CodyToolWindowContent(private val project: Project) { private var codyOnboardingGuidancePanel: CodyOnboardingGuidancePanel? = null private val signInWithSourcegraphPanel = SignInWithSourcegraphPanel(project) - private val historyTree = HistoryTree(project, ::selectChat, ::removeChat, ::removeAllChats) + private val chatHistoryPanel = ChatHistoryPanel(project, ::selectChat, ::removeChat, ::removeAllChats) private val tabbedPane = TabbedPaneWrapper(CodyAgentService.getInstance(project)) private val currentChatSession: AtomicReference = AtomicReference(null) @@ -54,7 +54,7 @@ class CodyToolWindowContent(private val project: Project) { init { tabbedPane.insertSimpleTab("Chat", chatContainerPanel, CHAT_TAB_INDEX) - tabbedPane.insertSimpleTab("Chat History", historyTree, HISTORY_TAB_INDEX) + tabbedPane.insertSimpleTab("Chat History", chatHistoryPanel, HISTORY_TAB_INDEX) tabbedPane.insertSimpleTab("Commands", commandsPanel, COMMANDS_TAB_INDEX) allContentPanel.add(tabbedPane.component, MAIN_PANEL, CHAT_PANEL_INDEX) @@ -99,7 +99,7 @@ class CodyToolWindowContent(private val project: Project) { } } - @RequiresEdt fun refreshHistoryTree() = historyTree.rebuildTree() + @RequiresEdt fun refreshChatHistoryPanel() = chatHistoryPanel.rebuildTree() @RequiresEdt fun refreshPanelsVisibility() { diff --git a/src/main/kotlin/com/sourcegraph/cody/config/SettingsMigration.kt b/src/main/kotlin/com/sourcegraph/cody/config/SettingsMigration.kt index 21c60fe43..f17a1f7ef 100644 --- a/src/main/kotlin/com/sourcegraph/cody/config/SettingsMigration.kt +++ b/src/main/kotlin/com/sourcegraph/cody/config/SettingsMigration.kt @@ -62,7 +62,7 @@ class SettingsMigration : Activity { .filter { it.accountId == null } .forEach { it.accountId = activeAccountId } // required because this activity is executed later than tool window creation - CodyToolWindowContent.executeOnInstanceIfNotDisposed(project) { refreshHistoryTree() } + CodyToolWindowContent.executeOnInstanceIfNotDisposed(project) { refreshChatHistoryPanel() } } private fun refreshAccountsIds(project: Project) { diff --git a/src/main/kotlin/com/sourcegraph/cody/config/notification/AccountSettingChangeListener.kt b/src/main/kotlin/com/sourcegraph/cody/config/notification/AccountSettingChangeListener.kt index b35cd5982..949a87ad2 100644 --- a/src/main/kotlin/com/sourcegraph/cody/config/notification/AccountSettingChangeListener.kt +++ b/src/main/kotlin/com/sourcegraph/cody/config/notification/AccountSettingChangeListener.kt @@ -34,7 +34,7 @@ class AccountSettingChangeListener(project: Project) : ChangeListener(project) { CodyToolWindowContent.executeOnInstanceIfNotDisposed(project) { refreshPanelsVisibility() refreshMyAccountTab() - refreshHistoryTree() + refreshChatHistoryPanel() } } diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryTree.kt b/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt similarity index 99% rename from src/main/kotlin/com/sourcegraph/cody/history/HistoryTree.kt rename to src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt index 2c492aa36..4e8788843 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryTree.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt @@ -41,7 +41,7 @@ import javax.swing.tree.DefaultTreeModel import javax.swing.tree.TreePath import javax.swing.tree.TreeSelectionModel -class HistoryTree( +class ChatHistoryPanel( private val project: Project, private val onSelect: (ChatState) -> Unit, private val onRemove: (ChatState) -> Unit, From 70db6d81c75a01269af191f766d4e93a4254289f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 3 Apr 2024 14:15:13 +0200 Subject: [PATCH 2/6] Update `updatedAtTime` only on sending a message instead of receiving it (fixes #1248) --- .../sourcegraph/cody/CodyToolWindowContent.kt | 3 +- .../sourcegraph/cody/chat/AgentChatSession.kt | 35 +++++++++++-------- .../cody/history/HistoryService.kt | 8 +++-- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt b/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt index aee874363..eb66498e7 100644 --- a/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt +++ b/src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt @@ -32,7 +32,8 @@ class CodyToolWindowContent(private val project: Project) { private var codyOnboardingGuidancePanel: CodyOnboardingGuidancePanel? = null private val signInWithSourcegraphPanel = SignInWithSourcegraphPanel(project) - private val chatHistoryPanel = ChatHistoryPanel(project, ::selectChat, ::removeChat, ::removeAllChats) + private val chatHistoryPanel = + ChatHistoryPanel(project, ::selectChat, ::removeChat, ::removeAllChats) private val tabbedPane = TabbedPaneWrapper(CodyAgentService.getInstance(project)) private val currentChatSession: AtomicReference = AtomicReference(null) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt index 3b60661c3..852192703 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt @@ -82,7 +82,7 @@ private constructor( text, displayText, ) - addMessageAtIndex(humanMessage, index = messages.count()) + addMessageAtIndex(humanMessage, index = messages.count(), shouldUpdateTime = true) val responsePlaceholder = ChatMessage( @@ -220,7 +220,11 @@ private constructor( } @RequiresEdt - private fun addMessageAtIndex(message: ChatMessage, index: Int) { + private fun addMessageAtIndex( + message: ChatMessage, + index: Int, + shouldUpdateTime: Boolean = false + ) { val messageToUpdate = messages.getOrNull(index) if (messageToUpdate != null) { messages[index] = message @@ -229,7 +233,7 @@ private constructor( } chatPanel.addOrUpdateMessage(message, index) - HistoryService.getInstance(project).updateChatMessages(internalId, messages) + HistoryService.getInstance(project).updateChatMessages(internalId, messages, shouldUpdateTime) } @RequiresEdt @@ -295,18 +299,21 @@ private constructor( }) chatSession.addMessageAtIndex( - ChatMessage( - Speaker.HUMAN, - commandId.displayName, - ), - chatSession.messages.count()) + message = + ChatMessage( + speaker = Speaker.HUMAN, + text = commandId.displayName, + ), + index = chatSession.messages.count(), + shouldUpdateTime = true) chatSession.addMessageAtIndex( - ChatMessage( - Speaker.ASSISTANT, - text = "", - displayText = "", - ), - chatSession.messages.count()) + message = + ChatMessage( + Speaker.ASSISTANT, + text = "", + displayText = "", + ), + index = chatSession.messages.count()) AgentChatSessionService.getInstance(project).addSession(chatSession) return chatSession } diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index 4e339bcbd..3d963f7ea 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -33,10 +33,14 @@ class HistoryService(private val project: Project) : } @Synchronized - fun updateChatMessages(internalId: String, chatMessages: List) { + fun updateChatMessages( + internalId: String, + chatMessages: List, + shouldUpdateTime: Boolean + ) { val found = getOrCreateChat(internalId) found.messages = chatMessages.map(::convertToMessageState).toMutableList() - if (chatMessages.lastOrNull()?.speaker == Speaker.HUMAN) { + if (shouldUpdateTime) { found.setUpdatedTimeAt(LocalDateTime.now()) } synchronized(listeners) { listeners.forEach { it(found) } } From c6b8126255057c7a771760e561c852de4700d688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 3 Apr 2024 18:36:43 +0200 Subject: [PATCH 3/6] Fixes --- .../sourcegraph/cody/chat/AgentChatSession.kt | 18 +++++++++--------- .../sourcegraph/cody/history/HistoryService.kt | 10 +++------- src/main/resources/CodyBundle.properties | 2 +- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt index 852192703..36a022e90 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt @@ -82,7 +82,7 @@ private constructor( text, displayText, ) - addMessageAtIndex(humanMessage, index = messages.count(), shouldUpdateTime = true) + addMessageAtIndex(humanMessage, index = messages.count()) val responsePlaceholder = ChatMessage( @@ -220,11 +220,7 @@ private constructor( } @RequiresEdt - private fun addMessageAtIndex( - message: ChatMessage, - index: Int, - shouldUpdateTime: Boolean = false - ) { + private fun addMessageAtIndex(message: ChatMessage, index: Int) { val messageToUpdate = messages.getOrNull(index) if (messageToUpdate != null) { messages[index] = message @@ -233,7 +229,7 @@ private constructor( } chatPanel.addOrUpdateMessage(message, index) - HistoryService.getInstance(project).updateChatMessages(internalId, messages, shouldUpdateTime) + HistoryService.getInstance(project).updateChatMessages(internalId, messages) } @RequiresEdt @@ -257,6 +253,11 @@ private constructor( ChatMessage(speaker = parsed, message.text) } + this.messages.addAll(chatMessages) + chatMessages.forEachIndexed { index, chatMessage -> + chatPanel.addOrUpdateMessage(chatMessage, index) + } + val newConnectionId = restoreChatSession(agent, chatMessages, chatModelProviderFromState, state.internalId!!) connectionId.getAndSet(newConnectionId) @@ -304,8 +305,7 @@ private constructor( speaker = Speaker.HUMAN, text = commandId.displayName, ), - index = chatSession.messages.count(), - shouldUpdateTime = true) + index = chatSession.messages.count()) chatSession.addMessageAtIndex( message = ChatMessage( diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index 3d963f7ea..4f441c359 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -33,16 +33,12 @@ class HistoryService(private val project: Project) : } @Synchronized - fun updateChatMessages( - internalId: String, - chatMessages: List, - shouldUpdateTime: Boolean - ) { + fun updateChatMessages(internalId: String, chatMessages: List) { val found = getOrCreateChat(internalId) - found.messages = chatMessages.map(::convertToMessageState).toMutableList() - if (shouldUpdateTime) { + if (found.messages.size < chatMessages.size) { found.setUpdatedTimeAt(LocalDateTime.now()) } + found.messages = chatMessages.map(::convertToMessageState).toMutableList() synchronized(listeners) { listeners.forEach { it(found) } } } diff --git a/src/main/resources/CodyBundle.properties b/src/main/resources/CodyBundle.properties index 3a4dc714a..211905dfd 100644 --- a/src/main/resources/CodyBundle.properties +++ b/src/main/resources/CodyBundle.properties @@ -70,7 +70,7 @@ UpgradeToCodyProNotification.title.upgrade=You've used up your autocompletes for UpgradeToCodyProNotification.title.explain=Thank you for using Cody so heavily today! UpgradeToCodyProNotification.content.upgrade=\ \ - You''ve used all autocomplete suggestions for the month. \ + You've used all autocomplete suggestions for the month. \ Upgrade to Cody Pro for unlimited autocompletes, chats, and commands.

\ (Already upgraded to Pro? Restart your IDE for changes to take effect)\ From 65ac961af3e0516bbdbd56b6965540320e154ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 3 Apr 2024 23:03:58 +0200 Subject: [PATCH 4/6] Fixes bug in com.sourcegraph.cody.history.ChatHistoryPanel.updatePresentation --- .../kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt b/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt index 4e8788843..c75dc88e2 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/ChatHistoryPanel.kt @@ -140,7 +140,9 @@ class ChatHistoryPanel( } } else { val currentPeriodText = DurationGroupFormatter.format(chat.getUpdatedTimeAt()) - val currentPeriod = root.periods().find { it.periodText == currentPeriodText } ?: return + val currentPeriod = + root.periods().find { it.periodText == currentPeriodText } + ?: PeriodNode(currentPeriodText) val leafWithChangedPeriod = root .periods() From 6a5d2366c2e27682b91301c6703407aec5d4faf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Thu, 4 Apr 2024 11:10:50 +0200 Subject: [PATCH 5/6] Fixes --- src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt | 4 ---- .../kotlin/com/sourcegraph/cody/history/HistoryService.kt | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt index 36a022e90..3a5e59dab 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt @@ -253,10 +253,6 @@ private constructor( ChatMessage(speaker = parsed, message.text) } - this.messages.addAll(chatMessages) - chatMessages.forEachIndexed { index, chatMessage -> - chatPanel.addOrUpdateMessage(chatMessage, index) - } val newConnectionId = restoreChatSession(agent, chatMessages, chatModelProviderFromState, state.internalId!!) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index 4f441c359..7e4c766b7 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -37,9 +37,9 @@ class HistoryService(private val project: Project) : val found = getOrCreateChat(internalId) if (found.messages.size < chatMessages.size) { found.setUpdatedTimeAt(LocalDateTime.now()) + found.messages = chatMessages.map(::convertToMessageState).toMutableList() + synchronized(listeners) { listeners.forEach { it(found) } } } - found.messages = chatMessages.map(::convertToMessageState).toMutableList() - synchronized(listeners) { listeners.forEach { it(found) } } } @Synchronized From a912047c4024fd61be43ce0bdb8806ad18c5ccf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Thu, 4 Apr 2024 11:40:15 +0200 Subject: [PATCH 6/6] Fixes --- .../com/sourcegraph/cody/history/HistoryService.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index 7e4c766b7..75a60d830 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -37,9 +37,17 @@ class HistoryService(private val project: Project) : val found = getOrCreateChat(internalId) if (found.messages.size < chatMessages.size) { found.setUpdatedTimeAt(LocalDateTime.now()) - found.messages = chatMessages.map(::convertToMessageState).toMutableList() - synchronized(listeners) { listeners.forEach { it(found) } } } + + chatMessages.map(::convertToMessageState).forEachIndexed { index, messageState -> + val messageToUpdate = found.messages.getOrNull(index) + if (messageToUpdate != null) { + found.messages[index] = messageState + } else { + found.messages.add(messageState) + } + } + synchronized(listeners) { listeners.forEach { it(found) } } } @Synchronized