From 9948ca419a998a3cb35c7b877b0e0b2edfe976c8 Mon Sep 17 00:00:00 2001 From: Luo Tim Date: Thu, 19 Sep 2024 10:56:27 +0800 Subject: [PATCH] Wait for client ready on loading commands --- .../core/handlers/ListCommandsRequestHandler.kt | 13 +++++++++++++ .../ai/devchat/plugin/DevChatToolWindowFactory.kt | 4 +--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ai/devchat/core/handlers/ListCommandsRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/ListCommandsRequestHandler.kt index 942cf6a..0073b0a 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/ListCommandsRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/ListCommandsRequestHandler.kt @@ -2,8 +2,12 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions +import ai.devchat.core.DevChatClient import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeoutOrNull class ListCommandsRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler( @@ -14,6 +18,15 @@ class ListCommandsRequestHandler(project: Project, requestAction: String, metada ) { override val actionName: String = DevChatActions.LIST_COMMANDS_RESPONSE override fun action() { + var client: DevChatClient? = null + runBlocking { + client = withTimeoutOrNull(5000) { // 5000 milliseconds = 5 seconds timeout + while (devChatService.client == null) { + delay(100) // Wait for 100 milliseconds before checking again + } + devChatService.client + } + } val recommendedWorkflows = client?.getWorkflowConfig()?.recommend?.workflows.orEmpty() val indexedCommands = client?.getWorkflowList()?.map { val commandName = it.name diff --git a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt index 571c19a..e74166d 100644 --- a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt +++ b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt @@ -61,7 +61,5 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable { devChatService.activeConversation = ActiveConversation() } - override fun dispose() { -// TODO("Not yet implemented") - } + override fun dispose() {} }