From 12437912d7d2b41663090fe91d5b15159ee9c22e Mon Sep 17 00:00:00 2001 From: Andrew Charneski Date: Mon, 4 Sep 2023 09:31:56 -0400 Subject: [PATCH] 1.2.14 (#91) --- gradle.properties | 2 +- .../aicoder/actions/dev/CodeChatAction.kt | 60 ++++++++++--------- .../actions/dev/LaunchSkyenetAction.kt | 41 +++++++------ 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/gradle.properties b/gradle.properties index dfa92696..52ff7d83 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ pluginName=intellij-aicoder pluginRepositoryUrl=https://github.com/SimiaCryptus/intellij-aicoder # SemVer format -> https://semver.org -pluginVersion=1.2.12 +pluginVersion=1.2.14 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html #pluginSinceBuild = 203 pluginSinceBuild=231 diff --git a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/CodeChatAction.kt b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/CodeChatAction.kt index ce492f29..013758ab 100644 --- a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/CodeChatAction.kt +++ b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/CodeChatAction.kt @@ -6,6 +6,7 @@ import com.github.simiacryptus.aicoder.actions.BaseAction import com.github.simiacryptus.aicoder.config.AppSettingsState import com.github.simiacryptus.aicoder.util.ComputerLanguage import com.github.simiacryptus.aicoder.util.UITools +import com.intellij.inspectopedia.extractor.utils.HtmlUtils import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.util.ui.FormBuilder @@ -27,9 +28,10 @@ class CodeChatAction : BaseAction() { port: Int, val language: String, val codeSelection: String, + baseURL: String = "http://localhost:$port", ) : SkyenetCodingSessionServer( applicationName = "Code Chat", - baseURL = "http://localhost:$port", + baseURL = baseURL, model = AppSettingsState.instance.defaultChatModel(), apiKey = AppSettingsState.instance.apiKey, ) { @@ -39,12 +41,18 @@ class CodeChatAction : BaseAction() { override fun newSession(sessionId: String): SkyenetCodingSession { val newSession = CodeChatSession(sessionId) - rootMessageTrail = - """$rootOperationID,

Code:

$codeSelection
""" +rootMessageTrail = +"""$rootOperationID,

Code:

${htmlEscape(codeSelection)}
""" newSession.send(rootMessageTrail) return newSession } + private fun htmlEscape(html: String): String { + return html.replace("&", "&").replace("<", "<") + .replace(">", ">").replace("\"", """) + .replace("'", "'") + } + open inner class CodeChatSession(sessionId: String) : SkyenetCodingSession(sessionId, this@CodeChatServer) { override fun run(userMessage: String) { var messageTrail = ChatSession.divInitializer() @@ -52,7 +60,7 @@ class CodeChatAction : BaseAction() { messages += ChatMessage(ChatMessage.Role.user, userMessage) val response = api.chat(chatRequest, model).choices?.first()?.message?.content.orEmpty() messages += ChatMessage(ChatMessage.Role.assistant, response) - messageTrail += """
${ChatSessionFlexmark.renderMarkdown(response)}
""" + messageTrail += ChatSessionFlexmark.renderMarkdown(response) send(messageTrail) } @@ -110,35 +118,25 @@ class CodeChatAction : BaseAction() { val selectedText = primaryCaret.selectedText ?: editor.document.text val language = ComputerLanguage.getComputerLanguage(event)?.name ?: return - val port = (8000 + (Math.random() * 1000).toInt()) - val skyenet = CodeChatServer(event, port, language, selectedText) + val port = (8000 + (Math.random() * 8000).toInt()) + val skyenet = CodeChatServer(event, port, language, selectedText, baseURL = "http://localhost:$port") val server = skyenet.start(port) Thread { try { - log.info("Server Running on $port") - server.join() - } finally { - log.info("Server Stopped") - } - }.start() - - Thread { - try { - val formBuilder = FormBuilder.createFormBuilder() - val openButton = JXButton("Open") - openButton.addActionListener { - Desktop.getDesktop().browse(server.uri.resolve("/index.html")) + UITools.run( + event.project, "Running CodeChat Server on $port", false + ) { + while (!it.isCanceled && server.isRunning) { + Thread.sleep(1000) + } + if(it.isCanceled) { + log.info("Server cancelled") + server.stop() + } else { + log.info("Server stopped") + } } - formBuilder.addLabeledComponent("Server Running on $port", openButton) - val showOptionDialog = - UITools.showOptionDialog( - formBuilder.panel, - "Close", - title = "Server Running on $port", - modal = true - ) - log.info("showOptionDialog = $showOptionDialog") } finally { log.info("Stopping Server") server.stop() @@ -147,7 +145,11 @@ class CodeChatAction : BaseAction() { Thread { Thread.sleep(500) - Desktop.getDesktop().browse(server.uri.resolve("/index.html")) + try { + Desktop.getDesktop().browse(server.uri.resolve("/index.html")) + } catch (e: Throwable) { + log.warn("Error opening browser", e) + } }.start() } diff --git a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/LaunchSkyenetAction.kt b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/LaunchSkyenetAction.kt index 88e537cf..53219d8f 100644 --- a/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/LaunchSkyenetAction.kt +++ b/src/main/kotlin/com/github/simiacryptus/aicoder/actions/dev/LaunchSkyenetAction.kt @@ -88,35 +88,34 @@ class LaunchSkyenetAction : BaseAction() { } val server = skyenet.start(port) + Thread { try { - log.info("Server Running on $port") - server.join() + UITools.run( + e.project, "Running Skyenet Server on $port", false + ) { + while (!it.isCanceled && server.isRunning) { + Thread.sleep(1000) + } + if(it.isCanceled) { + log.info("Server cancelled") + server.stop() + } else { + log.info("Server stopped") + } + } } finally { - log.info("Server Stopped") + log.info("Stopping Server") + server.stop() } }.start() Thread { + Thread.sleep(500) try { - val url = server.uri - val formBuilder = FormBuilder.createFormBuilder() - val openButton = JXButton("Open") - openButton.addActionListener { - Desktop.getDesktop().browse(url.resolve("/index.html")) - } - formBuilder.addLabeledComponent("Server Running on $port", openButton) - val showOptionDialog = - UITools.showOptionDialog( - formBuilder.panel, - "Close", - title = "Server Running on $port", - modal = false - ) - log.info("showOptionDialog = $showOptionDialog") - } finally { - log.info("Stopping Server") - server.stop() + Desktop.getDesktop().browse(server.uri.resolve("/index.html")) + } catch (e: Throwable) { + log.warn("Error opening browser", e) } }.start() }