From f4b4ca4329a7d63555aaae65d0066ccca2c0da24 Mon Sep 17 00:00:00 2001 From: Haiping Chen <101423@smsassist.com> Date: Thu, 22 Feb 2024 15:14:13 -0600 Subject: [PATCH] OnConversationRedirected --- .../Conversations/ConversationHookBase.cs | 5 +++ .../Conversations/IConversationHook.cs | 8 +++++ .../Loggers/Enums/ContentLogSource.cs | 1 + .../Routing/Functions/RouteToAgentFn.cs | 5 +++ .../Hooks/StreamingLogHook.cs | 33 ++++++++++++++----- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs index 0d0cb67e..cd45a39e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs @@ -91,4 +91,9 @@ public abstract class ConversationHookBase : IConversationHook { return Task.CompletedTask; } + + public virtual Task OnConversationRedirected(string toAgentId, RoleDialogModel message) + { + return Task.CompletedTask; + } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs index 4c7168a2..6056718d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs @@ -82,4 +82,12 @@ public interface IConversationHook /// /// Task OnHumanInterventionNeeded(RoleDialogModel message); + + /// + /// Conversation is redirected to another agent + /// + /// + /// + /// + Task OnConversationRedirected(string toAgentId, RoleDialogModel message); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs index f6973770..45d74a51 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs @@ -6,4 +6,5 @@ public static class ContentLogSource public const string Prompt = "prompt"; public const string FunctionCall = "function call"; public const string AgentResponse = "agent response"; + public const string HardRule = "hard rule"; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs index 26f14f45..c367d631 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs @@ -153,6 +153,11 @@ public class RouteToAgentFn : IFunctionCallback #else logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***"); #endif + var hooks = _services.GetServices(); + foreach (var hook in hooks) + { + hook.OnConversationRedirected(routingRule.RedirectTo, message); + } } else { diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 13997825..0f802b07 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -46,6 +46,18 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message)); } + public override async Task OnConversationRedirected(string toAgentId, RoleDialogModel message) + { + var agentService = _services.GetRequiredService(); + 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)); + } + public async Task BeforeGenerating(Agent agent, List conversations) { if (!_convSettings.ShowVerboseLog) return; @@ -83,23 +95,26 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook var agent = await agentService.LoadAgent(message.CurrentAgentId); var logSource = string.Empty; + var log = tokenStats.Prompt; + logSource = ContentLogSource.Prompt; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, agent?.Name, log, logSource, message)); + // Log routing output try { var inst = message.Content.JsonContent(); - logSource = ContentLogSource.AgentResponse; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message)); + if (!string.IsNullOrEmpty(inst.Function)) + { + logSource = ContentLogSource.AgentResponse; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", + BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message)); + } } catch { // ignore } - - var log = tokenStats.Prompt; - logSource = ContentLogSource.Prompt; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", - BuildContentLog(conversationId, agent?.Name, log, logSource, message)); } /// @@ -118,7 +133,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook { var agentService = _services.GetRequiredService(); var agent = await agentService.LoadAgent(message.CurrentAgentId); - var log = $"[{agent?.Name}]: {message.Content}"; + var log = $"{message.Content}"; if (message.RichContent != null && message.RichContent.Message.RichType != "text") { var richContent = JsonSerializer.Serialize(message.RichContent, _serializerOptions);