From 3a26e2941a847864046522c444ebad656a09dedf Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Thu, 19 Sep 2024 20:44:27 +0800 Subject: [PATCH] Remove cache of devchat client in BaseActionHandler --- .../ai/devchat/core/BaseActionHandler.kt | 1 - .../DeleteLastConversationRequestHandler.kt | 3 ++- .../handlers/DeleteTopicRequestHandler.kt | 3 ++- .../core/handlers/ListTopicsRequestHandler.kt | 3 ++- .../LoadConversationRequestHandler.kt | 3 ++- .../handlers/SendMessageRequestHandler.kt | 8 +++--- .../devchat/storage/SensitiveDataStorage.kt | 27 ------------------- 7 files changed, 13 insertions(+), 35 deletions(-) delete mode 100644 src/main/kotlin/ai/devchat/storage/SensitiveDataStorage.kt diff --git a/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt b/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt index 1ba8e1c..d2892cc 100644 --- a/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt +++ b/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt @@ -16,7 +16,6 @@ abstract class BaseActionHandler( var payload: JSONObject? = null ) : ActionHandler { val devChatService: DevChatService = project.getService(DevChatService::class.java) - val client: DevChatClient? = devChatService.client val wrapper: DevChatWrapper? = devChatService.wrapper val browser: Browser? = devChatService.browser val activeConversation: ActiveConversation? = devChatService.activeConversation diff --git a/src/main/kotlin/ai/devchat/core/handlers/DeleteLastConversationRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/DeleteLastConversationRequestHandler.kt index d4f1e4a..fdd3426 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/DeleteLastConversationRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/DeleteLastConversationRequestHandler.kt @@ -2,6 +2,7 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions +import ai.devchat.plugin.DevChatService import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -14,7 +15,7 @@ class DeleteLastConversationRequestHandler(project: Project, requestAction: Stri override val actionName: String = DevChatActions.DELETE_LAST_CONVERSATION_RESPONSE override fun action() { val promptHash = payload!!.getString("promptHash") - client!!.deleteLog(promptHash) + project.getService(DevChatService::class.java).client!!.deleteLog(promptHash) send(payload = mapOf("promptHash" to promptHash)) } } diff --git a/src/main/kotlin/ai/devchat/core/handlers/DeleteTopicRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/DeleteTopicRequestHandler.kt index 659dafb..3f1926e 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/DeleteTopicRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/DeleteTopicRequestHandler.kt @@ -2,6 +2,7 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions +import ai.devchat.plugin.DevChatService import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -14,7 +15,7 @@ class DeleteTopicRequestHandler(project: Project, requestAction: String, metadat override val actionName: String = DevChatActions.DELETE_TOPIC_RESPONSE override fun action() { val topicHash = payload!!.getString("topicHash") - client!!.deleteTopic(topicHash) + project.getService(DevChatService::class.java).client!!.deleteTopic(topicHash) send(payload = mapOf("topicHash" to topicHash)) } } diff --git a/src/main/kotlin/ai/devchat/core/handlers/ListTopicsRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/ListTopicsRequestHandler.kt index a514de4..c0537af 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/ListTopicsRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/ListTopicsRequestHandler.kt @@ -2,6 +2,7 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions +import ai.devchat.plugin.DevChatService import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -14,7 +15,7 @@ class ListTopicsRequestHandler(project: Project, requestAction: String, metadata ) { override val actionName: String = DevChatActions.LIST_TOPICS_RESPONSE override fun action() { - val topics = client!!.getTopics().map { + val topics = project.getService(DevChatService::class.java).client!!.getTopics().map { val request = it.rootPromptRequest val response = it.rootPromptResponse mapOf( diff --git a/src/main/kotlin/ai/devchat/core/handlers/LoadConversationRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/LoadConversationRequestHandler.kt index 8eaef50..215756d 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/LoadConversationRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/LoadConversationRequestHandler.kt @@ -2,6 +2,7 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions +import ai.devchat.plugin.DevChatService import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -20,7 +21,7 @@ class LoadConversationRequestHandler(project: Project, requestAction: String, me topicHash.isNullOrEmpty() -> activeConversation!!.reset() topicHash == activeConversation!!.topic -> res["reset"] = false else -> { - val logs = client!!.getTopicLogs(topicHash) + val logs = project.getService(DevChatService::class.java).client!!.getTopicLogs(topicHash) activeConversation.reset(topicHash, logs) } } diff --git a/src/main/kotlin/ai/devchat/core/handlers/SendMessageRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/SendMessageRequestHandler.kt index f7df73b..16e902d 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/SendMessageRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/SendMessageRequestHandler.kt @@ -3,6 +3,7 @@ package ai.devchat.core.handlers import ai.devchat.common.Log import ai.devchat.common.PathUtils import ai.devchat.core.* +import ai.devchat.plugin.DevChatService import ai.devchat.storage.CONFIG import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -52,7 +53,7 @@ class SendMessageRequestHandler(project: Project, requestAction: String, metadat contextContents = contextContents, ) - client!!.message( + project.getService(DevChatService::class.java).client!!.message( chatRequest, dataHandler(chatRequest), ::errorHandler, @@ -99,10 +100,11 @@ class SendMessageRequestHandler(project: Project, requestAction: String, metadat } private fun finishHandler(chatRequest: ChatRequest): (Int) -> Unit { val response = chatRequest.response!! + val devChatService = project.getService(DevChatService::class.java) return { exitCode: Int -> when(exitCode) { 0 -> { - val entry = client!!.insertLog( + val entry = devChatService.client!!.insertLog( LogEntry( chatRequest.modelName, chatRequest.parent, @@ -115,7 +117,7 @@ class SendMessageRequestHandler(project: Project, requestAction: String, metadat promptCallback(response) val currentTopic = activeConversation!!.topic ?: response.promptHash!! - val logs = client.getTopicLogs(currentTopic, 0, 1) + val logs = devChatService.client!!.getTopicLogs(currentTopic, 0, 1) if (currentTopic == activeConversation.topic) { activeConversation.addMessage(logs.first()) diff --git a/src/main/kotlin/ai/devchat/storage/SensitiveDataStorage.kt b/src/main/kotlin/ai/devchat/storage/SensitiveDataStorage.kt deleted file mode 100644 index a694083..0000000 --- a/src/main/kotlin/ai/devchat/storage/SensitiveDataStorage.kt +++ /dev/null @@ -1,27 +0,0 @@ -package ai.devchat.storage - -import com.intellij.credentialStore.CredentialAttributes -import com.intellij.credentialStore.Credentials -import com.intellij.credentialStore.generateServiceName -import com.intellij.ide.passwordSafe.PasswordSafe - -object SensitiveDataStorage { - private const val KEY_NAME = "USER_KEY" - private const val SUBSYSTEM = "ai.devchat.idea" - private fun createCredentialAttributes(): CredentialAttributes { - return CredentialAttributes( - generateServiceName(SUBSYSTEM, KEY_NAME) - ) - } - - var key: String? - get() { - val attributes = createCredentialAttributes() - return PasswordSafe.instance.getPassword(attributes) - } - set(key) { - val attributes = createCredentialAttributes() - val credentials = Credentials("default", key) - PasswordSafe.instance.set(attributes, credentials) - } -}