Skip to content

fix(amazonq): Enable stop button of chat #5671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.future.await
import kotlinx.coroutines.launch
import org.cef.browser.CefBrowser
import org.eclipse.lsp4j.Position
Expand All @@ -32,6 +31,7 @@
import software.aws.toolkits.jetbrains.services.amazonq.lsp.encryption.JwtEncryptionManager
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.AwsServerCapabilitiesProvider
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ChatCommunicationManager
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ChatCommunicationManager.Companion.convertToJsonToSendToChat
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.getTextDocumentIdentifier
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ButtonClickNotification
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ButtonClickParams
Expand Down Expand Up @@ -61,6 +61,7 @@
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ChatParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ChatPrompt
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ChatReadyNotification
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ChatUiMessageParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ConversationClickRequest
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.CopyCodeToClipboardNotification
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.CopyCodeToClipboardParams
Expand Down Expand Up @@ -90,9 +91,11 @@
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.PromptInputOptionChangeParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.QuickChatActionRequest
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_CHAT_COMMAND_PROMPT
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.STOP_CHAT_RESPONSE
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SendChatPromptRequest
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SourceLinkClickNotification
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SourceLinkClickParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.StopResponseMessage
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TabBarActionParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TabBarActionRequest
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TabEventParams
Expand All @@ -102,6 +105,7 @@
import software.aws.toolkits.jetbrains.services.amazonq.webview.theme.AmazonQTheme
import software.aws.toolkits.jetbrains.services.amazonq.webview.theme.ThemeBrowserAdapter
import software.aws.toolkits.jetbrains.settings.MeetQSettings
import software.aws.toolkits.resources.message

Check warning on line 108 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

View workflow job for this annotation

GitHub Actions / qodana

Usage of redundant or deprecated syntax or deprecated symbols

Remove deprecated symbol import
import software.aws.toolkits.telemetry.MetricResult
import software.aws.toolkits.telemetry.Telemetry
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -245,6 +249,10 @@
encryptionManager = this.encryptionManager
encryptionManager?.encrypt(chatParams)?.let { EncryptedChatParams(it, partialResultToken) }?.let { server.sendChatPrompt(it) }
} ?: (CompletableFuture.failedFuture(IllegalStateException("LSP Server not running")))

// We assume there is only one outgoing request per tab because the input is
// blocked when there is an outgoing request
chatCommunicationManager.setInflightRequestForTab(tabId, result)
showResult(result, partialResultToken, tabId, encryptionManager, browser)
}
CHAT_QUICK_ACTION -> {
Expand All @@ -262,6 +270,10 @@
}
} ?: (CompletableFuture.failedFuture(IllegalStateException("LSP Server not running")))

// We assume there is only one outgoing request per tab because the input is
// blocked when there is an outgoing request
chatCommunicationManager.setInflightRequestForTab(tabId, result)

showResult(result, partialResultToken, tabId, encryptionManager, browser)
}
CHAT_LIST_CONVERSATIONS -> {
Expand Down Expand Up @@ -318,6 +330,7 @@
CHAT_TAB_REMOVE -> {
handleChatNotification<TabEventRequest, TabEventParams>(node) { server, params ->
chatCommunicationManager.removePartialChatMessage(params.tabId)
cancelInflightRequests(params.tabId)
server.tabRemove(params)
}
}
Expand Down Expand Up @@ -414,6 +427,30 @@
server.createPrompt(params)
}
}
STOP_CHAT_RESPONSE -> {
val stopResponseRequest = serializer.deserializeChatMessages<StopResponseMessage>(node)
if (!chatCommunicationManager.hasInflightRequest(stopResponseRequest.params.tabId)) {
return
}
cancelInflightRequests(stopResponseRequest.params.tabId)
chatCommunicationManager.removePartialChatMessage(stopResponseRequest.params.tabId)

val paramsJson = Gson().toJson(
// https://github.com/aws/language-servers/blob/1c0d88806087125b6fc561f610cc15e98127c6bf/server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts#L403
ChatUiMessageParams(
title = message("amazonqChat.stopChatResponse"),

Check warning on line 441 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

View workflow job for this annotation

GitHub Actions / qodana

Usage of redundant or deprecated syntax or deprecated symbols

'message(String, vararg Any): String' is deprecated. Use extension-specific localization bundle instead
body = ""
)
)

val uiMessage = convertToJsonToSendToChat(
command = SEND_CHAT_COMMAND_PROMPT,
tabId = stopResponseRequest.params.tabId,
params = paramsJson.toString(),
isPartialResult = false
)
browser.postChat(uiMessage)
}
}
}

Expand All @@ -426,13 +463,21 @@
) {
result.whenComplete { value, error ->
chatCommunicationManager.removePartialChatMessage(partialResultToken)
val messageToChat = ChatCommunicationManager.convertToJsonToSendToChat(

Check warning on line 466 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/BrowserConnector.kt

View workflow job for this annotation

GitHub Actions / qodana

Redundant qualifier name

Redundant qualifier name
SEND_CHAT_COMMAND_PROMPT,
tabId,
encryptionManager?.decrypt(value).orEmpty(),
isPartialResult = false
)
browser.postChat(messageToChat)
chatCommunicationManager.removeInflightRequestForTab(tabId)
}
}

private fun cancelInflightRequests(tabId: String) {
chatCommunicationManager.getInflightRequestForTab(tabId)?.let { request ->
request.cancel(true)
chatCommunicationManager.removeInflightRequestForTab(tabId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ import java.util.concurrent.ConcurrentHashMap
@Service(Service.Level.PROJECT)
class ChatCommunicationManager {
private val chatPartialResultMap = ConcurrentHashMap<String, String>()
private fun getPartialChatMessage(partialResultToken: String): String =
chatPartialResultMap.getValue(partialResultToken)
private fun getPartialChatMessage(partialResultToken: String): String? =
chatPartialResultMap.getOrDefault(partialResultToken, null)

private val inflightRequestByTabId = ConcurrentHashMap<String, CompletableFuture<String>>()

fun setInflightRequestForTab(tabId: String, result: CompletableFuture<String>) {
inflightRequestByTabId[tabId] = result
}
fun removeInflightRequestForTab(tabId: String) {
inflightRequestByTabId.remove(tabId)
}

fun getInflightRequestForTab(tabId: String): CompletableFuture<String>? = inflightRequestByTabId[tabId]

fun hasInflightRequest(tabId: String): Boolean = inflightRequestByTabId.containsKey(tabId)

fun addPartialChatMessage(tabId: String): String {
val partialResultToken: String = UUID.randomUUID().toString()
Expand All @@ -35,7 +48,7 @@ class ChatCommunicationManager {
fun handlePartialResultProgressNotification(project: Project, params: ProgressParams) {
val token = ProgressNotificationUtils.getToken(params)
val tabId = getPartialChatMessage(token)
if (tabId == null || tabId.isEmpty()) {
if (tabId.isNullOrEmpty()) {
return
}
if (params.value.isLeft || params.value.right == null) {
Expand All @@ -47,7 +60,6 @@ class ChatCommunicationManager {
val encryptedPartialChatResult = getObject(params, String::class.java)
if (encryptedPartialChatResult != null) {
val partialChatResult = AmazonQLspService.getInstance(project).encryptionManager.decrypt(encryptedPartialChatResult)

val uiMessage = convertToJsonToSendToChat(
command = SEND_CHAT_COMMAND_PROMPT,
tabId = tabId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat
import java.util.UUID

data class ChatUiMessageParams(
val title: String,
val additionalMessages: List<String> = emptyList(),
val messageId: String = UUID.randomUUID().toString(),
val buttons: List<String> = emptyList(),
val codeReference: List<String> = emptyList(),
val body: String = "",
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ const val CHAT_SEND_UPDATE = "aws/chat/sendChatUpdate"
const val CHAT_CREATE_PROMPT = "aws/chat/createPrompt"
const val SHOW_SAVE_FILE_DIALOG_REQUEST_METHOD = "aws/showSaveFileDialog"
const val GET_SERIALIZED_CHAT_REQUEST_METHOD = "aws/chat/getSerializedChat"
const val STOP_CHAT_RESPONSE = "stopChatResponse"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat

data class StopResponseMessage(
val params: StopResponseParams,
)

data class StopResponseParams(
val tabId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ action.aws.toolkit.toolwindow.explorer.newConnection.text=Setup authentication t
action.aws.toolkit.toolwindow.newConnection.text=Add Another Connection...
action.dynamic.open.text=Open Resource...
action.q.openchat.text=Open Chat Panel
amazonqChat.stopChatResponse=You stopped your current work, please provide additional examples or ask another question.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use amazonq bundle instead @leigaol

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AmazonQBundle.properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are lots of new messages checked in the core bundle... I was following previous commits.

Can do as a fast follow. This needs to go in for today's bug bash. I have 2nd PR depending on this!

amazonqChat.project_context.index_in_progress=By the way, I'm still indexing this project for full context from your workspace. I may have a better response in a few minutes when it's complete if you'd like to try again then.
amazonqDoc.answer.codeResult=You can accept the changes to your files, or describe any additional changes you'd like me to make.
amazonqDoc.answer.readmeCreated=I've created a README for your code.
Expand Down
Loading