From 9e90ae27d2de095f46c171a5c3660e70fae3d19e Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Fri, 21 Aug 2026 16:03:16 +0800 Subject: [PATCH] Fixed the issue of obtaining CurrentAgentId using SetState in a Hook. --- .../BotSharp.Abstraction/Routing/IRoutingContext.cs | 1 + .../Services/ConversationService.SendMessage.cs | 1 + .../BotSharp.Core/Routing/RoutingContext.cs | 10 ++++++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs index 4bf0e0c6a..d2f99e399 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs @@ -10,6 +10,7 @@ public interface IRoutingContext string ConversationId { get; } string MessageId { get; } void SetMessageId(string conversationId, string messageId); + void SetMessageAgentId(string agentId); bool IsEmpty { get; } string IntentName { get; set; } int AgentCount { get; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 12bcff992..aacdc7bb0 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -36,6 +36,7 @@ public async Task SendMessage(string agentId, // Enqueue receiving agent first in case it stop completion by OnMessageReceived var routing = _services.GetRequiredService(); routing.Context.SetMessageId(_conversationId, message.MessageId); + routing.Context.SetMessageAgentId(message.CurrentAgentId); // Save payload in order to assign the payload before hook is invoked if (replyMessage != null && !string.IsNullOrEmpty(replyMessage.Payload)) diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index af7fb2e37..87751369b 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -11,6 +11,7 @@ public class RoutingContext : IRoutingContext private string[] _routerAgentIds; private string _conversationId; private string _messageId; + private string _messageAgentId; private int _currentRecursionDepth = 0; private List _dialogs = []; @@ -60,7 +61,7 @@ public string EntryAgentId { get { - return _stack.LastOrDefault() ?? string.Empty; + return _stack.LastOrDefault() ?? _messageAgentId; } } @@ -70,7 +71,7 @@ public string GetCurrentAgentId() { if (_stack.Count == 0) { - return string.Empty; + return _messageAgentId; } return _stack.Peek(); @@ -228,6 +229,11 @@ public void SetMessageId(string conversationId, string messageId) _messageId = messageId; } + public void SetMessageAgentId(string agentId) + { + _messageAgentId = agentId; + } + public int GetRecursiveCounter() { return _currentRecursionDepth;