From cbf0fe61205760f11b5b88961ea36a5df7bf1e7f Mon Sep 17 00:00:00 2001 From: Joanna Ren <101223@smsassist.com> Date: Thu, 4 Sep 2025 17:59:29 -0500 Subject: [PATCH] add report_summary to PlotChart --- .../BotSharp.Plugin.ChartHandler.csproj | 1 + .../Functions/PlotChartFn.cs | 60 ++++++++++++++++++- .../LlmContext/LlmContextOut.cs | 4 ++ .../util-chart-plot_instruction.liquid | 3 +- .../Helpers/EventEmitter.cs | 4 +- 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/BotSharp.Plugin.ChartHandler.csproj b/src/Plugins/BotSharp.Plugin.ChartHandler/BotSharp.Plugin.ChartHandler.csproj index 784b2405d..a5a725605 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/BotSharp.Plugin.ChartHandler.csproj +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/BotSharp.Plugin.ChartHandler.csproj @@ -30,5 +30,6 @@ + diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs index 8e8106835..64c44c569 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs @@ -1,4 +1,9 @@ +using BotSharp.Abstraction.Conversations.Dtos; +using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Messaging.Models.RichContent.Template; +using BotSharp.Abstraction.Users; +using BotSharp.Plugin.ChatHub.Helpers; +using System.Runtime.CompilerServices; namespace BotSharp.Plugin.ChartHandler.Functions; @@ -6,6 +11,7 @@ public class PlotChartFn : IFunctionCallback { private readonly IServiceProvider _services; private readonly ILogger _logger; + private readonly BotSharpOptions _options; public string Name => "util-chart-plot_chart"; public string Indication => "Plotting chart"; @@ -13,10 +19,12 @@ public class PlotChartFn : IFunctionCallback public PlotChartFn( IServiceProvider services, - ILogger logger) + ILogger logger, + BotSharpOptions options) { _services = services; _logger = logger; + _options = options; } public async Task Execute(RoleDialogModel message) @@ -63,10 +71,60 @@ public async Task Execute(RoleDialogModel message) Language = "javascript" } }; + + // Send report summary after 1.5 seconds if exists + if (!string.IsNullOrEmpty(obj?.ReportSummary)) + { + _ = Task.Run(async () => + { + var services = _services.CreateScope().ServiceProvider; + await Task.Delay(1500); + await SendDelayedMessage(services, obj.ReportSummary, convService.ConversationId, agent.Id, agent.Name); + }); + } + message.StopCompletion = true; return true; } + private async Task SendDelayedMessage(IServiceProvider services, string text, string conversationId, string agentId, string agentName) + { + try + { + var messageId = Guid.NewGuid().ToString(); + var messageData = new ChatResponseDto + { + ConversationId = conversationId, + MessageId = messageId, + Text = text, + Sender = new() { FirstName = agentName, LastName = "", Role = AgentRole.Assistant } + }; + + var dialogModel = new RoleDialogModel(AgentRole.Assistant, text) + { + MessageId = messageId, + CurrentAgentId = agentId, + CreatedAt = DateTime.UtcNow + }; + + var storage = services.GetService(); + storage?.Append(conversationId, dialogModel); + await SendEvent(services, ChatEvent.OnMessageReceivedFromAssistant, conversationId, messageData); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to send delayed message"); + } + } + + private async Task SendEvent(IServiceProvider services, string @event, string conversationId, T data, [CallerMemberName] string callerName = "") + { + var user = services.GetService(); + var json = JsonSerializer.Serialize(data, _options.JsonSerializerOptions); + await EventEmitter.SendChatEvent(services, _logger, @event, conversationId, user?.Id, json, nameof(PlotChartFn), callerName); + } + private async Task GetChatCompletion(Agent agent, List dialogs) { try diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/LlmContext/LlmContextOut.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/LlmContext/LlmContextOut.cs index 7109f72d7..863ad44ea 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/LlmContext/LlmContextOut.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/LlmContext/LlmContextOut.cs @@ -8,6 +8,10 @@ public class LlmContextOut [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? GreetingMessage { get; set; } + [JsonPropertyName("report_summary")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? ReportSummary { get; set; } + [JsonPropertyName("js_code")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? JsCode { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid b/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid index 0d631b51d..84a7b8aca 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot_instruction.liquid @@ -27,5 +27,6 @@ Please take a look at "Plotting Requirement" and generate a javascript code that You must output the response in the following JSON format: { "greeting_message": "A short polite message that informs user that the charts have been generated.", - "js_code": "The javascript code that can generate the charts as requested." + "js_code": "The javascript code that can generate the charts as requested.", + "report_summary": "An insight summary report based on the data, highlighting the key information in markdown format." } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs index 821b173e3..7c2b40778 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs @@ -3,9 +3,9 @@ namespace BotSharp.Plugin.ChatHub.Helpers; -internal class EventEmitter +public class EventEmitter { - internal static async Task SendChatEvent( + public static async Task SendChatEvent( IServiceProvider services, ILogger logger, string @event,