diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs index 26c10a71..de84692f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs @@ -12,13 +12,13 @@ public class StateChangeModel public string Name { get; set; } [JsonPropertyName("before_value")] - public string BeforeValue { get; set; } + public string? BeforeValue { get; set; } [JsonPropertyName("before_active_rounds")] public int? BeforeActiveRounds { get; set; } [JsonPropertyName("after_value")] - public string AfterValue { get; set; } + public string? AfterValue { get; set; } [JsonPropertyName("after_active_rounds")] public int? AfterActiveRounds { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 5f632bd6..3f2fe11c 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -1,5 +1,8 @@ +using Amazon.Auth.AccessControlPolicy; +using AspectInjector.Broker; using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Users.Enums; +using System; namespace BotSharp.Core.Conversations.Services; @@ -201,9 +204,9 @@ public class ConversationStateService : IConversationStateService, IDisposable var key = pair.Key; var curValue = pair.Value; - if (!_historyStates.TryGetValue(key, out var historyValue) - || historyValue == null - || historyValue.Values.IsNullOrEmpty() + if (!_historyStates.TryGetValue(key, out var historyValue) + || historyValue == null + || historyValue.Values.IsNullOrEmpty() || !curValue.Versioning) { states.Add(curValue); @@ -232,8 +235,9 @@ public class ConversationStateService : IConversationStateService, IDisposable if (!ContainsState(name)) return false; var routingCtx = _services.GetRequiredService(); - var leafNode = _curStates[name].Values?.LastOrDefault(); - if (leafNode == null) return false; + var value = _curStates[name]; + var leafNode = value?.Values?.LastOrDefault(); + if (value == null || !value.Versioning || leafNode == null) return false; _curStates[name].Values.Add(new StateValue { @@ -246,6 +250,24 @@ public class ConversationStateService : IConversationStateService, IDisposable UpdateTime = DateTime.UtcNow }); + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + hook.OnStateChanged(new StateChangeModel + { + ConversationId = _conversationId, + MessageId = routingCtx.MessageId, + Name = name, + BeforeValue = leafNode.Data, + BeforeActiveRounds = leafNode.ActiveRounds, + AfterValue = null, + AfterActiveRounds = leafNode.ActiveRounds, + DataType = leafNode.DataType, + Source = leafNode.Source, + Readonly = _curStates[name].Readonly + }).Wait(); + } + return true; }