|
4 | 4 | package software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat |
5 | 5 |
|
6 | 6 | import com.google.gson.Gson |
| 7 | +import com.google.gson.JsonElement |
7 | 8 | import com.intellij.openapi.components.Service |
8 | 9 | import com.intellij.openapi.components.service |
9 | 10 | import com.intellij.openapi.project.Project |
10 | 11 | import org.eclipse.lsp4j.ProgressParams |
11 | 12 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService |
12 | 13 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ProgressNotificationUtils.getObject |
| 14 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ErrorParams |
13 | 15 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.GetSerializedChatResult |
14 | 16 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.OpenTabResult |
15 | 17 | import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT |
| 18 | +import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.CHAT_ERROR_PARAMS |
16 | 19 | import java.util.UUID |
17 | 20 | import java.util.concurrent.CompletableFuture |
18 | 21 | import java.util.concurrent.ConcurrentHashMap |
@@ -70,13 +73,42 @@ class ChatCommunicationManager { |
70 | 73 | } |
71 | 74 | } |
72 | 75 |
|
| 76 | + fun sendErrorToUi(tabId: String, exception: Exception, token: String?) { |
| 77 | + token?.let { |
| 78 | + removePartialChatMessage(it) |
| 79 | + } |
| 80 | + val errorTitle = "An error occurred while processing your request." |
| 81 | + val errorMessage = "Details: ${exception.message}" |
| 82 | + val errorParams = Gson().toJsonTree(ErrorParams(tabId, null, errorMessage, errorTitle)) |
| 83 | + sendErrorMessageToChatUi(CHAT_ERROR_PARAMS, tabId, errorParams, false) |
| 84 | + } |
| 85 | + |
| 86 | + private fun sendErrorMessageToChatUi(command: String, tabId: String, partialChatResult: JsonElement, isPartialResult: Boolean) { |
| 87 | + val uiMessage = """ |
| 88 | + { |
| 89 | + "command":"$command", |
| 90 | + "tabId": "$tabId", |
| 91 | + "params": $partialChatResult, |
| 92 | + "isPartialResult": $isPartialResult |
| 93 | + } |
| 94 | + """.trimIndent() |
| 95 | + AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage) |
| 96 | + } |
| 97 | + |
73 | 98 | companion object { |
74 | 99 | fun getInstance(project: Project) = project.service<ChatCommunicationManager>() |
75 | 100 |
|
76 | 101 | val pendingSerializedChatRequests = ConcurrentHashMap<String, CompletableFuture<GetSerializedChatResult>>() |
77 | 102 | fun completeSerializedChatResponse(requestId: String, content: String) { |
78 | 103 | pendingSerializedChatRequests.remove(requestId)?.complete(GetSerializedChatResult((content))) |
79 | 104 | } |
| 105 | + fun getErrorUiMessage(tabId: String, e: Exception): String = Gson().toJson( |
| 106 | + ErrorParams( |
| 107 | + title = "An error occurred while processing your request.", |
| 108 | + message = "Details: ${e.message}", |
| 109 | + tabID = tabId, |
| 110 | + triggerType = TODO(), |
| 111 | + )).toString() |
80 | 112 |
|
81 | 113 | fun convertToJsonToSendToChat(command: String, tabId: String, params: String, isPartialResult: Boolean): String = |
82 | 114 | """ |
|
0 commit comments