diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs index c342881ff..f98a896dd 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs @@ -14,6 +14,12 @@ public class StateChangeModel [JsonPropertyName("before_value")] public string BeforeValue { get; set; } + [JsonPropertyName("before_active_rounds")] + public int? BeforeActiveRounds { get; set; } + [JsonPropertyName("after_value")] 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 cf7b9e7ee..9356e2f5e 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -43,29 +43,31 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe var preValue = string.Empty; var currentValue = value.ToString(); var hooks = _services.GetServices(); + var curActiveRounds = activeRounds > 0 ? activeRounds : -1; + int? preActiveRounds = null; if (ContainsState(name) && _states.TryGetValue(name, out var pair)) { - var lastNode = pair?.Values.LastOrDefault(); + var lastNode = pair?.Values?.LastOrDefault(); + preActiveRounds = lastNode?.ActiveRounds; preValue = lastNode?.Data ?? string.Empty; } _logger.LogInformation($"[STATE] {name} = {value}"); var routingCtx = _services.GetRequiredService(); - if (!ContainsState(name) || preValue != currentValue) + foreach (var hook in hooks) { - foreach (var hook in hooks) + hook.OnStateChanged(new StateChangeModel { - hook.OnStateChanged(new StateChangeModel - { - ConversationId = _conversationId, - MessageId = routingCtx.MessageId, - Name = name, - BeforeValue = preValue, - AfterValue = currentValue - }).Wait(); - } + ConversationId = _conversationId, + MessageId = routingCtx.MessageId, + Name = name, + BeforeValue = preValue, + BeforeActiveRounds = preActiveRounds, + AfterValue = currentValue, + AfterActiveRounds = curActiveRounds + }).Wait(); } var newPair = new StateKeyValue @@ -79,7 +81,7 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe Data = currentValue, MessageId = routingCtx.MessageId, Active = true, - ActiveRounds = activeRounds > 0 ? activeRounds : -1, + ActiveRounds = curActiveRounds, UpdateTime = DateTime.UtcNow, }; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index e93026c31..940e3dec7 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -394,7 +394,9 @@ private string BuildStateChangeLog(StateChangeModel stateChange) MessageId = stateChange.MessageId, Name = stateChange.Name, BeforeValue = stateChange.BeforeValue, + BeforeActiveRounds = stateChange.BeforeActiveRounds, AfterValue = stateChange.AfterValue, + AfterActiveRounds = stateChange.AfterActiveRounds, CreateTime = DateTime.UtcNow };