Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Webview Communication and Context Handling #213

Merged
merged 13 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gui
1 change: 1 addition & 0 deletions src/main/kotlin/ai/devchat/core/ActionHandlerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ActionHandlerFactory {
DevChatActions.REGENERATION_REQUEST to SendMessageRequestHandler::class,
DevChatActions.LIST_COMMANDS_REQUEST to ListCommandsRequestHandler::class,
DevChatActions.LOAD_CONVERSATIONS_REQUEST to LoadConversationRequestHandler::class,
DevChatActions.NEW_CONVERSATIONS_REQUEST to LoadConversationRequestHandler::class,
DevChatActions.LOAD_HISTORY_MESSAGES_REQUEST to LoadHistoryMessagesRequestHandler::class,
DevChatActions.OPEN_LINK_REQUEST to OpenLinkRequestHandler::class,
DevChatActions.LIST_TOPICS_REQUEST to ListTopicsRequestHandler::class,
Expand Down
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 @@ -43,7 +43,7 @@ abstract class BaseActionHandler(
"error" to ""
))
response["payload"] = payload ?: JSONObject()
browser!!.executeJS(jsCallback, response)
browser!!.sendToWebView(payload ?: JSONObject())
}

override fun executeAction() {
Expand Down
63 changes: 32 additions & 31 deletions src/main/kotlin/ai/devchat/core/DevChatActions.kt
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
package ai.devchat.core

object DevChatActions {
const val SEND_MESSAGE_REQUEST = "sendMessage/request"
const val SEND_MESSAGE_REQUEST = "sendMessage"
const val SEND_MESSAGE_RESPONSE = "sendMessage/response"
const val REGENERATION_REQUEST = "regeneration/request"
const val REGENERATION_REQUEST = "regeneration"
const val SEND_USER_MESSAGE_REQUEST = "sendUserMessage/request"
const val SEND_USER_MESSAGE_RESPONSE = "sendUserMessage/response"
const val SEND_USER_MESSAGE_RESPONSE = "chatWithDevChat"
const val CODE_DIFF_APPLY_REQUEST = "codeDiffApply/request"
const val CODE_DIFF_APPLY_RESPONSE = "codeDiffApply/response"
const val ADD_CONTEXT_NOTIFY = "addContext/notify"
const val LIST_COMMANDS_REQUEST = "listCommands/request"
const val LIST_COMMANDS_RESPONSE = "listCommands/response"
const val LOAD_CONVERSATIONS_REQUEST = "loadConversations/request"
const val CODE_DIFF_APPLY_RESPONSE = "codeDiffApply"
const val ADD_CONTEXT_NOTIFY = "contextDetailResponse"
const val LIST_COMMANDS_REQUEST = "regCommandList"
const val LIST_COMMANDS_RESPONSE = "regCommandList"
const val NEW_CONVERSATIONS_REQUEST = "setNewTopic"
const val LOAD_CONVERSATIONS_REQUEST = "getTopicDetail"
const val LOAD_CONVERSATIONS_RESPONSE = "loadConversations/response"
const val LOAD_HISTORY_MESSAGES_REQUEST = "loadHistoryMessages/request"
const val LOAD_HISTORY_MESSAGES_RESPONSE = "loadHistoryMessages/response"
const val OPEN_LINK_REQUEST = "openLink/request"
const val LOAD_HISTORY_MESSAGES_REQUEST = "historyMessages"
const val LOAD_HISTORY_MESSAGES_RESPONSE = "reloadMessage"
const val OPEN_LINK_REQUEST = "openLink"
const val OPEN_LINK_RESPONSE = "openLink/response"
const val LIST_TOPICS_REQUEST = "listTopics/request"
const val LIST_TOPICS_RESPONSE = "listTopics/response"
const val INSERT_CODE_REQUEST = "insertCode/request"
const val LIST_TOPICS_REQUEST = "getTopics"
const val LIST_TOPICS_RESPONSE = "listTopics"
const val INSERT_CODE_REQUEST = "code_apply"
const val INSERT_CODE_RESPONSE = "insertCode/response"
const val NEW_SRC_FILE_REQUEST = "newSrcFile/request"
const val NEW_SRC_FILE_REQUEST = "code_new_file"
const val NEW_SRC_FILE_RESPONSE = "newSrcFile/response"
const val REPLACE_FILE_CONTENT_REQUEST = "replaceFileContent/request"
const val REPLACE_FILE_CONTENT_REQUEST = "code_file_apply"
const val REPLACE_FILE_CONTENT_RESPONSE = "replaceFileContent/response"
const val VIEW_DIFF_REQUEST = "viewDiff/request"
const val VIEW_DIFF_REQUEST = "show_diff"
const val VIEW_DIFF_RESPONSE = "viewDiff/response"
const val GET_IDE_SERVICE_PORT_REQUEST = "getIDEServicePort/request"
const val GET_IDE_SERVICE_PORT_RESPONSE = "getIDEServicePort/response"
const val GET_SETTING_REQUEST = "getSetting/request"
const val GET_SETTING_RESPONSE = "getSetting/response"
const val UPDATE_SETTING_REQUEST = "updateSetting/request"
const val UPDATE_SETTING_RESPONSE = "updateSetting/response"
const val GET_SERVER_SETTINGS_REQUEST = "getServerSettings/request"
const val GET_SERVER_SETTINGS_RESPONSE = "getServerSettings/response"
const val UPDATE_SERVER_SETTINGS_REQUEST = "updateServerSettings/request"
const val GET_IDE_SERVICE_PORT_REQUEST = "getIDEServicePort"
const val GET_IDE_SERVICE_PORT_RESPONSE = "getIDEServicePort"
const val GET_SETTING_REQUEST = "readConfig"
const val GET_SETTING_RESPONSE = "readConfig"
const val UPDATE_SETTING_REQUEST = "writeConfig"
const val UPDATE_SETTING_RESPONSE = "updateSetting"
const val GET_SERVER_SETTINGS_REQUEST = "readServerConfigBase"
const val GET_SERVER_SETTINGS_RESPONSE = "readServerConfigBase"
const val UPDATE_SERVER_SETTINGS_REQUEST = "writeServerConfigBase"
const val UPDATE_SERVER_SETTINGS_RESPONSE = "updateServerSettings/response"
const val INPUT_REQUEST = "input/request"
const val INPUT_REQUEST = "userInput"
const val INPUT_RESPONSE = "input/response"
const val STOP_GENERATION_REQUEST = "stopGeneration/request"
const val STOP_GENERATION_REQUEST = "stopDevChat"
const val STOP_GENERATION_RESPONSE = "stopGeneration/request"
const val DELETE_LAST_CONVERSATION_REQUEST = "deleteLastConversation/request"
const val DELETE_LAST_CONVERSATION_RESPONSE = "deleteLastConversation/response"
const val DELETE_TOPIC_REQUEST = "deleteTopic/request"
const val DELETE_LAST_CONVERSATION_REQUEST = "deleteChatMessage"
const val DELETE_LAST_CONVERSATION_RESPONSE = "deletedChatMessage"
const val DELETE_TOPIC_REQUEST = "deleteTopic"
const val DELETE_TOPIC_RESPONSE = "deleteTopic/response"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ class AddContextNotifyHandler(project: Project, requestAction: String, metadata:
payload
) {
override val actionName: String = DevChatActions.ADD_CONTEXT_NOTIFY

override fun action() {
send(payload=mapOf(
"path" to payload?.getString("path"),
"content" to payload?.getString("content"),
"languageId" to payload?.getString("languageId"),
"startLine" to payload?.getInteger("startLine")
))
val contextObj = mapOf(
"path" to (payload?.getString("path") ?: ""),
"content" to (payload?.getString("content") ?: ""),
"command" to ""
)

val newPayload = mapOf(
"command" to actionName,
"file" to (payload?.getString("path") ?: ""),
"result" to JSONObject.toJSONString(contextObj)
)

send(payload = newPayload)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class CodeDiffApplyHandler(project: Project, requestAction: String, metadata: JS
payload
) {
override val actionName: String = DevChatActions.CODE_DIFF_APPLY_RESPONSE
override fun action() { send() }
override fun action() { send(payload = mapOf("command" to actionName)) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class DeleteLastConversationRequestHandler(project: Project, requestAction: Stri
) {
override val actionName: String = DevChatActions.DELETE_LAST_CONVERSATION_RESPONSE
override fun action() {
val promptHash = payload!!.getString("promptHash")
val promptHash = payload!!.getString("hash")
client!!.deleteLog(promptHash)
send(payload = mapOf("promptHash" to promptHash))
send(payload = mapOf("command" to actionName, "hash" to promptHash))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,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 devChatService.ideServicePort))
send(payload= mapOf("command" to actionName, "result" to devChatService.ideServicePort))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class GetServerSettingsRequestHandler(project: Project, requestAction: String, m
override val actionName: String = DevChatActions.GET_SERVER_SETTINGS_RESPONSE
@Suppress("UNCHECKED_CAST")
override fun action() {
send(payload= SERVER_CONFIG.get() as? Map<String, *>)
send(payload= mapOf( "command" to actionName, "value" to SERVER_CONFIG.get() as? Map<String, *>))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class GetSettingRequestHandler(project: Project, requestAction: String, metadata
override val actionName: String = DevChatActions.GET_SETTING_RESPONSE
@Suppress("UNCHECKED_CAST")
override fun action() {
send(payload= CONFIG.get() as? Map<String, *>)
send(payload= mapOf("command" to actionName, "value" to CONFIG.get() as? Map<String, *>))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class InputRequestHandler(project: Project, requestAction: String, metadata: JSO

override fun action() {
runBlocking {
wrapper!!.activeChannel?.send(payload!!.getString("data"))
wrapper!!.activeChannel?.send(payload!!.getString("text"))
}
send()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ class ListCommandsRequestHandler(project: Project, requestAction: String, metada
"recommend" to recommendedWorkflows.indexOf(commandName)
)
}.orEmpty()
send(payload = mapOf("commands" to indexedCommands))
send(payload = mapOf("command" to actionName, "result" to indexedCommands))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class ListTopicsRequestHandler(project: Project, requestAction: String, metadata
"hidden" to it.hidden,
)
}
send(payload= mapOf("topics" to topics))
send(payload= mapOf("command" to actionName, "list" to topics))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ai.devchat.core.handlers

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

Expand All @@ -14,7 +15,7 @@ class LoadConversationRequestHandler(project: Project, requestAction: String, me
override val actionName: String = DevChatActions.LOAD_CONVERSATIONS_RESPONSE

override fun action() {
val topicHash = metadata!!.getString("topicHash")
val topicHash = metadata?.getString("topicHash")
val res = mutableMapOf("reset" to true)
when {
topicHash.isNullOrEmpty() -> activeConversation!!.reset()
Expand All @@ -24,7 +25,37 @@ class LoadConversationRequestHandler(project: Project, requestAction: String, me
activeConversation!!.reset(topicHash, logs)
}
}
send(payload=res)

loadConversation()
}

fun loadConversation() {
val pageSize = CONFIG["max_log_count"] as Int
val pageIndex = 1
val messages = activeConversation!!.getMessages(pageIndex, pageSize)

// 创建新的 payload
val newPayload = mapOf(
"command" to "reloadMessage",
"entries" to (messages?.map { shortLog ->
mapOf(
"hash" to shortLog.hash,
"parent" to shortLog.parent,
"user" to shortLog.user,
"date" to shortLog.date,
"request" to shortLog.request,
"responses" to shortLog.responses,
"context" to shortLog.context,
"request_tokens" to shortLog.requestTokens,
"response_tokens" to shortLog.responseTokens,
"response" to (shortLog.responses?.joinToString("\n") ?: "")
)
}?.reversed() ?: emptyList()),
"pageIndex" to 0,
"reset" to messages.isNullOrEmpty()
)

send(payload = newPayload)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ai.devchat.core.handlers

import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.core.ShortLog
import ai.devchat.storage.CONFIG
import com.alibaba.fastjson.JSONObject
import com.intellij.openapi.project.Project
Expand All @@ -16,8 +17,30 @@ class LoadHistoryMessagesRequestHandler(project: Project, requestAction: String,

override fun action() {
val pageSize = CONFIG["max_log_count"] as Int
val pageIndex = metadata!!.getInteger("pageIndex") ?: 1
val pageIndex = metadata!!.getInteger("page") ?: 1
val messages = activeConversation!!.getMessages(pageIndex, pageSize)
send(payload = mapOf("messages" to messages))

// 创建新的 payload
val newPayload = mapOf(
"command" to "reloadMessage",
"entries" to (messages?.map { shortLog ->
mapOf(
"hash" to shortLog.hash,
"parent" to shortLog.parent,
"user" to shortLog.user,
"date" to shortLog.date,
"request" to shortLog.request,
"responses" to shortLog.responses,
"context" to shortLog.context,
"request_tokens" to shortLog.requestTokens,
"response_tokens" to shortLog.responseTokens,
"response" to (shortLog.responses?.joinToString("\n") ?: "")
)
}?.reversed() ?: emptyList()),
"pageIndex" to 0,
"reset" to messages.isNullOrEmpty()
)

send(payload = newPayload)
}
}
Loading
Loading