From ae84088b5fb5f6afd6c07ad06e15770e61c2648f Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Thu, 6 Mar 2025 11:11:45 -0600 Subject: [PATCH] refine instruction log --- .../Instructs/Models/InstructLogFilter.cs | 3 +- .../Loggers/Models/InstructionLogModel.cs | 3 + .../Loggers/Services/ILoggerService.cs | 9 +++ .../BotSharp.Core/BotSharp.Core.csproj | 2 +- .../Instructs/InsturctionPlugin.cs | 5 +- .../BotSharp.Core/Loggers/LoggerPlugin.cs | 15 ++++ .../Loggers/Services/LoggerService.cs | 78 +++++++++++++++++++ .../FileRepository/FileRepository.Log.cs | 11 +-- src/Infrastructure/BotSharp.Core/Using.cs | 4 +- .../Controllers/LoggerController.cs | 13 +++- .../Instructs/InstructionLogViewModel.cs | 5 ++ .../Repository/MongoRepository.Log.cs | 7 -- 12 files changed, 130 insertions(+), 25 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs create mode 100644 src/Infrastructure/BotSharp.Core/Loggers/LoggerPlugin.cs create mode 100644 src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs index 9d154052..6e192619 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructLogFilter.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; - namespace BotSharp.Abstraction.Instructs.Models; public class InstructLogFilter : Pagination @@ -8,6 +6,7 @@ public class InstructLogFilter : Pagination public List? Providers { get; set; } public List? Models { get; set; } public List? TemplateNames { get; set; } + public List? UserIds { get; set; } public static InstructLogFilter Empty() { diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs index 844d7942..de843023 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/InstructionLogModel.cs @@ -39,6 +39,9 @@ public class InstructionLogModel [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? UserId { get; set; } + [JsonIgnore] + public string? UserName { get; set; } + [JsonIgnore] public Dictionary States { get; set; } = []; diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs new file mode 100644 index 00000000..aa970bbd --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs @@ -0,0 +1,9 @@ +using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.Loggers.Models; + +namespace BotSharp.Abstraction.Loggers.Services; + +public interface ILoggerService +{ + Task> GetInstructionLogs(InstructLogFilter filter); +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 044149f5..00e1f30a 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs index 663c710a..47f6b245 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InsturctionPlugin.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Plugins.Models; -using BotSharp.Abstraction.Users.Enums; using Microsoft.Extensions.Configuration; namespace BotSharp.Core.Instructs; @@ -22,8 +21,8 @@ public class InsturctionPlugin : IBotSharpPlugin { SubMenu = new List { - new PluginMenuDef("Instruction", link: "page/instruction"), - new PluginMenuDef("Log", link: "page/instruction/log") { Roles = [UserRole.Root, UserRole.Admin] } + new PluginMenuDef("Testing", link: "page/instruction/testing"), + new PluginMenuDef("Log", link: "page/instruction/log") } }); diff --git a/src/Infrastructure/BotSharp.Core/Loggers/LoggerPlugin.cs b/src/Infrastructure/BotSharp.Core/Loggers/LoggerPlugin.cs new file mode 100644 index 00000000..bc99b9c8 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Loggers/LoggerPlugin.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.Configuration; + +namespace BotSharp.Core.Loggers; + +public class LoggerPlugin : IBotSharpPlugin +{ + public string Id => "ea1aade7-7e29-4f13-a78b-2b1835aa4fea"; + public string Name => "Logger"; + public string Description => "Provide log service"; + + public void RegisterDI(IServiceCollection services, IConfiguration config) + { + services.AddScoped(); + } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs new file mode 100644 index 00000000..fbd1d78a --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.cs @@ -0,0 +1,78 @@ +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 +{ + private readonly IServiceProvider _services; + private readonly IUserIdentity _user; + private readonly ILogger _logger; + + public LoggerService( + IServiceProvider services, + IUserIdentity user, + ILogger logger) + { + _services = services; + _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.Core/Repository/FileRepository/FileRepository.Log.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs index ea1f27b5..398fb03a 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs @@ -182,6 +182,10 @@ namespace BotSharp.Core.Repository { matched = matched && filter.TemplateNames.Contains(log.TemplateName); } + if (!filter.UserIds.IsNullOrEmpty()) + { + matched = matched && filter.UserIds.Contains(log.UserId); + } if (!matched) continue; @@ -190,12 +194,6 @@ namespace BotSharp.Core.Repository } var records = logs.OrderByDescending(x => x.CreatedTime).Skip(filter.Offset).Take(filter.Size); - var agentIds = records.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); - var agents = GetAgents(new AgentFilter - { - AgentIds = agentIds - }); - records = records.Select(x => { var states = x.InnerStates.ToDictionary(p => p.Key, p => @@ -203,7 +201,6 @@ namespace BotSharp.Core.Repository var data = p.Value.RootElement.GetProperty("data"); return data.ValueKind != JsonValueKind.Null ? data.ToString() : null; }); - x.AgentName = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; x.States = states ?? []; return x; }).ToList(); diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index 8655afb1..0afac090 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -39,6 +39,8 @@ global using BotSharp.Abstraction.SideCar.Attributes; global using BotSharp.Abstraction.Statistics.Models; global using BotSharp.Abstraction.Statistics.Enums; global using BotSharp.Abstraction.Statistics.Services; +global using BotSharp.Abstraction.Loggers.Services; +global using BotSharp.Abstraction.Infrastructures.Events; global using BotSharp.Core.Repository; global using BotSharp.Core.Routing; global using BotSharp.Core.Agents.Services; @@ -46,4 +48,4 @@ global using BotSharp.Core.Conversations.Services; global using BotSharp.Core.Infrastructures; global using BotSharp.Core.Users.Services; global using BotSharp.Core.Statistics.Services; -global using BotSharp.Abstraction.Infrastructures.Events; \ No newline at end of file +global using BotSharp.Core.Loggers.Services; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs index 2c46b5b4..de6748db 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs @@ -1,6 +1,6 @@ using BotSharp.Abstraction.Instructs.Models; using BotSharp.Abstraction.Loggers.Models; -using BotSharp.Abstraction.Repositories; +using BotSharp.Abstraction.Loggers.Services; using BotSharp.OpenAPI.ViewModels.Instructs; using Microsoft.AspNetCore.Hosting; @@ -11,10 +11,14 @@ namespace BotSharp.OpenAPI.Controllers; public class LoggerController : ControllerBase { private readonly IServiceProvider _services; + private readonly IUserIdentity _user; - public LoggerController(IServiceProvider services) + public LoggerController( + IServiceProvider services, + IUserIdentity user) { _services = services; + _user = user; } [HttpGet("/logger/full-log")] @@ -52,8 +56,9 @@ public class LoggerController : ControllerBase [HttpGet("/logger/instruction/log")] public async Task> GetInstructionLogs([FromQuery] InstructLogFilter request) { - var db = _services.GetRequiredService(); - var logs = db.GetInstructionLogs(request); + var logging = _services.GetRequiredService(); + var logs = await logging.GetInstructionLogs(request); + return new PagedItems { Items = logs.Items.Select(x => InstructionLogViewModel.From(x)), diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructionLogViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructionLogViewModel.cs index 88fc27e2..aca73f76 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructionLogViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructionLogViewModel.cs @@ -40,6 +40,10 @@ public class InstructionLogViewModel [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? UserId { get; set; } + [JsonPropertyName("user_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? UserName { get; set; } + [JsonPropertyName("states")] public Dictionary States { get; set; } = []; @@ -60,6 +64,7 @@ public class InstructionLogViewModel SystemInstruction = log.SystemInstruction, CompletionText = log.CompletionText, UserId = log.UserId, + UserName = log.UserName, States = log.States, CreatedTime = log.CreatedTime }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index 05d5a6a9..20cca2a7 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -178,16 +178,9 @@ public partial class MongoRepository var docs = _dc.InstructionLogs.Find(filterDef).Sort(sortDef).Skip(filter.Offset).Limit(filter.Size).ToList(); var count = _dc.InstructionLogs.CountDocuments(filterDef); - var agentIds = docs.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList(); - var agents = GetAgents(new AgentFilter - { - AgentIds = agentIds - }); - var logs = docs.Select(x => { var log = InstructionLogBetaDocument.ToDomainModel(x); - log.AgentName = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null; log.States = x.States.ToDictionary(p => p.Key, p => { var jsonStr = p.Value.ToJson();