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

105 lines
3.5 KiB
C#
Raw Normal View History

2023-06-27 18:31:13 +00:00
using BotSharp.Abstraction.Conversations.Models;
2023-09-03 19:06:40 +00:00
using BotSharp.Abstraction.Conversations.Settings;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
2023-08-28 18:57:51 +00:00
using MongoDB.Bson;
2023-09-03 19:06:40 +00:00
using System.IO;
2023-06-03 02:07:30 +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-06-03 02:07:30 +00:00
2023-08-17 04:04:23 +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,
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-08-09 21:49:55 +00:00
_logger = logger;
2023-06-03 02:07:30 +00:00
}
2023-06-27 18:31:13 +00:00
public Task DeleteConversation(string id)
2023-06-03 02:07:30 +00:00
{
2023-06-27 18:31:13 +00:00
throw new NotImplementedException();
2023-06-03 02:07:30 +00:00
}
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-07-19 22:30:23 +00:00
var query = from sess in db.Conversation
where sess.Id == id
orderby sess.CreatedTime descending
select sess.ToConversation();
return query.FirstOrDefault();
}
2023-06-27 18:31:13 +00:00
public async Task<List<Conversation>> GetConversations()
2023-06-03 02:07:30 +00:00
{
2023-08-10 04:53:22 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-06-27 18:31:13 +00:00
var query = from sess in db.Conversation
where sess.UserId == _user.Id
orderby sess.CreatedTime descending
select sess.ToConversation();
return query.ToList();
2023-06-03 02:07:30 +00:00
}
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-09-03 19:06:40 +00:00
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var foundUserId = user?.Id ?? _user.Id;
2023-06-27 18:31:13 +00:00
var record = ConversationRecord.FromConversation(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 19:06:40 +00:00
//db.Transaction<IBotSharpTable>(delegate
//{
// db.Add<IBotSharpTable>(record);
//});
var dir = Path.Combine(dbSettings.FileRepository, conversationSettings.DataDir, record.Id);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var path = Path.Combine(dir, "conversation.json");
File.WriteAllText(path, JsonSerializer.Serialize(record, new JsonSerializerOptions
2023-06-27 18:31:13 +00:00
{
2023-09-03 19:06:40 +00:00
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
}));
2023-06-27 18:31:13 +00:00
2023-08-09 21:49:55 +00:00
_storage.InitStorage(record.Id);
2023-06-27 18:31:13 +00:00
return record.ToConversation();
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-08-09 21:49:55 +00:00
public List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20)
2023-06-27 18:31:13 +00:00
{
2023-08-09 21:49:55 +00:00
var dialogs = _storage.GetDialogs(conversationId);
2023-08-14 22:14:05 +00:00
return dialogs
.Where(x => x.CreatedAt > DateTime.UtcNow.AddHours(-8))
.TakeLast(lastCount).ToList();
2023-06-27 18:31:13 +00:00
}
2023-06-03 02:07:30 +00:00
}