Skip to content

Commit 4652954

Browse files
committed
style(CustomLLMProvider): simplify methods and improve logging
- Removed unnecessary override method bodies and replaced them with expressions. - Replaced deprecated `println` with logger instance for better logging. - Removed redundant empty lines and magic literals. - Removed TODO comment as the code was cleaned up.
1 parent ffb07be commit 4652954

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomLLMProvider.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
5656
private val messages: MutableList<Message> = mutableListOf()
5757
private val logger = logger<CustomLLMProvider>()
5858

59-
override fun clearMessage() {
60-
messages.clear()
61-
}
59+
override fun clearMessage() = messages.clear()
6260

6361
override fun appendLocalMessage(msg: String, role: ChatRole) {
6462
messages += Message(role.roleName(), msg)
6563
}
6664

67-
override fun prompt(promptText: String): String {
68-
return this.prompt(promptText, "")
69-
}
65+
override fun prompt(promptText: String): String = this.prompt(promptText, "")
7066

7167
override fun stream(promptText: String, systemPrompt: String, keepHistory: Boolean): Flow<String> {
7268
if (!keepHistory) {
@@ -93,10 +89,10 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
9389
client = client.newBuilder().readTimeout(timeout).build()
9490
val call = client.newCall(builder.url(url).post(body).build())
9591

96-
if (autoDevSettingsState.customEngineResponseType == ResponseType.SSE.name) {
97-
return streamSSE(call)
92+
return if (autoDevSettingsState.customEngineResponseType == ResponseType.SSE.name) {
93+
streamSSE(call)
9894
} else {
99-
return streamJson(call)
95+
streamJson(call)
10096
}
10197
}
10298

@@ -204,8 +200,7 @@ fun Request.Builder.appendCustomHeaders(customRequestHeader: String): Request.Bu
204200
}
205201
}
206202
}.onFailure {
207-
// should I warn user?
208-
println("Failed to parse custom request header ${it.message}")
203+
logger<CustomLLMProvider>().warn("Failed to parse custom request header", it)
209204
}
210205
}
211206

@@ -217,14 +212,12 @@ fun JsonObject.updateCustomBody(customRequest: String): JsonObject {
217212
this@updateCustomBody.forEach { u, v -> put(u, v) }
218213

219214
val customRequestJson = Json.parseToJsonElement(customRequest).jsonObject
220-
221215
customRequestJson["customFields"]?.let { customFields ->
222216
customFields.jsonObject.forEach { (key, value) ->
223217
put(key, value.jsonPrimitive)
224218
}
225219
}
226220

227-
228221
// TODO clean code with magic literals
229222
var roleKey = "role"
230223
var contentKey = "message"
@@ -234,8 +227,6 @@ fun JsonObject.updateCustomBody(customRequest: String): JsonObject {
234227
}
235228

236229
val messages: JsonArray = this@updateCustomBody["messages"]?.jsonArray ?: buildJsonArray { }
237-
238-
239230
this.put("messages", buildJsonArray {
240231
messages.forEach { message ->
241232
val role: String = message.jsonObject["role"]?.jsonPrimitive?.content ?: "user"

0 commit comments

Comments
 (0)