BotSharp/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs
2024-04-23 17:13:38 -05:00

99 lines
3.8 KiB
C#

using BotSharp.Abstraction.Infrastructures.Enums;
using BotSharp.Abstraction.Routing.Settings;
namespace BotSharp.Core.Routing.Handlers;
public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
public string Name => "route_to_agent";
public string Description => "Route request to appropriate virtual agent.";
public List<ParameterPropertyDef> Parameters => new List<ParameterPropertyDef>
{
new ParameterPropertyDef("next_action_reason",
"the reason why route to this virtual agent",
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",
required: true),
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("args",
"useful parameters of next action agent, format: { }",
type: "object"),
new ParameterPropertyDef("is_new_task",
"whether the user is requesting a new task that is different from the previous topic.",
type: "boolean"),
new ParameterPropertyDef("user_message_in_english",
"Translate user message from non-English to English"),
new ParameterPropertyDef("language",
"User prefered language, considering the whole conversation. Language could be English, Spanish or Chinese.",
required: true),
};
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger<RouteToAgentRoutingHandler> logger, RoutingSettings settings)
: base(services, logger, settings)
{
}
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
{
var states = _services.GetRequiredService<IConversationStateService>();
var goalAgent = states.GetState(StateConst.EXPECTED_GOAL_AGENT);
if (!string.IsNullOrEmpty(goalAgent) && inst.OriginalAgent != goalAgent)
{
inst.OriginalAgent = goalAgent;
// Emit hook
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
await hook.OnRoutingInstructionRevised(inst, message)
);
}
if (inst.IsNewTask)
{
await HookEmitter.Emit<IConversationHook>(_services, async hook =>
await hook.OnNewTaskDetected(message, inst.NextActionReason)
);
}
message.FunctionArgs = JsonSerializer.Serialize(inst);
var ret = await routing.InvokeFunction(message.FunctionName, message);
var agentId = routing.Context.GetCurrentAgentId();
// Update next action agent's name
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
inst.AgentName = agent.Name;
if (inst.ExecutingDirectly)
{
message.Content = inst.Question;
}
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
{
ret = await routing.InvokeAgent(agentId, _dialogs);
}
var response = _dialogs.Last();
inst.Response = response.Content;
return true;
}
}