Skip to content

Commit ad1b76f

Browse files
committed
merge conflicts and test changes
1 parent 4b52490 commit ad1b76f

File tree

4 files changed

+98
-25
lines changed

4 files changed

+98
-25
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,24 @@ class BrowserConnector(
185185

186186
result.whenComplete {
187187
value, error ->
188-
chatCommunicationManager.removePartialChatMessage(partialResultToken)
189-
val messageToChat = ChatCommunicationManager.convertToJsonToSendToChat(
190-
node.command,
191-
requestFromUi.params.tabId,
192-
encryptionManager?.decrypt(value).orEmpty(),
193-
isPartialResult = false
194-
)
195-
browser.postChat(messageToChat)
188+
if(error != null) {
189+
throw error
190+
}
191+
try {
192+
chatCommunicationManager.removePartialChatMessage(partialResultToken)
193+
val messageToChat = ChatCommunicationManager.convertToJsonToSendToChat(
194+
node.command,
195+
requestFromUi.params.tabId,
196+
encryptionManager?.decrypt(value) ?: "",
197+
isPartialResult = false
198+
)
199+
throw Exception("Trying an error message")
200+
browser.postChat(messageToChat)
201+
} catch (e: Exception) {
202+
chatCommunicationManager.sendErrorToUi(requestFromUi.params.tabId, e, partialResultToken)
203+
}
204+
205+
196206
}
197207
}
198208
}

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/flareChat/ChatCommunicationManager.kt

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33

44
package 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
611
import com.intellij.openapi.components.Service
712
import com.intellij.openapi.components.service
813
import com.intellij.openapi.project.Project
914
import org.eclipse.lsp4j.ProgressParams
1015
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
1116
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
1219
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT
1320
import java.util.UUID
1421
import java.util.concurrent.ConcurrentHashMap
1522

23+
1624
@Service(Service.Level.PROJECT)
1725
class 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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat
5+
6+
data class ErrorParams(
7+
val tabID: String,
8+
val triggerType: String?,
9+
val message: String,
10+
val title: String
11+
)

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/model/aws/chat/FlareChatCommands.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat
55

66
const val SEND_CHAT_COMMAND_PROMPT = "aws/chat/sendChatPrompt"
7+
const val CHAT_ERROR_PARAMS = "errorMessage"

0 commit comments

Comments
 (0)