2023-09-06 03:39:56 +00:00
|
|
|
using BotSharp.Abstraction.Repositories;
|
2023-08-09 21:49:55 +00:00
|
|
|
|
|
|
|
|
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;
|
2024-01-01 04:56:10 +00:00
|
|
|
private ConversationHistoryState _historyStates;
|
2023-08-09 21:49:55 +00:00
|
|
|
private string _conversationId;
|
2023-09-06 03:39:56 +00:00
|
|
|
private readonly IBotSharpRepository _db;
|
2023-08-09 21:49:55 +00:00
|
|
|
|
2023-08-14 17:00:01 +00:00
|
|
|
public ConversationStateService(ILogger<ConversationStateService> logger,
|
2024-01-01 04:56:10 +00:00
|
|
|
IServiceProvider services,
|
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-09-06 03:39:56 +00:00
|
|
|
_db = db;
|
2023-09-06 03:19:36 +00:00
|
|
|
_states = new ConversationState();
|
2024-01-01 04:56:10 +00:00
|
|
|
_historyStates = new ConversationHistoryState();
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-27 19:05:18 +00:00
|
|
|
public string GetConversationId() => _conversationId;
|
|
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set conversation state
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
/// <param name="value"></param>
|
2024-01-01 19:53:05 +00:00
|
|
|
/// <param name="isNeedVersion">whether the state is related to message or not</param>
|
2024-01-01 04:56:10 +00:00
|
|
|
/// <returns></returns>
|
2024-01-01 19:53:05 +00:00
|
|
|
public IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-18 08:35:02 +00:00
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentValue = value.ToString();
|
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] : "";
|
2024-01-01 04:56:10 +00:00
|
|
|
|
2023-09-18 08:35:02 +00:00
|
|
|
if (!_states.ContainsKey(name) || _states[name] != currentValue)
|
2023-08-14 17:00:01 +00:00
|
|
|
{
|
2023-09-06 03:19:36 +00:00
|
|
|
_states[name] = currentValue;
|
2023-10-20 03:47:14 +00:00
|
|
|
_logger.LogInformation($"[STATE] {name} = {value}");
|
2023-08-14 17:00:01 +00:00
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
hook.OnStateChanged(name, preValue, currentValue).Wait();
|
|
|
|
|
}
|
2024-01-01 04:56:10 +00:00
|
|
|
|
|
|
|
|
var historyStateValue = new HistoryStateValue
|
|
|
|
|
{
|
|
|
|
|
Data = currentValue,
|
|
|
|
|
UpdateTime = DateTime.UtcNow
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-01 19:53:05 +00:00
|
|
|
if (!_historyStates.ContainsKey(name) || !isNeedVersion)
|
2024-01-01 04:56:10 +00:00
|
|
|
{
|
2024-01-01 19:53:05 +00:00
|
|
|
_historyStates[name] = new List<HistoryStateValue> { historyStateValue };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_historyStates[name].Add(historyStateValue);
|
2024-01-01 04:56:10 +00:00
|
|
|
}
|
2023-08-14 17:00:01 +00:00
|
|
|
}
|
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
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
var savedStates = _db.GetConversationStates(_conversationId).ToList();
|
|
|
|
|
_historyStates = new ConversationHistoryState(savedStates);
|
2023-08-09 21:49:55 +00:00
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
if (!savedStates.IsNullOrEmpty())
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-01-01 04:56:10 +00:00
|
|
|
foreach (var state in savedStates)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-01-01 04:56:10 +00:00
|
|
|
var value = state.Values.LastOrDefault()?.Data ?? string.Empty;
|
|
|
|
|
_states[state.Key] = value;
|
|
|
|
|
_logger.LogInformation($"[STATE] {state.Key} : {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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
var historyStates = new List<HistoryStateKeyValue>();
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
foreach (var dic in _historyStates)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-01-01 04:56:10 +00:00
|
|
|
historyStates.Add(new HistoryStateKeyValue(dic.Key, dic.Value));
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
_db.UpdateConversationStates(_conversationId, historyStates);
|
|
|
|
|
_logger.LogInformation($"Saved states of conversation {_conversationId}");
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-15 15:41:34 +00:00
|
|
|
public void CleanState()
|
|
|
|
|
{
|
2024-01-01 04:56:10 +00:00
|
|
|
|
2023-08-15 15:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
public ConversationState GetStates() => _states;
|
2023-09-06 03:19:36 +00:00
|
|
|
|
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-09-14 16:42:48 +00:00
|
|
|
{
|
|
|
|
|
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-09-25 22:46:00 +00:00
|
|
|
|
|
|
|
|
public bool ContainsState(string name)
|
|
|
|
|
{
|
|
|
|
|
return _states.ContainsKey(name) && !string.IsNullOrEmpty(_states[name]);
|
|
|
|
|
}
|
2023-11-01 01:48:12 +00:00
|
|
|
|
|
|
|
|
public void SaveStateByArgs(JsonDocument args)
|
|
|
|
|
{
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args.RootElement is JsonElement root)
|
|
|
|
|
{
|
|
|
|
|
foreach (JsonProperty property in root.EnumerateObject())
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(property.Value.ToString()))
|
|
|
|
|
{
|
|
|
|
|
SetState(property.Name, property.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|