Skip to content

Commit a9a70ae

Browse files
authored
Merge pull request SciSharp#1302 from yileicn/master
Add OnRoutingRulesLoaded hook to allow rewriting routing rules
2 parents 705a4dd + b7081b5 commit a9a70ae

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Functions.Models;
22
using BotSharp.Abstraction.Hooks;
3+
using BotSharp.Abstraction.Routing.Models;
34

45
namespace BotSharp.Abstraction.Routing;
56

@@ -28,4 +29,13 @@ Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null
2829

2930
Task OnAgentQueueEmptied(string agentId, string? reason = null)
3031
=> Task.CompletedTask;
32+
33+
/// <summary>
34+
/// Called when routing rules are loaded for an agent (GetRulesByAgentName / GetRulesByAgentId).
35+
/// Hooks can modify rules in place to rewrite or filter rules before they are returned.
36+
/// </summary>
37+
/// <param name="agentId">Agent id the rules belong to.</param>
38+
/// <param name="rules">Mutable list of routing rules.</param>
39+
Task OnRoutingRulesLoaded(string agentId, IList<RoutingRule> rules)
40+
=> Task.CompletedTask;
3141
}

src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public override string ToString()
3737

3838
public RoutingRule()
3939
{
40-
4140
}
41+
42+
/// <summary>
43+
/// Returns a defensive copy so hook mutations do not affect cached/shared instances.
44+
/// </summary>
45+
public RoutingRule Clone() => new RoutingRule
46+
{
47+
AgentId = AgentId,
48+
AgentName = AgentName,
49+
Type = Type,
50+
Field = Field,
51+
Description = Description,
52+
FieldType = FieldType,
53+
Required = Required,
54+
RedirectTo = RedirectTo,
55+
RedirectToAgentName = RedirectToAgentName
56+
};
4257
}

src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingAgentHook.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public override async Task<bool> OnFunctionsLoaded(List<FunctionDef> functions)
6868
{
6969
// check if enabled the routing rule
7070
var routing = _services.GetRequiredService<IRoutingService>();
71-
var rules = await routing.GetRulesByAgentId(_agent.Id);
71+
var rules = (await routing.GetRulesByAgentId(_agent.Id)).ToList();
72+
if (!rules.IsNullOrEmpty())
73+
{
74+
await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRoutingRulesLoaded(_agent.Id, rules), _agent.Id);
75+
}
7276
var rule = rules.FirstOrDefault(x => x.Type == RuleType.Fallback);
7377
if (rule != null)
7478
{

src/Infrastructure/BotSharp.Core/Routing/RoutingService.HasMissingRequiredField.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public partial class RoutingService
1616
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
1717
var routing = _services.GetRequiredService<IRoutingService>();
1818

19-
var routingRules = await routing.GetRulesByAgentName(args.AgentName);
20-
21-
if (routingRules == null || !routingRules.Any())
19+
var routingRules = (await routing.GetRulesByAgentName(args.AgentName)).ToList();
20+
if (routingRules.IsNullOrEmpty())
2221
{
2322
agentId = message.CurrentAgentId;
2423
return (false, reason, agentId);
2524
}
25+
await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRoutingRulesLoaded(routingRules[0].AgentId, routingRules), routingRules[0].AgentId);
2626

2727
agentId = routingRules.First().AgentId;
2828
// Add routed agent

src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public async Task<RoutingRule[]> GetRulesByAgentName(string name)
149149
var records = await GetRoutingRecords();
150150
return records
151151
.Where(x => x.AgentName.ToLower() == name.ToLower())
152+
.Select(r => r.Clone())
152153
.ToArray();
153154
}
154155

@@ -157,6 +158,7 @@ public async Task<RoutingRule[]> GetRulesByAgentId(string id)
157158
var records = await GetRoutingRecords();
158159
return records
159160
.Where(x => x.AgentId == id)
161+
.Select(r => r.Clone())
160162
.ToArray();
161163
}
162164
}

0 commit comments

Comments
 (0)