Skip to content

Commit 7c53833

Browse files
authored
Merge pull request #856 from iceljc/features/refine-agent-filter
Features/refine agent filter
2 parents d17a642 + 169cb95 commit 7c53833

File tree

25 files changed

+405
-184
lines changed

25 files changed

+405
-184
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public enum AgentField
99
Disabled,
1010
Type,
1111
InheritAgentId,
12-
Profiles,
12+
Profile,
13+
Label,
1314
RoutingRule,
1415
Instruction,
1516
Function,
@@ -30,5 +31,5 @@ public enum AgentTaskField
3031
Description,
3132
Enabled,
3233
Content,
33-
DirectAgentId
34+
Status
3435
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public class Agent
8989
/// </summary>
9090
public List<string> Profiles { get; set; } = new();
9191

92+
/// <summary>
93+
/// Agent labels
94+
/// </summary>
95+
public List<string> Labels { get; set; } = new();
96+
9297
/// <summary>
9398
/// Merge utilities from entry agent
9499
/// </summary>
@@ -158,6 +163,7 @@ public static Agent Clone(Agent agent)
158163
MergeUtility = agent.MergeUtility,
159164
MaxMessageCount = agent.MaxMessageCount,
160165
Profiles = agent.Profiles,
166+
Labels = agent.Labels,
161167
RoutingRules = agent.RoutingRules,
162168
Rules = agent.Rules,
163169
LlmConfig = agent.LlmConfig,
@@ -269,6 +275,12 @@ public Agent SetProfiles(List<string> profiles)
269275
return this;
270276
}
271277

278+
public Agent SetLables(List<string> labels)
279+
{
280+
Labels = labels ?? [];
281+
return this;
282+
}
283+
272284
public Agent SetRoutingRules(List<RoutingRule> rules)
273285
{
274286
RoutingRules = rules ?? [];

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ namespace BotSharp.Abstraction.Repositories.Filters;
33
public class AgentFilter
44
{
55
public Pagination Pager { get; set; } = new Pagination();
6-
public string? AgentName { get; set; }
6+
public List<string>? AgentIds { get; set; }
7+
public List<string>? AgentNames { get; set; }
78
public string? SimilarName { get; set; }
89
public bool? Disabled { get; set; }
910
public bool? Installed { get; set; }
10-
public string? Type { get; set; }
11+
public List<string>? Types { get; set; }
12+
public List<string>? Labels { get; set; }
1113
public bool? IsPublic { get; set; }
12-
public List<string>? AgentIds { get; set; }
1314

1415
public static AgentFilter Empty()
1516
{

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

Lines changed: 176 additions & 85 deletions
Large diffs are not rendered by default.

src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using BotSharp.Abstraction.Agents.Models;
1718
using BotSharp.Abstraction.Repositories;
1819
using BotSharp.Abstraction.Repositories.Filters;
1920
using BotSharp.Abstraction.Tasks;
2021
using BotSharp.Abstraction.Tasks.Models;
2122
using BotSharp.Abstraction.Utilities;
2223
using BotSharp.Core.Infrastructures;
23-
using Microsoft.EntityFrameworkCore.Metadata.Internal;
2424
using Microsoft.Extensions.Logging;
2525
using System.Text.RegularExpressions;
2626

@@ -60,31 +60,30 @@ public async Task<List<CrontabItem>> GetCrontable()
6060

6161
public async Task<List<AgentTask>> GetTasks()
6262
{
63-
var agentService = _services.GetRequiredService<IAgentService>();
6463
var tasks = new List<AgentTask>();
64+
var agentService = _services.GetRequiredService<IAgentService>();
6565
var cronsources = _services.GetServices<ICrontabSource>();
66-
foreach (var source in cronsources)
67-
{
68-
var cron = source.GetCrontabItem();
6966

70-
// Get all agent subscribed to this cron
71-
72-
var agents = await agentService.GetAgents(new AgentFilter
67+
// Get all agent subscribed to this cron
68+
var agents = await agentService.GetAgents(new AgentFilter
69+
{
70+
Pager = new Pagination
7371
{
74-
Pager = new Pagination
75-
{
76-
Size = 1000
77-
}
78-
});
72+
Size = 1000
73+
}
74+
});
7975

76+
foreach (var source in cronsources)
77+
{
78+
var cron = source.GetCrontabItem();
8079
var preFilteredAgents = agents.Items.Where(x =>
8180
x.Rules.Exists(r => r.TriggerName == cron.Title)).ToList();
8281

8382
tasks.AddRange(preFilteredAgents.Select(x => new AgentTask
8483
{
8584
Id = Guid.Empty.ToString(),
8685
AgentId = x.Id,
87-
Agent = new BotSharp.Abstraction.Agents.Models.Agent
86+
Agent = new Agent
8887
{
8988
Name = x.Name,
9089
Description = x.Description

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
3131
record.MaxMessageCount = agent.MaxMessageCount;
3232
record.Type = agent.Type;
3333
record.Profiles = agent.Profiles ?? [];
34+
record.Labels = agent.Labels ?? [];
3435
record.RoutingRules = agent.RoutingRules ?? [];
3536
record.Instruction = agent.Instruction ?? string.Empty;
3637
record.ChannelInstructions = agent.ChannelInstructions ?? [];
@@ -96,6 +97,7 @@ public async Task<string> UpdateAgentFromFile(string id)
9697
.SetMergeUtility(foundAgent.MergeUtility)
9798
.SetAgentType(foundAgent.Type)
9899
.SetProfiles(foundAgent.Profiles)
100+
.SetLables(foundAgent.Labels)
99101
.SetRoutingRules(foundAgent.RoutingRules)
100102
.SetInstruction(foundAgent.Instruction)
101103
.SetChannelInstructions(foundAgent.ChannelInstructions)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
136136
public Dictionary<string, string> Load(string conversationId, bool isReadOnly = false)
137137
{
138138
_conversationId = !isReadOnly ? conversationId : null;
139+
Reset();
139140

140141
var routingCtx = _services.GetRequiredService<IRoutingContext>();
141142
var curMsgId = routingCtx.MessageId;

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

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ public void UpdateAgent(Agent agent, AgentField field)
3030
case AgentField.InheritAgentId:
3131
UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId);
3232
break;
33-
case AgentField.Profiles:
33+
case AgentField.Profile:
3434
UpdateAgentProfiles(agent.Id, agent.Profiles);
3535
break;
36+
case AgentField.Label:
37+
UpdateAgentLabels(agent.Id, agent.Labels);
38+
break;
3639
case AgentField.RoutingRule:
3740
UpdateAgentRoutingRules(agent.Id, agent.RoutingRules);
3841
break;
@@ -160,6 +163,20 @@ private void UpdateAgentProfiles(string agentId, List<string> profiles)
160163
File.WriteAllText(agentFile, json);
161164
}
162165

166+
public bool UpdateAgentLabels(string agentId, List<string> labels)
167+
{
168+
if (labels == null) return false;
169+
170+
var (agent, agentFile) = GetAgentFromFile(agentId);
171+
if (agent == null) return false;
172+
173+
agent.Labels = labels;
174+
agent.UpdatedDateTime = DateTime.UtcNow;
175+
var json = JsonSerializer.Serialize(agent, _options);
176+
File.WriteAllText(agentFile, json);
177+
return true;
178+
}
179+
163180
private void UpdateAgentUtilities(string agentId, bool mergeUtility, List<AgentUtility> utilities)
164181
{
165182
if (utilities == null) return;
@@ -341,6 +358,7 @@ private void UpdateAgentAllFields(Agent inputAgent)
341358
agent.MergeUtility = inputAgent.MergeUtility;
342359
agent.Type = inputAgent.Type;
343360
agent.Profiles = inputAgent.Profiles;
361+
agent.Labels = inputAgent.Labels;
344362
agent.Utilities = inputAgent.Utilities;
345363
agent.KnowledgeBases = inputAgent.KnowledgeBases;
346364
agent.RoutingRules = inputAgent.RoutingRules;
@@ -417,9 +435,14 @@ public List<Agent> GetAgents(AgentFilter filter)
417435
}
418436

419437
var query = Agents;
420-
if (!string.IsNullOrEmpty(filter.AgentName))
438+
if (filter.AgentIds != null)
439+
{
440+
query = query.Where(x => filter.AgentIds.Contains(x.Id));
441+
}
442+
443+
if (!filter.AgentNames.IsNullOrEmpty())
421444
{
422-
query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower());
445+
query = query.Where(x => filter.AgentNames.Contains(x.Name));
423446
}
424447

425448
if (!string.IsNullOrEmpty(filter.SimilarName))
@@ -433,20 +456,19 @@ public List<Agent> GetAgents(AgentFilter filter)
433456
query = query.Where(x => x.Disabled == filter.Disabled);
434457
}
435458

436-
if (filter.Type != null)
459+
if (!filter.Types.IsNullOrEmpty())
437460
{
438-
var types = filter.Type.Split(",");
439-
query = query.Where(x => types.Contains(x.Type));
461+
query = query.Where(x => filter.Types.Contains(x.Type));
440462
}
441463

442-
if (filter.IsPublic.HasValue)
464+
if (!filter.Labels.IsNullOrEmpty())
443465
{
444-
query = query.Where(x => x.IsPublic == filter.IsPublic);
466+
query = query.Where(x => x.Labels.Any(y => filter.Labels.Contains(y)));
445467
}
446468

447-
if (filter.AgentIds != null)
469+
if (filter.IsPublic.HasValue)
448470
{
449-
query = query.Where(x => filter.AgentIds.Contains(x.Id));
471+
query = query.Where(x => x.IsPublic == filter.IsPublic);
450472
}
451473

452474
return query.ToList();
@@ -515,6 +537,22 @@ public bool PatchAgentTemplate(string agentId, AgentTemplate template)
515537
return true;
516538
}
517539

540+
public bool AppendAgentLabels(string agentId, List<string> labels)
541+
{
542+
if (labels.IsNullOrEmpty()) return false;
543+
544+
var (agent, agentFile) = GetAgentFromFile(agentId);
545+
if (agent == null) return false;
546+
547+
var prevLabels = agent.Labels ?? [];
548+
var curLabels = prevLabels.Concat(labels).Distinct().ToList();
549+
agent.Labels = curLabels;
550+
agent.UpdatedDateTime = DateTime.UtcNow;
551+
var json = JsonSerializer.Serialize(agent, _options);
552+
File.WriteAllText(agentFile, json);
553+
return true;
554+
}
555+
518556
public void BulkInsertAgents(List<Agent> agents)
519557
{
520558
if (agents.IsNullOrEmpty()) return;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
5050
matched = matched && task.Enabled == filter.Enabled;
5151
}
5252

53+
if (!string.IsNullOrEmpty(filter?.Status))
54+
{
55+
matched = matched && task.Status == filter.Status;
56+
}
57+
5358
if (!matched) continue;
5459

5560
totalCount++;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public async Task<bool> Execute(RoleDialogModel message)
1818
var agentService = _services.GetRequiredService<IAgentService>();
1919
var agents = await agentService.GetAgents(new AgentFilter
2020
{
21-
AgentName = args.AgentName
21+
AgentNames = [args.AgentName]
2222
});
2323
var targetAgent = agents.Items.FirstOrDefault();
2424
if (targetAgent == null)

0 commit comments

Comments
 (0)