Skip to content

Commit

Permalink
Update updatedAtTime only on sending a message instead of receiving…
Browse files Browse the repository at this point in the history
… it (fixes #1248) (#1278)

Fixes #1248.

## Test plan

See the issue for specific steps.

1. Having two chats in the Chat History panel.
2. Restart IDE.
3. Go to Chat History panel and restore the older one.
4. Go to Chat History

Expected:
The restored chat remains as the last one in the list (it is not bumped
to the top).
  • Loading branch information
mkondratek committed Apr 5, 2024
1 parent 71e2269 commit 167f970
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/main/kotlin/com/sourcegraph/cody/CodyToolWindowContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,7 +32,8 @@ 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<AgentChatSession?> = AtomicReference(null)

Expand All @@ -54,7 +55,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)
Expand Down Expand Up @@ -99,7 +100,7 @@ class CodyToolWindowContent(private val project: Project) {
}
}

@RequiresEdt fun refreshHistoryTree() = historyTree.rebuildTree()
@RequiresEdt fun refreshChatHistoryPanel() = chatHistoryPanel.rebuildTree()

@RequiresEdt
fun refreshPanelsVisibility() {
Expand Down
25 changes: 14 additions & 11 deletions src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private constructor(

ChatMessage(speaker = parsed, message.text)
}

val newConnectionId =
restoreChatSession(agent, chatMessages, chatModelProviderFromState, state.internalId!!)
connectionId.getAndSet(newConnectionId)
Expand Down Expand Up @@ -295,18 +296,20 @@ private constructor(
})

chatSession.addMessageAtIndex(
ChatMessage(
Speaker.HUMAN,
commandId.displayName,
),
chatSession.messages.count())
message =
ChatMessage(
speaker = Speaker.HUMAN,
text = commandId.displayName,
),
index = chatSession.messages.count())
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AccountSettingChangeListener(project: Project) : ChangeListener(project) {
CodyToolWindowContent.executeOnInstanceIfNotDisposed(project) {
refreshPanelsVisibility()
refreshMyAccountTab()
refreshHistoryTree()
refreshChatHistoryPanel()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -140,7 +140,9 @@ class HistoryTree(
}
} 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()
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ class HistoryService(private val project: Project) :
@Synchronized
fun updateChatMessages(internalId: String, chatMessages: List<ChatMessage>) {
val found = getOrCreateChat(internalId)
found.messages = chatMessages.map(::convertToMessageState).toMutableList()
if (chatMessages.lastOrNull()?.speaker == Speaker.HUMAN) {
if (found.messages.size < chatMessages.size) {
found.setUpdatedTimeAt(LocalDateTime.now())
}

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) } }
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/CodyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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=\
<html>\
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.<br><br>\
(Already upgraded to Pro? Restart your IDE for changes to take effect)\
</html>
Expand Down

0 comments on commit 167f970

Please sign in to comment.