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:
@@ -3,10 +3,13 @@ package chatpipline
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// PluginChatCompletionStream implements streaming chat completion functionality
|
||||
@@ -49,7 +52,16 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
logger.Info(ctx, "Preparing chat messages")
|
||||
chatMessages := prepareMessagesWithHistory(chatManage)
|
||||
|
||||
// Initiate streaming chat model call
|
||||
// EventBus is required for event-driven streaming
|
||||
if chatManage.EventBus == nil {
|
||||
logger.Error(ctx, "EventBus is required but not available")
|
||||
return ErrModelCall.WithError(errors.New("EventBus is required for streaming"))
|
||||
}
|
||||
eventBus := chatManage.EventBus
|
||||
|
||||
logger.Info(ctx, "EventBus detected, enabling event-driven streaming mode")
|
||||
|
||||
// Initiate streaming chat model call with independent context
|
||||
logger.Info(ctx, "Calling chat stream model")
|
||||
responseChan, err := chatModel.ChatStream(ctx, chatMessages, opt)
|
||||
if err != nil {
|
||||
@@ -62,6 +74,49 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context,
|
||||
}
|
||||
|
||||
logger.Info(ctx, "Chat stream initiated successfully")
|
||||
chatManage.ResponseChan = responseChan
|
||||
|
||||
// Start goroutine to consume channel and emit events directly
|
||||
go func() {
|
||||
answerID := fmt.Sprintf("%s-answer", uuid.New().String()[:8])
|
||||
var finalContent string
|
||||
|
||||
for response := range responseChan {
|
||||
// Emit event for each answer chunk
|
||||
if response.ResponseType == types.ResponseTypeAnswer {
|
||||
finalContent += response.Content
|
||||
if err := eventBus.Emit(ctx, types.Event{
|
||||
ID: answerID,
|
||||
Type: types.EventType(event.EventAgentFinalAnswer),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentFinalAnswerData{
|
||||
Content: response.Content,
|
||||
Done: response.Done,
|
||||
},
|
||||
}); err != nil {
|
||||
logger.Errorf(ctx, "Failed to emit answer event: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info(ctx, "Chat stream completed, emitting completion event")
|
||||
|
||||
// Emit completion event when stream finishes
|
||||
// This allows other components to detect stream completion
|
||||
if err := eventBus.Emit(ctx, types.Event{
|
||||
ID: fmt.Sprintf("%s-complete", uuid.New().String()[:8]),
|
||||
Type: types.EventType(event.EventAgentComplete),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentCompleteData{
|
||||
SessionID: chatManage.SessionID,
|
||||
MessageID: chatManage.MessageID,
|
||||
FinalAnswer: finalContent,
|
||||
},
|
||||
}); err != nil {
|
||||
logger.Errorf(ctx, "Failed to emit completion event: %v", err)
|
||||
}
|
||||
|
||||
logger.Info(ctx, "Chat stream completed and completion event emitted")
|
||||
}()
|
||||
|
||||
return next()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user