From 66e233a11c85b6544d63216d03d4351ff150c9e2 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 15 May 2024 15:54:08 -0500 Subject: [PATCH 01/19] add user role filter --- .../FileRepository/FileRepository.User.cs | 2 + .../Controllers/AgentController.cs | 18 +++++++- .../Controllers/ConversationController.cs | 43 +++++++++++++------ .../ViewModels/Agents/AgentViewModel.cs | 2 + 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs index bc992a08..2d299f6d 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Users.Enums; using BotSharp.Abstraction.Users.Models; using System.IO; @@ -24,6 +25,7 @@ public partial class FileRepository { var userId = Guid.NewGuid().ToString(); user.Id = userId; + user.Role = UserRole.Admin; var dir = Path.Combine(_dbSettings.FileRepository, "users", userId); if (!Directory.Exists(dir)) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index bb67de2c..b10b60e8 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,4 +1,6 @@ using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Users.Enums; namespace BotSharp.OpenAPI.Controllers; @@ -7,11 +9,13 @@ namespace BotSharp.OpenAPI.Controllers; public class AgentController : ControllerBase { private readonly IAgentService _agentService; + private readonly IUserIdentity _user; private readonly IServiceProvider _services; - public AgentController(IAgentService agentService, IServiceProvider services) + public AgentController(IAgentService agentService, IUserIdentity user, IServiceProvider services) { _agentService = agentService; + _user = user; _services = services; } @@ -45,6 +49,18 @@ public class AgentController : ControllerBase rule.RedirectToAgentName = found.Name; } + + var editable = false; + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user != null && user.Role != UserRole.Admin) + { + var db = _services.GetRequiredService(); + var userAgents = db.GetAgentsByUser(user.Id); + editable = userAgents?.Select(x => x.Id)?.Contains(targetAgent.Id) ?? false; + } + + targetAgent.Editable = editable || user?.Role == UserRole.Admin; return targetAgent; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 34095acf..cbcc9028 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.Routing; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; +using BotSharp.Abstraction.Users.Enums; namespace BotSharp.OpenAPI.Controllers; @@ -38,10 +39,16 @@ public class ConversationController : ControllerBase [HttpPost("/conversations")] public async Task> GetConversations([FromBody] ConversationFilter filter) { - var service = _services.GetRequiredService(); - var conversations = await service.GetConversations(filter); - + var convService = _services.GetRequiredService(); var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user == null) + { + return new PagedItems(); + } + + filter.UserId = user.Role != UserRole.Admin ? user.Id : null; + var conversations = await convService.GetConversations(filter); var agentService = _services.GetRequiredService(); var list = conversations.Items .Select(x => ConversationViewModel.FromSession(x)) @@ -49,9 +56,8 @@ public class ConversationController : ControllerBase foreach (var item in list) { - var user = await userService.GetUser(item.User.Id); + user = await userService.GetUser(item.User.Id); item.User = UserViewModel.FromUser(user); - var agent = await agentService.GetAgent(item.AgentId); item.AgentName = agent?.Name; } @@ -116,21 +122,30 @@ public class ConversationController : ControllerBase } [HttpGet("/conversation/{conversationId}")] - public async Task GetConversation([FromRoute] string conversationId) + public async Task GetConversation([FromRoute] string conversationId) { var service = _services.GetRequiredService(); - var conversations = await service.GetConversations(new ConversationFilter - { - Id = conversationId - }); - var userService = _services.GetRequiredService(); - var result = ConversationViewModel.FromSession(conversations.Items.First()); + var user = await userService.GetUser(_user.Id); + if (user == null) + { + return null; + } + var filter = new ConversationFilter + { + Id = conversationId, + UserId = user.Role != UserRole.Admin ? user.Id : null + }; + var conversations = await service.GetConversations(filter); + if (conversations.Items.IsNullOrEmpty()) + { + return null; + } + + var result = ConversationViewModel.FromSession(conversations.Items.First()); var state = _services.GetRequiredService(); result.States = state.Load(conversationId, isReadOnly: true); - - var user = await userService.GetUser(result.User.Id); result.User = UserViewModel.FromUser(user); return result; diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index 536f5b5d..9d87a3e2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -42,6 +42,8 @@ public class AgentViewModel public PluginDef Plugin { get; set; } + public bool Editable { get; set; } + [JsonPropertyName("created_datetime")] public DateTime CreatedDateTime { get; set; } From 394d6071638fa1311711d0b35b97efeb455305a5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 15 May 2024 17:30:45 -0500 Subject: [PATCH 02/19] add user role limit --- .../Agents/IAgentService.cs | 2 ++ .../Services/AgentService.UpdateAgent.cs | 5 +++ .../Agents/Services/AgentService.cs | 6 ++++ .../FileRepository/FileRepository.User.cs | 1 - .../Users/Services/UserService.cs | 1 - .../Controllers/AgentController.cs | 9 +++-- .../Controllers/ConversationController.cs | 4 +-- .../Controllers/PluginController.cs | 33 +++++++++++++------ 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index db4db62a..b435a4d1 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -48,5 +48,7 @@ public interface IAgentService string GetDataDir(); string GetAgentDataDir(string agentId); + List GetAgentsByUser(string userId); + PluginDef GetPlugin(string agentId); } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 154d7398..af6cb65e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Repositories.Enums; using BotSharp.Abstraction.Routing.Models; +using BotSharp.Abstraction.Users.Enums; using System.IO; namespace BotSharp.Core.Agents.Services; @@ -8,6 +9,10 @@ public partial class AgentService { public async Task UpdateAgent(Agent agent, AgentField updateField) { + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role != UserRole.Admin) return; + if (agent == null || string.IsNullOrEmpty(agent.Id)) return; var record = _db.GetAgent(agent.Id); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index bd009db0..da1b8cf1 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -47,4 +47,10 @@ public partial class AgentService : IAgentService } return dir; } + + public List GetAgentsByUser(string userId) + { + var agents = _db.GetAgentsByUser(userId); + return agents; + } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs index 2d299f6d..8f1103e6 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs @@ -25,7 +25,6 @@ public partial class FileRepository { var userId = Guid.NewGuid().ToString(); user.Id = userId; - user.Role = UserRole.Admin; var dir = Path.Combine(_dbSettings.FileRepository, "users", userId); if (!Directory.Exists(dir)) { diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 6e856b9f..322b300d 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Users.Models; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index b10b60e8..d867e782 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -50,17 +50,16 @@ public class AgentController : ControllerBase rule.RedirectToAgentName = found.Name; } - var editable = false; + var editable = true; var userService = _services.GetRequiredService(); var user = await userService.GetUser(_user.Id); - if (user != null && user.Role != UserRole.Admin) + if (user?.Role != UserRole.Admin) { - var db = _services.GetRequiredService(); - var userAgents = db.GetAgentsByUser(user.Id); + var userAgents = _agentService.GetAgentsByUser(user?.Id); editable = userAgents?.Select(x => x.Id)?.Contains(targetAgent.Id) ?? false; } - targetAgent.Editable = editable || user?.Role == UserRole.Admin; + targetAgent.Editable = editable; return targetAgent; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index cbcc9028..f62680d2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -50,9 +50,7 @@ public class ConversationController : ControllerBase filter.UserId = user.Role != UserRole.Admin ? user.Id : null; var conversations = await convService.GetConversations(filter); var agentService = _services.GetRequiredService(); - var list = conversations.Items - .Select(x => ConversationViewModel.FromSession(x)) - .ToList(); + var list = conversations.Items.Select(x => ConversationViewModel.FromSession(x)).ToList(); foreach (var item in list) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs index 2c231e16..dc2c2740 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Plugins.Models; +using BotSharp.Abstraction.Users.Enums; using BotSharp.Core.Plugins; namespace BotSharp.OpenAPI.Controllers; @@ -8,38 +9,50 @@ namespace BotSharp.OpenAPI.Controllers; public class PluginController : ControllerBase { private readonly IServiceProvider _services; + private readonly IUserIdentity _user; private readonly PluginSettings _settings; - public PluginController(IServiceProvider services, PluginSettings settings) + public PluginController(IServiceProvider services, IUserIdentity user, PluginSettings settings) { _services = services; + _user = user; _settings = settings; } [HttpGet("/plugins")] - public PagedItems GetPlugins([FromQuery] PluginFilter filter) + public async Task> GetPlugins([FromQuery] PluginFilter filter) { + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role != UserRole.Admin) + { + return new PagedItems(); + } + var loader = _services.GetRequiredService(); return loader.GetPagedPlugins(_services, filter); } [HttpGet("/plugin/menu")] - public List GetPluginMenu() + public async Task> GetPluginMenu() { var menu = new List { new PluginMenuDef("Apps", weight: 5) { IsHeader = true, - }, - new PluginMenuDef("System", weight: 30) - { - IsHeader = true - }, - new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31), - new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32), + } }; + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + if (user?.Role == UserRole.Admin) + { + menu.Add(new PluginMenuDef("System", weight: 30) { IsHeader = true }); + menu.Add(new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31)); + menu.Add(new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32)); + } + var loader = _services.GetRequiredService(); foreach (var plugin in loader.GetPlugins(_services)) { From ca5407f97c777947dd2f27332934ec4a78f9c644 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 15 May 2024 17:53:39 -0500 Subject: [PATCH 03/19] add user role in menu --- .../Plugins/Models/PluginMenuDef.cs | 3 +++ .../BotSharp.Core/Agents/AgentPlugin.cs | 2 +- .../BotSharp.Core/Plugins/PluginLoader.cs | 16 ++++++++++++ .../BotSharp.Core/Tasks/TaskPlugin.cs | 6 ++++- .../Controllers/PluginController.cs | 26 ++++++++++++------- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginMenuDef.cs b/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginMenuDef.cs index bd5a1d88..5f8a59b9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginMenuDef.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginMenuDef.cs @@ -19,6 +19,9 @@ public class PluginMenuDef [JsonIgnore] public int Weight { get; set; } + [JsonIgnore] + public List? Roles { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List? SubMenu { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs index eb21fb31..4aef8ca4 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs @@ -43,7 +43,7 @@ public class AgentPlugin : IBotSharpPlugin { SubMenu = new List { - new PluginMenuDef("Routing", link: "page/agent/router"), // icon: "bx bx-map-pin" + new PluginMenuDef("Routing", link: "page/agent/router") { Roles = new List { "admin" } }, // icon: "bx bx-map-pin" new PluginMenuDef("Evaluating", link: "page/agent/evaluator"), // icon: "bx bx-task" new PluginMenuDef("Agents", link: "page/agent"), // icon: "bx bx-bot" } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index f6de19ac..05867cb0 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -269,4 +269,20 @@ public class PluginLoader } }); } + + public List FilterPluginsByRoles(List plugins, string userRole) + { + if (plugins.IsNullOrEmpty()) return plugins; + + var filtered = new List(); + foreach (var plugin in plugins) + { + if (plugin.Roles.IsNullOrEmpty() || plugin.Roles.Contains(userRole)) + { + plugin.SubMenu = FilterPluginsByRoles(plugin.SubMenu, userRole); + filtered.Add(plugin); + } + } + return filtered; + } } diff --git a/src/Infrastructure/BotSharp.Core/Tasks/TaskPlugin.cs b/src/Infrastructure/BotSharp.Core/Tasks/TaskPlugin.cs index ddf709ec..27c55ab5 100644 --- a/src/Infrastructure/BotSharp.Core/Tasks/TaskPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Tasks/TaskPlugin.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Tasks; +using BotSharp.Abstraction.Users.Enums; using BotSharp.Core.Tasks.Services; using Microsoft.Extensions.Configuration; @@ -19,7 +20,10 @@ public class TaskPlugin : IBotSharpPlugin public bool AttachMenu(List menu) { var section = menu.First(x => x.Label == "Apps"); - menu.Add(new PluginMenuDef("Task", link: "page/task", icon: "bx bx-task", weight: section.Weight + 8)); + menu.Add(new PluginMenuDef("Task", link: "page/task", icon: "bx bx-task", weight: section.Weight + 8) + { + Roles = new List { UserRole.Admin } + }); return true; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs index dc2c2740..da3de2c8 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs @@ -41,18 +41,22 @@ public class PluginController : ControllerBase new PluginMenuDef("Apps", weight: 5) { IsHeader = true, + }, + new PluginMenuDef("System", weight: 30) + { + IsHeader = true, + Roles = new List { UserRole.Admin } + }, + new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31) + { + Roles = new List { UserRole.Admin } + }, + new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32) + { + Roles = new List { UserRole.Admin } } }; - var userService = _services.GetRequiredService(); - var user = await userService.GetUser(_user.Id); - if (user?.Role == UserRole.Admin) - { - menu.Add(new PluginMenuDef("System", weight: 30) { IsHeader = true }); - menu.Add(new PluginMenuDef("Plugins", link: "page/plugin", icon: "bx bx-plug", weight: 31)); - menu.Add(new PluginMenuDef("Settings", link: "page/setting", icon: "bx bx-cog", weight: 32)); - } - var loader = _services.GetRequiredService(); foreach (var plugin in loader.GetPlugins(_services)) { @@ -62,6 +66,10 @@ public class PluginController : ControllerBase } plugin.Module.AttachMenu(menu); } + + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + menu = loader.FilterPluginsByRoles(menu, user?.Role); menu = menu.OrderBy(x => x.Weight).ToList(); return menu; } From 89c33bafc0e8628e4c02ba652dc6b393bda8af58 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 15 May 2024 18:07:11 -0500 Subject: [PATCH 04/19] check convsation user when delete --- .../Controllers/ConversationController.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index f62680d2..0946084c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -2,6 +2,7 @@ using BotSharp.Abstraction.Routing; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using BotSharp.Abstraction.Users.Enums; +using BotSharp.Abstraction.Users.Models; namespace BotSharp.OpenAPI.Controllers; @@ -181,7 +182,22 @@ public class ConversationController : ControllerBase [HttpDelete("/conversation/{conversationId}")] public async Task DeleteConversation([FromRoute] string conversationId) { + var userService = _services.GetRequiredService(); var conversationService = _services.GetRequiredService(); + + var user = await userService.GetUser(_user.Id); + var filter = new ConversationFilter + { + Id = conversationId, + UserId = user.Role != UserRole.Admin ? user.Id : null + }; + var conversations = await conversationService.GetConversations(filter); + + if (conversations.Items.IsNullOrEmpty()) + { + return false; + } + var response = await conversationService.DeleteConversations(new List { conversationId }); return response; } From 04e90a3664d8064b339fac254373c47fa156c0b3 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 16 May 2024 10:41:59 -0500 Subject: [PATCH 05/19] rename function --- .../BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs | 4 ---- src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs | 4 ++-- .../BotSharp.OpenAPI/Controllers/PluginController.cs | 2 +- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index ce6512af..d3754a94 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -1,8 +1,4 @@ -using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Tasks.Models; -using BotSharp.Abstraction.Users.Models; using System.IO; using System.Text.RegularExpressions; diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index 05867cb0..1b883657 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -270,7 +270,7 @@ public class PluginLoader }); } - public List FilterPluginsByRoles(List plugins, string userRole) + public List GetPluginMenuByRoles(List plugins, string userRole) { if (plugins.IsNullOrEmpty()) return plugins; @@ -279,7 +279,7 @@ public class PluginLoader { if (plugin.Roles.IsNullOrEmpty() || plugin.Roles.Contains(userRole)) { - plugin.SubMenu = FilterPluginsByRoles(plugin.SubMenu, userRole); + plugin.SubMenu = GetPluginMenuByRoles(plugin.SubMenu, userRole); filtered.Add(plugin); } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs index da3de2c8..342f39fb 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs @@ -69,7 +69,7 @@ public class PluginController : ControllerBase var userService = _services.GetRequiredService(); var user = await userService.GetUser(_user.Id); - menu = loader.FilterPluginsByRoles(menu, user?.Role); + menu = loader.GetPluginMenuByRoles(menu, user?.Role); menu = menu.OrderBy(x => x.Weight).ToList(); return menu; } From 6625b3ec088fa204bf8b414206feea59422954b8 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 16 May 2024 12:58:37 -0500 Subject: [PATCH 06/19] minor change --- src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs index 4aef8ca4..83ce823c 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.Users.Enums; using Microsoft.Extensions.Configuration; namespace BotSharp.Core.Agents; @@ -43,8 +44,8 @@ public class AgentPlugin : IBotSharpPlugin { SubMenu = new List { - new PluginMenuDef("Routing", link: "page/agent/router") { Roles = new List { "admin" } }, // icon: "bx bx-map-pin" - new PluginMenuDef("Evaluating", link: "page/agent/evaluator"), // icon: "bx bx-task" + new PluginMenuDef("Routing", link: "page/agent/router") { Roles = new List { UserRole.Admin } }, // icon: "bx bx-map-pin" + new PluginMenuDef("Evaluating", link: "page/agent/evaluator") { Roles = new List { UserRole.Admin } }, // icon: "bx bx-task" new PluginMenuDef("Agents", link: "page/agent"), // icon: "bx bx-bot" } }); From 003de30538ea864e3738b89ee6a7c23441d95dd1 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 21 May 2024 11:20:03 -0500 Subject: [PATCH 07/19] resolve conflict --- .../BotSharp.OpenAPI/Controllers/ConversationController.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 08e4c0a0..6831e74c 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Routing; +using BotSharp.Abstraction.Users.Enums; namespace BotSharp.OpenAPI.Controllers; From effd44eb7c7f6a620f7a50be71fbbd142223cfb8 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 21 May 2024 15:55:12 -0500 Subject: [PATCH 08/19] add agent delete --- .../Services/AgentService.CreateAgent.cs | 23 ++---------- .../Services/AgentService.DeleteAgent.cs | 13 ++++++- .../FileRepository/FileRepository.Agent.cs | 35 ++++++++++++++++++- .../Controllers/AgentController.cs | 11 ++++-- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index d3754a94..ae429484 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -22,32 +22,13 @@ public partial class AgentService var dbSettings = _services.GetRequiredService(); var agentSettings = _services.GetRequiredService(); - var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir); - var foundAgent = FetchAgentFileByName(agent.Name, filePath); - - if (foundAgent != null) - { - agentRecord.SetId(foundAgent.Id) - .SetName(foundAgent.Name) - .SetDescription(foundAgent.Description) - .SetIsPublic(foundAgent.IsPublic) - .SetDisabled(foundAgent.Disabled) - .SetAgentType(foundAgent.Type) - .SetProfiles(foundAgent.Profiles) - .SetRoutingRules(foundAgent.RoutingRules) - .SetInstruction(foundAgent.Instruction) - .SetTemplates(foundAgent.Templates) - .SetFunctions(foundAgent.Functions) - .SetResponses(foundAgent.Responses) - .SetLlmConfig(foundAgent.LlmConfig); - } var user = _db.GetUserById(_user.Id); var userAgentRecord = new UserAgent { Id = Guid.NewGuid().ToString(), UserId = user.Id, - AgentId = foundAgent?.Id ?? agentRecord.Id, + AgentId = agentRecord.Id, Editable = false, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow @@ -61,7 +42,7 @@ public partial class AgentService Utilities.ClearCache(); - return agentRecord; + return await Task.FromResult(agentRecord); } private Agent FetchAgentFileByName(string agentName, string filePath) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs index 23111fd2..1fe5c6e1 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs @@ -1,9 +1,20 @@ +using BotSharp.Abstraction.Users.Enums; + namespace BotSharp.Core.Agents.Services; public partial class AgentService { public async Task DeleteAgent(string id) { - throw new NotImplementedException(); + var user = _db.GetUserById(_user.Id); + var agent = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Id.IsEqualTo(id)); + + if (user?.Role != UserRole.Admin && agent == null) + { + return false; + } + + var deleted = _db.DeleteAgent(id); + return await Task.FromResult(deleted); } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index a46669a5..b7141a5b 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -436,7 +436,40 @@ namespace BotSharp.Core.Repository public bool DeleteAgent(string agentId) { - return false; + if (string.IsNullOrEmpty(agentId)) return false; + + try + { + var agentDir = GetAgentDataDir(agentId); + if (string.IsNullOrEmpty(agentDir)) return false; + + // Delete agent user relationships + var usersDir = Path.Combine(_dbSettings.FileRepository, "users"); + if (Directory.Exists(usersDir)) + { + foreach (var userDir in Directory.GetDirectories(usersDir)) + { + var userAgentFile = Directory.GetFiles(userDir).FirstOrDefault(x => Path.GetFileName(x) == USER_AGENT_FILE); + if (string.IsNullOrEmpty(userAgentFile)) continue; + + var text = File.ReadAllText(userAgentFile); + var userAgents = JsonSerializer.Deserialize>(text, _options); + if (userAgents.IsNullOrEmpty()) continue; + + userAgents = userAgents.Where(x => x.AgentId != agentId).ToList(); + File.WriteAllText(userAgentFile, JsonSerializer.Serialize(userAgents, _options)); + } + } + + // Delete agent folder + Directory.Delete(agentDir, true); + + return true; + } + catch + { + return false; + } } } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index d867e782..a7b5652d 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Users.Enums; namespace BotSharp.OpenAPI.Controllers; @@ -27,7 +26,7 @@ public class AgentController : ControllerBase } [HttpGet("/agent/{id}")] - public async Task GetAgent([FromRoute] string id) + public async Task GetAgent([FromRoute] string id) { var agents = await GetAgents(new AgentFilter { @@ -35,6 +34,8 @@ public class AgentController : ControllerBase }); var targetAgent = agents.Items.FirstOrDefault(); + if (targetAgent == null) return null; + var redirectAgentIds = targetAgent.RoutingRules .Where(x => !string.IsNullOrEmpty(x.RedirectTo)) .Select(x => x.RedirectTo).ToList(); @@ -133,4 +134,10 @@ public class AgentController : ControllerBase model.Id = agentId; return await _agentService.PatchAgentTemplate(model); } + + [HttpDelete("/agent/{agentId}")] + public async Task DeleteAgent([FromRoute] string agentId) + { + return await _agentService.DeleteAgent(agentId); + } } \ No newline at end of file From c93511065a7b50218b02b326dbb39633c9100b94 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 21 May 2024 23:33:31 -0500 Subject: [PATCH 09/19] refine file storage --- .../Files/IBotSharpFileService.cs | 7 +- .../Files/BotSharpFileService.cs | 105 ++++++++++++++---- .../Controllers/FileController.cs | 25 ++++- .../ViewModels/Users/UserViewModel.cs | 4 +- .../WebSocketsMiddleware.cs | 22 +++- 5 files changed, 133 insertions(+), 30 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs index 272abf0d..16197b9c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs @@ -5,8 +5,11 @@ public interface IBotSharpFileService string GetDirectory(string conversationId); IEnumerable GetChatImages(string conversationId, List conversations, int offset = 2); IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false); - string? GetMessageFile(string conversationId, string messageId, string fileName); - void SaveMessageFiles(string conversationId, string messageId, List files); + string GetMessageFile(string conversationId, string messageId, string fileName); + bool SaveMessageFiles(string conversationId, string messageId, List files); + + string GetUserAvatar(); + bool SaveUserAvatar(BotSharpFile file); /// /// Delete files under messages diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs index d7e961be..178790bc 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.StaticFiles; +using System; using System.IO; using System.Threading; @@ -8,21 +9,27 @@ public class BotSharpFileService : IBotSharpFileService { private readonly BotSharpDatabaseSettings _dbSettings; private readonly IServiceProvider _services; + private readonly IUserIdentity _user; private readonly ILogger _logger; private readonly string _baseDir; private readonly IEnumerable _allowedTypes = new List { "image/png", "image/jpeg" }; private const string CONVERSATION_FOLDER = "conversations"; private const string FILE_FOLDER = "files"; + private const string USERS_FOLDER = "users"; + private const string USER_AVATAR_FOLDER = "avatar"; + private const int MIN_OFFSET = 1; private const int MAX_OFFSET = 5; public BotSharpFileService( BotSharpDatabaseSettings dbSettings, + IUserIdentity user, ILogger logger, IServiceProvider services) { _dbSettings = dbSettings; + _user = user; _logger = logger; _services = services; _baseDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbSettings.FileRepository); @@ -38,7 +45,7 @@ public class BotSharpFileService : IBotSharpFileService return dir; } - public IEnumerable GetChatImages(string conversationId, List conversations, int offset = 2) + public IEnumerable GetChatImages(string conversationId, List conversations, int offset = 1) { var files = new List(); if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty()) @@ -68,7 +75,7 @@ public class BotSharpFileService : IBotSharpFileService foreach (var messageId in messageIds) { var dir = GetConversationFileDirectory(conversationId, messageId); - if (string.IsNullOrEmpty(dir)) + if (!ExistDirectory(dir)) { continue; } @@ -101,24 +108,24 @@ public class BotSharpFileService : IBotSharpFileService return files; } - public string? GetMessageFile(string conversationId, string messageId, string fileName) + public string GetMessageFile(string conversationId, string messageId, string fileName) { var dir = GetConversationFileDirectory(conversationId, messageId); - if (string.IsNullOrEmpty(dir)) + if (!ExistDirectory(dir)) { - return null; + return string.Empty; } var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName)); return found; } - public void SaveMessageFiles(string conversationId, string messageId, List files) + public bool SaveMessageFiles(string conversationId, string messageId, List files) { - if (files.IsNullOrEmpty()) return; + if (files.IsNullOrEmpty()) return false; var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); - if (string.IsNullOrEmpty(dir)) return; + if (!ExistDirectory(dir)) return false; try { @@ -136,10 +143,53 @@ public class BotSharpFileService : IBotSharpFileService Thread.Sleep(100); File.WriteAllBytes(Path.Combine(dir, fileName), bytes); } + return true; } catch (Exception ex) { - _logger.LogError($"Error when saving conversation files: {ex.Message}"); + _logger.LogWarning($"Error when saving conversation files: {ex.Message}"); + return false; + } + } + + public string GetUserAvatar() + { + var db = _services.GetRequiredService(); + var user = db.GetUserById(_user.Id); + var dir = GetUserAvatarDir(user?.Id); + + if (!ExistDirectory(dir)) return string.Empty; + + var found = Directory.GetFiles(dir).FirstOrDefault() ?? string.Empty; + return found; + } + + public bool SaveUserAvatar(BotSharpFile file) + { + if (file == null || string.IsNullOrEmpty(file.FileData)) return false; + + try + { + var db = _services.GetRequiredService(); + var user = db.GetUserById(_user.Id); + var dir = GetUserAvatarDir(user?.Id); + + if (string.IsNullOrEmpty(dir)) return false; + + if (Directory.Exists(dir)) + { + Directory.Delete(dir, true); + } + + dir = GetUserAvatarDir(user?.Id, createNewDir: true); + var (_, bytes) = GetFileInfoFromData(file.FileData); + File.WriteAllBytes(Path.Combine(dir, file.FileName), bytes); + return true; + } + catch (Exception ex) + { + _logger.LogWarning($"Error when saving user avatar: {ex.Message}"); + return false; } } @@ -152,9 +202,9 @@ public class BotSharpFileService : IBotSharpFileService var prevDir = GetConversationFileDirectory(conversationId, targetMessageId); var newDir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId); - if (Directory.Exists(prevDir)) + if (ExistDirectory(prevDir)) { - if (Directory.Exists(newDir)) + if (ExistDirectory(newDir)) { Directory.Delete(newDir, true); } @@ -182,7 +232,7 @@ public class BotSharpFileService : IBotSharpFileService foreach (var conversationId in conversationIds) { var convDir = FindConversationDirectory(conversationId); - if (string.IsNullOrEmpty(convDir)) continue; + if (!ExistDirectory(convDir)) continue; Directory.Delete(convDir, true); } @@ -215,16 +265,9 @@ public class BotSharpFileService : IBotSharpFileService } var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); - if (!Directory.Exists(dir)) + if (!Directory.Exists(dir) && createNewDir) { - if (createNewDir) - { - Directory.CreateDirectory(dir); - } - else - { - return string.Empty; - } + Directory.CreateDirectory(dir); } return dir; } @@ -234,8 +277,21 @@ public class BotSharpFileService : IBotSharpFileService if (string.IsNullOrEmpty(conversationId)) return null; var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId); - if (!Directory.Exists(dir)) return null; + return dir; + } + private string GetUserAvatarDir(string? userId, bool createNewDir = false) + { + if (string.IsNullOrEmpty(userId)) + { + return string.Empty; + } + + var dir = Path.Combine(_baseDir, USERS_FOLDER, userId, USER_AVATAR_FOLDER); + if (!Directory.Exists(dir) && createNewDir) + { + Directory.CreateDirectory(dir); + } return dir; } @@ -250,5 +306,10 @@ public class BotSharpFileService : IBotSharpFileService return contentType; } + + private bool ExistDirectory(string? dir) + { + return !string.IsNullOrEmpty(dir) && Directory.Exists(dir); + } #endregion } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs index cfae602c..0c7fa255 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/FileController.cs @@ -46,7 +46,7 @@ public class FileController : ControllerBase } [HttpGet("/conversation/{conversationId}/message/{messageId}/file/{fileName}")] - public async Task GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName) + public IActionResult GetMessageFile([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string fileName) { var fileService = _services.GetRequiredService(); var file = fileService.GetMessageFile(conversationId, messageId, fileName); @@ -54,7 +54,30 @@ public class FileController : ControllerBase { return NotFound(); } + return BuildFileResult(file); + } + [HttpPost("/user/avatar")] + public bool UploadUserAvatar([FromBody] BotSharpFile file) + { + var fileService = _services.GetRequiredService(); + return fileService.SaveUserAvatar(file); + } + + [HttpGet("/user/avatar")] + public IActionResult GetUserAvatar() + { + var fileService = _services.GetRequiredService(); + var file = fileService.GetUserAvatar(); + if (string.IsNullOrEmpty(file)) + { + return NotFound(); + } + return BuildFileResult(file); + } + + private FileContentResult BuildFileResult(string file) + { using Stream stream = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read); var bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs index eeb1a8a2..8d28cf03 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs @@ -19,6 +19,7 @@ public class UserViewModel public string Source { get; set; } [JsonPropertyName("external_id")] public string? ExternalId { get; set; } + public string Avatar { get; set; } = "/user/avatar"; [JsonPropertyName("create_date")] public DateTime CreateDate { get; set; } [JsonPropertyName("update_date")] @@ -47,7 +48,8 @@ public class UserViewModel Source = user.Source, ExternalId = user.ExternalId, CreateDate = user.CreatedTime, - UpdateDate = user.UpdatedTime + UpdateDate = user.UpdatedTime, + Avatar = "/user/avatar" }; } } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs b/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs index e20f8602..79cb803a 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs @@ -14,13 +14,11 @@ public class WebSocketsMiddleware public async Task Invoke(HttpContext httpContext) { - var request = httpContext.Request;; - var messageFileRegex = new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase); + var request = httpContext.Request; // web sockets cannot pass headers so we must take the access token from query param and // add it to the header before authentication middleware runs - if ((request.Path.StartsWithSegments("/chatHub", StringComparison.OrdinalIgnoreCase) - || messageFileRegex.IsMatch(request.Path.Value ?? string.Empty)) && + if ((VerifyChatHubRequest(request) || VerifyGetRequest(request)) && request.Query.TryGetValue("access_token", out var accessToken)) { request.Headers["Authorization"] = $"Bearer {accessToken}"; @@ -28,4 +26,20 @@ public class WebSocketsMiddleware await _next(httpContext); } + + private bool VerifyChatHubRequest(HttpRequest request) + { + return request.Path.StartsWithSegments("/chatHub", StringComparison.OrdinalIgnoreCase); + } + + private bool VerifyGetRequest(HttpRequest request) + { + var regexes = new List + { + new Regex(@"/conversation/[a-z0-9-]+/message/[a-z0-9-]+/file/[a-z0-9-]+", RegexOptions.IgnoreCase), + new Regex(@"/user/avatar", RegexOptions.IgnoreCase) + }; + + return request.Method.IsEqualTo("GET") && regexes.Any(x => x.IsMatch(request.Path.Value ?? string.Empty)); + } } From 9dad9ae80be85d3615995173d561470dee1e2c61 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 22 May 2024 10:39:04 -0500 Subject: [PATCH 10/19] split file service --- .../Files/BotSharpFileService.Conversation.cs | 188 ++++++++++++++ .../Files/BotSharpFileService.User.cs | 65 +++++ .../Files/BotSharpFileService.cs | 234 +----------------- 3 files changed, 254 insertions(+), 233 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs create mode 100644 src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.User.cs diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs new file mode 100644 index 00000000..f12ecb60 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs @@ -0,0 +1,188 @@ +using Microsoft.AspNetCore.StaticFiles; +using System.IO; +using System.Threading; + +namespace BotSharp.Core.Files; + +public partial class BotSharpFileService +{ + public IEnumerable GetChatImages(string conversationId, List conversations, int offset = 1) + { + var files = new List(); + if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty()) + { + return files; + } + + if (offset <= 0) + { + offset = MIN_OFFSET; + } + else if (offset > MAX_OFFSET) + { + offset = MAX_OFFSET; + } + + var messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset).ToList(); + files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList(); + return files; + } + + public IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false) + { + var files = new List(); + if (messageIds.IsNullOrEmpty()) return files; + + foreach (var messageId in messageIds) + { + var dir = GetConversationFileDirectory(conversationId, messageId); + if (!ExistDirectory(dir)) + { + continue; + } + + foreach (var file in Directory.GetFiles(dir)) + { + var contentType = GetFileContentType(file); + if (imageOnly && !_allowedTypes.Contains(contentType)) + { + continue; + } + + var fileName = Path.GetFileNameWithoutExtension(file); + var extension = Path.GetExtension(file); + var fileType = extension.Substring(1); + + var model = new MessageFileModel() + { + MessageId = messageId, + FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}", + FileStorageUrl = file, + FileName = fileName, + FileType = fileType, + ContentType = contentType + }; + files.Add(model); + } + } + + return files; + } + + public string GetMessageFile(string conversationId, string messageId, string fileName) + { + var dir = GetConversationFileDirectory(conversationId, messageId); + if (!ExistDirectory(dir)) + { + return string.Empty; + } + + var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName)); + return found; + } + + public bool SaveMessageFiles(string conversationId, string messageId, List files) + { + if (files.IsNullOrEmpty()) return false; + + var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); + if (!ExistDirectory(dir)) return false; + + try + { + for (int i = 0; i < files.Count; i++) + { + var file = files[i]; + if (string.IsNullOrEmpty(file.FileData)) + { + continue; + } + + var (_, bytes) = GetFileInfoFromData(file.FileData); + var fileType = Path.GetExtension(file.FileName); + var fileName = $"{i + 1}{fileType}"; + Thread.Sleep(100); + File.WriteAllBytes(Path.Combine(dir, fileName), bytes); + } + return true; + } + catch (Exception ex) + { + _logger.LogWarning($"Error when saving conversation files: {ex.Message}"); + return false; + } + } + + + + public bool DeleteMessageFiles(string conversationId, IEnumerable messageIds, string targetMessageId, string? newMessageId = null) + { + if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false; + + if (!string.IsNullOrEmpty(targetMessageId) && !string.IsNullOrEmpty(newMessageId)) + { + var prevDir = GetConversationFileDirectory(conversationId, targetMessageId); + var newDir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId); + + if (ExistDirectory(prevDir)) + { + if (ExistDirectory(newDir)) + { + Directory.Delete(newDir, true); + } + + Directory.Move(prevDir, newDir); + } + } + + foreach (var messageId in messageIds) + { + var dir = GetConversationFileDirectory(conversationId, messageId); + if (string.IsNullOrEmpty(dir)) continue; + + Thread.Sleep(100); + Directory.Delete(dir, true); + } + + return true; + } + + public bool DeleteConversationFiles(IEnumerable conversationIds) + { + if (conversationIds.IsNullOrEmpty()) return false; + + foreach (var conversationId in conversationIds) + { + var convDir = FindConversationDirectory(conversationId); + if (!ExistDirectory(convDir)) continue; + + Directory.Delete(convDir, true); + } + return true; + } + + #region Private methods + private string GetConversationFileDirectory(string? conversationId, string? messageId, bool createNewDir = false) + { + if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) + { + return string.Empty; + } + + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); + if (!Directory.Exists(dir) && createNewDir) + { + Directory.CreateDirectory(dir); + } + return dir; + } + + private string? FindConversationDirectory(string conversationId) + { + if (string.IsNullOrEmpty(conversationId)) return null; + + var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId); + return dir; + } + #endregion +} diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.User.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.User.cs new file mode 100644 index 00000000..b6a87993 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.User.cs @@ -0,0 +1,65 @@ +using System.IO; + +namespace BotSharp.Core.Files; + +public partial class BotSharpFileService +{ + public string GetUserAvatar() + { + var db = _services.GetRequiredService(); + var user = db.GetUserById(_user.Id); + var dir = GetUserAvatarDir(user?.Id); + + if (!ExistDirectory(dir)) return string.Empty; + + var found = Directory.GetFiles(dir).FirstOrDefault() ?? string.Empty; + return found; + } + + public bool SaveUserAvatar(BotSharpFile file) + { + if (file == null || string.IsNullOrEmpty(file.FileData)) return false; + + try + { + var db = _services.GetRequiredService(); + var user = db.GetUserById(_user.Id); + var dir = GetUserAvatarDir(user?.Id); + + if (string.IsNullOrEmpty(dir)) return false; + + if (Directory.Exists(dir)) + { + Directory.Delete(dir, true); + } + + dir = GetUserAvatarDir(user?.Id, createNewDir: true); + var (_, bytes) = GetFileInfoFromData(file.FileData); + File.WriteAllBytes(Path.Combine(dir, file.FileName), bytes); + return true; + } + catch (Exception ex) + { + _logger.LogWarning($"Error when saving user avatar: {ex.Message}"); + return false; + } + } + + + #region Private methods + private string GetUserAvatarDir(string? userId, bool createNewDir = false) + { + if (string.IsNullOrEmpty(userId)) + { + return string.Empty; + } + + var dir = Path.Combine(_baseDir, USERS_FOLDER, userId, USER_AVATAR_FOLDER); + if (!Directory.Exists(dir) && createNewDir) + { + Directory.CreateDirectory(dir); + } + return dir; + } + #endregion +} diff --git a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs index 178790bc..76d26dbc 100644 --- a/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.cs @@ -5,7 +5,7 @@ using System.Threading; namespace BotSharp.Core.Files; -public class BotSharpFileService : IBotSharpFileService +public partial class BotSharpFileService : IBotSharpFileService { private readonly BotSharpDatabaseSettings _dbSettings; private readonly IServiceProvider _services; @@ -45,200 +45,6 @@ public class BotSharpFileService : IBotSharpFileService return dir; } - public IEnumerable GetChatImages(string conversationId, List conversations, int offset = 1) - { - var files = new List(); - if (string.IsNullOrEmpty(conversationId) || conversations.IsNullOrEmpty()) - { - return files; - } - - if (offset <= 0) - { - offset = MIN_OFFSET; - } - else if (offset > MAX_OFFSET) - { - offset = MAX_OFFSET; - } - - var messageIds = conversations.Select(x => x.MessageId).Distinct().TakeLast(offset).ToList(); - files = GetMessageFiles(conversationId, messageIds, imageOnly: true).ToList(); - return files; - } - - public IEnumerable GetMessageFiles(string conversationId, IEnumerable messageIds, bool imageOnly = false) - { - var files = new List(); - if (messageIds.IsNullOrEmpty()) return files; - - foreach (var messageId in messageIds) - { - var dir = GetConversationFileDirectory(conversationId, messageId); - if (!ExistDirectory(dir)) - { - continue; - } - - foreach (var file in Directory.GetFiles(dir)) - { - var contentType = GetFileContentType(file); - if (imageOnly && !_allowedTypes.Contains(contentType)) - { - continue; - } - - var fileName = Path.GetFileNameWithoutExtension(file); - var extension = Path.GetExtension(file); - var fileType = extension.Substring(1); - - var model = new MessageFileModel() - { - MessageId = messageId, - FileUrl = $"/conversation/{conversationId}/message/{messageId}/file/{fileName}", - FileStorageUrl = file, - FileName = fileName, - FileType = fileType, - ContentType = contentType - }; - files.Add(model); - } - } - - return files; - } - - public string GetMessageFile(string conversationId, string messageId, string fileName) - { - var dir = GetConversationFileDirectory(conversationId, messageId); - if (!ExistDirectory(dir)) - { - return string.Empty; - } - - var found = Directory.GetFiles(dir).FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).IsEqualTo(fileName)); - return found; - } - - public bool SaveMessageFiles(string conversationId, string messageId, List files) - { - if (files.IsNullOrEmpty()) return false; - - var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true); - if (!ExistDirectory(dir)) return false; - - try - { - for (int i = 0; i < files.Count; i++) - { - var file = files[i]; - if (string.IsNullOrEmpty(file.FileData)) - { - continue; - } - - var (_, bytes) = GetFileInfoFromData(file.FileData); - var fileType = Path.GetExtension(file.FileName); - var fileName = $"{i + 1}{fileType}"; - Thread.Sleep(100); - File.WriteAllBytes(Path.Combine(dir, fileName), bytes); - } - return true; - } - catch (Exception ex) - { - _logger.LogWarning($"Error when saving conversation files: {ex.Message}"); - return false; - } - } - - public string GetUserAvatar() - { - var db = _services.GetRequiredService(); - var user = db.GetUserById(_user.Id); - var dir = GetUserAvatarDir(user?.Id); - - if (!ExistDirectory(dir)) return string.Empty; - - var found = Directory.GetFiles(dir).FirstOrDefault() ?? string.Empty; - return found; - } - - public bool SaveUserAvatar(BotSharpFile file) - { - if (file == null || string.IsNullOrEmpty(file.FileData)) return false; - - try - { - var db = _services.GetRequiredService(); - var user = db.GetUserById(_user.Id); - var dir = GetUserAvatarDir(user?.Id); - - if (string.IsNullOrEmpty(dir)) return false; - - if (Directory.Exists(dir)) - { - Directory.Delete(dir, true); - } - - dir = GetUserAvatarDir(user?.Id, createNewDir: true); - var (_, bytes) = GetFileInfoFromData(file.FileData); - File.WriteAllBytes(Path.Combine(dir, file.FileName), bytes); - return true; - } - catch (Exception ex) - { - _logger.LogWarning($"Error when saving user avatar: {ex.Message}"); - return false; - } - } - - public bool DeleteMessageFiles(string conversationId, IEnumerable messageIds, string targetMessageId, string? newMessageId = null) - { - if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false; - - if (!string.IsNullOrEmpty(targetMessageId) && !string.IsNullOrEmpty(newMessageId)) - { - var prevDir = GetConversationFileDirectory(conversationId, targetMessageId); - var newDir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, newMessageId); - - if (ExistDirectory(prevDir)) - { - if (ExistDirectory(newDir)) - { - Directory.Delete(newDir, true); - } - - Directory.Move(prevDir, newDir); - } - } - - foreach ( var messageId in messageIds) - { - var dir = GetConversationFileDirectory(conversationId, messageId); - if (string.IsNullOrEmpty(dir)) continue; - - Thread.Sleep(100); - Directory.Delete(dir, true); - } - - return true; - } - - public bool DeleteConversationFiles(IEnumerable conversationIds) - { - if (conversationIds.IsNullOrEmpty()) return false; - - foreach (var conversationId in conversationIds) - { - var convDir = FindConversationDirectory(conversationId); - if (!ExistDirectory(convDir)) continue; - - Directory.Delete(convDir, true); - } - return true; - } - public (string, byte[]) GetFileInfoFromData(string data) { if (string.IsNullOrEmpty(data)) @@ -257,44 +63,6 @@ public class BotSharpFileService : IBotSharpFileService } #region Private methods - private string GetConversationFileDirectory(string? conversationId, string? messageId, bool createNewDir = false) - { - if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) - { - return string.Empty; - } - - var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId); - if (!Directory.Exists(dir) && createNewDir) - { - Directory.CreateDirectory(dir); - } - return dir; - } - - private string? FindConversationDirectory(string conversationId) - { - if (string.IsNullOrEmpty(conversationId)) return null; - - var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId); - return dir; - } - - private string GetUserAvatarDir(string? userId, bool createNewDir = false) - { - if (string.IsNullOrEmpty(userId)) - { - return string.Empty; - } - - var dir = Path.Combine(_baseDir, USERS_FOLDER, userId, USER_AVATAR_FOLDER); - if (!Directory.Exists(dir) && createNewDir) - { - Directory.CreateDirectory(dir); - } - return dir; - } - private string GetFileContentType(string filePath) { string contentType; From 2a456df8465140f1c4026c4a795deb4c3cfc8b65 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 20:40:24 -0500 Subject: [PATCH 11/19] temp save --- .../Conversations/IConversationService.cs | 2 ++ .../BotSharp.Core/BotSharp.Core.csproj | 6 +++- .../Services/ConversationService.Summary.cs | 32 +++++++++++++++++++ .../templates/conversation.summary.liquid | 6 ++++ .../Controllers/ConversationController.cs | 7 ++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 9df6ae7f..18cee8fc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -53,4 +53,6 @@ public interface IConversationService /// Append user init words /// Task UpdateBreakpoint(bool resetStates = false, string? reason = null); + + Task GetConversationSummary(string conversationId); } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 0ddc52e8..10e21b3c 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -56,6 +56,7 @@ + @@ -142,6 +143,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs new file mode 100644 index 00000000..53c6470a --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -0,0 +1,32 @@ +using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Templating; + +namespace BotSharp.Core.Conversations.Services; + +public partial class ConversationService +{ + public async Task GetConversationSummary(string conversationId) + { + if (string.IsNullOrEmpty(conversationId)) return string.Empty; + + var dialogs = _storage.GetDialogs(conversationId); + + return string.Empty; + } + + private IEnumerable BuildConversationContent(List dialogs) + { + + } + + private string GetPrompt(Agent router, List dialogs) + { + var template = router.Templates.First(x => x.Name == "conversation.summary").Content; + + var render = _services.GetRequiredService(); + return render.Render(template, new Dictionary + { + { "conversation", } + }); + } +} diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid new file mode 100644 index 00000000..3b6bb53f --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -0,0 +1,6 @@ +Please follow these steps to summarize the conversation: +1. Read the [CONVERSATION] content. +2. Summarize the conversation in one sentence. + +[CONVERSATION] +{{ conversation }} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index fd66b225..ef44472a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -134,6 +134,13 @@ public class ConversationController : ControllerBase return result; } + [HttpGet("/conversation/{conversationId}/summary")] + public async Task GetConversationSummary([FromRoute] string conversationId) + { + var service = _services.GetRequiredService(); + return await service.GetConversationSummary(conversationId); + } + [HttpGet("/conversation/{conversationId}/user")] public async Task GetConversationUser([FromRoute] string conversationId) { From 6b5d77604e635b614f836106817c25c4d3957f8c Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 21:30:57 -0500 Subject: [PATCH 12/19] add conversation summary --- .../Services/ConversationService.Summary.cs | 54 ++++++++++++++----- .../Services/ConversationService.cs | 2 + .../templates/conversation.summary.liquid | 7 +-- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 53c6470a..00924b93 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -9,24 +9,52 @@ public partial class ConversationService { if (string.IsNullOrEmpty(conversationId)) return string.Empty; + var routing = _services.GetRequiredService(); + var agentService = _services.GetRequiredService(); + var dialogs = _storage.GetDialogs(conversationId); + if (dialogs.IsNullOrEmpty()) return string.Empty; - return string.Empty; + var router = await agentService.LoadAgent(AIAssistant); + var content = await routing.GetConversationContent(dialogs); + var prompt = GetPrompt(router, content); + var summary = await Summarize(router, prompt, dialogs); + + return summary; } - private IEnumerable BuildConversationContent(List dialogs) + private string GetPrompt(Agent agent, string content) { - - } - - private string GetPrompt(Agent router, List dialogs) - { - var template = router.Templates.First(x => x.Name == "conversation.summary").Content; - + var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var render = _services.GetRequiredService(); - return render.Render(template, new Dictionary + return render.Render(template, new Dictionary { }); + } + + private async Task Summarize(Agent agent, string prompt, List dialogs) + { + var provider = agent.LlmConfig.Provider; + var model = agent.LlmConfig.Model; + + if (provider == null || model == null) { - { "conversation", } - }); + var agentSettings = _services.GetRequiredService(); + provider = agentSettings.LlmConfig.Provider; + model = agentSettings.LlmConfig.Model; + } + + var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); + var response = await chatCompletion.GetChatCompletions(new Agent + { + Id = agent.Id, + Name = agent.Name, + Instruction = prompt + }, dialogs); + + return response.Content; + } + + private void SaveState(string summary) + { + _state.SetState("conversation_summary", summary, source: StateSource.Application); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 4de36b9d..8e113226 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -12,6 +12,8 @@ public partial class ConversationService : IConversationService private readonly IConversationStorage _storage; private readonly IConversationStateService _state; private string _conversationId; + private const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"; + public string ConversationId => _conversationId; public IConversationStateService States => _state; diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index 3b6bb53f..a935e497 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1,6 +1 @@ -Please follow these steps to summarize the conversation: -1. Read the [CONVERSATION] content. -2. Summarize the conversation in one sentence. - -[CONVERSATION] -{{ conversation }} \ No newline at end of file +Please summarize the conversation. \ No newline at end of file From 953a53c4bc00eb798b48efb418bb39d6a9ea65aa Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 22:19:06 -0500 Subject: [PATCH 13/19] clean code --- .../Conversations/Services/ConversationService.Summary.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 00924b93..252d1e7f 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -52,9 +51,4 @@ public partial class ConversationService return response.Content; } - - private void SaveState(string summary) - { - _state.SetState("conversation_summary", summary, source: StateSource.Application); - } } From 6c5be6da631595e83338873ff602d1edd6badad5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 22:20:27 -0500 Subject: [PATCH 14/19] clean code --- .../Conversations/Services/ConversationService.Summary.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 252d1e7f..d76678cc 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -15,14 +15,13 @@ public partial class ConversationService if (dialogs.IsNullOrEmpty()) return string.Empty; var router = await agentService.LoadAgent(AIAssistant); - var content = await routing.GetConversationContent(dialogs); - var prompt = GetPrompt(router, content); + var prompt = GetPrompt(router); var summary = await Summarize(router, prompt, dialogs); return summary; } - private string GetPrompt(Agent agent, string content) + private string GetPrompt(Agent agent) { var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var render = _services.GetRequiredService(); From b8becca94542c0fabb384df4712f0b72551b67d7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 22:20:56 -0500 Subject: [PATCH 15/19] minor change --- .../Conversations/Services/ConversationService.Summary.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index d76678cc..4a547204 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -30,8 +30,8 @@ public partial class ConversationService private async Task Summarize(Agent agent, string prompt, List dialogs) { - var provider = agent.LlmConfig.Provider; - var model = agent.LlmConfig.Model; + var provider = agent?.LlmConfig?.Provider; + var model = agent?.LlmConfig?.Model; if (provider == null || model == null) { From a5dad4dee48b54b369299b205b8e329d6b79a5b5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 27 May 2024 12:12:39 -0500 Subject: [PATCH 16/19] refine summary prompt --- .../Services/ConversationService.Summary.cs | 28 ++++++++++++++----- .../templates/conversation.summary.liquid | 9 +++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 4a547204..25d754d6 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -30,17 +31,30 @@ public partial class ConversationService private async Task Summarize(Agent agent, string prompt, List dialogs) { - var provider = agent?.LlmConfig?.Provider; - var model = agent?.LlmConfig?.Model; + var provider = "openai"; + string? model; - if (provider == null || model == null) + var providerService = _services.GetRequiredService(); + var modelSettings = providerService.GetProviderModels(provider); + var modelSetting = modelSettings.FirstOrDefault(x => x.Name.IsEqualTo("gpt4-turbo") || x.Name.IsEqualTo("gpt-4o")); + + if (modelSetting != null) { - var agentSettings = _services.GetRequiredService(); - provider = agentSettings.LlmConfig.Provider; - model = agentSettings.LlmConfig.Model; + model = modelSetting.Name; + } + else + { + provider = agent?.LlmConfig?.Provider; + model = agent?.LlmConfig?.Model; + if (provider == null || model == null) + { + var agentSettings = _services.GetRequiredService(); + provider = agentSettings.LlmConfig.Provider; + model = agentSettings.LlmConfig.Model; + } } - var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); + var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider, model); var response = await chatCompletion.GetChatCompletions(new Agent { Id = agent.Id, diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index a935e497..08792af3 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1 +1,8 @@ -Please summarize the conversation. \ No newline at end of file +Please summarize the conversation. + +*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. *** +** Please do not respond to the latest conversation. +** If there are different topics in the conversation, please summarize each topic in different sentences and list them in bullets. +* Please use concise sentences to summarize each topic. +* Please do not include excessive details in the summaries. +* Please use 'user' instead of 'you', 'he' or 'she'. \ No newline at end of file From 520b91507c4e5964aab0c5a2bf7abee157061608 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 27 May 2024 12:51:00 -0500 Subject: [PATCH 17/19] minor change --- .../templates/conversation.summary.liquid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index 08792af3..e2ffe5db 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -2,7 +2,7 @@ Please summarize the conversation. *** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. *** ** Please do not respond to the latest conversation. -** If there are different topics in the conversation, please summarize each topic in different sentences and list them in bullets. +** If there are different topics in the conversation, please summarize each topic in different sentences and list them with bullets. * Please use concise sentences to summarize each topic. * Please do not include excessive details in the summaries. * Please use 'user' instead of 'you', 'he' or 'she'. \ No newline at end of file From 0b56a18aa682ee237b59c7ca7b168dab7757466a Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 28 May 2024 21:50:16 -0500 Subject: [PATCH 18/19] summarize multiple conversations --- .../Conversations/IConversationService.cs | 2 +- .../Services/ConversationService.Summary.cs | 61 ++++++++++++++++--- .../templates/conversation.summary.liquid | 14 +++-- .../Controllers/ConversationController.cs | 6 +- .../Conversations/ConversationSummaryModel.cs | 9 +++ 5 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationSummaryModel.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 2aa8a72f..c8b997ec 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -55,5 +55,5 @@ public interface IConversationService /// Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates); - Task GetConversationSummary(string conversationId); + Task GetConversationSummary(IEnumerable conversationId); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 25d754d6..533020f0 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -5,31 +5,51 @@ namespace BotSharp.Core.Conversations.Services; public partial class ConversationService { - public async Task GetConversationSummary(string conversationId) + public async Task GetConversationSummary(IEnumerable conversationIds) { - if (string.IsNullOrEmpty(conversationId)) return string.Empty; + if (conversationIds.IsNullOrEmpty()) return string.Empty; var routing = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); - var dialogs = _storage.GetDialogs(conversationId); - if (dialogs.IsNullOrEmpty()) return string.Empty; + var contents = new List(); + foreach ( var conversationId in conversationIds) + { + if (string.IsNullOrEmpty(conversationId)) continue; + + var dialogs = _storage.GetDialogs(conversationId); + + if (dialogs.IsNullOrEmpty()) continue; + + var content = GetConversationContent(dialogs); + contents.Add(content); + } var router = await agentService.LoadAgent(AIAssistant); - var prompt = GetPrompt(router); - var summary = await Summarize(router, prompt, dialogs); + var prompt = GetPrompt(router, contents); + var summary = await Summarize(router, prompt); return summary; } - private string GetPrompt(Agent agent) + private string GetPrompt(Agent agent, List contents) { var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var render = _services.GetRequiredService(); - return render.Render(template, new Dictionary { }); + + var texts = string.Empty; + for (int i = 0; i < contents.Count; i++) + { + texts += $"[Conversation {i+1}]\r\n{contents[i]}"; + } + + return render.Render(template, new Dictionary + { + { "texts", texts } + }); } - private async Task Summarize(Agent agent, string prompt, List dialogs) + private async Task Summarize(Agent agent, string prompt) { var provider = "openai"; string? model; @@ -60,8 +80,29 @@ public partial class ConversationService Id = agent.Id, Name = agent.Name, Instruction = prompt - }, dialogs); + }, new List + { + new RoleDialogModel(AgentRole.User, "Please summarize the conversations.") + }); return response.Content; } + + private string GetConversationContent(List dialogs, int maxDialogCount = 50) + { + var conversation = ""; + + foreach (var dialog in dialogs.TakeLast(maxDialogCount)) + { + var role = dialog.Role; + if (role != AgentRole.User) + { + role = AgentRole.Assistant; + } + + conversation += $"{role}: {dialog.Payload ?? dialog.Content}\r\n"; + } + + return conversation + "\r\n"; + } } diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index e2ffe5db..d7ffae60 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1,8 +1,14 @@ -Please summarize the conversation. +Please read each conversation in the [CONVERSATION] section and provide a summary. -*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. *** +*** Super Important! Please consider every conversation. Do not only consider the recent sentences. *** ** Please do not respond to the latest conversation. -** If there are different topics in the conversation, please summarize each topic in different sentences and list them with bullets. +** If there are different topics in the conversations, please summarize each topic in different sentences and list them in bullets. * Please use concise sentences to summarize each topic. * Please do not include excessive details in the summaries. -* Please use 'user' instead of 'you', 'he' or 'she'. \ No newline at end of file +* Please use 'user' instead of 'you', 'he' or 'she'. + +[CONVERSATIONS] + +{% for text in texts -%} +{{ text }}{{ "\r\n\r\n" }} +{%- endfor %} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index a2c2b3b0..adc37636 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -152,11 +152,11 @@ public class ConversationController : ControllerBase return result; } - [HttpGet("/conversation/{conversationId}/summary")] - public async Task GetConversationSummary([FromRoute] string conversationId) + [HttpPost("/conversation/summary")] + public async Task GetConversationSummary([FromBody] ConversationSummaryModel input) { var service = _services.GetRequiredService(); - return await service.GetConversationSummary(conversationId); + return await service.GetConversationSummary(input.ConversationIds); } [HttpGet("/conversation/{conversationId}/user")] diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationSummaryModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationSummaryModel.cs new file mode 100644 index 00000000..0854ab2a --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationSummaryModel.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.OpenAPI.ViewModels.Conversations; + +public class ConversationSummaryModel +{ + [JsonPropertyName("conversation_ids")] + public List ConversationIds { get; set; } = new List(); +} From 11fa2513b1303f4e6508a17e6587adf2bca612a1 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 28 May 2024 21:51:13 -0500 Subject: [PATCH 19/19] minor change --- .../templates/conversation.summary.liquid | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index d7ffae60..bb4e764f 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1,4 +1,4 @@ -Please read each conversation in the [CONVERSATION] section and provide a summary. +Please read each conversation in the [CONVERSATIONS] section and provide a summary. *** Super Important! Please consider every conversation. Do not only consider the recent sentences. *** ** Please do not respond to the latest conversation. @@ -10,5 +10,5 @@ Please read each conversation in the [CONVERSATION] section and provide a summar [CONVERSATIONS] {% for text in texts -%} -{{ text }}{{ "\r\n\r\n" }} +{{ text }}{{ "\r\n" }} {%- endfor %} \ No newline at end of file