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/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 6b749b3b..5912b1e0 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -4,18 +4,14 @@ namespace BotSharp.Core.Agents.Services; public partial class AgentService { -#if !DEBUG - [MemoryCache(10 * 60)] -#endif + [MemoryCache(10 * 60, PerInstanceCache = true)] public async Task> GetAgents(bool? allowRouting = null) { var agents = _db.GetAgents(allowRouting: allowRouting); return await Task.FromResult(agents); } -#if !DEBUG - [MemoryCache(10 * 60)] -#endif + [MemoryCache(10 * 60, PerInstanceCache = true)] public async Task GetAgent(string id) { var profile = _db.GetAgent(id); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index a1779c34..8aed5989 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -486,9 +486,6 @@ public class FileRepository : IBotSharpRepository return responses; } -#if !DEBUG - [MemoryCache(10 * 60)] -#endif public Agent? GetAgent(string agentId) { var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); 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