@@ -498,7 +498,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
498
498
499
499
private fun mainResetChat () {
500
500
executorService.submit {
501
- backend.resetChat()
501
+ callBackend { backend.resetChat() }
502
502
viewModelScope.launch {
503
503
clearHistory()
504
504
switchToReady()
@@ -528,6 +528,28 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
528
528
modelChatState.value = ModelChatState .Ready
529
529
}
530
530
531
+ private fun switchToFailed () {
532
+ modelChatState.value = ModelChatState .Falied
533
+ }
534
+
535
+ private fun callBackend (callback : () -> Unit ): Boolean {
536
+ try {
537
+ callback()
538
+ } catch (e: Exception ) {
539
+ viewModelScope.launch {
540
+ val stackTrace = e.stackTraceToString()
541
+ val errorMessage = e.localizedMessage
542
+ appendMessage(
543
+ MessageRole .Bot ,
544
+ " MLCChat failed\n\n Stack trace:\n $stackTrace \n\n Error message:\n $errorMessage "
545
+ )
546
+ switchToFailed()
547
+ }
548
+ return false
549
+ }
550
+ return true
551
+ }
552
+
531
553
fun requestResetChat () {
532
554
require(interruptable())
533
555
interruptChat(
@@ -571,7 +593,7 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
571
593
572
594
private fun mainTerminateChat (callback : () -> Unit ) {
573
595
executorService.submit {
574
- backend.unload()
596
+ callBackend { backend.unload() }
575
597
viewModelScope.launch {
576
598
clearHistory()
577
599
switchToReady()
@@ -609,8 +631,10 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
609
631
viewModelScope.launch {
610
632
Toast .makeText(application, " Initialize..." , Toast .LENGTH_SHORT ).show()
611
633
}
612
- backend.unload()
613
- backend.reload(modelLib, modelPath)
634
+ if (! callBackend {
635
+ backend.unload()
636
+ backend.reload(modelLib, modelPath)
637
+ }) return @submit
614
638
viewModelScope.launch {
615
639
Toast .makeText(application, " Ready to chat" , Toast .LENGTH_SHORT ).show()
616
640
switchToReady()
@@ -624,11 +648,13 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
624
648
executorService.submit {
625
649
appendMessage(MessageRole .User , prompt)
626
650
appendMessage(MessageRole .Bot , " " )
627
- backend.prefill(prompt)
651
+ if ( ! callBackend { backend.prefill(prompt) }) return @submit
628
652
while (! backend.stopped()) {
629
- backend.decode()
630
- val newText = backend.getMessage()
631
- viewModelScope.launch { updateMessage(MessageRole .Bot , newText) }
653
+ if (! callBackend {
654
+ backend.decode()
655
+ val newText = backend.message
656
+ viewModelScope.launch { updateMessage(MessageRole .Bot , newText) }
657
+ }) return @submit
632
658
if (modelChatState.value != ModelChatState .Generating ) return @submit
633
659
}
634
660
val runtimeStats = backend.runtimeStatsText()
@@ -653,7 +679,9 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
653
679
}
654
680
655
681
fun interruptable (): Boolean {
656
- return modelChatState.value == ModelChatState .Ready || modelChatState.value == ModelChatState .Generating
682
+ return modelChatState.value == ModelChatState .Ready
683
+ || modelChatState.value == ModelChatState .Generating
684
+ || modelChatState.value == ModelChatState .Falied
657
685
}
658
686
}
659
687
}
@@ -674,7 +702,8 @@ enum class ModelChatState {
674
702
Resetting ,
675
703
Reloading ,
676
704
Terminating ,
677
- Ready
705
+ Ready ,
706
+ Falied
678
707
}
679
708
680
709
enum class MessageRole {
0 commit comments