65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
using BotSharp.Abstraction.Agents.Models;
|
|
using BotSharp.Abstraction.Repositories;
|
|
using BotSharp.Abstraction.Routing.Models;
|
|
using BotSharp.Abstraction.Routing.Settings;
|
|
|
|
namespace BotSharp.Core.Routing;
|
|
|
|
public class Router : IAgentRouting
|
|
{
|
|
protected readonly IServiceProvider _services;
|
|
protected readonly ILogger _logger;
|
|
protected readonly RoutingSettings _settings;
|
|
|
|
public virtual string AgentId => _settings.RouterId;
|
|
|
|
public Router(IServiceProvider services,
|
|
ILogger<Router> logger,
|
|
RoutingSettings settings)
|
|
{
|
|
_services = services;
|
|
_logger = logger;
|
|
_settings = settings;
|
|
}
|
|
|
|
public virtual async Task<Agent> LoadRouter()
|
|
{
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
return await agentService.LoadAgent(AgentId);
|
|
}
|
|
|
|
#if !DEBUG
|
|
[MemoryCache(10 * 60)]
|
|
#endif
|
|
public RoutingItem[] GetRoutingRecords()
|
|
{
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
var records = db.RoutingItems.ToArray();
|
|
var profiles = db.RoutingProfiles.ToList();
|
|
|
|
if (!profiles.IsNullOrEmpty())
|
|
{
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
var name = state.GetState("channel");
|
|
var specifiedProfile = profiles.FirstOrDefault(x => x.Name == name);
|
|
if (specifiedProfile != null)
|
|
{
|
|
records = records.Where(x => specifiedProfile.AgentIds.Contains(x.AgentId)).ToArray();
|
|
}
|
|
}
|
|
|
|
return records;
|
|
}
|
|
|
|
public RoutingItem GetRecordByName(string name)
|
|
{
|
|
return GetRoutingRecords().FirstOrDefault(x => x.Name.ToLower() == name.ToLower());
|
|
}
|
|
|
|
public RoutingItem GetRecordByAgentId(string id)
|
|
{
|
|
return GetRoutingRecords().FirstOrDefault(x => x.AgentId == id);
|
|
}
|
|
}
|