Skip to content

Commit 060a132

Browse files
rlimanodnyab
andauthored
fix(amazonq): fix issue where chat messages get sent to all projects (#5787)
* fix(amazonq): fix issue where chat messages get sent to all projects * callsites --------- Co-authored-by: manodnyab <[email protected]>
1 parent 69f1522 commit 060a132

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/ActionRegistrar.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ActionRegistrar {
2929

3030
fun reportMessageClick(command: EditorContextCommand, project: Project) {
3131
if (command == EditorContextCommand.GenerateUnitTests) {
32-
AsyncChatUiListener.notifyPartialMessageUpdate(Gson().toJson(TestCommandMessage()))
32+
AsyncChatUiListener.notifyPartialMessageUpdate(project, Gson().toJson(TestCommandMessage()))
3333
} else {
3434
// new agentic chat route
3535
ApplicationManager.getApplication().executeOnPooledThread {
@@ -45,7 +45,7 @@ class ActionRegistrar {
4545
val params = SendToPromptParams(selection = codeSelection, triggerType = TriggerType.CONTEXT_MENU)
4646
uiMessage = FlareUiMessage(command = SEND_TO_PROMPT, params = params)
4747
}
48-
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
48+
AsyncChatUiListener.notifyPartialMessageUpdate(project, uiMessage)
4949
}
5050
}
5151
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/codescan/actions/ExplainCodeIssueAction.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package software.aws.toolkits.jetbrains.services.cwc.commands.codescan.actions
55

66
import com.intellij.openapi.actionSystem.ActionManager
7+
import com.intellij.openapi.actionSystem.ActionUpdateThread
78
import com.intellij.openapi.actionSystem.AnAction
89
import com.intellij.openapi.actionSystem.AnActionEvent
910
import com.intellij.openapi.actionSystem.DataKey
@@ -18,7 +19,14 @@ import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SendT
1819
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TriggerType
1920

2021
class ExplainCodeIssueAction : AnAction(), DumbAware {
22+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
23+
24+
override fun update(e: AnActionEvent) {
25+
e.presentation.isEnabledAndVisible = e.project != null
26+
}
27+
2128
override fun actionPerformed(e: AnActionEvent) {
29+
val project = e.project ?: return
2230
val issueDataKey = DataKey.create<MutableMap<String, String>>("amazonq.codescan.explainissue")
2331
val issueContext = e.getData(issueDataKey) ?: return
2432

@@ -50,7 +58,7 @@ class ExplainCodeIssueAction : AnAction(), DumbAware {
5058
)
5159

5260
val uiMessage = FlareUiMessage(SEND_TO_PROMPT, params)
53-
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
61+
AsyncChatUiListener.notifyPartialMessageUpdate(project, uiMessage)
5462
}
5563
}
5664
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
315315

316316
override fun sendChatUpdate(params: LSPAny): CompletableFuture<Unit> {
317317
AsyncChatUiListener.notifyPartialMessageUpdate(
318+
project,
318319
FlareUiMessage(
319320
command = CHAT_SEND_UPDATE,
320321
params = params,

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33

44
package software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat
55

6-
import com.intellij.openapi.application.ApplicationManager
6+
import com.intellij.openapi.project.Project
77
import com.intellij.util.messages.Topic
88
import java.util.EventListener
99

10+
@Deprecated("Why are we using a message bus for this????????")
1011
interface AsyncChatUiListener : EventListener {
1112
@Deprecated("shouldn't need this version")
1213
fun onChange(command: String) {}
1314

1415
fun onChange(command: FlareUiMessage) {}
1516

1617
companion object {
17-
@Topic.AppLevel
18+
@Topic.ProjectLevel
1819
val TOPIC = Topic.create("Partial chat message provider", AsyncChatUiListener::class.java)
1920

20-
fun notifyPartialMessageUpdate(command: FlareUiMessage) {
21-
ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).onChange(command)
21+
@Deprecated("Why are we using a message bus for this????????")
22+
fun notifyPartialMessageUpdate(project: Project, command: FlareUiMessage) {
23+
project.messageBus.syncPublisher(TOPIC).onChange(command)
2224
}
2325

2426
@Deprecated("shouldn't need this version")
25-
fun notifyPartialMessageUpdate(command: String) {
26-
ApplicationManager.getApplication().messageBus.syncPublisher(TOPIC).onChange(command)
27+
fun notifyPartialMessageUpdate(project: Project, command: String) {
28+
project.messageBus.syncPublisher(TOPIC).onChange(command)
2729
}
2830
}
2931
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import java.util.concurrent.CompletableFuture
3737
import java.util.concurrent.ConcurrentHashMap
3838

3939
@Service(Service.Level.PROJECT)
40-
class ChatCommunicationManager(private val cs: CoroutineScope) {
40+
class ChatCommunicationManager(private val project: Project, private val cs: CoroutineScope) {
4141
val uiReady = CompletableDeferred<Boolean>()
4242
private val chatPartialResultMap = ConcurrentHashMap<String, String>()
4343
private val inflightRequestByTabId = ConcurrentHashMap<String, CompletableFuture<String>>()
@@ -53,7 +53,7 @@ class ChatCommunicationManager(private val cs: CoroutineScope) {
5353
fun notifyUi(uiMessage: FlareUiMessage) {
5454
cs.launch {
5555
uiReady.await()
56-
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
56+
AsyncChatUiListener.notifyPartialMessageUpdate(project, uiMessage)
5757
}
5858
}
5959

@@ -148,7 +148,7 @@ class ChatCommunicationManager(private val cs: CoroutineScope) {
148148
params = partialChatResult,
149149
isPartialResult = true
150150
)
151-
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
151+
AsyncChatUiListener.notifyPartialMessageUpdate(project, uiMessage)
152152
finalResultProcessed[token] = true
153153
ChatAsyncResultManager.getInstance(project).setResult(token, partialResultMap)
154154
return
@@ -169,7 +169,7 @@ class ChatCommunicationManager(private val cs: CoroutineScope) {
169169
params = partialChatResult,
170170
isPartialResult = true
171171
)
172-
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
172+
AsyncChatUiListener.notifyPartialMessageUpdate(project, uiMessage)
173173
}
174174
}
175175
}

0 commit comments

Comments
 (0)