refine stream

This commit is contained in:
Jicheng Lu 2025-06-17 22:23:43 -05:00
parent f4a74258c2
commit eeb4f06f63
2 changed files with 29 additions and 47 deletions

View file

@ -23,7 +23,7 @@ public class ChatHubObserver : IObserver<HubObserveData>
public void OnCompleted() public void OnCompleted()
{ {
_logger.LogInformation($"{nameof(ChatHubObserver)} receives complete notification."); _logger.LogWarning($"{nameof(ChatHubObserver)} receives complete notification.");
} }
public void OnError(Exception error) public void OnError(Exception error)
@ -35,10 +35,11 @@ public class ChatHubObserver : IObserver<HubObserveData>
{ {
_services = value.ServiceProvider; _services = value.ServiceProvider;
if (!AllowSendingMessage()) return;
var message = value.Data; var message = value.Data;
var model = new ChatResponseDto(); var model = new ChatResponseDto();
if (value.EventName == BEFORE_RECEIVE_LLM_STREAM_MESSAGE if (value.EventName == BEFORE_RECEIVE_LLM_STREAM_MESSAGE)
|| value.EventName == AFTER_RECEIVE_LLM_STREAM_MESSAGE)
{ {
var conv = _services.GetRequiredService<IConversationService>(); var conv = _services.GetRequiredService<IConversationService>();
model = new ChatResponseDto() model = new ChatResponseDto()
@ -57,37 +58,37 @@ public class ChatHubObserver : IObserver<HubObserveData>
var action = new ConversationSenderActionModel var action = new ConversationSenderActionModel
{ {
ConversationId = conv.ConversationId, ConversationId = conv.ConversationId,
SenderAction = value.EventName == BEFORE_RECEIVE_LLM_STREAM_MESSAGE ? SenderActionEnum.TypingOn : SenderActionEnum.TypingOff SenderAction = SenderActionEnum.TypingOn
}; };
GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult(); GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult();
} }
else if (value.EventName == AFTER_RECEIVE_LLM_STREAM_MESSAGE) else if (value.EventName == AFTER_RECEIVE_LLM_STREAM_MESSAGE)
{ {
//var conv = _services.GetRequiredService<IConversationService>(); var conv = _services.GetRequiredService<IConversationService>();
//model = new ChatResponseDto() model = new ChatResponseDto()
//{ {
// ConversationId = conv.ConversationId, ConversationId = conv.ConversationId,
// MessageId = message.MessageId, MessageId = message.MessageId,
// Text = string.Empty, Text = message.Content,
// Sender = new() Sender = new()
// { {
// FirstName = "AI", FirstName = "AI",
// LastName = "Assistant", LastName = "Assistant",
// Role = AgentRole.Assistant Role = AgentRole.Assistant
// } }
//}; };
//var action = new ConversationSenderActionModel var action = new ConversationSenderActionModel
//{ {
// ConversationId = conv.ConversationId, ConversationId = conv.ConversationId,
// SenderAction = SenderActionEnum.TypingOff SenderAction = SenderActionEnum.TypingOff
//}; };
//GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult(); GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult();
//var storage = _services.GetRequiredService<IConversationStorage>(); var storage = _services.GetRequiredService<IConversationStorage>();
//storage.Append(conv.ConversationId, message); storage.Append(conv.ConversationId, message);
} }
else if (value.EventName == ON_RECEIVE_LLM_STREAM_MESSAGE) else if (value.EventName == ON_RECEIVE_LLM_STREAM_MESSAGE)
{ {
@ -112,27 +113,6 @@ public class ChatHubObserver : IObserver<HubObserveData>
OnReceiveAssistantMessage(value.EventName, model.ConversationId, model).ConfigureAwait(false).GetAwaiter().GetResult(); OnReceiveAssistantMessage(value.EventName, model.ConversationId, model).ConfigureAwait(false).GetAwaiter().GetResult();
} }
private async Task ReceiveLlmStreamResponse(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();
var model = new ChatResponseDto()
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
Function = message.FunctionName,
RichContent = message.SecondaryRichContent ?? message.RichContent,
Data = message.Data,
Sender = new()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
};
await OnReceiveAssistantMessage(ON_RECEIVE_LLM_STREAM_MESSAGE, conv.ConversationId, model);
}
private async Task OnReceiveAssistantMessage(string @event, string conversationId, ChatResponseDto model) private async Task OnReceiveAssistantMessage(string @event, string conversationId, ChatResponseDto model)
{ {
try try

View file

@ -190,6 +190,7 @@ public class ChatCompletionProvider : IChatCompletion
var hub = _services.GetRequiredService<MessageHub>(); var hub = _services.GetRequiredService<MessageHub>();
var response = chatClient.CompleteChatStreamingAsync(messages, options); var response = chatClient.CompleteChatStreamingAsync(messages, options);
var messageId = conversations.LastOrDefault()?.MessageId ?? string.Empty; var messageId = conversations.LastOrDefault()?.MessageId ?? string.Empty;
var allText = string.Empty;
hub.Push(new() hub.Push(new()
{ {
@ -219,6 +220,7 @@ public class ChatCompletionProvider : IChatCompletion
if (choice.ContentUpdate.IsNullOrEmpty()) continue; if (choice.ContentUpdate.IsNullOrEmpty()) continue;
var text = choice.ContentUpdate[0]?.Text ?? string.Empty; var text = choice.ContentUpdate[0]?.Text ?? string.Empty;
allText += text;
_logger.LogInformation(text); _logger.LogInformation(text);
var content = new RoleDialogModel(AgentRole.Assistant, text) var content = new RoleDialogModel(AgentRole.Assistant, text)
@ -243,7 +245,7 @@ public class ChatCompletionProvider : IChatCompletion
{ {
ServiceProvider = _services, ServiceProvider = _services,
EventName = "AfterReceiveLlmStreamMessage", EventName = "AfterReceiveLlmStreamMessage",
Data = new RoleDialogModel(AgentRole.Assistant, string.Empty) Data = new RoleDialogModel(AgentRole.Assistant, allText)
{ {
CurrentAgentId = agent.Id, CurrentAgentId = agent.Id,
MessageId = messageId MessageId = messageId