Skip to content

Commit dcae35c

Browse files
committed
feat(custom-agent): add support for custom webview in chat interface #51
The commit introduces a new feature to the chat interface, allowing for the display of custom webviews. This enhancement enables the chat system to render dynamic content such as HTML, CSS, and JavaScript, providing a more interactive user experience. The changes include modifications to the server.py file to handle incoming webview requests, and updates to the CustomAgentChatProcessor.kt and WebViewWindow.kt files to implement the webview functionality within the chat application. Additionally, the ChatCodingPanel.kt file has been updated to include the necessary imports and functions for displaying webviews and code blocks.
1 parent 6cd1e2c commit dcae35c

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

example/custom_agent/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def mock_frontend(messages: Messages):
5858

5959
@app.post("/api/agent/ux")
6060
def mock_frontend(messages: Messages):
61-
return """```html
61+
return """
6262
<html>
6363
<head>
6464
<meta charset="UTF-8">
@@ -72,7 +72,7 @@ def mock_frontend(messages: Messages):
7272
<p>你好,世界</p>
7373
</body>
7474
</html>
75-
```"""
75+
"""
7676

7777

7878
@app.exception_handler(RequestValidationError)

src/main/kotlin/cc/unitmesh/devti/counit/CustomAgentChatProcessor.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.counit.model.CustomAgentConfig
55
import cc.unitmesh.devti.counit.model.CustomAgentState
66
import cc.unitmesh.devti.counit.model.ResponseAction
7-
import cc.unitmesh.devti.counit.view.WebBlock
8-
import cc.unitmesh.devti.counit.view.WebBlockView
97
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
108
import cc.unitmesh.devti.gui.chat.ChatContext
11-
import cc.unitmesh.devti.gui.chat.ChatRole
129
import cc.unitmesh.devti.provider.ContextPrompter
1310
import cc.unitmesh.devti.util.LLMCoroutineScope
1411
import com.intellij.openapi.components.Service
1512
import com.intellij.openapi.diagnostic.logger
1613
import com.intellij.openapi.project.Project
17-
import com.intellij.temporary.gui.block.SimpleMessage
1814
import kotlinx.coroutines.launch
1915
import kotlinx.coroutines.runBlocking
2016

@@ -79,11 +75,10 @@ class CustomAgentChatProcessor(val project: Project) {
7975
sb.append(it)
8076
}
8177
}
78+
// TODO: add decode support
8279
val content = sb.toString()
8380

84-
val webBlock = WebBlock(SimpleMessage(content, content, ChatRole.User))
85-
val blockView = WebBlockView(webBlock, project, {})
86-
ui.appendWebView(blockView)
81+
ui.appendWebView(content, project)
8782
ui.hiddenProgressBar()
8883
}
8984
}

src/main/kotlin/cc/unitmesh/devti/counit/view/WebViewWindow.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import java.awt.Component
1313
* for custom webview can refs: https://github.com/mucharafal/jcef_example
1414
*/
1515
class WebViewWindow {
16-
private val browser: JBCefBrowser = JBCefBrowser().also {}
16+
private val browser: JBCefBrowser = JBCefBrowser().also {
17+
18+
}
1719

1820
val component: Component = browser.component
1921

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingPanel.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ package cc.unitmesh.devti.gui.chat
33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.alignRight
55
import cc.unitmesh.devti.counit.model.CustomAgentConfig
6+
import cc.unitmesh.devti.counit.view.WebBlock
7+
import cc.unitmesh.devti.counit.view.WebBlockView
68
import cc.unitmesh.devti.fullHeight
79
import cc.unitmesh.devti.fullWidth
810
import cc.unitmesh.devti.provider.ContextPrompter
911
import cc.unitmesh.devti.settings.AutoDevSettingsState
1012
import com.intellij.ide.BrowserUtil
1113
import com.intellij.openapi.Disposable
1214
import com.intellij.openapi.diagnostic.logger
15+
import com.intellij.openapi.project.Project
1316
import com.intellij.openapi.ui.DialogPanel
1417
import com.intellij.openapi.ui.NullableComponent
1518
import com.intellij.openapi.ui.SimpleToolWindowPanel
1619
import com.intellij.openapi.wm.IdeFocusManager
17-
import com.intellij.temporary.gui.block.MessageBlockView
20+
import com.intellij.temporary.gui.block.CodeBlock
21+
import com.intellij.temporary.gui.block.CodeBlockView
22+
import com.intellij.temporary.gui.block.SimpleMessage
1823
import com.intellij.temporary.gui.block.whenDisposed
1924
import com.intellij.ui.Gray
2025
import com.intellij.ui.JBColor
@@ -266,27 +271,37 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
266271
updateUI()
267272
}
268273

269-
fun appendWebView(blockView: MessageBlockView) {
270-
myList.add(SimpleView(blockView))
274+
fun appendWebView(content: String, project: Project) {
275+
val msg = SimpleMessage(content, content, ChatRole.System)
276+
val webBlock = WebBlock(msg)
277+
val blockView = WebBlockView(webBlock, project, {})
278+
val codeView = CodeBlockView(CodeBlock(msg), project, {})
279+
280+
myList.add(SimpleView(blockView, codeView))
281+
271282
updateUI()
272283
}
273284
}
274285

275-
class SimpleView(val view: MessageBlockView) : JBPanel<MessageView>() {
286+
class SimpleView(val webview: WebBlockView, val codeView: CodeBlockView) : JBPanel<MessageView>() {
276287
init {
277288
isDoubleBuffered = true
278289
isOpaque = true
279-
background = JBColor(0x6F59FF, 0x2d2f30)
290+
background = JBColor(0xE0EEF7, 0x2d2f30)
280291

281292
layout = BorderLayout(JBUI.scale(8), 0)
282293
val centerPanel = JPanel(VerticalLayout(JBUI.scale(8)))
283294

284295
centerPanel.isOpaque = false
285296
centerPanel.border = JBUI.Borders.emptyRight(8)
297+
centerPanel.background = JBColor(0xE0EEF7, 0x2d2f30)
286298

287299
add(centerPanel, BorderLayout.WEST)
288300

289-
view.initialize()
290-
centerPanel.add(view.getComponent())
301+
webview.initialize()
302+
centerPanel.add(webview.getComponent())
303+
304+
codeView.initialize()
305+
centerPanel.add(codeView.getComponent())
291306
}
292307
}

0 commit comments

Comments
 (0)