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

181 lines
5.1 KiB
C#
Raw Normal View History

2023-08-09 21:49:55 +00:00
using BotSharp.Abstraction.Conversations.Models;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
2023-08-28 18:57:51 +00:00
using BotSharp.Abstraction.Repositories.Records;
using MongoDB.Bson;
2023-08-09 21:49:55 +00:00
using System.IO;
namespace BotSharp.Core.Conversations.Services;
/// <summary>
/// Maintain the conversation state
/// </summary>
public class ConversationStateService : IConversationStateService, IDisposable
{
2023-08-14 17:00:01 +00:00
private readonly ILogger _logger;
private readonly IServiceProvider _services;
2023-08-28 18:57:51 +00:00
private readonly AgentSettings _agentSettings;
private readonly IUserIdentity _user;
private readonly IBotSharpRepository _db;
2023-08-09 21:49:55 +00:00
private ConversationState _state;
2023-09-01 22:33:36 +00:00
private BotSharpDatabaseSettings _dbSettings;
2023-08-09 21:49:55 +00:00
private string _conversationId;
private string _file;
2023-08-14 17:00:01 +00:00
public ConversationStateService(ILogger<ConversationStateService> logger,
IServiceProvider services,
2023-09-01 22:33:36 +00:00
BotSharpDatabaseSettings dbSettings,
2023-08-28 18:57:51 +00:00
AgentSettings agentSettings,
IUserIdentity user,
IBotSharpRepository db)
2023-08-09 21:49:55 +00:00
{
2023-08-14 17:00:01 +00:00
_logger = logger;
_services = services;
2023-08-10 04:53:22 +00:00
_dbSettings = dbSettings;
2023-08-28 18:57:51 +00:00
_agentSettings = agentSettings;
_user = user;
_db = db;
2023-08-09 21:49:55 +00:00
}
public void SetState(string name, string value)
{
2023-08-14 17:00:01 +00:00
var hooks = _services.GetServices<IConversationHook>();
string preValue = _state.ContainsKey(name) ? _state[name] : "";
if (!_state.ContainsKey(name) || _state[name] != value)
{
var currentValue = value;
_state[name] = currentValue;
2023-08-14 22:14:05 +00:00
_logger.LogInformation($"Set state: {name} = {value}");
2023-08-14 17:00:01 +00:00
foreach (var hook in hooks)
{
hook.OnStateChanged(name, preValue, currentValue).Wait();
}
}
2023-08-09 21:49:55 +00:00
}
2023-08-14 17:00:01 +00:00
public void SetConversation(string conversationId)
2023-08-09 21:49:55 +00:00
{
2023-08-14 17:00:01 +00:00
_conversationId = conversationId;
2023-08-09 21:49:55 +00:00
}
2023-08-14 17:00:01 +00:00
public ConversationState Load()
2023-08-09 21:49:55 +00:00
{
if (_state != null)
{
return _state;
}
_state = new ConversationState();
2023-09-01 22:33:36 +00:00
_file = GetStorageFile(_conversationId);
//_file = GetConversationState(_conversationId);
2023-08-09 21:49:55 +00:00
2023-08-28 18:57:51 +00:00
if (_file != null)
2023-08-09 21:49:55 +00:00
{
2023-09-01 22:33:36 +00:00
var dict = File.ReadAllLines(_file);
//var dict = _file.SplitByNewLine();
2023-08-09 21:49:55 +00:00
foreach (var line in dict)
{
2023-08-14 17:00:01 +00:00
_state[line.Split('=')[0]] = line.Split('=')[1];
2023-08-09 21:49:55 +00:00
}
}
2023-08-14 17:00:01 +00:00
_logger.LogInformation($"Loaded state {_conversationId}");
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
hook.OnStateLoaded(_state).Wait();
}
2023-08-09 21:49:55 +00:00
return _state;
}
public void Save()
{
2023-08-28 18:57:51 +00:00
var states = new StringBuilder();
var conversation = _db.Conversation.FirstOrDefault(x => x.Id == _conversationId);
2023-08-09 21:49:55 +00:00
foreach (var dic in _state)
{
2023-08-28 18:57:51 +00:00
//states.Add($"{dic.Key}={dic.Value}");
states.AppendLine($"{dic.Key}={dic.Value}");
2023-08-09 21:49:55 +00:00
}
2023-08-28 18:57:51 +00:00
//File.WriteAllLines(_file, states);
2023-08-14 17:00:01 +00:00
_logger.LogInformation($"Saved state {_conversationId}");
2023-08-28 18:57:51 +00:00
if (conversation != null)
{
conversation.State = states.ToString();
_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(conversation);
});
}
2023-08-09 21:49:55 +00:00
}
2023-08-15 15:41:34 +00:00
public void CleanState()
{
2023-08-28 18:57:51 +00:00
//File.Delete(_file);
var conversation = _db.Conversation.FirstOrDefault(x => x.Id == _conversationId);
if (conversation != null)
{
conversation.State = string.Empty;
_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(conversation);
});
}
2023-08-15 15:41:34 +00:00
}
2023-08-09 21:49:55 +00:00
private string GetStorageFile(string conversationId)
{
2023-08-10 04:53:22 +00:00
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
return Path.Combine(dir, "state.dict");
2023-08-09 21:49:55 +00:00
}
2023-08-28 18:57:51 +00:00
private string GetConversationState(string conversationId)
{
var conversation = _db.Conversation.FirstOrDefault(x => x.Id == conversationId);
if (conversation == null)
{
var user = _db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var record = new ConversationRecord()
{
Id = ObjectId.GenerateNewId().ToString(),
2023-09-01 22:33:36 +00:00
//AgentId = _agentSettings.RouterId,
2023-08-28 18:57:51 +00:00
UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(),
Title = "New Conversation",
Dialog = string.Empty,
State = string.Empty
};
_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(record);
});
conversation = _db.Conversation.FirstOrDefault(x => x.Id == record.Id);
}
return conversation.State ?? string.Empty;
}
2023-08-09 21:49:55 +00:00
public string GetState(string name)
{
if (!_state.ContainsKey(name))
{
_state[name] = "";
}
return _state[name];
}
2023-08-14 17:00:01 +00:00
public void Dispose()
{
Save();
}
2023-08-09 21:49:55 +00:00
}