generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): add AutoPageFlow and AutoPageTask #81
Add the `AutoPageFlow` and `AutoPageTask` classes to handle the generation of pages in the JavaScript code. These classes are responsible for clarifying the requirements and designing the pages based on the selected components. The `AutoPageFlow` class generates prompts for the user to clarify the requirements and design the pages, while the `AutoPageTask` class runs the flow in the background and updates the progress indicator.
- Loading branch information
Showing
8 changed files
with
128 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/AutoPage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/AutoPageFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cc.unitmesh.ide.javascript.flow | ||
|
||
import cc.unitmesh.devti.AutoDevBundle | ||
import cc.unitmesh.devti.flow.TaskFlow | ||
import cc.unitmesh.devti.gui.chat.ChatCodingPanel | ||
import cc.unitmesh.devti.llms.LLMProvider | ||
import cc.unitmesh.devti.template.TemplateRender | ||
import cc.unitmesh.ide.javascript.flow.model.AutoPageContext | ||
import cc.unitmesh.ide.javascript.flow.model.DsComponent | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class AutoPageFlow(val context: AutoPageContext, val panel: ChatCodingPanel, val llm: LLMProvider) : | ||
TaskFlow<String> { | ||
override fun clarify(): String { | ||
val stepOnePrompt = generateStepOnePrompt(context) | ||
|
||
panel.addMessage(stepOnePrompt, true, stepOnePrompt) | ||
panel.addMessage(AutoDevBundle.message("autodev.loading")) | ||
|
||
return runBlocking { | ||
val prompt = llm.stream(stepOnePrompt, "") | ||
return@runBlocking panel.updateMessage(prompt) | ||
} | ||
} | ||
|
||
private fun generateStepOnePrompt(context: AutoPageContext): String { | ||
val templateRender = TemplateRender("genius/page") | ||
val template = templateRender.getTemplate("page-gen-clarify.vm") | ||
|
||
templateRender.context = context | ||
|
||
val prompter = templateRender.renderTemplate(template) | ||
return prompter | ||
} | ||
|
||
|
||
override fun design(context: Any): List<String> { | ||
val componentList = context as List<DsComponent> | ||
val stepTwoPrompt = generateStepTwoPrompt(componentList) | ||
|
||
panel.addMessage(stepTwoPrompt, true, stepTwoPrompt) | ||
panel.addMessage(AutoDevBundle.message("autodev.loading")) | ||
|
||
return runBlocking { | ||
val prompt = llm.stream(stepTwoPrompt, "") | ||
return@runBlocking panel.updateMessage(prompt) | ||
}.let { listOf(it) } | ||
} | ||
|
||
private fun generateStepTwoPrompt(selectedComponents: List<DsComponent>): String { | ||
val templateRender = TemplateRender("genius/page") | ||
val template = templateRender.getTemplate("page-gen-design.vm") | ||
|
||
context.pages = selectedComponents.map { it.format() } | ||
templateRender.context = context | ||
|
||
val prompter = templateRender.renderTemplate(template) | ||
return prompter | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/AutoPageTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cc.unitmesh.ide.javascript.flow | ||
|
||
import cc.unitmesh.devti.AutoDevBundle | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.progress.ProgressIndicator | ||
import com.intellij.openapi.progress.Task | ||
import com.intellij.openapi.project.Project | ||
|
||
class AutoPageTask( | ||
private val project: Project, | ||
private val flow: AutoPageFlow, | ||
private val editor: Editor, | ||
private val autoPage: ReactAutoPage | ||
) : Task.Backgroundable(project, "Gen Page", true) { | ||
override fun run(indicator: ProgressIndicator) { | ||
indicator.fraction = 0.2 | ||
|
||
indicator.text = AutoDevBundle.message("autopage.generate.clarify") | ||
val components = flow.clarify() | ||
// tables will be list in string format, like: `[table1, table2]`, we need to parse to Lists | ||
val componentNames = components.substringAfter("[").substringBefore("]") | ||
.split(", ").map { it.trim() } | ||
|
||
val filterComponents = autoPage.filterComponents(componentNames) | ||
flow.context.components = filterComponents.map { it.format() } | ||
|
||
indicator.fraction = 0.6 | ||
indicator.text = AutoDevBundle.message("autopage.generate.design") | ||
flow.design(filterComponents) | ||
|
||
indicator.fraction = 0.8 | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/ReactAutoPage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/model/AutoPageContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cc.unitmesh.ide.javascript.flow.model | ||
|
||
import cc.unitmesh.ide.javascript.flow.ReactAutoPage | ||
|
||
data class AutoPageContext( | ||
val requirement: String, | ||
var pages: List<String>, | ||
val pageNames: List<String>, | ||
var components: List<String>, | ||
val componentNames: List<String>, | ||
val routes: List<String>, | ||
) { | ||
companion object { | ||
fun build(reactAutoPage: ReactAutoPage): AutoPageContext { | ||
return AutoPageContext( | ||
requirement = reactAutoPage.userTask, | ||
pages = reactAutoPage.getPages().map { it.format() }, | ||
pageNames = reactAutoPage.getPages().map { it.name }, | ||
components = reactAutoPage.getComponents().map { it.format() }, | ||
componentNames = reactAutoPage.getComponents().map { it.name }, | ||
routes = reactAutoPage.getRoutes(), | ||
) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...itmesh/ide/javascript/flow/DsComponent.kt → .../ide/javascript/flow/model/DsComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
javascript/src/main/kotlin/cc/unitmesh/ide/javascript/util/ReactPsiUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters