refactor: Implement event-driven streaming and enhance session management
- Removed the StreamEvent accumulation logic, transitioning to an append-only model for event streaming. - Updated StreamManager interface to support appending events and retrieving events with offsets. - Enhanced session handling to support real-time event emission through an EventBus, improving responsiveness. - Refactored chat pipeline components to utilize the new event-driven architecture, ensuring seamless integration of agent responses and knowledge references. - Improved frontend components to handle the new event structure, ensuring accurate rendering of agent interactions and responses.
This commit is contained in:
@@ -20,20 +20,23 @@ type ChatManage struct {
|
||||
RerankTopK int `json:"rerank_top_k"` // Number of top results after reranking
|
||||
RerankThreshold float64 `json:"rerank_threshold"` // Minimum score threshold for reranked results
|
||||
|
||||
ChatModelID string `json:"chat_model_id"` // Model ID for chat completion
|
||||
ChatModelID string `json:"chat_model_id"` // ID of the chat model to use
|
||||
SummaryConfig SummaryConfig `json:"summary_config"` // Configuration for summary generation
|
||||
FallbackStrategy FallbackStrategy `json:"fallback_strategy"` // Strategy when no relevant results are found
|
||||
FallbackResponse string `json:"fallback_response"` // Default response when fallback occurs
|
||||
|
||||
// Internal fields for pipeline data processing
|
||||
SearchResult []*SearchResult `json:"-"` // Results from search phase
|
||||
RerankResult []*SearchResult `json:"-"` // Results after reranking
|
||||
MergeResult []*SearchResult `json:"-"` // Final merged results after all processing
|
||||
Entity []string `json:"-"` // List of identified entities
|
||||
GraphResult *GraphData `json:"-"` // Graph data from search phase
|
||||
UserContent string `json:"-"` // Processed user content
|
||||
ChatResponse *ChatResponse `json:"-"` // Final response from chat model
|
||||
ResponseChan <-chan StreamResponse `json:"-"` // Channel for streaming responses
|
||||
SearchResult []*SearchResult `json:"-"` // Results from search phase
|
||||
RerankResult []*SearchResult `json:"-"` // Results after reranking
|
||||
MergeResult []*SearchResult `json:"-"` // Final merged results after all processing
|
||||
Entity []string `json:"-"` // List of identified entities
|
||||
GraphResult *GraphData `json:"-"` // Graph data from search phase
|
||||
UserContent string `json:"-"` // Processed user content
|
||||
ChatResponse *ChatResponse `json:"-"` // Final response from chat model
|
||||
|
||||
// Event system for streaming responses
|
||||
EventBus EventBusInterface `json:"-"` // EventBus for emitting streaming events
|
||||
MessageID string `json:"-"` // Assistant message ID for event emission
|
||||
}
|
||||
|
||||
// Clone creates a deep copy of the ChatManage object
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// EventHandler is a function that handles events
|
||||
type EventHandler func(ctx context.Context, evt Event) error
|
||||
|
||||
// Event represents an event in the system
|
||||
// This is a simplified version to avoid import cycle with event package
|
||||
type Event struct {
|
||||
ID string // Event ID
|
||||
Type EventType // Event type (uses EventType from chat_manage.go)
|
||||
SessionID string // Session ID
|
||||
Data interface{} // Event data
|
||||
Metadata map[string]interface{} // Event metadata
|
||||
RequestID string // Request ID
|
||||
}
|
||||
|
||||
// EventBusInterface defines the interface for event bus operations
|
||||
// This interface allows types package to use EventBus without importing the concrete type
|
||||
// and avoids circular dependencies
|
||||
type EventBusInterface interface {
|
||||
// On registers an event handler for a specific event type
|
||||
On(eventType EventType, handler EventHandler)
|
||||
|
||||
// Emit publishes an event to all registered handlers
|
||||
Emit(ctx context.Context, evt Event) error
|
||||
}
|
||||
@@ -28,16 +28,17 @@ type SessionService interface {
|
||||
GenerateTitleAsync(ctx context.Context, session *types.Session, userQuery string, eventBus *event.EventBus)
|
||||
// KnowledgeQA performs knowledge-based question answering
|
||||
// knowledgeBaseIDs: list of knowledge base IDs to search (supports multi-KB)
|
||||
// Events are emitted through eventBus (references, answer chunks, completion)
|
||||
KnowledgeQA(ctx context.Context,
|
||||
session *types.Session, query string, knowledgeBaseIDs []string,
|
||||
) ([]*types.SearchResult, <-chan types.StreamResponse, error)
|
||||
session *types.Session, query string, knowledgeBaseIDs []string, assistantMessageID string, eventBus *event.EventBus,
|
||||
) error
|
||||
// KnowledgeQAByEvent performs knowledge-based question answering by event
|
||||
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
|
||||
// SearchKnowledge performs knowledge-based search, without summarization
|
||||
SearchKnowledge(ctx context.Context, knowledgeBaseID, query string) ([]*types.SearchResult, error)
|
||||
// AgentQA performs agent-based question answering with conversation history and streaming support
|
||||
// eventBus is optional - if nil, uses service's default EventBus
|
||||
AgentQA(ctx context.Context, session *types.Session, query string, assistantMessageID string, eventBus *event.EventBus) ([]*types.SearchResult, error)
|
||||
AgentQA(ctx context.Context, session *types.Session, query string, assistantMessageID string, eventBus *event.EventBus) error
|
||||
// ClearContext clears the LLM context for a session
|
||||
ClearContext(ctx context.Context, sessionID string) error
|
||||
}
|
||||
|
||||
@@ -9,43 +9,24 @@ import (
|
||||
|
||||
// StreamEvent represents a single event in the stream
|
||||
type StreamEvent struct {
|
||||
ID string `json:"id"` // Unique event ID for precise replacement
|
||||
Type types.ResponseType `json:"type"` // Event type (thinking, tool_call, tool_result, etc.)
|
||||
Content string `json:"content"` // Event content
|
||||
ID string `json:"id"` // Unique event ID
|
||||
Type types.ResponseType `json:"type"` // Event type (thinking, tool_call, tool_result, references, complete, etc.)
|
||||
Content string `json:"content"` // Event content (chunk for streaming events)
|
||||
Done bool `json:"done"` // Whether this event is done
|
||||
Timestamp time.Time `json:"timestamp"` // When this event occurred
|
||||
Data map[string]interface{} `json:"data,omitempty"` // Additional event data
|
||||
Data map[string]interface{} `json:"data,omitempty"` // Additional event data (references, metadata, etc.)
|
||||
}
|
||||
|
||||
// StreamInfo stream information
|
||||
type StreamInfo struct {
|
||||
SessionID string // session ID
|
||||
RequestID string // request ID
|
||||
Query string // query content
|
||||
Events []StreamEvent // all events in order (for replay on refresh)
|
||||
KnowledgeReferences types.References // knowledge references
|
||||
LastUpdated time.Time // last updated time
|
||||
IsCompleted bool // whether completed
|
||||
}
|
||||
|
||||
// StreamManager stream manager interface
|
||||
// StreamManager stream manager interface - minimal append-only design
|
||||
// All stream state is managed through events: metadata, references, completion, etc.
|
||||
type StreamManager interface {
|
||||
// RegisterStream registers a new stream
|
||||
RegisterStream(ctx context.Context, sessionID, requestID, query string) error
|
||||
// AppendEvent appends a single event to the stream
|
||||
// Uses Redis RPush for O(1) append performance
|
||||
// All event types (thinking, tool_call, references, complete) use this method
|
||||
AppendEvent(ctx context.Context, sessionID, messageID string, event StreamEvent) error
|
||||
|
||||
// PushEvent pushes an event to the stream (append mode)
|
||||
PushEvent(ctx context.Context, sessionID, requestID string, event StreamEvent) error
|
||||
|
||||
// ReplaceEvent replaces an event by ID (for streaming progress updates)
|
||||
// If no matching event exists, it will append the event
|
||||
ReplaceEvent(ctx context.Context, sessionID, requestID string, event StreamEvent) error
|
||||
|
||||
// UpdateReferences updates knowledge references
|
||||
UpdateReferences(ctx context.Context, sessionID, requestID string, references types.References) error
|
||||
|
||||
// CompleteStream completes the stream
|
||||
CompleteStream(ctx context.Context, sessionID, requestID string) error
|
||||
|
||||
// GetStream gets a specific stream (for replay on refresh)
|
||||
GetStream(ctx context.Context, sessionID, requestID string) (*StreamInfo, error)
|
||||
// GetEvents gets events starting from offset
|
||||
// Uses Redis LRange for incremental reads
|
||||
// Returns: events slice, next offset for subsequent reads, error
|
||||
GetEvents(ctx context.Context, sessionID, messageID string, fromOffset int) ([]StreamEvent, int, error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user