3
3
4
4
package software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat
5
5
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
6
11
import com.intellij.openapi.components.Service
7
12
import com.intellij.openapi.components.service
8
13
import com.intellij.openapi.project.Project
9
14
import org.eclipse.lsp4j.ProgressParams
10
15
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
11
16
import 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
12
19
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT
13
20
import java.util.UUID
14
21
import java.util.concurrent.ConcurrentHashMap
15
22
23
+
16
24
@Service(Service .Level .PROJECT )
17
25
class ChatCommunicationManager {
18
26
private val chatPartialResultMap = ConcurrentHashMap <String , String >()
@@ -34,36 +42,79 @@ class ChatCommunicationManager {
34
42
if (tabId == null || tabId.isEmpty()) {
35
43
return
36
44
}
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)
41
60
}
42
61
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
+ }
46
73
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)
55
93
}
56
94
companion object {
57
95
fun getInstance (project : Project ) = project.service<ChatCommunicationManager >()
58
96
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 """
61
110
{
62
111
"command":"$command ",
63
112
"tabId": "$tabId ",
64
113
"params": $params ,
65
114
"isPartialResult": $isPartialResult
66
115
}
67
116
""" .trimIndent()
117
+ }
118
+
68
119
}
69
120
}
0 commit comments