From 2cd3dfcd2978517146df86c2620c7ca6dd6bbb48 Mon Sep 17 00:00:00 2001 From: hchen Date: Thu, 26 Oct 2023 13:52:44 -0500 Subject: [PATCH] JsonContent --- .../Utilities/StringExtensions.cs | 13 +++++++++++++ .../Routing/RoutingService.GetNextInstruction.cs | 4 +--- .../templates/next_step_prompt.liquid | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs index d62b9ec0..17232920 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using System.Text.RegularExpressions; namespace BotSharp.Abstraction.Utilities; @@ -27,4 +28,16 @@ public static class StringExtensions { return str1.Equals(str2, option); } + + public static string JsonContent(this string text) + { + var m = Regex.Match(text, @"\{(?:[^{}]|(?\{)|(?<-open>\}))+(?(open)(?!))\}"); + return m.Success ? m.Value : "{}"; + } + + public static T? JsonContent(this string text) + { + text = JsonContent(text); + return JsonSerializer.Deserialize(text); + } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs index 9cf2726a..10541e4c 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs @@ -82,9 +82,7 @@ public partial class RoutingService new RoleDialogModel(AgentRole.User, content) }); - var pattern = @"\{(?:[^{}]|(?\{)|(?<-open>\}))+(?(open)(?!))\}"; - response.Content = Regex.Match(response.Content, pattern).Value; - args = JsonSerializer.Deserialize(response.Content); + args = response.Content.JsonContent(); break; } catch (Exception ex) diff --git a/src/WebStarter/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/next_step_prompt.liquid b/src/WebStarter/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/next_step_prompt.liquid index b61e4b34..24132c96 100644 --- a/src/WebStarter/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/next_step_prompt.liquid +++ b/src/WebStarter/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/next_step_prompt.liquid @@ -1 +1 @@ -What is the next step based on the CONVERSATION? Response must be in appropriate JSON format. Route to the latest agent as much as possible. \ No newline at end of file +What is the next step based on the CONVERSATION? Response must be in appropriate JSON format. \ No newline at end of file