Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EDT violations and chat refresh #460

Merged
merged 5 commits into from
Feb 1, 2024
Merged
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 @@ -131,6 +131,7 @@ class CodyToolWindowContent(private val project: Project) {
allContentLayout.show(allContentPanel, MAIN_PANEL)
}

@RequiresEdt
private fun selectHistory(state: ChatState) {
val session = AgentChatSessionService.getInstance(project).getOrCreateFromState(state)
switchToChatSession(session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sourcegraph.cody.chat
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.util.concurrency.annotations.RequiresEdt
import com.sourcegraph.cody.agent.CodyAgent
import com.sourcegraph.cody.history.state.ChatState
import java.util.concurrent.ConcurrentLinkedQueue
Expand All @@ -24,6 +25,7 @@ class AgentChatSessionService(private val project: Project) {
return chatSessions.remove(session)
}

@RequiresEdt
fun getOrCreateFromState(state: ChatState): AgentChatSession {
val session = chatSessions.find { it.getInternalId() == state.internalId }
return session ?: AgentChatSession.createFromState(project, state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sourcegraph.cody.commands.ui
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -33,8 +34,10 @@ class CommandsContextMenu {
CodyEditorFactoryListener.Util.informAgentAboutEditorChange(
it, hasFileChanged = false) {
CodyToolWindowContent.executeOnInstanceIfNotDisposed(project) {
switchToChatSession(
AgentChatSession.createFromCommand(project, commandId))
ApplicationManager.getApplication().invokeLater {
switchToChatSession(
AgentChatSession.createFromCommand(project, commandId))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sourcegraph.cody.commands.ui

import com.intellij.ide.ui.laf.darcula.ui.DarculaButtonUI
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBPanelWithEmptyText
Expand All @@ -26,7 +27,7 @@ class CommandsTabPanel(
private fun executeCommandWithContext(commandId: CommandId) {
FileEditorManager.getInstance(project).selectedTextEditor?.let {
CodyEditorFactoryListener.Util.informAgentAboutEditorChange(it, hasFileChanged = false) {
executeCommand(commandId)
ApplicationManager.getApplication().invokeLater { executeCommand(commandId) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class HistoryService : SimplePersistentStateComponent<HistoryState>(HistoryState
fun update(internalId: String, chatMessages: List<ChatMessage>) {
val found = getChatOrCreate(internalId)
found.messages = chatMessages.map(::convertToMessageState).toMutableList()
found.setUpdatedTimeAt(LocalDateTime.now())
if (chatMessages.lastOrNull()?.speaker == Speaker.HUMAN) {
found.setUpdatedTimeAt(LocalDateTime.now())
}
Comment on lines +24 to +26
Copy link
Contributor

@mkondratek mkondratek Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the flickering list when +2 responses are being generated.

synchronized(listeners) { listeners.forEach { it(found) } }
}

Expand Down
41 changes: 23 additions & 18 deletions src/main/kotlin/com/sourcegraph/cody/history/HistoryTree.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,35 @@ class HistoryTree(
addChatToPeriodAndSort(period, chat)
}
} else {
val period =
val currentPeriodText = DurationGroupFormatter.format(chat.getUpdatedTimeAt())
val currentPeriod = root.periods().find { it.periodText == currentPeriodText }
val leafWithChangedPeriod =
root
.periods()
.filter { it.periodText != currentPeriodText }
.flatMap { it.leafs() }
.find { it.chat.internalId == chat.internalId }!!
.parent as PeriodNode
val periodText = DurationGroupFormatter.format(chat.getUpdatedTimeAt())
// Checking if chat needs to be moved to different period
if (period.periodText != periodText) {
val filter = period.leafs().filter { it.chat.internalId != chat.internalId }
if (filter.isEmpty()) {
model.removeNodeFromParent(period)
} else {
period.removeAllChildren()
for (child in filter) period.add(child)
.find { it.chat.internalId == chat.internalId }

if (leafWithChangedPeriod != null) {
val previousPeriod = leafWithChangedPeriod.parent as? PeriodNode
previousPeriod?.let { period ->
period.remove(leafWithChangedPeriod)
if (period.childCount == 0) period.removeFromParent()
model.reload(period)
}
currentPeriod?.let { period ->
addChatToPeriodAndSort(period, chat)
model.reload(period)
}
val per = root.periods().find { it.periodText == periodText }
addChatToPeriodAndSort(per!!, chat)
} else {
val sorted = period.leafs().sortedByDescending { it.chat.getUpdatedTimeAt() }
period.removeAllChildren()
for (child in sorted) period.add(child)
model.reload(period)
currentPeriod?.let { period ->
val sorted = period.leafs().sortedByDescending { it.chat.getUpdatedTimeAt() }
if (period.leafs() != sorted) {
period.removeAllChildren()
for (child in sorted) period.add(child)
model.reload(period)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.util.ui.UIUtil
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.config.CodyAccountManager
import com.sourcegraph.cody.config.CodyAuthenticationManager
Expand Down Expand Up @@ -75,18 +76,12 @@ class CodyAutocompleteStatusService : CodyAutocompleteStatusListener, Disposable
}

private fun updateCodyStatusBarIcons() {
val action = Runnable {
UIUtil.invokeLaterIfNeeded {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrel fix

val openProjects = ProjectManager.getInstance().openProjects
openProjects.forEach { project ->
project.takeIf { !it.isDisposed }?.let { CodyStatusBarWidget.update(it) }
}
}
val application = ApplicationManager.getApplication()
if (application.isDispatchThread) {
action.run()
} else {
application.invokeLater(action)
}
}

private fun getStatus(): CodyAutocompleteStatus {
Expand Down
Loading