WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// memoryStreamData holds stream events in memory
|
||||
type memoryStreamData struct {
|
||||
events []interfaces.StreamEvent
|
||||
lastUpdated time.Time
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// MemoryStreamManager implements StreamManager using in-memory storage
|
||||
type MemoryStreamManager struct {
|
||||
// Map: sessionID -> messageID -> stream data
|
||||
streams map[string]map[string]*memoryStreamData
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewMemoryStreamManager creates a new in-memory stream manager
|
||||
func NewMemoryStreamManager() *MemoryStreamManager {
|
||||
return &MemoryStreamManager{
|
||||
streams: make(map[string]map[string]*memoryStreamData),
|
||||
}
|
||||
}
|
||||
|
||||
// getOrCreateStream gets or creates stream data
|
||||
func (m *MemoryStreamManager) getOrCreateStream(sessionID, messageID string) *memoryStreamData {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if _, exists := m.streams[sessionID]; !exists {
|
||||
m.streams[sessionID] = make(map[string]*memoryStreamData)
|
||||
}
|
||||
|
||||
if _, exists := m.streams[sessionID][messageID]; !exists {
|
||||
m.streams[sessionID][messageID] = &memoryStreamData{
|
||||
events: make([]interfaces.StreamEvent, 0),
|
||||
lastUpdated: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
return m.streams[sessionID][messageID]
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
if sessionMap, exists := m.streams[sessionID]; exists {
|
||||
return sessionMap[messageID]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppendEvent appends a single event to the stream
|
||||
func (m *MemoryStreamManager) AppendEvent(
|
||||
ctx context.Context,
|
||||
sessionID, messageID string,
|
||||
event interfaces.StreamEvent,
|
||||
) error {
|
||||
stream := m.getOrCreateStream(sessionID, messageID)
|
||||
|
||||
stream.mu.Lock()
|
||||
defer stream.mu.Unlock()
|
||||
|
||||
// Set timestamp if not already set
|
||||
if event.Timestamp.IsZero() {
|
||||
event.Timestamp = time.Now()
|
||||
}
|
||||
|
||||
// Append event
|
||||
stream.events = append(stream.events, event)
|
||||
stream.lastUpdated = time.Now()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetEvents gets events starting from offset
|
||||
// Returns: events slice, next offset, error
|
||||
func (m *MemoryStreamManager) GetEvents(
|
||||
ctx context.Context,
|
||||
sessionID, messageID string,
|
||||
fromOffset int,
|
||||
) ([]interfaces.StreamEvent, int, error) {
|
||||
stream := m.getStream(sessionID, messageID)
|
||||
if stream == nil {
|
||||
// Stream doesn't exist yet
|
||||
return []interfaces.StreamEvent{}, fromOffset, nil
|
||||
}
|
||||
|
||||
stream.mu.RLock()
|
||||
defer stream.mu.RUnlock()
|
||||
|
||||
// Check if offset is beyond current events
|
||||
if fromOffset >= len(stream.events) {
|
||||
return []interfaces.StreamEvent{}, fromOffset, nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
return eventsCopy, nextOffset, nil
|
||||
}
|
||||
|
||||
// Ensure MemoryStreamManager implements StreamManager interface
|
||||
var _ interfaces.StreamManager = (*MemoryStreamManager)(nil)
|
||||
Reference in New Issue
Block a user