2024-01-26 04:32:48 +00:00
|
|
|
using BotSharp.Abstraction.Agents;
|
|
|
|
|
using BotSharp.Abstraction.Repositories.Filters;
|
2023-10-20 23:57:45 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Settings;
|
2024-01-26 04:32:48 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-10-20 23:57:45 +00:00
|
|
|
|
2023-10-02 20:16:35 +00:00
|
|
|
namespace BotSharp.Abstraction.Routing.Models;
|
|
|
|
|
|
|
|
|
|
public class RoutingContext
|
|
|
|
|
{
|
2024-01-26 04:32:48 +00:00
|
|
|
private readonly IServiceProvider _services;
|
2023-10-20 23:57:45 +00:00
|
|
|
private readonly RoutingSettings _setting;
|
2024-01-26 04:32:48 +00:00
|
|
|
private string[] _routerAgentIds;
|
|
|
|
|
|
|
|
|
|
public RoutingContext(IServiceProvider services, RoutingSettings setting)
|
2023-10-20 23:57:45 +00:00
|
|
|
{
|
2024-01-26 04:32:48 +00:00
|
|
|
_services = services;
|
2023-10-20 23:57:45 +00:00
|
|
|
_setting = setting;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 20:16:35 +00:00
|
|
|
private Stack<string> _stack { get; set; }
|
|
|
|
|
= new Stack<string>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Intent name
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string IntentName { get; set; }
|
|
|
|
|
|
2023-10-27 01:23:06 +00:00
|
|
|
/// <summary>
|
2024-01-13 21:17:40 +00:00
|
|
|
/// Agent that can handle user original goal.
|
2023-10-27 01:23:06 +00:00
|
|
|
/// </summary>
|
2023-10-02 20:16:35 +00:00
|
|
|
public string OriginAgentId
|
2024-01-26 04:32:48 +00:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_routerAgentIds == null)
|
|
|
|
|
{
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
_routerAgentIds = agentService.GetAgents(new AgentFilter
|
|
|
|
|
{
|
|
|
|
|
Type = AgentType.Routing
|
|
|
|
|
}).Result.Items
|
|
|
|
|
.Select(x => x.Id).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _stack.Where(x => !_routerAgentIds.Contains(x)).Last();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-02 20:16:35 +00:00
|
|
|
|
2023-10-29 01:54:10 +00:00
|
|
|
public bool IsEmpty => !_stack.Any();
|
2023-10-20 23:57:45 +00:00
|
|
|
public string GetCurrentAgentId()
|
|
|
|
|
{
|
|
|
|
|
return _stack.Peek();
|
|
|
|
|
}
|
2023-10-02 20:16:35 +00:00
|
|
|
|
|
|
|
|
public void Push(string agentId)
|
|
|
|
|
{
|
|
|
|
|
if (_stack.Count == 0 || _stack.Peek() != agentId)
|
|
|
|
|
{
|
|
|
|
|
_stack.Push(agentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pop current agent
|
|
|
|
|
/// </summary>
|
2023-10-29 01:54:10 +00:00
|
|
|
public void Pop()
|
2023-10-02 20:16:35 +00:00
|
|
|
{
|
2023-10-29 01:54:10 +00:00
|
|
|
_stack.Pop();
|
|
|
|
|
}
|
2023-10-02 20:16:35 +00:00
|
|
|
|
2024-01-26 22:23:10 +00:00
|
|
|
public void Replace(string agentId)
|
|
|
|
|
{
|
|
|
|
|
if (_stack.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_stack.Push(agentId);
|
|
|
|
|
}
|
|
|
|
|
else if (_stack.Peek() != agentId)
|
|
|
|
|
{
|
|
|
|
|
_stack.Pop();
|
|
|
|
|
_stack.Push(agentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 01:54:10 +00:00
|
|
|
public void Empty()
|
|
|
|
|
{
|
|
|
|
|
_stack.Clear();
|
2023-10-02 20:16:35 +00:00
|
|
|
}
|
|
|
|
|
}
|