diff --git a/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt b/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt index aff1cbc..1ba8e1c 100644 --- a/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt +++ b/src/main/kotlin/ai/devchat/core/BaseActionHandler.kt @@ -15,7 +15,7 @@ abstract class BaseActionHandler( var metadata: JSONObject? = null, var payload: JSONObject? = null ) : ActionHandler { - private val devChatService: DevChatService = project.getService(DevChatService::class.java) + val devChatService: DevChatService = project.getService(DevChatService::class.java) val client: DevChatClient? = devChatService.client val wrapper: DevChatWrapper? = devChatService.wrapper val browser: Browser? = devChatService.browser diff --git a/src/main/kotlin/ai/devchat/core/DevChatWrapper.kt b/src/main/kotlin/ai/devchat/core/DevChatWrapper.kt index 861839f..8b997b7 100644 --- a/src/main/kotlin/ai/devchat/core/DevChatWrapper.kt +++ b/src/main/kotlin/ai/devchat/core/DevChatWrapper.kt @@ -3,7 +3,7 @@ package ai.devchat.core import ai.devchat.common.Log import ai.devchat.common.Notifier import ai.devchat.common.PathUtils -import ai.devchat.plugin.ideServerPort +import ai.devchat.plugin.DevChatService import ai.devchat.storage.CONFIG import com.intellij.execution.process.OSProcessUtil.killProcessTree import com.intellij.openapi.Disposable @@ -207,6 +207,7 @@ class DevChatWrapper(val project: Project): Disposable { ).addEnv(getEnv()) private fun getEnv(): Map { + val ideServicePort = project.getService(DevChatService::class.java).ideServicePort val env: MutableMap = mutableMapOf() apiBase?.let { env["OPENAI_API_BASE"] = it @@ -216,8 +217,8 @@ class DevChatWrapper(val project: Project): Disposable { env["OPENAI_API_KEY"] = it } env["PYTHONPATH"] = PathUtils.pythonPath - env["DEVCHAT_IDE_SERVICE_URL"] = "http://localhost:${ideServerPort}" - env["DEVCHAT_IDE_SERVICE_PORT"] = ideServerPort.toString() + env["DEVCHAT_IDE_SERVICE_URL"] = "http://localhost:${ideServicePort}" + env["DEVCHAT_IDE_SERVICE_PORT"] = ideServicePort.toString() env["PYTHONUTF8"] = "1" env["DEVCHAT_UNIT_TESTS_USE_USER_MODEL"] = "1" env["MAMBA_BIN_PATH"] = PathUtils.mambaBinPath diff --git a/src/main/kotlin/ai/devchat/core/handlers/GetIDEServicePortRequestHandler.kt b/src/main/kotlin/ai/devchat/core/handlers/GetIDEServicePortRequestHandler.kt index 629b4bb..29d6b5f 100644 --- a/src/main/kotlin/ai/devchat/core/handlers/GetIDEServicePortRequestHandler.kt +++ b/src/main/kotlin/ai/devchat/core/handlers/GetIDEServicePortRequestHandler.kt @@ -2,7 +2,6 @@ package ai.devchat.core.handlers import ai.devchat.core.BaseActionHandler import ai.devchat.core.DevChatActions -import ai.devchat.plugin.ideServerPort import com.alibaba.fastjson.JSONObject import com.intellij.openapi.project.Project @@ -15,6 +14,6 @@ class GetIDEServicePortRequestHandler(project: Project, requestAction: String, m ) { override val actionName: String = DevChatActions.GET_IDE_SERVICE_PORT_RESPONSE override fun action() { - send(payload= mapOf("result" to ideServerPort)) + send(payload= mapOf("result" to devChatService.ideServicePort)) } } diff --git a/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt b/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt index 6dd468e..58f2e01 100644 --- a/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt +++ b/src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt @@ -44,7 +44,7 @@ class DevChatSetupThread(val project: Project, val toolWindowContent: Content) : private fun startLocalService() { try { val localService = LocalService(project).start() - devChatService.localService = localService + devChatService.localServicePort = localService.port!! devChatService.client = DevChatClient(project, localService.port!!) Disposer.register(toolWindowContent, localService) } catch(e: Exception) { diff --git a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt index 434c901..571c19a 100644 --- a/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt +++ b/src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt @@ -24,7 +24,8 @@ import javax.swing.SwingConstants class DevChatService(project: Project) { var activeConversation: ActiveConversation? = null var browser: Browser? = null - var localService: LocalService? = null + var localServicePort: Int? = null + var ideServicePort: Int? = null var client: DevChatClient? = null var wrapper: DevChatWrapper? = null var uiLoaded: Boolean = false @@ -51,6 +52,7 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable { Disposer.register(content, browser) IDEServer(project).start().let { Disposer.register(content, it) + devChatService.ideServicePort = it.port } DevChatWrapper(project).let { Disposer.register(content, it) diff --git a/src/main/kotlin/ai/devchat/plugin/IDEServer.kt b/src/main/kotlin/ai/devchat/plugin/IDEServer.kt index 683310a..fc6f6cf 100644 --- a/src/main/kotlin/ai/devchat/plugin/IDEServer.kt +++ b/src/main/kotlin/ai/devchat/plugin/IDEServer.kt @@ -82,12 +82,13 @@ data class Result( class IDEServer(private var project: Project): Disposable { private var server: ApplicationEngine? = null private var isShutdownHookRegistered: Boolean = false + var port: Int? = null fun start(): IDEServer { ServerSocket(0).use { - ideServerPort = it.localPort + port = it.localPort } - server = embeddedServer(Netty, port= ideServerPort!!) { + server = embeddedServer(Netty, port= port!!) { install(CORS) { anyHost() allowSameOrigin = true @@ -156,7 +157,7 @@ class IDEServer(private var project: Project): Disposable { post("/get_local_service_port") { val devChatService = project.getService(DevChatService::class.java) - call.respond(Result(devChatService.localService?.port)) + call.respond(Result(devChatService.localServicePort)) } post("/ide_language") { @@ -362,7 +363,7 @@ class IDEServer(private var project: Project): Disposable { } server?.start(wait = false) - Notifier.info("IDE server started at $ideServerPort.") + Notifier.info("IDE server started at $port.") return this } @@ -626,5 +627,3 @@ fun findTypeDefinition( listOfNotNull(it.getLocation()) }.orEmpty() } - -var ideServerPort: Int? = null