This commit is contained in:
Jicheng Lu 2025-03-06 11:31:53 -06:00
parent ae84088b5f
commit c4d2f5b026
6 changed files with 78 additions and 70 deletions

View file

@ -27,8 +27,6 @@ public interface IConversationService
/// <param name="newMessageId">If not null, delete messages while input a new message; otherwise delete messages only</param>
/// <returns></returns>
Task<bool> TruncateConversation(string conversationId, string messageId, string? newMessageId = null);
Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId);
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);
/// <summary>
/// Send message to LLM

View file

@ -5,5 +5,12 @@ namespace BotSharp.Abstraction.Loggers.Services;
public interface ILoggerService
{
#region Conversation
Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId);
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);
#endregion
#region Instruction
Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter);
#endregion
}

View file

@ -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<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId)
{

View file

@ -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<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
{
if (filter == null)
{
filter = InstructLogFilter.Empty();
}
var userService = _services.GetRequiredService<IUserService>();
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<Agent>();
var users = new List<User>();
var db = _services.GetRequiredService<IBotSharpRepository>();
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<InstructionLogModel>
{
Items = items,
Count = logs.Count
};
}
}

View file

@ -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<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
{
if (filter == null)
{
filter = InstructLogFilter.Empty();
}
var userService = _services.GetRequiredService<IUserService>();
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<Agent>();
var users = new List<User>();
var db = _services.GetRequiredService<IBotSharpRepository>();
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<InstructionLogModel>
{
Items = items,
Count = logs.Count
};
}
}

View file

@ -42,15 +42,15 @@ public class LoggerController : ControllerBase
[HttpGet("/logger/conversation/{conversationId}/content-log")]
public async Task<List<ContentLogOutputModel>> GetConversationContentLogs([FromRoute] string conversationId)
{
var conversationService = _services.GetRequiredService<IConversationService>();
return await conversationService.GetConversationContentLogs(conversationId);
var logging = _services.GetRequiredService<ILoggerService>();
return await logging.GetConversationContentLogs(conversationId);
}
[HttpGet("/logger/conversation/{conversationId}/state-log")]
public async Task<List<ConversationStateLogModel>> GetConversationStateLogs([FromRoute] string conversationId)
{
var conversationService = _services.GetRequiredService<IConversationService>();
return await conversationService.GetConversationStateLogs(conversationId);
var logging = _services.GetRequiredService<ILoggerService>();
return await logging.GetConversationStateLogs(conversationId);
}
[HttpGet("/logger/instruction/log")]