2024-04-02 16:38:18 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Enums;
|
2024-03-27 18:50:27 +00:00
|
|
|
using BotSharp.Abstraction.Users.Enums;
|
2024-03-26 21:57:13 +00:00
|
|
|
|
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;
|
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();
|
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-04-02 16:38:18 +00:00
|
|
|
public IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true,
|
|
|
|
|
int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2023-09-18 08:35:02 +00:00
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
var preValue = string.Empty;
|
2023-09-18 08:35:02 +00:00
|
|
|
var currentValue = value.ToString();
|
2023-08-14 17:00:01 +00:00
|
|
|
var hooks = _services.GetServices<IConversationHook>();
|
2024-03-31 03:43:45 +00:00
|
|
|
var curActiveRounds = activeRounds > 0 ? activeRounds : -1;
|
|
|
|
|
int? preActiveRounds = null;
|
2024-01-01 04:56:10 +00:00
|
|
|
|
2024-03-26 21:57:13 +00:00
|
|
|
if (ContainsState(name) && _states.TryGetValue(name, out var pair))
|
2024-01-02 17:40:08 +00:00
|
|
|
{
|
2024-03-31 03:43:45 +00:00
|
|
|
var lastNode = pair?.Values?.LastOrDefault();
|
|
|
|
|
preActiveRounds = lastNode?.ActiveRounds;
|
2024-03-29 21:01:13 +00:00
|
|
|
preValue = lastNode?.Data ?? string.Empty;
|
2024-01-02 17:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-29 21:37:35 +00:00
|
|
|
_logger.LogInformation($"[STATE] {name} = {value}");
|
|
|
|
|
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
2024-03-28 18:16:16 +00:00
|
|
|
|
2024-04-02 16:38:18 +00:00
|
|
|
if (!ContainsState(name) || preValue != currentValue || preActiveRounds != curActiveRounds)
|
2024-03-29 21:37:35 +00:00
|
|
|
{
|
2024-04-02 16:38:18 +00:00
|
|
|
foreach (var hook in hooks)
|
2023-08-14 17:00:01 +00:00
|
|
|
{
|
2024-04-02 16:38:18 +00:00
|
|
|
hook.OnStateChanged(new StateChangeModel
|
|
|
|
|
{
|
|
|
|
|
ConversationId = _conversationId,
|
|
|
|
|
MessageId = routingCtx.MessageId,
|
|
|
|
|
Name = name,
|
|
|
|
|
BeforeValue = preValue,
|
|
|
|
|
BeforeActiveRounds = preActiveRounds,
|
|
|
|
|
AfterValue = currentValue,
|
2024-04-02 19:09:42 +00:00
|
|
|
AfterActiveRounds = curActiveRounds,
|
|
|
|
|
DataType = valueType,
|
|
|
|
|
Source = source
|
2024-04-02 16:38:18 +00:00
|
|
|
}).Wait();
|
|
|
|
|
}
|
2024-03-29 21:37:35 +00:00
|
|
|
}
|
2024-03-26 21:57:13 +00:00
|
|
|
|
2024-03-29 21:37:35 +00:00
|
|
|
var newPair = new StateKeyValue
|
|
|
|
|
{
|
|
|
|
|
Key = name,
|
|
|
|
|
Versioning = isNeedVersion
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var newValue = new StateValue
|
|
|
|
|
{
|
|
|
|
|
Data = currentValue,
|
|
|
|
|
MessageId = routingCtx.MessageId,
|
|
|
|
|
Active = true,
|
2024-03-31 03:43:45 +00:00
|
|
|
ActiveRounds = curActiveRounds,
|
2024-04-02 16:38:18 +00:00
|
|
|
DataType = valueType,
|
|
|
|
|
Source = source,
|
2024-03-29 21:37:35 +00:00
|
|
|
UpdateTime = DateTime.UtcNow,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!isNeedVersion || !_states.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
newPair.Values = new List<StateValue> { newValue };
|
|
|
|
|
_states[name] = newPair;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_states[name].Values.Add(newValue);
|
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
|
|
|
}
|
|
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
public Dictionary<string, string> 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-03-27 18:50:27 +00:00
|
|
|
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
|
|
|
|
var curMsgId = routingCtx.MessageId;
|
2024-01-02 17:50:07 +00:00
|
|
|
_states = _db.GetConversationStates(_conversationId);
|
2024-03-27 18:50:27 +00:00
|
|
|
var dialogs = _db.GetConversationDialogs(_conversationId);
|
|
|
|
|
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User || x.MetaData?.Role == UserRole.Client)
|
|
|
|
|
.OrderBy(x => x.MetaData?.CreateTime)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var curMsgIndex = userDialogs.FindIndex(x => !string.IsNullOrEmpty(curMsgId) && x.MetaData?.MessageId == curMsgId);
|
|
|
|
|
curMsgIndex = curMsgIndex < 0 ? userDialogs.Count() : curMsgIndex;
|
2024-01-02 17:40:08 +00:00
|
|
|
var curStates = new Dictionary<string, string>();
|
2023-08-09 21:49:55 +00:00
|
|
|
|
2024-01-02 17:50:07 +00:00
|
|
|
if (!_states.IsNullOrEmpty())
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-01-02 17:50:07 +00:00
|
|
|
foreach (var state in _states)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-03-26 21:57:13 +00:00
|
|
|
var value = state.Value?.Values?.LastOrDefault();
|
|
|
|
|
if (value == null || !value.Active) continue;
|
|
|
|
|
|
2024-03-27 18:50:27 +00:00
|
|
|
if (value.ActiveRounds > 0)
|
|
|
|
|
{
|
|
|
|
|
var stateMsgIndex = userDialogs.FindIndex(x => !string.IsNullOrEmpty(x.MetaData?.MessageId) && x.MetaData.MessageId == value.MessageId);
|
|
|
|
|
if (stateMsgIndex >= 0 && curMsgIndex - stateMsgIndex >= value.ActiveRounds)
|
|
|
|
|
{
|
|
|
|
|
state.Value.Values.Add(new StateValue
|
|
|
|
|
{
|
|
|
|
|
Data = value.Data,
|
2024-03-28 15:32:09 +00:00
|
|
|
MessageId = curMsgId,
|
2024-03-27 18:50:27 +00:00
|
|
|
Active = false,
|
|
|
|
|
ActiveRounds = value.ActiveRounds,
|
2024-04-02 16:38:18 +00:00
|
|
|
DataType = value.DataType,
|
|
|
|
|
Source = value.Source,
|
2024-03-27 18:50:27 +00:00
|
|
|
UpdateTime = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 21:57:13 +00:00
|
|
|
var data = value.Data ?? string.Empty;
|
|
|
|
|
curStates[state.Key] = data;
|
|
|
|
|
_logger.LogInformation($"[STATE] {state.Key} : {data}");
|
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
|
|
|
}
|
|
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
return curStates;
|
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-02 17:40:08 +00:00
|
|
|
var states = new List<StateKeyValue>();
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
foreach (var dic in _states)
|
2023-08-09 21:49:55 +00:00
|
|
|
{
|
2024-03-26 21:57:13 +00:00
|
|
|
states.Add(dic.Value);
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
2023-08-28 18:57:51 +00:00
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
_db.UpdateConversationStates(_conversationId, states);
|
2024-01-01 04:56:10 +00:00
|
|
|
_logger.LogInformation($"Saved states of conversation {_conversationId}");
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 13:56:21 +00:00
|
|
|
public void CleanStates()
|
2023-08-15 15:41:34 +00:00
|
|
|
{
|
2024-03-28 03:21:34 +00:00
|
|
|
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
|
|
|
|
var curMsgId = routingCtx.MessageId;
|
2024-03-26 21:57:13 +00:00
|
|
|
var utcNow = DateTime.UtcNow;
|
2024-03-28 03:21:34 +00:00
|
|
|
|
2024-03-26 21:57:13 +00:00
|
|
|
foreach (var key in _states.Keys)
|
|
|
|
|
{
|
|
|
|
|
var value = _states[key];
|
|
|
|
|
if (value == null || !value.Versioning || value.Values.IsNullOrEmpty()) continue;
|
|
|
|
|
|
|
|
|
|
var lastValue = value.Values.LastOrDefault();
|
|
|
|
|
if (lastValue == null || !lastValue.Active) continue;
|
|
|
|
|
|
2024-03-26 22:09:49 +00:00
|
|
|
value.Values.Add(new StateValue
|
|
|
|
|
{
|
|
|
|
|
Data = lastValue.Data,
|
2024-03-28 15:32:09 +00:00
|
|
|
MessageId = curMsgId,
|
2024-03-26 22:09:49 +00:00
|
|
|
Active = false,
|
2024-03-27 18:50:27 +00:00
|
|
|
ActiveRounds = lastValue.ActiveRounds,
|
2024-04-02 16:38:18 +00:00
|
|
|
DataType = lastValue.DataType,
|
|
|
|
|
Source = lastValue.Source,
|
2024-03-26 22:09:49 +00:00
|
|
|
UpdateTime = utcNow
|
|
|
|
|
});
|
2024-03-26 21:57:13 +00:00
|
|
|
}
|
2023-08-15 15:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 17:40:08 +00:00
|
|
|
public Dictionary<string, string> GetStates()
|
|
|
|
|
{
|
|
|
|
|
var curStates = new Dictionary<string, string>();
|
|
|
|
|
foreach (var state in _states)
|
|
|
|
|
{
|
2024-03-26 21:57:13 +00:00
|
|
|
var value = state.Value?.Values?.LastOrDefault();
|
|
|
|
|
if (value == null || !value.Active) continue;
|
|
|
|
|
|
|
|
|
|
curStates[state.Key] = value.Data ?? string.Empty;
|
2024-01-02 17:40:08 +00:00
|
|
|
}
|
|
|
|
|
return curStates;
|
|
|
|
|
}
|
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
|
|
|
{
|
2024-03-26 21:57:13 +00:00
|
|
|
if (!_states.ContainsKey(name) || _states[name].Values.IsNullOrEmpty() || !_states[name].Values.Last().Active)
|
2023-09-14 16:42:48 +00:00
|
|
|
{
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 21:57:13 +00:00
|
|
|
return _states[name].Values.Last().Data;
|
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)
|
|
|
|
|
{
|
2024-01-02 17:40:08 +00:00
|
|
|
return _states.ContainsKey(name)
|
2024-03-26 21:57:13 +00:00
|
|
|
&& !_states[name].Values.IsNullOrEmpty()
|
|
|
|
|
&& _states[name].Values.LastOrDefault()?.Active == true
|
|
|
|
|
&& !string.IsNullOrEmpty(_states[name].Values.Last().Data);
|
2023-09-25 22:46:00 +00:00
|
|
|
}
|
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()))
|
|
|
|
|
{
|
2024-04-02 16:38:18 +00:00
|
|
|
SetState(property.Name, property.Value, source: StateSource.Application);
|
2023-11-01 01:48:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-09 21:49:55 +00:00
|
|
|
}
|