Skip to content

Commit 02b5074

Browse files
authored
Merge pull request #29 from devchat-ai/fix-initialize-errors
Fix initialize errors
2 parents c1e90b2 + f2ef1a3 commit 02b5074

4 files changed

Lines changed: 46 additions & 21 deletions

File tree

src/main/kotlin/ai/devchat/cli/DevChatWrapper.kt

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.io.IOException
1313
private const val DEFAULT_LOG_MAX_COUNT = 10000
1414

1515

16+
class CommandExecutionException(message:String): Exception(message)
1617
private suspend fun Process.await(
1718
onOutput: (String) -> Unit,
1819
onError: (String) -> Unit
@@ -78,13 +79,13 @@ class DevChatWrapper(
7879
val errors = errorLines.joinToString("\n")
7980

8081
if (exitCode != 0) {
81-
throw RuntimeException("Command failure with exit Code: $exitCode, Errors: $errors")
82+
throw CommandExecutionException("Command failure with exit Code: $exitCode, Errors: $errors")
8283
} else {
8384
outputLines.joinToString("\n")
8485
}
8586
} catch (e: IOException) {
86-
Log.error("Failed to execute command: $commands, Exception: $e")
87-
throw RuntimeException("Failed to execute command: $commands", e)
87+
Log.warn("Failed to execute command: $commands, Exception: $e")
88+
throw e
8889
}
8990
}
9091

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

103104
return cmdScope.launch(exceptionHandler) {
104105
val exitCode = executeCommand(commands, getEnv(), onOutput, onError)
105106
if (exitCode != 0) {
106-
throw RuntimeException("Command failure with exit Code: $exitCode")
107+
throw CommandExecutionException("Command failure with exit Code: $exitCode")
107108
}
108109
}
109110
}
@@ -117,20 +118,38 @@ class DevChatWrapper(
117118
subCommand(listOf("prompt"))(flags, callback)
118119
}
119120

120-
val logTopic: (String, Int?) -> JSONArray get() = {topic: String, maxCount: Int? ->
121-
val num: Int = maxCount ?: DEFAULT_LOG_MAX_COUNT
122-
JSON.parseArray(log(mutableListOf(
123-
"topic" to topic,
124-
"max-count" to num.toString()
125-
), null))
126-
}
127-
128121
val run get() = subCommand(listOf("run"))
129122
val log get() = subCommand(listOf("log"))
130123
val topic get() = subCommand(listOf("topic"))
131124

132-
val topicList: JSONArray get() = JSON.parseArray(topic(mutableListOf("list" to null), null))
133-
val commandList: JSONArray get() = JSON.parseArray(run(mutableListOf("list" to null), null))
125+
val topicList: JSONArray get() = try {
126+
val r = topic(mutableListOf("list" to null), null) ?: "[]"
127+
JSON.parseArray(r)
128+
} catch (e: Exception) {
129+
Log.warn("Error list topics: $e")
130+
JSONArray()
131+
}
132+
val commandList: JSONArray get() = try {
133+
val r = run(mutableListOf("list" to null), null) ?: "[]"
134+
JSON.parseArray(r)
135+
} catch (e: Exception) {
136+
Log.warn("Error list commands: $e")
137+
JSONArray()
138+
}
139+
140+
val logTopic: (String, Int?) -> JSONArray get() = {topic: String, maxCount: Int? ->
141+
val num: Int = maxCount ?: DEFAULT_LOG_MAX_COUNT
142+
try {
143+
val r = log(mutableListOf(
144+
"topic" to topic,
145+
"max-count" to num.toString()
146+
), null) ?: "[]"
147+
JSON.parseArray(r)
148+
} catch (e: Exception) {
149+
Log.warn("Error log topic: $e")
150+
JSONArray()
151+
}
152+
}
134153

135154

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

@@ -159,8 +178,8 @@ class DevChatWrapper(
159178
try {
160179
callback?.let { execCommandAsync(cmd, callback); "" } ?: execCommand(cmd)
161180
} catch (e: Exception) {
162-
Log.error("Failed to run command $cmd: ${e.message}")
163-
throw RuntimeException("Failed to run command $cmd", e)
181+
Log.warn("Failed to run command $cmd: ${e.message}")
182+
throw CommandExecutionException("Failed to run command $cmd: ${e.message}")
164183
}
165184
}
166185
}

src/main/kotlin/ai/devchat/common/Log.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ object Log {
2929
LOG.error(PREFIX + message)
3030
}
3131

32+
@JvmStatic
33+
fun warn(message: String) {
34+
LOG.warn(PREFIX + message)
35+
}
36+
3237
fun debug(message: String) {
3338
LOG.debug(PREFIX + message)
3439
}

src/main/kotlin/ai/devchat/idea/DevChatSetupThread.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ai.devchat.idea
22

33
import ai.devchat.cli.DevChatConfig
4+
import ai.devchat.cli.DevChatWrapper
45
import ai.devchat.cli.PythonEnvManager
56
import ai.devchat.common.DevChatPathUtil.workPath
67
import ai.devchat.common.Log
@@ -19,6 +20,7 @@ class DevChatSetupThread(private val project: Project) : Thread() {
1920
val envManager = PythonEnvManager(workPath)
2021
val devChatEnv = envManager.createEnv("devchat", "3.11.4")
2122
devChatEnv.installPackage("devchat", "0.2.10")
23+
DevChatWrapper().run(mutableListOf("update-sys" to null), null)
2224
listOf("sys", "org", "usr")
2325
.map { "$workPath/workflows/$it/requirements.txt" }
2426
.firstOrNull { File(it).exists() }

src/main/kotlin/ai/devchat/idea/DevChatToolWindow.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class DevChatToolWindow : ToolWindowFactory, DumbAware {
3030
contentManager.addContent(content)
3131
val devChatThread = DevChatSetupThread(project)
3232
devChatThread.start()
33-
devChatThread.join()
3433
}
3534
}
3635

0 commit comments

Comments
 (0)