diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index a52d934ea..61f489be7 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -43,27 +43,34 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe var preValue = string.Empty; var currentValue = value.ToString(); var hooks = _services.GetServices(); + var preActiveRounds = -1; + var curActiveRounds = activeRounds > 0 ? activeRounds : -1; if (ContainsState(name) && _states.TryGetValue(name, out var pair)) { - preValue = pair?.Values.LastOrDefault()?.Data ?? string.Empty; + var lastNode = pair?.Values.LastOrDefault(); + preActiveRounds = lastNode?.ActiveRounds ?? -1; + preValue = lastNode?.Data ?? string.Empty; } - if (!ContainsState(name) || preValue != currentValue) + if (!ContainsState(name) || preValue != currentValue || (preValue == currentValue && preActiveRounds > 0)) { _logger.LogInformation($"[STATE] {name} = {value}"); var routingCtx = _services.GetRequiredService(); - foreach (var hook in hooks) + if (!ContainsState(name) || preValue != currentValue) { - hook.OnStateChanged(new StateChangeModel + foreach (var hook in hooks) { - ConversationId = _conversationId, - MessageId = routingCtx.MessageId, - Name = name, - BeforeValue = preValue, - AfterValue = currentValue - }).Wait(); + hook.OnStateChanged(new StateChangeModel + { + ConversationId = _conversationId, + MessageId = routingCtx.MessageId, + Name = name, + BeforeValue = preValue, + AfterValue = currentValue + }).Wait(); + } } var newPair = new StateKeyValue @@ -77,7 +84,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, };