Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't reference ChatHub in Core project.

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
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;

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";


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)
Expand Down Expand Up @@ -63,10 +71,60 @@ public async Task<bool> 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<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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
4 changes: 2 additions & 2 deletions src/Plugins/BotSharp.Plugin.ChatHub/Helpers/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

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,
Expand Down
Loading