From a2643a8ce6ea877664278b33e91a3e65cf7b5fc2 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Mon, 20 Oct 2025 15:56:02 +0800 Subject: [PATCH 1/2] optimize LoadAgent --- .../Utilities/ObjectExtensions.cs | 25 +++++++++++++++++++ .../Agents/Services/AgentService.LoadAgent.cs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs new file mode 100644 index 000000000..2c738f834 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace BotSharp.Abstraction.Utilities +{ + public static class ObjectExtensions + { + private static readonly JsonSerializerOptions DefaultOptions = new JsonSerializerOptions + { + ReferenceHandler = ReferenceHandler.IgnoreCycles + }; + + public static T DeepClone(this T obj) where T : class + { + if (obj == null) return null; + + var json = JsonSerializer.Serialize(obj, DefaultOptions); + return JsonSerializer.Deserialize(json, DefaultOptions); + } + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index ef81adafd..2dcd15b77 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -14,7 +14,7 @@ public async Task LoadAgent(string id, bool loadUtility = true) HookEmitter.Emit(_services, hook => hook.OnAgentLoading(ref id), id); - var agent = await GetAgent(id); + var agent = (await GetAgent(id)).DeepClone(); if (agent == null) return null; agent.TemplateDict = []; From ac03402a471de763a5438833982eeaf8a5434053 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Mon, 20 Oct 2025 16:57:52 +0800 Subject: [PATCH 2/2] optimize DeepClone --- .../Utilities/ObjectExtensions.cs | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs index 2c738f834..7b3bca61b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/ObjectExtensions.cs @@ -1,25 +1,55 @@ +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace BotSharp.Abstraction.Utilities { public static class ObjectExtensions { - private static readonly JsonSerializerOptions DefaultOptions = new JsonSerializerOptions + private static readonly JsonSerializerOptions DefaultOptions = new() { ReferenceHandler = ReferenceHandler.IgnoreCycles }; + private static JsonSerializerSettings DefaultSettings = new() + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore + }; public static T DeepClone(this T obj) where T : class { if (obj == null) return null; - var json = JsonSerializer.Serialize(obj, DefaultOptions); - return JsonSerializer.Deserialize(json, DefaultOptions); + try + { + var json = JsonSerializer.Serialize(obj, DefaultOptions); + return JsonSerializer.Deserialize(json, DefaultOptions); + } + catch(Exception ex) + { + Console.WriteLine($"DeepClone Error:{ex}"); + return DeepCloneWithNewtonsoft(obj); + } + } + + private static T DeepCloneWithNewtonsoft(this T obj) where T : class + { + if (obj == null) return null; + + try + { + var json = JsonConvert.SerializeObject(obj, DefaultSettings); + return JsonConvert.DeserializeObject(json, DefaultSettings); + } + catch(Exception ex) + { + Console.WriteLine($"DeepClone Error:{ex}"); + return obj; + } } } }