@@ -7,6 +7,9 @@ import com.google.gson.Gson
77import com.intellij.openapi.components.Service
88import com.intellij.openapi.components.service
99import com.intellij.openapi.project.Project
10+ import kotlinx.coroutines.CompletableDeferred
11+ import kotlinx.coroutines.CoroutineScope
12+ import kotlinx.coroutines.launch
1013import org.eclipse.lsp4j.ProgressParams
1114import software.aws.toolkits.core.utils.getLogger
1215import software.aws.toolkits.core.utils.warn
@@ -29,12 +32,23 @@ import java.util.concurrent.CompletableFuture
2932import java.util.concurrent.ConcurrentHashMap
3033
3134@Service(Service .Level .PROJECT )
32- class ChatCommunicationManager {
35+ class ChatCommunicationManager (private val cs : CoroutineScope ) {
36+ val uiReady = CompletableDeferred <Boolean >()
3337 private val chatPartialResultMap = ConcurrentHashMap <String , String >()
34- private fun getPartialChatMessage (partialResultToken : String ): String? =
35- chatPartialResultMap.getOrDefault(partialResultToken, null )
36-
3738 private val inflightRequestByTabId = ConcurrentHashMap <String , CompletableFuture <String >>()
39+ private val pendingSerializedChatRequests = ConcurrentHashMap <String , CompletableFuture <GetSerializedChatResult >>()
40+ private val pendingTabRequests = ConcurrentHashMap <String , CompletableFuture <OpenTabResult >>()
41+
42+ fun setUiReady () {
43+ uiReady.complete(true )
44+ }
45+
46+ fun notifyUi (uiMessage : FlareUiMessage ) {
47+ cs.launch {
48+ uiReady.await()
49+ AsyncChatUiListener .notifyPartialMessageUpdate(uiMessage)
50+ }
51+ }
3852
3953 fun setInflightRequestForTab (tabId : String , result : CompletableFuture <String >) {
4054 inflightRequestByTabId[tabId] = result
@@ -53,9 +67,36 @@ class ChatCommunicationManager {
5367 return partialResultToken
5468 }
5569
70+ private fun getPartialChatMessage (partialResultToken : String ): String? =
71+ chatPartialResultMap.getOrDefault(partialResultToken, null )
72+
5673 fun removePartialChatMessage (partialResultToken : String ) =
5774 chatPartialResultMap.remove(partialResultToken)
5875
76+ fun addSerializedChatRequest (requestId : String , result : CompletableFuture <GetSerializedChatResult >) {
77+ pendingSerializedChatRequests[requestId] = result
78+ }
79+
80+ fun completeSerializedChatResponse (requestId : String , content : String ) {
81+ pendingSerializedChatRequests.remove(requestId)?.complete(GetSerializedChatResult ((content)))
82+ }
83+
84+ fun removeSerializedChatRequest (requestId : String ) {
85+ pendingSerializedChatRequests.remove(requestId)
86+ }
87+
88+ fun addTabOpenRequest (requestId : String , result : CompletableFuture <OpenTabResult >) {
89+ pendingTabRequests[requestId] = result
90+ }
91+
92+ fun completeTabOpen (requestId : String , tabId : String ) {
93+ pendingTabRequests.remove(requestId)?.complete(OpenTabResult (tabId))
94+ }
95+
96+ fun removeTabOpenRequest (requestId : String ) {
97+ pendingTabRequests.remove(requestId)
98+ }
99+
59100 fun handlePartialResultProgressNotification (project : Project , params : ProgressParams ) {
60101 val token = ProgressNotificationUtils .getToken(params)
61102 val tabId = getPartialChatMessage(token)
@@ -134,11 +175,6 @@ class ChatCommunicationManager {
134175
135176 private val LOG = getLogger<ChatCommunicationManager >()
136177
137- val pendingSerializedChatRequests = ConcurrentHashMap <String , CompletableFuture <GetSerializedChatResult >>()
138- fun completeSerializedChatResponse (requestId : String , content : String ) {
139- pendingSerializedChatRequests.remove(requestId)?.complete(GetSerializedChatResult ((content)))
140- }
141-
142178 fun convertToJsonToSendToChat (command : String , tabId : String , params : String , isPartialResult : Boolean ): String =
143179 """
144180 {
@@ -148,11 +184,5 @@ class ChatCommunicationManager {
148184 "isPartialResult": $isPartialResult
149185 }
150186 """ .trimIndent()
151-
152- val pendingTabRequests = ConcurrentHashMap <String , CompletableFuture <OpenTabResult >>()
153-
154- fun completeTabOpen (requestId : String , tabId : String ) {
155- pendingTabRequests.remove(requestId)?.complete(OpenTabResult (tabId))
156- }
157187 }
158188}
0 commit comments