chat: preserve cross-client reconciliation work
This commit is contained in:
@@ -218,12 +218,13 @@ type ChatHistoryPage struct {
|
||||
// message; the server sanitises it and falls back to a generic label when it is
|
||||
// blank.
|
||||
type ChatPromptRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Text string `json:"text"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Agent string `json:"agent,omitempty"`
|
||||
Files []string `json:"files,omitempty"`
|
||||
Client string `json:"client,omitempty"`
|
||||
SessionID string `json:"session_id"`
|
||||
Text string `json:"text"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Agent string `json:"agent,omitempty"`
|
||||
Files []string `json:"files,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
|
||||
@@ -239,8 +240,9 @@ type ChatMessage struct {
|
||||
// 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
|
||||
// assistant turn.
|
||||
Client string `json:"client,omitempty"`
|
||||
Parts []ChatPart `json:"parts"`
|
||||
Client string `json:"client,omitempty"`
|
||||
OperationID string `json:"operation_id,omitempty"`
|
||||
Parts []ChatPart `json:"parts"`
|
||||
}
|
||||
|
||||
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
|
||||
@@ -328,7 +330,8 @@ type ChatEventFrame struct {
|
||||
Shell *ChatShell `json:"shell,omitempty"`
|
||||
// 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.
|
||||
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.
|
||||
|
||||
@@ -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()})
|
||||
return
|
||||
}
|
||||
page.Messages = hub.reconcileHistory(query.SessionID, page.Messages)
|
||||
response.ChatMessages = page.Messages // legacy peers keep working.
|
||||
response.ChatHistory = &page
|
||||
_ = write(response)
|
||||
|
||||
@@ -55,11 +55,12 @@ type chatPart struct {
|
||||
// are carried for a user turn the server echoed (a prompt another client sent),
|
||||
// so a resumed transcript still says who sent it.
|
||||
type chatMessage struct {
|
||||
id string
|
||||
role string
|
||||
client string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
id string
|
||||
role string
|
||||
client string
|
||||
operationID string
|
||||
order []string
|
||||
parts map[string]*chatPart
|
||||
}
|
||||
|
||||
type chatTurn struct {
|
||||
@@ -168,12 +169,15 @@ func (h *chatHub) applyLocked(frame protocol.ChatEventFrame) {
|
||||
role = protocol.ChatRoleUser
|
||||
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.messages[frame.MessageID] = message
|
||||
} else if message.client == "" && frame.Kind == protocol.ChatEventUser {
|
||||
message.client = frame.Client
|
||||
}
|
||||
if message.operationID == "" && frame.Kind == protocol.ChatEventUser {
|
||||
message.operationID = frame.OperationID
|
||||
}
|
||||
switch frame.Kind {
|
||||
case protocol.ChatEventUser:
|
||||
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.Client = message.client
|
||||
out.OperationID = message.operationID
|
||||
for _, partID := range message.order {
|
||||
if part, ok := message.parts[partID]; ok {
|
||||
piece := part.part
|
||||
@@ -460,6 +465,43 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
|
||||
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
|
||||
// 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
|
||||
|
||||
@@ -100,10 +100,11 @@ func (s *chatSub) close() {
|
||||
// request id; write is kept either way so a later broker failure is still
|
||||
// reported to the client that sent it.
|
||||
type chatQueuedPrompt struct {
|
||||
text string
|
||||
requestID string
|
||||
write func(protocol.Message) error
|
||||
acked bool
|
||||
text string
|
||||
operationID string
|
||||
requestID string
|
||||
write func(protocol.Message) error
|
||||
acked bool
|
||||
}
|
||||
|
||||
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
|
||||
// sender's own client appended it locally, so it is excluded below.
|
||||
userFrame := protocol.ChatEventFrame{
|
||||
SessionID: sessionID,
|
||||
MessageID: hub.nextUserMessageID(),
|
||||
Kind: protocol.ChatEventUser,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
||||
Client: sub.label,
|
||||
SessionID: sessionID,
|
||||
MessageID: hub.nextUserMessageID(),
|
||||
Kind: protocol.ChatEventUser,
|
||||
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
|
||||
Client: sub.label,
|
||||
OperationID: prompt.OperationID,
|
||||
}
|
||||
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
|
||||
text: prompt.Text,
|
||||
requestID: request.ID,
|
||||
write: write,
|
||||
text: prompt.Text,
|
||||
operationID: prompt.OperationID,
|
||||
requestID: request.ID,
|
||||
write: write,
|
||||
})
|
||||
if refused {
|
||||
_ = 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