From e6675828d087f0997b63e721976340f4ab606f8f Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 18 Jul 2023 21:53:31 +0800 Subject: [PATCH] feat: fix no prompter issues --- .../ide/idea/provider/JavaContextPrompter.kt | 2 +- idea/src/main/resources/cc.unitmesh.idea.xml | 5 ++- .../pycharm/provider/PythonContextPrompter.kt | 2 +- .../main/resources/cc.unitmesh.pycharm.xml | 5 ++- .../devti/actions/chat/ChatBaseAction.kt | 2 +- .../devti/gui/chat/ChatCodingComponent.kt | 2 +- .../editor/AbstractChatIntention.kt | 2 +- .../devti/provider/ContextPrompter.kt | 37 ++++++++++++++++--- .../provider/JavaScriptContextPrompter.kt | 2 +- .../main/resources/cc.unitmesh.webstorm.xml | 4 +- 10 files changed, 49 insertions(+), 14 deletions(-) diff --git a/idea/src/main/kotlin/cc/unitmesh/ide/idea/provider/JavaContextPrompter.kt b/idea/src/main/kotlin/cc/unitmesh/ide/idea/provider/JavaContextPrompter.kt index fd39e7cedf..e357849e2e 100644 --- a/idea/src/main/kotlin/cc/unitmesh/ide/idea/provider/JavaContextPrompter.kt +++ b/idea/src/main/kotlin/cc/unitmesh/ide/idea/provider/JavaContextPrompter.kt @@ -16,7 +16,7 @@ import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.psi.PsiFile import com.intellij.psi.PsiManager -class JavaContextPrompter : ContextPrompter { +class JavaContextPrompter : ContextPrompter() { private var additionContext: String = "" private val autoDevSettingsState = AutoDevSettingsState.getInstance() private var promptConfig: PromptConfig? = null diff --git a/idea/src/main/resources/cc.unitmesh.idea.xml b/idea/src/main/resources/cc.unitmesh.idea.xml index 9920379e3a..a76e7c0677 100644 --- a/idea/src/main/resources/cc.unitmesh.idea.xml +++ b/idea/src/main/resources/cc.unitmesh.idea.xml @@ -18,7 +18,10 @@ - + + diff --git a/pycharm/src/main/kotlin/cc/unitmesh/ide/pycharm/provider/PythonContextPrompter.kt b/pycharm/src/main/kotlin/cc/unitmesh/ide/pycharm/provider/PythonContextPrompter.kt index 8498966436..bc330ffe7d 100644 --- a/pycharm/src/main/kotlin/cc/unitmesh/ide/pycharm/provider/PythonContextPrompter.kt +++ b/pycharm/src/main/kotlin/cc/unitmesh/ide/pycharm/provider/PythonContextPrompter.kt @@ -7,7 +7,7 @@ import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.project.Project import com.intellij.psi.PsiFile -class PythonContextPrompter : ContextPrompter { +class PythonContextPrompter : ContextPrompter() { private var action: ChatBotActionType? = null private var selectedText: String = "" private var file: PsiFile? = null diff --git a/pycharm/src/main/resources/cc.unitmesh.pycharm.xml b/pycharm/src/main/resources/cc.unitmesh.pycharm.xml index d7e8f78a3c..724f2e5a8c 100644 --- a/pycharm/src/main/resources/cc.unitmesh.pycharm.xml +++ b/pycharm/src/main/resources/cc.unitmesh.pycharm.xml @@ -14,7 +14,10 @@ - + + diff --git a/src/main/kotlin/cc/unitmesh/devti/actions/chat/ChatBaseAction.kt b/src/main/kotlin/cc/unitmesh/devti/actions/chat/ChatBaseAction.kt index c588b39ed1..bf086cb341 100644 --- a/src/main/kotlin/cc/unitmesh/devti/actions/chat/ChatBaseAction.kt +++ b/src/main/kotlin/cc/unitmesh/devti/actions/chat/ChatBaseAction.kt @@ -45,7 +45,7 @@ abstract class ChatBaseAction : AnAction() { val actionType = chatCodingService.actionType - val prompter = ContextPrompter.prompter() + val prompter = ContextPrompter.prompter(file?.language?.displayName ?: "") prompter?.initContext(actionType, prefixText, file, project) toolWindowManager?.activate { diff --git a/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt b/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt index f4302aa0fb..0b2f43bccd 100644 --- a/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt +++ b/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingComponent.kt @@ -151,7 +151,7 @@ class ChatCodingComponent(private val chatCodingService: ChatCodingService) : JB val context = ChatContext(null, "", "") chatCodingService.actionType = ChatBotActionType.REFACTOR - chatCodingService.handlePromptAndResponse(this, object : ContextPrompter { + chatCodingService.handlePromptAndResponse(this, object : ContextPrompter() { override fun getUIPrompt() = prompt override fun getRequestPrompt() = prompt }, context) diff --git a/src/main/kotlin/cc/unitmesh/devti/intentions/editor/AbstractChatIntention.kt b/src/main/kotlin/cc/unitmesh/devti/intentions/editor/AbstractChatIntention.kt index 446e4b7fd6..c03f9c7989 100644 --- a/src/main/kotlin/cc/unitmesh/devti/intentions/editor/AbstractChatIntention.kt +++ b/src/main/kotlin/cc/unitmesh/devti/intentions/editor/AbstractChatIntention.kt @@ -58,7 +58,7 @@ abstract class AbstractChatIntention : IntentionAction { contentManager.removeAllContents(true) contentManager.addContent(content) toolWindowManager.activate { - val prompter = ContextPrompter.prompter() + val prompter = ContextPrompter.prompter(file?.language?.displayName ?: "") prompter?.initContext(actionType, selectedText, file, project) chatCodingService.handlePromptAndResponse(contentPanel, prompter!!) diff --git a/src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt b/src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt index afd1e700af..4310c0cf77 100644 --- a/src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt +++ b/src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt @@ -1,20 +1,47 @@ package cc.unitmesh.devti.provider import cc.unitmesh.devti.gui.chat.ChatBotActionType +import cc.unitmesh.devti.prompting.model.PromptConfig +import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.extensions.ExtensionPointName import com.intellij.openapi.project.Project import com.intellij.psi.PsiFile +import com.intellij.serviceContainer.LazyExtensionInstance +import com.intellij.util.xmlb.annotations.Attribute -interface ContextPrompter { - fun getUIPrompt(): String - fun getRequestPrompt(): String - fun initContext(actionType: ChatBotActionType, prefixText: String, file: PsiFile?, project: Project) {} +abstract class ContextPrompter : LazyExtensionInstance() { + @Attribute("language") + var language: String? = null + + @Attribute("implementationClass") + var implementationClass: String? = null + + override fun getImplementationClassName(): String? { + return implementationClass + } + + open fun getUIPrompt(): String = "" + open fun getRequestPrompt(): String = "" + open fun initContext(actionType: ChatBotActionType, prefixText: String, file: PsiFile?, project: Project) {} companion object { private val EP_NAME: ExtensionPointName = ExtensionPointName.create("cc.unitmesh.contextPrompter") - fun prompter(): ContextPrompter? = EP_NAME.extensionList.firstOrNull() + private val logger = Logger.getInstance(ContextPrompter::class.java) + + fun prompter(lang: String): ContextPrompter? { + val extensionList = EP_NAME.extensionList + val contextPrompter = extensionList.filter { + it.language?.lowercase() == lang.lowercase() + } + + return if (contextPrompter.isEmpty()) { + extensionList.first() + } else { + contextPrompter.first() + } + } } } diff --git a/webstorm/src/main/kotlin/cc/unitmesh/ide/webstorm/provider/JavaScriptContextPrompter.kt b/webstorm/src/main/kotlin/cc/unitmesh/ide/webstorm/provider/JavaScriptContextPrompter.kt index 1c485c202d..05804f9a5e 100644 --- a/webstorm/src/main/kotlin/cc/unitmesh/ide/webstorm/provider/JavaScriptContextPrompter.kt +++ b/webstorm/src/main/kotlin/cc/unitmesh/ide/webstorm/provider/JavaScriptContextPrompter.kt @@ -5,7 +5,7 @@ import cc.unitmesh.devti.provider.ContextPrompter import com.intellij.openapi.project.Project import com.intellij.psi.PsiFile -class JavaScriptContextPrompter : ContextPrompter { +class JavaScriptContextPrompter : ContextPrompter() { private var action: ChatBotActionType? = null private var selectedText: String = "" private var file: PsiFile? = null diff --git a/webstorm/src/main/resources/cc.unitmesh.webstorm.xml b/webstorm/src/main/resources/cc.unitmesh.webstorm.xml index a4dfaae90b..8986a9ee12 100644 --- a/webstorm/src/main/resources/cc.unitmesh.webstorm.xml +++ b/webstorm/src/main/resources/cc.unitmesh.webstorm.xml @@ -5,7 +5,9 @@ - +