Skip to content

Commit

Permalink
Merge pull request #208 from devchat-ai/support-multiple-projects
Browse files Browse the repository at this point in the history
Support multiple projects
  • Loading branch information
pplam authored Sep 14, 2024
2 parents c13962f + 677837d commit b526d4c
Show file tree
Hide file tree
Showing 39 changed files with 152 additions and 86 deletions.
5 changes: 3 additions & 2 deletions src/main/kotlin/ai/devchat/core/ActionHandlerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ai.devchat.core

import ai.devchat.core.handlers.*
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor

Expand Down Expand Up @@ -29,10 +30,10 @@ class ActionHandlerFactory {
DevChatActions.STOP_GENERATION_REQUEST to StopGenerationRequestHandler::class,
)

fun createActionHandler(action: String, metadata: JSONObject, payload: JSONObject): ActionHandler? {
fun createActionHandler(project: Project, action: String, metadata: JSONObject, payload: JSONObject): ActionHandler? {
val handlerClass = actionHandlerMap[action] ?: throw RuntimeException("Action handler not found: $action")
return try {
handlerClass.primaryConstructor!!.call(action, metadata, payload)
handlerClass.primaryConstructor!!.call(project, action, metadata, payload)
} catch (e: Exception) {
// Catch any exception since the handling is the same
throw RuntimeException("Failed to instantiate action handler for: $action", e)
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/ai/devchat/core/BaseActionHandler.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ai.devchat.core

import ai.devchat.common.Log
import ai.devchat.plugin.browser
import ai.devchat.plugin.DevChatBrowserService
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

const val DEFAULT_RESPONSE_FUNC = "IdeaToJSMessage"

abstract class BaseActionHandler(
val project: Project,
val requestAction: String,
var metadata: JSONObject? = null,
var payload: JSONObject? = null
Expand Down Expand Up @@ -35,7 +37,9 @@ abstract class BaseActionHandler(
"error" to ""
))
response["payload"] = payload ?: JSONObject()
browser.executeJS(jsCallback, response)
val browser = project.getService(DevChatBrowserService::class.java).browser?.let {
it.executeJS(jsCallback, response)
}
}

override fun executeAction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ai.devchat.core.handlers

import ai.devchat.core.*
import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class AddContextNotifyHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class AddContextNotifyHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package ai.devchat.core.handlers
import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class CodeDiffApplyHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class CodeDiffApplyHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DC_CLIENT
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class DeleteLastConversationRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class DeleteLastConversationRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DC_CLIENT
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class DeleteTopicRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class DeleteTopicRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ 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



class GetIDEServicePortRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class GetIDEServicePortRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.SERVER_CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class GetServerSettingsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class GetServerSettingsRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project



class GetSettingRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class GetSettingRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package ai.devchat.core.handlers

import ai.devchat.core.DevChatWrapper
import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.core.DevChatWrapper
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project
import kotlinx.coroutines.runBlocking

class InputRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class InputRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project

class InsertCodeRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class InsertCodeRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DC_CLIENT
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class ListCommandsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class ListCommandsRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DC_CLIENT
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class ListTopicsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class ListTopicsRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import ai.devchat.core.DC_CLIENT
import ai.devchat.core.DevChatActions
import ai.devchat.storage.ActiveConversation
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class LoadConversationRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class LoadConversationRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import ai.devchat.core.DevChatActions
import ai.devchat.storage.ActiveConversation
import ai.devchat.storage.CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class LoadHistoryMessagesRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class LoadHistoryMessagesRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/ai/devchat/core/handlers/NewSrcFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import com.intellij.lang.Language
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.PsiManager

class NewSrcFile(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class NewSrcFile(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.project.Project
import java.net.URL

class OpenLinkRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class OpenLinkRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project

class ReplaceFileContentHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class ReplaceFileContentHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import ai.devchat.core.*
import ai.devchat.storage.ActiveConversation
import ai.devchat.storage.CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer

class SendMessageRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class SendMessageRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ai.devchat.core.handlers

import ai.devchat.core.*
import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project


class SendUserMessageHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class SendUserMessageHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ai.devchat.core.handlers

import ai.devchat.core.DevChatWrapper
import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.core.DevChatWrapper
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class StopGenerationRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class StopGenerationRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.SERVER_CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class UpdateServerSettingsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class UpdateServerSettingsRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project

class UpdateSettingRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class UpdateSettingRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import ai.devchat.core.DevChatActions
import ai.devchat.plugin.DiffViewerDialog
import ai.devchat.plugin.currentProject
import com.alibaba.fastjson.JSONObject
import com.intellij.diff.DiffContentFactory
import com.intellij.diff.DiffManager
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project

class ViewDiffRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
class ViewDiffRequestHandler(project: Project, requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
project,
requestAction,
metadata,
payload
Expand Down
15 changes: 9 additions & 6 deletions src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package ai.devchat.installer
import ai.devchat.common.*
import ai.devchat.common.Constants.ASSISTANT_NAME_EN
import ai.devchat.core.DC_CLIENT
import ai.devchat.plugin.DevChatToolWindow
import ai.devchat.plugin.browser
import ai.devchat.plugin.DevChatBrowserService
import ai.devchat.plugin.DevChatToolWindowFactory
import ai.devchat.storage.CONFIG
import ai.devchat.storage.DevChatState
import com.intellij.ide.plugins.PluginManagerCore
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.project.Project
import java.io.BufferedReader
import java.io.File
import java.nio.file.Paths

class DevChatSetupThread : Thread() {
class DevChatSetupThread(val project: Project) : Thread() {
private val minimalPythonVersion: String = "3.8"
private val defaultPythonVersion: String = "3.11.4"
private val devChatVersion = PluginManagerCore.getPlugin(
Expand All @@ -27,7 +28,9 @@ class DevChatSetupThread : Thread() {
Log.info("Start configuring the $ASSISTANT_NAME_EN CLI environment.")
setup(PythonEnvManager())
DevChatState.instance.lastVersion = devChatVersion
browser.executeJS("onInitializationFinish")
project.getService(DevChatBrowserService::class.java).browser?.let {
it.executeJS("onInitializationFinish")
}
Notifier.info("$ASSISTANT_NAME_EN initialization has completed successfully.")
} catch (e: Exception) {
Log.error("Failed to install $ASSISTANT_NAME_EN CLI: $e\n" + e.stackTrace.joinToString("\n"))
Expand Down Expand Up @@ -55,7 +58,7 @@ class DevChatSetupThread : Thread() {
).pythonCommand
}
}
DevChatToolWindow.pythonReady = true
DevChatToolWindowFactory.pythonReady = true
PathUtils.copyResourceDirToPath(
"/tools/code-editor/${PathUtils.codeEditorBinary}",
Paths.get(PathUtils.toolsPath, PathUtils.codeEditorBinary).toString(),
Expand All @@ -68,7 +71,7 @@ class DevChatSetupThread : Thread() {
)
PathUtils.copyResourceDirToPath("/workflows", PathUtils.workflowPath)

DevChatToolWindow.pythonReady = true
DevChatToolWindowFactory.pythonReady = true
try {
DC_CLIENT.updateWorkflows()
DC_CLIENT.updateCustomWorkflows()
Expand Down
Loading

0 comments on commit b526d4c

Please sign in to comment.