package chatpipline import ( "context" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/types" "github.com/Tencent/WeKnora/internal/types/interfaces" ) // PluginChatCompletionStream implements streaming chat completion functionality // as a plugin that can be registered to EventManager type PluginChatCompletionStream struct { modelService interfaces.ModelService // Interface for model operations } // NewPluginChatCompletionStream creates a new PluginChatCompletionStream instance // and registers it with the EventManager func NewPluginChatCompletionStream(eventManager *EventManager, modelService interfaces.ModelService, ) *PluginChatCompletionStream { res := &PluginChatCompletionStream{ modelService: modelService, } eventManager.Register(res) return res } // ActivationEvents returns the event types this plugin handles func (p *PluginChatCompletionStream) ActivationEvents() []types.EventType { return []types.EventType{types.CHAT_COMPLETION_STREAM} } // OnEvent handles streaming chat completion events // It prepares the chat model, messages, and initiates streaming response func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError, ) *PluginError { logger.Info(ctx, "Starting chat completion stream") // Prepare chat model and options chatModel, opt, err := prepareChatModel(ctx, p.modelService, chatManage) if err != nil { return ErrGetChatModel.WithError(err) } // Prepare base messages without history logger.Info(ctx, "Preparing chat messages") chatMessages := prepareMessagesWithHistory(chatManage) // Initiate streaming chat model call logger.Info(ctx, "Calling chat stream model") responseChan, err := chatModel.ChatStream(ctx, chatMessages, opt) if err != nil { logger.Errorf(ctx, "Failed to call chat stream model: %v", err) return ErrModelCall.WithError(err) } logger.Info(ctx, "Chat stream initiated successfully") chatManage.ResponseChan = responseChan return next() }