Skip to content

Commit

Permalink
refactor(database): rename GenFlowContext to GenSqlContext #80
Browse files Browse the repository at this point in the history
This commit renames the `GenFlowContext` class to `GenSqlContext` in order to provide a more accurate and descriptive name for the class. The class represents the context for generating SQL scripts and contains properties such as requirement, database version, schema name, table names, and table information. The renaming improves code readability and maintainability.
  • Loading branch information
phodal committed Jan 24, 2024
1 parent 95b3272 commit a324699
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.unitmesh.database.actions

import cc.unitmesh.database.DbContextActionProvider
import cc.unitmesh.database.flow.GenFlowContext
import cc.unitmesh.database.flow.GenSqlContext
import cc.unitmesh.database.flow.GenSqlFlow
import cc.unitmesh.database.flow.GenSqlTask
import cc.unitmesh.devti.AutoDevBundle
Expand All @@ -11,15 +11,14 @@ import cc.unitmesh.devti.llms.LlmFactory
import com.intellij.database.model.ObjectKind
import com.intellij.database.psi.DbPsiFacade
import com.intellij.database.util.DasUtil
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile


class GenSqlScriptBySelection : AbstractChatIntention() {
class GenSqlAction : AbstractChatIntention() {
override fun priority(): Int = 1001
override fun startInWriteAction(): Boolean = false
override fun getFamilyName(): String = AutoDevBundle.message("migration.database.plsql")
Expand All @@ -30,8 +29,6 @@ class GenSqlScriptBySelection : AbstractChatIntention() {
return true
}

private val logger = logger<GenSqlScriptBySelection>()

override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (editor == null || file == null) return

Expand All @@ -48,7 +45,7 @@ class GenSqlScriptBySelection : AbstractChatIntention() {
tables.filter { table -> table.kind == ObjectKind.TABLE && table.dasParent?.name == schemaName }
}.toList()

val genFlowContext = GenFlowContext(
val genSqlContext = GenSqlContext(
requirement = selectedText ?: "",
databaseVersion = databaseVersion.let {
"name: ${it.name}, version: ${it.version}"
Expand All @@ -61,7 +58,7 @@ class GenSqlScriptBySelection : AbstractChatIntention() {

sendToChatPanel(project) { contentPanel, _ ->
val llmProvider = LlmFactory().create(project)
val prompter = GenSqlFlow(genFlowContext, actions, contentPanel, llmProvider, project)
val prompter = GenSqlFlow(genSqlContext, actions, contentPanel, llmProvider, project)

val task = GenSqlTask(project, prompter, editor)
ProgressManager.getInstance()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cc.unitmesh.database.flow

data class GenFlowContext(
data class GenSqlContext(
val requirement: String,
val databaseVersion: String,
val schemaName: String,
val tableNames: List<String>,
// for step 2
/**
* Step 2.
* A list of table names to retrieve the columns from.
*/
var tableInfos: List<String> = emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.intellij.openapi.project.Project
import kotlinx.coroutines.runBlocking

class GenSqlFlow(
val genFlowContext: GenFlowContext,
val genSqlContext: GenSqlContext,
val actions: DbContextActionProvider,
val ui: ChatCodingPanel,
val llm: LLMProvider,
Expand All @@ -20,7 +20,7 @@ class GenSqlFlow(
private val logger = logger<GenSqlFlow>()

fun clarify(): String {
val stepOnePrompt = generateStepOnePrompt(genFlowContext, actions)
val stepOnePrompt = generateStepOnePrompt(genSqlContext, actions)

LLMCoroutineScope.scope(project).runCatching {
ui.addMessage(stepOnePrompt, true, stepOnePrompt)
Expand All @@ -36,7 +36,7 @@ class GenSqlFlow(
}

fun generate(tableNames: List<String>): String {
val stepTwoPrompt = generateStepTwoPrompt(genFlowContext, actions, tableNames)
val stepTwoPrompt = generateStepTwoPrompt(genSqlContext, actions, tableNames)

LLMCoroutineScope.scope(project).runCatching {
ui.addMessage(stepTwoPrompt, true, stepTwoPrompt)
Expand All @@ -51,7 +51,7 @@ class GenSqlFlow(
}
}

private fun generateStepOnePrompt(context: GenFlowContext, actions: DbContextActionProvider): String {
private fun generateStepOnePrompt(context: GenSqlContext, actions: DbContextActionProvider): String {
val templateRender = TemplateRender("genius/sql")
val template = templateRender.getTemplate("sql-gen-clarify.vm")

Expand All @@ -65,16 +65,16 @@ class GenSqlFlow(
}

private fun generateStepTwoPrompt(
genFlowContext: GenFlowContext,
genSqlContext: GenSqlContext,
actions: DbContextActionProvider,
tableInfos: List<String>
): String {
val templateRender = TemplateRender("genius/sql")
val template = templateRender.getTemplate("sql-gen-design.vm")

genFlowContext.tableInfos = actions.getTableColumns(tableInfos)
genSqlContext.tableInfos = actions.getTableColumns(tableInfos)

templateRender.context = genFlowContext
templateRender.context = genSqlContext
templateRender.actions = actions

val prompter = templateRender.renderTemplate(template)
Expand Down
7 changes: 1 addition & 6 deletions exts/database/src/main/resources/cc.unitmesh.database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@
<categoryKey>intention.category.llm</categoryKey>
</autoDevIntention>
<autoDevIntention>
<className>cc.unitmesh.database.actions.GenSqlScriptBySelection</className>
<bundleName>messages.AutoDevBundle</bundleName>
<categoryKey>intention.category.llm</categoryKey>
</autoDevIntention>
<autoDevIntention>
<className>cc.unitmesh.database.actions.ModularDesignAction</className>
<className>cc.unitmesh.database.actions.GenSqlAction</className>
<bundleName>messages.AutoDevBundle</bundleName>
<categoryKey>intention.category.llm</categoryKey>
</autoDevIntention>
Expand Down

0 comments on commit a324699

Please sign in to comment.