1
1
package cc.unitmesh.devti.mcp.editor
2
2
3
3
import cc.unitmesh.devti.llm2.model.LlmConfig
4
+ import cc.unitmesh.devti.llms.LlmFactory
5
+ import cc.unitmesh.devti.llms.custom.CustomLLMProvider
4
6
import cc.unitmesh.devti.mcp.client.CustomMcpServerManager
5
7
import cc.unitmesh.devti.sketch.ui.patch.readText
8
+ import cc.unitmesh.devti.util.AutoDevCoroutineScope
6
9
import com.intellij.openapi.application.runReadAction
7
10
import com.intellij.openapi.editor.Editor
8
11
import com.intellij.openapi.fileEditor.FileEditor
@@ -24,7 +27,9 @@ import io.modelcontextprotocol.kotlin.sdk.Tool
24
27
import kotlinx.coroutines.CoroutineScope
25
28
import kotlinx.coroutines.Dispatchers
26
29
import kotlinx.coroutines.Job
30
+ import kotlinx.coroutines.flow.Flow
27
31
import kotlinx.coroutines.flow.MutableStateFlow
32
+ import kotlinx.coroutines.flow.cancellable
28
33
import kotlinx.coroutines.launch
29
34
import java.awt.BorderLayout
30
35
import java.awt.FlowLayout
@@ -56,7 +61,7 @@ open class McpPreviewEditor(
56
61
private val config = McpLlmConfig ()
57
62
private val borderColor = JBColor (0xE5E7EB , 0x3C3F41 ) // Equivalent to Tailwind gray-200
58
63
private val textGray = JBColor (0x6B7280 , 0x9DA0A8 ) // Equivalent to Tailwind gray-500
59
-
64
+
60
65
init {
61
66
createUI()
62
67
loadTools()
@@ -200,7 +205,7 @@ open class McpPreviewEditor(
200
205
201
206
private fun updateToolsContainer () {
202
207
toolsContainer.removeAll()
203
-
208
+
204
209
if (allTools.isEmpty()) {
205
210
val noToolsLabel = JBLabel (" No tools available. Please check MCP server configuration." ).apply {
206
211
font = JBUI .Fonts .label(14.0f )
@@ -215,14 +220,14 @@ open class McpPreviewEditor(
215
220
}
216
221
}
217
222
}
218
-
223
+
219
224
toolsContainer.revalidate()
220
225
toolsContainer.repaint()
221
226
}
222
227
223
228
private fun showConfigDialog () {
224
229
val dialog = McpLlmConfigDialog (project, config, allTools)
225
-
230
+
226
231
if (dialog.showAndGet()) {
227
232
config.temperature = dialog.getConfig().temperature
228
233
config.enabledTools = dialog.getConfig().enabledTools
@@ -231,9 +236,19 @@ open class McpPreviewEditor(
231
236
}
232
237
233
238
private fun sendMessage () {
239
+ val llmConfig = LlmConfig .load().firstOrNull { it.name == chatbotSelector.selectedItem }
240
+ ? : LlmConfig .default()
241
+ val llmProvider = CustomLLMProvider (project, llmConfig)
234
242
val message = chatInput.text.trim()
235
- if (message.isNotEmpty()) {
236
- chatInput.text = " "
243
+ val result = StringBuilder ()
244
+ val stream: Flow <String > = llmProvider.stream(message, systemPrompt = config.systemPrompt)
245
+ // / create a result panel to save stream text
246
+
247
+ AutoDevCoroutineScope .scope(project).launch {
248
+ stream.cancellable().collect { chunk ->
249
+ result.append(chunk)
250
+ // / update result panel in here ?
251
+ }
237
252
}
238
253
}
239
254
0 commit comments