Skip to content

Commit

Permalink
Add message type filter and system messages handling
Browse files Browse the repository at this point in the history
Added a new attribute 'type' to the Message class and an enumeration MessageType to accommodate different types of messages. Updated the filters in toTextList() method and adjusted the code in several areas to handle system messages accordingly. The message type is now considered when populating and presenting chat messages to the user.
  • Loading branch information
pwh-pwh committed Dec 8, 2023
1 parent 75819b0 commit dce11bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
12 changes: 10 additions & 2 deletions src/main/kotlin/dev/coderpwh/sparkapidesk/pojo/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ data class Message(
val id: String,
var content: String,
var createTime: Date,
var sender: String
var sender: String,
var type: MessageType = MessageType.AssistantMessage
)

enum class MessageType {
SysMessage,
AssistantMessage
}

fun SnapshotStateList<Message>.toTextList():List<Text> {
return map {
return filter {
it.type != MessageType.SysMessage
}.map {
Text(
role = it.sender,
content = it.content
Expand Down
51 changes: 37 additions & 14 deletions src/main/kotlin/dev/coderpwh/sparkapidesk/screen/ChatScreen.kt
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.pojo.MessageType
import dev.coderpwh.sparkapidesk.service.CommandService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -55,10 +56,14 @@ fun ChatScreen() {
var cmdService = CommandService()
var scope = rememberCoroutineScope()
var rememberLazyListState = rememberLazyListState()
ShowMessage(msgList, modifier = Modifier.align(Alignment.TopCenter)
.fillMaxHeight(0.85f),state = rememberLazyListState)
Row(modifier = Modifier.padding(8.dp)
.align(Alignment.BottomCenter).fillMaxWidth()) {
ShowMessage(
msgList, modifier = Modifier.align(Alignment.TopCenter)
.fillMaxHeight(0.85f), state = rememberLazyListState
)
Row(
modifier = Modifier.padding(8.dp)
.align(Alignment.BottomCenter).fillMaxWidth()
) {

// 输入框
OutlinedTextField(
Expand All @@ -75,11 +80,20 @@ fun ChatScreen() {
SparkApiClient(cmdService).simpleChat(msgList)
rememberLazyListState.animateScrollToItem(rememberLazyListState.firstVisibleItemIndex + 2)
} catch (e: Exception) {
msgList.add(Message(UUID.randomUUID().toString(), e.toString(), Date(), "assistant"))
msgList.add(
Message(
UUID.randomUUID().toString(),
e.toString(),
Date(),
"assistant",
MessageType.SysMessage
)
)
}
}
true
}

else -> false
}
},
Expand All @@ -99,7 +113,15 @@ fun ChatScreen() {
SparkApiClient(cmdService).simpleChat(msgList)
rememberLazyListState.animateScrollToItem(rememberLazyListState.firstVisibleItemIndex + 2)
} catch (e: Exception) {
msgList.add(Message(UUID.randomUUID().toString(), e.toString(), Date(), "assistant"))
msgList.add(
Message(
UUID.randomUUID().toString(),
e.toString(),
Date(),
"assistant",
MessageType.SysMessage
)
)
}
}
}, modifier = Modifier.height(60.dp).padding(top = 9.dp)) {
Expand All @@ -112,12 +134,11 @@ fun ChatScreen() {
}

@Composable
fun ShowMessage(msgList:List<Message>,modifier: Modifier,state:LazyListState) {
fun ShowMessage(msgList: List<Message>, modifier: Modifier, state: LazyListState) {
LazyColumn(
modifier = modifier, state = state
) {
items(items = msgList) {
msg ->
items(items = msgList) { msg ->
if (msg.sender == "assistant") {
MessageLeft(msg)
} else {
Expand All @@ -128,12 +149,12 @@ fun ShowMessage(msgList:List<Message>,modifier: Modifier,state:LazyListState) {
}

@Composable
fun MessageLeft(msg:Message) {
fun MessageLeft(msg: Message) {
Row(
modifier = Modifier.padding(all = 10.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start
){
) {
Image(
painter = painterResource("gpt.png"),
contentDescription = "gpt头像",
Expand All @@ -147,9 +168,11 @@ fun MessageLeft(msg:Message) {
mutableStateOf(true)
}
Column {
Text(text = msg.sender,
Text(
text = msg.sender,
color = MaterialTheme.colors.secondary,
style = MaterialTheme.typography.body2)
style = MaterialTheme.typography.body2
)
Spacer(modifier = Modifier.height(10.dp))
Surface(
shape = RectangleShape,
Expand Down Expand Up @@ -177,7 +200,7 @@ fun MessageLeft(msg:Message) {
}

@Composable
fun MessageRight(msg:Message) {
fun MessageRight(msg: Message) {
Row(
modifier = Modifier
.padding(10.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.coderpwh.sparkapidesk.service

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

/**
Expand All @@ -18,7 +19,7 @@ class CommandService {
true
}
"/help" -> {
msgList.add(Message(UUID.randomUUID().toString(),"/clear: 清空聊天记录\n/help: 帮助", Date(),"assistant"))
msgList.add(Message(UUID.randomUUID().toString(),"/clear: 清空聊天记录\n/help: 帮助", Date(),"assistant",MessageType.SysMessage))
true
}
else -> {
Expand Down

0 comments on commit dce11bc

Please sign in to comment.