chat: reconcile history by canonical message identity

This commit is contained in:
2026-10-19 00:08:30 +01:00
parent 3187b0c06e
commit 7da3d54013
13 changed files with 189 additions and 15 deletions
@@ -353,7 +353,7 @@ class PipelinesRepository(
if (sessionId.isBlank()) throw ProtocolException("Select a chat session first.")
val trimmed = text.trim()
if (trimmed.isEmpty()) throw ProtocolException("Enter a message before sending.")
return client.call(ChatProtocol.promptRequest(sessionId, trimmed, files, clientLabel, operationId)).chatQueued
return client.call(ChatProtocol.promptRequest(sessionId, trimmed, files, clientLabel, operationId, nativeMessageId(operationId))).chatQueued
}
suspend fun chatInterrupt(sessionId: String) {
@@ -408,5 +408,12 @@ class PipelinesRepository(
/** A durable key for one chat-create intent; retain it when retrying. */
fun newChatCreateOperationId(): String = "android-chat-" + UUID.randomUUID().toString()
/** OpenCode's optional caller id is the canonical bridge to history. */
fun nativeMessageId(operationId: String): String =
operationId.filter { character -> character.isLetterOrDigit() }.take(120)
.takeIf { it.isNotEmpty() }
?.let { "msg_" + it }
.orEmpty()
}
}
@@ -84,6 +84,7 @@ data class ChatPromptRequest(
// a generic label when it is blank.
val client: String = "",
@SerialName("operation_id") val operationId: String = "",
@SerialName("native_message_id") val nativeMessageId: String = "",
)
/** One stored turn: a user turn is a single text part, an assistant turn holds text/reasoning/tool parts. */
@@ -361,10 +362,10 @@ object ChatProtocol {
* shape a text-only prompt has always sent). Built here (not inline in the
* repository) so production and its unit test share the one wire shape.
*/
fun promptRequest(sessionId: String, text: String, files: List<String> = emptyList(), client: String = "", operationId: String = ""): Message =
fun promptRequest(sessionId: String, text: String, files: List<String> = emptyList(), client: String = "", operationId: String = "", nativeMessageId: String = ""): Message =
Message(
type = Protocol.CHAT_PROMPT,
chatPrompt = ChatPromptRequest(sessionId = sessionId, text = text, files = files, client = client, operationId = operationId),
chatPrompt = ChatPromptRequest(sessionId = sessionId, text = text, files = files, client = client, operationId = operationId, nativeMessageId = nativeMessageId),
)
/**
@@ -1,6 +1,7 @@
package net.awan.pipelines.ui.chat
import net.awan.pipelines.data.protocol.ChatEventFrame
import net.awan.pipelines.data.PipelinesRepository
import net.awan.pipelines.data.protocol.ChatMessage
import net.awan.pipelines.data.protocol.ChatPart
import net.awan.pipelines.data.protocol.ChatPermissionFrame
@@ -380,6 +381,13 @@ class MultiChatStateTest {
assertEquals(attachmentUri, messages.first().parts.firstOrNull { it.type == ChatProtocol.PART_FILE }?.uri)
}
@Test
fun emptyOperationDoesNotInventASharedNativeMessageId() {
assertEquals("", PipelinesRepository.nativeMessageId(""))
assertEquals("", PipelinesRepository.nativeMessageId(" "))
assertEquals("msg_opx", PipelinesRepository.nativeMessageId("op-x"))
}
@Test
fun historyOperationIdentityReplacesLocalIdWithoutDroppingAttachment() {
val store = ChatStore()