Merge branch 'main' into small-actionbar-notifications
# Conflicts: # android/app/src/main/java/net/awan/pipelines/ui/theme/Dimens.kt
This commit is contained in:
@@ -349,11 +349,11 @@ class PipelinesRepository(
|
||||
* prompt the operator actually sent — never on their own. Returns true when the
|
||||
* server queued the prompt behind an in-flight turn.
|
||||
*/
|
||||
suspend fun chatPrompt(sessionId: String, text: String, files: List<String> = emptyList()): Boolean {
|
||||
suspend fun chatPrompt(sessionId: String, text: String, files: List<String> = emptyList(), operationId: String = newRequestKey()): Boolean {
|
||||
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)).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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,8 @@ data class ChatPromptRequest(
|
||||
// client can attribute the message; the server sanitises it and falls back to
|
||||
// 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. */
|
||||
@@ -97,6 +99,7 @@ data class ChatMessage(
|
||||
// The sending device's label on a user turn the server echoed, so a resumed
|
||||
// transcript still says who sent the message. Empty on an assistant turn.
|
||||
val client: String = "",
|
||||
@SerialName("operation_id") val operationId: String = "",
|
||||
val parts: List<ChatPart> = emptyList(),
|
||||
)
|
||||
|
||||
@@ -185,6 +188,7 @@ data class ChatEventFrame(
|
||||
// The sending device's label on a `user` frame (another client's prompt), so
|
||||
// the transcript can say who sent it. Empty on the agent's own frames.
|
||||
val client: String = "",
|
||||
@SerialName("operation_id") val operationId: String = "",
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -358,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 = ""): 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),
|
||||
chatPrompt = ChatPromptRequest(sessionId = sessionId, text = text, files = files, client = client, operationId = operationId, nativeMessageId = nativeMessageId),
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -1224,11 +1224,12 @@ class AppViewModel(
|
||||
private fun postChatPrompt(sessionId: String, text: String, attachments: List<ChatAttachment>) {
|
||||
val body = text.trim()
|
||||
if (body.isEmpty()) return
|
||||
val localId = chatStore.appendLocalUserMessage(sessionId, body, attachments) ?: return
|
||||
val operationId = PipelinesRepository.newRequestKey()
|
||||
val localId = chatStore.appendLocalUserMessage(sessionId, body, attachments, operationId) ?: return
|
||||
chatStore.running(sessionId, true)
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val queued = repository.chatPrompt(sessionId, body, attachmentFiles(attachments))
|
||||
val queued = repository.chatPrompt(sessionId, body, attachmentFiles(attachments), operationId)
|
||||
// The server accepted the prompt (queued or sent); a queued prompt
|
||||
// is one waiting behind an in-flight turn, and the composer says so
|
||||
// instead of blocking input.
|
||||
|
||||
@@ -894,15 +894,45 @@ class ChatStore(
|
||||
fun history(sessionId: String, messages: List<ChatMessage>, nextCursor: String = "", hasMore: Boolean = false) {
|
||||
update(sessionId) {
|
||||
val existing = it.messages.associateBy { message -> message.id }
|
||||
// Operation identity remains valid after a live user event replaces
|
||||
// local-* with the server's synthetic id. Index every user turn,
|
||||
// not only optimistic records, so a later history read can replace
|
||||
// that synthetic id with the provider's authoritative id.
|
||||
val existingUserByOperation = it.messages
|
||||
.asSequence()
|
||||
.filter { message -> message.role == ChatProtocol.ROLE_USER && message.operationId.isNotBlank() }
|
||||
.associateBy { message -> message.operationId }
|
||||
val reconciledIds = messages.asSequence()
|
||||
.mapNotNull { message -> existingUserByOperation[message.operationId]?.id }
|
||||
.toSet()
|
||||
val authoritativeMessages = messages.map { message ->
|
||||
val local = existingUserByOperation[message.operationId]
|
||||
if (local == null || message.role != ChatProtocol.ROLE_USER) {
|
||||
message
|
||||
} else {
|
||||
// History is authoritative for identity and ordering, but it
|
||||
// does not carry the phone's local attachment parts or live
|
||||
// fields. Keep those fields on the same turn while replacing
|
||||
// the temporary local id.
|
||||
message.copy(
|
||||
client = message.client.ifBlank { local.client },
|
||||
parts = message.parts + local.parts.filter { localPart ->
|
||||
message.parts.none { it.type == localPart.type && it.id == localPart.id } &&
|
||||
(localPart.type == ChatProtocol.PART_FILE || localPart.type == ChatProtocol.PART_TEXT || localPart.type == ChatProtocol.PART_REASONING)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
val baseline = it.historyBeforeLoad?.associateBy { message -> message.id }
|
||||
val pageIds = messages.mapTo(mutableSetOf()) { message -> message.id }
|
||||
val pageIds = authoritativeMessages.mapTo(mutableSetOf()) { message -> message.id }
|
||||
val firstOverlap = it.messages.indexOfFirst { message -> message.id in pageIds }
|
||||
val olderPrefix = when {
|
||||
firstOverlap >= 0 -> it.messages.take(firstOverlap)
|
||||
it.historyBeforeLoad != null && !it.historyLoadOlder -> it.messages
|
||||
else -> emptyList()
|
||||
}
|
||||
val merged = (olderPrefix + messages + it.messages).fold(LinkedHashMap<String, ChatMessage>()) { result, message ->
|
||||
val merged = (olderPrefix + authoritativeMessages + it.messages.filterNot { message -> message.id in reconciledIds })
|
||||
.fold(LinkedHashMap<String, ChatMessage>()) { result, message ->
|
||||
// A live event may have extended this message while the read was
|
||||
// in flight. Preserve only that fold, not stale pre-request cache
|
||||
// which would otherwise hide text missed during a disconnect.
|
||||
@@ -1071,6 +1101,7 @@ class ChatStore(
|
||||
sessionId: String,
|
||||
text: String,
|
||||
attachments: List<ChatAttachment> = emptyList(),
|
||||
operationId: String = "",
|
||||
): String? {
|
||||
var id: String? = null
|
||||
update(sessionId) { thread ->
|
||||
@@ -1079,6 +1110,7 @@ class ChatStore(
|
||||
id = id!!,
|
||||
sessionId = sessionId,
|
||||
role = ChatProtocol.ROLE_USER,
|
||||
operationId = operationId,
|
||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)) +
|
||||
attachments.map { attachmentPart(it) },
|
||||
)
|
||||
@@ -1156,12 +1188,28 @@ class ChatStore(
|
||||
val text = frame.part?.text.orEmpty()
|
||||
if (frame.messageId.isEmpty() || text.isEmpty()) return
|
||||
update(frame.sessionId) { thread ->
|
||||
val optimistic = thread.messages.firstOrNull {
|
||||
it.role == ChatProtocol.ROLE_USER &&
|
||||
it.id.startsWith(LOCAL_MESSAGE_PREFIX) &&
|
||||
frame.operationId.isNotBlank() && it.operationId == frame.operationId
|
||||
}
|
||||
if (optimistic != null) {
|
||||
val replacement = optimistic.copy(id = frame.messageId, client = frame.client.ifBlank { optimistic.client })
|
||||
return@update thread.copy(messages = thread.messages.map { if (it.id == optimistic.id) replacement else it })
|
||||
}
|
||||
// A replay can arrive after authoritative history has already
|
||||
// replaced the optimistic id. Operation identity is a correlation
|
||||
// key, not a second message id.
|
||||
if (frame.operationId.isNotBlank() && thread.messages.any {
|
||||
it.role == ChatProtocol.ROLE_USER && it.operationId == frame.operationId
|
||||
}) return@update thread
|
||||
if (thread.messages.any { it.id == frame.messageId }) return@update thread
|
||||
val message = ChatMessage(
|
||||
id = frame.messageId,
|
||||
sessionId = frame.sessionId,
|
||||
role = ChatProtocol.ROLE_USER,
|
||||
client = frame.client,
|
||||
operationId = frame.operationId,
|
||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)),
|
||||
)
|
||||
thread.copy(messages = thread.messages + message)
|
||||
|
||||
@@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.selection.selectable
|
||||
import androidx.compose.foundation.selection.selectableGroup
|
||||
import androidx.compose.material.icons.Icons
|
||||
@@ -41,7 +40,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import net.awan.pipelines.ui.theme.DETAIL_MAX_WIDTH_DP
|
||||
import net.awan.pipelines.ui.theme.ICON_RAIL_DP
|
||||
import net.awan.pipelines.ui.theme.SPACING_L
|
||||
import net.awan.pipelines.ui.theme.SPACING_M
|
||||
@@ -229,9 +227,9 @@ private fun RailTab(tab: DetailTab, selected: Boolean, onClick: () -> Unit) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constrains a text tab's single scroll container to a comfortable reading width
|
||||
* on a wide window (centred) instead of letting lines run edge to edge; on a
|
||||
* narrow window the content simply fills the tab.
|
||||
* Gives a tab body the complete safe-area width supplied by its page container.
|
||||
* Internal content remains responsible for any narrower, purpose-specific layout
|
||||
* such as chat bubbles.
|
||||
*/
|
||||
@Composable
|
||||
fun DetailTabBody(
|
||||
@@ -239,18 +237,7 @@ fun DetailTabBody(
|
||||
modifier: Modifier = Modifier,
|
||||
content: @Composable (Modifier) -> Unit,
|
||||
) {
|
||||
if (wide) {
|
||||
Box(modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
|
||||
content(
|
||||
Modifier
|
||||
.fillMaxHeight()
|
||||
.widthIn(max = DETAIL_MAX_WIDTH_DP.dp)
|
||||
.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
content(modifier.fillMaxSize())
|
||||
}
|
||||
content(modifier.fillMaxSize())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -84,14 +84,11 @@ internal val MIN_COVERED_PANE_WIDTH_DP = 112.dp
|
||||
*/
|
||||
internal val DESTINATION_PILL_RESERVE = 88.dp
|
||||
|
||||
// ---- Detail rail and max content width (ui/components/DetailTabs.kt) -------
|
||||
// ---- Detail rail (ui/components/DetailTabs.kt) ------------------------------
|
||||
|
||||
/** The always-visible wide section rail's width (icons with labels). */
|
||||
internal const val WIDE_RAIL_WIDTH_DP = 200
|
||||
|
||||
/** Comfortable max text width for a tab body on a wide window. */
|
||||
const val DETAIL_MAX_WIDTH_DP = 840
|
||||
|
||||
// ---- Workflow graph geometry (ui/graph/WorkflowGraph.kt) -------------------
|
||||
// The graph's node, edge and legend geometry. These are named here like every
|
||||
// other app dimension so the graph file imports them instead of writing raw dp
|
||||
|
||||
@@ -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
|
||||
@@ -317,6 +318,97 @@ class MultiChatStateTest {
|
||||
assertEquals(2, store.value.thread("a")!!.messages.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun canonicalUserEventReplacesOptimisticTurnByOperationIdentity() {
|
||||
val store = ChatStore()
|
||||
store.open(session("a"))
|
||||
val local = store.appendLocalUserMessage("a", "X", operationId = "op-x")!!
|
||||
store.event(ChatEventFrame(
|
||||
sessionId = "a",
|
||||
messageId = "native-x",
|
||||
kind = ChatProtocol.EVENT_USER,
|
||||
operationId = "op-x",
|
||||
client = "Phone",
|
||||
part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
|
||||
))
|
||||
|
||||
val messages = store.value.thread("a")!!.messages
|
||||
assertEquals(listOf("native-x"), messages.map { it.id })
|
||||
assertEquals("Phone", messages.single().client)
|
||||
assertEquals("op-x", messages.single().operationId)
|
||||
assertFalse(messages.any { it.id == local })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun delayedHistoryReconcilesOptimisticTurnAndKeepsDistinctRepeatedText() {
|
||||
val store = ChatStore()
|
||||
store.open(session("a"))
|
||||
val attachmentUri = "content://x"
|
||||
store.appendLocalUserMessage("a", "X", listOf(ChatAttachment(uri = attachmentUri, name = "x.txt", mime = "text/plain")), "op-x")
|
||||
// The sender sees the live event before the provider history catches up.
|
||||
store.event(ChatEventFrame(
|
||||
sessionId = "a", messageId = "native-x", kind = ChatProtocol.EVENT_USER,
|
||||
operationId = "op-x", client = "Phone", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
|
||||
))
|
||||
// History replaces the synthetic event id, but must keep the same turn
|
||||
// (including its attachment) rather than appending a second X.
|
||||
store.history("a", listOf(ChatMessage(
|
||||
id = "native-history-x", sessionId = "a", role = ChatProtocol.ROLE_USER,
|
||||
operationId = "op-x", client = "Phone",
|
||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = "X")),
|
||||
)))
|
||||
// A different client sends Y after the history read.
|
||||
store.event(ChatEventFrame(
|
||||
sessionId = "a", messageId = "native-y", kind = ChatProtocol.EVENT_USER,
|
||||
operationId = "op-y", client = "Computer", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "Y"),
|
||||
))
|
||||
// The delayed original X is an idempotent delivery, not another turn.
|
||||
store.event(ChatEventFrame(
|
||||
sessionId = "a", messageId = "native-x", kind = ChatProtocol.EVENT_USER,
|
||||
operationId = "op-x", client = "Phone", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
|
||||
))
|
||||
// A later, genuinely different X remains a separate message.
|
||||
store.event(ChatEventFrame(
|
||||
sessionId = "a", messageId = "native-x-2", kind = ChatProtocol.EVENT_USER,
|
||||
operationId = "op-x-2", client = "Phone", part = ChatPart(type = ChatProtocol.PART_TEXT, text = "X"),
|
||||
))
|
||||
|
||||
val messages = store.value.thread("a")!!.messages
|
||||
assertEquals(listOf("native-history-x", "native-y", "native-x-2"), messages.map { it.id })
|
||||
assertEquals(listOf("X", "Y", "X"), messages.map { it.parts.first().text })
|
||||
assertEquals(listOf("Phone", "Computer", "Phone"), messages.map { it.client })
|
||||
assertEquals(listOf("op-x", "op-y", "op-x-2"), messages.map { it.operationId })
|
||||
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()
|
||||
store.open(session("a"))
|
||||
store.appendLocalUserMessage(
|
||||
"a", "X",
|
||||
listOf(ChatAttachment(uri = "content://x", name = "x.txt", mime = "text/plain")),
|
||||
"op-x",
|
||||
)
|
||||
store.history("a", listOf(ChatMessage(
|
||||
id = "native-x", sessionId = "a", role = ChatProtocol.ROLE_USER,
|
||||
client = "Phone", operationId = "op-x",
|
||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = "X")),
|
||||
)))
|
||||
|
||||
val message = store.value.thread("a")!!.messages.single()
|
||||
assertEquals("native-x", message.id)
|
||||
assertEquals("op-x", message.operationId)
|
||||
assertTrue(message.parts.any { it.type == ChatProtocol.PART_FILE && it.uri == "content://x" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun remoteAnswerClearsTheApprovalCard() {
|
||||
val store = ChatStore()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.awan.pipelines.ui.components
|
||||
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import java.io.File
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -75,4 +76,17 @@ class DetailTabsTest {
|
||||
state.toggle("evidence-4")
|
||||
assertFalse(state.isExpanded("evidence-4"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun detailTabBodyUsesThePageSafeAreaWithoutAnArtificialWideCap() {
|
||||
val source = File(
|
||||
System.getProperty("user.dir"),
|
||||
"src/main/java/net/awan/pipelines/ui/components/DetailTabs.kt",
|
||||
).readText()
|
||||
|
||||
assertTrue(source.contains("content(modifier.fillMaxSize())"))
|
||||
assertFalse(source.contains("DETAIL_MAX_WIDTH_DP"))
|
||||
assertFalse(source.contains("widthIn(max"))
|
||||
assertFalse(source.contains("contentAlignment = Alignment.TopCenter"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user