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

138 lines
3.6 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-09-03 22:43:50 +00:00
using BotSharp.Abstraction.Repositories.Models;
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-09-03 22:43:50 +00:00
private List<KeyValueModel> _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
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-03 22:43:50 +00:00
_savedStates = _db.GetConversationState(_conversationId);
2023-08-09 21:49:55 +00:00
2023-09-03 22:43:50 +00:00
if (_savedStates != null)
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-03 22:43:50 +00:00
_state[data.Key] = data.Value;
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-09-03 22:43:50 +00:00
var states = new List<KeyValueModel>();
2023-08-28 18:57:51 +00:00
2023-08-09 21:49:55 +00:00
foreach (var dic in _state)
{
2023-09-03 22:43:50 +00:00
states.Add(new KeyValueModel(dic.Key, dic.Value));
2023-08-09 21:49:55 +00:00
}
2023-08-28 18:57:51 +00:00
2023-09-03 22:43:50 +00:00
_db.UpdateConversationState(_conversationId, states);
_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
}
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
}