Skip to content

Commit c4edd0d

Browse files
committed
Support workflows & fix setup exceptions
1 parent 79fb298 commit c4edd0d

7 files changed

Lines changed: 85 additions & 44 deletions

File tree

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,88 @@
11
package ai.devchat.cli
22

3-
import ai.devchat.common.DevChatPathUtil.workPath
43
import com.fasterxml.jackson.databind.ObjectMapper
54
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
65
import java.io.File
76

8-
data class ModelConfig(var provider: String? = null, var isStream: Boolean = false)
9-
7+
/*
8+
* default_model: gpt-3.5-turbo
9+
* models:
10+
* gpt-3.5-turbo:
11+
* provider: devchat.ai
12+
* stream: true
13+
*/
1014
class DevChatConfig {
1115
private var configPath: String
1216

1317
// Getters and Setters
14-
private var defaultModel: String? = null
15-
private var models: Map<String, ModelConfig>? = null
18+
var default_model: String? = null
19+
var models: Map<String, ModelConfig>? = null
1620

1721
constructor() {
1822
// default config path
19-
configPath = "$workPath/config.yml"
23+
configPath = System.getProperty("user.home") + "/.chat/config.yml"
2024
}
2125

2226
constructor(configPath: String) {
2327
this.configPath = configPath
2428
}
2529

30+
open class ModelConfig {
31+
// getters and setters
32+
var provider: String? = null
33+
var isStream = false
34+
}
35+
2636
fun writeDefaultConfig() {
27-
defaultModel = "gpt-3.5-turbo"
28-
models = mapOf(
29-
"gpt-3.5-turbo" to ModelConfig(
30-
provider = "devchat.ai", isStream = true
31-
), "gpt-3.5-turbo-16k" to ModelConfig(
32-
provider = "devchat.ai", isStream = true
33-
), "gpt-4" to ModelConfig(
34-
provider = "devchat.ai", isStream = true
35-
), "claude-2" to ModelConfig(
36-
provider = "general", isStream = true
37-
)
38-
)
37+
default_model = "gpt-3.5-turbo"
38+
models = java.util.Map.of(
39+
"gpt-3.5-turbo",
40+
object : ModelConfig() {
41+
init {
42+
provider = "devchat.ai"
43+
isStream = true
44+
}
45+
},
46+
"gpt-3.5-turbo-16k",
47+
object : ModelConfig() {
48+
init {
49+
provider = "devchat.ai"
50+
isStream = true
51+
}
52+
},
53+
"gpt-4",
54+
object : ModelConfig() {
55+
init {
56+
provider = "devchat.ai"
57+
isStream = true
58+
}
59+
},
60+
"claude-2",
61+
object : ModelConfig() {
62+
init {
63+
provider = "general"
64+
isStream = true
65+
}
66+
})
3967
save()
4068
}
4169

4270
fun load(): DevChatConfig {
4371
return try {
44-
ObjectMapper(YAMLFactory()).readValue(File(configPath), DevChatConfig::class.java)
72+
val mapper =
73+
ObjectMapper(YAMLFactory())
74+
mapper.readValue(File(configPath), DevChatConfig::class.java)
4575
} catch (e: Exception) {
4676
throw RuntimeException("Failed to load config", e)
4777
}
4878
}
4979

50-
private fun save() {
80+
fun save() {
5181
try {
52-
ObjectMapper(YAMLFactory()).writeValue(File(configPath), this)
82+
val mapper = ObjectMapper(YAMLFactory())
83+
mapper.writeValue(File(configPath), this)
5384
} catch (e: Exception) {
5485
throw RuntimeException("Failed to save config", e)
5586
}
5687
}
57-
}
88+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ai.devchat.common.Log
55
import ai.devchat.common.Settings
66
import com.alibaba.fastjson.JSON
77
import com.alibaba.fastjson.JSONArray
8+
import com.intellij.util.alsoIfNull
89
import com.intellij.util.containers.addIfNotNull
910
import kotlinx.coroutines.*
1011
import java.io.IOException
@@ -42,14 +43,14 @@ class DevChatWrapper(
4243
private val command: String = DevChatPathUtil.devchatBinPath,
4344
private var apiBase: String? = null,
4445
private var apiKey: String? = null,
45-
private var currentModel: String? = null
46+
private var defaultModel: String? = null
4647
) {
4748
init {
48-
if (apiBase.isNullOrEmpty() || apiKey.isNullOrEmpty() || currentModel.isNullOrEmpty()) {
49+
if (apiBase.isNullOrEmpty() || apiKey.isNullOrEmpty() || defaultModel.isNullOrEmpty()) {
4950
val (key, api, model) = Settings.getAPISettings()
5051
apiBase = apiBase ?: api
5152
apiKey = apiKey ?: key
52-
currentModel = currentModel ?: model
53+
defaultModel = defaultModel ?: model
5354
}
5455
}
5556

@@ -109,7 +110,10 @@ class DevChatWrapper(
109110

110111
val prompt: (MutableList<Pair<String, String?>>, String, ((String) -> Unit)?) -> Unit get() = {
111112
flags: MutableList<Pair<String, String?>>, message: String, callback: ((String) -> Unit)? ->
112-
flags.addAll(listOf("model" to currentModel, "" to message))
113+
flags
114+
.find { it.first == "model" && !it.second.isNullOrEmpty() }
115+
.alsoIfNull { flags.add("model" to defaultModel) }
116+
flags.add("" to message)
113117
subCommand(listOf("prompt"))(flags, callback)
114118
}
115119

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class PythonEnvManager(private val workDir: String) {
1515
private val mambaBinPath: String = "$mambaWorkDir/micromamba"
1616

1717
init {
18-
Log.info("Start configuring the DevChat CLI environment.")
1918
try {
2019
installMamba()
2120
} catch (e: Exception) {
@@ -32,9 +31,13 @@ class PythonEnvManager(private val workDir: String) {
3231
Log.info("Installing Mamba to: " + dstFile.path)
3332
val dstDir = dstFile.parentFile
3433
dstDir.exists() || dstDir.mkdirs() || throw RuntimeException("Unable to create directory: $dstDir")
35-
File(
36-
javaClass.getResource("/tool/mamba/micromamba-$platform/bin/micromamba")!!.file
37-
).copyTo(dstFile)
34+
javaClass.getResource(
35+
"/tool/mamba/micromamba-$platform/bin/micromamba"
36+
)!!.openStream().buffered().use { input ->
37+
dstFile.outputStream().buffered().use { output ->
38+
input.copyTo(output)
39+
}
40+
}
3841
}
3942
if (!dstFile.canExecute() && !dstFile.setExecutable(true)) throw RuntimeException(
4043
"$errPrefix Unable to set executable permissions on: $dstFile"
@@ -64,8 +67,9 @@ class PythonEnvManager(private val workDir: String) {
6467
try {
6568
ProcessBuilder(*command).start().also { process ->
6669
process.inputStream.bufferedReader().forEachLine { Log.info("[Mamba installation] $it") }
67-
if (process.waitFor() != 0) throw RuntimeException(
68-
"Command execution failed with exit code: ${process.exitValue()}"
70+
val exitCode = process.waitFor()
71+
if (exitCode != 0) throw RuntimeException(
72+
"Command execution failed with exit code: ${exitCode}"
6973
)
7074
}
7175
} catch (e: IOException) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ package ai.devchat.common
33
object DevChatPathUtil {
44
@JvmStatic
55
val workPath: String
6-
get() =// return PathManager.getPluginsPath() + "/devchat";
7-
// return System.getProperty("user.home") + "/.chat";
8-
9-
// TODO: change this to the .chat after testing
10-
System.getProperty("user.home") + "/.chat-intellij"
6+
get() = System.getProperty("user.home") + "/.chat"
117
val devchatBinPath: String
12-
get() = workPath + "/mamba" + "/envs/devchat/bin/devchat"
8+
get() = "$workPath/mamba/envs/devchat/bin/devchat"
139
}

src/main/kotlin/ai/devchat/devchat/handler/SendMessageRequestHandler.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class SendMessageRequestHandler(metadata: JSONObject?, payload: JSONObject?) : B
4343
metadata!!.getString("parent")?.takeIf { it.isNotEmpty() }?.let {
4444
flags.add("parent" to it)
4545
}
46+
payload.getString("model")?.takeIf { it.isNotEmpty() }?.let {
47+
flags.add("model" to it)
48+
}
4649

4750

4851
Log.info("Preparing to retrieve the command in the message...")

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package ai.devchat.idea
33
import ai.devchat.cli.DevChatConfig
44
import ai.devchat.cli.PythonEnvManager
55
import ai.devchat.common.DevChatPathUtil.workPath
6-
import ai.devchat.common.Log.error
7-
import ai.devchat.common.Log.info
6+
import ai.devchat.common.Log
87
import ai.devchat.idea.balloon.DevChatNotifier.notifyError
98
import ai.devchat.idea.balloon.DevChatNotifier.notifyInfo
109
import com.intellij.openapi.project.Project
@@ -13,23 +12,26 @@ import java.io.File
1312
class DevChatSetupThread(private val project: Project) : Thread() {
1413
override fun run() {
1514
val workPath = workPath
16-
info("Work path is: $workPath")
15+
Log.info("Work path is: $workPath")
1716
notifyInfo(project, "Starting DevChat initialization...")
1817
try {
18+
Log.info("Start configuring the DevChat CLI environment.")
1919
val envManager = PythonEnvManager(workPath)
2020
val devChatEnv = envManager.createEnv("devchat", "3.11.4")
21-
val workflowEnv = envManager.createEnv("devchat-commands", "3.11.4")
2221
devChatEnv.installPackage("devchat", "0.2.10")
2322
listOf("sys", "org", "usr")
2423
.map { "$workPath/workflows/$it/requirements.txt" }
2524
.firstOrNull { File(it).exists() }
26-
?.let { workflowEnv.installRequirements(it) }
25+
?.let {
26+
val workflowEnv = envManager.createEnv("devchat-commands", "3.11.4")
27+
workflowEnv.installRequirements(it)
28+
}
2729

2830
val config = DevChatConfig()
2931
config.writeDefaultConfig()
3032
notifyInfo(project, "DevChat initialization has completed successfully.")
3133
} catch (e: Exception) {
32-
error("Failed to install DevChat CLI: " + e.message)
34+
Log.error("Failed to install DevChat CLI: " + e.message)
3335
notifyError(
3436
project,
3537
"DevChat initialization has failed. Please check the logs for more details."

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

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

0 commit comments

Comments
 (0)