Skip to content

Commit 7ff4f29

Browse files
bweedopmanodnyabrlidhasani23David Hasani
authored
feat(amazonq): Add command to generate unit tests for selected code (aws#4869)
* feat (Amazon Q): Add basic UX and logic for generating unit tests * Update cwspr model with GENERATE_UNIT_TESTS user intent * fix (Amazon Q): Pass context specific intent and trigger type to chat (aws#4842) * Added authType to loginWithBrowser metric (aws#4843) * Added authType to loginWithBrowser metric * Add open and close sign in metrics * detekt * addressed feedback * feedback * moved the util function * Added UI click metrics on all login options (aws#4838) * Added UI click metrics on all login options * Combined calls for continue and back button * Updating version to 3.25 * Updating SNAPSHOT version to 3.26-SNAPSHOT * Revert open and close sign in webview telemetry (aws#4850) * Revert open and close sign in webview telemetry * add changelog * added issue in changelog * Update bugfix-64bb0c4b-8d3b-45b0-b615-8722bb3e9e4a.json --------- Co-authored-by: Richard Li <[email protected]> * Updating version to 3.26 * Updating SNAPSHOT version to 3.27-SNAPSHOT * telemetry(amazonq): update telemetry (aws#4847) * telemetry(amazonq): update telemetry * remove unused import --------- Co-authored-by: David Hasani <[email protected]> * feat(amazonq): Reduce @workspace indexing time by 50% (aws#4846) * Hash startUrl * Revert CodeWhisperer endpoint back gamma endpoint * Revert adding user intent to interact with message metadata * Add changelog * Refactor for consistent hashing of startUrl * Remove hashing logic on startUrl and changelog entry * Add (Beta) suffix to generate unit tests command * Add check for startUrl when getting user intent from prompt * Update TestCodeAction to resolve lint failure Co-authored-by: Richard Li <[email protected]> * address comments: Better GenerateUnitTest Action naming * Refactor to use more specific command for UTG * Change name of action to GenerateUnitTests * Fix linting errors * Override 'getActionUpdateThread' * Reorder MessageBundle.properties * Fix syntax error --------- Co-authored-by: manodnyab <[email protected]> Co-authored-by: aws-toolkit-automation <> Co-authored-by: Richard Li <[email protected]> Co-authored-by: David <[email protected]> Co-authored-by: David Hasani <[email protected]> Co-authored-by: Lei Gao <[email protected]> Co-authored-by: David Lin <[email protected]>
1 parent b88f7d8 commit 7ff4f29

File tree

11 files changed

+54
-7
lines changed

11 files changed

+54
-7
lines changed

plugins/amazonq/chat/jetbrains-community/resources/META-INF/plugin-chat.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<projectListeners>
99
<listener class="software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AmazonQToolWindowListener"
10-
topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener"/>
10+
topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener"/>
1111
<listener class="software.aws.toolkits.jetbrains.services.cwc.editor.context.project.ProjectContextEditorListener"
1212
topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>
1313
</projectListeners>
@@ -70,6 +70,11 @@
7070
<keyboard-shortcut keymap="$default" first-keystroke="meta alt A" />
7171
</action>
7272

73+
<action id="aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction"
74+
class="software.aws.toolkits.jetbrains.services.cwc.commands.GenerateUnitTestsAction">
75+
<keyboard-shortcut keymap="$default" first-keystroke="meta alt T" />
76+
</action>
77+
7378
<action id="aws.toolkit.jetbrains.core.services.cwc.commands.SendToPromptAction"
7479
class="software.aws.toolkits.jetbrains.services.cwc.commands.SendToPromptAction">
7580
<keyboard-shortcut keymap="$default" first-keystroke="meta alt S" />

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/clients/chat/v1/ChatSessionV1.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ class ChatSessionV1(
297297
UserIntent.EXPLAIN_LINE_BY_LINE -> FollowUpType.LineByLine
298298
UserIntent.EXPLAIN_CODE_SELECTION -> FollowUpType.ExplainInDetail
299299
UserIntent.UNKNOWN_TO_SDK_VERSION -> FollowUpType.Generated
300+
UserIntent.GENERATE_UNIT_TESTS -> FollowUpType.Generated
300301
null -> FollowUpType.Generated
301302
}
302303

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ enum class EditorContextCommand(
2626
verb = "Optimize",
2727
actionId = "aws.amazonq.optimizeCode",
2828
),
29+
GenerateUnitTests(
30+
verb = "Generate unit tests for",
31+
actionId = "aws.amazonq.generateUnitTests",
32+
),
2933
SendToPrompt(
3034
verb = "SendToPrompt",
3135
actionId = "aws.amazonq.sendToPrompt",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.cwc.commands
5+
6+
import com.intellij.openapi.actionSystem.ActionUpdateThread
7+
import com.intellij.openapi.actionSystem.AnActionEvent
8+
import com.intellij.openapi.actionSystem.CommonDataKeys
9+
import software.aws.toolkits.jetbrains.core.credentials.AwsBearerTokenConnection
10+
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
11+
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
12+
13+
class GenerateUnitTestsAction : CustomAction(EditorContextCommand.GenerateUnitTests) {
14+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
15+
16+
override fun update(e: AnActionEvent) {
17+
val project = e.getData(CommonDataKeys.PROJECT) ?: return
18+
val connection = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) as? AwsBearerTokenConnection
19+
e.presentation.isEnabledAndVisible = connection?.startUrl == "https://amzn.awsapps.com/start"
20+
}
21+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class ChatController private constructor(
130130
var shouldAddIndexInProgressMessage: Boolean = false
131131
var shouldUseWorkspaceContext: Boolean = false
132132
val isDataCollectionGroup = CodeWhispererFeatureConfigService.getInstance().getIsDataCollectionEnabled()
133+
val startUrl = getStartUrl(context.project)
134+
133135
if (prompt.contains("@workspace")) {
134136
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
135137
shouldUseWorkspaceContext = true
@@ -151,7 +153,7 @@ class ChatController private constructor(
151153
triggerId = triggerId,
152154
message = prompt,
153155
activeFileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ChatMessage),
154-
userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage),
156+
userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage, startUrl),
155157
TriggerType.Click,
156158
projectContextQueryResult = queryResult,
157159
shouldAddIndexInProgressMessage = shouldAddIndexInProgressMessage,
@@ -332,7 +334,11 @@ class ChatController private constructor(
332334
}
333335

334336
// Create prompt
335-
val prompt = "${message.command} the following part of my code for me: $codeSelection"
337+
val prompt = if (EditorContextCommand.GenerateUnitTests == message.command) {
338+
"${message.command.verb} the following part of my code for me: $codeSelection"
339+
} else {
340+
"${message.command} the following part of my code for me: $codeSelection"
341+
}
336342

337343
processPromptActions(prompt, message, triggerId, fileContext)
338344
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
5858
UserIntent.CITE_SOURCES -> CwsprChatUserIntent.CiteSources
5959
UserIntent.EXPLAIN_LINE_BY_LINE -> CwsprChatUserIntent.ExplainLineByLine
6060
UserIntent.EXPLAIN_CODE_SELECTION -> CwsprChatUserIntent.ExplainCodeSelection
61+
UserIntent.GENERATE_UNIT_TESTS -> CwsprChatUserIntent.GenerateUnitTests
6162
UserIntent.UNKNOWN_TO_SDK_VERSION -> CwsprChatUserIntent.Unknown
6263
}
6364

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ class UserIntentRecognizer {
1616
EditorContextCommand.Fix -> UserIntent.APPLY_COMMON_BEST_PRACTICES
1717
EditorContextCommand.Optimize -> UserIntent.IMPROVE_CODE
1818
EditorContextCommand.ExplainCodeScanIssue -> UserIntent.EXPLAIN_CODE_SELECTION
19+
EditorContextCommand.GenerateUnitTests -> UserIntent.GENERATE_UNIT_TESTS
1920
EditorContextCommand.SendToPrompt -> null
2021
}
2122

22-
fun getUserIntentFromPromptChatMessage(prompt: String) = when {
23+
fun getUserIntentFromPromptChatMessage(prompt: String, startUrl: String?) = when {
2324
prompt.startsWith("Explain") -> UserIntent.EXPLAIN_CODE_SELECTION
2425
prompt.startsWith("Refactor") -> UserIntent.SUGGEST_ALTERNATE_IMPLEMENTATION
2526
prompt.startsWith("Fix") -> UserIntent.APPLY_COMMON_BEST_PRACTICES
2627
prompt.startsWith("Optimize") -> UserIntent.IMPROVE_CODE
28+
prompt.startsWith("Generate unit tests") && isInternalAmazonUser(startUrl) -> UserIntent.GENERATE_UNIT_TESTS
2729
else -> null
2830
}
2931

@@ -43,4 +45,6 @@ class UserIntentRecognizer {
4345
fun getUserIntentFromOnboardingPageInteraction(interaction: OnboardingPageInteraction) = when (interaction.type) {
4446
OnboardingPageInteractionType.CwcButtonClick -> null
4547
}
48+
49+
private fun isInternalAmazonUser(startUrl: String?): Boolean = startUrl == "https://amzn.awsapps.com/start"
4650
}

plugins/core/jetbrains-community/resources/telemetryOverride.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"showExample",
6060
"citeSources",
6161
"explainLineByLine",
62-
"explainCodeSelection"
62+
"explainCodeSelection",
63+
"generateUnitTests"
6364
],
6465
"description": "Explict user intent associated with a chat message"
6566
},

plugins/core/resources/resources/software/aws/toolkits/resources/MessagesBundle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.descri
2121
action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.text = Explain Code
2222
action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.description = Fixes the selected code
2323
action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.text = Fix Code
24+
action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.description = Generates unit tests for the selected code
25+
action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.text = Generate Tests (Beta)
2426
action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.description = Optimizes the selected code
2527
action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.text = Optimize Code
2628
action.aws.toolkit.jetbrains.core.services.cwc.commands.RefactorCodeAction.description = Refactors the selected code

plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,8 @@
19521952
"SHOW_EXAMPLES",
19531953
"CITE_SOURCES",
19541954
"EXPLAIN_LINE_BY_LINE",
1955-
"EXPLAIN_CODE_SELECTION"
1955+
"EXPLAIN_CODE_SELECTION",
1956+
"GENERATE_UNIT_TESTS"
19561957
]
19571958
},
19581959
"UserModificationEvent":{

0 commit comments

Comments
 (0)