2023-08-28 03:50:10 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-08-28 19:36:02 +00:00
|
|
|
using BotSharp.Abstraction.Repositories;
|
2023-08-28 03:50:10 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Models;
|
2023-08-30 02:19:19 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Settings;
|
2023-08-28 03:50:10 +00:00
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Routing;
|
|
|
|
|
|
|
|
|
|
public class Router : IAgentRouting
|
|
|
|
|
{
|
|
|
|
|
protected readonly IServiceProvider _services;
|
|
|
|
|
protected readonly ILogger _logger;
|
2023-08-30 02:19:19 +00:00
|
|
|
protected readonly RoutingSettings _settings;
|
2023-08-28 03:50:10 +00:00
|
|
|
|
|
|
|
|
public virtual string AgentId => _settings.RouterId;
|
|
|
|
|
|
|
|
|
|
public Router(IServiceProvider services,
|
|
|
|
|
ILogger<Router> logger,
|
2023-08-30 02:19:19 +00:00
|
|
|
RoutingSettings settings)
|
2023-08-28 03:50:10 +00:00
|
|
|
{
|
|
|
|
|
_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>();
|
2023-08-30 02:19:19 +00:00
|
|
|
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, _settings.RouterId, "route.json");
|
2023-08-29 20:31:53 +00:00
|
|
|
var records = JsonSerializer.Deserialize<RoutingRecord[]>(File.ReadAllText(filePath));
|
2023-08-28 19:36:02 +00:00
|
|
|
|
2023-08-29 20:31:53 +00:00
|
|
|
// check if routing profile is specified
|
|
|
|
|
filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, "routing-profile.json");
|
|
|
|
|
if (File.Exists(filePath))
|
2023-08-28 19:36:02 +00:00
|
|
|
{
|
2023-08-29 20:31:53 +00:00
|
|
|
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();
|
|
|
|
|
}
|
2023-08-28 19:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 20:31:53 +00:00
|
|
|
return records;
|
2023-08-28 03:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoutingRecord GetRecordByName(string name)
|
|
|
|
|
{
|
|
|
|
|
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
|
|
|
|
|
}
|
|
|
|
|
}
|