From 8168b22707ff2e09ca9c435c3b099ab94165f170 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 2 Apr 2024 14:09:42 -0500 Subject: [PATCH] refine element button --- .../Conversations/Models/StateChangeModel.cs | 6 +++ .../Messaging/BotSharpMessageParser.cs | 43 +++++++++++-------- .../RichContentJsonConverter .cs | 3 +- .../TemplateMessageJsonConverter.cs | 3 +- .../Models/RichContent/ElementButton.cs | 10 ++++- .../Template/ButtonTemplateMessage.cs | 21 +-------- .../Services/ConversationService.cs | 3 +- .../Services/ConversationStateService.cs | 4 +- .../Controllers/ConversationController.cs | 11 +++-- .../Hooks/StreamingLogHook.cs | 2 + .../Models/StateMongoElement.cs | 7 +++ 11 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs index f98a896d..ea602ad3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs @@ -22,4 +22,10 @@ public class StateChangeModel [JsonPropertyName("after_active_rounds")] public int? AfterActiveRounds { get; set; } + + [JsonPropertyName("data_type")] + public string DataType { get; set; } + + [JsonPropertyName("source")] + public string Source { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs index 6697ef34..e22150e9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/BotSharpMessageParser.cs @@ -1,20 +1,19 @@ -using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Messaging.Enums; using BotSharp.Abstraction.Messaging.Models.RichContent.Template; using BotSharp.Abstraction.Messaging.Models.RichContent; using System.Text.Json; using System.Reflection; using Newtonsoft.Json; -using JsonSerializer = System.Text.Json.JsonSerializer; -namespace BotSharp.Core.Messaging; +namespace BotSharp.Abstraction.Messaging; public static class BotSharpMessageParser { - public static IRichMessage? ParseRichMessage(JsonElement root, JsonSerializerOptions options) + public static IRichMessage? ParseRichMessage(JsonElement root) { IRichMessage? res = null; + Type? targetType = null; JsonElement element; var jsonText = root.GetRawText(); @@ -23,23 +22,23 @@ public static class BotSharpMessageParser var richType = element.GetString(); if (richType == RichTypeEnum.ButtonTemplate) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(ButtonTemplateMessage); } else if (richType == RichTypeEnum.MultiSelectTemplate) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(MultiSelectTemplateMessage); } else if (richType == RichTypeEnum.QuickReply) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(QuickReplyMessage); } else if (richType == RichTypeEnum.CouponTemplate) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(CouponTemplateMessage); } else if (richType == RichTypeEnum.Text) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(TextMessage); } else if (richType == RichTypeEnum.GenericTemplate) { @@ -51,19 +50,24 @@ public static class BotSharpMessageParser if (wrapperType != null && genericType != null) { - var targetType = wrapperType.MakeGenericType(genericType); - res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage; + targetType = wrapperType.MakeGenericType(genericType); } } } } + if (targetType != null) + { + res = JsonConvert.DeserializeObject(jsonText, targetType) as IRichMessage; + } + return res; } - public static ITemplateMessage? ParseTemplateMessage(JsonElement root, JsonSerializerOptions options) + public static ITemplateMessage? ParseTemplateMessage(JsonElement root) { ITemplateMessage? res = null; + Type? targetType = null; JsonElement element; var jsonText = root.GetRawText(); @@ -72,19 +76,19 @@ public static class BotSharpMessageParser var templateType = element.GetString(); if (templateType == TemplateTypeEnum.Button) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(ButtonTemplateMessage); } else if (templateType == TemplateTypeEnum.MultiSelect) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(MultiSelectTemplateMessage); } else if (templateType == TemplateTypeEnum.Coupon) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(CouponTemplateMessage); } else if (templateType == TemplateTypeEnum.Product) { - res = JsonSerializer.Deserialize(jsonText, options); + targetType = typeof(ProductTemplateMessage); } else if (templateType == TemplateTypeEnum.Generic) { @@ -96,13 +100,18 @@ public static class BotSharpMessageParser if (wrapperType != null && genericType != null) { - var targetType = wrapperType.MakeGenericType(genericType); + targetType = wrapperType.MakeGenericType(genericType); res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage; } } } } + if (targetType != null) + { + res = JsonConvert.DeserializeObject(jsonText, targetType) as ITemplateMessage; + } + return res; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs index 94d12796..1748eb5a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/RichContentJsonConverter .cs @@ -1,4 +1,3 @@ -using BotSharp.Core.Messaging; using System.Text.Json; namespace BotSharp.Abstraction.Messaging.JsonConverters; @@ -9,7 +8,7 @@ public class RichContentJsonConverter : JsonConverter { using var jsonDoc = JsonDocument.ParseValue(ref reader); var root = jsonDoc.RootElement; - var res = BotSharpMessageParser.ParseRichMessage(root, options); + var res = BotSharpMessageParser.ParseRichMessage(root); return res; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs index 84963d39..ce42c489 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/JsonConverters/TemplateMessageJsonConverter.cs @@ -1,4 +1,3 @@ -using BotSharp.Core.Messaging; using System.Text.Json; namespace BotSharp.Abstraction.Messaging.JsonConverters; @@ -9,7 +8,7 @@ public class TemplateMessageJsonConverter : JsonConverter { using var jsonDoc = JsonDocument.ParseValue(ref reader); var root = jsonDoc.RootElement; - var res = BotSharpMessageParser.ParseTemplateMessage(root, options); + var res = BotSharpMessageParser.ParseTemplateMessage(root); return res; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/ElementButton.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/ElementButton.cs index b3642194..bc13660a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/ElementButton.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/ElementButton.cs @@ -5,13 +5,19 @@ namespace BotSharp.Abstraction.Messaging.Models.RichContent; /// public class ElementButton { - public string Type { get; set; } + public string Type { get; set; } = "web_url"; [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string Url { get; set; } - public string Title { get; set; } + public string Title { get; set; } = string.Empty; [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string Payload { get; set; } + + [JsonPropertyName("is_primary")] + public bool IsPrimary { get; set; } + + [JsonPropertyName("is_secondary")] + public bool IsSecondary { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs index 4c92a3fb..32ad73e2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs @@ -17,27 +17,8 @@ public class ButtonTemplateMessage : IRichMessage, ITemplateMessage public string TemplateType => TemplateTypeEnum.Button; [JsonPropertyName("buttons")] - public ButtonElement[] Buttons { get; set; } = new ButtonElement[0]; + public ElementButton[] Buttons { get; set; } = new ElementButton[0]; [JsonPropertyName("is_horizontal")] public bool IsHorizontal { get; set; } } - -public class ButtonElement -{ - /// - /// web_url, postback, phone_number - /// - public string Type { get; set; } = "web_url"; - - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Url { get; set; } - - [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? Payload { get; set; } - - public string Title { get; set; } = string.Empty; - - [JsonPropertyName("is_primary")] - public bool IsPrimary { get; set; } -} diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 8b3bb0fc..188715d4 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Models; namespace BotSharp.Core.Conversations.Services; @@ -126,6 +127,6 @@ public partial class ConversationService : IConversationService { _conversationId = conversationId; _state.Load(_conversationId); - states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds)); + states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External)); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 32fbb30a..c413d501 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -70,7 +70,9 @@ public class ConversationStateService : IConversationStateService, IDisposable BeforeValue = preValue, BeforeActiveRounds = preActiveRounds, AfterValue = currentValue, - AfterActiveRounds = curActiveRounds + AfterActiveRounds = curActiveRounds, + DataType = valueType, + Source = source }).Wait(); } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index daf2b116..e01a0f46 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Routing; -using BotSharp.Abstraction.Users.Models; namespace BotSharp.OpenAPI.Controllers; @@ -166,11 +165,11 @@ public class ConversationController : ControllerBase routing.Context.SetMessageId(conversationId, inputMsg.MessageId); conv.SetConversationId(conversationId, input.States); - conv.States.SetState("channel", input.Channel) - .SetState("provider", input.Provider) - .SetState("model", input.Model) - .SetState("temperature", input.Temperature) - .SetState("sampling_factor", input.SamplingFactor); + conv.States.SetState("channel", input.Channel, source: StateSource.External) + .SetState("provider", input.Provider, source: StateSource.External) + .SetState("model", input.Model, source: StateSource.External) + .SetState("temperature", input.Temperature, source: StateSource.External) + .SetState("sampling_factor", input.SamplingFactor, source: StateSource.External); var response = new ChatResponseModel(); diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 2162bcb9..20f7a0a2 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -436,6 +436,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR BeforeActiveRounds = stateChange.BeforeActiveRounds, AfterValue = stateChange.AfterValue, AfterActiveRounds = stateChange.AfterActiveRounds, + DataType = stateChange.DataType, + Source = stateChange.Source, CreateTime = DateTime.UtcNow }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs index b56483e1..54f67350 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs @@ -35,6 +35,9 @@ public class StateValueMongoElement public string? MessageId { get; set; } public bool Active { get; set; } public int ActiveRounds { get; set; } + public string DataType { get; set; } + public string Source { get; set; } + public DateTime UpdateTime { get; set; } public static StateValueMongoElement ToMongoElement(StateValue element) @@ -45,6 +48,8 @@ public class StateValueMongoElement MessageId = element.MessageId, Active = element.Active, ActiveRounds = element.ActiveRounds, + DataType = element.DataType, + Source = element.Source, UpdateTime = element.UpdateTime }; } @@ -57,6 +62,8 @@ public class StateValueMongoElement MessageId = element.MessageId, Active = element.Active, ActiveRounds = element.ActiveRounds, + DataType= element.DataType, + Source = element.Source, UpdateTime = element.UpdateTime }; }