BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

126 lines
4 KiB
C#
Raw Normal View History

2023-09-06 03:39:56 +00:00
using BotSharp.Abstraction.Repositories;
2023-11-27 22:20:49 +00:00
using BotSharp.Abstraction.Repositories.Filters;
2023-11-14 01:25:25 +00:00
using BotSharp.Abstraction.Users.Enums;
2023-09-06 03:39:56 +00:00
namespace BotSharp.Core.Conversations.Services;
2023-06-03 02:07:30 +00:00
public partial class ConversationService : IConversationService
2023-06-03 02:07:30 +00:00
{
2023-08-09 21:49:55 +00:00
private readonly ILogger _logger;
2023-06-27 18:31:13 +00:00
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
private readonly ConversationSetting _settings;
private readonly IConversationStorage _storage;
2023-09-06 03:19:36 +00:00
private readonly IConversationStateService _state;
private string _conversationId;
2023-10-30 16:48:18 +00:00
public string ConversationId => _conversationId;
2023-09-06 03:19:36 +00:00
public IConversationStateService States => _state;
2023-06-03 02:07:30 +00:00
2023-09-06 03:39:56 +00:00
public ConversationService(
IServiceProvider services,
2023-06-27 18:31:13 +00:00
IUserIdentity user,
ConversationSetting settings,
2023-08-09 21:49:55 +00:00
IConversationStorage storage,
2023-09-06 03:19:36 +00:00
IConversationStateService state,
2023-08-09 21:49:55 +00:00
ILogger<ConversationService> logger)
2023-06-03 02:07:30 +00:00
{
2023-06-27 18:31:13 +00:00
_services = services;
_user = user;
_settings = settings;
_storage = storage;
2023-09-06 03:19:36 +00:00
_state = state;
2023-08-09 21:49:55 +00:00
_logger = logger;
2023-06-03 02:07:30 +00:00
}
2023-11-26 06:29:06 +00:00
public async Task<bool> DeleteConversation(string id)
2023-06-03 02:07:30 +00:00
{
2023-11-26 06:29:06 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
var isDeleted = db.DeleteConversation(id);
return await Task.FromResult(isDeleted);
2023-06-03 02:07:30 +00:00
}
2023-11-26 06:29:06 +00:00
public async Task<Conversation> UpdateConversationTitle(string id, string title)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.UpdateConversationTitle(id, title);
var conversation = db.GetConversation(id);
return conversation;
}
2023-07-19 22:30:23 +00:00
public async Task<Conversation> GetConversation(string id)
{
2023-08-10 04:53:22 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-09-03 22:43:50 +00:00
var conversation = db.GetConversation(id);
2023-09-07 22:04:34 +00:00
return conversation;
2023-07-19 22:30:23 +00:00
}
2023-12-04 23:42:46 +00:00
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
2023-06-03 02:07:30 +00:00
{
2023-08-10 04:53:22 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-11-14 01:25:25 +00:00
var user = db.GetUserById(_user.Id);
2023-12-04 23:42:46 +00:00
var conversations = db.GetConversations(filter);
var result = new PagedItems<Conversation>
2023-11-27 22:20:49 +00:00
{
2023-12-04 23:42:46 +00:00
Count = conversations.Count(),
Items = conversations.OrderByDescending(x => x.CreatedTime)
2023-11-27 22:20:49 +00:00
};
2023-12-04 23:42:46 +00:00
return result;
2023-06-03 02:07:30 +00:00
}
2023-11-10 16:01:03 +00:00
public async Task<List<Conversation>> GetLastConversations()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
return db.GetLastConversations();
}
2023-06-27 18:31:13 +00:00
public async Task<Conversation> NewConversation(Conversation sess)
2023-06-03 02:07:30 +00:00
{
2023-08-10 04:53:22 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-11-14 01:25:25 +00:00
var user = db.GetUserById(_user.Id);
var foundUserId = user?.Id ?? string.Empty;
2023-06-27 18:31:13 +00:00
2023-09-07 22:04:34 +00:00
var record = sess;
2023-09-03 19:06:40 +00:00
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId);
2023-06-27 18:31:13 +00:00
record.Title = "New Conversation";
2023-09-03 22:43:50 +00:00
db.CreateNewConversation(record);
2023-10-16 20:07:07 +00:00
var hooks = _services.GetServices<IConversationHook>().ToList();
foreach (var hook in hooks)
{
2023-11-27 21:00:26 +00:00
// If user connect agent first time
await hook.OnUserAgentConnectedInitially(sess);
2023-10-16 20:07:07 +00:00
await hook.OnConversationInitialized(record);
}
2023-09-07 22:04:34 +00:00
return record;
2023-06-03 02:07:30 +00:00
}
2023-06-27 18:31:13 +00:00
public Task CleanHistory(string agentId)
2023-06-03 02:07:30 +00:00
{
throw new NotImplementedException();
}
2023-06-27 18:31:13 +00:00
2023-10-27 15:18:48 +00:00
public List<RoleDialogModel> GetDialogHistory(int lastCount = 50)
2023-06-27 18:31:13 +00:00
{
2023-11-14 14:13:54 +00:00
if (string.IsNullOrEmpty(_conversationId))
{
throw new ArgumentNullException("ConversationId is null.");
}
2023-09-06 03:19:36 +00:00
var dialogs = _storage.GetDialogs(_conversationId);
2023-08-14 22:14:05 +00:00
return dialogs
2023-09-06 03:19:36 +00:00
.TakeLast(lastCount)
.ToList();
}
2023-09-14 01:41:51 +00:00
public void SetConversationId(string conversationId, List<string> states)
2023-09-06 03:19:36 +00:00
{
_conversationId = conversationId;
_state.Load(_conversationId);
2023-09-14 01:41:51 +00:00
states.ForEach(x => _state.SetState(x.Split('=')[0], x.Split('=')[1]));
2023-06-27 18:31:13 +00:00
}
2023-06-03 02:07:30 +00:00
}