chat: preserve cross-client reconciliation work
This commit is contained in:
@@ -331,11 +331,11 @@ class PipelinesRepository(
|
|||||||
* prompt the operator actually sent — never on their own. Returns true when the
|
* prompt the operator actually sent — never on their own. Returns true when the
|
||||||
* server queued the prompt behind an in-flight turn.
|
* 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.")
|
if (sessionId.isBlank()) throw ProtocolException("Select a chat session first.")
|
||||||
val trimmed = text.trim()
|
val trimmed = text.trim()
|
||||||
if (trimmed.isEmpty()) throw ProtocolException("Enter a message before sending.")
|
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)).chatQueued
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun chatInterrupt(sessionId: String) {
|
suspend fun chatInterrupt(sessionId: String) {
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ data class ChatPromptRequest(
|
|||||||
// client can attribute the message; the server sanitises it and falls back to
|
// client can attribute the message; the server sanitises it and falls back to
|
||||||
// a generic label when it is blank.
|
// a generic label when it is blank.
|
||||||
val client: String = "",
|
val client: String = "",
|
||||||
|
@SerialName("operation_id") val operationId: String = "",
|
||||||
)
|
)
|
||||||
|
|
||||||
/** One stored turn: a user turn is a single text part, an assistant turn holds text/reasoning/tool parts. */
|
/** One stored turn: a user turn is a single text part, an assistant turn holds text/reasoning/tool parts. */
|
||||||
@@ -95,6 +96,7 @@ data class ChatMessage(
|
|||||||
// The sending device's label on a user turn the server echoed, so a resumed
|
// 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.
|
// transcript still says who sent the message. Empty on an assistant turn.
|
||||||
val client: String = "",
|
val client: String = "",
|
||||||
|
@SerialName("operation_id") val operationId: String = "",
|
||||||
val parts: List<ChatPart> = emptyList(),
|
val parts: List<ChatPart> = emptyList(),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -183,6 +185,7 @@ data class ChatEventFrame(
|
|||||||
// The sending device's label on a `user` frame (another client's prompt), so
|
// 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.
|
// the transcript can say who sent it. Empty on the agent's own frames.
|
||||||
val client: String = "",
|
val client: String = "",
|
||||||
|
@SerialName("operation_id") val operationId: String = "",
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -334,10 +337,10 @@ object ChatProtocol {
|
|||||||
* shape a text-only prompt has always sent). Built here (not inline in the
|
* 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.
|
* 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 = ""): Message =
|
||||||
Message(
|
Message(
|
||||||
type = Protocol.CHAT_PROMPT,
|
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),
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1173,11 +1173,12 @@ class AppViewModel(
|
|||||||
private fun postChatPrompt(sessionId: String, text: String, attachments: List<ChatAttachment>) {
|
private fun postChatPrompt(sessionId: String, text: String, attachments: List<ChatAttachment>) {
|
||||||
val body = text.trim()
|
val body = text.trim()
|
||||||
if (body.isEmpty()) return
|
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)
|
chatStore.running(sessionId, true)
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
try {
|
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
|
// The server accepted the prompt (queued or sent); a queued prompt
|
||||||
// is one waiting behind an in-flight turn, and the composer says so
|
// is one waiting behind an in-flight turn, and the composer says so
|
||||||
// instead of blocking input.
|
// instead of blocking input.
|
||||||
|
|||||||
@@ -845,15 +845,40 @@ class ChatStore(
|
|||||||
fun history(sessionId: String, messages: List<ChatMessage>, nextCursor: String = "", hasMore: Boolean = false) {
|
fun history(sessionId: String, messages: List<ChatMessage>, nextCursor: String = "", hasMore: Boolean = false) {
|
||||||
update(sessionId) {
|
update(sessionId) {
|
||||||
val existing = it.messages.associateBy { message -> message.id }
|
val existing = it.messages.associateBy { message -> message.id }
|
||||||
|
val optimisticByOperation = it.messages
|
||||||
|
.asSequence()
|
||||||
|
.filter { message -> message.id.startsWith(LOCAL_MESSAGE_PREFIX) && message.operationId.isNotBlank() }
|
||||||
|
.associateBy { message -> message.operationId }
|
||||||
|
val reconciledIds = messages.asSequence()
|
||||||
|
.mapNotNull { message -> optimisticByOperation[message.operationId]?.id }
|
||||||
|
.toSet()
|
||||||
|
val authoritativeMessages = messages.map { message ->
|
||||||
|
val optimistic = optimisticByOperation[message.operationId]
|
||||||
|
if (optimistic == 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 { optimistic.client },
|
||||||
|
parts = message.parts + optimistic.parts.filter { localPart ->
|
||||||
|
localPart.type == ChatProtocol.PART_FILE && message.parts.none { it.type == ChatProtocol.PART_FILE && it.uri == localPart.uri }
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
val baseline = it.historyBeforeLoad?.associateBy { message -> message.id }
|
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 firstOverlap = it.messages.indexOfFirst { message -> message.id in pageIds }
|
||||||
val olderPrefix = when {
|
val olderPrefix = when {
|
||||||
firstOverlap >= 0 -> it.messages.take(firstOverlap)
|
firstOverlap >= 0 -> it.messages.take(firstOverlap)
|
||||||
it.historyBeforeLoad != null && !it.historyLoadOlder -> it.messages
|
it.historyBeforeLoad != null && !it.historyLoadOlder -> it.messages
|
||||||
else -> emptyList()
|
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
|
// A live event may have extended this message while the read was
|
||||||
// in flight. Preserve only that fold, not stale pre-request cache
|
// in flight. Preserve only that fold, not stale pre-request cache
|
||||||
// which would otherwise hide text missed during a disconnect.
|
// which would otherwise hide text missed during a disconnect.
|
||||||
@@ -988,6 +1013,7 @@ class ChatStore(
|
|||||||
sessionId: String,
|
sessionId: String,
|
||||||
text: String,
|
text: String,
|
||||||
attachments: List<ChatAttachment> = emptyList(),
|
attachments: List<ChatAttachment> = emptyList(),
|
||||||
|
operationId: String = "",
|
||||||
): String? {
|
): String? {
|
||||||
var id: String? = null
|
var id: String? = null
|
||||||
update(sessionId) { thread ->
|
update(sessionId) { thread ->
|
||||||
@@ -996,6 +1022,7 @@ class ChatStore(
|
|||||||
id = id!!,
|
id = id!!,
|
||||||
sessionId = sessionId,
|
sessionId = sessionId,
|
||||||
role = ChatProtocol.ROLE_USER,
|
role = ChatProtocol.ROLE_USER,
|
||||||
|
operationId = operationId,
|
||||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)) +
|
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)) +
|
||||||
attachments.map { attachmentPart(it) },
|
attachments.map { attachmentPart(it) },
|
||||||
)
|
)
|
||||||
@@ -1068,12 +1095,28 @@ class ChatStore(
|
|||||||
val text = frame.part?.text.orEmpty()
|
val text = frame.part?.text.orEmpty()
|
||||||
if (frame.messageId.isEmpty() || text.isEmpty()) return
|
if (frame.messageId.isEmpty() || text.isEmpty()) return
|
||||||
update(frame.sessionId) { thread ->
|
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
|
if (thread.messages.any { it.id == frame.messageId }) return@update thread
|
||||||
val message = ChatMessage(
|
val message = ChatMessage(
|
||||||
id = frame.messageId,
|
id = frame.messageId,
|
||||||
sessionId = frame.sessionId,
|
sessionId = frame.sessionId,
|
||||||
role = ChatProtocol.ROLE_USER,
|
role = ChatProtocol.ROLE_USER,
|
||||||
client = frame.client,
|
client = frame.client,
|
||||||
|
operationId = frame.operationId,
|
||||||
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)),
|
parts = listOf(ChatPart(type = ChatProtocol.PART_TEXT, text = text)),
|
||||||
)
|
)
|
||||||
thread.copy(messages = thread.messages + message)
|
thread.copy(messages = thread.messages + message)
|
||||||
|
|||||||
@@ -317,6 +317,81 @@ class MultiChatStateTest {
|
|||||||
assertEquals(2, store.value.thread("a")!!.messages.size)
|
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")
|
||||||
|
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"),
|
||||||
|
))
|
||||||
|
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"),
|
||||||
|
))
|
||||||
|
// A replayed 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-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 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
|
@Test
|
||||||
fun remoteAnswerClearsTheApprovalCard() {
|
fun remoteAnswerClearsTheApprovalCard() {
|
||||||
val store = ChatStore()
|
val store = ChatStore()
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
# Cross-client message reconciliation
|
||||||
|
|
||||||
|
The chat wire contract now carries a durable prompt operation identity through the
|
||||||
|
optimistic Android message and the server's cross-client user event. Existing
|
||||||
|
shared README/topic and packaged skill references that describe user echoes as
|
||||||
|
connection-local are stale; `docs-fold` should update them. This item does not
|
||||||
|
edit shared documentation.
|
||||||
@@ -218,12 +218,13 @@ type ChatHistoryPage struct {
|
|||||||
// message; the server sanitises it and falls back to a generic label when it is
|
// message; the server sanitises it and falls back to a generic label when it is
|
||||||
// blank.
|
// blank.
|
||||||
type ChatPromptRequest struct {
|
type ChatPromptRequest struct {
|
||||||
SessionID string `json:"session_id"`
|
SessionID string `json:"session_id"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Model string `json:"model,omitempty"`
|
Model string `json:"model,omitempty"`
|
||||||
Agent string `json:"agent,omitempty"`
|
Agent string `json:"agent,omitempty"`
|
||||||
Files []string `json:"files,omitempty"`
|
Files []string `json:"files,omitempty"`
|
||||||
Client string `json:"client,omitempty"`
|
Client string `json:"client,omitempty"`
|
||||||
|
OperationID string `json:"operation_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatMessage is one stored turn returned by chat_messages. A user turn is a
|
// ChatMessage is one stored turn returned by chat_messages. A user turn is a
|
||||||
@@ -239,8 +240,9 @@ type ChatMessage struct {
|
|||||||
// Client is the sending device's label on a user turn the server echoed, so a
|
// Client is the sending device's label on a user turn the server echoed, so a
|
||||||
// resumed transcript still says who sent the message. It is empty on an
|
// resumed transcript still says who sent the message. It is empty on an
|
||||||
// assistant turn.
|
// assistant turn.
|
||||||
Client string `json:"client,omitempty"`
|
Client string `json:"client,omitempty"`
|
||||||
Parts []ChatPart `json:"parts"`
|
OperationID string `json:"operation_id,omitempty"`
|
||||||
|
Parts []ChatPart `json:"parts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
|
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
|
||||||
@@ -328,7 +330,8 @@ type ChatEventFrame struct {
|
|||||||
Shell *ChatShell `json:"shell,omitempty"`
|
Shell *ChatShell `json:"shell,omitempty"`
|
||||||
// Client is the sending device's label on a user frame (ChatEventUser), so a
|
// Client is the sending device's label on a user frame (ChatEventUser), so a
|
||||||
// transcript can say who sent the message. It is empty on assistant frames.
|
// transcript can say who sent the message. It is empty on assistant frames.
|
||||||
Client string `json:"client,omitempty"`
|
Client string `json:"client,omitempty"`
|
||||||
|
OperationID string `json:"operation_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatStep is a progress marker for the current step of a turn.
|
// ChatStep is a progress marker for the current step of a turn.
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ func handleChatMessages(ctx context.Context, request protocol.Message, broker ch
|
|||||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
page.Messages = hub.reconcileHistory(query.SessionID, page.Messages)
|
||||||
response.ChatMessages = page.Messages // legacy peers keep working.
|
response.ChatMessages = page.Messages // legacy peers keep working.
|
||||||
response.ChatHistory = &page
|
response.ChatHistory = &page
|
||||||
_ = write(response)
|
_ = write(response)
|
||||||
|
|||||||
@@ -55,11 +55,12 @@ type chatPart struct {
|
|||||||
// are carried for a user turn the server echoed (a prompt another client sent),
|
// are carried for a user turn the server echoed (a prompt another client sent),
|
||||||
// so a resumed transcript still says who sent it.
|
// so a resumed transcript still says who sent it.
|
||||||
type chatMessage struct {
|
type chatMessage struct {
|
||||||
id string
|
id string
|
||||||
role string
|
role string
|
||||||
client string
|
client string
|
||||||
order []string
|
operationID string
|
||||||
parts map[string]*chatPart
|
order []string
|
||||||
|
parts map[string]*chatPart
|
||||||
}
|
}
|
||||||
|
|
||||||
type chatTurn struct {
|
type chatTurn struct {
|
||||||
@@ -168,12 +169,15 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
|
|||||||
role = protocol.ChatRoleUser
|
role = protocol.ChatRoleUser
|
||||||
client = frame.Client
|
client = frame.Client
|
||||||
}
|
}
|
||||||
message = &chatMessage{id: frame.MessageID, role: role, client: client, parts: map[string]*chatPart{}}
|
message = &chatMessage{id: frame.MessageID, role: role, client: client, operationID: frame.OperationID, parts: map[string]*chatPart{}}
|
||||||
turn.order = append(turn.order, frame.MessageID)
|
turn.order = append(turn.order, frame.MessageID)
|
||||||
turn.messages[frame.MessageID] = message
|
turn.messages[frame.MessageID] = message
|
||||||
} else if message.client == "" && frame.Kind == protocol.ChatEventUser {
|
} else if message.client == "" && frame.Kind == protocol.ChatEventUser {
|
||||||
message.client = frame.Client
|
message.client = frame.Client
|
||||||
}
|
}
|
||||||
|
if message.operationID == "" && frame.Kind == protocol.ChatEventUser {
|
||||||
|
message.operationID = frame.OperationID
|
||||||
|
}
|
||||||
switch frame.Kind {
|
switch frame.Kind {
|
||||||
case protocol.ChatEventUser:
|
case protocol.ChatEventUser:
|
||||||
if frame.Part == nil || frame.Part.ID == "" || frame.Part.Text == "" || turn.overflow {
|
if frame.Part == nil || frame.Part.ID == "" || frame.Part.Text == "" || turn.overflow {
|
||||||
@@ -448,6 +452,7 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
|
|||||||
out.Role = message.role
|
out.Role = message.role
|
||||||
}
|
}
|
||||||
out.Client = message.client
|
out.Client = message.client
|
||||||
|
out.OperationID = message.operationID
|
||||||
for _, partID := range message.order {
|
for _, partID := range message.order {
|
||||||
if part, ok := message.parts[partID]; ok {
|
if part, ok := message.parts[partID]; ok {
|
||||||
piece := part.part
|
piece := part.part
|
||||||
@@ -460,6 +465,43 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
|
|||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reconcileHistory annotates an authoritative page with operation identities
|
||||||
|
// from the retained live user turns. Pairing is by the backend's message order,
|
||||||
|
// never by text, timestamps, or a connection/request id. It is deliberately
|
||||||
|
// conservative: a partial page cannot establish a safe one-to-one mapping.
|
||||||
|
func (h *chatHub) reconcileHistory(sessionID string, messages []protocol.ChatMessage) []protocol.ChatMessage {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
turn := h.turns[sessionID]
|
||||||
|
if turn == nil {
|
||||||
|
return messages
|
||||||
|
}
|
||||||
|
var live []*chatMessage
|
||||||
|
for _, id := range turn.order {
|
||||||
|
message := turn.messages[id]
|
||||||
|
if message != nil && message.role == protocol.ChatRoleUser && message.operationID != "" {
|
||||||
|
live = append(live, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var users []int
|
||||||
|
for i := range messages {
|
||||||
|
if messages[i].Role == protocol.ChatRoleUser {
|
||||||
|
users = append(users, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(users) == 0 || len(users) != len(live) {
|
||||||
|
return messages
|
||||||
|
}
|
||||||
|
out := append([]protocol.ChatMessage(nil), messages...)
|
||||||
|
for i, index := range users {
|
||||||
|
out[index].OperationID = live[i].operationID
|
||||||
|
if out[index].Client == "" {
|
||||||
|
out[index].Client = live[i].client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// shellSnapshot returns the session's still-running shells, in the order they
|
// shellSnapshot returns the session's still-running shells, in the order they
|
||||||
// started, so a client attaching to the session's stream can be re-synced the
|
// started, so a client attaching to the session's stream can be re-synced the
|
||||||
// background processes it cannot have seen. It is the shell half of the resume
|
// background processes it cannot have seen. It is the shell half of the resume
|
||||||
|
|||||||
@@ -100,10 +100,11 @@ func (s *chatSub) close() {
|
|||||||
// request id; write is kept either way so a later broker failure is still
|
// request id; write is kept either way so a later broker failure is still
|
||||||
// reported to the client that sent it.
|
// reported to the client that sent it.
|
||||||
type chatQueuedPrompt struct {
|
type chatQueuedPrompt struct {
|
||||||
text string
|
text string
|
||||||
requestID string
|
operationID string
|
||||||
write func(protocol.Message) error
|
requestID string
|
||||||
acked bool
|
write func(protocol.Message) error
|
||||||
|
acked bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type chatObservation struct {
|
type chatObservation struct {
|
||||||
|
|||||||
@@ -54,16 +54,18 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
|
|||||||
// The user turn every *other* client sees, carrying the sender's label. The
|
// The user turn every *other* client sees, carrying the sender's label. The
|
||||||
// sender's own client appended it locally, so it is excluded below.
|
// sender's own client appended it locally, so it is excluded below.
|
||||||
userFrame := protocol.ChatEventFrame{
|
userFrame := protocol.ChatEventFrame{
|
||||||
SessionID: sessionID,
|
SessionID: sessionID,
|
||||||
MessageID: hub.nextUserMessageID(),
|
MessageID: hub.nextUserMessageID(),
|
||||||
Kind: protocol.ChatEventUser,
|
Kind: protocol.ChatEventUser,
|
||||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
||||||
Client: sub.label,
|
Client: sub.label,
|
||||||
|
OperationID: prompt.OperationID,
|
||||||
}
|
}
|
||||||
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
||||||
text: prompt.Text,
|
text: prompt.Text,
|
||||||
requestID: request.ID,
|
operationID: prompt.OperationID,
|
||||||
write: write,
|
requestID: request.ID,
|
||||||
|
write: write,
|
||||||
})
|
})
|
||||||
if refused {
|
if refused {
|
||||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
|
||||||
|
|||||||
Reference in New Issue
Block a user