add message delete event

This commit is contained in:
Jicheng Lu 2024-03-11 15:32:13 -05:00
parent 656b533523
commit 6c1cebe87b
5 changed files with 27 additions and 1 deletions

View file

@ -67,4 +67,7 @@ public abstract class ConversationHookBase : IConversationHook
public virtual Task OnUserAgentConnectedInitially(Conversation conversation)
=> Task.CompletedTask;
public virtual Task OnMessageDeleted(string conversationId, string messageId)
=> Task.CompletedTask;
}

View file

@ -84,4 +84,12 @@ public interface IConversationHook
/// <param name="conversation"></param>
/// <returns></returns>
Task OnHumanInterventionNeeded(RoleDialogModel message);
/// <summary>
/// Delete message in a conversation
/// </summary>
/// <param name="conversationId"></param>
/// <param name="messageId"></param>
/// <returns></returns>
Task OnMessageDeleted(string conversationId, string messageId);
}

View file

@ -8,6 +8,11 @@ public partial class ConversationService : IConversationService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var isSaved = db.TruncateConversation(conversationId, messageId, true);
var hooks = _services.GetServices<IConversationHook>().ToList();
foreach (var hook in hooks)
{
await hook.OnMessageDeleted(conversationId, messageId);
}
return await Task.FromResult(isSaved);
}
}

View file

@ -30,7 +30,7 @@ public static class BotSharpOpenApiExtensions
bool enableValidation)
{
services.AddScoped<IUserIdentity, UserIdentity>();
services.AddHostedService<ConversationTimeoutService>();
//services.AddHostedService<ConversationTimeoutService>();
// Add bearer authentication
var schema = "MIXED_SCHEME";

View file

@ -96,4 +96,14 @@ public class ChatHubConversationHook : ConversationHookBase
await base.OnResponseGenerated(message);
}
public override async Task OnMessageDeleted(string conversationId, string messageId)
{
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageDeleted", new ChatResponseModel
{
ConversationId = conversationId,
MessageId = messageId
});
await base.OnMessageDeleted(conversationId, messageId);
}
}