|
| 1 | +package com.github.uc4w6c.bedrockassistant.action; |
| 2 | + |
| 3 | +import com.github.uc4w6c.bedrockassistant.domain.AwsCredentials; |
| 4 | +import com.github.uc4w6c.bedrockassistant.domain.Message; |
| 5 | +import com.github.uc4w6c.bedrockassistant.domain.MessageRole; |
| 6 | +import com.github.uc4w6c.bedrockassistant.exceptions.BedrockException; |
| 7 | +import com.github.uc4w6c.bedrockassistant.exceptions.TokenException; |
| 8 | +import com.github.uc4w6c.bedrockassistant.helper.TokenHelper; |
| 9 | +import com.github.uc4w6c.bedrockassistant.popup.MfaPopupComponent; |
| 10 | +import com.github.uc4w6c.bedrockassistant.popup.PromptPopupComponent; |
| 11 | +import com.github.uc4w6c.bedrockassistant.repository.BedrockRepository; |
| 12 | +import com.github.uc4w6c.bedrockassistant.service.BedrockAssistantToolWindowService; |
| 13 | +import com.github.uc4w6c.bedrockassistant.state.BedrockAssistantCacheState; |
| 14 | +import com.github.uc4w6c.bedrockassistant.state.BedrockAssistantState; |
| 15 | +import com.github.uc4w6c.bedrockassistant.state.TokenManager; |
| 16 | +import com.github.uc4w6c.bedrockassistant.window.BedrockAssistantToolWindow; |
| 17 | +import com.intellij.notification.NotificationGroupManager; |
| 18 | +import com.intellij.notification.NotificationType; |
| 19 | +import com.intellij.openapi.actionSystem.AnAction; |
| 20 | +import com.intellij.openapi.actionSystem.AnActionEvent; |
| 21 | +import com.intellij.openapi.actionSystem.CommonDataKeys; |
| 22 | +import com.intellij.openapi.application.ApplicationManager; |
| 23 | +import com.intellij.openapi.command.WriteCommandAction; |
| 24 | +import com.intellij.openapi.editor.CaretModel; |
| 25 | +import com.intellij.openapi.editor.Document; |
| 26 | +import com.intellij.openapi.editor.Editor; |
| 27 | +import com.intellij.openapi.fileEditor.FileDocumentManager; |
| 28 | +import com.intellij.openapi.project.Project; |
| 29 | +import com.intellij.openapi.vfs.VirtualFile; |
| 30 | +import com.intellij.psi.PsiDocumentManager; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Optional; |
| 35 | +import java.util.function.Consumer; |
| 36 | +import java.util.function.Function; |
| 37 | +import java.util.regex.Matcher; |
| 38 | +import java.util.regex.Pattern; |
| 39 | + |
| 40 | +public class CodeGenerateAction extends AnAction { |
| 41 | + private final String GENERATE_CODE_PROMPT = """ |
| 42 | + The file name is %s. |
| 43 | + |
| 44 | + Here is the surrounding code context: |
| 45 | + ``` |
| 46 | + %s |
| 47 | + ``` |
| 48 | + |
| 49 | + Write the new code on line %d. |
| 50 | + |
| 51 | + Generate the code according to the instructions below. |
| 52 | + |
| 53 | + Return only the generated code inside triple backticks. Do not include any explanations, comments, or additional text. |
| 54 | + |
| 55 | + %s |
| 56 | + """; |
| 57 | + |
| 58 | + @Override |
| 59 | + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { |
| 60 | + Project project = anActionEvent.getProject(); |
| 61 | + if (project == null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR); |
| 66 | + if (editor == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + Consumer<String> submitActionListener = (promptText) -> { |
| 71 | + BedrockAssistantToolWindow bedrockAssistantToolWindow = BedrockAssistantToolWindowService.getInstance(project) |
| 72 | + .getBedrockAssistantToolWindow(); |
| 73 | + |
| 74 | + if (bedrockAssistantToolWindow == null) { |
| 75 | + throw new RuntimeException(); |
| 76 | + } |
| 77 | + BedrockAssistantState.State state = BedrockAssistantState.getInstance().getState(); |
| 78 | + |
| 79 | + TokenHelper tokenHelper = new TokenHelper(); |
| 80 | + Optional<AwsCredentials> optionalAwsCredentials = tokenHelper.getCredentials(project, state.profile); |
| 81 | + |
| 82 | + if (optionalAwsCredentials.isEmpty()) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument()); |
| 87 | + if (file == null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + String fileName = file.getName(); |
| 92 | + |
| 93 | + Document document = editor.getDocument(); |
| 94 | + |
| 95 | + int totalLines = document.getLineCount(); |
| 96 | + CaretModel caretModel = editor.getCaretModel(); |
| 97 | + int currentLine = document.getLineNumber(caretModel.getOffset()); |
| 98 | + |
| 99 | + int startLine = Math.max(0, currentLine - 100); |
| 100 | + int endLine = Math.min(totalLines - 1, currentLine + 100); |
| 101 | + |
| 102 | + int selectedLineIndex = currentLine - startLine + 1; |
| 103 | + |
| 104 | + int startOffset = document.getLineStartOffset(startLine); |
| 105 | + int endOffset = document.getLineEndOffset(endLine); |
| 106 | + |
| 107 | + String code = document.getText().substring(startOffset, endOffset); |
| 108 | + |
| 109 | + String context = String.format(GENERATE_CODE_PROMPT |
| 110 | + ,fileName |
| 111 | + ,code |
| 112 | + ,selectedLineIndex |
| 113 | + ,promptText); |
| 114 | + List<Message> messages = List.of(new Message(MessageRole.USER, context)); |
| 115 | + |
| 116 | + try { |
| 117 | + BedrockRepository bedrockRepository = new BedrockRepository(); |
| 118 | + String response = bedrockRepository.get(optionalAwsCredentials.get(), state.region, messages); |
| 119 | + |
| 120 | + Function<String, String> extractCodeBlock = (responseText) -> { |
| 121 | + Pattern pattern = Pattern.compile("```(.*?)```", Pattern.DOTALL); |
| 122 | + Matcher matcher = pattern.matcher(responseText); |
| 123 | + |
| 124 | + if (matcher.find()) { |
| 125 | + return matcher.group(1).trim(); |
| 126 | + } |
| 127 | + return responseText; |
| 128 | + }; |
| 129 | + |
| 130 | + int caretOffset = caretModel.getOffset(); |
| 131 | + int lineNumber = document.getLineNumber(caretOffset); |
| 132 | + |
| 133 | + if (lineNumber < 0) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + int lineEndOffset = document.getLineEndOffset(lineNumber); |
| 138 | + |
| 139 | + WriteCommandAction.runWriteCommandAction(project, () -> |
| 140 | + document.insertString(lineEndOffset, extractCodeBlock.apply(response))); |
| 141 | + |
| 142 | + PsiDocumentManager.getInstance(project).commitDocument(document); |
| 143 | + } catch (BedrockException e) { |
| 144 | + NotificationGroupManager.getInstance() |
| 145 | + .getNotificationGroup("BedrockAssistantBalloon") |
| 146 | + .createNotification(String.format(""" |
| 147 | + Bedrock call failed. |
| 148 | + %s |
| 149 | + """, e.getMessage()), |
| 150 | + NotificationType.ERROR) |
| 151 | + .notify(project); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + ApplicationManager.getApplication().invokeLater(() -> { |
| 156 | + new PromptPopupComponent.PromptPopupComponentBuilder() |
| 157 | + .submitActionListener(submitActionListener) |
| 158 | + .build(); |
| 159 | + }); |
| 160 | + } |
| 161 | +} |
0 commit comments