fix: voice messages appear in wrong order after reopening chat
Voice auto-send was saving messages to the server but not caching them in Room with correct sortOrder. When navigating away and back, the app loaded from Room cache (empty for voice messages) then from server, where the tree-based ordering could differ from insertion order. Now caches both user and assistant messages in Room with proper sortOrder after the server save succeeds, matching the normal send() flow.
This commit is contained in:
parent
1899f2cff1
commit
44ff82a66d
1 changed files with 22 additions and 2 deletions
|
|
@ -283,9 +283,29 @@ fun ChatScreen(
|
||||||
val cid = convIdDeferred?.await() ?: conversationId ?: return@launch
|
val cid = convIdDeferred?.await() ?: conversationId ?: return@launch
|
||||||
if (conversationId == null) conversationId = cid
|
if (conversationId == null) conversationId = cid
|
||||||
val assistantMsg = messages.lastOrNull { it.id == assistantId } ?: return@launch
|
val assistantMsg = messages.lastOrNull { it.id == assistantId } ?: return@launch
|
||||||
|
val finalContent = assistantMsg.content
|
||||||
|
val finalReasoning = assistantMsg.reasoning.takeIf { it.isNotBlank() }
|
||||||
|
val finalSources = assistantMsg.sources.takeIf { it.isNotEmpty() }
|
||||||
val userSave = SaveMessage(id = userMsg.wireId, parentId = userParentId, role = "user", content = "", attachments = listOf(att))
|
val userSave = SaveMessage(id = userMsg.wireId, parentId = userParentId, role = "user", content = "", attachments = listOf(att))
|
||||||
val assistantSave = SaveMessage(id = assistantMsg.wireId, parentId = userMsg.wireId, role = "assistant", content = assistantMsg.content, model = cfg.model, reasoning = assistantMsg.reasoning.takeIf { it.isNotBlank() }, sources = assistantMsg.sources.takeIf { it.isNotEmpty() }?.map { dev.kaizen.app.net.SearchSource(it.title, it.url, it.snippet) })
|
val assistantSave = SaveMessage(id = assistantWireId, parentId = userMsg.wireId, role = "assistant", content = finalContent, model = cfg.model, reasoning = finalReasoning, sources = finalSources?.map { dev.kaizen.app.net.SearchSource(it.title, it.url, it.snippet) })
|
||||||
KaizenApi.saveMessages(cfg.baseUrl, cfg.token, cid, listOf(userSave, assistantSave))
|
val saved = KaizenApi.saveMessages(cfg.baseUrl, cfg.token, cid, listOf(userSave, assistantSave))
|
||||||
|
if (saved) {
|
||||||
|
val msgCount = messages.size
|
||||||
|
chat.messageRepo.cacheMessages(cid, listOf(
|
||||||
|
dev.kaizen.app.db.MessageEntity(
|
||||||
|
id = userMsg.wireId, conversationId = cid,
|
||||||
|
role = "user", content = "",
|
||||||
|
attachments = listOf(att), sortOrder = msgCount - 2,
|
||||||
|
),
|
||||||
|
dev.kaizen.app.db.MessageEntity(
|
||||||
|
id = assistantWireId, conversationId = cid,
|
||||||
|
role = "assistant", content = finalContent,
|
||||||
|
attachments = emptyList(), sortOrder = msgCount - 1,
|
||||||
|
reasoning = finalReasoning,
|
||||||
|
sources = finalSources?.map { dev.kaizen.app.net.SearchSource(it.title, it.url, it.snippet) } ?: emptyList(),
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}
|
||||||
if (convIdDeferred != null) {
|
if (convIdDeferred != null) {
|
||||||
chat.conversationRepo.insertOptimistic(cid, "(Voice)")
|
chat.conversationRepo.insertOptimistic(cid, "(Voice)")
|
||||||
val title = KaizenApi.generateTitle(cfg.baseUrl, cfg.token, cid)
|
val title = KaizenApi.generateTitle(cfg.baseUrl, cfg.token, cid)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue