Skip to content

Commit

Permalink
Integrate CommandService into SparkApiClient
Browse files Browse the repository at this point in the history
CommandService has been added to SparkApiClient as a parameter. This adjustment allows the application to use this service to supervise the chat flow. The chat screen was modified accordingly to instantiate SparkApiClient using CommandService. This update also increments the package version to 1.0.4.
  • Loading branch information
pwh-pwh committed Dec 8, 2023
1 parent bd1dcf8 commit 75819b0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ compose.desktop {
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb,TargetFormat.Exe)
packageName = "sparkapidesk"
packageVersion = "1.0.3"
packageVersion = "1.0.4"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.coderpwh.sparkapidesk.config.ApiConfig
import dev.coderpwh.sparkapidesk.entity.req.*
import dev.coderpwh.sparkapidesk.entity.resp.RespPayload
import dev.coderpwh.sparkapidesk.pojo.toTextList
import dev.coderpwh.sparkapidesk.service.CommandService
import io.ktor.client.*
import io.ktor.client.plugins.websocket.*
import io.ktor.http.*
Expand All @@ -25,7 +26,7 @@ import javax.crypto.spec.SecretKeySpec
* @Date: 2023/11/28 09:33
* @Description:
*/
class SparkApiClient {
class SparkApiClient(val cmdService: CommandService) {
val client = HttpClient {
install(WebSockets)
}
Expand Down Expand Up @@ -105,6 +106,9 @@ class SparkApiClient {
}

suspend fun simpleChat(msgList:SnapshotStateList<dev.coderpwh.sparkapidesk.pojo.Message>){
if (cmdService.doDispatch(msgList)) {
return
}
client.webSocket(
method = HttpMethod.Get,
host = "${SparkApiClient.host}",
Expand Down Expand Up @@ -142,7 +146,7 @@ class SparkApiClient {

fun main() {
ApiConfig.initConfig()
var sparkApiClient = SparkApiClient()
var sparkApiClient = SparkApiClient(CommandService())
var req = ReqPayload(
header = Header(
ApiConfig.config!!.appId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import dev.coderpwh.sparkapidesk.client.SparkApiClient
import dev.coderpwh.sparkapidesk.pojo.Message
import dev.coderpwh.sparkapidesk.service.CommandService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -51,6 +52,7 @@ fun ChatScreen() {
var textFieldState by remember {
mutableStateOf("")
}
var cmdService = CommandService()
var scope = rememberCoroutineScope()
var rememberLazyListState = rememberLazyListState()
ShowMessage(msgList, modifier = Modifier.align(Alignment.TopCenter)
Expand All @@ -70,7 +72,7 @@ fun ChatScreen() {
textFieldState = ""
scope.launch {
try {
SparkApiClient().simpleChat(msgList)
SparkApiClient(cmdService).simpleChat(msgList)
rememberLazyListState.animateScrollToItem(rememberLazyListState.firstVisibleItemIndex + 2)
} catch (e: Exception) {
msgList.add(Message(UUID.randomUUID().toString(), e.toString(), Date(), "assistant"))
Expand All @@ -94,7 +96,7 @@ fun ChatScreen() {
textFieldState = ""
scope.launch {
try {
SparkApiClient().simpleChat(msgList)
SparkApiClient(cmdService).simpleChat(msgList)
rememberLazyListState.animateScrollToItem(rememberLazyListState.firstVisibleItemIndex + 2)
} catch (e: Exception) {
msgList.add(Message(UUID.randomUUID().toString(), e.toString(), Date(), "assistant"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.coderpwh.sparkapidesk.service

import androidx.compose.runtime.snapshots.SnapshotStateList
import dev.coderpwh.sparkapidesk.pojo.Message
import java.util.*

/**
* @Auther: pangwenhao
* @Date: 2023/12/8 16:40
* @Description:
*/
class CommandService {
fun doDispatch(msgList: SnapshotStateList<Message>):Boolean {
val last = msgList.last()
return when (last.content) {
"/clear" -> {
msgList.clear()
true
}
"/help" -> {
msgList.add(Message(UUID.randomUUID().toString(),"/clear: 清空聊天记录\n/help: 帮助", Date(),"assistant"))
true
}
else -> {
false
}
}
}
}

0 comments on commit 75819b0

Please sign in to comment.