Skip to content

Commit

Permalink
Merge pull request #29 from devchat-ai/fix-initialize-errors
Browse files Browse the repository at this point in the history
Fix initialize errors
  • Loading branch information
pplam authored Dec 13, 2023
2 parents c1e90b2 + f2ef1a3 commit 02b5074
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
59 changes: 39 additions & 20 deletions src/main/kotlin/ai/devchat/cli/DevChatWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.io.IOException
private const val DEFAULT_LOG_MAX_COUNT = 10000


class CommandExecutionException(message:String): Exception(message)
private suspend fun Process.await(
onOutput: (String) -> Unit,
onError: (String) -> Unit
Expand Down Expand Up @@ -78,13 +79,13 @@ class DevChatWrapper(
val errors = errorLines.joinToString("\n")

if (exitCode != 0) {
throw RuntimeException("Command failure with exit Code: $exitCode, Errors: $errors")
throw CommandExecutionException("Command failure with exit Code: $exitCode, Errors: $errors")
} else {
outputLines.joinToString("\n")
}
} catch (e: IOException) {
Log.error("Failed to execute command: $commands, Exception: $e")
throw RuntimeException("Failed to execute command: $commands", e)
Log.warn("Failed to execute command: $commands, Exception: $e")
throw e
}
}

Expand All @@ -95,15 +96,15 @@ class DevChatWrapper(
): Job {
Log.info("Executing command: ${commands.joinToString(" ")}}")
val exceptionHandler = CoroutineExceptionHandler { _, exception ->
Log.error("Failed to execute command: $commands, Exception: $exception")
throw RuntimeException("Failed to execute command: $commands", exception)
Log.warn("Failed to execute command: $commands, Exception: $exception")
throw CommandExecutionException("Failed to execute command: $commands, $exception")
}
val cmdScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)

return cmdScope.launch(exceptionHandler) {
val exitCode = executeCommand(commands, getEnv(), onOutput, onError)
if (exitCode != 0) {
throw RuntimeException("Command failure with exit Code: $exitCode")
throw CommandExecutionException("Command failure with exit Code: $exitCode")
}
}
}
Expand All @@ -117,20 +118,38 @@ class DevChatWrapper(
subCommand(listOf("prompt"))(flags, callback)
}

val logTopic: (String, Int?) -> JSONArray get() = {topic: String, maxCount: Int? ->
val num: Int = maxCount ?: DEFAULT_LOG_MAX_COUNT
JSON.parseArray(log(mutableListOf(
"topic" to topic,
"max-count" to num.toString()
), null))
}

val run get() = subCommand(listOf("run"))
val log get() = subCommand(listOf("log"))
val topic get() = subCommand(listOf("topic"))

val topicList: JSONArray get() = JSON.parseArray(topic(mutableListOf("list" to null), null))
val commandList: JSONArray get() = JSON.parseArray(run(mutableListOf("list" to null), null))
val topicList: JSONArray get() = try {
val r = topic(mutableListOf("list" to null), null) ?: "[]"
JSON.parseArray(r)
} catch (e: Exception) {
Log.warn("Error list topics: $e")
JSONArray()
}
val commandList: JSONArray get() = try {
val r = run(mutableListOf("list" to null), null) ?: "[]"
JSON.parseArray(r)
} catch (e: Exception) {
Log.warn("Error list commands: $e")
JSONArray()
}

val logTopic: (String, Int?) -> JSONArray get() = {topic: String, maxCount: Int? ->
val num: Int = maxCount ?: DEFAULT_LOG_MAX_COUNT
try {
val r = log(mutableListOf(
"topic" to topic,
"max-count" to num.toString()
), null) ?: "[]"
JSON.parseArray(r)
} catch (e: Exception) {
Log.warn("Error log topic: $e")
JSONArray()
}
}


fun runCommand(subCommands: List<String>?, flags: List<Pair<String, String?>>? = null, callback: ((String) -> Unit)? = null): String? {
Expand All @@ -143,8 +162,8 @@ class DevChatWrapper(
return try {
callback?.let { execCommandAsync(cmd, callback); "" } ?: execCommand(cmd)
} catch (e: Exception) {
Log.error("Failed to run command $cmd: ${e.message}")
throw RuntimeException("Failed to run command $cmd", e)
Log.warn("Failed to run command $cmd: ${e.message}")
throw CommandExecutionException("Failed to run command $cmd, $e")
}
}

Expand All @@ -159,8 +178,8 @@ class DevChatWrapper(
try {
callback?.let { execCommandAsync(cmd, callback); "" } ?: execCommand(cmd)
} catch (e: Exception) {
Log.error("Failed to run command $cmd: ${e.message}")
throw RuntimeException("Failed to run command $cmd", e)
Log.warn("Failed to run command $cmd: ${e.message}")
throw CommandExecutionException("Failed to run command $cmd: ${e.message}")
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/ai/devchat/common/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ object Log {
LOG.error(PREFIX + message)
}

@JvmStatic
fun warn(message: String) {
LOG.warn(PREFIX + message)
}

fun debug(message: String) {
LOG.debug(PREFIX + message)
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/ai/devchat/idea/DevChatSetupThread.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.devchat.idea

import ai.devchat.cli.DevChatConfig
import ai.devchat.cli.DevChatWrapper
import ai.devchat.cli.PythonEnvManager
import ai.devchat.common.DevChatPathUtil.workPath
import ai.devchat.common.Log
Expand All @@ -19,6 +20,7 @@ class DevChatSetupThread(private val project: Project) : Thread() {
val envManager = PythonEnvManager(workPath)
val devChatEnv = envManager.createEnv("devchat", "3.11.4")
devChatEnv.installPackage("devchat", "0.2.10")
DevChatWrapper().run(mutableListOf("update-sys" to null), null)
listOf("sys", "org", "usr")
.map { "$workPath/workflows/$it/requirements.txt" }
.firstOrNull { File(it).exists() }
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/ai/devchat/idea/DevChatToolWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class DevChatToolWindow : ToolWindowFactory, DumbAware {
contentManager.addContent(content)
val devChatThread = DevChatSetupThread(project)
devChatThread.start()
devChatThread.join()
}
}

Expand Down

0 comments on commit 02b5074

Please sign in to comment.