Skip to content

Commit 2cd3dfc

Browse files
committed
JsonContent<T>
1 parent 0121584 commit 2cd3dfc

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using System.Text.RegularExpressions;
23

34
namespace BotSharp.Abstraction.Utilities;
@@ -27,4 +28,16 @@ public static bool IsEqualTo(this string str1, string str2, StringComparison opt
2728
{
2829
return str1.Equals(str2, option);
2930
}
31+
32+
public static string JsonContent(this string text)
33+
{
34+
var m = Regex.Match(text, @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}");
35+
return m.Success ? m.Value : "{}";
36+
}
37+
38+
public static T? JsonContent<T>(this string text)
39+
{
40+
text = JsonContent(text);
41+
return JsonSerializer.Deserialize<T>(text);
42+
}
3043
}

src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetNextInstruction.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction()
8282
new RoleDialogModel(AgentRole.User, content)
8383
});
8484

85-
var pattern = @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}";
86-
response.Content = Regex.Match(response.Content, pattern).Value;
87-
args = JsonSerializer.Deserialize<FunctionCallFromLlm>(response.Content);
85+
args = response.Content.JsonContent<FunctionCallFromLlm>();
8886
break;
8987
}
9088
catch (Exception ex)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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.
1+
What is the next step based on the CONVERSATION? Response must be in appropriate JSON format.

0 commit comments

Comments
 (0)