Skip to content

Commit

Permalink
feat: update for explain biz logic
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 6, 2023
1 parent 6723920 commit 95049b5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
Expand Up @@ -80,13 +80,15 @@ open class JavaContextPrompter : ContextPrompter() {

finalPrompt += "```$lang\n$selectedText\n```"

println("final prompt: $finalPrompt")
logger.info("final prompt: $finalPrompt")
return@runBlocking finalPrompt
}
}


private fun createPrompt(selectedText: String): String {
additionContext = ""
var prompt = action!!.instruction(lang)

when (action!!) {
Expand Down
Expand Up @@ -4,9 +4,9 @@ import cc.unitmesh.devti.getElementToAction
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.toolwindow.sendToChat
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ReadAction

class ExplainBusinessAction : ChatBaseAction() {
override fun getActionType(): ChatActionType {
Expand All @@ -18,19 +18,15 @@ class ExplainBusinessAction : ChatBaseAction() {
val caretModel = event.getData(CommonDataKeys.EDITOR)?.caretModel
val file = event.getData(CommonDataKeys.PSI_FILE)

val element = event.getData(CommonDataKeys.PSI_ELEMENT)
val actionType = getActionType()
val editor = event.getData(CommonDataKeys.EDITOR) ?: return
val elementToChat = getElementToAction(project, editor) ?: return

val prompter = ContextPrompter.prompter(file?.language?.displayName ?: "")
prompter.initContext(actionType, elementToChat.text, file, project, caretModel?.offset ?: 0, elementToChat)
val text = ReadAction.compute<String, Throwable> { elementToChat.text }
prompter.initContext(actionType, text, file, project, caretModel?.offset ?: 0, elementToChat)

sendToChat(project, actionType, prompter)
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
}

17 changes: 13 additions & 4 deletions src/main/kotlin/cc/unitmesh/devti/context/MethodContext.kt
Expand Up @@ -15,7 +15,7 @@ class MethodContext(
val returnType: String? = null,
val paramNames: List<String> = emptyList(),
val includeClassContext: Boolean = false,
val usages: List<PsiReference> = emptyList()
val usages: List<PsiReference> = emptyList(),
) : NamedElementContext(
root, text, name
) {
Expand All @@ -31,13 +31,22 @@ class MethodContext(
}

override fun toQuery(): String {
val query = """language: ${language ?: "_"}
val usageString = usages.joinToString("\n") {
val classFile = it.element.containingFile
val useText = it.element.text
"${classFile.name} -> $useText"
}

var query = """language: ${language ?: "_"}
fun name: ${name ?: "_"}
fun signature: ${signature ?: "_"}
$text"""
"""
if (usageString.isNotEmpty()) {
query += "usages: \n$usageString"
}

if (classContext != null) {
return query + classContext.toQuery()
query += classContext.toQuery()
}

return query
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatActionType.kt
Expand Up @@ -78,11 +78,12 @@ $diff
GEN_COMMIT_MESSAGE -> {
generateCommitMessage(prepareVcsContext())
}

CREATE_DDL -> "create ddl based on the given information"
CREATE_CHANGELOG -> "generate release note"
CHAT -> ""
CUSTOM_COMPLETE -> ""
EXPLAIN_BUSINESS -> "According the given code, return the business logic"
EXPLAIN_BUSINESS -> "Recover the original business scene and functionality of the provided code by describing it in a User Story format. Ensure that your explanation avoids any technical jargon or technology-related terms."
}
}
}
Expand Up @@ -122,7 +122,7 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp

setContent(panelContent)

inputSection.text = ""
// inputSection.text = ""

disposable?.whenDisposed(disposable) {
myList.removeAll()
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/provider/ContextPrompter.kt
Expand Up @@ -26,7 +26,6 @@ abstract class ContextPrompter : LazyExtensionInstance<ContextPrompter>() {
if (chatContextCache.containsKey(creationContext)) {
val cachedContent = chatContextCache[creationContext]!!
if (cachedContent.isNotEmpty()) {
logger<ContextPrompter>().info("use cache for $creationContext, content: $cachedContent")
return cachedContent
}
}
Expand All @@ -50,7 +49,7 @@ abstract class ContextPrompter : LazyExtensionInstance<ContextPrompter>() {
file: PsiFile?,
project: Project,
offset: Int,
element: PsiElement? = null
element: PsiElement? = null,
) {
this.action = actionType
this.selectedText = selectedText
Expand Down Expand Up @@ -102,7 +101,7 @@ abstract class ContextPrompter : LazyExtensionInstance<ContextPrompter>() {

private fun filterByLang(
extensionList: List<ContextPrompter>,
langLowercase: String
langLowercase: String,
): List<ContextPrompter> {
val contextPrompter = extensionList.filter {
it.language?.lowercase() == langLowercase
Expand Down
Expand Up @@ -10,8 +10,4 @@ data class ChatCreationContext(
val sourceFile: PsiFile?,
val extraItems: List<ChatContextItem> = emptyList(),
val element: PsiElement? = null
) {
override fun toString(): String {
return "ChatCreationContext(origin=$origin, action=$action, sourceFile=$sourceFile, extraItems=$extraItems, element=$element)"
}
}
)

0 comments on commit 95049b5

Please sign in to comment.