Skip to content

Commit 1286f35

Browse files
committed
Add AgentFilter.
1 parent 27e0c4b commit 1286f35

File tree

16 files changed

+47
-8
lines changed

16 files changed

+47
-8
lines changed
17.9 KB
Loading

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Repositories.Filters;
2+
13
namespace BotSharp.Abstraction.Agents;
24

35
/// <summary>
@@ -7,7 +9,7 @@ public interface IAgentService
79
{
810
Task<Agent> CreateAgent(Agent agent);
911
Task RefreshAgents();
10-
Task<List<Agent>> GetAgents(bool? allowRouting = null);
12+
Task<List<Agent>> GetAgents(AgentFilter filter);
1113

1214
/// <summary>
1315
/// Load agent configurations and trigger hooks

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Agent
6262
public bool AllowRouting { get; set; }
6363

6464
public bool Disabled { get; set; }
65+
public string IconUrl { get; set; }
6566

6667
/// <summary>
6768
/// Profile by channel

src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public class AgentFilter
66
public bool? Disabled { get; set; }
77
public bool? AllowRouting { get; set; }
88
public bool? IsPublic { get; set; }
9+
public bool? IsRouter { get; set; }
910
public List<string>? AgentIds { get; set; }
1011
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public partial class AgentService
88
#if !DEBUG
99
[MemoryCache(10 * 60)]
1010
#endif
11-
public async Task<List<Agent>> GetAgents(bool? allowRouting = null)
11+
public async Task<List<Agent>> GetAgents(AgentFilter filter)
1212
{
13-
var filter = new AgentFilter { AllowRouting = allowRouting };
1413
var agents = _db.GetAgents(filter);
1514
return await Task.FromResult(agents);
1615
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Functions.Models;
33
using BotSharp.Abstraction.Planning;
4+
using BotSharp.Abstraction.Repositories.Filters;
45
using BotSharp.Abstraction.Routing.Models;
56
using BotSharp.Abstraction.Routing.Settings;
67
using BotSharp.Abstraction.Templating;
@@ -108,7 +109,10 @@ private string GetNextStepPrompt(Agent router)
108109
private void FixMalformedResponse(FunctionCallFromLlm args)
109110
{
110111
var agentService = _services.GetRequiredService<IAgentService>();
111-
var agents = agentService.GetAgents(allowRouting: true).Result;
112+
var agents = agentService.GetAgents(new AgentFilter
113+
{
114+
AllowRouting = true
115+
}).Result;
112116
var malformed = false;
113117

114118
// Sometimes it populate malformed Function in Agent name

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
88
using BotSharp.Abstraction.Repositories.Filters;
9-
using BotSharp.Abstraction.Utilities;
10-
using BotSharp.Abstraction.Conversations.Models;
119
using BotSharp.Abstraction.Repositories.Models;
10+
using BotSharp.Abstraction.Routing.Settings;
1211

1312
namespace BotSharp.Core.Repository;
1413

1514
public class FileRepository : IBotSharpRepository
1615
{
16+
private readonly IServiceProvider _services;
1717
private readonly BotSharpDatabaseSettings _dbSettings;
1818
private readonly AgentSettings _agentSettings;
1919
private readonly ConversationSetting _conversationSettings;
2020
private JsonSerializerOptions _options;
2121

2222
public FileRepository(
23+
IServiceProvider services,
2324
BotSharpDatabaseSettings dbSettings,
2425
AgentSettings agentSettings,
2526
ConversationSetting conversationSettings)
2627
{
28+
_services = services;
2729
_dbSettings = dbSettings;
2830
_agentSettings = agentSettings;
2931
_conversationSettings = conversationSettings;
@@ -542,6 +544,14 @@ public List<Agent> GetAgents(AgentFilter filter)
542544
query = query.Where(x => x.IsPublic == filter.IsPublic);
543545
}
544546

547+
if (filter.IsRouter.HasValue)
548+
{
549+
var route = _services.GetRequiredService<RoutingSettings>();
550+
query = filter.IsRouter.Value ?
551+
query.Where(x => x.Id == route.AgentId) :
552+
query.Where(x => x.Id != route.AgentId);
553+
}
554+
545555
if (filter.AgentIds != null)
546556
{
547557
query = query.Where(x => filter.AgentIds.Contains(x.Id));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public async Task<AgentViewModel> GetAgent([FromRoute] string id)
2121
}
2222

2323
[HttpGet("/agents")]
24-
public async Task<List<AgentViewModel>> GetAgents()
24+
public async Task<List<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter)
2525
{
26-
var agents = await _agentService.GetAgents();
26+
var agents = await _agentService.GetAgents(filter);
2727
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
2828
}
2929

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class AgentViewModel
2121
[JsonPropertyName("allow_routing")]
2222
public bool AllowRouting { get; set; }
2323
public bool Disabled { get; set; }
24+
[JsonPropertyName("icon_url")]
25+
public string IconUrl { get; set; }
2426
public List<string> Profiles { get; set; }
2527

2628
[JsonPropertyName("routing_rules")]
@@ -51,6 +53,7 @@ public static AgentViewModel FromAgent(Agent agent)
5153
Samples = agent.Samples,
5254
IsPublic= agent.IsPublic,
5355
Disabled = agent.Disabled,
56+
IconUrl = agent.IconUrl,
5457
AllowRouting = agent.AllowRouting,
5558
Profiles = agent.Profiles,
5659
RoutingRules = agent.RoutingRules,

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using BotSharp.Abstraction.Repositories.Filters;
55
using BotSharp.Abstraction.Repositories.Models;
66
using BotSharp.Abstraction.Routing.Models;
7+
using BotSharp.Abstraction.Routing.Settings;
78
using BotSharp.Abstraction.Users.Models;
89
using BotSharp.Plugin.MongoStorage.Collections;
910
using BotSharp.Plugin.MongoStorage.Models;
@@ -461,6 +462,14 @@ public List<Agent> GetAgents(AgentFilter filter)
461462
query = query.Where(x => x.IsPublic == filter.IsPublic);
462463
}
463464

465+
if (filter.IsRouter.HasValue)
466+
{
467+
var route = _services.GetRequiredService<RoutingSettings>();
468+
query = filter.IsRouter.Value ?
469+
query.Where(x => x.Id == route.AgentId) :
470+
query.Where(x => x.Id != route.AgentId);
471+
}
472+
464473
if (filter.AgentIds != null)
465474
{
466475
query = query.Where(x => filter.AgentIds.Contains(x.Id));

0 commit comments

Comments
 (0)