2023-11-10 16:01:03 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
2023-11-11 03:50:11 +00:00
|
|
|
namespace BotSharp.OpenAPI.BackgroundServices
|
2023-11-10 16:01:03 +00:00
|
|
|
{
|
|
|
|
|
public class ConversationTimeoutService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ILogger<ConversationTimeoutService> _logger;
|
|
|
|
|
|
|
|
|
|
public ConversationTimeoutService(IServiceProvider services, ILogger<ConversationTimeoutService> logger)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Conversation Timeout Service is running.");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
stoppingToken.ThrowIfCancellationRequested();
|
2024-03-07 07:34:52 +00:00
|
|
|
var delay = Task.Delay(TimeSpan.FromSeconds(10));
|
2023-11-10 16:01:03 +00:00
|
|
|
try
|
|
|
|
|
{
|
2024-03-07 07:34:52 +00:00
|
|
|
//await CloseIdleConversationsAsync(TimeSpan.FromMinutes(10));
|
|
|
|
|
await CleanIdleConversationsAsync();
|
|
|
|
|
|
2023-11-10 16:01:03 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, $"Error occurred closing conversations.");
|
|
|
|
|
}
|
|
|
|
|
await delay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task StopAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Conversation Timeout Service is stopping.");
|
|
|
|
|
await base.StopAsync(stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CloseIdleConversationsAsync(TimeSpan conversationIdleTimeout)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _services.CreateScope();
|
|
|
|
|
var conversationService = scope.ServiceProvider.GetRequiredService<IConversationService>();
|
|
|
|
|
var hooks = scope.ServiceProvider.GetServices<IConversationHook>()
|
|
|
|
|
.OrderBy(x => x.Priority)
|
|
|
|
|
.ToList();
|
|
|
|
|
var moment = DateTime.UtcNow.Add(-conversationIdleTimeout);
|
|
|
|
|
var conversations =
|
|
|
|
|
(await conversationService.GetLastConversations())
|
|
|
|
|
.Where(c => c.CreatedTime <= moment);
|
|
|
|
|
foreach (var conversation in conversations)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = new RoleDialogModel(AgentRole.Assistant, "End the conversation due to timeout.")
|
|
|
|
|
{
|
|
|
|
|
StopCompletion = true,
|
|
|
|
|
FunctionName = "conversation_end"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.OnConversationEnding(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, $"Error occurred closing conversation #{conversation.Id}.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-07 07:34:52 +00:00
|
|
|
|
2024-03-07 17:25:23 +00:00
|
|
|
private async Task CleanIdleConversationsAsync()
|
2024-03-07 07:34:52 +00:00
|
|
|
{
|
|
|
|
|
using var scope = _services.CreateScope();
|
2024-03-07 17:25:23 +00:00
|
|
|
var settings = scope.ServiceProvider.GetRequiredService<ConversationSetting>();
|
|
|
|
|
var cleanSetting = settings.CleanSetting;
|
|
|
|
|
|
|
|
|
|
if (cleanSetting == null || !cleanSetting.Enable) return;
|
|
|
|
|
|
2024-03-07 07:34:52 +00:00
|
|
|
var conversationService = scope.ServiceProvider.GetRequiredService<IConversationService>();
|
2024-03-07 17:25:23 +00:00
|
|
|
var conversationIds = await conversationService.GetIdleConversations(cleanSetting.BatchSize, cleanSetting.MessageLimit, cleanSetting.BufferHours);
|
2024-03-07 07:34:52 +00:00
|
|
|
|
|
|
|
|
if (!conversationIds.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
await conversationService.DeleteConversations(conversationIds);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-10 16:01:03 +00:00
|
|
|
}
|
|
|
|
|
}
|