Expose native chat compaction activity and retained replay
This commit is contained in:
+72
-3
@@ -63,6 +63,8 @@ type Chat struct {
|
||||
emitted map[string]bool
|
||||
closed bool
|
||||
connectionErr error
|
||||
compactions map[string]protocol.ChatEventFrame
|
||||
endedTurns map[string]string
|
||||
}
|
||||
|
||||
type pendingForm struct {
|
||||
@@ -194,6 +196,7 @@ func (c *Chat) readLoop() {
|
||||
|
||||
func (c *Chat) failPending(err error) {
|
||||
c.mu.Lock()
|
||||
c.compactions = nil
|
||||
c.connectionErr = err
|
||||
c.approvals = nil
|
||||
c.forms = map[string]pendingForm{}
|
||||
@@ -271,6 +274,42 @@ func (c *Chat) respond(id json.RawMessage, result any) {
|
||||
|
||||
func (c *Chat) emit(frame protocol.ChatEventFrame) {
|
||||
c.mu.Lock()
|
||||
if frame.Kind == protocol.ChatEventCompaction {
|
||||
if frame.Compaction == nil {
|
||||
c.mu.Unlock()
|
||||
return
|
||||
}
|
||||
if frame.NativeTurnID != "" && c.endedTurns[frame.SessionID] == frame.NativeTurnID {
|
||||
c.mu.Unlock()
|
||||
return
|
||||
}
|
||||
if current := c.turns[frame.SessionID]; current != "" && frame.NativeTurnID != "" && current != frame.NativeTurnID {
|
||||
c.mu.Unlock()
|
||||
return
|
||||
}
|
||||
if frame.Compaction.Status == "running" {
|
||||
if c.compactions == nil {
|
||||
c.compactions = map[string]protocol.ChatEventFrame{}
|
||||
}
|
||||
c.compactions[frame.SessionID] = frame
|
||||
} else {
|
||||
previous := c.compactions[frame.SessionID]
|
||||
if previous.Compaction != nil && frame.Compaction.ID != "" && previous.Compaction.ID != frame.Compaction.ID {
|
||||
c.mu.Unlock()
|
||||
return
|
||||
}
|
||||
delete(c.compactions, frame.SessionID)
|
||||
}
|
||||
}
|
||||
if frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
|
||||
delete(c.compactions, frame.SessionID)
|
||||
if frame.Step.TurnID != "" {
|
||||
if c.endedTurns == nil {
|
||||
c.endedTurns = map[string]string{}
|
||||
}
|
||||
c.endedTurns[frame.SessionID] = frame.Step.TurnID
|
||||
}
|
||||
}
|
||||
set := c.subs[frame.SessionID]
|
||||
var targets []chan protocol.ChatEventFrame
|
||||
for ch := range set {
|
||||
@@ -294,6 +333,9 @@ func (c *Chat) subscribe(id string) chan protocol.ChatEventFrame {
|
||||
c.subs[id] = map[chan protocol.ChatEventFrame]struct{}{}
|
||||
}
|
||||
c.subs[id][ch] = struct{}{}
|
||||
if frame, ok := c.compactions[id]; ok {
|
||||
ch <- frame
|
||||
}
|
||||
c.mu.Unlock()
|
||||
return ch
|
||||
}
|
||||
@@ -727,9 +769,19 @@ func (c *Chat) handleNotification(msg rpcMessage) {
|
||||
c.mu.Unlock()
|
||||
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded, completedID))
|
||||
case "item/started":
|
||||
c.handleItem(params.ThreadID, params.ItemID, params.Item, false)
|
||||
c.handleItem(params.ThreadID, params.ItemID, params.Item, false, params.TurnID)
|
||||
case "item/completed":
|
||||
c.handleItem(params.ThreadID, params.ItemID, params.Item, true)
|
||||
c.handleItem(params.ThreadID, params.ItemID, params.Item, true, params.TurnID)
|
||||
case "thread/compacted":
|
||||
// Deprecated ContextCompactedNotification has only threadId/turnId.
|
||||
// Modern item lifecycles are correlated more precisely; never let this
|
||||
// unkeyed legacy duplicate clear a newer modern compaction item.
|
||||
c.mu.Lock()
|
||||
modern := c.compactions[params.ThreadID].Compaction != nil
|
||||
c.mu.Unlock()
|
||||
if !modern {
|
||||
c.emit(protocol.ChatEventFrame{SessionID: params.ThreadID, NativeTurnID: params.TurnID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "completed"}})
|
||||
}
|
||||
case "item/agentMessage/delta", "item/agentMessage/streaming":
|
||||
if params.ItemID != "" && params.Delta != "" {
|
||||
c.markEmitted(params.ItemID)
|
||||
@@ -745,7 +797,7 @@ func (c *Chat) handleNotification(msg rpcMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool) {
|
||||
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool, nativeTurnID ...string) {
|
||||
var record item
|
||||
if err := json.Unmarshal(raw, &record); err != nil {
|
||||
return
|
||||
@@ -754,6 +806,19 @@ func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, complete
|
||||
record.ID = itemID
|
||||
}
|
||||
switch record.Type {
|
||||
case "contextCompaction":
|
||||
if record.ID == "" {
|
||||
return
|
||||
}
|
||||
status := "running"
|
||||
if completed {
|
||||
status = "completed"
|
||||
}
|
||||
frame := protocol.ChatEventFrame{SessionID: threadID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: status, ID: record.ID}}
|
||||
if len(nativeTurnID) > 0 {
|
||||
frame.NativeTurnID = nativeTurnID[0]
|
||||
}
|
||||
c.emit(frame)
|
||||
case "agentMessage":
|
||||
if completed && record.Text != "" && !c.wasEmitted(record.ID) {
|
||||
c.emit(textFrame(threadID, record.ID, protocol.ChatEventText, record.Text))
|
||||
@@ -867,6 +932,10 @@ func (t thread) chatSession() protocol.ChatSession {
|
||||
func (i item) chatMessage(sessionID string) (protocol.ChatMessage, bool) {
|
||||
msg := protocol.ChatMessage{ID: i.ID, SessionID: sessionID}
|
||||
switch i.Type {
|
||||
case "contextCompaction":
|
||||
// Historical ThreadItem has no lifecycle status. It is neither a
|
||||
// synthetic tool nor evidence that compaction is currently running.
|
||||
return protocol.ChatMessage{}, false
|
||||
case "userMessage":
|
||||
msg.Role = protocol.ChatRoleUser
|
||||
if i.ClientID != "" {
|
||||
|
||||
Reference in New Issue
Block a user