Skip to content

Commit c4d2f5b

Browse files
author
Jicheng Lu
committed
relocate
1 parent ae84088 commit c4d2f5b

6 files changed

Lines changed: 78 additions & 70 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public interface IConversationService
2727
/// <param name="newMessageId">If not null, delete messages while input a new message; otherwise delete messages only</param>
2828
/// <returns></returns>
2929
Task<bool> TruncateConversation(string conversationId, string messageId, string? newMessageId = null);
30-
Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId);
31-
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);
3230

3331
/// <summary>
3432
/// Send message to LLM

src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ namespace BotSharp.Abstraction.Loggers.Services;
55

66
public interface ILoggerService
77
{
8+
#region Conversation
9+
Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId);
10+
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);
11+
#endregion
12+
13+
#region Instruction
814
Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter);
15+
#endregion
916
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Log.cs renamed to src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using BotSharp.Abstraction.Loggers.Models;
2-
using BotSharp.Abstraction.Repositories;
32

4-
namespace BotSharp.Core.Conversations.Services;
3+
namespace BotSharp.Core.Loggers.Services;
54

6-
public partial class ConversationService
5+
public partial class LoggerService
76
{
87
public async Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId)
98
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using BotSharp.Abstraction.Instructs.Models;
2+
using BotSharp.Abstraction.Loggers.Models;
3+
using BotSharp.Abstraction.Users.Enums;
4+
using BotSharp.Abstraction.Users.Models;
5+
6+
namespace BotSharp.Core.Loggers.Services;
7+
8+
public partial class LoggerService
9+
{
10+
public async Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
11+
{
12+
if (filter == null)
13+
{
14+
filter = InstructLogFilter.Empty();
15+
}
16+
17+
var userService = _services.GetRequiredService<IUserService>();
18+
var user = await userService.GetUser(_user.Id);
19+
var isAdmin = UserConstant.AdminRoles.Contains(user?.Role);
20+
if (!isAdmin && user?.Id == null) return new();
21+
22+
filter.UserIds = isAdmin ? [] : user?.Id != null ? [user.Id] : [];
23+
24+
var agents = new List<Agent>();
25+
var users = new List<User>();
26+
27+
var db = _services.GetRequiredService<IBotSharpRepository>();
28+
var logs = db.GetInstructionLogs(filter);
29+
var agentIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList();
30+
var userIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.UserId)).Select(x => x.UserId).ToList();
31+
agents = db.GetAgents(new AgentFilter
32+
{
33+
AgentIds = agentIds,
34+
Pager = new Pagination { Size = filter.Size }
35+
});
36+
37+
if (isAdmin)
38+
{
39+
users = db.GetUserByIds(userIds);
40+
}
41+
42+
var items = logs.Items.Select(x =>
43+
{
44+
x.AgentId = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null;
45+
46+
if (!isAdmin)
47+
{
48+
x.UserName = user != null ? $"{user.FirstName} {user.LastName}" : null;
49+
}
50+
else
51+
{
52+
var found = !string.IsNullOrEmpty(x.UserId) ? users.FirstOrDefault(u => u.Id == x.UserId) : null;
53+
x.UserName = found != null ? $"{found.FirstName} {found.LastName}" : null;
54+
}
55+
return x;
56+
}).ToList();
57+
58+
return new PagedItems<InstructionLogModel>
59+
{
60+
Items = items,
61+
Count = logs.Count
62+
};
63+
}
64+
}
Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
using BotSharp.Abstraction.Instructs.Models;
2-
using BotSharp.Abstraction.Loggers.Models;
3-
using BotSharp.Abstraction.Users.Enums;
4-
using BotSharp.Abstraction.Users.Models;
5-
61
namespace BotSharp.Core.Loggers.Services;
72

8-
public class LoggerService : ILoggerService
3+
public partial class LoggerService : ILoggerService
94
{
105
private readonly IServiceProvider _services;
116
private readonly IUserIdentity _user;
@@ -20,59 +15,4 @@ public LoggerService(
2015
_user = user;
2116
_logger = logger;
2217
}
23-
24-
public async Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
25-
{
26-
if (filter == null)
27-
{
28-
filter = InstructLogFilter.Empty();
29-
}
30-
31-
var userService = _services.GetRequiredService<IUserService>();
32-
var user = await userService.GetUser(_user.Id);
33-
var isAdmin = UserConstant.AdminRoles.Contains(user?.Role);
34-
if (!isAdmin && user?.Id == null) return new();
35-
36-
filter.UserIds = isAdmin ? [] : user?.Id != null ? [user.Id] : [];
37-
38-
var agents = new List<Agent>();
39-
var users = new List<User>();
40-
41-
var db = _services.GetRequiredService<IBotSharpRepository>();
42-
var logs = db.GetInstructionLogs(filter);
43-
var agentIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.AgentId)).Select(x => x.AgentId).ToList();
44-
var userIds = logs.Items.Where(x => !string.IsNullOrEmpty(x.UserId)).Select(x => x.UserId).ToList();
45-
agents = db.GetAgents(new AgentFilter
46-
{
47-
AgentIds = agentIds,
48-
Pager = new Pagination { Size = filter.Size }
49-
});
50-
51-
if (isAdmin)
52-
{
53-
users = db.GetUserByIds(userIds);
54-
}
55-
56-
var items = logs.Items.Select(x =>
57-
{
58-
x.AgentId = !string.IsNullOrEmpty(x.AgentId) ? agents.FirstOrDefault(a => a.Id == x.AgentId)?.Name : null;
59-
60-
if (!isAdmin)
61-
{
62-
x.UserName = user != null ? $"{user.FirstName} {user.LastName}" : null;
63-
}
64-
else
65-
{
66-
var found = !string.IsNullOrEmpty(x.UserId) ? users.FirstOrDefault(u => u.Id == x.UserId) : null;
67-
x.UserName = found != null ? $"{found.FirstName} {found.LastName}" : null;
68-
}
69-
return x;
70-
}).ToList();
71-
72-
return new PagedItems<InstructionLogModel>
73-
{
74-
Items = items,
75-
Count = logs.Count
76-
};
77-
}
7818
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public async Task<IActionResult> GetFullLog()
4242
[HttpGet("/logger/conversation/{conversationId}/content-log")]
4343
public async Task<List<ContentLogOutputModel>> GetConversationContentLogs([FromRoute] string conversationId)
4444
{
45-
var conversationService = _services.GetRequiredService<IConversationService>();
46-
return await conversationService.GetConversationContentLogs(conversationId);
45+
var logging = _services.GetRequiredService<ILoggerService>();
46+
return await logging.GetConversationContentLogs(conversationId);
4747
}
4848

4949
[HttpGet("/logger/conversation/{conversationId}/state-log")]
5050
public async Task<List<ConversationStateLogModel>> GetConversationStateLogs([FromRoute] string conversationId)
5151
{
52-
var conversationService = _services.GetRequiredService<IConversationService>();
53-
return await conversationService.GetConversationStateLogs(conversationId);
52+
var logging = _services.GetRequiredService<ILoggerService>();
53+
return await logging.GetConversationStateLogs(conversationId);
5454
}
5555

5656
[HttpGet("/logger/instruction/log")]

0 commit comments

Comments
 (0)