From 40bafdc327bff9e0b1ffcd648a2df3520ebd03fe Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 2 Apr 2024 15:39:14 -0500 Subject: [PATCH] add readonly --- .../Conversations/IConversationStateService.cs | 2 +- .../Conversations/Models/StateChangeModel.cs | 3 +++ .../Conversations/Models/StateKeyValue.cs | 1 + .../Conversations/Services/ConversationStateService.cs | 8 +++++--- .../BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs | 1 + .../Models/StateMongoElement.cs | 3 +++ 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index d71111fd..4b28c1d3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -14,7 +14,7 @@ public interface IConversationStateService bool ContainsState(string name); Dictionary GetStates(); IConversationStateService SetState(string name, T value, bool isNeedVersion = true, - int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User); + int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User, bool readOnly = false); void SaveStateByArgs(JsonDocument args); void CleanStates(); void Save(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs index ea602ad3..26c10a71 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs @@ -28,4 +28,7 @@ public class StateChangeModel [JsonPropertyName("source")] public string Source { get; set; } + + [JsonPropertyName("readonly")] + public bool Readonly { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs index c70dbf03..4afea9da 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs @@ -6,6 +6,7 @@ public class StateKeyValue { public string Key { get; set; } public bool Versioning { get; set; } + public bool Readonly { get; set; } public List Values { get; set; } = new List(); public StateKeyValue() diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index c413d501..b87a8fd7 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -35,7 +35,7 @@ public class ConversationStateService : IConversationStateService, IDisposable /// whether the state is related to message or not /// public IConversationStateService SetState(string name, T value, bool isNeedVersion = true, - int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User) + int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User, bool readOnly = false) { if (value == null) { @@ -72,7 +72,8 @@ public class ConversationStateService : IConversationStateService, IDisposable AfterValue = currentValue, AfterActiveRounds = curActiveRounds, DataType = valueType, - Source = source + Source = source, + Readonly = readOnly }).Wait(); } } @@ -80,7 +81,8 @@ public class ConversationStateService : IConversationStateService, IDisposable var newPair = new StateKeyValue { Key = name, - Versioning = isNeedVersion + Versioning = isNeedVersion, + Readonly = readOnly }; var newValue = new StateValue diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 20f7a0a2..2e2ed3d6 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -438,6 +438,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR AfterActiveRounds = stateChange.AfterActiveRounds, DataType = stateChange.DataType, Source = stateChange.Source, + Readonly = stateChange.Readonly, CreateTime = DateTime.UtcNow }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs index 54f67350..a939f59c 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs @@ -6,6 +6,7 @@ public class StateMongoElement { public string Key { get; set; } public bool Versioning { get; set; } + public bool Readonly { get; set; } public List Values { get; set; } public static StateMongoElement ToMongoElement(StateKeyValue state) @@ -14,6 +15,7 @@ public class StateMongoElement { Key = state.Key, Versioning = state.Versioning, + Readonly = state.Readonly, Values = state.Values?.Select(x => StateValueMongoElement.ToMongoElement(x))?.ToList() ?? new List() }; } @@ -24,6 +26,7 @@ public class StateMongoElement { Key = state.Key, Versioning = state.Versioning, + Readonly = state.Readonly, Values = state.Values?.Select(x => StateValueMongoElement.ToDomainElement(x))?.ToList() ?? new List() }; }