Skip to content

Commit bc5818f

Browse files
authored
Merge pull request #221 from iceljc/features/add-conversation-agent-filter
add filters for conversation and agent
2 parents 8c1b0bc + 801b491 commit bc5818f

File tree

12 files changed

+107
-53
lines changed

12 files changed

+107
-53
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BotSharp.Abstraction.Repositories.Filters;
2+
3+
public class AgentFilter
4+
{
5+
public string? AgentName { get; set; }
6+
public bool? Disabled { get; set; }
7+
public bool? AllowRouting { get; set; }
8+
public bool? IsPublic { get; set; }
9+
public List<string>? AgentIds { get; set; }
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Repositories.Filters;
2+
3+
public class ConversationFilter
4+
{
5+
public string? AgentId { get; set; }
6+
public string? Status { get; set; }
7+
public string? Channel { get; set; }
8+
public string? UserId { get; set; }
9+
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Filters;
12
using BotSharp.Abstraction.Users.Models;
23

34
namespace BotSharp.Abstraction.Repositories;
@@ -16,8 +17,7 @@ public interface IBotSharpRepository
1617
#region Agent
1718
void UpdateAgent(Agent agent, AgentField field);
1819
Agent? GetAgent(string agentId);
19-
List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRouting = null,
20-
bool? isPublic = null, List<string>? agentIds = null);
20+
List<Agent> GetAgents(AgentFilter filter);
2121
List<Agent> GetAgentsByUser(string userId);
2222
void BulkInsertAgents(List<Agent> agents);
2323
void BulkInsertUserAgents(List<UserAgent> userAgents);
@@ -35,7 +35,7 @@ List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRou
3535
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
3636
void UpdateConversationStatus(string conversationId, string status);
3737
Conversation GetConversation(string conversationId);
38-
List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null);
38+
List<Conversation> GetConversations(ConversationFilter filter);
3939
void UpdateConversationTitle(string conversationId, string title);
4040
List<Conversation> GetLastConversations();
4141
void AddExectionLogs(string conversationId, List<string> logs);

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Agents.Models;
2+
using BotSharp.Abstraction.Repositories.Filters;
23

34
namespace BotSharp.Core.Agents.Services;
45

@@ -9,7 +10,8 @@ public partial class AgentService
910
#endif
1011
public async Task<List<Agent>> GetAgents(bool? allowRouting = null)
1112
{
12-
var agents = _db.GetAgents(allowRouting: allowRouting);
13+
var filter = new AgentFilter { AllowRouting = allowRouting };
14+
var agents = _db.GetAgents(filter);
1315
return await Task.FromResult(agents);
1416
}
1517

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories;
2+
using BotSharp.Abstraction.Repositories.Filters;
23
using BotSharp.Abstraction.Users.Enums;
34

45
namespace BotSharp.Core.Conversations.Services;
@@ -57,8 +58,11 @@ public async Task<List<Conversation>> GetConversations()
5758
{
5859
var db = _services.GetRequiredService<IBotSharpRepository>();
5960
var user = db.GetUserById(_user.Id);
60-
var targetUserId = user.Role == UserRole.CSR ? null : user?.Id;
61-
var conversations = db.GetConversations(userId: targetUserId);
61+
var filter = new ConversationFilter
62+
{
63+
UserId = user.Role == UserRole.CSR ? string.Empty : user?.Id
64+
};
65+
var conversations = db.GetConversations(filter);
6266
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
6367
}
6468

src/Infrastructure/BotSharp.Core/Planning/HFPlanner.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.Functions.Models;
33
using BotSharp.Abstraction.Planning;
44
using BotSharp.Abstraction.Repositories;
5+
using BotSharp.Abstraction.Repositories.Filters;
56
using BotSharp.Abstraction.Routing.Models;
67
using BotSharp.Abstraction.Routing.Settings;
78
using BotSharp.Abstraction.Templating;
@@ -72,7 +73,8 @@ public async Task<bool> AgentExecuting(FunctionCallFromLlm inst, RoleDialogModel
7273
if (!string.IsNullOrEmpty(inst.AgentName))
7374
{
7475
var db = _services.GetRequiredService<IBotSharpRepository>();
75-
var agent = db.GetAgents(inst.AgentName).FirstOrDefault();
76+
var filter = new AgentFilter { AgentName = inst.AgentName };
77+
var agent = db.GetAgents(filter).FirstOrDefault();
7678

7779
var context = _services.GetRequiredService<RoutingContext>();
7880
context.Push(agent.Id);

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Repositories;
3+
using BotSharp.Abstraction.Repositories.Filters;
34
using BotSharp.Abstraction.Users.Models;
45
using Microsoft.EntityFrameworkCore.Infrastructure;
56

@@ -72,8 +73,7 @@ public Agent GetAgent(string agentId)
7273
throw new NotImplementedException();
7374
}
7475

75-
public List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRouting = null,
76-
bool? isPublic = null, List<string>? agentIds = null)
76+
public List<Agent> GetAgents(AgentFilter filter)
7777
{
7878
throw new NotImplementedException();
7979
}
@@ -131,7 +131,7 @@ public Conversation GetConversation(string conversationId)
131131
throw new NotImplementedException();
132132
}
133133

134-
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
134+
public List<Conversation> GetConversations(ConversationFilter filter)
135135
{
136136
throw new NotImplementedException();
137137
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using BotSharp.Abstraction.Agents.Models;
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
8+
using BotSharp.Abstraction.Repositories.Filters;
9+
using BotSharp.Abstraction.Utilities;
10+
811
namespace BotSharp.Core.Repository;
912

1013
public class FileRepository : IBotSharpRepository
@@ -500,33 +503,32 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
500503
return null;
501504
}
502505

503-
public List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRouting = null,
504-
bool? isPublic = null, List<string>? agentIds = null)
506+
public List<Agent> GetAgents(AgentFilter filter)
505507
{
506508
var query = Agents;
507-
if (!string.IsNullOrEmpty(name))
509+
if (!string.IsNullOrEmpty(filter.AgentName))
508510
{
509-
query = query.Where(x => x.Name.ToLower() == name.ToLower());
511+
query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower());
510512
}
511513

512-
if (disabled.HasValue)
514+
if (filter.Disabled.HasValue)
513515
{
514-
query = query.Where(x => x.Disabled == disabled);
516+
query = query.Where(x => x.Disabled == filter.Disabled);
515517
}
516518

517-
if (allowRouting.HasValue)
519+
if (filter.AllowRouting.HasValue)
518520
{
519-
query = query.Where(x => x.AllowRouting == allowRouting);
521+
query = query.Where(x => x.AllowRouting == filter.AllowRouting);
520522
}
521523

522-
if (isPublic.HasValue)
524+
if (filter.IsPublic.HasValue)
523525
{
524-
query = query.Where(x => x.IsPublic == isPublic);
526+
query = query.Where(x => x.IsPublic == filter.IsPublic);
525527
}
526528

527-
if (agentIds != null)
529+
if (filter.AgentIds != null)
528530
{
529-
query = query.Where(x => agentIds.Contains(x.Id));
531+
query = query.Where(x => filter.AgentIds.Contains(x.Id));
530532
}
531533

532534
return query.ToList();
@@ -539,7 +541,12 @@ join u in Users on ua.UserId equals u.Id
539541
where ua.UserId == userId || u.ExternalId == userId
540542
select ua.AgentId).ToList();
541543

542-
var agents = GetAgents(isPublic: true, agentIds: agentIds);
544+
var filter = new AgentFilter
545+
{
546+
IsPublic = true,
547+
AgentIds = agentIds
548+
};
549+
var agents = GetAgents(filter);
543550
return agents;
544551
}
545552

@@ -734,7 +741,7 @@ public Conversation GetConversation(string conversationId)
734741
return record;
735742
}
736743

737-
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
744+
public List<Conversation> GetConversations(ConversationFilter filter)
738745
{
739746
var records = new List<Conversation>();
740747
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
@@ -749,10 +756,10 @@ public List<Conversation> GetConversations(string? agentId = null, string? statu
749756
if (record == null) continue;
750757

751758
var matched = true;
752-
if (!string.IsNullOrEmpty(agentId)) matched = matched && record.AgentId == agentId;
753-
if (!string.IsNullOrEmpty(status)) matched = matched && record.Status == status;
754-
if (!string.IsNullOrEmpty(channel)) matched = matched && record.Channel == channel;
755-
if (!string.IsNullOrEmpty(userId)) matched = matched && record.UserId == userId;
759+
if (!string.IsNullOrEmpty(filter.AgentId)) matched = matched && record.AgentId == filter.AgentId;
760+
if (!string.IsNullOrEmpty(filter.Status)) matched = matched && record.Status == filter.Status;
761+
if (!string.IsNullOrEmpty(filter.Channel)) matched = matched && record.Channel == filter.Channel;
762+
if (!string.IsNullOrEmpty(filter.UserId)) matched = matched && record.UserId == filter.UserId;
756763

757764
if (!matched) continue;
758765
records.Add(record);

src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Functions;
22
using BotSharp.Abstraction.Repositories;
3+
using BotSharp.Abstraction.Repositories.Filters;
34
using BotSharp.Abstraction.Routing;
45
using BotSharp.Abstraction.Routing.Models;
56
using System.Drawing;
@@ -29,7 +30,8 @@ public async Task<bool> Execute(RoleDialogModel message)
2930
if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32)
3031
{
3132
var db = _services.GetRequiredService<IBotSharpRepository>();
32-
var originalAgent = db.GetAgents(name: args.OriginalAgent).FirstOrDefault();
33+
var filter = new AgentFilter { AgentName = args.OriginalAgent };
34+
var originalAgent = db.GetAgents(filter).FirstOrDefault();
3335
if (originalAgent != null)
3436
{
3537
_context.Push(originalAgent.Id);
@@ -48,7 +50,8 @@ public async Task<bool> Execute(RoleDialogModel message)
4850
else
4951
{
5052
var db = _services.GetRequiredService<IBotSharpRepository>();
51-
var targetAgent = db.GetAgents(args.AgentName).FirstOrDefault();
53+
var filter = new AgentFilter { AgentName = args.AgentName };
54+
var targetAgent = db.GetAgents(filter).FirstOrDefault();
5255
if (targetAgent == null)
5356
{
5457
message.Data = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);

src/Infrastructure/BotSharp.Core/Routing/Handlers/ContinueExecuteTaskRoutingHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Functions.Models;
22
using BotSharp.Abstraction.Models;
33
using BotSharp.Abstraction.Repositories;
4+
using BotSharp.Abstraction.Repositories.Filters;
45
using BotSharp.Abstraction.Routing;
56
using BotSharp.Abstraction.Routing.Settings;
67
using BotSharp.Core.Planning;
@@ -37,7 +38,8 @@ public ContinueExecuteTaskRoutingHandler(IServiceProvider services, ILogger<Cont
3738
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
3839
{
3940
var db = _services.GetRequiredService<IBotSharpRepository>();
40-
var record = db.GetAgents(inst.AgentName).FirstOrDefault();
41+
var filter = new AgentFilter { AgentName = inst.AgentName };
42+
var record = db.GetAgents(filter).FirstOrDefault();
4143

4244
message.FunctionName = inst.Function;
4345
message.CurrentAgentId = record.Id;

0 commit comments

Comments
 (0)