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

167 lines
5 KiB
C#
Raw Normal View History

2023-10-29 01:54:10 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-09-20 10:31:50 +00:00
using BotSharp.Abstraction.Routing.Settings;
2023-09-19 12:17:50 +00:00
namespace BotSharp.Core.Routing;
public partial class RoutingService : IRoutingService
2023-09-19 12:17:50 +00:00
{
private readonly IServiceProvider _services;
2023-09-20 10:31:50 +00:00
private readonly RoutingSettings _settings;
2024-02-28 20:40:59 +00:00
private readonly IRoutingContext _context;
2023-09-19 12:17:50 +00:00
private readonly ILogger _logger;
2023-10-30 16:48:18 +00:00
private Agent _router;
2024-02-28 20:40:59 +00:00
public IRoutingContext Context => _context;
2023-10-30 16:48:18 +00:00
public Agent Router => _router;
2023-09-19 12:17:50 +00:00
2024-10-29 19:59:46 +00:00
public RoutingService(
IServiceProvider services,
2023-09-20 10:31:50 +00:00
RoutingSettings settings,
2024-02-28 20:40:59 +00:00
IRoutingContext context,
2023-11-01 01:48:12 +00:00
ILogger<RoutingService> logger)
2023-09-19 12:17:50 +00:00
{
_services = services;
2023-09-20 10:31:50 +00:00
_settings = settings;
2024-02-28 20:40:59 +00:00
_context = context;
2023-09-19 12:17:50 +00:00
_logger = logger;
}
public async Task<RoleDialogModel> InstructDirect(Agent agent, RoleDialogModel message)
2023-09-23 21:33:05 +00:00
{
var handlers = _services.GetServices<IRoutingHandler>();
var handler = handlers.FirstOrDefault(x => x.Name == "route_to_agent");
2023-11-16 18:20:43 +00:00
var conv = _services.GetRequiredService<IConversationService>();
2024-04-23 19:02:00 +00:00
var storage = _services.GetRequiredService<IConversationStorage>();
storage.Append(conv.ConversationId, message);
var dialogs = conv.GetDialogHistory();
2023-10-30 16:48:18 +00:00
handler.SetDialogs(dialogs);
2023-10-30 16:48:18 +00:00
var inst = new FunctionCallFromLlm
2023-09-23 21:33:05 +00:00
{
2023-09-25 22:46:00 +00:00
Function = "route_to_agent",
Question = message.Content,
2024-03-22 19:06:20 +00:00
NextActionReason = message.Content,
2023-11-08 22:27:42 +00:00
AgentName = agent.Name,
OriginalAgent = agent.Name,
ExecutingDirectly = true
2023-10-30 16:48:18 +00:00
};
2023-09-23 21:33:05 +00:00
var result = await handler.Handle(this, inst, message);
2023-10-30 16:48:18 +00:00
var response = dialogs.Last();
response.MessageId = message.MessageId;
response.Instruction = inst;
return response;
2023-09-23 21:33:05 +00:00
}
2024-01-27 01:14:14 +00:00
public List<RoutingHandlerDef> GetHandlers(Agent router)
2023-09-20 10:31:50 +00:00
{
2024-11-26 00:06:26 +00:00
var reasoner = GetReasoner(router);
2023-11-01 01:48:12 +00:00
return _services.GetServices<IRoutingHandler>()
2024-11-26 00:06:26 +00:00
.Where(x => x.Planers == null || x.Planers.Contains(reasoner.GetType().Name))
2023-11-01 01:48:12 +00:00
.Where(x => !string.IsNullOrEmpty(x.Description))
.Select((x, i) => new RoutingHandlerDef
{
Name = x.Name,
Description = x.Description,
Parameters = x.Parameters
}).ToList();
}
#if !DEBUG
2025-01-22 05:47:32 +00:00
[SharpCache(10)]
2023-11-01 01:48:12 +00:00
#endif
protected RoutingRule[] GetRoutingRecords()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-11-27 22:20:49 +00:00
var filter = new AgentFilter
{
2024-12-10 18:30:41 +00:00
Disabled = false
2023-11-27 22:20:49 +00:00
};
var agents = db.GetAgents(filter);
2024-12-10 18:30:41 +00:00
var records = agents.Where(x => x.Type == AgentType.Task || x.Type == AgentType.Planning).SelectMany(x =>
2023-11-01 01:48:12 +00:00
{
x.RoutingRules.ForEach(r =>
{
r.AgentId = x.Id;
r.AgentName = x.Name;
});
return x.RoutingRules;
}).ToArray();
return records;
}
#if !DEBUG
2025-01-22 05:47:32 +00:00
[SharpCache(10)]
2023-11-01 01:48:12 +00:00
#endif
public RoutableAgent[] GetRoutableAgents(List<string> profiles)
2023-11-01 01:48:12 +00:00
{
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-11-27 22:20:49 +00:00
var filter = new AgentFilter
{
2024-12-10 18:30:41 +00:00
Disabled = false
2023-11-27 22:20:49 +00:00
};
2023-11-27 22:20:49 +00:00
var agents = db.GetAgents(filter);
2024-12-10 18:30:41 +00:00
var routableAgents = agents.Where(x => x.Type == AgentType.Task || x.Type == AgentType.Planning).Select(x => new RoutableAgent
2023-09-20 10:31:50 +00:00
{
2023-11-01 01:48:12 +00:00
AgentId = x.Id,
Description = x.Description,
Name = x.Name,
2024-12-10 18:30:41 +00:00
Type = x.Type,
Profiles = x.Profiles,
2023-11-01 01:48:12 +00:00
RequiredFields = x.RoutingRules
.Where(p => p.Required)
.Select(p => new ParameterPropertyDef(p.Field, p.Description, type: p.FieldType)
2023-09-26 03:04:04 +00:00
{
2023-11-01 01:48:12 +00:00
Required = p.Required
}).ToList(),
OptionalFields = x.RoutingRules
.Where(p => !p.Required)
.Select(p => new ParameterPropertyDef(p.Field, p.Description, type: p.FieldType)
2023-11-01 01:48:12 +00:00
{
Required = p.Required
}).ToList()
}).ToArray();
// Handle profile.
// Router profile must match the agent profile
if (routableAgents.Length > 0 && profiles.Count > 0)
{
routableAgents = routableAgents.Where(x => x.Profiles != null &&
x.Profiles.Exists(x1 => profiles.Exists(y => x1 == y)))
.ToArray();
}
2024-01-26 04:32:48 +00:00
else if (profiles == null || profiles.Count == 0)
{
routableAgents = routableAgents.Where(x => x.Profiles == null ||
x.Profiles.Count == 0)
.ToArray();
}
return routableAgents;
2023-11-01 01:48:12 +00:00
}
public RoutingRule[] GetRulesByAgentName(string name)
2023-11-01 01:48:12 +00:00
{
return GetRoutingRecords()
.Where(x => x.AgentName.ToLower() == name.ToLower())
.ToArray();
}
public RoutingRule[] GetRulesByAgentId(string id)
{
return GetRoutingRecords()
.Where(x => x.AgentId == id)
.ToArray();
2023-09-22 20:38:58 +00:00
}
2023-09-19 12:17:50 +00:00
}