From c4caa7fc1fd1cd42691a3708d1a58a05beb4b3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:17:16 +0100 Subject: [PATCH 01/12] Remove redundant field from HistoryService --- src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt | 3 +-- .../com/sourcegraph/cody/config/SettingsMigrationTest.kt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index 77cb0e088a..a6aefa989a 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -14,8 +14,7 @@ import com.sourcegraph.cody.history.state.LLMState @State(name = "ChatHistory", storages = [Storage("cody_history.xml")]) @Service(Service.Level.PROJECT) -class HistoryService(private val project: Project) : - SimplePersistentStateComponent(HistoryState()) { +class HistoryService : SimplePersistentStateComponent(HistoryState()) { @Synchronized fun getDefaultLlm(): LLMState? { diff --git a/src/test/kotlin/com/sourcegraph/cody/config/SettingsMigrationTest.kt b/src/test/kotlin/com/sourcegraph/cody/config/SettingsMigrationTest.kt index 8803bbab32..e5de39f790 100644 --- a/src/test/kotlin/com/sourcegraph/cody/config/SettingsMigrationTest.kt +++ b/src/test/kotlin/com/sourcegraph/cody/config/SettingsMigrationTest.kt @@ -182,7 +182,7 @@ class SettingsMigrationTest : BasePlatformTestCase() { ChatState("chat4")) } val project = myFixture.project - project.registerServiceInstance(HistoryService::class.java, HistoryService(project)) + project.registerServiceInstance(HistoryService::class.java, HistoryService()) HistoryService.getInstance(project) .loadState(HistoryState().also { it.copyFrom(originalHistory) }) From 32d52d3bb6b282a18f7d373c399bc2bfccaff71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:20:11 +0100 Subject: [PATCH 02/12] Remove redundant methods from HistoryService --- .../com/sourcegraph/cody/history/HistoryService.kt | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index a6aefa989a..f804493020 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -31,25 +31,12 @@ class HistoryService : SimplePersistentStateComponent(HistoryState getOrCreateActiveAccountEntry().defaultLlm = newDefaultLlm } - @Synchronized - fun remove(internalId: String?) { - getOrCreateActiveAccountEntry().chats.removeIf { it.internalId == internalId } - } - - @Synchronized - fun findActiveAccountChat(internalId: String): ChatState? = - getActiveAccountHistory()?.chats?.find { it.internalId == internalId } - @Synchronized fun getChatHistoryFor(accountId: String): List? = findEntry(accountId)?.chats private fun findEntry(accountId: String): AccountData? = state.accountData.find { it.accountId == accountId } - @Synchronized - fun getActiveAccountHistory(): AccountData? = - DeprecatedCodyAccountManager.getInstance().account?.let { findEntry(it.id) } - private fun getOrCreateActiveAccountEntry(): AccountData { val activeAccount = DeprecatedCodyAccountManager.getInstance().account From 678466ebffab57539ffe9c7cef5e428eb8bdbe50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:21:15 +0100 Subject: [PATCH 03/12] Remove redundant method --- .../sourcegraph/cody/auth/deprecated/DeprecatedCodyAccount.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/auth/deprecated/DeprecatedCodyAccount.kt b/src/main/kotlin/com/sourcegraph/cody/auth/deprecated/DeprecatedCodyAccount.kt index 16cea291a2..f9ee279bdd 100644 --- a/src/main/kotlin/com/sourcegraph/cody/auth/deprecated/DeprecatedCodyAccount.kt +++ b/src/main/kotlin/com/sourcegraph/cody/auth/deprecated/DeprecatedCodyAccount.kt @@ -20,8 +20,6 @@ data class DeprecatedCodyAccount( @Attribute("id") var id: String = generateId(), ) { - fun isDotcomAccount(): Boolean = server.url.lowercase().startsWith(ConfigUtil.DOTCOM_URL) - fun credentialAttributes(): CredentialAttributes = CredentialAttributes(generateServiceName("Sourcegraph", id)) From bb44089f18cbd41ee1221d005c85d034eb9e7400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:22:11 +0100 Subject: [PATCH 04/12] Update DeprecatedCodyAccountManager state --- .../kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt index b06d7db548..fc50462aa1 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt @@ -28,6 +28,8 @@ import com.sourcegraph.cody.agent.protocol_generated.Window_DidChangeContextPara import com.sourcegraph.cody.agent.protocol_generated.WorkspaceEditParams import com.sourcegraph.cody.auth.CodyAccount import com.sourcegraph.cody.auth.SourcegraphServerPath +import com.sourcegraph.cody.auth.deprecated.DeprecatedCodyAccount +import com.sourcegraph.cody.auth.deprecated.DeprecatedCodyAccountManager import com.sourcegraph.cody.edit.EditService import com.sourcegraph.cody.edit.lenses.LensesService import com.sourcegraph.cody.error.CodyConsole @@ -264,7 +266,11 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW } if (params.key == "cody.serverEndpoint") { val endpoint = params.value ?: return - CodyAccount.setActiveAccount(CodyAccount(SourcegraphServerPath(endpoint))) + val server = SourcegraphServerPath(endpoint) + CodyAccount.setActiveAccount(CodyAccount(server)) + DeprecatedCodyAccountManager.getInstance() + .setActiveAccount( + DeprecatedCodyAccount(name = "user", displayName = "User", server = server)) CodyStatusService.resetApplication(project) } } From a70f2376ca38a4823a19d32a09cc264dc2a1ce23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:22:47 +0100 Subject: [PATCH 05/12] Ignore missing account in DeprecatedCodyAccountManager --- .../com/sourcegraph/cody/history/HistoryService.kt | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index f804493020..eec8f13801 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -28,7 +28,7 @@ class HistoryService : SimplePersistentStateComponent(HistoryState fun setDefaultLlm(defaultLlm: LLMState) { val newDefaultLlm = LLMState() newDefaultLlm.copyFrom(defaultLlm) - getOrCreateActiveAccountEntry().defaultLlm = newDefaultLlm + getOrCreateActiveAccountData()?.defaultLlm = newDefaultLlm } @Synchronized @@ -37,11 +37,8 @@ class HistoryService : SimplePersistentStateComponent(HistoryState private fun findEntry(accountId: String): AccountData? = state.accountData.find { it.accountId == accountId } - private fun getOrCreateActiveAccountEntry(): AccountData { - val activeAccount = - DeprecatedCodyAccountManager.getInstance().account - ?: throw IllegalStateException("No active account") - + private fun getOrCreateActiveAccountData(): AccountData? { + val activeAccount = DeprecatedCodyAccountManager.getInstance().account ?: return null val existingEntry = findEntry(activeAccount.id) return existingEntry ?: AccountData(activeAccount.id).also { state.accountData += it } } From e05d0f52eabbb950efedb513cd8314a1f9b25972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:56:10 +0100 Subject: [PATCH 06/12] Remove redundant `chatModelFromState` --- src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt | 5 +---- .../kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index 3def642add..b78c00e5e9 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -27,7 +27,6 @@ class LlmDropdown( private val project: Project, private val onSetSelectedItem: (Model) -> Unit, val parentDialog: EditCommandPrompt?, - private val chatModelFromState: Model?, private val model: String? = null ) : ComboBox(MutableCollectionComboBoxModel()) { private var hasServerSentModels = project.service().get().serverSentModels @@ -83,9 +82,7 @@ class LlmDropdown( it.model.id == selectedFromChatState?.id } ?: models.firstOrNull() - // If the dropdown is already disabled, don't change it. It can happen - // in the case of the legacy commands (updateAfterFirstMessage happens before this call). - isEnabled = isEnabled && chatModelFromState == null + isEnabled = true isVisible = selectedItem != null setMaximumRowCount(15) diff --git a/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt b/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt index 60915922e6..2fac6793be 100644 --- a/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt +++ b/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt @@ -135,7 +135,6 @@ class EditCommandPrompt( project = project, onSetSelectedItem = { model = it.id }, this, - chatModelFromState = null, model = model) .apply { foreground = boldLabelColor() From cfaa97eded6dc1a240bcdbb611d96a7ba3df5703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 20:56:34 +0100 Subject: [PATCH 07/12] Move to a dedicated default llm storage --- .../sourcegraph/cody/agent/CodyAgentClient.kt | 5 ---- .../sourcegraph/cody/chat/ui/LlmDropdown.kt | 18 ++++++------ .../cody/config/DefaultLlmStorage.kt | 28 +++++++++++++++++++ .../cody/history/HistoryService.kt | 23 --------------- 4 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt diff --git a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt index fc50462aa1..9fa01d125d 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt @@ -28,8 +28,6 @@ import com.sourcegraph.cody.agent.protocol_generated.Window_DidChangeContextPara import com.sourcegraph.cody.agent.protocol_generated.WorkspaceEditParams import com.sourcegraph.cody.auth.CodyAccount import com.sourcegraph.cody.auth.SourcegraphServerPath -import com.sourcegraph.cody.auth.deprecated.DeprecatedCodyAccount -import com.sourcegraph.cody.auth.deprecated.DeprecatedCodyAccountManager import com.sourcegraph.cody.edit.EditService import com.sourcegraph.cody.edit.lenses.LensesService import com.sourcegraph.cody.error.CodyConsole @@ -268,9 +266,6 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW val endpoint = params.value ?: return val server = SourcegraphServerPath(endpoint) CodyAccount.setActiveAccount(CodyAccount(server)) - DeprecatedCodyAccountManager.getInstance() - .setActiveAccount( - DeprecatedCodyAccount(name = "user", displayName = "User", server = server)) CodyStatusService.resetApplication(project) } } diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index b78c00e5e9..71cbfec681 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -15,9 +15,9 @@ import com.sourcegraph.cody.agent.protocol_extensions.isDeprecated import com.sourcegraph.cody.agent.protocol_generated.Chat_ModelsParams import com.sourcegraph.cody.agent.protocol_generated.Model import com.sourcegraph.cody.agent.protocol_generated.ModelAvailabilityStatus +import com.sourcegraph.cody.auth.CodyAccount +import com.sourcegraph.cody.config.DefaultLlmStorage import com.sourcegraph.cody.edit.EditCommandPrompt -import com.sourcegraph.cody.history.HistoryService -import com.sourcegraph.cody.history.state.LLMState import com.sourcegraph.cody.ui.LlmComboBoxRenderer import com.sourcegraph.common.BrowserOpener import java.util.concurrent.TimeUnit @@ -72,15 +72,12 @@ class LlmDropdown( val availableModels = models.map { it }.filterNot { it.model.isDeprecated() } availableModels.sortedBy { it.model.isCodyProOnly() }.forEach { addItem(it) } - val selectedFromChatState = chatModelFromState - val selectedFromHistory = HistoryService.getInstance(project).getDefaultLlm() + val defaultLlm = + DefaultLlmStorage.getInstance(project).get(CodyAccount.getActiveAccount()?.server) selectedItem = - availableModels.find { - it.model.id == model || - it.model.id == selectedFromHistory?.model || - it.model.id == selectedFromChatState?.id - } ?: models.firstOrNull() + availableModels.find { it.model.id == model || it.model.id == defaultLlm?.id } + ?: models.firstOrNull() isEnabled = true isVisible = selectedItem != null @@ -103,7 +100,8 @@ class LlmDropdown( return } - HistoryService.getInstance(project).setDefaultLlm(LLMState.fromChatModel(modelProvider.model)) + DefaultLlmStorage.getInstance(project) + .store(CodyAccount.getActiveAccount()?.server, modelProvider.model) super.setSelectedItem(anObject) onSetSelectedItem(modelProvider.model) diff --git a/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt b/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt new file mode 100644 index 0000000000..18575a383d --- /dev/null +++ b/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt @@ -0,0 +1,28 @@ +package com.sourcegraph.cody.config + +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.service +import com.intellij.openapi.project.Project +import com.sourcegraph.cody.agent.protocol_generated.Model +import com.sourcegraph.cody.auth.SourcegraphServerPath + +@Service(Service.Level.PROJECT) +class DefaultLlmStorage { + + private val storage = HashMap() + + fun store(server: SourcegraphServerPath?, model: Model) { + server ?: return + storage[server] = model + } + + fun get(server: SourcegraphServerPath?): Model? { + server ?: return null + return storage[server] + } + + companion object { + @JvmStatic + fun getInstance(project: Project): DefaultLlmStorage = project.service() + } +} diff --git a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt index eec8f13801..116de3d195 100644 --- a/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt +++ b/src/main/kotlin/com/sourcegraph/cody/history/HistoryService.kt @@ -6,43 +6,20 @@ import com.intellij.openapi.components.State import com.intellij.openapi.components.Storage import com.intellij.openapi.components.service import com.intellij.openapi.project.Project -import com.sourcegraph.cody.auth.deprecated.DeprecatedCodyAccountManager import com.sourcegraph.cody.history.state.AccountData import com.sourcegraph.cody.history.state.ChatState import com.sourcegraph.cody.history.state.HistoryState -import com.sourcegraph.cody.history.state.LLMState @State(name = "ChatHistory", storages = [Storage("cody_history.xml")]) @Service(Service.Level.PROJECT) class HistoryService : SimplePersistentStateComponent(HistoryState()) { - @Synchronized - fun getDefaultLlm(): LLMState? { - val account = DeprecatedCodyAccountManager.getInstance().account - val llm = account?.let { findEntry(it.id) }?.defaultLlm - if (llm == null) return null - return LLMState().also { it.copyFrom(llm) } - } - - @Synchronized - fun setDefaultLlm(defaultLlm: LLMState) { - val newDefaultLlm = LLMState() - newDefaultLlm.copyFrom(defaultLlm) - getOrCreateActiveAccountData()?.defaultLlm = newDefaultLlm - } - @Synchronized fun getChatHistoryFor(accountId: String): List? = findEntry(accountId)?.chats private fun findEntry(accountId: String): AccountData? = state.accountData.find { it.accountId == accountId } - private fun getOrCreateActiveAccountData(): AccountData? { - val activeAccount = DeprecatedCodyAccountManager.getInstance().account ?: return null - val existingEntry = findEntry(activeAccount.id) - return existingEntry ?: AccountData(activeAccount.id).also { state.accountData += it } - } - companion object { @JvmStatic fun getInstance(project: Project): HistoryService = project.service() } From 30ba6c627a095d0b1d0db020bf0059f8f2c121eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 21:07:24 +0100 Subject: [PATCH 08/12] Revert redundant change --- src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt index 9fa01d125d..b06d7db548 100644 --- a/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt +++ b/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentClient.kt @@ -264,8 +264,7 @@ class CodyAgentClient(private val project: Project, private val webview: NativeW } if (params.key == "cody.serverEndpoint") { val endpoint = params.value ?: return - val server = SourcegraphServerPath(endpoint) - CodyAccount.setActiveAccount(CodyAccount(server)) + CodyAccount.setActiveAccount(CodyAccount(SourcegraphServerPath(endpoint))) CodyStatusService.resetApplication(project) } } From 1b22bc18a8118defa6d663bc9cf1b5c6d7c48a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Wed, 27 Nov 2024 21:47:20 +0100 Subject: [PATCH 09/12] Remove redundant `serverSentModels` --- src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index 71cbfec681..a2667a6ebf 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -1,7 +1,6 @@ package com.sourcegraph.cody.chat.ui import com.intellij.openapi.application.invokeLater -import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.openapi.ui.ComboBox import com.intellij.ui.MutableCollectionComboBoxModel @@ -29,7 +28,6 @@ class LlmDropdown( val parentDialog: EditCommandPrompt?, private val model: String? = null ) : ComboBox(MutableCollectionComboBoxModel()) { - private var hasServerSentModels = project.service().get().serverSentModels init { renderer = LlmComboBoxRenderer(this) @@ -57,7 +55,6 @@ class LlmDropdown( } private fun handleConfigUpdate(config: ConfigFeatures) { - hasServerSentModels = config.serverSentModels if (!isVisible && config.serverSentModels) { isVisible = true revalidate() From 702ee0300627454b7bfe029d1fd07b3e4a082159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Thu, 28 Nov 2024 11:10:17 +0100 Subject: [PATCH 10/12] Integrate DefaultLlmStorage.kt into LlmDropdown.kt --- .../sourcegraph/cody/chat/ui/LlmDropdown.kt | 10 +++---- .../cody/config/DefaultLlmStorage.kt | 28 ------------------- 2 files changed, 5 insertions(+), 33 deletions(-) delete mode 100644 src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index a2667a6ebf..252687783b 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -15,7 +15,7 @@ import com.sourcegraph.cody.agent.protocol_generated.Chat_ModelsParams import com.sourcegraph.cody.agent.protocol_generated.Model import com.sourcegraph.cody.agent.protocol_generated.ModelAvailabilityStatus import com.sourcegraph.cody.auth.CodyAccount -import com.sourcegraph.cody.config.DefaultLlmStorage +import com.sourcegraph.cody.auth.SourcegraphServerPath import com.sourcegraph.cody.edit.EditCommandPrompt import com.sourcegraph.cody.ui.LlmComboBoxRenderer import com.sourcegraph.common.BrowserOpener @@ -29,6 +29,8 @@ class LlmDropdown( private val model: String? = null ) : ComboBox(MutableCollectionComboBoxModel()) { + private val serverToRecentModel = HashMap() + init { renderer = LlmComboBoxRenderer(this) isVisible = false @@ -69,8 +71,7 @@ class LlmDropdown( val availableModels = models.map { it }.filterNot { it.model.isDeprecated() } availableModels.sortedBy { it.model.isCodyProOnly() }.forEach { addItem(it) } - val defaultLlm = - DefaultLlmStorage.getInstance(project).get(CodyAccount.getActiveAccount()?.server) + val defaultLlm = serverToRecentModel[CodyAccount.getActiveAccount()?.server] selectedItem = availableModels.find { it.model.id == model || it.model.id == defaultLlm?.id } @@ -97,8 +98,7 @@ class LlmDropdown( return } - DefaultLlmStorage.getInstance(project) - .store(CodyAccount.getActiveAccount()?.server, modelProvider.model) + CodyAccount.getActiveAccount()?.server?.also { serverToRecentModel[it] = modelProvider.model } super.setSelectedItem(anObject) onSetSelectedItem(modelProvider.model) diff --git a/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt b/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt deleted file mode 100644 index 18575a383d..0000000000 --- a/src/main/kotlin/com/sourcegraph/cody/config/DefaultLlmStorage.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.sourcegraph.cody.config - -import com.intellij.openapi.components.Service -import com.intellij.openapi.components.service -import com.intellij.openapi.project.Project -import com.sourcegraph.cody.agent.protocol_generated.Model -import com.sourcegraph.cody.auth.SourcegraphServerPath - -@Service(Service.Level.PROJECT) -class DefaultLlmStorage { - - private val storage = HashMap() - - fun store(server: SourcegraphServerPath?, model: Model) { - server ?: return - storage[server] = model - } - - fun get(server: SourcegraphServerPath?): Model? { - server ?: return null - return storage[server] - } - - companion object { - @JvmStatic - fun getInstance(project: Project): DefaultLlmStorage = project.service() - } -} From 61fadbf37987bc240544ee45b2395422a57c0f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Thu, 28 Nov 2024 11:14:02 +0100 Subject: [PATCH 11/12] Disable dropdown when fixedModel is defined --- src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt | 6 +++--- .../kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index 252687783b..d1ddf9ee32 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -26,7 +26,7 @@ class LlmDropdown( private val project: Project, private val onSetSelectedItem: (Model) -> Unit, val parentDialog: EditCommandPrompt?, - private val model: String? = null + private val fixedModel: String? = null ) : ComboBox(MutableCollectionComboBoxModel()) { private val serverToRecentModel = HashMap() @@ -74,10 +74,10 @@ class LlmDropdown( val defaultLlm = serverToRecentModel[CodyAccount.getActiveAccount()?.server] selectedItem = - availableModels.find { it.model.id == model || it.model.id == defaultLlm?.id } + availableModels.find { it.model.id == fixedModel || it.model.id == defaultLlm?.id } ?: models.firstOrNull() - isEnabled = true + isEnabled = fixedModel != null isVisible = selectedItem != null setMaximumRowCount(15) diff --git a/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt b/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt index 2fac6793be..34a98ac130 100644 --- a/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt +++ b/src/main/kotlin/com/sourcegraph/cody/edit/EditCommandPrompt.kt @@ -135,7 +135,7 @@ class EditCommandPrompt( project = project, onSetSelectedItem = { model = it.id }, this, - model = model) + fixedModel = model) .apply { foreground = boldLabelColor() background = textFieldBackground() From 33a99878373088cdbeefc741e721e9b9273520bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kondratek?= Date: Thu, 28 Nov 2024 11:34:44 +0100 Subject: [PATCH 12/12] Make serverToRecentModel map static --- .../kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt index d1ddf9ee32..3e5bb25497 100644 --- a/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt +++ b/src/main/kotlin/com/sourcegraph/cody/chat/ui/LlmDropdown.kt @@ -29,8 +29,6 @@ class LlmDropdown( private val fixedModel: String? = null ) : ComboBox(MutableCollectionComboBoxModel()) { - private val serverToRecentModel = HashMap() - init { renderer = LlmComboBoxRenderer(this) isVisible = false @@ -77,7 +75,7 @@ class LlmDropdown( availableModels.find { it.model.id == fixedModel || it.model.id == defaultLlm?.id } ?: models.firstOrNull() - isEnabled = fixedModel != null + isEnabled = fixedModel == null isVisible = selectedItem != null setMaximumRowCount(15) @@ -104,4 +102,8 @@ class LlmDropdown( onSetSelectedItem(modelProvider.model) } } + + companion object { + private val serverToRecentModel = HashMap() + } }