diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 61bcfbfd..36b2ddeb 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -58,9 +58,9 @@ public class ConversationStateService : IConversationStateService, IDisposable if (ContainsState(name) && _curStates.TryGetValue(name, out var pair)) { - var lastNode = pair?.Values?.LastOrDefault(); - preActiveRounds = lastNode?.ActiveRounds; - preValue = lastNode?.Data ?? string.Empty; + var leafNode = pair?.Values?.LastOrDefault(); + preActiveRounds = leafNode?.ActiveRounds; + preValue = leafNode?.Data ?? string.Empty; } _logger.LogInformation($"[STATE] {name} = {value}"); @@ -139,7 +139,7 @@ public class ConversationStateService : IConversationStateService, IDisposable { var key = state.Key; var value = state.Value; - var leafNode = value.Values.LastOrDefault(); + var leafNode = value?.Values?.LastOrDefault(); if (leafNode == null) continue; _curStates[key] = new StateKeyValue @@ -261,7 +261,7 @@ public class ConversationStateService : IConversationStateService, IDisposable AfterActiveRounds = leafNode.ActiveRounds, DataType = leafNode.DataType, Source = leafNode.Source, - Readonly = _curStates[name].Readonly + Readonly = value.Readonly }).Wait(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index e5c193c1..f55ac4e1 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Loggers.Models; -using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Repositories.Models; using System.Globalization; using System.IO; @@ -35,30 +34,13 @@ namespace BotSharp.Core.Repository var stateFile = Path.Combine(dir, STATE_FILE); if (!File.Exists(stateFile)) { - var states = conversation.States ?? new Dictionary(); - var initialStates = states.Select(x => new StateKeyValue - { - Key = x.Key, - Values = new List - { - new StateValue { Data = x.Value, UpdateTime = DateTime.UtcNow } - } - }).ToList(); - File.WriteAllText(stateFile, JsonSerializer.Serialize(initialStates, _options)); + File.WriteAllText(stateFile, JsonSerializer.Serialize(new List(), _options)); } var breakpointFile = Path.Combine(dir, BREAKPOINT_FILE); if (!File.Exists(breakpointFile)) { - var initialBreakpoints = new List - { - new ConversationBreakpoint() - { - Breakpoint = utcNow.AddMilliseconds(-100), - CreatedTime = DateTime.UtcNow - } - }; - File.WriteAllText(breakpointFile, JsonSerializer.Serialize(initialBreakpoints, _options)); + File.WriteAllText(breakpointFile, JsonSerializer.Serialize(new List(), _options)); } } @@ -180,8 +162,8 @@ namespace BotSharp.Core.Repository { MessageId = breakpoint.MessageId, Breakpoint = breakpoint.Breakpoint, - CreatedTime = DateTime.UtcNow, Reason = breakpoint.Reason, + CreatedTime = DateTime.UtcNow, } }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStateDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStateDocument.cs index 42331616..1f8f0e90 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStateDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationStateDocument.cs @@ -5,6 +5,6 @@ namespace BotSharp.Plugin.MongoStorage.Collections; public class ConversationStateDocument : MongoBase { public string ConversationId { get; set; } - public List States { get; set; } - public List Breakpoints { get; set; } + public List States { get; set; } = new List(); + public List Breakpoints { get; set; } = new List(); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 4c5af6bc..ac5a47d5 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -3,7 +3,6 @@ using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Repositories.Models; using BotSharp.Plugin.MongoStorage.Collections; using BotSharp.Plugin.MongoStorage.Models; -using System.Text.RegularExpressions; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -34,21 +33,11 @@ public partial class MongoRepository Dialogs = new List() }; - var states = conversation.States ?? new Dictionary(); - var initialStates = states.Select(x => new StateMongoElement - { - Key = x.Key, - Values = new List - { - new StateValueMongoElement { Data = x.Value, UpdateTime = DateTime.UtcNow } - } - }).ToList(); - var stateDoc = new ConversationStateDocument { Id = Guid.NewGuid().ToString(), ConversationId = convDoc.Id, - States = initialStates, + States = new List(), Breakpoints = new List() }; @@ -169,19 +158,20 @@ public partial class MongoRepository var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); var state = _dc.ConversationStates.Find(filter).FirstOrDefault(); + var leafNode = state?.Breakpoints?.LastOrDefault(); - if (state == null || state.Breakpoints.IsNullOrEmpty()) + if (leafNode == null) { return null; } - return state.Breakpoints.Select(x => new ConversationBreakpoint + return new ConversationBreakpoint { - Breakpoint = x.Breakpoint, - CreatedTime = x.CreatedTime, - MessageId = x.MessageId, - Reason = x.Reason, - }).LastOrDefault(); + Breakpoint = leafNode.Breakpoint, + MessageId = leafNode.MessageId, + Reason = leafNode.Reason, + CreatedTime = leafNode.CreatedTime, + }; } public ConversationState GetConversationStates(string conversationId)