Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat(amazonq): AI code gen % for Q features #5215

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package software.aws.toolkits.jetbrains.services.amazonqCodeScan.controller

import com.intellij.openapi.application.ApplicationManager
import software.aws.toolkits.jetbrains.services.amazonq.messages.MessagePublisher
import software.aws.toolkits.jetbrains.services.amazonqCodeScan.buildClearPromptProgressMessage
import software.aws.toolkits.jetbrains.services.amazonqCodeScan.buildPromptProgressMessage
Expand All @@ -11,6 +12,8 @@ import software.aws.toolkits.jetbrains.services.amazonqCodeScan.messages.CodeSca
import software.aws.toolkits.jetbrains.services.amazonqCodeScan.messages.CodeScanChatMessageContent
import software.aws.toolkits.jetbrains.services.amazonqCodeScan.messages.UpdatePlaceholderMessage
import software.aws.toolkits.jetbrains.services.amazonqCodeScan.storage.ChatSessionStorage
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.messages.ChatMessageType
import java.util.UUID

Expand All @@ -34,7 +37,8 @@ class CodeScanChatHelper(
clearPreviousItemButtons: Boolean? = false,
) {
if (isInValidSession()) return

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
messagePublisher.publish(
CodeScanChatMessage(
tabId = activeCodeScanTabId as String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.messages.sendA
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
import software.aws.toolkits.jetbrains.services.codewhisperer.language.CodeWhispererProgrammingLanguage
import software.aws.toolkits.jetbrains.services.codewhisperer.language.programmingLanguage
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.codewhisperer.toolwindow.CodeWhispererCodeReferenceManager
import software.aws.toolkits.jetbrains.services.cwc.ChatConstants
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
Expand Down Expand Up @@ -1181,6 +1183,8 @@ class CodeTestChatController(
"Processing message: $message " +
"tabId: $tabId"
}
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
when (session.conversationState) {
ConversationState.WAITING_FOR_BUILD_COMMAND_INPUT -> handleBuildCommandInput(session, message)
ConversationState.WAITING_FOR_REGENERATE_INPUT -> handleRegenerateInput(session, message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.intellij.diff.contents.EmptyContent
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.diff.util.DiffUserDataKeys
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Caret
Expand Down Expand Up @@ -74,6 +75,8 @@ import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.session.Delete
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.session.NewFileZipInfo
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.session.SessionStatePhase
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.CancellationTokenSource
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.controller.chat.telemetry.FeedbackComment
import software.aws.toolkits.jetbrains.services.cwc.controller.chat.telemetry.getStartUrl
import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
Expand Down Expand Up @@ -398,6 +401,8 @@ class DocController(
logger.debug { "$FEATURE_NAME: Processing InsertCodeAtCursorPosition: $message" }

withContext(EDT) {
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditor ?: return@withContext

val caret: Caret = editor.caretModel.primaryCaret
Expand All @@ -409,6 +414,8 @@ class DocController(
}
editor.document.insertString(offset, message.code)
}
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
}
}

Expand Down Expand Up @@ -823,7 +830,8 @@ class DocController(
is PrepareDocGenerationState -> state.filePaths
else -> emptyList()
}

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
processOpenDiff(
message = IncomingDocMessage.OpenDiff(tabId = tabId, filePath = filePaths[0].zipFilePath, deleted = false)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.diff.editor.ChainDiffVirtualFile
import com.intellij.diff.editor.DiffEditorTabFilesManager
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Caret
Expand Down Expand Up @@ -71,6 +72,8 @@ import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.session.Sessio
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.storage.ChatSessionStorage
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.InsertAction
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.getFollowUpOptions
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.codewhisperer.util.content
import software.aws.toolkits.jetbrains.services.cwc.controller.chat.telemetry.FeedbackComment
import software.aws.toolkits.jetbrains.services.cwc.controller.chat.telemetry.getStartUrl
Expand Down Expand Up @@ -191,6 +194,8 @@ class FeatureDevController(
logger.debug { "$FEATURE_NAME: Processing InsertCodeAtCursorPosition: $message" }

withContext(EDT) {
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditor ?: return@withContext

val caret: Caret = editor.caretModel.primaryCaret
Expand All @@ -202,6 +207,8 @@ class FeatureDevController(
}
editor.document.insertString(offset, message.code)
}
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
}
}

Expand Down Expand Up @@ -679,7 +686,8 @@ class FeatureDevController(
}

session.preloader(message, messenger)

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
when (session.sessionState.phase) {
SessionStatePhase.CODEGEN -> onCodeGeneration(session, message, tabId)
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import software.aws.toolkits.jetbrains.services.amazonq.project.ProjectContextCo
import software.aws.toolkits.jetbrains.services.amazonq.project.RelevantDocument
import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhispererConfigurable
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeWhispererUserModificationTracker
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.InboundAppMessagesHandler
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.exceptions.ChatApiException
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
Expand Down Expand Up @@ -215,6 +217,9 @@ class ChatController private constructor(
}

override suspend fun processInsertCodeAtCursorPosition(message: IncomingCwcMessage.InsertCodeAtCursorPosition) {

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
withContext(EDT) {
val editor: Editor = FileEditorManager.getInstance(context.project).selectedTextEditor ?: return@withContext

Expand Down Expand Up @@ -245,6 +250,9 @@ class ChatController private constructor(
}
}
telemetryHelper.recordInteractWithMessage(message)

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
}

override suspend fun processStopResponseMessage(message: IncomingCwcMessage.StopResponse) {
Expand Down Expand Up @@ -438,7 +446,8 @@ class ChatController private constructor(
sessionInfo.history.add(requestData)
telemetryHelper.recordEnterFocusConversation(tabId)
telemetryHelper.recordStartConversation(tabId, requestData)

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
// Send the request to the API and publish the responses back to the UI.
// This is launched in a scope attached to the sessionInfo so that the Job can be cancelled on a per-session basis.
ChatPromptHandler(telemetryHelper).handle(tabId, triggerId, requestData, sessionInfo, shouldAddIndexInProgressMessage)
Expand Down Expand Up @@ -558,5 +567,8 @@ class ChatController private constructor(
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)


}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package software.aws.toolkits.jetbrains.services.cwc.controller.chat.messenger

import com.intellij.openapi.application.ApplicationManager
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
Expand All @@ -13,6 +14,8 @@ import software.amazon.awssdk.awscore.exception.AwsServiceException
import software.amazon.awssdk.services.codewhispererstreaming.model.CodeWhispererStreamingException
import software.aws.toolkits.core.utils.convertMarkdownToHTML
import software.aws.toolkits.core.utils.extractCodeBlockLanguage
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.exceptions.ChatApiException
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatResponseEvent
Expand Down Expand Up @@ -115,6 +118,9 @@ class ChatPromptHandler(private val telemetryHelper: TelemetryHelper) {
)
telemetryHelper.recordAddMessage(data, response, responseText.length, statusCode, countTotalNumberOfCodeBlocks(responseText))
emit(response)

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
}
.catch { exception ->
val statusCode = if (exception is AwsServiceException) exception.statusCode() else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import software.aws.toolkits.jetbrains.services.amazonq.auth.AuthController
import software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AMAZON_Q_WINDOW_ID
import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretPosition
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.ChatRequestData
import software.aws.toolkits.jetbrains.services.cwc.clients.chat.model.TriggerType
import software.aws.toolkits.jetbrains.services.cwc.controller.ReferenceLogController
Expand Down Expand Up @@ -191,7 +193,6 @@ class InlineChatController(

private fun addPopupListeners(popup: JBPopup, editor: Editor) {
val popupListener = object : JBPopupListener {

override fun onClosed(event: LightweightWindowEvent) {
if (canPopupAbort.get() && event.asPopup().isDisposed) {
popupCancelHandler.invoke(editor)
Expand Down Expand Up @@ -534,6 +535,8 @@ class InlineChatController(
private fun insertString(editor: Editor, offset: Int, text: String): RangeMarker {
lateinit var rangeMarker: RangeMarker

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
ApplicationManager.getApplication().invokeAndWait {
CommandProcessor.getInstance().runUndoTransparentAction {
WriteCommandAction.runWriteCommandAction(project) {
Expand All @@ -543,18 +546,24 @@ class InlineChatController(
highlightCodeWithBackgroundColor(editor, rangeMarker.startOffset, rangeMarker.endOffset, true)
}
}

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
return rangeMarker
}

private fun replaceString(document: Document, start: Int, end: Int, text: String) {

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
ApplicationManager.getApplication().invokeAndWait {
CommandProcessor.getInstance().runUndoTransparentAction {
WriteCommandAction.runWriteCommandAction(project) {
document.replaceString(start, end, text)
}
}
}
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
}

private fun highlightString(editor: Editor, start: Int, end: Int, isInsert: Boolean) {
Expand Down Expand Up @@ -711,6 +720,9 @@ class InlineChatController(
canPopupAbort.set(true)
undoChanges()
}

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
return errorMessage
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package software.aws.toolkits.jetbrains.services.codemodernizer.controller

import com.intellij.ide.BrowserUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.fileChooser.FileChooser
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
Expand Down Expand Up @@ -110,6 +111,8 @@ import software.aws.toolkits.jetbrains.services.codemodernizer.utils.toVirtualFi
import software.aws.toolkits.jetbrains.services.codemodernizer.utils.tryGetJdk
import software.aws.toolkits.jetbrains.services.codemodernizer.utils.unzipFile
import software.aws.toolkits.jetbrains.services.codemodernizer.utils.validateSctMetadata
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.cwc.messages.ChatMessageType
import software.aws.toolkits.resources.message
import software.aws.toolkits.telemetry.CodeTransformVCSViewerSrcComponents
Expand All @@ -136,7 +139,8 @@ class CodeTransformChatController(
if (objective == "language upgrade" || objective == "sql conversion") {
telemetry.submitSelection(objective)
}

ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.INVOCATION)
when (objective) {
"language upgrade" -> this.handleLanguageUpgrade()
"sql conversion" -> this.handleSQLConversion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhisp
import software.aws.toolkits.jetbrains.services.codewhisperer.language.CodeWhispererProgrammingLanguage
import software.aws.toolkits.jetbrains.services.codewhisperer.language.programmingLanguage
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.CodeWhispererTelemetryService
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.QFeatureEvent
import software.aws.toolkits.jetbrains.services.codewhisperer.telemetry.UserWrittenCodeTracker.Companion.Q_FEATURE_TOPIC
import software.aws.toolkits.jetbrains.services.codewhisperer.toolwindow.CodeWhispererCodeReferenceManager
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.CODE_SCAN_ISSUE_TITLE_MAX_LENGTH
Expand Down Expand Up @@ -331,6 +333,8 @@ fun applySuggestedFix(project: Project, issue: CodeWhispererCodeScanIssue) {
try {
val manager = CodeWhispererCodeReferenceManager.getInstance(issue.project)
WriteCommandAction.runWriteCommandAction(issue.project) {
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.STARTS_EDITING)
val document = FileDocumentManager.getInstance().getDocument(issue.file) ?: return@runWriteCommandAction

val documentContent = document.text
Expand All @@ -343,6 +347,8 @@ fun applySuggestedFix(project: Project, issue: CodeWhispererCodeScanIssue) {
LOG.debug { "Original content from reference span: $originalContent" }
manager.addReferenceLogPanelEntry(reference = reference, null, null, originalContent.split("\n"))
}
ApplicationManager.getApplication().messageBus.syncPublisher(Q_FEATURE_TOPIC)
.onEvent(QFeatureEvent.FINISHES_EDITING)
}
if (issue.suggestedFixes[0].references.isNotEmpty()) {
manager.toolWindow?.show()
Expand Down
Loading
Loading