|
|
@@ -1,6 +1,7 @@
|
|
|
package com.nova.brain.glass.viewmodel
|
|
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
+import com.nova.brain.glass.model.ChatItem
|
|
|
import com.nova.brain.glass.model.ChatModel
|
|
|
import com.nova.brain.glass.model.ChatModel1
|
|
|
import com.nova.brain.glass.model.ChatModel2
|
|
|
@@ -8,45 +9,97 @@ import com.nova.brain.glass.model.data.ChatData
|
|
|
import com.nova.brain.glass.repository.Service
|
|
|
import com.xuqm.base.common.GsonImplHelp
|
|
|
import com.xuqm.base.di.manager.HttpManager
|
|
|
-import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
|
|
|
+import com.xuqm.base.viewmodel.BaseListViewModel
|
|
|
+import com.xuqm.base.viewmodel.callback.Response
|
|
|
+import android.os.Handler
|
|
|
+import android.os.Looper
|
|
|
+import io.reactivex.disposables.Disposable
|
|
|
import io.reactivex.schedulers.Schedulers
|
|
|
|
|
|
-
|
|
|
-class ChatVM : BaseViewModel() {
|
|
|
+class ChatVM : BaseListViewModel<ChatItem>() {
|
|
|
val result = MutableLiveData<String>()
|
|
|
- private var t = "string"
|
|
|
+ val chatItems: MutableList<ChatItem> = mutableListOf()
|
|
|
+
|
|
|
+ private var currentTask: Disposable? = null
|
|
|
+
|
|
|
+ private val questions = listOf(
|
|
|
+ "我的代办任务有哪些?",
|
|
|
+ "今天天气情况如何?",
|
|
|
+ "当前设备状态如何?",
|
|
|
+ "今日巡检计划是什么?",
|
|
|
+ "有哪些待处理的告警?",
|
|
|
+ "最近的维修记录有哪些?",
|
|
|
+ "当前区域的安全评分是多少?",
|
|
|
+ "有哪些设备需要维护?",
|
|
|
+ "今天有哪些会议安排?",
|
|
|
+ "当前库存状态如何?"
|
|
|
+ )
|
|
|
+ private var questionIndex = 0
|
|
|
+ private var dataSourceReady = false
|
|
|
+ private val mainHandler = Handler(Looper.getMainLooper())
|
|
|
+
|
|
|
+ override fun loadData(page: Int, onResponse: Response<ChatItem>) {
|
|
|
+ dataSourceReady = true
|
|
|
+ if (page == 0) {
|
|
|
+ onResponse.onResponse(ArrayList(chatItems))
|
|
|
+ } else {
|
|
|
+ onResponse.onResponse(ArrayList())
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
fun demoPostSse() {
|
|
|
- t = ""
|
|
|
- HttpManager.getApi(Service::class.java).chat(ChatData("我的代办任务有哪些?"))
|
|
|
+ currentTask?.dispose()
|
|
|
+ currentTask = null
|
|
|
+
|
|
|
+ val question = questions[questionIndex % questions.size]
|
|
|
+ questionIndex++
|
|
|
+
|
|
|
+ chatItems.add(ChatItem(question, ""))
|
|
|
+ if (dataSourceReady) invalidate()
|
|
|
+
|
|
|
+ currentTask = HttpManager.getApi(Service::class.java).chat(ChatData(question))
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.subscribe({ body ->
|
|
|
- var sb = ""
|
|
|
+ var content = ""
|
|
|
+ var type = ""
|
|
|
body.charStream().buffered().use { reader ->
|
|
|
try {
|
|
|
var line: String?
|
|
|
while (reader.readLine().also { line = it } != null) {
|
|
|
val l = line!!
|
|
|
if (l.isNotEmpty()) {
|
|
|
+ if (l.trimStart().startsWith("<")) {
|
|
|
+ // HTML响应(隧道断开),回退问题索引并在主线程重试
|
|
|
+ mainHandler.post {
|
|
|
+ questionIndex--
|
|
|
+ if (chatItems.isNotEmpty()) chatItems.removeAt(chatItems.size - 1)
|
|
|
+ demoPostSse()
|
|
|
+ }
|
|
|
+ return@use
|
|
|
+ }
|
|
|
val json = if (l.startsWith("data:")) l.removePrefix("data:").trim() else l
|
|
|
val model = GsonImplHelp.get().toObject(json, ChatModel::class.java)
|
|
|
if (model.type == null) {
|
|
|
result.postValue(model.msg ?: json)
|
|
|
return@use
|
|
|
}
|
|
|
- if (t != model.type) {
|
|
|
- sb = ""
|
|
|
+ if (type != model.type) {
|
|
|
+ content = ""
|
|
|
}
|
|
|
- t = model.type
|
|
|
+ type = model.type
|
|
|
if (model.type == "string") {
|
|
|
- val model1 =
|
|
|
- GsonImplHelp.get().toObject(json, ChatModel1::class.java)
|
|
|
- sb+=model1.data
|
|
|
+ val model1 = GsonImplHelp.get().toObject(json, ChatModel1::class.java)
|
|
|
+ content += model1.data
|
|
|
} else {
|
|
|
- val model2 =
|
|
|
- GsonImplHelp.get().toObject(json, ChatModel2::class.java)
|
|
|
- sb+=model2.data.content
|
|
|
+ val model2 = GsonImplHelp.get().toObject(json, ChatModel2::class.java)
|
|
|
+ content += model2.data.content
|
|
|
+ }
|
|
|
+ val lastIndex = chatItems.size - 1
|
|
|
+ if (lastIndex >= 0) {
|
|
|
+ chatItems[lastIndex] = chatItems[lastIndex].copy(content = content)
|
|
|
+ notifyItem(lastIndex)
|
|
|
}
|
|
|
- result.postValue(sb)
|
|
|
+ result.postValue(content)
|
|
|
}
|
|
|
}
|
|
|
} catch (e: Exception) {
|
|
|
@@ -55,6 +108,7 @@ class ChatVM : BaseViewModel() {
|
|
|
}
|
|
|
}, { e ->
|
|
|
result.postValue("AI反馈异常: ${e.message}")
|
|
|
- }).adds()
|
|
|
+ })
|
|
|
+ currentTask?.also { add(it) }
|
|
|
}
|
|
|
}
|