diff --git a/plugins/amazonq/chat/jetbrains-community/resources/META-INF/plugin-chat.xml b/plugins/amazonq/chat/jetbrains-community/resources/META-INF/plugin-chat.xml index 7e29e78705..daec47d109 100644 --- a/plugins/amazonq/chat/jetbrains-community/resources/META-INF/plugin-chat.xml +++ b/plugins/amazonq/chat/jetbrains-community/resources/META-INF/plugin-chat.xml @@ -7,7 +7,7 @@ + topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener"/> @@ -70,6 +70,11 @@ + + + + diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/clients/chat/v1/ChatSessionV1.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/clients/chat/v1/ChatSessionV1.kt index 77446326f0..a5057f797c 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/clients/chat/v1/ChatSessionV1.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/clients/chat/v1/ChatSessionV1.kt @@ -297,6 +297,7 @@ class ChatSessionV1( UserIntent.EXPLAIN_LINE_BY_LINE -> FollowUpType.LineByLine UserIntent.EXPLAIN_CODE_SELECTION -> FollowUpType.ExplainInDetail UserIntent.UNKNOWN_TO_SDK_VERSION -> FollowUpType.Generated + UserIntent.GENERATE_UNIT_TESTS -> FollowUpType.Generated null -> FollowUpType.Generated } diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/EditorContextCommand.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/EditorContextCommand.kt index e08aab928c..e75a7d584f 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/EditorContextCommand.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/EditorContextCommand.kt @@ -26,6 +26,10 @@ enum class EditorContextCommand( verb = "Optimize", actionId = "aws.amazonq.optimizeCode", ), + GenerateUnitTests( + verb = "Generate unit tests for", + actionId = "aws.amazonq.generateUnitTests", + ), SendToPrompt( verb = "SendToPrompt", actionId = "aws.amazonq.sendToPrompt", diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/GenerateUnitTestsAction.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/GenerateUnitTestsAction.kt new file mode 100644 index 0000000000..2b1592110a --- /dev/null +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/GenerateUnitTestsAction.kt @@ -0,0 +1,21 @@ +// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package software.aws.toolkits.jetbrains.services.cwc.commands + +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.CommonDataKeys +import software.aws.toolkits.jetbrains.core.credentials.AwsBearerTokenConnection +import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager +import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection + +class GenerateUnitTestsAction : CustomAction(EditorContextCommand.GenerateUnitTests) { + override fun getActionUpdateThread() = ActionUpdateThread.BGT + + override fun update(e: AnActionEvent) { + val project = e.getData(CommonDataKeys.PROJECT) ?: return + val connection = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) as? AwsBearerTokenConnection + e.presentation.isEnabledAndVisible = connection?.startUrl == "https://amzn.awsapps.com/start" + } +} diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/ChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/ChatController.kt index d4258e0ce3..cd1ae5da02 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/ChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/ChatController.kt @@ -130,6 +130,8 @@ class ChatController private constructor( var shouldAddIndexInProgressMessage: Boolean = false var shouldUseWorkspaceContext: Boolean = false val isDataCollectionGroup = CodeWhispererFeatureConfigService.getInstance().getIsDataCollectionEnabled() + val startUrl = getStartUrl(context.project) + if (prompt.contains("@workspace")) { if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) { shouldUseWorkspaceContext = true @@ -151,7 +153,7 @@ class ChatController private constructor( triggerId = triggerId, message = prompt, activeFileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ChatMessage), - userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage), + userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage, startUrl), TriggerType.Click, projectContextQueryResult = queryResult, shouldAddIndexInProgressMessage = shouldAddIndexInProgressMessage, @@ -332,7 +334,11 @@ class ChatController private constructor( } // Create prompt - val prompt = "${message.command} the following part of my code for me: $codeSelection" + val prompt = if (EditorContextCommand.GenerateUnitTests == message.command) { + "${message.command.verb} the following part of my code for me: $codeSelection" + } else { + "${message.command} the following part of my code for me: $codeSelection" + } processPromptActions(prompt, message, triggerId, fileContext) } diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/telemetry/TelemetryHelper.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/telemetry/TelemetryHelper.kt index 22ac5d7fe7..3e8837fc73 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/telemetry/TelemetryHelper.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/telemetry/TelemetryHelper.kt @@ -58,6 +58,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se UserIntent.CITE_SOURCES -> CwsprChatUserIntent.CiteSources UserIntent.EXPLAIN_LINE_BY_LINE -> CwsprChatUserIntent.ExplainLineByLine UserIntent.EXPLAIN_CODE_SELECTION -> CwsprChatUserIntent.ExplainCodeSelection + UserIntent.GENERATE_UNIT_TESTS -> CwsprChatUserIntent.GenerateUnitTests UserIntent.UNKNOWN_TO_SDK_VERSION -> CwsprChatUserIntent.Unknown } diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/userIntent/UserIntentRecognizer.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/userIntent/UserIntentRecognizer.kt index a0637b10ac..f15f0125e9 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/userIntent/UserIntentRecognizer.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/controller/chat/userIntent/UserIntentRecognizer.kt @@ -16,14 +16,16 @@ class UserIntentRecognizer { EditorContextCommand.Fix -> UserIntent.APPLY_COMMON_BEST_PRACTICES EditorContextCommand.Optimize -> UserIntent.IMPROVE_CODE EditorContextCommand.ExplainCodeScanIssue -> UserIntent.EXPLAIN_CODE_SELECTION + EditorContextCommand.GenerateUnitTests -> UserIntent.GENERATE_UNIT_TESTS EditorContextCommand.SendToPrompt -> null } - fun getUserIntentFromPromptChatMessage(prompt: String) = when { + fun getUserIntentFromPromptChatMessage(prompt: String, startUrl: String?) = when { prompt.startsWith("Explain") -> UserIntent.EXPLAIN_CODE_SELECTION prompt.startsWith("Refactor") -> UserIntent.SUGGEST_ALTERNATE_IMPLEMENTATION prompt.startsWith("Fix") -> UserIntent.APPLY_COMMON_BEST_PRACTICES prompt.startsWith("Optimize") -> UserIntent.IMPROVE_CODE + prompt.startsWith("Generate unit tests") && isInternalAmazonUser(startUrl) -> UserIntent.GENERATE_UNIT_TESTS else -> null } @@ -43,4 +45,6 @@ class UserIntentRecognizer { fun getUserIntentFromOnboardingPageInteraction(interaction: OnboardingPageInteraction) = when (interaction.type) { OnboardingPageInteractionType.CwcButtonClick -> null } + + private fun isInternalAmazonUser(startUrl: String?): Boolean = startUrl == "https://amzn.awsapps.com/start" } diff --git a/plugins/core/jetbrains-community/resources/telemetryOverride.json b/plugins/core/jetbrains-community/resources/telemetryOverride.json index e3e8bee136..e221fbcfb8 100644 --- a/plugins/core/jetbrains-community/resources/telemetryOverride.json +++ b/plugins/core/jetbrains-community/resources/telemetryOverride.json @@ -59,7 +59,8 @@ "showExample", "citeSources", "explainLineByLine", - "explainCodeSelection" + "explainCodeSelection", + "generateUnitTests" ], "description": "Explict user intent associated with a chat message" }, diff --git a/plugins/core/resources/resources/software/aws/toolkits/resources/MessagesBundle.properties b/plugins/core/resources/resources/software/aws/toolkits/resources/MessagesBundle.properties index 867cf7178b..806b0f2701 100644 --- a/plugins/core/resources/resources/software/aws/toolkits/resources/MessagesBundle.properties +++ b/plugins/core/resources/resources/software/aws/toolkits/resources/MessagesBundle.properties @@ -21,6 +21,8 @@ action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.descri action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.text = Explain Code action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.description = Fixes the selected code action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.text = Fix Code +action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.description = Generates unit tests for the selected code +action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.text = Generate Tests (Beta) action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.description = Optimizes the selected code action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.text = Optimize Code action.aws.toolkit.jetbrains.core.services.cwc.commands.RefactorCodeAction.description = Refactors the selected code diff --git a/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json b/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json index 219b319d55..f8f4a99f5d 100644 --- a/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json +++ b/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json @@ -1952,7 +1952,8 @@ "SHOW_EXAMPLES", "CITE_SOURCES", "EXPLAIN_LINE_BY_LINE", - "EXPLAIN_CODE_SELECTION" + "EXPLAIN_CODE_SELECTION", + "GENERATE_UNIT_TESTS" ] }, "UserModificationEvent":{ diff --git a/plugins/core/sdk-codegen/codegen-resources/codewhispererstreaming/service-2.json b/plugins/core/sdk-codegen/codegen-resources/codewhispererstreaming/service-2.json index 72ed91db3f..863326fcb6 100644 --- a/plugins/core/sdk-codegen/codegen-resources/codewhispererstreaming/service-2.json +++ b/plugins/core/sdk-codegen/codegen-resources/codewhispererstreaming/service-2.json @@ -741,7 +741,8 @@ "SHOW_EXAMPLES", "CITE_SOURCES", "EXPLAIN_LINE_BY_LINE", - "EXPLAIN_CODE_SELECTION" + "EXPLAIN_CODE_SELECTION", + "GENERATE_UNIT_TESTS" ] }, "ValidationException":{