Skip to content

Commit

Permalink
Access IDE service port via project-level service
Browse files Browse the repository at this point in the history
  • Loading branch information
pplam committed Sep 19, 2024
1 parent 961bc10 commit c564f7d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/ai/devchat/core/BaseActionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/ai/devchat/core/DevChatWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -207,6 +207,7 @@ class DevChatWrapper(val project: Project): Disposable {
).addEnv(getEnv())

private fun getEnv(): Map<String, String> {
val ideServicePort = project.getService(DevChatService::class.java).ideServicePort
val env: MutableMap<String, String> = mutableMapOf()
apiBase?.let {
env["OPENAI_API_BASE"] = it
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/ai/devchat/plugin/IDEServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ data class Result<T>(
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
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -626,5 +627,3 @@ fun findTypeDefinition(
listOfNotNull(it.getLocation())
}.orEmpty()
}

var ideServerPort: Int? = null

0 comments on commit c564f7d

Please sign in to comment.