2025-08-05 07:08:07 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// memoryStreamData holds stream events in memory
|
|
|
|
|
type memoryStreamData struct {
|
|
|
|
|
events []interfaces.StreamEvent
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
steerEvents []interfaces.StreamEvent // control-plane events, never surfaced on SSE
|
2025-11-07 14:41:41 +00:00
|
|
|
lastUpdated time.Time
|
|
|
|
|
mu sync.RWMutex
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
// liveRunMarker names the run that is currently generating for a session.
|
|
|
|
|
type liveRunMarker struct {
|
|
|
|
|
assistantMessageID string
|
|
|
|
|
requestID string
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// MemoryStreamManager implements StreamManager using in-memory storage
|
2025-08-05 07:08:07 +00:00
|
|
|
type MemoryStreamManager struct {
|
2025-11-07 14:41:41 +00:00
|
|
|
// Map: sessionID -> messageID -> stream data
|
|
|
|
|
streams map[string]map[string]*memoryStreamData
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
// Map: sessionID -> the run currently generating. Single-process only,
|
|
|
|
|
// which is exactly why the memory backend is not suitable for a
|
|
|
|
|
// multi-replica deployment (see stream.NewStreamManager).
|
|
|
|
|
liveRuns map[string]liveRunMarker
|
|
|
|
|
mu sync.RWMutex
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// NewMemoryStreamManager creates a new in-memory stream manager
|
2025-08-05 07:08:07 +00:00
|
|
|
func NewMemoryStreamManager() *MemoryStreamManager {
|
|
|
|
|
return &MemoryStreamManager{
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
streams: make(map[string]map[string]*memoryStreamData),
|
|
|
|
|
liveRuns: make(map[string]liveRunMarker),
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// getOrCreateStream gets or creates stream data
|
|
|
|
|
func (m *MemoryStreamManager) getOrCreateStream(sessionID, messageID string) *memoryStreamData {
|
2025-08-05 07:08:07 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
if _, exists := m.streams[sessionID]; !exists {
|
|
|
|
|
m.streams[sessionID] = make(map[string]*memoryStreamData)
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
if _, exists := m.streams[sessionID][messageID]; !exists {
|
|
|
|
|
m.streams[sessionID][messageID] = &memoryStreamData{
|
|
|
|
|
events: make([]interfaces.StreamEvent, 0),
|
|
|
|
|
lastUpdated: time.Now(),
|
|
|
|
|
}
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
return m.streams[sessionID][messageID]
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// getStream gets existing stream data (returns nil if not found)
|
|
|
|
|
func (m *MemoryStreamManager) getStream(sessionID, messageID string) *memoryStreamData {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
2025-08-05 07:08:07 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
if sessionMap, exists := m.streams[sessionID]; exists {
|
|
|
|
|
return sessionMap[messageID]
|
2025-11-05 15:18:44 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// AppendEvent appends a single event to the stream
|
2025-11-28 08:17:52 +00:00
|
|
|
func (m *MemoryStreamManager) AppendEvent(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
sessionID, messageID string,
|
|
|
|
|
event interfaces.StreamEvent,
|
|
|
|
|
) error {
|
2025-11-07 14:41:41 +00:00
|
|
|
stream := m.getOrCreateStream(sessionID, messageID)
|
2025-11-05 15:18:44 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
stream.mu.Lock()
|
|
|
|
|
defer stream.mu.Unlock()
|
2025-11-05 15:18:44 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// Set timestamp if not already set
|
|
|
|
|
if event.Timestamp.IsZero() {
|
|
|
|
|
event.Timestamp = time.Now()
|
|
|
|
|
}
|
2025-11-05 15:18:44 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// Append event
|
|
|
|
|
stream.events = append(stream.events, event)
|
2025-11-28 08:10:37 +00:00
|
|
|
stream.lastUpdated = time.Now()
|
2025-11-07 14:41:41 +00:00
|
|
|
|
2025-08-05 07:08:07 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// GetEvents gets events starting from offset
|
|
|
|
|
// Returns: events slice, next offset, error
|
2025-11-28 08:17:52 +00:00
|
|
|
func (m *MemoryStreamManager) GetEvents(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
sessionID, messageID string,
|
|
|
|
|
fromOffset int,
|
|
|
|
|
) ([]interfaces.StreamEvent, int, error) {
|
2025-11-07 14:41:41 +00:00
|
|
|
stream := m.getStream(sessionID, messageID)
|
|
|
|
|
if stream == nil {
|
|
|
|
|
// Stream doesn't exist yet
|
|
|
|
|
return []interfaces.StreamEvent{}, fromOffset, nil
|
|
|
|
|
}
|
2025-08-05 07:08:07 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
stream.mu.RLock()
|
|
|
|
|
defer stream.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
// Check if offset is beyond current events
|
|
|
|
|
if fromOffset >= len(stream.events) {
|
|
|
|
|
return []interfaces.StreamEvent{}, fromOffset, nil
|
2025-08-05 07:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// Get events from offset to end
|
|
|
|
|
events := stream.events[fromOffset:]
|
|
|
|
|
nextOffset := len(stream.events)
|
|
|
|
|
|
|
|
|
|
// Return copy of events to avoid race conditions
|
|
|
|
|
eventsCopy := make([]interfaces.StreamEvent, len(events))
|
|
|
|
|
copy(eventsCopy, events)
|
2025-08-05 07:08:07 +00:00
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
return eventsCopy, nextOffset, nil
|
2025-11-28 08:10:37 +00:00
|
|
|
}
|
2025-08-05 07:08:07 +00:00
|
|
|
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
// AppendSteerEvents appends control events to the dedicated steer sub-list.
|
|
|
|
|
func (m *MemoryStreamManager) AppendSteerEvents(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, messageID string,
|
|
|
|
|
events []interfaces.StreamEvent,
|
|
|
|
|
) error {
|
|
|
|
|
stream := m.getOrCreateStream(sessionID, messageID)
|
|
|
|
|
|
|
|
|
|
stream.mu.Lock()
|
|
|
|
|
defer stream.mu.Unlock()
|
|
|
|
|
|
2026-09-09 12:05:47 +00:00
|
|
|
seen := make(map[string]bool, len(stream.steerEvents))
|
|
|
|
|
for _, event := range stream.steerEvents {
|
|
|
|
|
seen[event.ID] = true
|
|
|
|
|
}
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
for i := range events {
|
2026-09-09 12:05:47 +00:00
|
|
|
if events[i].ID != "" && seen[events[i].ID] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
seen[events[i].ID] = true
|
feat(chat): append messages to a running agent turn (#3123)
* feat(stream): persist live-run markers and per-run steer queues
Mid-run sends need a cross-replica pointer to the generating
assistant and a control-plane list that never hits the SSE stream.
* feat(agent): inject queued user messages at ReAct round boundaries
The engine drains the steer sink after each round, persists accepted
rows, and delays Done until after a loop-end inject so the turn can continue.
* feat(session): expose /steer and hand leftover messages to a follow-up turn
Owner-scoped steer routes queue or inject into the live run, single-flight
AgentQA via the live-run marker, and history replay keeps multiple user rows
inside one turn.
* feat(frontend): queue mid-run messages while an agent turn streams
Stop and Send stay visible together, the overlay follows server delivery,
and history splits the transcript around injected user bubbles.
* fix(frontend): type pending and client_id on SteerQueueItem
vue-tsc failed because the overlay sets those fields while POST /steer is in flight.
* fix(session): harden steer persist, handoff, and new_run
Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject
persist idempotent, append-then-delete on rebind, cap overrun rounds,
and stop aborting a live stream when /steer returns new_run.
* fix(session): satisfy pre-push lint on steer paths
Wrap long lines, check discarded Error/Emit results, and keep
context as the first argument so the fork push hook can pass.
---------
Co-authored-by: hailongzhao <hailongzhao@tencent.com>
Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-09-09 08:43:33 +00:00
|
|
|
if events[i].Timestamp.IsZero() {
|
|
|
|
|
events[i].Timestamp = time.Now()
|
|
|
|
|
}
|
|
|
|
|
stream.steerEvents = append(stream.steerEvents, events[i])
|
|
|
|
|
}
|
|
|
|
|
stream.lastUpdated = time.Now()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSteerEvents drains the steer sub-list starting fromOffset.
|
|
|
|
|
func (m *MemoryStreamManager) GetSteerEvents(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, messageID string,
|
|
|
|
|
fromOffset int,
|
|
|
|
|
) ([]interfaces.StreamEvent, int, error) {
|
|
|
|
|
stream := m.getStream(sessionID, messageID)
|
|
|
|
|
if stream == nil {
|
|
|
|
|
return []interfaces.StreamEvent{}, fromOffset, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.mu.RLock()
|
|
|
|
|
defer stream.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
if fromOffset >= len(stream.steerEvents) {
|
|
|
|
|
return []interfaces.StreamEvent{}, fromOffset, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
events := stream.steerEvents[fromOffset:]
|
|
|
|
|
nextOffset := len(stream.steerEvents)
|
|
|
|
|
eventsCopy := make([]interfaces.StreamEvent, len(events))
|
|
|
|
|
copy(eventsCopy, events)
|
|
|
|
|
return eventsCopy, nextOffset, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateSteerEventData merges keys into a queued steer event's Data map.
|
|
|
|
|
func (m *MemoryStreamManager) UpdateSteerEventData(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, messageID, eventID string,
|
|
|
|
|
data map[string]interface{},
|
|
|
|
|
) (bool, error) {
|
|
|
|
|
stream := m.getStream(sessionID, messageID)
|
|
|
|
|
if stream == nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.mu.Lock()
|
|
|
|
|
defer stream.mu.Unlock()
|
|
|
|
|
for i := range stream.steerEvents {
|
|
|
|
|
if stream.steerEvents[i].ID != eventID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// Copy before mutating: GetSteerEvents hands out a shallow copy of the
|
|
|
|
|
// slice, so callers may still hold the old Data map.
|
|
|
|
|
merged := make(map[string]interface{}, len(stream.steerEvents[i].Data)+len(data))
|
|
|
|
|
for k, v := range stream.steerEvents[i].Data {
|
|
|
|
|
merged[k] = v
|
|
|
|
|
}
|
|
|
|
|
for k, v := range data {
|
|
|
|
|
merged[k] = v
|
|
|
|
|
}
|
|
|
|
|
stream.steerEvents[i].Data = merged
|
|
|
|
|
stream.lastUpdated = time.Now()
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteSteerEvent removes a queued steer event by ID.
|
|
|
|
|
func (m *MemoryStreamManager) DeleteSteerEvent(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, messageID, eventID string,
|
|
|
|
|
) (bool, error) {
|
|
|
|
|
stream := m.getStream(sessionID, messageID)
|
|
|
|
|
if stream == nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream.mu.Lock()
|
|
|
|
|
defer stream.mu.Unlock()
|
|
|
|
|
for i := range stream.steerEvents {
|
|
|
|
|
if stream.steerEvents[i].ID != eventID {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if stream.steerEvents[i].Data != nil {
|
|
|
|
|
if consumed, _ := stream.steerEvents[i].Data["consumed"].(bool); consumed {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stream.steerEvents = append(stream.steerEvents[:i], stream.steerEvents[i+1:]...)
|
|
|
|
|
stream.lastUpdated = time.Now()
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetLiveRun records the session's currently generating assistant message.
|
|
|
|
|
func (m *MemoryStreamManager) SetLiveRun(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, assistantMessageID, requestID string,
|
|
|
|
|
) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
if existing, ok := m.liveRuns[sessionID]; ok && existing.assistantMessageID != assistantMessageID {
|
|
|
|
|
return ErrLiveRunExists
|
|
|
|
|
}
|
|
|
|
|
m.liveRuns[sessionID] = liveRunMarker{
|
|
|
|
|
assistantMessageID: assistantMessageID,
|
|
|
|
|
requestID: requestID,
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClaimLiveRun overwrites the live-run marker for follow-up handoff.
|
|
|
|
|
func (m *MemoryStreamManager) ClaimLiveRun(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, assistantMessageID, requestID string,
|
|
|
|
|
) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
m.liveRuns[sessionID] = liveRunMarker{
|
|
|
|
|
assistantMessageID: assistantMessageID,
|
|
|
|
|
requestID: requestID,
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetLiveRun returns the session's generating assistant message, if any.
|
|
|
|
|
func (m *MemoryStreamManager) GetLiveRun(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID string,
|
|
|
|
|
) (string, string, error) {
|
|
|
|
|
m.mu.RLock()
|
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
|
marker, ok := m.liveRuns[sessionID]
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", "", nil
|
|
|
|
|
}
|
|
|
|
|
return marker.assistantMessageID, marker.requestID, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ClearLiveRun drops the marker only when it still points at assistantMessageID.
|
|
|
|
|
func (m *MemoryStreamManager) ClearLiveRun(
|
|
|
|
|
_ context.Context,
|
|
|
|
|
sessionID, assistantMessageID string,
|
|
|
|
|
) error {
|
|
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
if marker, ok := m.liveRuns[sessionID]; ok && marker.assistantMessageID == assistantMessageID {
|
|
|
|
|
delete(m.liveRuns, sessionID)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 14:41:41 +00:00
|
|
|
// Ensure MemoryStreamManager implements StreamManager interface
|
2025-08-05 07:08:07 +00:00
|
|
|
var _ interfaces.StreamManager = (*MemoryStreamManager)(nil)
|