33
44package software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat
55
6+ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7+ import com.google.gson.Gson
8+ import com.google.gson.JsonElement
9+ import com.google.gson.JsonParser
10+ import com.intellij.docker.agent.util.toJson
611import com.intellij.openapi.components.Service
712import com.intellij.openapi.components.service
813import com.intellij.openapi.project.Project
914import org.eclipse.lsp4j.ProgressParams
1015import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
1116import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ProgressNotificationUtils.getObject
17+ import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.CHAT_ERROR_PARAMS
18+ import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ErrorParams
1219import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT
1320import java.util.UUID
1421import java.util.concurrent.ConcurrentHashMap
1522
23+
1624@Service(Service .Level .PROJECT )
1725class ChatCommunicationManager {
1826 private val chatPartialResultMap = ConcurrentHashMap <String , String >()
@@ -34,36 +42,79 @@ class ChatCommunicationManager {
3442 if (tabId == null || tabId.isEmpty()) {
3543 return
3644 }
37- if (params.value.isLeft || params.value.right == null ) {
38- error(
39- " Error handling partial result notification: expected value of type Object"
40- )
45+ try {
46+ if (params.value.isLeft || params.value.right == null ) {
47+ error(
48+ " Error handling partial result notification: expected value of type Object"
49+ )
50+ }
51+
52+ val encryptedPartialChatResult = getObject(params, String ::class .java)
53+ if (encryptedPartialChatResult != null ) {
54+ val partialChatResult = AmazonQLspService .getInstance(project).encryptionManager.decrypt(encryptedPartialChatResult)
55+ sendErrorToUi(tabId, IllegalStateException (" Try out an error" ), token)
56+ // sendMessageToChatUi(SEND_CHAT_COMMAND_PROMPT, tabId, partialChatResult, isPartialResult = true)
57+ }
58+ } catch (e: Exception ) {
59+ sendErrorToUi(tabId, e, token)
4160 }
4261
43- val encryptedPartialChatResult = getObject(params, String ::class .java)
44- if (encryptedPartialChatResult != null ) {
45- val partialChatResult = AmazonQLspService .getInstance(project).encryptionManager.decrypt(encryptedPartialChatResult)
62+ }
63+
64+ private fun sendMessageToChatUi (command : String , tabId : String , partialChatResult : String , isPartialResult : Boolean ) {
65+ val uiMessage = convertToJsonToSendToChat(
66+ command = command,
67+ tabId = tabId,
68+ params = partialChatResult,
69+ isPartialResult = isPartialResult
70+ )
71+ AsyncChatUiListener .notifyPartialMessageUpdate(uiMessage)
72+ }
4673
47- val uiMessage = convertToJsonToSendToChat(
48- command = SEND_CHAT_COMMAND_PROMPT ,
49- tabId = tabId,
50- params = partialChatResult,
51- isPartialResult = true
52- )
53- AsyncChatUiListener .notifyPartialMessageUpdate(uiMessage)
54- }
74+
75+ fun sendErrorToUi (tabId : String , exception : Exception , token : String ) {
76+ removePartialChatMessage(token)
77+ val errorTitle = " An error occurred while processing your request."
78+ val errorMessage = " Details: ${exception.message} "
79+ val errorParams = Gson ().toJsonTree(ErrorParams (tabId, null , errorMessage, errorTitle))
80+ sendErrorMessageToChatUi(CHAT_ERROR_PARAMS , tabId, errorParams, false )
81+ }
82+
83+ private fun sendErrorMessageToChatUi (command : String , tabId : String , partialChatResult : JsonElement , isPartialResult : Boolean ) {
84+ val uiMessage = """
85+ {
86+ "command":"$command ",
87+ "tabId": "$tabId ",
88+ "params": $partialChatResult ,
89+ "isPartialResult": $isPartialResult
90+ }
91+ """ .trimIndent()
92+ AsyncChatUiListener .notifyPartialMessageUpdate(uiMessage)
5593 }
5694 companion object {
5795 fun getInstance (project : Project ) = project.service<ChatCommunicationManager >()
5896
59- fun convertToJsonToSendToChat (command : String , tabId : String , params : String , isPartialResult : Boolean ): String =
60- """
97+ fun convertToJsonToSendToChat (command : String , tabId : String , params : String , isPartialResult : Boolean ): String {
98+ if (command == CHAT_ERROR_PARAMS ) {
99+ val param = JsonParser .parseString(params)
100+ return """
101+ {
102+ "command":"$command ",
103+ "tabId": "$tabId ",
104+ "params": $param ,
105+ "isPartialResult": $isPartialResult
106+ }
107+ """ .trimIndent()
108+ }
109+ return """
61110 {
62111 "command":"$command ",
63112 "tabId": "$tabId ",
64113 "params": $params ,
65114 "isPartialResult": $isPartialResult
66115 }
67116 """ .trimIndent()
117+ }
118+
68119 }
69120}
0 commit comments