From 20de3f75658be2bfea0fd67609750947db9dbff9 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Fri, 1 Aug 2025 14:41:17 -0500 Subject: [PATCH] refine chat event --- .../BotSharp.Core/BotSharp.Core.csproj | 5 +- .../Conversations/ConversationPlugin.cs | 8 +-- .../Demo/Functions/GetFunEventsFn.cs | 51 +++++++++++++++++++ .../Demo/Functions/GetWeatherFn.cs | 18 +++++++ .../MessageHub/Services/ObserverService.cs | 3 ++ .../functions/get_fun_events.json | 15 ++++++ .../functions/get_weather.json | 1 + .../BotSharp.Plugin.ChatHub/ChatHubPlugin.cs | 7 +-- .../Hooks/ChatHubConversationHook.cs | 15 +++--- .../Observers/ChatHubObserver.cs | 10 +--- 10 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Core/Demo/Functions/GetFunEventsFn.cs create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_fun_events.json diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index ddcf68cb..ec10cb45 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -98,6 +98,7 @@ + @@ -211,6 +212,9 @@ PreserveNewest + + PreserveNewest + @@ -242,5 +246,4 @@ true - diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs index 0759f104..511a27f7 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs @@ -21,7 +21,7 @@ using Microsoft.Extensions.Configuration; namespace BotSharp.Core.Conversations; -public class ConversationPlugin : IBotSharpPlugin, IBotSharpAppPlugin +public class ConversationPlugin : IBotSharpPlugin { public string Id => "99e9b971-a9f1-4273-84da-876d2873d192"; public string Name => "Conversation"; @@ -48,6 +48,7 @@ public class ConversationPlugin : IBotSharpPlugin, IBotSharpAppPlugin return settingService.Bind("GoogleApi"); }); + // Observer and observable services.AddSingleton>>(); services.AddScoped>>(); services.AddScoped>, ConversationObserver>(); @@ -75,9 +76,4 @@ public class ConversationPlugin : IBotSharpPlugin, IBotSharpAppPlugin menu.Add(new PluginMenuDef("Conversation", link: "page/conversation", icon: "bx bx-conversation", weight: section.Weight + 5)); return true; } - - public void Configure(IApplicationBuilder app) - { - - } } diff --git a/src/Infrastructure/BotSharp.Core/Demo/Functions/GetFunEventsFn.cs b/src/Infrastructure/BotSharp.Core/Demo/Functions/GetFunEventsFn.cs new file mode 100644 index 00000000..beae84c3 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Demo/Functions/GetFunEventsFn.cs @@ -0,0 +1,51 @@ +using BotSharp.Abstraction.Functions; +using BotSharp.Core.MessageHub; +using System.Text.Json.Serialization; + +namespace BotSharp.Core.Demo.Functions; + +public class GetFunEventsFn : IFunctionCallback +{ + private readonly IServiceProvider _services; + + public GetFunEventsFn(IServiceProvider services) + { + _services = services; + } + + public string Name => "get_fun_events"; + public string Indication => "Searching fun events"; + + public async Task Execute(RoleDialogModel message) + { + var args = JsonSerializer.Deserialize(message.FunctionArgs); + var conv = _services.GetRequiredService(); + var messageHub = _services.GetRequiredService>>(); + + await Task.Delay(1000); + + message.Indication = $"Start querying event data in {args?.City}"; + messageHub.Push(new() + { + EventName = ChatEvent.OnIndicationReceived, + Data = message, + RefId = conv.ConversationId + }); + + await Task.Delay(1500); + + message.Indication = $"Still searching in {args?.City}"; + messageHub.Push(new() + { + EventName = ChatEvent.OnIndicationReceived, + Data = message, + RefId = conv.ConversationId + }); + + await Task.Delay(1500); + + message.Content = $"Here in {args?.City}, there are a lot of fun events in summer."; + message.StopCompletion = true; + return true; + } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs b/src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs index f39571d9..6465b88e 100644 --- a/src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs +++ b/src/Infrastructure/BotSharp.Core/Demo/Functions/GetWeatherFn.cs @@ -1,4 +1,6 @@ using BotSharp.Abstraction.Functions; +using BotSharp.Abstraction.Models; +using BotSharp.Abstraction.SideCar; using BotSharp.Core.MessageHub; using System.Text.Json.Serialization; @@ -46,6 +48,22 @@ public class GetWeatherFn : IFunctionCallback message.Content = $"It is a sunny day!"; message.StopCompletion = false; + +#if DEBUG + var sidecar = _services.GetService(); + if (sidecar != null) + { + var text = $"I want to know fun events in {args?.City}"; + var states = new List + { + new() { Key = "channel", Value = "email" } + }; + + var msg = await sidecar.SendMessage(message.CurrentAgentId, text, states: states); + message.Content = $"{message.Content} {msg.Content}"; + } +#endif + return true; } } diff --git a/src/Infrastructure/BotSharp.Core/MessageHub/Services/ObserverService.cs b/src/Infrastructure/BotSharp.Core/MessageHub/Services/ObserverService.cs index 60f1320a..19a5d651 100644 --- a/src/Infrastructure/BotSharp.Core/MessageHub/Services/ObserverService.cs +++ b/src/Infrastructure/BotSharp.Core/MessageHub/Services/ObserverService.cs @@ -66,7 +66,10 @@ public class ObserverService : IObserverService foreach (var sub in subscriptions) { + if (!sub.Observer.Active) continue; + sub.UnSubscribe(); } + container.Remove(names); } } diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_fun_events.json b/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_fun_events.json new file mode 100644 index 00000000..94b6abbf --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_fun_events.json @@ -0,0 +1,15 @@ +{ + "name": "get_fun_events", + "description": "Get fun events information for user.", + "visibility_expression": "{% if states.channel == 'email' %}visible{% endif %}", + "parameters": { + "type": "object", + "properties": { + "city": { + "type": "string", + "description": "The city where user wants to find fun events." + } + }, + "required": [ "city" ] + } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_weather.json b/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_weather.json index 0fd0a459..ab73340f 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_weather.json +++ b/src/Infrastructure/BotSharp.Core/data/agents/01e2fc5c-2c89-4ec7-8470-7688608b496c/functions/get_weather.json @@ -1,6 +1,7 @@ { "name": "get_weather", "description": "Get weather information for user.", + "visibility_expression": "{% if states.channel != 'email' %}visible{% endif %}", "parameters": { "type": "object", "properties": { diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs index 14b7ca01..51466692 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs @@ -13,7 +13,7 @@ namespace BotSharp.Plugin.ChatHub; /// /// The dialogue channel connects users, AI assistants and customer service representatives. /// -public class ChatHubPlugin : IBotSharpPlugin, IBotSharpAppPlugin +public class ChatHubPlugin : IBotSharpPlugin { public string Id => "6e52d42d-1e23-406b-8599-36af36c83209"; public string Name => "Chat Hub"; @@ -36,9 +36,4 @@ public class ChatHubPlugin : IBotSharpPlugin, IBotSharpAppPlugin services.AddScoped(); services.AddScoped(); } - - public void Configure(IApplicationBuilder app) - { - - } } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs index 52dd4f7c..bd590276 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -113,16 +113,13 @@ public class ChatHubConversationHook : ConversationHookBase }; // Send typing-off to client - if (!message.IsStreaming) + var action = new ConversationSenderActionModel { - var action = new ConversationSenderActionModel - { - ConversationId = conv.ConversationId, - SenderAction = SenderActionEnum.TypingOff - }; - await SendEvent(ChatEvent.OnSenderActionGenerated, conv.ConversationId, action); - } - + ConversationId = conv.ConversationId, + SenderAction = SenderActionEnum.TypingOff + }; + await SendEvent(ChatEvent.OnSenderActionGenerated, conv.ConversationId, action); + await SendEvent(ChatEvent.OnMessageReceivedFromAssistant, conv.ConversationId, data); await base.OnResponseGenerated(message); } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs index d9c4dcc2..9b5a97e5 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Observers/ChatHubObserver.cs @@ -99,14 +99,6 @@ public class ChatHubObserver : BotSharpObserverBase