Skip to content

Commit

Permalink
Merge pull request #5 from skydoves/feature/error
Browse files Browse the repository at this point in the history
Handle session error cases for the ChatGPT messages
  • Loading branch information
skydoves authored Dec 10, 2022
2 parents 45d18c0 + c737f22 commit db9d308
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.skydoves.chatgpt.core.data.chat.chatGPTUser
import com.skydoves.chatgpt.core.navigation.AppComposeNavigator
import com.skydoves.chatgpt.feature.chat.R
import com.skydoves.chatgpt.feature.chat.theme.ChatGPTStreamTheme
import io.getstream.chat.android.client.models.Channel
import io.getstream.chat.android.common.state.DeletedMessageVisibility
Expand Down Expand Up @@ -108,9 +109,7 @@ fun ChatGPTMessages(

BackHandler(enabled = true, onBack = backAction)

LaunchedEffect(Unit) {
viewModel.sendHelloMessage(channelId)
}
HandleToastMessages(channelId = channelId)

ChatGPTStreamTheme {
Box(modifier = Modifier.fillMaxSize()) {
Expand Down Expand Up @@ -214,3 +213,22 @@ fun ChatGPTMessages(
}
}
}

@Composable
private fun HandleToastMessages(
channelId: String,
viewModel: ChatGPTMessagesViewModel = hiltViewModel()
) {
val context = LocalContext.current
val isError by viewModel.isError.collectAsState()

LaunchedEffect(key1 = Unit) {
viewModel.sendStreamChatMessage(channelId, context.getString(R.string.toast_hello))
}

LaunchedEffect(key1 = isError) {
if (isError) {
viewModel.sendStreamChatMessage(channelId, context.getString(R.string.toast_error_session))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.skydoves.chatgpt.core.model.GPTContent
import com.skydoves.chatgpt.core.model.GPTMessage
import com.skydoves.chatgpt.core.network.BuildConfig.CONVERSATION_ID
import com.skydoves.chatgpt.core.network.BuildConfig.PARENT_MESSAGE_ID
import com.skydoves.sandwich.message
import com.skydoves.sandwich.messageOrNull
import com.skydoves.sandwich.onFailure
import com.skydoves.sandwich.suspendOnSuccess
Expand All @@ -37,6 +38,7 @@ import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

Expand All @@ -51,9 +53,14 @@ class ChatGPTMessagesViewModel @Inject constructor(
it.isNotEmpty()
}.stateIn(viewModelScope, WhileSubscribedOrRetained, false)

fun sendHelloMessage(channelId: String) {
private val mutableIsError: MutableStateFlow<String> = MutableStateFlow("")
val isError: StateFlow<Boolean> = mutableIsError.mapLatest {
it.isNotEmpty()
}.stateIn(viewModelScope, WhileSubscribedOrRetained, false)

fun sendStreamChatMessage(channelId: String, text: String) {
viewModelScope.launch {
sendStreamMessage(channelId, "Hi, I'm OpenAI's ChatGPT. Ask me anything!")
sendStreamMessage(channelId, text)
}
}

Expand All @@ -77,6 +84,8 @@ class ChatGPTMessagesViewModel @Inject constructor(
messageItemSet.value -= text
sendStreamMessage(channelId, data)
}.onFailure {
messageItemSet.value -= messageItemSet.value
mutableIsError.value = message()
streamLog { "Failure: $messageOrNull" }
}
}
Expand Down
2 changes: 2 additions & 0 deletions feature-chat/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<string name="stream_top_bar">Stream</string>
<string name="stream_api_key">mgzfqwft2se8</string>
<string name="toast_success_create_channel">A random ChatGPT channel is created!</string>
<string name="toast_hello">Hi, I\'m OpenAI\'s ChatGPT. Ask me anything!</string>
<string name="toast_error">Something went wrong</string>
<string name="toast_error_session">Something went wrong. Please check out your session and conversation id is valid.</string>
</resources>

0 comments on commit db9d308

Please sign in to comment.