🚧 jetbrains webview messenging
This commit is contained in:
parent
5bb270fa97
commit
05b8776bd3
5 changed files with 106 additions and 53 deletions
|
|
@ -4,13 +4,11 @@ import com.github.continuedev.continueintellijextension.services.ContinuePluginS
|
|||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.ui.DialogWrapper
|
||||
import com.intellij.openapi.ui.Messages
|
||||
import com.intellij.openapi.wm.ToolWindowManager
|
||||
import java.awt.Dimension
|
||||
import java.awt.GridLayout
|
||||
import javax.swing.*
|
||||
import javax.swing.event.DocumentEvent
|
||||
import javax.swing.event.DocumentListener
|
||||
|
|
@ -130,7 +128,7 @@ class QuickTextEntryAction : AnAction() {
|
|||
}
|
||||
|
||||
continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus()
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(mutableMapOf("type" to "focusContinueInput")))
|
||||
continuePluginService.sendToWebview("focusContinueInput", null)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -179,7 +177,7 @@ class FocusContinueInputWithoutClearAction : AnAction() {
|
|||
|
||||
val continuePluginService = pluginServiceFromActionEvent(e) ?: return
|
||||
continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus()
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(mutableMapOf("type" to "focusContinueInputWithoutClear")))
|
||||
continuePluginService.sendToWebview("focusContinueInputWithoutClear", null)
|
||||
|
||||
continuePluginService.ideProtocolClient?.sendHighlightedCode()
|
||||
}
|
||||
|
|
@ -202,7 +200,7 @@ class FocusContinueInputAction : AnAction() {
|
|||
val continuePluginService = pluginServiceFromActionEvent(e) ?: return
|
||||
|
||||
continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus()
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(mutableMapOf("type" to "focusContinueInput")))
|
||||
continuePluginService.sendToWebview("focusContinueInput", null)
|
||||
|
||||
continuePluginService.ideProtocolClient?.sendHighlightedCode()
|
||||
}
|
||||
|
|
@ -225,7 +223,7 @@ class NewContinueSessionAction : AnAction() {
|
|||
val continuePluginService = pluginServiceFromActionEvent(e) ?: return
|
||||
|
||||
continuePluginService.continuePluginWindow?.content?.components?.get(0)?.requestFocus()
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(mutableMapOf("type" to "focusContinueInputWithNewSession")))
|
||||
continuePluginService.sendToWebview("focusContinueInputWithNewSession", null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -239,6 +237,6 @@ class ViewHistoryAction : AnAction() {
|
|||
|
||||
val continuePluginService = pluginServiceFromActionEvent(e) ?: return
|
||||
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(mutableMapOf("type" to "viewHistory")))
|
||||
continuePluginService.sendToWebview("viewHistory", null)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
package com.github.continuedev.continueintellijextension.`continue`
|
||||
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.OutputStreamWriter
|
||||
|
|
@ -7,8 +6,6 @@ import java.io.OutputStreamWriter
|
|||
import java.io.*
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import java.io.*
|
||||
|
||||
class CoreMessenger(continueCorePath: String, ideProtocolClient: IdeProtocolClient) {
|
||||
private val writer: OutputStreamWriter
|
||||
|
|
@ -41,14 +38,14 @@ class CoreMessenger(continueCorePath: String, ideProtocolClient: IdeProtocolClie
|
|||
write(message)
|
||||
}
|
||||
|
||||
private fun handleResponse(json: String) {
|
||||
private fun handleMessage(json: String) {
|
||||
val responseMap = gson.fromJson(json, Map::class.java)
|
||||
val messageId = responseMap["messageId"].toString()
|
||||
val messageType = responseMap["messageType"].toString()
|
||||
val responseData = gson.toJson(responseMap["data"])
|
||||
val data = gson.toJson(responseMap["data"])
|
||||
|
||||
// IDE listeners
|
||||
if (ideMessageTypes.contains(responseMap["messageType"])) {
|
||||
if (ideMessageTypes.contains(messageType)) {
|
||||
ideProtocolClient.handleMessage(json) { data ->
|
||||
val message = gson.toJson(mapOf(
|
||||
"messageId" to messageId,
|
||||
|
|
@ -61,7 +58,7 @@ class CoreMessenger(continueCorePath: String, ideProtocolClient: IdeProtocolClie
|
|||
|
||||
// Responses for messageId
|
||||
responseListeners[messageId]?.let { listener ->
|
||||
listener(responseData)
|
||||
listener(data)
|
||||
responseListeners.remove(messageId)
|
||||
}
|
||||
}
|
||||
|
|
@ -118,7 +115,7 @@ class CoreMessenger(continueCorePath: String, ideProtocolClient: IdeProtocolClie
|
|||
val line = reader.readLine()
|
||||
println("Core: $line")
|
||||
if (line != null && line.isNotEmpty()) {
|
||||
handleResponse(line)
|
||||
handleMessage(line)
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
|
|
|
|||
|
|
@ -123,13 +123,7 @@ class IdeProtocolClient (
|
|||
|
||||
private fun send(messageType: String, data: Any?, messageId: String? = null) {
|
||||
val id = messageId ?: uuid()
|
||||
val message = IdeMessage(
|
||||
messageType,
|
||||
id,
|
||||
data
|
||||
)
|
||||
val json = Gson().toJson(message)
|
||||
continuePluginService.dispatchCustomEvent(json)
|
||||
continuePluginService.sendToWebview(messageType, data, id)
|
||||
}
|
||||
|
||||
fun handleMessage(text: String, respond: (Any?) -> Unit) {
|
||||
|
|
@ -143,7 +137,7 @@ class IdeProtocolClient (
|
|||
println("Recieved message without type: $text")
|
||||
return@launch
|
||||
}
|
||||
val data = parsedMessage["data"] as Map<*, *>
|
||||
val data = parsedMessage["data"]
|
||||
|
||||
val historyManager = HistoryManager()
|
||||
|
||||
|
|
@ -171,8 +165,12 @@ class IdeProtocolClient (
|
|||
))
|
||||
}
|
||||
|
||||
"getUniqueId" -> {
|
||||
respond(uniqueId())
|
||||
}
|
||||
|
||||
"showDiff" -> {
|
||||
val data = data["message"] as Map<String, Any>
|
||||
val data = data as Map<String, Any>
|
||||
diffManager.showDiff(
|
||||
data["filepath"] as String,
|
||||
data["newContents"] as String,
|
||||
|
|
@ -182,10 +180,31 @@ class IdeProtocolClient (
|
|||
}
|
||||
|
||||
"readFile" -> {
|
||||
val msg = readFile((data["message"] as Map<String, String>)["filepath"] as String)
|
||||
val msg = readFile((data as Map<String, String>)["filepath"] as String)
|
||||
respond(msg)
|
||||
}
|
||||
|
||||
"isTelemetryEnabled" -> {
|
||||
respond(true)
|
||||
}
|
||||
|
||||
"readRangeInFile" -> {
|
||||
val fullContents = readFile((data as Map<String, String>)["filepath"] as String)
|
||||
val range = data["range"] as Map<String, Any>
|
||||
val start = range["start"] as Map<String, Any>
|
||||
val end = range["end"] as Map<String, Any>
|
||||
val startLine = start["line"] as Int
|
||||
val startCharacter = start["character"] as Int
|
||||
val endLine = end["line"] as Int
|
||||
val endCharacter = end["character"] as Int
|
||||
|
||||
val firstLine = fullContents.split("\n")[startLine].slice(startCharacter until fullContents.split("\n")[startLine].length)
|
||||
val lastLine = fullContents.split("\n")[endLine].slice(0 until endCharacter)
|
||||
val between = fullContents.split("\n").slice(startLine + 1 until endLine).joinToString("\n")
|
||||
|
||||
respond(firstLine + "\n" + between + "\n" + lastLine)
|
||||
}
|
||||
|
||||
"listWorkspaceContents" -> {
|
||||
respond(listDirectoryContents(null))
|
||||
}
|
||||
|
|
@ -194,6 +213,10 @@ class IdeProtocolClient (
|
|||
respond(workspaceDirectories())
|
||||
}
|
||||
|
||||
"getWorkspaceConfigs" -> {
|
||||
respond(emptyList<String>())
|
||||
}
|
||||
|
||||
"getTerminalContents" -> {
|
||||
respond(
|
||||
mapOf("contents" to "Terminal cannot be accessed in JetBrains IDE")
|
||||
|
|
@ -207,10 +230,11 @@ class IdeProtocolClient (
|
|||
}
|
||||
|
||||
"saveFile" -> {
|
||||
saveFile((data["message"] as Map<String, String>)["filepath"] ?: throw Exception("No filepath provided"))
|
||||
saveFile((data as Map<String, String>)["filepath"] ?: throw Exception("No filepath provided"))
|
||||
respond(null)
|
||||
}
|
||||
"showVirtualFile" -> {
|
||||
val data = data as Map<String, Any>
|
||||
showVirtualFile(
|
||||
data["name"] as String,
|
||||
data["contents"] as String
|
||||
|
|
@ -220,10 +244,11 @@ class IdeProtocolClient (
|
|||
|
||||
"connected" -> {}
|
||||
"showMessage" -> {
|
||||
showMessage(data["message"] as String)
|
||||
showMessage(data as String)
|
||||
respond(null)
|
||||
}
|
||||
"setFileOpen" -> {
|
||||
val data = data as Map<String, Any>
|
||||
setFileOpen(
|
||||
data["filepath"] as String,
|
||||
data["open"] as Boolean
|
||||
|
|
@ -231,8 +256,27 @@ class IdeProtocolClient (
|
|||
respond(null)
|
||||
}
|
||||
|
||||
"showLines" -> {
|
||||
val data = data as Map<String, Any>
|
||||
val filepath = data["filepath"] as String
|
||||
val startLine = data["startLine"] as Int
|
||||
val endLine = data["endLine"] as Int
|
||||
highlightCode(
|
||||
RangeInFile(
|
||||
filepath,
|
||||
Range(
|
||||
Position(startLine, 0),
|
||||
Position(endLine, 0)
|
||||
)
|
||||
),
|
||||
data["color"] as String
|
||||
)
|
||||
respond(null)
|
||||
}
|
||||
|
||||
"highlightCode" -> {
|
||||
val gson = Gson()
|
||||
val data = data as Map<String, Any>
|
||||
val json = gson.toJson(data["rangeInFile"])
|
||||
val type = object : TypeToken<RangeInFile>() {}.type
|
||||
val rangeInFile =
|
||||
|
|
@ -304,7 +348,7 @@ class IdeProtocolClient (
|
|||
respond(configJsDataUrl)
|
||||
}
|
||||
"writeFile" -> {
|
||||
val msg = data["message"] as Map<String, String>;
|
||||
val msg = data as Map<String, String>;
|
||||
val file = File(msg["path"])
|
||||
file.writeText(msg["contents"] as String)
|
||||
respond(null);
|
||||
|
|
@ -313,7 +357,7 @@ class IdeProtocolClient (
|
|||
respond(getContinueGlobalPath())
|
||||
}
|
||||
"openFile" -> {
|
||||
setFileOpen((data["message"] as Map<String, Any>)["path"] as String)
|
||||
setFileOpen((data as Map<String, Any>)["path"] as String)
|
||||
respond(null)
|
||||
}
|
||||
"runCommand" -> {
|
||||
|
|
@ -321,7 +365,12 @@ class IdeProtocolClient (
|
|||
// Running commands not yet supported in JetBrains
|
||||
}
|
||||
"errorPopup" -> {
|
||||
showMessage(data["message"] as String)
|
||||
showMessage(data as String)
|
||||
respond(null)
|
||||
}
|
||||
|
||||
"listFolders" -> {
|
||||
respond(null)
|
||||
}
|
||||
|
||||
// History
|
||||
|
|
@ -329,15 +378,15 @@ class IdeProtocolClient (
|
|||
respond(historyManager.list());
|
||||
}
|
||||
"saveSession" -> {
|
||||
historyManager.save(data["message"] as PersistedSessionInfo);
|
||||
historyManager.save(data as PersistedSessionInfo);
|
||||
respond(null);
|
||||
}
|
||||
"deleteSession" -> {
|
||||
historyManager.delete(data["message"] as String);
|
||||
historyManager.delete(data as String);
|
||||
respond(null);
|
||||
}
|
||||
"loadSession" -> {
|
||||
val session = historyManager.load(data["message"] as String)
|
||||
val session = historyManager.load(data as String)
|
||||
respond(session)
|
||||
}
|
||||
"getSearchResults" -> {
|
||||
|
|
@ -349,7 +398,12 @@ class IdeProtocolClient (
|
|||
val openFiles = visibleFiles()
|
||||
respond(openFiles)
|
||||
}
|
||||
"getPinnedFiles" -> {
|
||||
val openFiles = visibleFiles()
|
||||
respond(openFiles)
|
||||
}
|
||||
"logDevData" -> {
|
||||
val data = data as Map<String, Any>
|
||||
val filename = data["tableName"] as String
|
||||
val jsonLine = data["data"]
|
||||
val filepath = getDevDataFilepath(filename)
|
||||
|
|
@ -357,6 +411,7 @@ class IdeProtocolClient (
|
|||
File(filepath).appendText(contents)
|
||||
}
|
||||
"addModel" -> {
|
||||
val data = data as Map<String, Any>
|
||||
val model = data["model"] as Map<String, Any>
|
||||
val updatedConfig = editConfigJson {
|
||||
val models = it["models"] as MutableList<Map<String, Any>>
|
||||
|
|
@ -370,7 +425,7 @@ class IdeProtocolClient (
|
|||
"deleteModel" -> {
|
||||
val configJson = editConfigJson { config ->
|
||||
var models: MutableList<Map<String, Any>> = config["models"] as MutableList<Map<String, Any>>
|
||||
|
||||
val data = data as Map<String, Any>
|
||||
val model = data["title"] as String
|
||||
models = models.filter { it["title"] != model }.toMutableList()
|
||||
config["models"] = models
|
||||
|
|
@ -380,6 +435,7 @@ class IdeProtocolClient (
|
|||
}
|
||||
"addOpenAIKey" -> {
|
||||
val updatedConfig = editConfigJson { config ->
|
||||
val data = data as Map<String, Any>
|
||||
val key = data["key"] as String
|
||||
var models = config["models"] as MutableList<MutableMap<String, Any>>
|
||||
models = models.map {
|
||||
|
|
@ -410,12 +466,7 @@ class IdeProtocolClient (
|
|||
}
|
||||
|
||||
fun configUpdate(config: Map<String, Any>) {
|
||||
val data = mapOf(
|
||||
"type" to "configUpdate",
|
||||
"config" to config
|
||||
)
|
||||
val json = Gson().toJson(data)
|
||||
continuePluginService.dispatchCustomEvent(json)
|
||||
continuePluginService.sendToWebview("configUpdate", config)
|
||||
}
|
||||
|
||||
private fun editConfigJson(callback: (config: MutableMap<String, Any>) -> Map<String, Any>): Map<String, Any> {
|
||||
|
|
@ -559,20 +610,17 @@ class IdeProtocolClient (
|
|||
// edit
|
||||
// ))
|
||||
|
||||
continuePluginService.dispatchCustomEvent(
|
||||
Gson().toJson(mapOf(
|
||||
"type" to "highlightedCode",
|
||||
continuePluginService.sendToWebview(
|
||||
"highlightedCode",
|
||||
mapOf(
|
||||
"rangeInFileWithContents" to rif,
|
||||
"edit" to edit
|
||||
)))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun sendMainUserInput(input: String) {
|
||||
val data = mapOf(
|
||||
"type" to "userInput",
|
||||
"input" to input,
|
||||
)
|
||||
continuePluginService.dispatchCustomEvent(Gson().toJson(data))
|
||||
continuePluginService.sendToWebview("userInput", mapOf("input" to input))
|
||||
}
|
||||
|
||||
fun sendAcceptRejectDiff(accepted: Boolean, stepIndex: Int) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.continuedev.continueintellijextension.services
|
||||
|
||||
import com.github.continuedev.continueintellijextension.`continue`.IdeProtocolClient
|
||||
import com.github.continuedev.continueintellijextension.`continue`.uuid
|
||||
import com.github.continuedev.continueintellijextension.toolWindow.ContinuePluginToolWindowFactory
|
||||
import com.google.gson.Gson
|
||||
import com.intellij.openapi.Disposable
|
||||
|
|
@ -33,10 +34,19 @@ class ContinuePluginService(project: Project) : Disposable, DumbAware {
|
|||
}
|
||||
}
|
||||
|
||||
fun dispatchCustomEvent(
|
||||
data: String
|
||||
fun sendToWebview(
|
||||
messageType: String,
|
||||
data: Any?,
|
||||
messageId: String = uuid()
|
||||
) {
|
||||
val jsCode = buildJavaScript(data)
|
||||
val jsonData = Gson().toJson(
|
||||
mapOf(
|
||||
"messageId" to messageId,
|
||||
"messageType" to messageType,
|
||||
"data" to data
|
||||
)
|
||||
)
|
||||
val jsCode = buildJavaScript(jsonData)
|
||||
|
||||
try {
|
||||
continuePluginWindow?.webView?.executeJavaScriptAsync(jsCode)
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ export class IpcMessenger {
|
|||
messageId,
|
||||
};
|
||||
// process.send?.(data);
|
||||
process.stdout?.write(JSON.stringify(data) + "\n");
|
||||
process.stdout?.write(JSON.stringify(data) + "\r\n");
|
||||
console.log("Sent: ", JSON.stringify(data));
|
||||
return messageId;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue