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()
{
_logger.LogInformation($"{nameof(ChatHubObserver)} receives complete notification.");
_logger.LogWarning($"{nameof(ChatHubObserver)} receives complete notification.");
}
public void OnError(Exception error)
@ -35,10 +35,11 @@ public class ChatHubObserver : IObserver<HubObserveData>
{
_services = value.ServiceProvider;
if (!AllowSendingMessage()) return;
var message = value.Data;
var model = new ChatResponseDto();
if (value.EventName == BEFORE_RECEIVE_LLM_STREAM_MESSAGE
|| value.EventName == AFTER_RECEIVE_LLM_STREAM_MESSAGE)
if (value.EventName == BEFORE_RECEIVE_LLM_STREAM_MESSAGE)
{
var conv = _services.GetRequiredService<IConversationService>();
model = new ChatResponseDto()
@ -57,37 +58,37 @@ public class ChatHubObserver : IObserver<HubObserveData>
var action = new ConversationSenderActionModel
{
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();
}
else if (value.EventName == AFTER_RECEIVE_LLM_STREAM_MESSAGE)
{
//var conv = _services.GetRequiredService<IConversationService>();
//model = new ChatResponseDto()
//{
// ConversationId = conv.ConversationId,
// MessageId = message.MessageId,
// Text = string.Empty,
// Sender = new()
// {
// FirstName = "AI",
// LastName = "Assistant",
// Role = AgentRole.Assistant
// }
//};
var conv = _services.GetRequiredService<IConversationService>();
model = new ChatResponseDto()
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Text = message.Content,
Sender = new()
{
FirstName = "AI",
LastName = "Assistant",
Role = AgentRole.Assistant
}
};
//var action = new ConversationSenderActionModel
//{
// ConversationId = conv.ConversationId,
// SenderAction = SenderActionEnum.TypingOff
//};
var action = new ConversationSenderActionModel
{
ConversationId = conv.ConversationId,
SenderAction = SenderActionEnum.TypingOff
};
//GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult();
GenerateSenderAction(conv.ConversationId, action).ConfigureAwait(false).GetAwaiter().GetResult();
//var storage = _services.GetRequiredService<IConversationStorage>();
//storage.Append(conv.ConversationId, message);
var storage = _services.GetRequiredService<IConversationStorage>();
storage.Append(conv.ConversationId, 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();
}
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)
{
try

View file

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