2023-09-06 03:39:56 +00:00
|
|
|
using BotSharp.Abstraction.Repositories;
|
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-09-06 03:19:36 +00:00
|
|
|
private ConversationState _states;
|
2023-09-06 03:39:56 +00:00
|
|
|
private BotSharpDatabaseSettings _dbSettings;
|
2023-08-09 21:49:55 +00:00
|
|
|
private string _conversationId;
|
|
|
|
|
private string _file;
|
2023-09-06 03:39:56 +00:00
|
|
|
private readonly IBotSharpRepository _db;
|
2023-09-09 21:44:25 +00:00
|
|
|
private List<StateKeyValue> _savedStates;
|
2023-08-09 21:49:55 +00:00
|
|
|
|
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
|
|
|
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-09-06 03:39:56 +00:00
|
|
|
_db = db;
|
2023-09-06 03:19:36 +00:00
|
|
|
_states = new ConversationState();
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 16:42:48 +00:00
|
|
|
public IConversationStateService SetState(string name, string value)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-08-14 17:00:01 +00:00
|
|
|
var hooks = _services.GetServices<IConversationHook>();
|
2023-09-06 03:19:36 +00:00
|
|
|
string preValue = _states.ContainsKey(name) ? _states[name] : "";
|
|
|
|
|
if (!_states.ContainsKey(name) || _states[name] != value)
|
2023-08-14 17:00:01 +00:00
|
|
|
{
|
|
|
|
|
var currentValue = value;
|
2023-09-06 03:19:36 +00:00
|
|
|
_states[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-09-14 16:42:48 +00:00
|
|
|
|
|
|
|
|
return this;
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
public ConversationState Load(string conversationId)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-08 18:16:23 +00:00
|
|
|
_conversationId = conversationId;
|
2023-08-09 21:49:55 +00:00
|
|
|
|
2023-09-07 22:04:34 +00:00
|
|
|
_savedStates = _db.GetConversationStates(_conversationId);
|
2023-08-09 21:49:55 +00:00
|
|
|
|
2023-09-06 04:43:05 +00:00
|
|
|
if (!_savedStates.IsNullOrEmpty())
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-03 22:43:50 +00:00
|
|
|
foreach (var data in _savedStates)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-06 03:39:56 +00:00
|
|
|
_states[data.Key] = data.Value;
|
|
|
|
|
_logger.LogInformation($"Loaded state: {data.Key}={data.Value}");
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
_logger.LogInformation($"Loaded conversation states: {_conversationId}");
|
2023-08-14 17:00:01 +00:00
|
|
|
var hooks = _services.GetServices<IConversationHook>();
|
|
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2023-09-06 03:19:36 +00:00
|
|
|
hook.OnStateLoaded(_states).Wait();
|
2023-08-14 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
return _states;
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
2023-09-03 22:53:36 +00:00
|
|
|
if (_conversationId == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-09 21:44:25 +00:00
|
|
|
var states = new List<StateKeyValue>();
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2023-09-06 03:39:56 +00:00
|
|
|
foreach (var dic in _states)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-09 21:44:25 +00:00
|
|
|
states.Add(new StateKeyValue(dic.Key, dic.Value));
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2023-09-07 22:04:34 +00:00
|
|
|
_db.UpdateConversationStates(_conversationId, states);
|
2023-09-03 22:43:50 +00:00
|
|
|
_logger.LogInformation($"Saved state {_conversationId}");
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-15 15:41:34 +00:00
|
|
|
public void CleanState()
|
|
|
|
|
{
|
2023-09-03 22:43:50 +00:00
|
|
|
//File.Delete(_file);
|
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);
|
|
|
|
|
}
|
2023-09-02 05:10:46 +00:00
|
|
|
|
|
|
|
|
var stateFile = Path.Combine(dir, "state.dict");
|
|
|
|
|
if (!File.Exists(stateFile))
|
|
|
|
|
{
|
|
|
|
|
File.WriteAllText(stateFile, "");
|
|
|
|
|
}
|
|
|
|
|
return stateFile;
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
public ConversationState GetStates()
|
|
|
|
|
=> _states;
|
|
|
|
|
|
2023-09-14 01:41:51 +00:00
|
|
|
public string GetState(string name, string defaultValue = "")
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-06 03:19:36 +00:00
|
|
|
if (!_states.ContainsKey(name))
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-14 01:41:51 +00:00
|
|
|
_states[name] = defaultValue ?? "";
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
2023-09-14 16:42:48 +00:00
|
|
|
|
|
|
|
|
if (_states[name] == null)
|
|
|
|
|
{
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
return _states[name];
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
2023-08-14 17:00:01 +00:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Save();
|
|
|
|
|
}
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|