Skip to content

Commit ab3b429

Browse files
committed
feat: Enhance DevChat API error handling
- Add specific error handling for Insufficient Balance - Implement logging for different API response scenarios - Improve code structure and readability in requestDevChatAPI
1 parent 33f86e2 commit ab3b429

File tree

1 file changed

+43
-28
lines changed
  • src/main/kotlin/ai/devchat/plugin/completion/agent

1 file changed

+43
-28
lines changed

src/main/kotlin/ai/devchat/plugin/completion/agent/Agent.kt

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,52 @@ class Agent(val scope: CoroutineScope) {
156156
}
157157
}
158158

159-
private fun requestDevChatAPI(prompt: String): Flow<CodeCompletionChunk> = flow {
160-
val devChatEndpoint = CONFIG["providers.devchat.api_base"] as? String
161-
val devChatAPIKey = CONFIG["providers.devchat.api_key"] as? String
162-
val endpoint = "$devChatEndpoint/completions"
163-
val endingChunk = "[DONE]"
164-
val payload = mapOf(
165-
"model" to ((CONFIG["complete_model"] as? String) ?: defaultCompletionModel),
166-
"prompt" to prompt,
167-
"stream" to true,
168-
"stop" to listOf("<|endoftext|>", "<|EOT|>", "<file_sep>", "```", "/", "\n\n"),
169-
"temperature" to 0.2
170-
)
171-
val requestBody = gson.toJson(payload).toRequestBody("application/json; charset=utf-8".toMediaType())
172-
val requestBuilder = Request.Builder().url(endpoint).post(requestBody)
173-
requestBuilder.addHeader("Authorization", "Bearer $devChatAPIKey")
174-
requestBuilder.addHeader("Accept", "text/event-stream")
175-
requestBuilder.addHeader("Content-Type", "application/json")
176-
httpClient.newCall(requestBuilder.build()).execute().use { response ->
177-
if (!response.isSuccessful) throw IllegalArgumentException("Unexpected code $response")
178-
response.body?.charStream()?.buffered()?.use {reader ->
179-
reader.lineSequence().asFlow()
180-
.filter {it.isNotEmpty()}
181-
.takeWhile { it.startsWith("data:") }
182-
.map { it.drop(5).trim() }
183-
.takeWhile { it.uppercase() != endingChunk }
184-
.map { gson.fromJson(it, CompletionResponseChunk::class.java) }
185-
.takeWhile {it != null}
186-
.collect { emit(CodeCompletionChunk(it.id, it.choices[0].text!!)) }
159+
private fun requestDevChatAPI(prompt: String): Flow<CodeCompletionChunk> = flow {
160+
val devChatEndpoint = CONFIG["providers.devchat.api_base"] as? String
161+
val devChatAPIKey = CONFIG["providers.devchat.api_key"] as? String
162+
val endpoint = "$devChatEndpoint/completions"
163+
val endingChunk = "[DONE]"
164+
val payload = mapOf(
165+
"model" to ((CONFIG["complete_model"] as? String) ?: defaultCompletionModel),
166+
"prompt" to prompt,
167+
"stream" to true,
168+
"stop" to listOf("<|endoftext|>", "<|EOT|>", "<file_sep>", "```", "/", "\n\n"),
169+
"temperature" to 0.2
170+
)
171+
val requestBody = gson.toJson(payload).toRequestBody("application/json; charset=utf-8".toMediaType())
172+
val requestBuilder = Request.Builder().url(endpoint).post(requestBody)
173+
requestBuilder.addHeader("Authorization", "Bearer $devChatAPIKey")
174+
requestBuilder.addHeader("Accept", "text/event-stream")
175+
requestBuilder.addHeader("Content-Type", "application/json")
176+
177+
httpClient.newCall(requestBuilder.build()).execute().use { response ->
178+
if (!response.isSuccessful) {
179+
val errorBody = response.body?.string() ?: "No error body"
180+
when (response.code) {
181+
500 -> {
182+
if (errorBody.contains("Insufficient Balance")) {
183+
logger.warn("DevChat API error: Insufficient balance. Please check your account.")
184+
} else {
185+
logger.warn("DevChat API server error. Response code: ${response.code}. Body: $errorBody")
186+
}
187+
}
188+
else -> logger.warn("Unexpected response from DevChat API. Code: ${response.code}. Body: $errorBody")
187189
}
190+
return@flow
191+
}
192+
193+
response.body?.charStream()?.buffered()?.use { reader ->
194+
reader.lineSequence().asFlow()
195+
.filter { it.isNotEmpty() }
196+
.takeWhile { it.startsWith("data:") }
197+
.map { it.drop(5).trim() }
198+
.takeWhile { it.uppercase() != endingChunk }
199+
.map { gson.fromJson(it, CompletionResponseChunk::class.java) }
200+
.takeWhile { it != null }
201+
.collect { emit(CodeCompletionChunk(it.id, it.choices[0].text!!)) }
188202
}
189203
}
204+
}
190205

191206
private fun toLines(chunks: Flow<CodeCompletionChunk>): Flow<CodeCompletionChunk> = flow {
192207
var ongoingLine = ""

0 commit comments

Comments
 (0)