From b927f17ce33b35a17f7fa6f3a9e6caff3faf2cbe Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sat, 25 Jan 2025 03:58:06 -0600 Subject: [PATCH 1/3] refine sidecar async --- .../SideCar/Attributes/SideCarAttribute.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs index f4bb238d..3d14a32a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs @@ -4,7 +4,6 @@ using Rougamo.Context; using Microsoft.Extensions.DependencyInjection; using BotSharp.Abstraction.Shared; - namespace BotSharp.Abstraction.SideCar.Attributes; [AttributeUsage(AttributeTargets.Method, Inherited = true)] @@ -34,7 +33,7 @@ public class SideCarAttribute : AsyncMoAttribute if (typeof(Task).IsAssignableFrom(retType)) { var syncResultType = retType.IsConstructedGenericType ? retType.GenericTypeArguments[0] : typeof(void); - (isHandled, value) = CallAsyncMethod(sidecar, sidecarMethod, syncResultType, methodArgs); + (isHandled, value) = await CallAsyncMethod(sidecar, sidecarMethod, syncResultType, methodArgs); } else { @@ -67,9 +66,10 @@ public class SideCarAttribute : AsyncMoAttribute return (sidecar, sidecarMethod); } - private (bool, object?) CallAsyncMethod(IConversationSideCar instance, MethodInfo method, Type retType, object[] args) + private async Task<(bool, object?)> CallAsyncMethod(IConversationSideCar instance, MethodInfo method, Type retType, object[] args) { object? value = null; + object? res = null; var isHandled = false; var enabled = instance != null && instance.IsEnabled() && method != null; @@ -81,12 +81,20 @@ public class SideCarAttribute : AsyncMoAttribute isHandled = true; if (retType == typeof(void)) { - value = GetMethod(nameof(CallAsync)).Invoke(this, [instance, method, args]); + res = GetMethod(nameof(CallAsync)).Invoke(this, [instance, method, args]); } else { - var task = GetMethod(nameof(CallGenericAsync)).MakeGenericMethod(retType).Invoke(this, [instance, method, args]); - value = task?.GetType().GetProperty("Result")?.GetValue(task); + res = GetMethod(nameof(CallGenericAsync)).MakeGenericMethod(retType).Invoke(this, [instance, method, args]); + } + + if (res != null && res is Task task) + { + await task; + if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)) + { + value = task?.GetType().GetProperty("Result")?.GetValue(task); + } } return (isHandled, value); From eb7bddd5e8feb917cf9bb0a3e6a48a014a481192 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sat, 25 Jan 2025 03:59:23 -0600 Subject: [PATCH 2/3] minor change --- .../BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs index 3d14a32a..c10ffd8d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/SideCar/Attributes/SideCarAttribute.cs @@ -93,7 +93,7 @@ public class SideCarAttribute : AsyncMoAttribute await task; if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)) { - value = task?.GetType().GetProperty("Result")?.GetValue(task); + value = task?.GetType()?.GetProperty("Result")?.GetValue(task); } } From 7bae527025e8c5f11c8b7f8c1f4ca8b0f15eaf78 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 27 Jan 2025 11:06:11 +0800 Subject: [PATCH 3/3] Move StatisticsSettings to agent --- src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs | 7 +++++++ src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs | 5 ----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs index ad29bed8..d34dcd1f 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Settings; +using BotSharp.Abstraction.Statistics.Settings; using BotSharp.Abstraction.Templating; using BotSharp.Abstraction.Users.Enums; using BotSharp.Core.Agents.Hooks; @@ -34,6 +35,12 @@ public class AgentPlugin : IBotSharpPlugin services.AddScoped(); services.AddScoped(); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("Statistics"); + }); + services.AddScoped(provider => { var settingService = provider.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs index 72375cf4..08ff4e3a 100644 --- a/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.Dashboard/DashboardPlugin.cs @@ -16,11 +16,6 @@ public class DashboardPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { services.AddScoped(); - services.AddScoped(provider => - { - var settingService = provider.GetRequiredService(); - return settingService.Bind("Statistics"); - }); } public bool AttachMenu(List menu)