Merge pull request #1 from jackjiang-sms/feature/AII-400

AII-400
This commit is contained in:
jackjiang-sms 2024-12-12 16:34:35 +08:00 committed by GitHub
commit 5023ee9245
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 30 additions and 4 deletions

View file

@ -56,4 +56,9 @@ public abstract class AgentHookBase : IAgentHook
public virtual void OnAgentUtilityLoaded(Agent agent)
{
}
public virtual void OnAgentLoadFilter(Agent agent)
{
}
}

View file

@ -33,4 +33,6 @@ public interface IAgentHook
/// <param name="agent"></param>
/// <returns></returns>
void OnAgentLoaded(Agent agent);
void OnAgentLoadFilter(Agent agent);
}

View file

@ -5,9 +5,8 @@ namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public static ConcurrentDictionary<string, Dictionary<string,string>> AgentParameterTypes = new();
public static ConcurrentDictionary<string, Dictionary<string, string>> AgentParameterTypes = new();
[MemoryCache(10 * 60, perInstanceCache: true)]
public async Task<Agent> LoadAgent(string id)
{
if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString())
@ -15,6 +14,26 @@ public partial class AgentService
return null;
}
Agent agent = await GetLoadAgent(id);
OnAgentLoadFilter(agent);
return agent;
}
private void OnAgentLoadFilter(Agent? agent)
{
if (agent != null && agent.Type == AgentType.Routing)
{
var hooks = _services.GetServices<IAgentHook>();
foreach (var hook in hooks)
{
hook.OnAgentLoadFilter(agent);
}
}
}
[MemoryCache(10 * 60, perInstanceCache: true)]
private async Task<Agent> GetLoadAgent(string id)
{
var hooks = _services.GetServices<IAgentHook>();
// Before agent is loaded.
@ -106,14 +125,14 @@ public partial class AgentService
{
var agentId = agent.Id ?? agent.Name;
if (AgentParameterTypes.ContainsKey(agentId)) return;
AddOrUpdateRoutesParameters(agentId, agent.RoutingRules);
AddOrUpdateFunctionsParameters(agentId, agent.Functions);
}
private void AddOrUpdateRoutesParameters(string agentId, List<RoutingRule> routingRules)
{
if(!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
{
parameterTypes = new();
}