Skip to content

Commit

Permalink
Merge pull request #171 from devchat-ai/server-config
Browse files Browse the repository at this point in the history
Sync server config
  • Loading branch information
pplam authored Jun 15, 2024
2 parents f6d07f0 + 5ffffc2 commit 64b19f7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gui
2 changes: 2 additions & 0 deletions src/main/kotlin/ai/devchat/core/ActionHandlerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ActionHandlerFactory {
DevChatActions.COMMIT_CODE_REQUEST to CommitCodeRequestHandler::class,
DevChatActions.GET_SETTING_REQUEST to GetSettingRequestHandler::class,
DevChatActions.UPDATE_SETTING_REQUEST to UpdateSettingRequestHandler::class,
DevChatActions.GET_SERVER_SETTINGS_REQUEST to GetServerSettingsRequestHandler::class,
DevChatActions.UPDATE_SERVER_SETTINGS_REQUEST to UpdateServerSettingsRequestHandler::class,
DevChatActions.INPUT_REQUEST to InputRequestHandler::class,
DevChatActions.SHOW_SETTING_DIALOG_REQUEST to ShowSettingDialogRequestHandler::class,
DevChatActions.DELETE_LAST_CONVERSATION_REQUEST to DeleteLastConversationRequestHandler::class,
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/ai/devchat/core/DevChatActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ object DevChatActions {
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 UPDATE_SERVER_SETTINGS_RESPONSE = "updateServerSettings/response"
const val INPUT_REQUEST = "input/request"
const val INPUT_RESPONSE = "input/response"
const val STOP_GENERATION_REQUEST = "stopGeneration/request"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ai.devchat.core.handlers

import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.SERVER_CONFIG
import com.alibaba.fastjson.JSONObject


class GetServerSettingsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
requestAction,
metadata,
payload
) {
override val actionName: String = DevChatActions.GET_SERVER_SETTINGS_RESPONSE
@Suppress("UNCHECKED_CAST")
override fun action() {
send(payload= SERVER_CONFIG.get() as? Map<String, *>)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ai.devchat.core.handlers

import ai.devchat.core.BaseActionHandler
import ai.devchat.core.DevChatActions
import ai.devchat.storage.SERVER_CONFIG
import com.alibaba.fastjson.JSONObject

class UpdateServerSettingsRequestHandler(requestAction: String, metadata: JSONObject?, payload: JSONObject?) : BaseActionHandler(
requestAction,
metadata,
payload
) {
override val actionName: String = DevChatActions.UPDATE_SERVER_SETTINGS_RESPONSE

override fun action() {
SERVER_CONFIG.replaceAll(payload!!)
send()
}
}
58 changes: 58 additions & 0 deletions src/main/kotlin/ai/devchat/storage/ServerConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ai.devchat.storage

import ai.devchat.common.PathUtils
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.io.File
import java.nio.file.Paths

class ServerConfig(
private val configPath: String = Paths.get(PathUtils.workPath, "server_config.yml").toString()
) {
private var data: MutableMap<String, Any?> = mutableMapOf()

init { load() }

fun load(): Map<String, Any?> {
val mapper = ObjectMapper(YAMLFactory()).apply { registerKotlinModule() }
val configFile = File(configPath)
data = if (configFile.isFile) {
try {
mapper.readValue(configFile, dataType)
} catch (e: Exception) {
e.printStackTrace()
mutableMapOf()
}
} else { mutableMapOf() }
return data
}

fun save() {
val file = File(configPath)
file.parentFile?.takeIf { !it.exists() }?.mkdirs() //Ensure parent directories exist
val mapper = ObjectMapper(YAMLFactory())
try {
mapper.writeValue(file, data)
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException("Failed to save config", e)
}
}

fun get(): Any {
return data
}

fun replaceAll(newData: Map<String, Any?>) {
data = newData.toMutableMap()
save()
}

companion object {
val dataType = object : TypeReference<MutableMap<String, Any?>>() {}
}
}

val SERVER_CONFIG: ServerConfig = ServerConfig()

0 comments on commit 64b19f7

Please sign in to comment.