From 98e0142fc3d24913f7891595de01a3d730eca5f3 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 15 Apr 2025 17:58:33 -0500 Subject: [PATCH] fix to string --- .../Models/MessageState.cs | 4 ++-- .../Utilities/StringExtensions.cs | 23 +++++++++++++++++++ .../Agents/Services/AgentService.LoadAgent.cs | 2 +- .../Services/ConversationStateService.cs | 5 +++- .../Controllers/ConversationController.cs | 2 +- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/MessageState.cs b/src/Infrastructure/BotSharp.Abstraction/Models/MessageState.cs index 15fd6a37..ff26e981 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Models/MessageState.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Models/MessageState.cs @@ -3,7 +3,7 @@ namespace BotSharp.Abstraction.Models; public class MessageState { public string Key { get; set; } - public string Value { get; set; } + public object Value { get; set; } [JsonPropertyName("active_rounds")] public int ActiveRounds { get; set; } = -1; @@ -13,7 +13,7 @@ public class MessageState } - public MessageState(string key, string value, int activeRounds = -1) + public MessageState(string key, object value, int activeRounds = -1) { Key = key; Value = value; diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs index af222d0d..94bda0cd 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs @@ -108,4 +108,27 @@ public static class StringExtensions uint.TryParse(value, out _) || ulong.TryParse(value, out _); } + + + public static string ConvertToString(this T? value, JsonSerializerOptions? jsonOptions = null) + { + if (value == null) + { + return string.Empty; + } + + if (value is string s) + { + return s; + } + + if (value is JsonElement elem + && elem.ValueKind == JsonValueKind.String) + { + return elem.ToString(); + } + + var str = JsonSerializer.Serialize(value, jsonOptions); + return str; + } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index d3ceea8b..e10b79cc 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -67,7 +67,7 @@ public partial class AgentService hook.OnSamplesLoaded(agent.Samples); } - if (loadUtility) + if (loadUtility && !agent.Utilities.IsNullOrEmpty()) { hook.OnAgentUtilityLoaded(agent); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 37c8c760..9e101b74 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -15,6 +15,7 @@ ******************************************************************************/ using BotSharp.Abstraction.Conversations.Enums; +using BotSharp.Abstraction.Options; using BotSharp.Abstraction.SideCar; namespace BotSharp.Core.Conversations.Services; @@ -69,9 +70,11 @@ public class ConversationStateService : IConversationStateService return this; } + var options = _services.GetRequiredService(); + var defaultRound = -1; var preValue = string.Empty; - var currentValue = value.ToString(); + var currentValue = value.ConvertToString(options.JsonSerializerOptions); var curActive = true; StateKeyValue? pair = null; StateValue? prevLeafNode = null; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index b0a0fa6d..55e3009f 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -33,7 +33,7 @@ public class ConversationController : ControllerBase var conv = new Conversation { AgentId = agentId, - Channel = channel == default ? ConversationChannel.OpenAPI : channel.Value, + Channel = channel == default ? ConversationChannel.OpenAPI : channel.Value.ToString(), Tags = config.Tags ?? new(), TaskId = config.TaskId };