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)
  • Loading branch information
mkondratek committed Apr 3, 2024
1 parent 5b1ac69 commit 70db6d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AgentChatSession?> = AtomicReference(null)

Expand Down
35 changes: 21 additions & 14 deletions src/main/kotlin/com/sourcegraph/cody/chat/AgentChatSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private constructor(
text,
displayText,
)
addMessageAtIndex(humanMessage, index = messages.count())
addMessageAtIndex(humanMessage, index = messages.count(), shouldUpdateTime = true)

val responsePlaceholder =
ChatMessage(
Expand Down Expand Up @@ -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
Expand All @@ -229,7 +233,7 @@ private constructor(
}

chatPanel.addOrUpdateMessage(message, index)
HistoryService.getInstance(project).updateChatMessages(internalId, messages)
HistoryService.getInstance(project).updateChatMessages(internalId, messages, shouldUpdateTime)
}

@RequiresEdt
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ class HistoryService(private val project: Project) :
}

@Synchronized
fun updateChatMessages(internalId: String, chatMessages: List<ChatMessage>) {
fun updateChatMessages(
internalId: String,
chatMessages: List<ChatMessage>,
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) } }
Expand Down

0 comments on commit 70db6d8

Please sign in to comment.