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

59 lines
2 KiB
C#
Raw Normal View History

2023-08-28 03:50:10 +00:00
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
using System.IO;
namespace BotSharp.Core.Routing;
public class Router : IAgentRouting
{
protected readonly IServiceProvider _services;
protected readonly ILogger _logger;
protected readonly AgentSettings _settings;
public virtual string AgentId => _settings.RouterId;
public Router(IServiceProvider services,
ILogger<Router> logger,
AgentSettings settings)
{
_services = services;
_logger = logger;
_settings = settings;
}
public virtual async Task<Agent> LoadRouter()
{
var agentService = _services.GetRequiredService<IAgentService>();
return await agentService.LoadAgent(AgentId);
}
public RoutingRecord[] GetRoutingRecords()
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
var dbSettings = _services.GetRequiredService<MyDatabaseSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json");
2023-08-29 20:31:53 +00:00
var records = JsonSerializer.Deserialize<RoutingRecord[]>(File.ReadAllText(filePath));
// check if routing profile is specified
filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
if (File.Exists(filePath))
{
var state = _services.GetRequiredService<IConversationStateService>();
var name = state.GetState("channel");
var profiles = JsonSerializer.Deserialize<RoutingProfileRecord[]>(File.ReadAllText(filePath));
var spcificedProfile = profiles.FirstOrDefault(x => x.Name == name);
if (spcificedProfile != null)
{
records = records.Where(x => spcificedProfile.AgentIds.Contains(x.AgentId)).ToArray();
}
}
return records;
2023-08-28 03:50:10 +00:00
}
public RoutingRecord GetRecordByName(string name)
{
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
}
}