Skip to content

Commit

Permalink
Multi step chat (#91)
Browse files Browse the repository at this point in the history
* multi step first version

* regenerate chat

* improve code logic

* modify data map struct

* remove @repo entrance

* remove @repo entrance

* chat cancel improve

* code prediction api

* fix web view show

* method comment merge into main func

* fix some bug

* fix

* handle inner class

* Local recall in PsiElementUtils.

* fix check style

* Local recall in PsiElementUtils, exclude compiled class

* Local recall in PsiElementUtils, exclude compiled class

* NPE fix.

* NPE fix.

* fix web view show

* Context recall refine.

* Inner class resolve.

* Inner class resolve.

* Inner class resolve.

* Add filter psielements logic in recall.

* webview

* Class recall add interface impl search logic.

* Other language do not context recall temp.

* Other languages do not context recall temp.

* Fix function shortcut display.

* multi language

* remove @repo logic

* remove @repo logic

* Remove redundant relatedContext in code prediction.

* remove redundent

* improve cancel judge logic to avoid thread race

* use constant to maintain some value

* fix

* webview fix

* normal chat and smart chat

* change chat api from v1 to v2

* remove git repo and update web view

* filter large class

---------

Co-authored-by: maozhen <[email protected]>
  • Loading branch information
xiangtianyu and maozhen520 authored Oct 10, 2024
1 parent 4a61840 commit 9df7e77
Show file tree
Hide file tree
Showing 33 changed files with 1,533 additions and 463 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.zhongan.devpilot.actions.editor.popupmenu.PopupMenuEditorActionGroupUtil;
import com.zhongan.devpilot.listener.DevPilotFileEditorListener;
import com.zhongan.devpilot.network.DevPilotAvailabilityChecker;
import com.zhongan.devpilot.update.DevPilotUpdate;

Expand All @@ -13,7 +12,6 @@ public class DevPilotStartupActivity implements StartupActivity {
@Override
public void runActivity(@NotNull Project project) {
PopupMenuEditorActionGroupUtil.refreshActions(project);
DevPilotFileEditorListener.registerListener();

new DevPilotUpdate.DevPilotUpdateTask(project).queue();
new DevPilotAvailabilityChecker(project).checkNetworkAndLogStatus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;

import static com.zhongan.devpilot.constant.DefaultConst.GIT_COMMIT_PROMPT_VERSION;

public class GenerateGitCommitMessageAction extends AnAction {

private static final Logger log = Logger.getInstance(GenerateGitCommitMessageAction.class);
Expand Down Expand Up @@ -97,8 +99,8 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
if (editor != null) {
((EditorEx) editor).setCaretVisible(false);
DevPilotChatCompletionRequest devPilotChatCompletionRequest = new DevPilotChatCompletionRequest();
devPilotChatCompletionRequest.setVersion("V240801");
devPilotChatCompletionRequest.getMessages().add(MessageUtil.createPromptMessage("-1", "GENERATE_COMMIT", Map.of("locale", getLocale(), "diff", diff)));
devPilotChatCompletionRequest.setVersion(GIT_COMMIT_PROMPT_VERSION);
devPilotChatCompletionRequest.getMessages().add(MessageUtil.createPromptMessage(System.currentTimeMillis() + "", "GENERATE_COMMIT", Map.of("locale", getLocale(), "diff", diff)));
devPilotChatCompletionRequest.setStream(Boolean.FALSE);
var llmProvider = new LlmProviderFactory().getLlmProvider(project);
DevPilotChatCompletionResponse result = llmProvider.chatCompletionSync(devPilotChatCompletionRequest);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
import com.intellij.codeInsight.hints.presentation.PresentationFactory;
import com.intellij.codeInsight.hints.presentation.SequencePresentation;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.FileIndexFacade;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.PopupStep;
Expand Down Expand Up @@ -72,7 +66,7 @@ public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @
return true;
}

boolean isSourceCode = isSourceCode(editor);
boolean isSourceCode = isSourceCode(psiElement, editor);
var elementType = PsiUtilCore.getElementType(psiElement).toString();

if ("CLASS".equals(elementType)) {
Expand Down Expand Up @@ -174,14 +168,15 @@ private List<EditorActionEnum> buildInlayPresentationGroupData() {
return options;
}

public static boolean isSourceCode(Editor editor) {
public static boolean isSourceCode(PsiElement psiElement, Editor editor) {
Document document = editor.getDocument();
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file == null || !file.isWritable()) {
VirtualFile virtualFile = PsiUtilCore.getVirtualFile(psiElement);
if (virtualFile == null || !virtualFile.isWritable()) {
return false;
}
FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(file.getName());
return !fileType.isBinary() && !document.getText().trim().isEmpty();
FileIndexFacade indexFacade = FileIndexFacade.getInstance(psiElement.getProject());
FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(virtualFile.getName());
return !fileType.isBinary() && !document.getText().trim().isEmpty() && (!indexFacade.isInLibrarySource(virtualFile) && !indexFacade.isInLibraryClasses(virtualFile));
}

private InlayPresentation buildClickableInlayPresentation(String displayPrefixText, String displaySuffixText, EditorActionEnum actionEnum, PsiElement psiElement) {
Expand All @@ -197,13 +192,10 @@ private void handleActionCallback(EditorActionEnum actionEnum, PsiElement psiEle
if (EditorActionEnum.COMMENT_METHOD.equals(actionEnum)) {
ApplicationManager.getApplication().invokeLater(() -> {
moveCareToPreviousLineStart(editor, textRange.getStartOffset());
AnAction action = ActionManager.getInstance().getAction("com.zhongan.devpilot.actions.editor.generate.method.comments");
DataContext context = SimpleDataContext.getProjectContext(editor.getProject());
action.actionPerformed(new AnActionEvent(null, context, "", new Presentation(), ActionManager.getInstance(), 0));
});
} else {
service.handleActions(actionEnum, psiElement);
}

service.handleActions(actionEnum, psiElement);
}

private static int getAnchorOffset(@NotNull PsiElement psiElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@
import com.intellij.psi.PsiElement;
import com.zhongan.devpilot.actions.notifications.DevPilotNotification;
import com.zhongan.devpilot.constant.DefaultConst;
import com.zhongan.devpilot.constant.PromptConst;
import com.zhongan.devpilot.enums.EditorActionEnum;
import com.zhongan.devpilot.enums.SessionTypeEnum;
import com.zhongan.devpilot.enums.UtFrameTypeEnum;
import com.zhongan.devpilot.gui.toolwindows.chat.DevPilotChatToolWindowService;
import com.zhongan.devpilot.gui.toolwindows.components.EditorInfo;
import com.zhongan.devpilot.provider.ut.UtFrameworkProvider;
import com.zhongan.devpilot.provider.ut.UtFrameworkProviderFactory;
import com.zhongan.devpilot.provider.file.FileAnalyzeProviderFactory;
import com.zhongan.devpilot.settings.actionconfiguration.EditorActionConfigurationState;
import com.zhongan.devpilot.settings.state.DevPilotLlmSettingsState;
import com.zhongan.devpilot.settings.state.LanguageSettingsState;
import com.zhongan.devpilot.util.DevPilotMessageBundle;
import com.zhongan.devpilot.util.DocumentUtil;
import com.zhongan.devpilot.util.LanguageUtil;
import com.zhongan.devpilot.util.PsiElementUtils;
import com.zhongan.devpilot.util.PsiFileUtil;
import com.zhongan.devpilot.webview.model.CodeReferenceModel;
import com.zhongan.devpilot.webview.model.MessageModel;

Expand All @@ -41,14 +36,9 @@

import javax.swing.Icon;

import static com.zhongan.devpilot.constant.PlaceholderConst.ADDITIONAL_MOCK_PROMPT;
import static com.zhongan.devpilot.constant.PlaceholderConst.ANSWER_LANGUAGE;
import static com.zhongan.devpilot.constant.PlaceholderConst.CLASS_FULL_NAME;
import static com.zhongan.devpilot.constant.PlaceholderConst.LANGUAGE;
import static com.zhongan.devpilot.constant.PlaceholderConst.MOCK_FRAMEWORK;
import static com.zhongan.devpilot.constant.PlaceholderConst.RELATED_CLASS;
import static com.zhongan.devpilot.constant.PlaceholderConst.SELECTED_CODE;
import static com.zhongan.devpilot.constant.PlaceholderConst.TEST_FRAMEWORK;

public class PopupMenuEditorActionGroupUtil {

Expand Down Expand Up @@ -106,30 +96,8 @@ protected void actionPerformed(Project project, Editor editor, String selectedTe

EditorInfo editorInfo = new EditorInfo(editor);
if (editorActionEnum == EditorActionEnum.GENERATE_TESTS) {
if (language != null && language.isJvmPlatform()
&& PsiFileUtil.isCaretInWebClass(project, editor)) {
data.put(ADDITIONAL_MOCK_PROMPT, PromptConst.MOCK_WEB_MVC);
}
UtFrameworkProvider utFrameworkProvider = UtFrameworkProviderFactory.create(language);
if (utFrameworkProvider != null) {
UtFrameTypeEnum utFramework = utFrameworkProvider.getUTFramework(project, editor);
data.put(TEST_FRAMEWORK, utFramework.getUtFrameType());
data.put(MOCK_FRAMEWORK, utFramework.getMockFrameType());
}
if (language != null && "java".equalsIgnoreCase(language.getLanguageName())) {
if (psiElement != null) {
var relatedClass = PsiElementUtils.getRelatedClass(psiElement);
var fullClassName = PsiElementUtils.getFullClassName(psiElement);

if (relatedClass != null) {
data.put(RELATED_CLASS, relatedClass);
}

if (fullClassName != null) {
data.put(CLASS_FULL_NAME, fullClassName);
}
}
}
FileAnalyzeProviderFactory.getProvider(language == null ? null : language.getLanguageName())
.buildTestDataMap(project, editor, data);
}

if (LanguageSettingsState.getInstance().getLanguageIndex() == 1) {
Expand All @@ -151,10 +119,15 @@ protected void actionPerformed(Project project, Editor editor, String selectedTe
var codeMessage = MessageModel.buildCodeMessage(
UUID.randomUUID().toString(), System.currentTimeMillis(), showText, username, codeReferenceModel);

service.sendMessage(SessionTypeEnum.MULTI_TURN.getCode(), editorActionEnum.name(), data, null, callback, codeMessage);
FileAnalyzeProviderFactory.getProvider(language == null ? null : language.getLanguageName())
.buildChatDataMap(project, psiElement, codeReferenceModel, data);

service.chat(SessionTypeEnum.MULTI_TURN.getCode(), editorActionEnum.name(), data, null, callback, codeMessage);
}
};
group.add(action);
if (!label.equals(EditorActionEnum.COMMENT_METHOD.getLabel())) {
group.add(action);
}
});
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/zhongan/devpilot/constant/DefaultConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ private DefaultConst() {

public static final int COMPLETION_TRIGGER_INTERVAL = 1000;

public static final int CHAT_STEP_ONE = 1;

public static final int CHAT_STEP_TWO = 2;

public static final int CHAT_STEP_THREE = 3;

public static final String DEFAULT_PROMPT_VERSION = "V240923";

public static final String CODE_PREDICT_PROMPT_VERSION = "V240923";

public static final String GIT_COMMIT_PROMPT_VERSION = "V240923";

public static final int NORMAL_CHAT_TYPE = 1;

public static final int SMART_CHAT_TYPE = 2;

}
12 changes: 12 additions & 0 deletions src/main/java/com/zhongan/devpilot/enums/EditorActionEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ public static EditorActionEnum getEnumByLabel(String label) {
return null;
}

public static EditorActionEnum getEnumByName(String name) {
if (Objects.isNull(name)) {
return null;
}
for (EditorActionEnum type : EditorActionEnum.values()) {
if (type.name().equals(name)) {
return type;
}
}
return null;
}

public String getLabel() {
return label;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void registerJsCallJavaHandler(JBCefBrowser browser) {

var message = service.getUserContentCode(messageModel);
var userMessageModel = MessageModel.buildCodeMessage(uuid, time, message.getContent(), username, message.getCodeRef());
service.sendMessage(SessionTypeEnum.MULTI_TURN.getCode(), "PURE_CHAT", null, message.getContent(), null, userMessageModel);
service.chat(SessionTypeEnum.MULTI_TURN.getCode(), "PURE_CHAT", null, message.getContent(), null, userMessageModel);

return new JBCefJSQuery.Response("success");
}
Expand Down
Loading

0 comments on commit 9df7e77

Please sign in to comment.