Skip to content

Commit

Permalink
feat(amazonq): Add command to generate unit tests for selected code (#…
Browse files Browse the repository at this point in the history
…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 (#4842)

* Added authType to loginWithBrowser metric (#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 (#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 (#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 (#4847)

* telemetry(amazonq): update telemetry

* remove unused import

---------

Co-authored-by: David Hasani <[email protected]>

* feat(amazonq): Reduce @workspace indexing time by 50% (#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]>
  • Loading branch information
7 people authored Oct 1, 2024
1 parent b88f7d8 commit 7ff4f29
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

<action id="aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction"
class="software.aws.toolkits.jetbrains.services.cwc.commands.GenerateUnitTestsAction">
<keyboard-shortcut keymap="$default" first-keystroke="meta alt T" />
</action>

<action id="aws.toolkit.jetbrains.core.services.cwc.commands.SendToPromptAction"
class="software.aws.toolkits.jetbrains.services.cwc.commands.SendToPromptAction">
<keyboard-shortcut keymap="$default" first-keystroke="meta alt S" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"showExample",
"citeSources",
"explainLineByLine",
"explainCodeSelection"
"explainCodeSelection",
"generateUnitTests"
],
"description": "Explict user intent associated with a chat message"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,8 @@
"SHOW_EXAMPLES",
"CITE_SOURCES",
"EXPLAIN_LINE_BY_LINE",
"EXPLAIN_CODE_SELECTION"
"EXPLAIN_CODE_SELECTION",
"GENERATE_UNIT_TESTS"
]
},
"UserModificationEvent":{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@
"SHOW_EXAMPLES",
"CITE_SOURCES",
"EXPLAIN_LINE_BY_LINE",
"EXPLAIN_CODE_SELECTION"
"EXPLAIN_CODE_SELECTION",
"GENERATE_UNIT_TESTS"
]
},
"ValidationException":{
Expand Down

0 comments on commit 7ff4f29

Please sign in to comment.