Merge pull request #1146 from Joannall/master

add report_summary to PlotChart
This commit is contained in:
Haiping 2025-09-04 21:45:50 -05:00 committed by GitHub
commit 27ada7c5a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 68 additions and 4 deletions

View file

@ -30,5 +30,6 @@
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj" />
</ItemGroup>
</Project>

View file

@ -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<PlotChartFn> _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<PlotChartFn> logger)
ILogger<PlotChartFn> logger,
BotSharpOptions options)
{
_services = services;
_logger = logger;
_options = options;
}
public async Task<bool> Execute(RoleDialogModel message)
@ -63,10 +71,60 @@ public class PlotChartFn : IFunctionCallback
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<IConversationStorage>();
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<T>(IServiceProvider services, string @event, string conversationId, T data, [CallerMemberName] string callerName = "")
{
var user = services.GetService<IUserIdentity>();
var json = JsonSerializer.Serialize(data, _options.JsonSerializerOptions);
await EventEmitter.SendChatEvent(services, _logger, @event, conversationId, user?.Id, json, nameof(PlotChartFn), callerName);
}
private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
{
try

View file

@ -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; }

View file

@ -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."
}

View file

@ -3,9 +3,9 @@ using System.Runtime.CompilerServices;
namespace BotSharp.Plugin.ChatHub.Helpers;
internal class EventEmitter
public class EventEmitter
{
internal static async Task SendChatEvent<T>(
public static async Task SendChatEvent<T>(
IServiceProvider services,
ILogger logger,
string @event,