using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations.Settings; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Records; using MongoDB.Bson; using System.IO; 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; public ConversationService(IServiceProvider services, IUserIdentity user, ConversationSetting settings, IConversationStorage storage, ILogger logger) { _services = services; _user = user; _settings = settings; _storage = storage; _logger = logger; } public Task DeleteConversation(string id) { throw new NotImplementedException(); } public async Task GetConversation(string id) { var db = _services.GetRequiredService(); var query = from sess in db.Conversation where sess.Id == id orderby sess.CreatedTime descending select sess.ToConversation(); return query.FirstOrDefault(); } public async Task> GetConversations() { var db = _services.GetRequiredService(); 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 NewConversation(Conversation sess) { var db = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); var conversationSettings = _services.GetRequiredService(); var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id); var foundUserId = user?.Id ?? _user.Id; var record = ConversationRecord.FromConversation(sess); record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString()); record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId); record.Title = "New Conversation"; //db.Transaction(delegate //{ // db.Add(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 { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true })); _storage.InitStorage(record.Id); return record.ToConversation(); } public Task CleanHistory(string agentId) { throw new NotImplementedException(); } public List GetDialogHistory(string conversationId, int lastCount = 20) { var dialogs = _storage.GetDialogs(conversationId); return dialogs .Where(x => x.CreatedAt > DateTime.UtcNow.AddHours(-8)) .TakeLast(lastCount).ToList(); } }