BotSharp/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs

99 lines
3.7 KiB
C#
Raw Normal View History

2024-03-31 16:12:20 +00:00
using BotSharp.Abstraction.Infrastructures.Enums;
2023-09-22 20:38:58 +00:00
using BotSharp.Abstraction.Routing.Settings;
namespace BotSharp.Core.Routing.Handlers;
public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
public string Name => "route_to_agent";
2024-03-31 00:48:13 +00:00
public string Description => "Route request to appropriate virtual agent.";
2023-09-22 20:38:58 +00:00
2023-10-28 20:59:26 +00:00
public List<ParameterPropertyDef> Parameters => new List<ParameterPropertyDef>
2023-09-22 20:38:58 +00:00
{
2024-04-08 03:15:51 +00:00
new ParameterPropertyDef("next_action_reason",
"the reason why route to this virtual agent.",
2024-04-08 03:15:51 +00:00
required: true),
new ParameterPropertyDef("next_action_agent",
"agent for next action based on user latest response, if user is replying last agent's question, you must route to this agent.",
2024-04-08 03:15:51 +00:00
required: true),
new ParameterPropertyDef("args",
"useful parameters of next action agent, format: { }",
type: "object"),
2024-04-08 03:15:51 +00:00
new ParameterPropertyDef("user_goal_description",
"user goal based on user initial task.",
required: true),
new ParameterPropertyDef("user_goal_agent",
"agent who can acheive user initial task, must align with user_goal_description.",
required: true),
new ParameterPropertyDef("is_new_task",
"whether the user is requesting a new task that is different from the previous topic.",
2024-05-09 20:59:59 +00:00
type: "boolean")
2023-09-22 20:38:58 +00:00
};
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgentRoutingHandler> logger, RoutingSettings settings)
: base(services, logger, settings)
{
}
2024-04-30 21:25:58 +00:00
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message, Func<RoleDialogModel, Task> onFunctionExecuting)
2023-09-22 20:38:58 +00:00
{
var states = _services.GetRequiredService<IConversationStateService>();
2024-03-31 16:12:20 +00:00
var goalAgent = states.GetState(StateConst.EXPECTED_GOAL_AGENT);
2024-03-11 16:03:42 +00:00
if (!string.IsNullOrEmpty(goalAgent) && inst.OriginalAgent != goalAgent)
{
inst.OriginalAgent = goalAgent;
2024-03-11 14:49:17 +00:00
// Emit hook
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnRoutingInstructionRevised(inst, message)
);
}
2024-04-08 03:15:51 +00:00
if (inst.IsNewTask)
{
await HookEmitter.Emit<IConversationHook>(_services, async hook =>
await hook.OnNewTaskDetected(message, inst.NextActionReason)
);
}
2024-03-31 16:12:20 +00:00
message.FunctionArgs = JsonSerializer.Serialize(inst);
2024-07-02 14:56:15 +00:00
if (message.FunctionName != null)
{
2024-07-11 21:43:03 +00:00
var msg = RoleDialogModel.From(message);
var ret = await routing.InvokeFunction(message.FunctionName, msg);
_dialogs.Add(msg);
2024-07-02 14:56:15 +00:00
}
2024-03-31 16:12:20 +00:00
2024-03-01 05:44:57 +00:00
var agentId = routing.Context.GetCurrentAgentId();
2023-11-14 20:56:51 +00:00
// Update next action agent's name
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
inst.AgentName = agent.Name;
if (inst.ExecutingDirectly)
2023-11-08 22:27:42 +00:00
{
message.Content = inst.Question;
}
2024-01-13 21:17:40 +00:00
if (agent.Disabled)
{
var content = $"This agent ({agent.Name}) is disabled, please install the corresponding plugin ({agent.Plugin.Name}) to activate this agent.";
message = RoleDialogModel.From(message,
role: AgentRole.Assistant,
content: content);
_dialogs.Add(message);
}
else
{
2024-07-02 14:56:15 +00:00
var ret = await routing.InvokeAgent(agentId, _dialogs, onFunctionExecuting);
2024-01-13 21:17:40 +00:00
}
2023-10-30 16:48:18 +00:00
var response = _dialogs.Last();
inst.Response = response.Content;
2023-10-27 15:18:48 +00:00
return true;
2023-09-22 20:38:58 +00:00
}
}