Skip to content

fix(amazonq): Fix "Explain" code scan issue #5712

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

Open
wants to merge 8 commits into
base: feature/q-lsp-chat
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.runBlocking
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.AsyncChatUiListener
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.FlareUiMessage
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.GENERIC_COMMAND
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.GenericCommandParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_TO_PROMPT
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SendToPromptParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TriggerType
import software.aws.toolkits.jetbrains.services.amazonq.messages.AmazonQMessage
Expand All @@ -36,23 +38,19 @@ class ActionRegistrar {
val fileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ContextMenu)
val codeSelection = "\n```\n${fileContext.focusAreaContext?.codeSelection?.trimIndent()?.trim()}\n```\n"
var uiMessage: FlareUiMessage? = null
if (command.verb != "sendToPrompt") {
if (command.verb != SEND_TO_PROMPT) {
val params = GenericCommandParams(selection = codeSelection, triggerType = TriggerType.CONTEXT_MENU, genericCommand = command.name)
uiMessage = FlareUiMessage(command = "genericCommand", params = params)
uiMessage = FlareUiMessage(command = GENERIC_COMMAND, params = params)
} else {
val params = SendToPromptParams(selection = codeSelection, triggerType = TriggerType.CONTEXT_MENU)
uiMessage = FlareUiMessage(command = "sendToPrompt", params = params)
uiMessage = FlareUiMessage(command = SEND_TO_PROMPT, params = params)
}
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
}
}
}
}

fun reportMessageClick(command: EditorContextCommand, issue: MutableMap<String, String>, project: Project) {
_messages.tryEmit(CodeScanIssueActionMessage(command, issue, project))
}

// provide singleton access
companion object {
val instance = ActionRegistrar()
Expand Down

This file was deleted.

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

package software.aws.toolkits.jetbrains.services.cwc.commands.codescan.actions

import software.aws.toolkits.jetbrains.services.cwc.commands.EditorContextCommand
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataKey
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAware
import kotlinx.coroutines.runBlocking
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.AsyncChatUiListener
import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.FlareUiMessage
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ChatPrompt
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SEND_TO_PROMPT
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.SendToPromptParams
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.TriggerType

class ExplainCodeIssueAction : CodeScanQActions(EditorContextCommand.ExplainCodeScanIssue)
class ExplainCodeIssueAction : AnAction(), DumbAware {
override fun actionPerformed(e: AnActionEvent) {
val issueDataKey = DataKey.create<MutableMap<String, String>>("amazonq.codescan.explainissue")
val issueContext = e.getData(issueDataKey) ?: return

ActionManager.getInstance().getAction("q.openchat").actionPerformed(e)

ApplicationManager.getApplication().executeOnPooledThread {
runBlocking {
// https://github.com/aws/aws-toolkit-vscode/blob/master/packages/amazonq/src/lsp/chat/commands.ts#L30
val codeSelection = "\n```\n${issueContext["code"]?.trimIndent()?.trim()}\n```\n"

val prompt = "Explain the issue \n\n " +
"Issue: \"${issueContext["title"]}\" \n" +
"Code: $codeSelection"

val modelPrompt = "Explain the issue ${issueContext["title"]} \n\n " +
"Issue: \"${issueContext["title"]}\" \n" +
"Description: ${issueContext["description"]} \n" +
"Code: $codeSelection and generate the code demonstrating the fix"

val params = SendToPromptParams(
selection = codeSelection,
triggerType = TriggerType.CONTEXT_MENU,
prompt = ChatPrompt(
prompt = prompt,
escapedPrompt = modelPrompt,
command = null
),
autoSubmit = true
)

val uiMessage = FlareUiMessage(SEND_TO_PROMPT, params)
AsyncChatUiListener.notifyPartialMessageUpdate(uiMessage)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import software.aws.toolkits.jetbrains.services.amazonq.lsp.flareChat.ContextCom
data class ChatPrompt(
val prompt: String,
val escapedPrompt: String,
val command: String,
val command: String?,
)

data class SendChatPromptRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const val DID_REMOVE_FILE = "aws/didRemoveFileOrDirectory"
const val DID_CREATE_DIRECTORY = "aws/didCreateDirectory"

const val GET_SERIALIZED_CHAT_REQUEST_METHOD = "aws/chat/getSerializedChat"
const val GENERIC_COMMAND = "genericCommand"

const val OPEN_FILE_DIFF = "aws/openFileDiff"
const val OPEN_SETTINGS = "openSettings"
Expand All @@ -47,4 +48,5 @@ const val PROMPT_INPUT_OPTIONS_CHANGE = "aws/chat/promptInputOptionChange"
const val SEND_CHAT_COMMAND_PROMPT = "aws/chat/sendChatPrompt"
const val SHOW_SAVE_FILE_DIALOG_REQUEST_METHOD = "aws/showSaveFileDialog"
const val STOP_CHAT_RESPONSE = "stopChatResponse"
const val SEND_TO_PROMPT = "sendToPrompt"
const val TELEMETRY_EVENT = "telemetry/event"