Changesets · Branches · Tags · Compare
| Changeset | c7054bc87e23b109d054f0dd31fbc62d4574d11c4807b43e442d6f4f44f21d6f |
|---|---|
| Author | Jakub Blach <jakubblach@gmail.com> · 2025-07-04 22:35 +02:00 |
| Committer | Jakub Blach <jakubblach@gmail.com> · 2025-07-08 14:27 +02:00 |
| Parents | 5c0668b587e2 |
7 files changed
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/constants/MessageTypes.kt +1 −1
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | class MessageTypes { |
| 4 | 4 | companion object { |
| 5 | - val ideMessageTypes = listOf( | |
| 5 | + val IDE_MESSAGE_TYPES = listOf( | |
| 6 | 6 | "readRangeInFile", |
| 7 | 7 | "isTelemetryEnabled", |
| 8 | 8 | "getUniqueId", |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessenger.kt +21 −176
| @@ -1,51 +1,40 @@ | ||
| 1 | 1 | package com.github.continuedev.continueintellijextension.`continue` |
| 2 | 2 | |
| 3 | 3 | import com.github.continuedev.continueintellijextension.constants.MessageTypes |
| 4 | -import com.github.continuedev.continueintellijextension.services.ContinueExtensionSettings | |
| 4 | +import com.github.continuedev.continueintellijextension.`continue`.process.ContinueBinaryProcess | |
| 5 | +import com.github.continuedev.continueintellijextension.`continue`.process.ContinueProcessHandler | |
| 6 | +import com.github.continuedev.continueintellijextension.`continue`.process.ContinueSocketProcess | |
| 5 | 7 | import com.github.continuedev.continueintellijextension.services.ContinuePluginService |
| 6 | -import com.github.continuedev.continueintellijextension.services.TelemetryService | |
| 7 | 8 | import com.github.continuedev.continueintellijextension.utils.uuid |
| 8 | 9 | import com.google.gson.Gson |
| 9 | 10 | import com.intellij.openapi.components.service |
| 10 | 11 | import com.intellij.openapi.project.Project |
| 11 | -import java.io.* | |
| 12 | -import java.net.Socket | |
| 13 | -import java.nio.charset.StandardCharsets | |
| 14 | -import java.nio.file.Files | |
| 15 | -import java.nio.file.Paths | |
| 16 | -import java.nio.file.attribute.PosixFilePermission | |
| 17 | 12 | import kotlinx.coroutines.* |
| 18 | 13 | |
| 19 | 14 | class CoreMessenger( |
| 20 | 15 | private val project: Project, |
| 21 | - continueCorePath: String, | |
| 22 | 16 | private val ideProtocolClient: IdeProtocolClient, |
| 23 | - val coroutineScope: CoroutineScope | |
| 17 | + val coroutineScope: CoroutineScope, | |
| 18 | + private val onExit: () -> Unit | |
| 24 | 19 | ) { |
| 25 | - private var writer: Writer? = null | |
| 26 | - private var reader: BufferedReader? = null | |
| 27 | - private var process: Process? = null | |
| 28 | 20 | private val gson = Gson() |
| 29 | 21 | private val responseListeners = mutableMapOf<String, (Any?) -> Unit>() |
| 30 | - private val useTcp: Boolean = System.getenv("USE_TCP")?.toBoolean() ?: false | |
| 31 | - | |
| 32 | - private fun write(message: String) { | |
| 33 | - try { | |
| 34 | - writer?.write(message + "\r\n") | |
| 35 | - writer?.flush() | |
| 36 | - } catch (e: Exception) { | |
| 37 | - println("Error writing to Continue core: $e") | |
| 38 | - } | |
| 39 | - } | |
| 22 | + private val process = startContinueProcess() | |
| 40 | 23 | |
| 41 | 24 | fun request(messageType: String, data: Any?, messageId: String?, onResponse: (Any?) -> Unit) { |
| 42 | 25 | val id = messageId ?: uuid() |
| 43 | - val message = | |
| 44 | - gson.toJson(mapOf("messageId" to id, "messageType" to messageType, "data" to data)) | |
| 26 | + val message = gson.toJson(mapOf("messageId" to id, "messageType" to messageType, "data" to data)) | |
| 45 | 27 | responseListeners[id] = onResponse |
| 46 | - coroutineScope.launch(Dispatchers.IO) { | |
| 47 | - write(message) | |
| 48 | - } | |
| 28 | + process.write(message) | |
| 29 | + } | |
| 30 | + | |
| 31 | + private fun startContinueProcess(): ContinueProcessHandler { | |
| 32 | + val isTcp = System.getenv("USE_TCP")?.toBoolean() ?: false | |
| 33 | + val process = if (isTcp) | |
| 34 | + ContinueSocketProcess() | |
| 35 | + else | |
| 36 | + ContinueBinaryProcess(onExit) | |
| 37 | + return ContinueProcessHandler(coroutineScope, process, ::handleMessage) | |
| 49 | 38 | } |
| 50 | 39 | |
| 51 | 40 | private fun handleMessage(json: String) { |
| @@ -55,13 +44,10 @@ | ||
| 55 | 44 | val data = responseMap["data"] |
| 56 | 45 | |
| 57 | 46 | // IDE listeners |
| 58 | - if (MessageTypes.ideMessageTypes.contains(messageType)) { | |
| 47 | + if (MessageTypes.IDE_MESSAGE_TYPES.contains(messageType)) { | |
| 59 | 48 | ideProtocolClient.handleMessage(json) { data -> |
| 60 | - val message = | |
| 61 | - gson.toJson( | |
| 62 | - mapOf("messageId" to messageId, "messageType" to messageType, "data" to data) | |
| 63 | - ) | |
| 64 | - write(message) | |
| 49 | + val message = gson.toJson(mapOf("messageId" to messageId, "messageType" to messageType, "data" to data)) | |
| 50 | + process.write(message) | |
| 65 | 51 | } |
| 66 | 52 | } |
| 67 | 53 | |
| @@ -82,148 +68,7 @@ | ||
| 82 | 68 | } |
| 83 | 69 | } |
| 84 | 70 | |
| 85 | - private fun setPermissions(destination: String) { | |
| 86 | - val osName = System.getProperty("os.name").toLowerCase() | |
| 87 | - if (osName.contains("mac") || osName.contains("darwin")) { | |
| 88 | - ProcessBuilder("xattr", "-dr", "com.apple.quarantine", destination).start().waitFor() | |
| 89 | - setFilePermissions(destination, "rwxr-xr-x") | |
| 90 | - } else if (osName.contains("nix") || osName.contains("nux")) { | |
| 91 | - setFilePermissions(destination, "rwxr-xr-x") | |
| 92 | - } | |
| 93 | - } | |
| 94 | - | |
| 95 | - private fun setFilePermissions(path: String, posixPermissions: String) { | |
| 96 | - val perms = HashSet<PosixFilePermission>() | |
| 97 | - if (posixPermissions.contains("r")) perms.add(PosixFilePermission.OWNER_READ) | |
| 98 | - if (posixPermissions.contains("w")) perms.add(PosixFilePermission.OWNER_WRITE) | |
| 99 | - if (posixPermissions.contains("x")) perms.add(PosixFilePermission.OWNER_EXECUTE) | |
| 100 | - Files.setPosixFilePermissions(Paths.get(path), perms) | |
| 101 | - } | |
| 102 | - | |
| 103 | - private val exitCallbacks: MutableList<() -> Unit> = mutableListOf() | |
| 104 | - | |
| 105 | - fun onDidExit(callback: () -> Unit) { | |
| 106 | - exitCallbacks.add(callback) | |
| 107 | - } | |
| 108 | - | |
| 109 | - init { | |
| 110 | - if (useTcp) { | |
| 111 | - try { | |
| 112 | - val socket = Socket("127.0.0.1", 3000) | |
| 113 | - val writer = PrintWriter(socket.getOutputStream(), true) | |
| 114 | - this.writer = writer | |
| 115 | - val reader = BufferedReader(InputStreamReader(socket.getInputStream())) | |
| 116 | - this.reader = reader | |
| 117 | - | |
| 118 | - Thread { | |
| 119 | - try { | |
| 120 | - while (true) { | |
| 121 | - val line = reader.readLine() | |
| 122 | - if (line != null && line.isNotEmpty()) { | |
| 123 | - try { | |
| 124 | - handleMessage(line) | |
| 125 | - } catch (e: Exception) { | |
| 126 | - println("Error handling message: $line") | |
| 127 | - println(e) | |
| 128 | - } | |
| 129 | - } else { | |
| 130 | - Thread.sleep(100) | |
| 131 | - } | |
| 132 | - } | |
| 133 | - } catch (e: IOException) { | |
| 134 | - e.printStackTrace() | |
| 135 | - } finally { | |
| 136 | - try { | |
| 137 | - reader.close() | |
| 138 | - writer.close() | |
| 139 | - } catch (e: IOException) { | |
| 140 | - e.printStackTrace() | |
| 141 | - } | |
| 142 | - } | |
| 143 | - } | |
| 144 | - .start() | |
| 145 | - } catch (e: Exception) { | |
| 146 | - println("TCP Connection Error: Unable to connect to 127.0.0.1:3000") | |
| 147 | - println("Reason: ${e.message}") | |
| 148 | - e.printStackTrace() | |
| 149 | - } | |
| 150 | - } else { | |
| 151 | - // Set proper permissions synchronously | |
| 152 | - runBlocking(Dispatchers.IO) { | |
| 153 | - setPermissions(continueCorePath) | |
| 154 | - } | |
| 155 | - | |
| 156 | - // Start the subprocess | |
| 157 | - val processBuilder = | |
| 158 | - ProcessBuilder(continueCorePath).directory(File(continueCorePath).parentFile) | |
| 159 | - process = processBuilder.start() | |
| 160 | - | |
| 161 | - val outputStream = process!!.outputStream | |
| 162 | - val inputStream = process!!.inputStream | |
| 163 | - | |
| 164 | - writer = OutputStreamWriter(outputStream, StandardCharsets.UTF_8) | |
| 165 | - reader = BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)) | |
| 166 | - | |
| 167 | - process!!.onExit().thenRun { | |
| 168 | - exitCallbacks.forEach { it() } | |
| 169 | - var err = process?.errorStream?.bufferedReader()?.readText()?.trim() | |
| 170 | - if (err != null) { | |
| 171 | - // There are often "⚡️Done in Xms" messages, and we want everything after the last one | |
| 172 | - val delimiter = "⚡ Done in" | |
| 173 | - val doneIndex = err.lastIndexOf(delimiter) | |
| 174 | - if (doneIndex != -1) { | |
| 175 | - err = err.substring(doneIndex + delimiter.length) | |
| 176 | - } | |
| 177 | - } | |
| 178 | - | |
| 179 | - println("Core process exited with output: $err") | |
| 180 | - | |
| 181 | - // Log the cause of the failure | |
| 182 | - val telemetryService = service<TelemetryService>() | |
| 183 | - telemetryService.capture("jetbrains_core_exit", mapOf("error" to err)) | |
| 184 | - | |
| 185 | - // Clean up all resources | |
| 186 | - writer?.close() | |
| 187 | - reader?.close() | |
| 188 | - process?.destroy() | |
| 189 | - } | |
| 190 | - | |
| 191 | - coroutineScope.launch(Dispatchers.IO) { | |
| 192 | - try { | |
| 193 | - while (true) { | |
| 194 | - val line = reader?.readLine() | |
| 195 | - if (line != null && line.isNotEmpty()) { | |
| 196 | - try { | |
| 197 | - handleMessage(line) | |
| 198 | - } catch (e: Exception) { | |
| 199 | - println("Error handling message: $line") | |
| 200 | - println(e) | |
| 201 | - } | |
| 202 | - } else { | |
| 203 | - delay(100) | |
| 204 | - } | |
| 205 | - } | |
| 206 | - } catch (e: IOException) { | |
| 207 | - e.printStackTrace() | |
| 208 | - } finally { | |
| 209 | - try { | |
| 210 | - reader?.close() | |
| 211 | - writer?.close() | |
| 212 | - outputStream.close() | |
| 213 | - inputStream.close() | |
| 214 | - process?.destroy() | |
| 215 | - } catch (e: IOException) { | |
| 216 | - e.printStackTrace() | |
| 217 | - } | |
| 218 | - } | |
| 219 | - } | |
| 220 | - } | |
| 221 | - } | |
| 222 | - | |
| 223 | 71 | fun killSubProcess() { |
| 224 | - process?.isAlive?.let { | |
| 225 | - exitCallbacks.clear() | |
| 226 | - process?.destroy() | |
| 227 | - } | |
| 72 | + process.close() | |
| 228 | 73 | } |
| 229 | 74 | } |
| No newline at the end of the file |
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/CoreMessengerManager.kt +16 −19
| @@ -2,11 +2,11 @@ | ||
| 2 | 2 | |
| 3 | 3 | import com.github.continuedev.continueintellijextension.services.TelemetryService |
| 4 | 4 | import com.github.continuedev.continueintellijextension.utils.castNestedOrNull |
| 5 | -import com.github.continuedev.continueintellijextension.utils.getContinueBinaryPath | |
| 6 | 5 | import com.github.continuedev.continueintellijextension.utils.getMachineUniqueID |
| 7 | 6 | import com.intellij.openapi.components.service |
| 8 | 7 | import com.intellij.openapi.project.Project |
| 9 | -import kotlinx.coroutines.* | |
| 8 | +import kotlinx.coroutines.CoroutineScope | |
| 9 | +import kotlinx.coroutines.launch | |
| 10 | 10 | |
| 11 | 11 | class CoreMessengerManager( |
| 12 | 12 | private val project: Project, |
| @@ -19,30 +19,27 @@ | ||
| 19 | 19 | |
| 20 | 20 | init { |
| 21 | 21 | coroutineScope.launch { |
| 22 | - val continueBinaryPath = getContinueBinaryPath() | |
| 23 | - setupCoreMessenger(continueBinaryPath) | |
| 22 | + setupCoreMessenger() | |
| 24 | 23 | } |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | - private fun setupCoreMessenger(continueCorePath: String) { | |
| 26 | + private fun setupCoreMessenger() { | |
| 28 | 27 | try { |
| 29 | - coreMessenger = CoreMessenger(project, continueCorePath, ideProtocolClient, coroutineScope) | |
| 30 | - | |
| 31 | - coreMessenger?.request("config/getSerializedProfileInfo", null, null) { response -> | |
| 32 | - val allowAnonymousTelemetry = response.castNestedOrNull<Boolean>("content", "result", "config", "allowAnonymousTelemetry") | |
| 33 | - | |
| 34 | - val telemetryService = service<TelemetryService>() | |
| 35 | - if (allowAnonymousTelemetry == true || allowAnonymousTelemetry == null) { | |
| 36 | - telemetryService.setup(getMachineUniqueID()) | |
| 37 | - } | |
| 38 | - } | |
| 39 | - | |
| 40 | - // On exit, use exponential backoff to create another CoreMessenger | |
| 41 | - coreMessenger?.onDidExit { | |
| 28 | + coreMessenger = CoreMessenger(project, ideProtocolClient, coroutineScope, onExit = { | |
| 42 | 29 | lastBackoffInterval *= 2 |
| 43 | 30 | println("CoreMessenger exited, retrying in $lastBackoffInterval seconds") |
| 44 | 31 | Thread.sleep((lastBackoffInterval * 1000).toLong()) |
| 45 | - setupCoreMessenger(continueCorePath) | |
| 32 | + setupCoreMessenger() | |
| 33 | + }) | |
| 34 | + | |
| 35 | + coreMessenger?.request("config/getSerializedProfileInfo", null, null) { response -> | |
| 36 | + val allowAnonymousTelemetry = | |
| 37 | + response.castNestedOrNull<Boolean>("content", "result", "config", "allowAnonymousTelemetry") | |
| 38 | + | |
| 39 | + val telemetryService = service<TelemetryService>() | |
| 40 | + if (allowAnonymousTelemetry == true || allowAnonymousTelemetry == null) { | |
| 41 | + telemetryService.setup(getMachineUniqueID()) | |
| 42 | + } | |
| 46 | 43 | } |
| 47 | 44 | } catch (err: Throwable) { |
| 48 | 45 | val telemetryService = service<TelemetryService>() |
added extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueBinaryProcess.kt +61 −0
| @@ -0,0 +1,61 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import com.github.continuedev.continueintellijextension.utils.OS | |
| 4 | +import com.github.continuedev.continueintellijextension.utils.getContinueBinaryPath | |
| 5 | +import com.github.continuedev.continueintellijextension.utils.getOS | |
| 6 | +import kotlinx.coroutines.Dispatchers | |
| 7 | +import kotlinx.coroutines.runBlocking | |
| 8 | +import java.io.File | |
| 9 | +import java.io.InputStream | |
| 10 | +import java.io.OutputStream | |
| 11 | +import java.nio.file.Files | |
| 12 | +import java.nio.file.Paths | |
| 13 | +import java.nio.file.attribute.PosixFilePermission | |
| 14 | + | |
| 15 | +class ContinueBinaryProcess(private val onExit: () -> Unit) : ContinueProcess { | |
| 16 | + | |
| 17 | + private val process = startBinaryProcess() | |
| 18 | + override val input: InputStream = process.inputStream | |
| 19 | + override val output: OutputStream = process.outputStream | |
| 20 | + | |
| 21 | + override fun close() = | |
| 22 | + process.destroy() | |
| 23 | + | |
| 24 | + private fun startBinaryProcess(): Process { | |
| 25 | + val path = getContinueBinaryPath() | |
| 26 | + runBlocking(Dispatchers.IO) { | |
| 27 | + setPermissions() | |
| 28 | + } | |
| 29 | + return ProcessBuilder(path).directory(File(path).parentFile) | |
| 30 | + .start() | |
| 31 | + .apply { onExit().thenRun(onExit) } | |
| 32 | + } | |
| 33 | + | |
| 34 | + private companion object { | |
| 35 | + | |
| 36 | + private fun setPermissions() { | |
| 37 | + val os = getOS() | |
| 38 | + when (os) { | |
| 39 | + OS.MAC -> setMacOsPermissions() | |
| 40 | + OS.WINDOWS -> {} | |
| 41 | + OS.LINUX -> elevatePermissions() | |
| 42 | + } | |
| 43 | + } | |
| 44 | + | |
| 45 | + private fun setMacOsPermissions() { | |
| 46 | + ProcessBuilder("xattr", "-dr", "com.apple.quarantine", getContinueBinaryPath()).start().waitFor() | |
| 47 | + elevatePermissions() | |
| 48 | + } | |
| 49 | + | |
| 50 | + private fun elevatePermissions() { | |
| 51 | + val path = getContinueBinaryPath() | |
| 52 | + val permissions = setOf( | |
| 53 | + PosixFilePermission.OWNER_READ, | |
| 54 | + PosixFilePermission.OWNER_WRITE, | |
| 55 | + PosixFilePermission.OWNER_EXECUTE | |
| 56 | + ) | |
| 57 | + Files.setPosixFilePermissions(Paths.get(path), permissions) | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | +} | |
| No newline at the end of the file |
added extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcess.kt +13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import java.io.InputStream | |
| 4 | +import java.io.OutputStream | |
| 5 | + | |
| 6 | +interface ContinueProcess { | |
| 7 | + | |
| 8 | + val input: InputStream | |
| 9 | + val output: OutputStream | |
| 10 | + | |
| 11 | + fun close() | |
| 12 | + | |
| 13 | +} | |
| No newline at the end of the file |
added extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandler.kt +57 −0
| @@ -0,0 +1,57 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import com.github.continuedev.continueintellijextension.error.ContinueErrorService | |
| 4 | +import com.intellij.openapi.components.service | |
| 5 | +import kotlinx.coroutines.CoroutineScope | |
| 6 | +import kotlinx.coroutines.Dispatchers | |
| 7 | +import kotlinx.coroutines.channels.Channel | |
| 8 | +import kotlinx.coroutines.delay | |
| 9 | +import kotlinx.coroutines.launch | |
| 10 | +import java.io.BufferedReader | |
| 11 | +import java.io.InputStreamReader | |
| 12 | +import java.io.OutputStreamWriter | |
| 13 | + | |
| 14 | +class ContinueProcessHandler( | |
| 15 | + coroutineScope: CoroutineScope, | |
| 16 | + private val process: ContinueProcess, | |
| 17 | + handleMessage: (String) -> (Unit) | |
| 18 | +) { | |
| 19 | + private val pendingWrites = Channel<String>(Channel.Factory.UNLIMITED) | |
| 20 | + private val writer = OutputStreamWriter(process.output) | |
| 21 | + private val reader = BufferedReader(InputStreamReader(process.input)) | |
| 22 | + | |
| 23 | + init { | |
| 24 | + coroutineScope.launch(Dispatchers.IO) { | |
| 25 | + while (true) { | |
| 26 | + if (reader.ready()) { | |
| 27 | + val line = reader.readLine() | |
| 28 | + if (line.isNotEmpty()) { | |
| 29 | + try { | |
| 30 | + handleMessage(line) | |
| 31 | + } catch (e: Exception) { | |
| 32 | + service<ContinueErrorService>().report(e, "Error handling message: $line") | |
| 33 | + } | |
| 34 | + } | |
| 35 | + } else | |
| 36 | + delay(100) | |
| 37 | + } | |
| 38 | + } | |
| 39 | + coroutineScope.launch(Dispatchers.IO) { | |
| 40 | + for (message in pendingWrites) { | |
| 41 | + writer.write(message) | |
| 42 | + writer.write("\r\n") | |
| 43 | + writer.flush() | |
| 44 | + } | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + fun write(message: String) = | |
| 49 | + pendingWrites.trySend(message) | |
| 50 | + | |
| 51 | + fun close() { | |
| 52 | + reader.close() | |
| 53 | + writer.close() | |
| 54 | + process.close() | |
| 55 | + } | |
| 56 | + | |
| 57 | +} | |
| No newline at the end of the file |
added extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueSocketProcess.kt +16 −0
| @@ -0,0 +1,16 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import java.io.InputStream | |
| 4 | +import java.io.OutputStream | |
| 5 | +import java.net.Socket | |
| 6 | + | |
| 7 | +class ContinueSocketProcess : ContinueProcess { | |
| 8 | + | |
| 9 | + private val socket = Socket("127.0.0.1", 3000) | |
| 10 | + override val input: InputStream = socket.inputStream | |
| 11 | + override val output: OutputStream = socket.outputStream | |
| 12 | + | |
| 13 | + override fun close() = | |
| 14 | + socket.close() | |
| 15 | + | |
| 16 | +} | |
| No newline at the end of the file |