diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 9e0ecf02f..c8775ea33 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -80,18 +80,24 @@ public async Task SendMessage(string agentId, if (agent.Type == AgentType.Routing) { + await routing.Context.Push(agent.Id, reason: "request started", updateLazyRouting: false); + // Check the routing mode var states = _services.GetRequiredService(); var routingMode = states.GetState(StateConst.ROUTING_MODE, agent.Mode ?? RoutingMode.Eager); - await routing.Context.Push(agent.Id, reason: "request started", updateLazyRouting: false); + var lazyRoutingAgentId = states.GetState(StateConst.LAZY_ROUTING_AGENT_ID); - if (routingMode == RoutingMode.Lazy) + if (routingMode == RoutingMode.Lazy && !string.IsNullOrEmpty(lazyRoutingAgentId)) { - message.CurrentAgentId = states.GetState(StateConst.LAZY_ROUTING_AGENT_ID, message.CurrentAgentId); + message.CurrentAgentId = lazyRoutingAgentId; + agent = await agentService.LoadAgent(message.CurrentAgentId); await routing.Context.Push(message.CurrentAgentId, reason: "lazy routing", updateLazyRouting: false); + response = await routing.InstructDirect(agent, message, dialogs); + } + else + { + response = await routing.InstructLoop(agent, message, dialogs); } - - response = await routing.InstructLoop(agent, message, dialogs); } else { diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index 985af20ee..af7fb2e37 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -299,14 +299,10 @@ private void UpdateLazyRoutingAgent(bool updateLazyRouting) // Set next handling agent for lazy routing mode var states = _services.GetRequiredService(); - var routingMode = states.GetState(StateConst.ROUTING_MODE, RoutingMode.Eager); - if (routingMode == RoutingMode.Lazy) + var agentId = GetCurrentAgentId(); + if (agentId != BuiltInAgentId.Fallback) { - var agentId = GetCurrentAgentId(); - if (agentId != BuiltInAgentId.Fallback) - { - states.SetState(StateConst.LAZY_ROUTING_AGENT_ID, agentId); - } + states.SetState(StateConst.LAZY_ROUTING_AGENT_ID, agentId); } } }