Expose native chat compaction activity and retained replay
This commit is contained in:
@@ -749,6 +749,11 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
|
||||
return err
|
||||
}
|
||||
defer body.Close()
|
||||
defer func() {
|
||||
state.mu.Lock()
|
||||
state.Compaction = nil
|
||||
state.mu.Unlock()
|
||||
}()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
var delivery sync.Mutex
|
||||
@@ -759,16 +764,30 @@ func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(pr
|
||||
busy = value
|
||||
state.mu.Lock()
|
||||
state.Active = value
|
||||
if !value {
|
||||
state.Compaction = nil
|
||||
}
|
||||
state.mu.Unlock()
|
||||
}
|
||||
deliver := func(frame protocol.ChatEventFrame) error {
|
||||
delivery.Lock()
|
||||
defer delivery.Unlock()
|
||||
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning {
|
||||
if frame.Kind == protocol.ChatEventStep || frame.Kind == protocol.ChatEventText || frame.Kind == protocol.ChatEventTool || frame.Kind == protocol.ChatEventReasoning || (frame.Compaction != nil && frame.Compaction.Status == "running") {
|
||||
setBusy(true)
|
||||
}
|
||||
return emit(frame)
|
||||
}
|
||||
// Recover a compaction already underway before this SSE attachment. The
|
||||
// event body is already open, so any concurrent terminal event follows this
|
||||
// snapshot in the scanner, rather than being lost between read and subscribe.
|
||||
if compaction := c.readCompaction(ctx, id); compaction != nil {
|
||||
state.mu.Lock()
|
||||
state.Compaction = compaction
|
||||
state.mu.Unlock()
|
||||
if err := deliver(protocol.ChatEventFrame{SessionID: id, Kind: protocol.ChatEventCompaction, Compaction: compaction}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// V2 /api/session/active describes foreground drains, not individual
|
||||
// provider steps. Poll it even after reconnect: a busy drain may have
|
||||
// started before this SSE subscription. Failed readbacks never imply idle.
|
||||
@@ -872,6 +891,7 @@ func isChatEventType(t string) bool {
|
||||
case "session.text.delta", "session.reasoning.delta",
|
||||
"session.step.started", "session.step.streamed", "session.step.ended",
|
||||
"session.usage.updated",
|
||||
"session.compaction.started", "session.compaction.ended", "session.compaction.failed",
|
||||
"session.tool.input.started", "session.tool.input.ended",
|
||||
"session.tool.called", "session.tool.progress",
|
||||
"session.tool.success", "session.tool.failed",
|
||||
@@ -1028,6 +1048,7 @@ func (i *chatShellInfo) shellSessionID() string {
|
||||
// - Forms maps a pending form id to its session, so form.replied/cancelled
|
||||
// (which carry no session) can be attributed to the right chat.
|
||||
type ChatStreamState struct {
|
||||
Compaction *protocol.ChatCompaction
|
||||
mu sync.Mutex
|
||||
Active bool // last observed foreground drain, retained across SSE reconnects
|
||||
LastMessageID string
|
||||
@@ -1157,6 +1178,27 @@ func NormalizeChatEvent(payload []byte, sessionID string, stream *ChatStreamStat
|
||||
if data.SessionID != sessionID {
|
||||
return protocol.ChatEventFrame{}, false, nil
|
||||
}
|
||||
// Verified V2 SessionEvent.Compaction schema (upstream 3fdb13bc):
|
||||
// only sessionID is shared across started/ended/failed; no message id.
|
||||
if strings.HasPrefix(outer.Type, "session.compaction.") {
|
||||
status := "running"
|
||||
if outer.Type == "session.compaction.ended" {
|
||||
status = "completed"
|
||||
}
|
||||
if outer.Type == "session.compaction.failed" {
|
||||
status = "failed"
|
||||
}
|
||||
compaction := &protocol.ChatCompaction{Status: status}
|
||||
if stream != nil {
|
||||
stream.mu.Lock()
|
||||
stream.Compaction = nil
|
||||
if status == "running" {
|
||||
stream.Compaction = compaction
|
||||
}
|
||||
stream.mu.Unlock()
|
||||
}
|
||||
return protocol.ChatEventFrame{SessionID: sessionID, Kind: protocol.ChatEventCompaction, Compaction: compaction}, true, nil
|
||||
}
|
||||
messageID := data.MessageID
|
||||
if messageID == "" {
|
||||
messageID = data.MessageIDAlt
|
||||
|
||||
Reference in New Issue
Block a user