Skip to content

Commit 76ca561

Browse files
committed
feat(bridge): add BridgeToolWindow and enhance SketchToolWindow #308
- Introduced a new `BridgeToolWindow` class for handling bridge actions. - Modified `SketchToolWindow` to support different `ChatActionType` and refactored internal structures. - Updated `SketchInputListener` to be more flexible and added setup method. - Extended `AutoDevToolWindowFactory` to create `BridgeToolWindow` alongside `SketchToolWindow`.
1 parent bb4be32 commit 76ca561

File tree

4 files changed

+89
-35
lines changed

4 files changed

+89
-35
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cc.unitmesh.devti.bridge
2+
3+
import cc.unitmesh.devti.gui.chat.message.ChatActionType
4+
import cc.unitmesh.devti.sketch.SketchInputListener
5+
import cc.unitmesh.devti.sketch.SketchToolWindow
6+
import com.intellij.openapi.application.invokeLater
7+
import com.intellij.openapi.editor.Editor
8+
import com.intellij.openapi.project.Project
9+
10+
class BridgeToolWindow(val myProject: Project, val myEditor: Editor?, private val showInput: Boolean = false) :
11+
SketchToolWindow(myProject, myEditor, showInput, ChatActionType.BRIDGE) {
12+
override val inputListener = object : SketchInputListener(project, chatCodingService, this) {
13+
14+
init {
15+
// no super
16+
val template = templateRender.getTemplate("bridge.vm")
17+
var systemPrompt = ""
18+
val customContext = BridgeRunContext.create(project, null, "")
19+
20+
systemPrompt = templateRender.renderTemplate(template, customContext)
21+
invokeLater {
22+
toolWindow.addSystemPrompt(systemPrompt)
23+
}
24+
}
25+
}
26+
}

core/src/main/kotlin/cc/unitmesh/devti/gui/AutoDevToolWindowFactory.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.gui
22

3+
import cc.unitmesh.devti.bridge.BridgeToolWindow
34
import cc.unitmesh.devti.gui.chat.message.ChatActionType
45
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
56
import cc.unitmesh.devti.gui.chat.ChatCodingService
@@ -41,6 +42,13 @@ class AutoDevToolWindowFactory : ToolWindowFactory, DumbAware {
4142
if (hasSketch == null) {
4243
createSketchToolWindow(project, toolWindow)
4344
}
45+
46+
val hasBridge =
47+
toolWindow.contentManager.component.components?.filterIsInstance<BridgeToolWindow>()?.firstOrNull()
48+
49+
if (hasBridge == null) {
50+
createBridgeToolWindow(project, toolWindow)
51+
}
4452
}
4553
}
4654

@@ -66,9 +74,15 @@ class AutoDevToolWindowFactory : ToolWindowFactory, DumbAware {
6674
}
6775

6876
fun createSketchToolWindow(project: Project, toolWindow: ToolWindow) {
69-
val sketchView = SketchToolWindow(project, null, true)
77+
val sketchView = SketchToolWindow(project, null, true, ChatActionType.SKETCH)
7078
val sketchPanel = ContentFactory.getInstance().createContent(sketchView, "Sketch", true)
7179
toolWindow.contentManager.addContent(sketchPanel)
7280
}
81+
82+
fun createBridgeToolWindow(project: Project, toolWindow: ToolWindow) {
83+
val sketchView = BridgeToolWindow(project, null, true)
84+
val sketchPanel = ContentFactory.getInstance().createContent(sketchView, "Bridge", true)
85+
toolWindow.contentManager.addContent(sketchPanel)
86+
}
7387
}
7488
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchInputListener.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ import com.intellij.openapi.Disposable
1515
import com.intellij.openapi.application.ApplicationManager
1616
import com.intellij.openapi.application.invokeLater
1717
import com.intellij.openapi.application.runReadAction
18+
import com.intellij.openapi.editor.ex.EditorEx
1819
import com.intellij.openapi.project.Project
1920
import kotlinx.coroutines.cancel
2021
import kotlinx.coroutines.flow.cancellable
2122
import kotlinx.coroutines.launch
2223

23-
class SketchInputListener(
24+
open class SketchInputListener(
2425
private val project: Project,
2526
private val chatCodingService: ChatCodingService,
26-
private val toolWindow: SketchToolWindow
27+
open val toolWindow: SketchToolWindow
2728
) : AutoDevInputListener, SimpleDevinPrompter(), Disposable {
2829
private val connection = ApplicationManager.getApplication().messageBus.connect(this)
2930

3031
override val template = templateRender.getTemplate("sketch.vm")
3132
override val templateRender: TemplateRender get() = TemplateRender(GENIUS_CODE)
3233
var systemPrompt = ""
3334

34-
init {
35+
fun setup() {
3536
systemPrompt = templateRender.renderTemplate(template, SketchRunContext.create(project, null, ""))
3637
toolWindow.addSystemPrompt(systemPrompt)
3738
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import com.intellij.openapi.actionSystem.ActionPlaces
2020
import com.intellij.openapi.actionSystem.ActionToolbar
2121
import com.intellij.openapi.actionSystem.AnAction
2222
import com.intellij.openapi.actionSystem.impl.ActionButton
23+
import com.intellij.openapi.application.ApplicationManager
24+
import com.intellij.openapi.application.ModalityState
2325
import com.intellij.openapi.application.runInEdt
2426
import com.intellij.openapi.editor.Editor
2527
import com.intellij.openapi.fileTypes.PlainTextLanguage
@@ -51,9 +53,15 @@ interface SketchProcessListener {
5153
fun onAfter() {}
5254
}
5355

54-
open class SketchToolWindow(val project: Project, val editor: Editor?, private val showInput: Boolean = false) :
56+
open class SketchToolWindow(
57+
val project: Project,
58+
val editor: Editor?,
59+
private val showInput: Boolean = false,
60+
chatActionType: ChatActionType = ChatActionType.SKETCH
61+
) :
5562
SimpleToolWindowPanel(true, true), NullableComponent, Disposable {
56-
private val chatCodingService = ChatCodingService(ChatActionType.SKETCH, project)
63+
open val chatCodingService = ChatCodingService(chatActionType, project)
64+
open val inputListener = SketchInputListener(project, chatCodingService, this)
5765
private var progressBar: CustomProgressBar = CustomProgressBar(this)
5866
private var inputSection: AutoDevInputSection = AutoDevInputSection(project, this, showAgent = false)
5967

@@ -66,19 +74,19 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
6674
this.isOpaque = true
6775
}
6876

69-
private var isUserScrolling: Boolean = false
77+
protected var isUserScrolling: Boolean = false
78+
protected var isInterrupted: Boolean = false
7079

71-
private var isInterrupted: Boolean = false
72-
73-
private var systemPrompt: JPanel = JPanel(BorderLayout())
74-
private var contentPanel = JPanel(BorderLayout())
80+
protected var systemPromptPanel: JPanel = JPanel(BorderLayout())
81+
protected var contentPanel = JPanel(BorderLayout())
7582

7683
val header = JButton(AllIcons.Actions.Copy).apply {
7784
this@apply.preferredSize = Dimension(32, 32)
7885

7986
addMouseListener(object : MouseAdapter() {
8087
override fun mouseClicked(e: MouseEvent?) {
81-
var allText = historyPanel.components.filterIsInstance<LangSketch>().joinToString("\n") { it.getViewText() }
88+
var allText =
89+
historyPanel.components.filterIsInstance<LangSketch>().joinToString("\n") { it.getViewText() }
8290
allText += myList.components.filterIsInstance<LangSketch>().joinToString("\n") { it.getViewText() }
8391

8492
val selection = StringSelection(allText)
@@ -88,8 +96,8 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
8896
})
8997
}
9098

91-
private var panelContent: DialogPanel = panel {
92-
row { cell(systemPrompt).fullWidth().fullHeight() }
99+
protected var panelContent: DialogPanel = panel {
100+
row { cell(systemPromptPanel).fullWidth().fullHeight() }
93101
row { cell(header).alignRight() }
94102
row { cell(historyPanel).fullWidth().fullHeight() }
95103
row { cell(myList).fullWidth().fullHeight() }
@@ -110,8 +118,6 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
110118

111119
var handleCancel: ((String) -> Unit)? = null
112120

113-
private val listener = SketchInputListener(project, chatCodingService, this)
114-
115121
private val processListeners = mutableListOf<SketchProcessListener>()
116122

117123
init {
@@ -150,26 +156,33 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
150156
contentPanel.add(progressBar, BorderLayout.SOUTH)
151157

152158
if (showInput) {
153-
inputSection.also {
154-
it.border = JBUI.Borders.empty(8)
155-
}
156-
157-
inputSection.addListener(listener)
158-
contentPanel.add(inputSection, BorderLayout.SOUTH)
159+
ApplicationManager.getApplication().invokeLater({
160+
setupListener()
161+
}, ModalityState.nonModal())
162+
}
159163

160-
addProcessListener(object : SketchProcessListener {
161-
override fun onBefore() {
162-
isInterrupted = false
163-
inputSection.showStopButton()
164-
}
164+
setContent(contentPanel)
165+
}
165166

166-
override fun onAfter() {
167-
inputSection.showSendButton()
168-
}
169-
})
167+
private fun setupListener() {
168+
inputSection.also {
169+
it.border = JBUI.Borders.empty(8)
170170
}
171171

172-
setContent(contentPanel)
172+
inputListener.setup()
173+
inputSection.addListener(inputListener)
174+
contentPanel.add(inputSection, BorderLayout.SOUTH)
175+
176+
addProcessListener(object : SketchProcessListener {
177+
override fun onBefore() {
178+
isInterrupted = false
179+
inputSection.showStopButton()
180+
}
181+
182+
override fun onAfter() {
183+
inputSection.showSendButton()
184+
}
185+
})
173186
}
174187

175188
fun onStart() {
@@ -226,7 +239,7 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
226239

227240
fun addSystemPrompt(text: String) {
228241
runInEdt {
229-
systemPrompt.add(createSingleTextView(text, language = "VTL"))
242+
systemPromptPanel.add(createSingleTextView(text, language = "VTL"))
230243
this.revalidate()
231244
this.repaint()
232245
}
@@ -331,7 +344,7 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
331344
AfterRun()
332345

333346
if (AutoSketchMode.getInstance(project).isEnable && !isInterrupted) {
334-
AutoSketchMode.getInstance(project).start(text, this@SketchToolWindow.listener)
347+
AutoSketchMode.getInstance(project).start(text, this@SketchToolWindow.inputListener)
335348
}
336349
}
337350

@@ -371,7 +384,7 @@ open class SketchToolWindow(val project: Project, val editor: Editor?, private v
371384
progressBar.isIndeterminate = false
372385
progressBar.isVisible = false
373386
blockViews.clear()
374-
systemPrompt.removeAll()
387+
systemPromptPanel.removeAll()
375388
myList.removeAll()
376389
historyPanel.removeAll()
377390
initializePreAllocatedBlocks(project)

0 commit comments

Comments
 (0)