Skip to content

Commit d687152

Browse files
committed
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.
1 parent aa61744 commit d687152

File tree

8 files changed

+128
-110
lines changed

8 files changed

+128
-110
lines changed
Lines changed: 5 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package cc.unitmesh.ide.javascript.actions
22

33
import cc.unitmesh.devti.AutoDevBundle
4-
import cc.unitmesh.devti.flow.TaskFlow
5-
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
64
import cc.unitmesh.devti.gui.sendToChatPanel
75
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
8-
import cc.unitmesh.devti.llms.LLMProvider
96
import cc.unitmesh.devti.llms.LlmFactory
10-
import cc.unitmesh.devti.template.TemplateRender
11-
import cc.unitmesh.ide.javascript.flow.DsComponent
7+
import cc.unitmesh.ide.javascript.flow.model.AutoPageContext
8+
import cc.unitmesh.ide.javascript.flow.AutoPageFlow
9+
import cc.unitmesh.ide.javascript.flow.AutoPageTask
1210
import cc.unitmesh.ide.javascript.flow.ReactAutoPage
1311
import cc.unitmesh.ide.javascript.util.LanguageApplicableUtil
1412
import com.intellij.openapi.editor.Editor
15-
import com.intellij.openapi.progress.ProgressIndicator
1613
import com.intellij.openapi.progress.ProgressManager
17-
import com.intellij.openapi.progress.Task
1814
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
1915
import com.intellij.openapi.project.Project
2016
import com.intellij.psi.PsiFile
21-
import kotlinx.coroutines.runBlocking
2217

2318
class AutoPageAction : ChatBaseIntention() {
2419
override fun priority(): Int = 1010
@@ -39,111 +34,13 @@ class AutoPageAction : ChatBaseIntention() {
3934
sendToChatPanel(project) { contentPanel, _ ->
4035
val llmProvider = LlmFactory().create(project)
4136
val context = AutoPageContext.build(reactAutoPage)
42-
val prompter = GenComponentFlow(context, contentPanel, llmProvider)
37+
val prompter = AutoPageFlow(context, contentPanel, llmProvider)
4338

44-
val task = GenComponentTask(project, prompter, editor, reactAutoPage)
39+
val task = AutoPageTask(project, prompter, editor, reactAutoPage)
4540
ProgressManager.getInstance()
4641
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
4742
}
4843

4944
}
5045
}
5146

52-
class GenComponentTask(
53-
private val project: Project,
54-
private val flow: GenComponentFlow,
55-
private val editor: Editor,
56-
private val autoPage: ReactAutoPage
57-
) : Task.Backgroundable(project, "Gen Page", true) {
58-
override fun run(indicator: ProgressIndicator) {
59-
indicator.fraction = 0.2
60-
61-
indicator.text = AutoDevBundle.message("autopage.generate.clarify")
62-
val components = flow.clarify()
63-
// tables will be list in string format, like: `[table1, table2]`, we need to parse to Lists
64-
val componentNames = components.substringAfter("[").substringBefore("]")
65-
.split(", ").map { it.trim() }
66-
67-
val filterComponents = autoPage.filterComponents(componentNames)
68-
flow.context.components = filterComponents.map { it.format() }
69-
70-
indicator.fraction = 0.6
71-
indicator.text = AutoDevBundle.message("autopage.generate.design")
72-
flow.design(filterComponents)
73-
74-
indicator.fraction = 0.8
75-
76-
}
77-
}
78-
79-
data class AutoPageContext(
80-
val requirement: String,
81-
var pages: List<String>,
82-
val pageNames: List<String>,
83-
var components: List<String>,
84-
val componentNames: List<String>,
85-
val routes: List<String>,
86-
) {
87-
companion object {
88-
fun build(reactAutoPage: ReactAutoPage): AutoPageContext {
89-
return AutoPageContext(
90-
requirement = reactAutoPage.userTask,
91-
pages = reactAutoPage.getPages().map { it.format() },
92-
pageNames = reactAutoPage.getPages().map { it.name },
93-
components = reactAutoPage.getComponents().map { it.format() },
94-
componentNames = reactAutoPage.getComponents().map { it.name },
95-
routes = reactAutoPage.getRoutes(),
96-
)
97-
}
98-
}
99-
}
100-
101-
class GenComponentFlow(val context: AutoPageContext, val panel: ChatCodingPanel, val llm: LLMProvider) :
102-
TaskFlow<String> {
103-
override fun clarify(): String {
104-
val stepOnePrompt = generateStepOnePrompt(context)
105-
106-
panel.addMessage(stepOnePrompt, true, stepOnePrompt)
107-
panel.addMessage(AutoDevBundle.message("autodev.loading"))
108-
109-
return runBlocking {
110-
val prompt = llm.stream(stepOnePrompt, "")
111-
return@runBlocking panel.updateMessage(prompt)
112-
}
113-
}
114-
115-
private fun generateStepOnePrompt(context: AutoPageContext): String {
116-
val templateRender = TemplateRender("genius/page")
117-
val template = templateRender.getTemplate("page-gen-clarify.vm")
118-
119-
templateRender.context = context
120-
121-
val prompter = templateRender.renderTemplate(template)
122-
return prompter
123-
}
124-
125-
126-
override fun design(context: Any): List<String> {
127-
val componentList = context as List<DsComponent>
128-
val stepTwoPrompt = generateStepTwoPrompt(componentList)
129-
130-
panel.addMessage(stepTwoPrompt, true, stepTwoPrompt)
131-
panel.addMessage(AutoDevBundle.message("autodev.loading"))
132-
133-
return runBlocking {
134-
val prompt = llm.stream(stepTwoPrompt, "")
135-
return@runBlocking panel.updateMessage(prompt)
136-
}.let { listOf(it) }
137-
}
138-
139-
private fun generateStepTwoPrompt(selectedComponents: List<DsComponent>): String {
140-
val templateRender = TemplateRender("genius/page")
141-
val template = templateRender.getTemplate("page-gen-design.vm")
142-
143-
context.pages = selectedComponents.map { it.format() }
144-
templateRender.context = context
145-
146-
val prompter = templateRender.renderTemplate(template)
147-
return prompter
148-
}
149-
}

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/AutoPage.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.unitmesh.ide.javascript.flow
22

33
import cc.unitmesh.devti.flow.TaskFlow
4+
import cc.unitmesh.ide.javascript.flow.model.DsComponent
45

56
/**
67
* FrontendFlow is an interface that represents the flow of tasks in a frontend application.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cc.unitmesh.ide.javascript.flow
2+
3+
import cc.unitmesh.devti.AutoDevBundle
4+
import cc.unitmesh.devti.flow.TaskFlow
5+
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
6+
import cc.unitmesh.devti.llms.LLMProvider
7+
import cc.unitmesh.devti.template.TemplateRender
8+
import cc.unitmesh.ide.javascript.flow.model.AutoPageContext
9+
import cc.unitmesh.ide.javascript.flow.model.DsComponent
10+
import kotlinx.coroutines.runBlocking
11+
12+
class AutoPageFlow(val context: AutoPageContext, val panel: ChatCodingPanel, val llm: LLMProvider) :
13+
TaskFlow<String> {
14+
override fun clarify(): String {
15+
val stepOnePrompt = generateStepOnePrompt(context)
16+
17+
panel.addMessage(stepOnePrompt, true, stepOnePrompt)
18+
panel.addMessage(AutoDevBundle.message("autodev.loading"))
19+
20+
return runBlocking {
21+
val prompt = llm.stream(stepOnePrompt, "")
22+
return@runBlocking panel.updateMessage(prompt)
23+
}
24+
}
25+
26+
private fun generateStepOnePrompt(context: AutoPageContext): String {
27+
val templateRender = TemplateRender("genius/page")
28+
val template = templateRender.getTemplate("page-gen-clarify.vm")
29+
30+
templateRender.context = context
31+
32+
val prompter = templateRender.renderTemplate(template)
33+
return prompter
34+
}
35+
36+
37+
override fun design(context: Any): List<String> {
38+
val componentList = context as List<DsComponent>
39+
val stepTwoPrompt = generateStepTwoPrompt(componentList)
40+
41+
panel.addMessage(stepTwoPrompt, true, stepTwoPrompt)
42+
panel.addMessage(AutoDevBundle.message("autodev.loading"))
43+
44+
return runBlocking {
45+
val prompt = llm.stream(stepTwoPrompt, "")
46+
return@runBlocking panel.updateMessage(prompt)
47+
}.let { listOf(it) }
48+
}
49+
50+
private fun generateStepTwoPrompt(selectedComponents: List<DsComponent>): String {
51+
val templateRender = TemplateRender("genius/page")
52+
val template = templateRender.getTemplate("page-gen-design.vm")
53+
54+
context.pages = selectedComponents.map { it.format() }
55+
templateRender.context = context
56+
57+
val prompter = templateRender.renderTemplate(template)
58+
return prompter
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cc.unitmesh.ide.javascript.flow
2+
3+
import cc.unitmesh.devti.AutoDevBundle
4+
import com.intellij.openapi.editor.Editor
5+
import com.intellij.openapi.progress.ProgressIndicator
6+
import com.intellij.openapi.progress.Task
7+
import com.intellij.openapi.project.Project
8+
9+
class AutoPageTask(
10+
private val project: Project,
11+
private val flow: AutoPageFlow,
12+
private val editor: Editor,
13+
private val autoPage: ReactAutoPage
14+
) : Task.Backgroundable(project, "Gen Page", true) {
15+
override fun run(indicator: ProgressIndicator) {
16+
indicator.fraction = 0.2
17+
18+
indicator.text = AutoDevBundle.message("autopage.generate.clarify")
19+
val components = flow.clarify()
20+
// tables will be list in string format, like: `[table1, table2]`, we need to parse to Lists
21+
val componentNames = components.substringAfter("[").substringBefore("]")
22+
.split(", ").map { it.trim() }
23+
24+
val filterComponents = autoPage.filterComponents(componentNames)
25+
flow.context.components = filterComponents.map { it.format() }
26+
27+
indicator.fraction = 0.6
28+
indicator.text = AutoDevBundle.message("autopage.generate.design")
29+
flow.design(filterComponents)
30+
31+
indicator.fraction = 0.8
32+
33+
}
34+
}

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/ReactAutoPage.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.ide.javascript.flow
22

3+
import cc.unitmesh.ide.javascript.flow.model.DsComponent
34
import cc.unitmesh.ide.javascript.util.ReactPsiUtil
45
import com.intellij.lang.ecmascript6.JSXHarmonyFileType
56
import com.intellij.lang.javascript.JavaScriptFileType
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cc.unitmesh.ide.javascript.flow.model
2+
3+
import cc.unitmesh.ide.javascript.flow.ReactAutoPage
4+
5+
data class AutoPageContext(
6+
val requirement: String,
7+
var pages: List<String>,
8+
val pageNames: List<String>,
9+
var components: List<String>,
10+
val componentNames: List<String>,
11+
val routes: List<String>,
12+
) {
13+
companion object {
14+
fun build(reactAutoPage: ReactAutoPage): AutoPageContext {
15+
return AutoPageContext(
16+
requirement = reactAutoPage.userTask,
17+
pages = reactAutoPage.getPages().map { it.format() },
18+
pageNames = reactAutoPage.getPages().map { it.name },
19+
components = reactAutoPage.getComponents().map { it.format() },
20+
componentNames = reactAutoPage.getComponents().map { it.name },
21+
routes = reactAutoPage.getRoutes(),
22+
)
23+
}
24+
}
25+
}

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/DsComponent.kt renamed to javascript/src/main/kotlin/cc/unitmesh/ide/javascript/flow/model/DsComponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.ide.javascript.flow
1+
package cc.unitmesh.ide.javascript.flow.model
22

33
import kotlinx.serialization.Serializable
44

javascript/src/main/kotlin/cc/unitmesh/ide/javascript/util/ReactPsiUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cc.unitmesh.ide.javascript.util
22

3-
import cc.unitmesh.ide.javascript.flow.DsComponent
3+
import cc.unitmesh.ide.javascript.flow.model.DsComponent
44
import com.intellij.lang.ecmascript6.psi.ES6ExportDeclaration
55
import com.intellij.lang.ecmascript6.psi.ES6ExportDefaultAssignment
66
import com.intellij.lang.javascript.presentable.JSFormatUtil

0 commit comments

Comments
 (0)