diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 1fe704a3..e3f1a903 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -1,8 +1,14 @@ +using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Loggers; using BotSharp.Abstraction.Loggers.Models; +using BotSharp.Abstraction.Messaging.Models.RichContent; +using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Repositories; +using BotSharp.Core.Agents.Services; using Microsoft.AspNetCore.SignalR; +using Microsoft.VisualBasic; namespace BotSharp.Plugin.ChatHub.Hooks; @@ -54,9 +60,20 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook public override async Task OnFunctionExecuted(RoleDialogModel message) { - + var agentService = _services.GetRequiredService(); + var conversationId = _state.GetConversationId(); + var agent = await agentService.LoadAgent(message.CurrentAgentId); + var log = $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs}) => {message.Content}"; + log += $"\r\n<== MessageId: {message.MessageId}"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, log, message)); } + /// + /// Used to log prompt + /// + /// + /// + /// public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats) { if (!_convSettings.ShowVerboseLog) return; @@ -66,20 +83,33 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook var agent = await agentService.LoadAgent(message.CurrentAgentId); await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, tokenStats.Prompt, message)); - - var log = message.Role == AgentRole.Function ? - $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" : - $"[{agent?.Name}]: {message.Content}"; - log += $"\r\n<== MessageId: {message.MessageId}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conversationId, agent?.Name, log, message)); } + /// + /// Used to log final response + /// + /// + /// public override async Task OnResponseGenerated(RoleDialogModel message) { var conv = _services.GetRequiredService(); var state = _services.GetRequiredService(); await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStateLogGenerated", BuildStateLog(conv.ConversationId, state.GetStates(), message)); + + if (message.Role == AgentRole.Assistant) + { + var agentService = _services.GetRequiredService(); + var agent = await agentService.LoadAgent(message.CurrentAgentId); + var log = $"[{agent?.Name}]: {message.Content}"; + if (message.RichContent != null && message.RichContent.Message.RichType != "text") + { + var richContent = JsonSerializer.Serialize(message.RichContent, _serializerOptions); + log += $"\r\n{richContent}"; + } + log += $"\r\n<== MessageId: {message.MessageId}"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(conv.ConversationId, agent?.Name, log, message)); + } } private string BuildContentLog(string conversationId, string? name, string content, RoleDialogModel message) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs index 66030f88..39929aba 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebDriverConversationHook.cs @@ -17,7 +17,11 @@ public class WebDriverConversationHook : ConversationHookBase // load screenshot if (dialog.Role == AgentRole.Assistant) { - dialog.Data = "data:image/png;base64," + webDriverService.GetScreenshotBase64(dialog.MessageId); + var image64 = webDriverService.GetScreenshotBase64(dialog.MessageId); + if (image64 != null) + { + dialog.Data = $"data:image/png;base64,{image64}"; + } } await base.OnDialogRecordLoaded(dialog);