-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added(agent):
chat/import
to migrate historical chats to agent stor…
…age for webview (#2108) Adds a migration to import local chat history into the agent to be used in the webview. Tie in for sourcegraph/cody#5304 ## Test plan Tested manually. Need to add a unit test. --------- Co-authored-by: Mikołaj Kondratek <mik.kondratek@gmail.com>
- Loading branch information
1 parent
ea5155f
commit 2f5acfd
Showing
6 changed files
with
208 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/kotlin/com/sourcegraph/cody/config/migration/ChatHistoryMigration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.sourcegraph.cody.config.migration | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.sourcegraph.cody.agent.CodyAgentService | ||
import com.sourcegraph.cody.agent.protocol_generated.Chat_ImportParams | ||
import com.sourcegraph.cody.agent.protocol_generated.SerializedChatInteraction | ||
import com.sourcegraph.cody.agent.protocol_generated.SerializedChatMessage | ||
import com.sourcegraph.cody.agent.protocol_generated.SerializedChatTranscript | ||
import com.sourcegraph.cody.config.CodyAccount | ||
import com.sourcegraph.cody.config.CodyAuthenticationManager | ||
import com.sourcegraph.cody.history.HistoryService | ||
import com.sourcegraph.cody.history.state.ChatState | ||
import com.sourcegraph.cody.history.state.MessageState | ||
|
||
// Copies chat history from locally stored jetbrains state to the cody agent | ||
// so historical chats can be viewed in the cody webview | ||
object ChatHistoryMigration { | ||
fun migrate(project: Project) { | ||
CodyAgentService.withAgent(project) { agent -> | ||
val chats = | ||
CodyAuthenticationManager.getInstance(project).getAccounts().associateWith { account -> | ||
(HistoryService.getInstance(project).getChatHistoryFor(account.id) ?: listOf()) | ||
} | ||
val history = toChatInput(chats) | ||
|
||
agent.server.chat_import(Chat_ImportParams(history = history, merge = true)).get() | ||
} | ||
} | ||
|
||
fun toChatInput( | ||
chats: Map<CodyAccount, List<ChatState>> | ||
): Map<String, Map<String, SerializedChatTranscript>> { | ||
return chats | ||
.map { (account, chats) -> | ||
val serializedChats = chats.mapNotNull(::toSerializedChatTranscript) | ||
val byId = serializedChats.associateBy { it.id } | ||
|
||
"${account.server.url}-${account.name}" to byId | ||
} | ||
.toMap() | ||
} | ||
|
||
private fun toSerializedChatTranscript(chat: ChatState): SerializedChatTranscript? { | ||
return SerializedChatTranscript( | ||
id = chat.internalId ?: return null, | ||
lastInteractionTimestamp = chat.updatedAt ?: return null, | ||
interactions = toSerializedInteractions(chat.messages, chat.llm?.model), | ||
) | ||
} | ||
} | ||
|
||
private fun toSerializedInteractions( | ||
messages: List<MessageState>, | ||
model: String? | ||
): List<SerializedChatInteraction> { | ||
fun toChatMessage(message: MessageState): SerializedChatMessage? { | ||
return SerializedChatMessage( | ||
text = message.text ?: return null, | ||
model = model, | ||
speaker = | ||
when (message.speaker) { | ||
MessageState.SpeakerState.HUMAN -> SerializedChatMessage.SpeakerEnum.Human | ||
MessageState.SpeakerState.ASSISTANT -> SerializedChatMessage.SpeakerEnum.Assistant | ||
null -> return null | ||
}, | ||
) | ||
} | ||
|
||
fun toInteraction(pair: List<SerializedChatMessage?>): SerializedChatInteraction? { | ||
val (human, assistant) = pair.getOrNull(0) to pair.getOrNull(1) | ||
if (human == null || | ||
human.speaker != SerializedChatMessage.SpeakerEnum.Human || | ||
assistant?.speaker != SerializedChatMessage.SpeakerEnum.Assistant) { | ||
return null | ||
} | ||
return SerializedChatInteraction(humanMessage = human, assistantMessage = assistant) | ||
} | ||
|
||
return messages.map(::toChatMessage).chunked(2).mapNotNull(::toInteraction) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters