Skip to content

Commit 823f6b1

Browse files
committed
feat(core): implement streaming support for MCP editor #371
- Add LLM config loading and custom LLM provider setup - Implement stream processing for sending messages - Update UI to handle streaming responses - Refactor code to support new streaming functionality
1 parent ca1904c commit 823f6b1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

core/src/main/kotlin/cc/unitmesh/devti/mcp/editor/McpPreviewEditor.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cc.unitmesh.devti.mcp.editor
22

33
import cc.unitmesh.devti.llm2.model.LlmConfig
4+
import cc.unitmesh.devti.llms.LlmFactory
5+
import cc.unitmesh.devti.llms.custom.CustomLLMProvider
46
import cc.unitmesh.devti.mcp.client.CustomMcpServerManager
57
import cc.unitmesh.devti.sketch.ui.patch.readText
8+
import cc.unitmesh.devti.util.AutoDevCoroutineScope
69
import com.intellij.openapi.application.runReadAction
710
import com.intellij.openapi.editor.Editor
811
import com.intellij.openapi.fileEditor.FileEditor
@@ -24,7 +27,9 @@ import io.modelcontextprotocol.kotlin.sdk.Tool
2427
import kotlinx.coroutines.CoroutineScope
2528
import kotlinx.coroutines.Dispatchers
2629
import kotlinx.coroutines.Job
30+
import kotlinx.coroutines.flow.Flow
2731
import kotlinx.coroutines.flow.MutableStateFlow
32+
import kotlinx.coroutines.flow.cancellable
2833
import kotlinx.coroutines.launch
2934
import java.awt.BorderLayout
3035
import java.awt.FlowLayout
@@ -56,7 +61,7 @@ open class McpPreviewEditor(
5661
private val config = McpLlmConfig()
5762
private val borderColor = JBColor(0xE5E7EB, 0x3C3F41) // Equivalent to Tailwind gray-200
5863
private val textGray = JBColor(0x6B7280, 0x9DA0A8) // Equivalent to Tailwind gray-500
59-
64+
6065
init {
6166
createUI()
6267
loadTools()
@@ -200,7 +205,7 @@ open class McpPreviewEditor(
200205

201206
private fun updateToolsContainer() {
202207
toolsContainer.removeAll()
203-
208+
204209
if (allTools.isEmpty()) {
205210
val noToolsLabel = JBLabel("No tools available. Please check MCP server configuration.").apply {
206211
font = JBUI.Fonts.label(14.0f)
@@ -215,14 +220,14 @@ open class McpPreviewEditor(
215220
}
216221
}
217222
}
218-
223+
219224
toolsContainer.revalidate()
220225
toolsContainer.repaint()
221226
}
222227

223228
private fun showConfigDialog() {
224229
val dialog = McpLlmConfigDialog(project, config, allTools)
225-
230+
226231
if (dialog.showAndGet()) {
227232
config.temperature = dialog.getConfig().temperature
228233
config.enabledTools = dialog.getConfig().enabledTools
@@ -231,9 +236,19 @@ open class McpPreviewEditor(
231236
}
232237

233238
private fun sendMessage() {
239+
val llmConfig = LlmConfig.load().firstOrNull { it.name == chatbotSelector.selectedItem }
240+
?: LlmConfig.default()
241+
val llmProvider = CustomLLMProvider(project, llmConfig)
234242
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+
}
237252
}
238253
}
239254

0 commit comments

Comments
 (0)