From 72c5ec7bd941489d7cec2d2939a707aacd07ea4b Mon Sep 17 00:00:00 2001 From: Haiping Chen <101423@smsassist.com> Date: Wed, 28 Feb 2024 14:40:59 -0600 Subject: [PATCH] Improve IRoutingHook. --- .../Conversations/IConversationService.cs | 4 +- .../Routing/IRoutingContext.cs | 13 +++-- .../Routing/IRoutingHook.cs | 18 ++----- .../Routing/IRoutingService.cs | 1 + .../ConversationService.SendMessage.cs | 13 +++-- .../Services/ConversationStateService.cs | 4 -- .../Routing/Functions/RouteToAgentFn.cs | 48 +++++++++++------ .../Routing/Planning/HFPlanner.cs | 2 +- .../Routing/Planning/NaivePlanner.cs | 2 +- .../Routing/Planning/SequentialPlanner.cs | 2 +- .../TwoStagePlanner/TwoStagePlanner.cs | 2 +- .../BotSharp.Core/Routing/RoutingContext.cs | 44 +++++++++++---- .../BotSharp.Core/Routing/RoutingService.cs | 17 +++--- src/Infrastructure/BotSharp.Core/Using.cs | 4 ++ .../Hooks/StreamingLogHook.cs | 54 ++++++++----------- 15 files changed, 132 insertions(+), 96 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 41a6333f..73978b98 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -23,13 +23,13 @@ public interface IConversationService /// /// /// - /// + /// Received the response from AI Agent /// This delegate is useful when you want to report progress on UI /// This delegate is useful when you want to report progress on UI /// Task SendMessage(string agentId, RoleDialogModel lastDalog, - Func onMessageReceived, + Func onResponseReceived, Func onFunctionExecuting, Func onFunctionExecuted); diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs index 01da8e31..2c0aa15e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs @@ -3,11 +3,16 @@ namespace BotSharp.Abstraction.Routing; public interface IRoutingContext { string GetCurrentAgentId(); + string PreviousAgentId(); string OriginAgentId { get; } + string ConversationId { get; } + string MessageId { get; } + void SetMessageId(string conversationId, string messageId); bool IsEmpty { get; } string IntentName { get; set; } - void Push(string agentId); - void Pop(); - void Replace(string agentId); - void Empty(); + int AgentCount { get; } + void Push(string agentId, string? reason = null); + void Pop(string? reason = null); + void Replace(string agentId, string? reason = null); + void Empty(string? reason = null); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs index 34a07162..9fb25a63 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHook.cs @@ -4,27 +4,19 @@ namespace BotSharp.Abstraction.Routing; public interface IRoutingHook { - /// - /// Conversation is redirected to another agent - /// - /// - /// - /// - Task OnConversationRedirected(string toAgentId, RoleDialogModel message); - /// /// Routing instruction is received from Router /// /// routing instruction /// message /// - Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message); + Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message); - Task OnAgentEnqueued(string agentId, string preAgentId); + Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null); - Task OnAgentDequeued(string agentId, string currentAgentId); + Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null); - Task OnAgentReplaced(string fromAgentId, string toAgentId); + Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null); - Task OnAgentQueueEmptied(string agentId); + Task OnAgentQueueEmptied(string agentId, string? reason = null); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs index b20da8de..74845e1f 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing; public interface IRoutingService { Agent Router { get; } + IRoutingContext Context { get; } /// /// Get routable agents diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 92b840d7..1f178fc1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -1,7 +1,6 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Messaging.Models.RichContent; -using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing.Settings; using System.Drawing; @@ -38,6 +37,11 @@ public partial class ConversationService RoleDialogModel response = message; bool stopCompletion = false; + // Enqueue receiving agent first in case it stop completion by OnMessageReceived + var routing = _services.GetRequiredService(); + routing.Context.SetMessageId(_conversationId, message.MessageId); + routing.Context.Push(agent.Id); + // Before chat completion hook foreach (var hook in hooks) { @@ -50,13 +54,14 @@ public partial class ConversationService if (message.StopCompletion) { stopCompletion = true; + routing.Context.Pop(); + break; } } if (!stopCompletion) { // Routing with reasoning - var routing = _services.GetRequiredService(); var settings = _services.GetRequiredService(); response = agent.Type == AgentType.Routing ? @@ -93,7 +98,7 @@ public partial class ConversationService return converation; } - private async Task HandleAssistantMessage(RoleDialogModel response, Func onMessageReceived) + private async Task HandleAssistantMessage(RoleDialogModel response, Func onResponseReceived) { var agentService = _services.GetRequiredService(); var agent = await agentService.GetAgent(response.CurrentAgentId); @@ -130,7 +135,7 @@ public partial class ConversationService await hook.OnResponseGenerated(response); } - await onMessageReceived(response); + await onResponseReceived(response); // Add to dialog history _storage.Append(_conversationId, response); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 0c106612..069d3598 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -1,6 +1,3 @@ -using BotSharp.Abstraction.Repositories; -using System.Linq; - namespace BotSharp.Core.Conversations.Services; /// @@ -26,7 +23,6 @@ public class ConversationStateService : IConversationStateService, IDisposable public string GetConversationId() => _conversationId; - /// /// Set conversation state /// diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs index c8085cf3..bba32ff8 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs @@ -25,22 +25,49 @@ public class RouteToAgentFn : IFunctionCallback public async Task Execute(RoleDialogModel message) { var args = JsonSerializer.Deserialize(message.FunctionArgs); + var states = _services.GetRequiredService(); // Push original task agent if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32) { + // Correct user goal agent to keep orignal task + var goalAgentInState = states.GetState("user_goal_agent", string.Empty); + if (goalAgentInState == string.Empty) + { + states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true); + } + else if (args.OriginalAgent == args.AgentName && args.OriginalAgent != goalAgentInState) + { + // Correct to original agent + args.OriginalAgent = goalAgentInState; + } + else if (args.OriginalAgent != args.AgentName && args.OriginalAgent != goalAgentInState) + { + // Correct to original agent + states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true); + } + var db = _services.GetRequiredService(); var filter = new AgentFilter { AgentName = args.OriginalAgent }; var originalAgent = db.GetAgents(filter).FirstOrDefault(); if (originalAgent != null) { - _context.Push(originalAgent.Id); + _context.Push(originalAgent.Id, $"user goal agent"); } } - else + + // Push next action agent + if (!string.IsNullOrEmpty(args.AgentName) && args.AgentName.Length < 32) { - // Push current agent to routing stack - _context.Push(message.CurrentAgentId); + var db = _services.GetRequiredService(); + var filter = new AgentFilter { AgentName = args.AgentName }; + var actionAgent = db.GetAgents(filter).FirstOrDefault(); + if (actionAgent != null) + { + _context.Push(actionAgent.Id, args.Reason); + } + + states.SetState("last_action_agent", args.AgentName, isNeedVersion: true); } if (string.IsNullOrEmpty(args.AgentName)) @@ -67,17 +94,11 @@ public class RouteToAgentFn : IFunctionCallback if (missingfield && message.CurrentAgentId != agentId) { // Stack original Agent - _context.Push(targetAgent.Id); - - message.CurrentAgentId = agentId; - } - else - { - message.CurrentAgentId = targetAgent.Id; + _context.Push(agentId, reason: "redirection rule"); } } - _context.Push(message.CurrentAgentId); + message.CurrentAgentId = _context.GetCurrentAgentId(); return true; } @@ -153,9 +174,6 @@ public class RouteToAgentFn : IFunctionCallback #else logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***"); #endif - HookEmitter.Emit(_services, async hook => - await hook.OnConversationRedirected(routingRule.RedirectTo, message) - ).Wait(); } else { diff --git a/src/Infrastructure/BotSharp.Core/Routing/Planning/HFPlanner.cs b/src/Infrastructure/BotSharp.Core/Routing/Planning/HFPlanner.cs index 6f49c933..4e508662 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Planning/HFPlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Planning/HFPlanner.cs @@ -85,7 +85,7 @@ public class HFPlanner : IPlaner public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { var context = _services.GetRequiredService(); - context.Empty(); + context.Empty(reason: $"Agent queue is cleared by {nameof(HFPlanner)}"); return true; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs b/src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs index 684c947c..37277b3c 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Planning/NaivePlanner.cs @@ -101,7 +101,7 @@ public class NaivePlanner : IPlaner } else { - context.Empty(); + context.Empty(reason: $"Agent queue is cleared by {nameof(NaivePlanner)}"); } return true; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs b/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs index 24cc2aa7..ff094770 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Planning/SequentialPlanner.cs @@ -144,7 +144,7 @@ public class SequentialPlanner : IPlaner if (message.StopCompletion) { - context.Empty(); + context.Empty(reason: $"Agent queue is cleared by {nameof(SequentialPlanner)}"); return false; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Planning/TwoStagePlanner/TwoStagePlanner.cs b/src/Infrastructure/BotSharp.Core/Routing/Planning/TwoStagePlanner/TwoStagePlanner.cs index 8dfa2f69..6df03a08 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Planning/TwoStagePlanner/TwoStagePlanner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Planning/TwoStagePlanner/TwoStagePlanner.cs @@ -159,7 +159,7 @@ public partial class TwoStagePlanner : IPlaner if (message.StopCompletion || _isTaskCompleted) { - context.Empty(); + context.Empty(reason: $"Agent queue is cleared by {nameof(TwoStagePlanner)}"); return false; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs index 6b39978e..d12adfa2 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core.Routing; @@ -8,6 +7,8 @@ public class RoutingContext : IRoutingContext private readonly IServiceProvider _services; private readonly RoutingSettings _setting; private string[] _routerAgentIds; + private string _conversationId; + private string _messageId; public RoutingContext(IServiceProvider services, RoutingSettings setting) { @@ -15,6 +16,10 @@ public class RoutingContext : IRoutingContext _setting = setting; } + public int AgentCount => _stack.Count; + public string ConversationId => _conversationId; + public string MessageId => _messageId; + private Stack _stack { get; set; } = new Stack(); @@ -45,12 +50,13 @@ public class RoutingContext : IRoutingContext } public bool IsEmpty => !_stack.Any(); + public string GetCurrentAgentId() { return _stack.Peek(); } - public void Push(string agentId) + public void Push(string agentId, string? reason = null) { if (_stack.Count == 0 || _stack.Peek() != agentId) { @@ -58,7 +64,7 @@ public class RoutingContext : IRoutingContext _stack.Push(agentId); HookEmitter.Emit(_services, async hook => - await hook.OnAgentEnqueued(agentId, preAgentId) + await hook.OnAgentEnqueued(agentId, preAgentId, reason: reason) ).Wait(); } } @@ -66,7 +72,7 @@ public class RoutingContext : IRoutingContext /// /// Pop current agent /// - public void Pop() + public void Pop(string? reason = null) { if (_stack.Count == 0) { @@ -76,11 +82,25 @@ public class RoutingContext : IRoutingContext var agentId = _stack.Pop(); HookEmitter.Emit(_services, async hook => - await hook.OnAgentDequeued(agentId, _stack.Peek()) + await hook.OnAgentDequeued(agentId, _stack.Peek(), reason: reason) ).Wait(); } - public void Replace(string agentId) + public string PreviousAgentId() + { + if (_stack.Count == 1) + { + return GetCurrentAgentId(); + } + else if (_stack.Count > 1) + { + return _stack.ToArray()[1]; + } + + return string.Empty; + } + + public void Replace(string agentId, string? reason = null) { var fromAgent = agentId; var toAgent = agentId; @@ -96,12 +116,12 @@ public class RoutingContext : IRoutingContext _stack.Push(agentId); HookEmitter.Emit(_services, async hook => - await hook.OnAgentReplaced(fromAgent, toAgent) + await hook.OnAgentReplaced(fromAgent, toAgent, reason: reason) ).Wait(); } } - public void Empty() + public void Empty(string? reason = null) { if (_stack.Count == 0) { @@ -111,7 +131,13 @@ public class RoutingContext : IRoutingContext var agentId = GetCurrentAgentId(); _stack.Clear(); HookEmitter.Emit(_services, async hook => - await hook.OnAgentQueueEmptied(agentId) + await hook.OnAgentQueueEmptied(agentId, reason: reason) ).Wait(); } + + public void SetMessageId(string conversationId, string messageId) + { + _conversationId = conversationId; + _messageId = messageId; + } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs index 8d93b420..8e2560bb 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs @@ -1,8 +1,3 @@ -using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Filters; -using BotSharp.Abstraction.Routing; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Routing.Planning; using BotSharp.Abstraction.Routing.Settings; @@ -14,8 +9,11 @@ public partial class RoutingService : IRoutingService { private readonly IServiceProvider _services; private readonly RoutingSettings _settings; + private readonly IRoutingContext _context; private readonly ILogger _logger; private Agent _router; + + public IRoutingContext Context => _context; public Agent Router => _router; public void ResetRecursiveCounter() @@ -25,10 +23,12 @@ public partial class RoutingService : IRoutingService public RoutingService(IServiceProvider services, RoutingSettings settings, + IRoutingContext context, ILogger logger) { _services = services; _settings = settings; + _context = context; _logger = logger; } @@ -80,15 +80,14 @@ public partial class RoutingService : IRoutingService var conv = _services.GetRequiredService(); var dialogs = conv.GetDialogHistory(); - var context = _services.GetRequiredService(); var executor = _services.GetRequiredService(); var planner = GetPlanner(_router); - context.Push(_router.Id); + _context.Push(_router.Id); int loopCount = 0; - while (loopCount < planner.MaxLoopCount && !context.IsEmpty) + while (loopCount < planner.MaxLoopCount && !_context.IsEmpty) { loopCount++; @@ -99,7 +98,7 @@ public partial class RoutingService : IRoutingService var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs); await HookEmitter.Emit(_services, async hook => - await hook.OnConversationRouting(inst, message) + await hook.OnRoutingInstructionReceived(inst, message) ); // Save states diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs index c55585e5..fbb1c8df 100644 --- a/src/Infrastructure/BotSharp.Core/Using.cs +++ b/src/Infrastructure/BotSharp.Core/Using.cs @@ -18,6 +18,10 @@ global using BotSharp.Abstraction.Conversations.Models; global using BotSharp.Abstraction.Agents.Settings; global using BotSharp.Abstraction.Conversations.Settings; global using BotSharp.Abstraction.Agents.Enums; +global using BotSharp.Abstraction.Agents.Models; +global using BotSharp.Abstraction.Functions.Models; +global using BotSharp.Abstraction.Repositories; +global using BotSharp.Abstraction.Repositories.Filters; global using BotSharp.Core.Repository; global using BotSharp.Core.Routing; global using BotSharp.Core.Agents.Services; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 67349f7e..b0ca1859 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -18,7 +18,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR private readonly IConversationStateService _state; private readonly IUserIdentity _user; private readonly IAgentService _agentService; - private string _messageId; + private readonly IRoutingContext _routingCtx; public StreamingLogHook( ConversationSetting convSettings, @@ -26,7 +26,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR IHubContext chatHub, IConversationStateService state, IUserIdentity user, - IAgentService agentService) + IAgentService agentService, + IRoutingContext routingCtx) { _convSettings = convSettings; _services = serivces; @@ -34,6 +35,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR _state = state; _user = user; _agentService = agentService; + _routingCtx = routingCtx; _serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, @@ -45,7 +47,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR public override async Task OnMessageReceived(RoleDialogModel message) { - _messageId = message.MessageId; var conversationId = _state.GetConversationId(); var log = $"{message.Content}"; await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", @@ -163,79 +164,68 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR } #region IRoutingHook - public async Task OnAgentEnqueued(string agentId, string preAgentId) + public async Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null) { var conversationId = _state.GetConversationId(); var agent = await _agentService.LoadAgent(agentId); var preAgent = await _agentService.LoadAgent(preAgentId); - var log = $"{agent.Name} is enqueued"; + var log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}"; await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, preAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) + BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) { - MessageId = _messageId + MessageId = _routingCtx.MessageId })); } - public async Task OnAgentDequeued(string agentId, string currentAgentId) + public async Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null) { var conversationId = _state.GetConversationId(); var agent = await _agentService.LoadAgent(agentId); var currentAgent = await _agentService.LoadAgent(currentAgentId); - var log = $"{agent.Name} is dequeued"; + var log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent.Name}"; await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) + BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) { - MessageId = _messageId + MessageId = _routingCtx.MessageId })); } - public async Task OnAgentReplaced(string fromAgentId, string toAgentId) + public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null) { var conversationId = _state.GetConversationId(); var fromAgent = await _agentService.LoadAgent(fromAgentId); var toAgent = await _agentService.LoadAgent(toAgentId); - var log = $"{fromAgent.Name} is replaced to {toAgent.Name}"; + var log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}"; await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, toAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) + BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) { - MessageId = _messageId + MessageId = _routingCtx.MessageId })); } - public async Task OnAgentQueueEmptied(string agentId) + public async Task OnAgentQueueEmptied(string agentId, string? reason = null) { var conversationId = _state.GetConversationId(); var agent = await _agentService.LoadAgent(agentId); - var log = $"Agent queue is cleared."; + var log = reason ?? "Agent queue is cleared"; await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) + BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log) { - MessageId = _messageId + MessageId = _routingCtx.MessageId })); } - public async Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message) + public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message) { var conversationId = _state.GetConversationId(); var agent = await _agentService.LoadAgent(message.CurrentAgentId); var log = JsonSerializer.Serialize(instruct, _serializerOptions); await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.AgentResponse, message)); - } - - public async Task OnConversationRedirected(string toAgentId, RoleDialogModel message) - { - var conversationId = _state.GetConversationId(); - var fromAgent = await _agentService.LoadAgent(message.CurrentAgentId); - var toAgent = await _agentService.LoadAgent(toAgentId); - - var log = $"{message.Content}\r\n=====\r\nREDIRECTED TO {toAgent.Name}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, fromAgent.Name, log, ContentLogSource.HardRule, message)); + BuildContentLog(conversationId, agent.Name, log, ContentLogSource.AgentResponse, message)); } #endregion }