From c4d2f5b0261245be62ad028168dd775b3f1785ac Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 6 Mar 2025 11:31:53 -0600 Subject: [PATCH] relocate --- .../Conversations/IConversationService.cs | 2 - .../Loggers/Services/ILoggerService.cs | 7 ++ .../Services/LoggerService.Conversation.cs} | 5 +- .../Services/LoggerService.Instruction.cs | 64 +++++++++++++++++++ .../Loggers/Services/LoggerService.cs | 62 +----------------- .../Controllers/LoggerController.cs | 8 +-- 6 files changed, 78 insertions(+), 70 deletions(-) rename src/Infrastructure/BotSharp.Core/{Conversations/Services/ConversationService.Log.cs => Loggers/Services/LoggerService.Conversation.cs} (83%) create mode 100644 src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 6d84103e..613940bf 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -27,8 +27,6 @@ public interface IConversationService /// If not null, delete messages while input a new message; otherwise delete messages only /// Task TruncateConversation(string conversationId, string messageId, string? newMessageId = null); - Task> GetConversationContentLogs(string conversationId); - Task> GetConversationStateLogs(string conversationId); /// /// Send message to LLM diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs index aa970bbd..23a4809f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs @@ -5,5 +5,12 @@ namespace BotSharp.Abstraction.Loggers.Services; public interface ILoggerService { + #region Conversation + Task> GetConversationContentLogs(string conversationId); + Task> GetConversationStateLogs(string conversationId); + #endregion + + #region Instruction Task> GetInstructionLogs(InstructLogFilter filter); + #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Log.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs similarity index 83% rename from src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Log.cs rename to src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs index 7b583f3f..5ce057c3 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Log.cs +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs @@ -1,9 +1,8 @@ using BotSharp.Abstraction.Loggers.Models; -using BotSharp.Abstraction.Repositories; -namespace BotSharp.Core.Conversations.Services; +namespace BotSharp.Core.Loggers.Services; -public partial class ConversationService +public partial class LoggerService { public async Task> GetConversationContentLogs(string conversationId) { diff --git a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs new file mode 100644 index 00000000..18a0564b --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs @@ -0,0 +1,64 @@ +using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.Loggers.Models; +using BotSharp.Abstraction.Users.Enums; +using BotSharp.Abstraction.Users.Models; + +namespace BotSharp.Core.Loggers.Services; + +public partial class LoggerService +{ + public async Task> GetInstructionLogs(InstructLogFilter filter) + { + if (filter == null) + { + filter = InstructLogFilter.Empty(); + } + + var userService = _services.GetRequiredService(); + var user = await userService.GetUser(_user.Id); + var isAdmin = UserConstant.AdminRoles.Contains(user?.Role); + if (!isAdmin && user?.Id == null) return new(); + + filter.UserIds = isAdmin ? [] : user?.Id != null ? [user.Id] : []; + + var agents = new List(); + var users = new List(); + + var db = _services.GetRequiredService(); + var logs = db.GetInstructionLogs(filter); + var agentIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); + var userIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.UserId)).Select(x => x.UserId).ToList(); + agents = db.GetAgents(new AgentFilter + { + AgentIds = agentIds, + Pager = new Pagination { Size = filter.Size } + }); + + if (isAdmin) + { + users = db.GetUserByIds(userIds); + } + + var items = logs.Items.Select(x => + { + x.AgentId = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; + + if (!isAdmin) + { + x.UserName = user != null ? $"{user.FirstName} {user.LastName}" : null; + } + else + { + var found = !string.IsNullOrEmpty(x.UserId) ? users.FirstOrDefault(u => u.Id == x.UserId) : null; + x.UserName = found != null ? $"{found.FirstName} {found.LastName}" : null; + } + return x; + }).ToList(); + + return new PagedItems + { + Items = items, + Count = logs.Count + }; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs index fbd1d78a..bd9a5147 100644 --- a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs @@ -1,11 +1,6 @@ -using BotSharp.Abstraction.Instructs.Models; -using BotSharp.Abstraction.Loggers.Models; -using BotSharp.Abstraction.Users.Enums; -using BotSharp.Abstraction.Users.Models; - namespace BotSharp.Core.Loggers.Services; -public class LoggerService : ILoggerService +public partial class LoggerService : ILoggerService { private readonly IServiceProvider _services; private readonly IUserIdentity _user; @@ -20,59 +15,4 @@ public class LoggerService : ILoggerService _user = user; _logger = logger; } - - public async Task> GetInstructionLogs(InstructLogFilter filter) - { - if (filter == null) - { - filter = InstructLogFilter.Empty(); - } - - var userService = _services.GetRequiredService(); - var user = await userService.GetUser(_user.Id); - var isAdmin = UserConstant.AdminRoles.Contains(user?.Role); - if (!isAdmin && user?.Id == null) return new(); - - filter.UserIds = isAdmin ? [] : user?.Id != null ? [user.Id] : []; - - var agents = new List(); - var users = new List(); - - var db = _services.GetRequiredService(); - var logs = db.GetInstructionLogs(filter); - var agentIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); - var userIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.UserId)).Select(x => x.UserId).ToList(); - agents = db.GetAgents(new AgentFilter - { - AgentIds = agentIds, - Pager = new Pagination { Size = filter.Size } - }); - - if (isAdmin) - { - users = db.GetUserByIds(userIds); - } - - var items = logs.Items.Select(x => - { - x.AgentId = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; - - if (!isAdmin) - { - x.UserName = user != null ? $"{user.FirstName} {user.LastName}" : null; - } - else - { - var found = !string.IsNullOrEmpty(x.UserId) ? users.FirstOrDefault(u => u.Id == x.UserId) : null; - x.UserName = found != null ? $"{found.FirstName} {found.LastName}" : null; - } - return x; - }).ToList(); - - return new PagedItems - { - Items = items, - Count = logs.Count - }; - } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs index de6748db..d67ba8cd 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs @@ -42,15 +42,15 @@ public class LoggerController : ControllerBase [HttpGet("/logger/conversation/{conversationId}/content-log")] public async Task> GetConversationContentLogs([FromRoute] string conversationId) { - var conversationService = _services.GetRequiredService(); - return await conversationService.GetConversationContentLogs(conversationId); + var logging = _services.GetRequiredService(); + return await logging.GetConversationContentLogs(conversationId); } [HttpGet("/logger/conversation/{conversationId}/state-log")] public async Task> GetConversationStateLogs([FromRoute] string conversationId) { - var conversationService = _services.GetRequiredService(); - return await conversationService.GetConversationStateLogs(conversationId); + var logging = _services.GetRequiredService(); + return await logging.GetConversationStateLogs(conversationId); } [HttpGet("/logger/instruction/log")]