Skip to content

Commit 20735ee

Browse files
committed
fix(genius): update SQL generation templates
The SQL generation templates in the `genius` package were updated to improve clarity and design. - Modified the `sql-gen-clarify.vm` template to include the phrase "User requirements" instead of just "requirements". - Modified the `sql-gen-design.vm` template to use Markdown syntax for SQL and removed the unnecessary explanation. These changes were made to enhance the user experience and make the generated SQL scripts more readable.
1 parent 0d2e6ce commit 20735ee

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

exts/database/src/main/kotlin/cc/unitmesh/database/actions/GenSqlScriptBySelection.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.intellij.database.model.ObjectKind
1212
import com.intellij.database.psi.DbPsiFacade
1313
import com.intellij.database.util.DasUtil
1414
import com.intellij.openapi.application.ApplicationManager
15-
import com.intellij.openapi.application.ReadAction
15+
import com.intellij.openapi.command.WriteCommandAction
1616
import com.intellij.openapi.diagnostic.logger
1717
import com.intellij.openapi.editor.Editor
1818
import com.intellij.openapi.progress.ProgressIndicator
@@ -69,34 +69,37 @@ class GenSqlScriptBySelection : AbstractChatIntention() {
6969
ApplicationManager.getApplication().invokeLater {
7070

7171
ProgressManager.getInstance()
72-
.run(generateSqlWorkflow(project, contentPanel, prompter))
72+
.run(generateSqlWorkflow(project, prompter, editor))
7373
}
7474
}
7575
}
7676

7777
private fun generateSqlWorkflow(
7878
project: Project,
79-
ui: ChatCodingPanel,
8079
flow: GenSqlFlow,
80+
editor: Editor,
8181
) =
8282
object : Task.Backgroundable(project, "Loading retained test failure", true) {
8383
override fun run(indicator: ProgressIndicator) {
8484
indicator.fraction = 0.2
8585

8686

8787
indicator.text = AutoDevBundle.message("migration.database.sql.generate.clarify")
88-
val tables = ReadAction.compute<String, Throwable> {
89-
flow.clarify()
90-
}
88+
val tables = flow.clarify()
9189

90+
logger.info("Tables: $tables")
9291
// tables will be list in string format, like: `[table1, table2]`, we need to parse to Lists
9392
val tableNames = tables.substringAfter("[").substringBefore("]")
9493
.split(", ").map { it.trim() }
9594

9695
indicator.fraction = 0.6
96+
indicator.text = AutoDevBundle.message("migration.database.sql.generate.generate")
9797
val sqlScript = flow.generate(tableNames)
9898

9999
logger.info("SQL Script: $sqlScript")
100+
WriteCommandAction.runWriteCommandAction(project, "Gen SQL", "cc.unitmesh.livingDoc", {
101+
editor.document.insertString(editor.caretModel.offset, sqlScript)
102+
})
100103

101104
indicator.fraction = 1.0
102105
}
@@ -113,9 +116,13 @@ class GenSqlFlow(
113116

114117
fun clarify(): String {
115118
val stepOnePrompt = generateStepOnePrompt(dbContext, actions)
116-
ui.addMessage(stepOnePrompt, true, stepOnePrompt)
117-
// for answer
118-
ui.addMessage(AutoDevBundle.message("autodev.loading"))
119+
try {
120+
ui.addMessage(stepOnePrompt, true, stepOnePrompt)
121+
// for answer
122+
ui.addMessage(AutoDevBundle.message("autodev.loading"))
123+
} catch (e: Exception) {
124+
logger.error("Error: $e")
125+
}
119126

120127
return runBlocking {
121128
val prompt = llm.stream(stepOnePrompt, "")
@@ -125,17 +132,20 @@ class GenSqlFlow(
125132

126133
fun generate(tableNames: List<String>): String {
127134
val stepTwoPrompt = generateStepTwoPrompt(dbContext, actions, tableNames)
128-
ui.addMessage(stepTwoPrompt, true, stepTwoPrompt)
129-
// for answer
130-
ui.addMessage(AutoDevBundle.message("autodev.loading"))
135+
try {
136+
ui.addMessage(stepTwoPrompt, true, stepTwoPrompt)
137+
// for answer
138+
ui.addMessage(AutoDevBundle.message("autodev.loading"))
139+
} catch (e: Exception) {
140+
logger.error("Error: $e")
141+
}
131142

132143
return runBlocking {
133144
val prompt = llm.stream(stepTwoPrompt, "")
134145
return@runBlocking ui.updateMessage(prompt)
135146
}
136147
}
137148

138-
139149
private fun generateStepOnePrompt(context: DbContext, actions: DbContextActionProvider): String {
140150
val templateRender = TemplateRender("genius/sql")
141151
val template = templateRender.getTemplate("sql-gen-clarify.vm")
@@ -155,7 +165,7 @@ class GenSqlFlow(
155165
tableInfos: List<String>
156166
): String {
157167
val templateRender = TemplateRender("genius/sql")
158-
val template = templateRender.getTemplate("sql-gen-generate.vm")
168+
val template = templateRender.getTemplate("sql-gen-design.vm")
159169

160170
dbContext.tableInfos = actions.getTableColumns(tableInfos)
161171

src/main/resources/genius/sql/sql-gen-clarify.vm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ According to the user's requirements, you should choose the best Tables for the
88
For example:
99

1010
- Question(requirements): calculate the average trip length by subscriber type.// User tables: trips, users, subscriber_type
11-
- Answer: [trips, subscriber_type]
11+
- You should anwser: [trips, subscriber_type]
1212

1313
----
1414

15-
Here are the requirements:
15+
Here are the User requirements:
1616

17-
```
17+
```markdown
1818
${context.requirement}
19-
```
19+
```markdown
2020

2121
Please choose the best Tables for the user, just return the table names in a list, no explain.

src/main/resources/genius/sql/sql-gen-design.vm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ select average_trip_length from subscriber_type where subscriber_type = 'subscri
1818

1919
Here are the requirements:
2020

21-
```
21+
```markdown
2222
${context.requirement}
2323
```
2424

25-
Please write your SQL here:
25+
Please write your SQL with Markdown syntax, no explanation is needed. :
2626

27-
```sql

0 commit comments

Comments
 (0)