BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
2023-09-05 22:19:36 -05:00

95 lines
2.9 KiB
C#

namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService : IConversationService
{
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
private readonly ConversationSetting _settings;
private readonly IConversationStorage _storage;
private readonly IConversationStateService _state;
private string _conversationId;
public IConversationStateService States => _state;
public ConversationService(IServiceProvider services,
IUserIdentity user,
ConversationSetting settings,
IConversationStorage storage,
IConversationStateService state,
ILogger<ConversationService> logger)
{
_services = services;
_user = user;
_settings = settings;
_storage = storage;
_state = state;
_logger = logger;
}
public Task DeleteConversation(string id)
{
throw new NotImplementedException();
}
public async Task<Conversation> GetConversation(string id)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var query = from sess in db.Conversation
where sess.Id == id
orderby sess.CreatedTime descending
select sess.ToConversation();
return query.FirstOrDefault();
}
public async Task<List<Conversation>> GetConversations()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var query = from sess in db.Conversation
where sess.UserId == _user.Id
orderby sess.CreatedTime descending
select sess.ToConversation();
return query.ToList();
}
public async Task<Conversation> NewConversation(Conversation sess)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var record = ConversationRecord.FromConversation(sess);
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
record.Title = "New Conversation";
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IBotSharpTable>(record);
});
_storage.InitStorage(record.Id);
return record.ToConversation();
}
public Task CleanHistory(string agentId)
{
throw new NotImplementedException();
}
public List<RoleDialogModel> GetDialogHistory(int lastCount = 20)
{
var dialogs = _storage.GetDialogs(_conversationId);
return dialogs
.Where(x => x.CreatedAt > DateTime.UtcNow.AddHours(-8))
.TakeLast(lastCount)
.ToList();
}
public void SetConversationId(string conversationId, string channel)
{
_conversationId = conversationId;
_state.Load(_conversationId);
_state.SetState("channel", channel);
}
}