Skip to content

Commit a578559

Browse files
committed
action commands in chat support
1 parent 20c045b commit a578559

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33

44
package software.aws.toolkits.jetbrains.services.cwc.commands
55

6+
import com.google.gson.Gson
67
import com.intellij.openapi.project.Project
78
import kotlinx.coroutines.flow.MutableSharedFlow
89
import kotlinx.coroutines.flow.asSharedFlow
10+
import kotlinx.coroutines.runBlocking
11+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.AsyncChatUiListener
12+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.GenericCommandParams
13+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TriggerType
914
import software.aws.toolkits.jetbrains.services.amazonq.messages.AmazonQMessage
15+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.ActiveFileContextExtractor
16+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.ExtractionTriggerType
1017

1118
// Register Editor Actions in the Editor Context Menu
1219
class ActionRegistrar {
@@ -15,7 +22,29 @@ class ActionRegistrar {
1522
val flow = _messages.asSharedFlow()
1623

1724
fun reportMessageClick(command: EditorContextCommand, project: Project) {
18-
_messages.tryEmit(ContextMenuActionMessage(command, project))
25+
if (command == EditorContextCommand.GenerateUnitTests) {
26+
// pre-existing old chat code path
27+
_messages.tryEmit(ContextMenuActionMessage(command, project))
28+
}
29+
else {
30+
// new agentic chat route
31+
runBlocking {
32+
val contextExtractor = ActiveFileContextExtractor.create(fqnWebviewAdapter = null, project = project)
33+
val fileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ContextMenu)
34+
val codeSelection = "\n```\n${fileContext.focusAreaContext?.codeSelection?.trimIndent()?.trim()}\n```\n"
35+
36+
val params = GenericCommandParams(selection = codeSelection, triggerType = TriggerType.CONTEXT_MENU, genericCommand = command.name)
37+
38+
val uiMessage = """
39+
{
40+
"command": "genericCommand",
41+
"params": ${Gson().toJson(params)}
42+
}
43+
""".trimIndent()
44+
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
45+
}
46+
}
47+
1948
}
2049

2150
fun reportMessageClick(command: EditorContextCommand, issue: MutableMap<String, String>, project: Project) {
@@ -25,5 +54,13 @@ class ActionRegistrar {
2554
// provide singleton access
2655
companion object {
2756
val instance = ActionRegistrar()
57+
58+
/**
59+
* Computes the product of 10 * 10
60+
* @return The result of 10 * 10
61+
*/
62+
fun computeTenTimesTen(): Int {
63+
return 10 * 10
64+
}
2865
}
2966
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/ChatController.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,28 +289,11 @@ class ChatController private constructor(
289289
if (message.project != context.project) {
290290
return
291291
}
292-
val fileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ContextMenu)
293-
val triggerId = UUID.randomUUID().toString()
294-
val codeSelection = "\n```\n${fileContext.focusAreaContext?.codeSelection?.trimIndent()?.trim()}\n```\n"
295-
296-
if (message.command == EditorContextCommand.SendToPrompt) {
297-
messagePublisher.publish(
298-
EditorContextCommandMessage(
299-
message = codeSelection,
300-
command = message.command.actionId,
301-
triggerId = triggerId,
302-
),
303-
)
304-
return
305-
}
292+
306293
if (message.command == EditorContextCommand.GenerateUnitTests) {
307294
// Publish an event to "codetest" tab with command as "test" and type as "addAnswer"
308295
val messageToPublish = TestCommandMessage()
309296
context.messagesFromAppToUi.publish(messageToPublish)
310-
} else {
311-
// Create prompt
312-
val prompt = "${message.command} the following part of my code for me: $codeSelection"
313-
processPromptActions(prompt, message, triggerId, fileContext)
314297
}
315298
}
316299

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
7+
//https://github.com/aws/language-server-runtimes/blob/main/chat-client-ui-types/src/uiContracts.ts#L27
8+
enum class TriggerType(val value: String) {
9+
HOTKEYS("hotkeys"),
10+
CLICK("click"),
11+
CONTEXT_MENU("contextMenu")
12+
}
13+
14+
data class GenericCommandParams(
15+
val selection: String,
16+
val triggerType: TriggerType,
17+
val genericCommand: String
18+
)

0 commit comments

Comments
 (0)