Skip to content

Commit c517ab7

Browse files
committed
Improve & simplify DevChatResponse builder and DevChatActionHandler
1 parent 85f8f5e commit c517ab7

4 files changed

Lines changed: 22 additions & 36 deletions

File tree

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

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
package ai.devchat.cli
22

3-
/*
4-
* User: Daniel Hu <tao.hu@merico.dev>
5-
* Date: Mon Oct 16 22:40:06 2023 +0800
6-
*
7-
* Hello! How can I assist you today?
8-
*
9-
* prompt 6e2a0d9b5c15eb33008250fee40383e77e8f80c75d9644b15bda60be256c8010
10-
*/
113
class DevChatResponse(line: String) {
124
var user: String? = null
135
var date: String? = null
146
var message: String? = null
157
var promptHash: String? = null
168

179
init {
18-
if (line.startsWith("User: ") && user == null) {
19-
user = line.substring("User: ".length)
20-
} else if (line.startsWith("Date: ") && date == null) {
21-
date = line.substring("Date: ".length)
22-
// 71 is the length of string
23-
// "prompt 6e2a0d9b5c15eb33008250fee40383e77e8f80c75d9644b15bda60be256c8010"
24-
} else if (line.startsWith("prompt ") && line.length == 71) {
25-
promptHash = line.substring("prompt ".length)
26-
message += "\n"
27-
} else if (!line.isEmpty()) {
28-
if (message == null) {
29-
message = line
30-
} else {
31-
message += """
32-
33-
$line
34-
""".trimIndent()
10+
when {
11+
line.startsWith("User: ") -> user = user ?: line.substring("User: ".length)
12+
line.startsWith("Date: ") -> date = date ?: line.substring("Date: ".length)
13+
// 71 is the length of the prompt hash
14+
line.startsWith("prompt ") && line.length == 71 -> {
15+
promptHash = line.substring("prompt ".length)
16+
message = message?.let { "$it\n" } ?: "\n"
3517
}
18+
line.isNotEmpty() -> message = message?.let { "$it\n$line" } ?: line
3619
}
3720
}
3821

src/main/kotlin/ai/devchat/devchat/DevChatActionHandler.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class DevChatActionHandler private constructor() {
2323
fun handle(
2424
action: String,
2525
jsCallback: String,
26-
callback: (JSONObject) -> Unit,
26+
success: (JSONObject, JSONObject) -> Unit,
27+
fail: (JSONObject, JSONObject) -> Unit,
2728
) {
2829
val response = JSONObject()
2930
response["action"] = action
@@ -34,13 +35,14 @@ class DevChatActionHandler private constructor() {
3435

3536
try {
3637
Log.info("Handling $action request")
37-
callback(payload)
3838
metadata["status"] = "success"
3939
metadata["error"] = ""
40+
success(metadata, payload)
4041
} catch (e: Exception) {
4142
Log.error("Exception occurred while handle action $action: ${e.message}")
4243
metadata["status"] = "error"
4344
metadata["error"] = e.message
45+
fail(metadata, payload)
4446
}
4547
cefBrowser!!.executeJavaScript("$jsCallback($response)", "", 0)
4648
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class LoadConversationRequestHandler(private val handler: DevChatActionHandler)
1111
private var metadata: JSONObject? = null
1212
private var payload: JSONObject? = null
1313

14-
private fun action(res: JSONObject) {
14+
private fun success(resMetadata: JSONObject, resPayload: JSONObject) {
1515
val topicHash = metadata!!.getString("topicHash")
16-
res["reset"] = true
16+
resPayload["reset"] = true
1717
when {
1818
topicHash.isNullOrEmpty() -> ActiveConversation.reset()
19-
topicHash == ActiveConversation.topic -> res["reset"] = false
19+
topicHash == ActiveConversation.topic -> resPayload["reset"] = false
2020
else -> {
2121
val arr = handler.devChat.logTopic(topicHash, null)
2222
// remove request_tokens and response_tokens in the conversations object
@@ -35,8 +35,8 @@ class LoadConversationRequestHandler(private val handler: DevChatActionHandler)
3535
handler.handle(
3636
DevChatActions.LOAD_CONVERSATIONS_RESPONSE,
3737
metadata!!.getString("callback"),
38-
::action
39-
)
38+
::success
39+
) { _, _ -> }
4040
}
4141

4242
override fun setMetadata(metadata: JSONObject) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@ import ai.devchat.devchat.DevChatActions
66
import ai.devchat.idea.settings.DevChatSettingsState
77
import ai.devchat.idea.storage.ActiveConversation
88
import com.alibaba.fastjson.JSONObject
9+
import org.jvnet.staxex.StAxSOAPBody.Payload
910

1011
class LoadHistoryMessagesRequestHandler(private val handler: DevChatActionHandler) : ActionHandler {
1112
private var metadata: JSONObject? = null
1213
private var payload: JSONObject? = null
1314

14-
private fun action(res: JSONObject) {
15+
private fun success(resMetadata: JSONObject, resPayload: JSONObject) {
1516
val pageSize = DevChatSettingsState.instance.maxLogCount
1617
val pageIndex = metadata!!.getInteger("pageIndex") ?: 1
17-
res["messages"] = ActiveConversation.getMessages(pageIndex, pageSize)
18+
resPayload["messages"] = ActiveConversation.getMessages(pageIndex, pageSize)
1819
}
1920

2021
override fun executeAction() {
2122
handler.handle(
2223
DevChatActions.LOAD_HISTORY_MESSAGES_RESPONSE,
2324
metadata!!.getString("callback"),
24-
::action
25-
)
25+
::success,
26+
) { _, _ -> }
2627
}
2728

2829
override fun setMetadata(metadata: JSONObject) {

0 commit comments

Comments
 (0)