From be88bfa12160069cc6856917a30bb35969f7e7ec Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 22 Nov 2023 10:27:55 -0600 Subject: [PATCH] ChatHub --- BotSharp.sln | 24 ++++++- README.md | 6 +- .../BotSharp.Plugin.ChatHub.csproj | 20 ++++++ .../BotSharp.Plugin.ChatHub/ChatHubPlugin.cs | 17 +++++ .../Controllers/ChatHubController.cs | 25 +++++++ .../Hooks/ChatHubConversationHook.cs | 67 +++++++++++++++++++ .../BotSharp.Plugin.ChatHub/SignalRHub.cs | 47 +++++++++++++ src/Plugins/BotSharp.Plugin.ChatHub/Using.cs | 22 ++++++ .../BotSharp.Plugin.TelegramBots.csproj | 16 +++++ .../TelegramBotsPlugin.cs | 18 +++++ src/WebStarter/WebStarter.csproj | 4 +- src/WebStarter/appsettings.json | 2 + 12 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/BotSharp.Plugin.ChatHub.csproj create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/Controllers/ChatHubController.cs create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs create mode 100644 src/Plugins/BotSharp.Plugin.ChatHub/Using.cs create mode 100644 src/Plugins/BotSharp.Plugin.TelegramBots/BotSharp.Plugin.TelegramBots.csproj create mode 100644 src/Plugins/BotSharp.Plugin.TelegramBots/TelegramBotsPlugin.cs diff --git a/BotSharp.sln b/BotSharp.sln index 27e352a2..5bc75c0a 100644 --- a/BotSharp.sln +++ b/BotSharp.sln @@ -69,7 +69,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SemanticKer EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Twilio", "src\Plugins\BotSharp.Plugin.Twilio\BotSharp.Plugin.Twilio.csproj", "{E627F1E3-BE03-443A-83A2-86A855A278EB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.WebDriver", "src\Plugins\BotSharp.Plugin.WebDriver\BotSharp.Plugin.WebDriver.csproj", "{F06B22CB-B143-4680-8FFF-35B9E50E6C47}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WebDriver", "src\Plugins\BotSharp.Plugin.WebDriver\BotSharp.Plugin.WebDriver.csproj", "{F06B22CB-B143-4680-8FFF-35B9E50E6C47}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.ChatHub", "src\Plugins\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj", "{EDCD9C20-2D9D-4098-A16E-03F97B306CB8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.TelegramBots", "src\Plugins\BotSharp.Plugin.TelegramBots\BotSharp.Plugin.TelegramBots.csproj", "{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -263,6 +267,22 @@ Global {F06B22CB-B143-4680-8FFF-35B9E50E6C47}.Release|Any CPU.Build.0 = Release|Any CPU {F06B22CB-B143-4680-8FFF-35B9E50E6C47}.Release|x64.ActiveCfg = Release|Any CPU {F06B22CB-B143-4680-8FFF-35B9E50E6C47}.Release|x64.Build.0 = Release|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Debug|x64.ActiveCfg = Debug|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Debug|x64.Build.0 = Debug|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Release|Any CPU.Build.0 = Release|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Release|x64.ActiveCfg = Release|Any CPU + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8}.Release|x64.Build.0 = Release|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Debug|x64.ActiveCfg = Debug|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Debug|x64.Build.0 = Debug|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|Any CPU.Build.0 = Release|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.ActiveCfg = Release|Any CPU + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -298,6 +318,8 @@ Global {BC57D428-A1A4-4D38-A2D0-AC6CA943F247} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC} {E627F1E3-BE03-443A-83A2-86A855A278EB} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} {F06B22CB-B143-4680-8FFF-35B9E50E6C47} = {51AFE054-AE99-497D-A593-69BAEFB5106F} + {EDCD9C20-2D9D-4098-A16E-03F97B306CB8} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} + {DCA18996-4D3A-4E98-BCD0-1FB77C59253E} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19} diff --git a/README.md b/README.md index 19ff8430..7ad13d60 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ The core module is mainly composed of abstraction and framework function impleme BotSharp uses component design, the kernel is kept to a minimum, and business functions are implemented by external components. The modular design also allows contributors to better participate. Below are the bulit-in plugins: #### Data Storages -- BotSharp.Plugin.MongoStorage +- BotSharp.Plugin.FileRepository - BotSharp.Plugin.MongoStorage #### LLMs @@ -74,11 +74,13 @@ BotSharp uses component design, the kernel is kept to a minimum, and business fu #### Messaging / Channel - BotSharp.OpenAPI +- BotSharp.Plugin.ChatHub - BotSharp.Plugin.MetaMessenger - BotSharp.Plugin.Twilio +- BotSharp.Plugin.TelegramBots - BotSharp.Plugin.WeChat -#### RAGS +#### RAGs - BotSharp.Plugin.KnowledgeBase - BotSharp.Plugin.Qdrant diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/BotSharp.Plugin.ChatHub.csproj b/src/Plugins/BotSharp.Plugin.ChatHub/BotSharp.Plugin.ChatHub.csproj new file mode 100644 index 00000000..487a2e95 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/BotSharp.Plugin.ChatHub.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + $(LangVersion) + $(BotSharpVersion) + $(GeneratePackageOnBuild) + True + + + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs new file mode 100644 index 00000000..9f9fe0b0 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs @@ -0,0 +1,17 @@ +using BotSharp.Plugin.ChatHub.Hooks; +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Plugin.ChatHub; + +/// +/// The dialogue channel connects users, AI assistants and customer service representatives. +/// +public class ChatHubPlugin : IBotSharpPlugin +{ + public string Name => "Chat Hub"; + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + // Register hooks + services.AddScoped(); + } +} diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Controllers/ChatHubController.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Controllers/ChatHubController.cs new file mode 100644 index 00000000..abd18425 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Controllers/ChatHubController.cs @@ -0,0 +1,25 @@ +using BotSharp.Abstraction.ApiAdapters; +using Microsoft.AspNetCore.SignalR; + +namespace BotSharp.Plugin.ChatHub.Controllers; + +[Authorize] +[ApiController] +public class ChatHubController : ControllerBase, IApiAdapter +{ + private readonly IServiceProvider _services; + private readonly ILogger _logger; + private readonly IHubContext _chatHub; + private readonly IUserIdentity _user; + + public ChatHubController(IServiceProvider services, + ILogger logger, + IHubContext chatHub, + IUserIdentity user) + { + _services = services; + _logger = logger; + _chatHub = chatHub; + _user = user; + } +} diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs new file mode 100644 index 00000000..4fadba6f --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.SignalR; + +namespace BotSharp.Plugin.ChatHub.Hooks; + +public class ChatHubConversationHook : ConversationHookBase +{ + private readonly IServiceProvider _services; + private readonly IHubContext _chatHub; + + public ChatHubConversationHook(IServiceProvider services, + IHubContext chatHub) + { + _services = services; + _chatHub = chatHub; + } + + public override async Task OnConversationInitialized(Conversation conversation) + { + var userService = _services.GetRequiredService(); + var conv = ConversationViewModel.FromSession(conversation); + + var user = await userService.GetUser(conv.User.Id); + conv.User = UserViewModel.FromUser(user); + + await _chatHub.Clients.All.SendAsync("OnConversationInitFromClient", conv); + + await base.OnConversationInitialized(conversation); + } + + public override async Task OnMessageReceived(RoleDialogModel message) + { + var conv = _services.GetRequiredService(); + var userService = _services.GetRequiredService(); + var sender = await userService.GetMyProfile(); + + // Update console conversation UI for CSR + await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromClient", new ChatResponseModel() + { + ConversationId = conv.ConversationId, + MessageId = message.MessageId, + Text = message.Content, + Sender = UserViewModel.FromUser(sender) + }); + + await base.OnMessageReceived(message); + } + + public override async Task OnResponseGenerated(RoleDialogModel message) + { + var conv = _services.GetRequiredService(); + + await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromAssistant", new ChatResponseModel() + { + ConversationId = conv.ConversationId, + MessageId = message.MessageId, + Text = message.Content, + Sender = new UserViewModel() + { + FirstName = "AI", + LastName = "Assistant", + Role = AgentRole.Assistant + } + }); + + await base.OnResponseGenerated(message); + } +} diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs new file mode 100644 index 00000000..ea18acb7 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/SignalRHub.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.SignalR; + +namespace BotSharp.Plugin.ChatHub; + +public class SignalRHub : Hub +{ + private readonly IServiceProvider _services; + private readonly ILogger _logger; + private readonly IUserIdentity _user; + + public SignalRHub(IServiceProvider services, + ILogger logger, + IUserIdentity user) + { + _services = services; + _logger = logger; + _user = user; + } + + public async Task OnMessageReceivedFromClient(string message) + { + // await Clients.User(_user.Id).SendAsync("ReceiveMessage", message); + } + + /// + /// Received message from client + /// + /// + /// + /// + public async Task SendMessage(string user, string message) + { + // await Clients.User(_user.Id).SendAsync("ReceiveMessage", message); + } + + public async Task SendMessageToCaller(string user, string message) + => await Clients.Caller.SendAsync("ReceiveMessage", user, message); + + public async Task SendMessageToGroup(string user, string message) + => await Clients.Group("SignalR Users").SendAsync("ReceiveMessage", user, message); + + public override Task OnConnectedAsync() + { + Console.WriteLine($"SignalR Hub: {Context.UserIdentifier} connected in {Context.ConnectionId} [{DateTime.Now}]"); + return base.OnConnectedAsync(); + } +} diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Using.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Using.cs new file mode 100644 index 00000000..0e730db9 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Using.cs @@ -0,0 +1,22 @@ +global using System; +global using System.Collections.Generic; +global using System.Text; +global using System.Threading.Tasks; +global using System.Linq; +global using System.Text.Json; +global using Microsoft.AspNetCore.Authorization; +global using Microsoft.AspNetCore.Mvc; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Logging; +global using BotSharp.Abstraction.Plugins; +global using BotSharp.Abstraction.Agents; +global using BotSharp.Abstraction.Conversations; +global using BotSharp.Abstraction.Knowledges; +global using BotSharp.Abstraction.Users; +global using BotSharp.Abstraction.Utilities; +global using BotSharp.Abstraction.Agents.Settings; +global using BotSharp.Abstraction.Conversations.Settings; +global using BotSharp.Abstraction.Agents.Enums; +global using BotSharp.Abstraction.Conversations.Models; +global using BotSharp.OpenAPI.ViewModels.Conversations; +global using BotSharp.OpenAPI.ViewModels.Users; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.TelegramBots/BotSharp.Plugin.TelegramBots.csproj b/src/Plugins/BotSharp.Plugin.TelegramBots/BotSharp.Plugin.TelegramBots.csproj new file mode 100644 index 00000000..55b5db95 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.TelegramBots/BotSharp.Plugin.TelegramBots.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.1 + enable + $(LangVersion) + $(BotSharpVersion) + $(GeneratePackageOnBuild) + True + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.TelegramBots/TelegramBotsPlugin.cs b/src/Plugins/BotSharp.Plugin.TelegramBots/TelegramBotsPlugin.cs new file mode 100644 index 00000000..86952750 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.TelegramBots/TelegramBotsPlugin.cs @@ -0,0 +1,18 @@ +using BotSharp.Abstraction.Plugins; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace BotSharp.Plugin.TelegramBots +{ + public class TelegramBotsPlugin : IBotSharpPlugin + { + public string Name => "Telegram Bots"; + public string Description => "Bots are small applications that run entirely within the Telegram app."; + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + + } + } +} diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj index 6dacdf9e..0c4ab1d7 100644 --- a/src/WebStarter/WebStarter.csproj +++ b/src/WebStarter/WebStarter.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -65,6 +65,8 @@ + + diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 72d404d0..7b4845f2 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -146,7 +146,9 @@ "BotSharp.Plugin.KnowledgeBase", "BotSharp.Plugin.Qdrant", "BotSharp.Plugin.PaddleSharp", + // "BotSharp.Plugin.ChatHub", "BotSharp.Plugin.WeChat", + // "BotSharp.Plugin.TelegramBots", // "BotSharp.Plugin.RoutingSpeeder", "BotSharp.Plugin.PizzaBot" ]