Skip to content

Commit

Permalink
refactor(provider): replace ContextPrompter with TextContextPrompter
Browse files Browse the repository at this point in the history
Introduce TextContextPrompter to simplify prompt handling across multiple files. Remove redundant anonymous ContextPrompter implementations and replace them with TextContextPrompter instances. Also, clean up related code and imports.
  • Loading branch information
phodal committed Mar 10, 2025
1 parent cbeec7b commit 3a33297
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class FixThisAction : ChatBaseAction() {

sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompt.displayText ?: ""
override fun requestPrompt(): String = prompt.requestText ?: ""
override fun displayPrompt(): String = prompt.displayText
override fun requestPrompt(): String = prompt.requestText
}, null, true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cc.unitmesh.devti.actions.run
import cc.unitmesh.devti.AutoDevNotifications
import cc.unitmesh.devti.gui.chat.message.ChatActionType
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import com.intellij.execution.ui.ConsoleView
import com.intellij.execution.ui.RunContentDescriptor
import com.intellij.execution.ui.RunContentManager
Expand All @@ -28,10 +28,7 @@ class RunPanelFixAction : AnAction() {
val content = sb.toString()
if (content.isNotEmpty()) {
sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = content
override fun requestPrompt(): String = content
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(content), null, true)
}
} else {
AutoDevNotifications.error(project, "Cannot extract text from run panel.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import cc.unitmesh.devti.gui.chat.view.FrontendCodeView
import cc.unitmesh.devti.gui.chat.view.MessageView
import cc.unitmesh.devti.gui.toolbar.NewChatAction
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import cc.unitmesh.devti.provider.devins.LanguageProcessor
import cc.unitmesh.devti.settings.AutoDevSettingsState
import cc.unitmesh.devti.settings.locale.LanguageChangedCallback.componentStateChanged
Expand All @@ -33,12 +34,10 @@ import com.intellij.temporary.gui.block.whenDisposed
import com.intellij.ui.Gray
import com.intellij.ui.JBColor
import com.intellij.ui.JBColor.PanelBackground
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.panels.VerticalLayout
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.panel
import com.intellij.util.ui.JBFont
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -105,18 +104,18 @@ class NormalChatCodingPanel(private val chatCodingService: ChatCodingService, va
return
}

val context = ChatContext(null, "", "")

val postProcessors = LanguageProcessor.devin()
if (postProcessors != null) {
prompt = postProcessors.compile(chatCodingService.project, prompt)
}

chatCodingService.actionType = ChatActionType.CHAT
chatCodingService.handlePromptAndResponse(this@NormalChatCodingPanel, object : ContextPrompter() {
override fun displayPrompt() = prompt
override fun requestPrompt() = prompt
}, context, false)
chatCodingService.handlePromptAndResponse(
this@NormalChatCodingPanel,
TextContextPrompter(prompt),
ChatContext(),
false
)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package cc.unitmesh.devti.gui.chat.message

data class ChatContext(
val postAction: ((response: String) -> Unit)? = null,
val prefixText: String,
val suffixText: String
val prefixText: String = "",
val suffixText: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cc.unitmesh.devti.provider

class TextContextPrompter(val prompt: String) : ContextPrompter() {
override fun displayPrompt(): String = prompt
override fun requestPrompt(): String = prompt
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import cc.unitmesh.devti.provider.context.ChatOrigin
import kotlinx.coroutines.runBlocking

class DefaultContextPrompter : ContextPrompter() {
override fun displayPrompt(): String {
return getPrompt()
}
override fun displayPrompt(): String = getPrompt()

override fun requestPrompt(): String {
return getPrompt()
}
override fun requestPrompt(): String = getPrompt()

private fun getPrompt(): String {
var additionContext: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.devti.gui.chat.message.ChatActionType
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.language.compiler.DevInsCompiledResult
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project

Expand Down Expand Up @@ -101,10 +102,7 @@ class DevInsConversationService(val project: Project) {

val finalPrompt = prompt.toString()
sendToChatWindow(project, ChatActionType.CHAT) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = finalPrompt
override fun requestPrompt(): String = finalPrompt
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(finalPrompt), null, true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import cc.unitmesh.devti.language.DevInLanguage
import cc.unitmesh.devti.language.compiler.DevInsCompiler
import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.language.psi.DevInVisitor
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import cc.unitmesh.devti.util.parser.CodeFence
import com.intellij.execution.process.ProcessEvent
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.PsiUtilBase
import com.intellij.openapi.fileEditor.FileEditorManager
import org.jetbrains.kotlin.psi.psiUtil.startOffset


@Service(Service.Level.PROJECT)
Expand Down Expand Up @@ -101,10 +101,7 @@ class DevInsProcessProcessor(val project: Project) {

if (result.hasError) {
sendToChatWindow(project, ChatActionType.CHAT) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = result.output
override fun requestPrompt(): String = result.output
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(result.output), null, true)
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cc.unitmesh.database.actions.base.SqlMigrationContext
import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
Expand All @@ -27,7 +27,6 @@ class GenerateEntityAction : ChatBaseIntention() {
return file.language is OraDialect
}


override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null) return
val selectedText = editor.selectionModel.selectedText ?: return
Expand All @@ -38,15 +37,10 @@ class GenerateEntityAction : ChatBaseIntention() {
lang = file.language.displayName,
sql = selectedText,
)
val prompter = templateRender.renderTemplate(template)

logger.info("Prompt: $prompter")

val prompt = templateRender.renderTemplate(template)
logger.info("Prompt: $prompt")
sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompter
override fun requestPrompt(): String = prompter
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(prompt), null, true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cc.unitmesh.database.actions.base.SqlMigrationContext
import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
Expand All @@ -25,9 +25,6 @@ class GenerateFunctionAction : ChatBaseIntention() {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
if (editor == null || file == null) return false
val isOracle = file.language is OraDialect
// val selectedText = editor.selectionModel
// val element = file.findElementAt(selectedText.selectionStart)

return isOracle
}

Expand All @@ -42,15 +39,12 @@ class GenerateFunctionAction : ChatBaseIntention() {
sql = selectedText,
)

val prompter = templateRender.renderTemplate(template)
val prompt = templateRender.renderTemplate(template)

logger.info("Prompt: $prompter")
logger.info("Prompt: $prompt")

sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompter
override fun requestPrompt(): String = prompter
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(prompt), null, true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cc.unitmesh.database.actions.base.SqlMigrationContext
import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.provider.TextContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
Expand Down Expand Up @@ -37,15 +37,12 @@ class GenerateUnittestAction : ChatBaseIntention() {
lang = file.language.displayName,
sql = selectedText,
)
val prompter = templateRender.renderTemplate(template)
val prompt = templateRender.renderTemplate(template)

logger.info("Prompt: $prompter")
logger.info("Prompt: $prompt")

sendToChatWindow(project, getActionType()) { panel, service ->
service.handlePromptAndResponse(panel, object : ContextPrompter() {
override fun displayPrompt(): String = prompter
override fun requestPrompt(): String = prompter
}, null, true)
service.handlePromptAndResponse(panel, TextContextPrompter(prompt), null, true)
}
}
}

0 comments on commit 3a33297

Please sign in to comment.