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 logger, AgentSettings settings) { _services = services; _logger = logger; _settings = settings; } public virtual async Task LoadRouter() { var agentService = _services.GetRequiredService(); return await agentService.LoadAgent(AgentId); } public RoutingRecord[] GetRoutingRecords() { var agentSettings = _services.GetRequiredService(); var dbSettings = _services.GetRequiredService(); var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json"); var records = JsonSerializer.Deserialize(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(); var name = state.GetState("channel"); var profiles = JsonSerializer.Deserialize(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; } public RoutingRecord GetRecordByName(string name) { return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower()); } }