Skip to content

Commit fc67fb8

Browse files
committed
feat(mcp): add dynamic tool config listener to sketch
Subscribe to tool configuration changes and automatically update system prompts when selected tools change. Add message bus connection management and proper cleanup in dispose method.
1 parent 14630ef commit fc67fb8

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cc.unitmesh.devti.gui.chat.ui.AutoDevInputSection
77
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputTrigger
88
import cc.unitmesh.devti.llm2.model.LlmConfig
99
import cc.unitmesh.devti.llms.cancelHandler
10+
import cc.unitmesh.devti.mcp.ui.SketchConfigListener
1011
import cc.unitmesh.devti.observer.agent.AgentStateService
1112
import cc.unitmesh.devti.prompting.SimpleDevinPrompter
1213
import cc.unitmesh.devti.provider.devins.LanguageProcessor
@@ -17,8 +18,10 @@ import com.intellij.openapi.Disposable
1718
import com.intellij.openapi.application.ApplicationManager
1819
import com.intellij.openapi.diagnostic.logger
1920
import com.intellij.openapi.project.Project
21+
import com.intellij.util.messages.MessageBusConnection
2022
import kotlinx.coroutines.flow.cancellable
2123
import kotlinx.coroutines.launch
24+
import io.modelcontextprotocol.kotlin.sdk.Tool
2225

2326
open class SketchInputListener(
2427
private val project: Project,
@@ -32,12 +35,38 @@ open class SketchInputListener(
3235
open var systemPrompt = ""
3336
open var planPrompt = ""
3437
val planTemplate = templateRender.getTemplate("plan.vm")
38+
private var messageBusConnection: MessageBusConnection? = null
3539

3640
open suspend fun setup() {
3741
val customContext = SketchRunContext.create(project, null, "")
3842
systemPrompt = templateRender.renderTemplate(template, customContext)
3943
planPrompt = templateRender.renderTemplate(planTemplate, customContext)
4044
toolWindow.addSystemPrompt(systemPrompt)
45+
46+
// Subscribe to tool configuration changes
47+
setupToolConfigListener()
48+
}
49+
50+
private fun setupToolConfigListener() {
51+
messageBusConnection = ApplicationManager.getApplication().messageBus.connect()
52+
messageBusConnection?.subscribe(SketchConfigListener.TOPIC, object : SketchConfigListener {
53+
override fun onSelectedToolsChanged(tools: Map<String, Set<Tool>>) {
54+
AutoDevCoroutineScope.scope(project).launch {
55+
updateSystemPrompt()
56+
}
57+
}
58+
})
59+
}
60+
61+
private suspend fun updateSystemPrompt() {
62+
val customContext = SketchRunContext.create(project, null, "")
63+
val newSystemPrompt = templateRender.renderTemplate(template, customContext)
64+
val newPlanPrompt = templateRender.renderTemplate(planTemplate, customContext)
65+
66+
systemPrompt = newSystemPrompt
67+
planPrompt = newPlanPrompt
68+
69+
toolWindow.updateSystemPrompt(newSystemPrompt)
4170
}
4271

4372
override fun onStop(component: AutoDevInputSection) {
@@ -114,6 +143,8 @@ open class SketchInputListener(
114143

115144
override fun dispose() {
116145
connection.disconnect()
146+
messageBusConnection?.disconnect()
147+
messageBusConnection = null
117148
}
118149

119150
fun stop() {

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,16 @@ open class SketchToolWindow(
274274
}
275275
}
276276

277-
// fun clearSystemPromptPanel() {
278-
// systemPromptPanel.removeAll()
279-
// systemPromptPanel.revalidate()
280-
// systemPromptPanel.repaint()
281-
// }
277+
fun updateSystemPrompt(text: String) {
278+
runInEdt {
279+
systemPromptPanel.removeAll()
280+
systemPromptPanel.add(createSingleTextView(text, language = "VTL"))
281+
systemPromptPanel.revalidate()
282+
systemPromptPanel.repaint()
283+
this.revalidate()
284+
this.repaint()
285+
}
286+
}
282287

283288
fun updateHistoryPanel() {
284289
runInEdt {

0 commit comments

Comments
 (0)