2025-09-04 22:59:29 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Dtos;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Enums;
|
2025-07-01 04:07:58 +00:00
|
|
|
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
|
2025-09-04 22:59:29 +00:00
|
|
|
using BotSharp.Abstraction.Users;
|
|
|
|
|
using BotSharp.Plugin.ChatHub.Helpers;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2025-07-01 04:07:58 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.ChartHandler.Functions;
|
|
|
|
|
|
2025-07-01 17:01:09 +00:00
|
|
|
public class PlotChartFn : IFunctionCallback
|
2025-07-01 04:07:58 +00:00
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
2025-07-01 17:01:09 +00:00
|
|
|
private readonly ILogger<PlotChartFn> _logger;
|
2025-09-04 22:59:29 +00:00
|
|
|
private readonly BotSharpOptions _options;
|
2025-07-01 04:07:58 +00:00
|
|
|
|
2025-07-01 17:01:09 +00:00
|
|
|
public string Name => "util-chart-plot_chart";
|
|
|
|
|
public string Indication => "Plotting chart";
|
2025-07-01 04:07:58 +00:00
|
|
|
|
|
|
|
|
|
2025-07-01 17:01:09 +00:00
|
|
|
public PlotChartFn(
|
2025-07-01 04:07:58 +00:00
|
|
|
IServiceProvider services,
|
2025-09-04 22:59:29 +00:00
|
|
|
ILogger<PlotChartFn> logger,
|
|
|
|
|
BotSharpOptions options)
|
2025-07-01 04:07:58 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_logger = logger;
|
2025-09-04 22:59:29 +00:00
|
|
|
_options = options;
|
2025-07-01 04:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var convService = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
|
|
|
|
|
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
|
2025-09-02 16:52:20 +00:00
|
|
|
|
2025-07-01 04:07:58 +00:00
|
|
|
var agent = await agentService.GetAgent(message.CurrentAgentId);
|
2025-09-02 16:52:20 +00:00
|
|
|
var inst = GetChartPlotInstruction(message.CurrentAgentId);
|
2025-07-01 04:07:58 +00:00
|
|
|
var innerAgent = new Agent
|
|
|
|
|
{
|
|
|
|
|
Id = agent.Id,
|
|
|
|
|
Name = agent.Name,
|
|
|
|
|
Instruction = inst,
|
2025-09-02 23:24:00 +00:00
|
|
|
LlmConfig = new AgentLlmConfig
|
|
|
|
|
{
|
|
|
|
|
MaxOutputTokens = 8192
|
|
|
|
|
},
|
2025-07-01 04:07:58 +00:00
|
|
|
TemplateDict = new Dictionary<string, object>
|
|
|
|
|
{
|
|
|
|
|
{ "plotting_requirement", args?.PlottingRequirement ?? string.Empty },
|
|
|
|
|
{ "chart_element_id", $"chart-{message.MessageId}" }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await GetChatCompletion(innerAgent, [
|
|
|
|
|
new RoleDialogModel(AgentRole.User, "Please follow the instruction to generate the javascript code.")
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = message.CurrentAgentId,
|
|
|
|
|
MessageId = message.MessageId
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-02 23:27:33 +00:00
|
|
|
var obj = response.JsonContent<LlmContextOut>();
|
2025-07-08 22:45:54 +00:00
|
|
|
message.Content = obj?.GreetingMessage ?? "Here is the chart you ask for:";
|
2025-07-01 04:07:58 +00:00
|
|
|
message.RichContent = new RichContent<IRichMessage>
|
|
|
|
|
{
|
|
|
|
|
Recipient = new Recipient { Id = convService.ConversationId },
|
2025-07-08 22:45:54 +00:00
|
|
|
Message = new ProgramCodeTemplateMessage
|
2025-07-01 04:07:58 +00:00
|
|
|
{
|
2025-07-08 22:45:54 +00:00
|
|
|
Text = obj?.JsCode ?? string.Empty,
|
|
|
|
|
Language = "javascript"
|
2025-07-01 04:07:58 +00:00
|
|
|
}
|
|
|
|
|
};
|
2025-09-04 22:59:29 +00:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 04:07:58 +00:00
|
|
|
message.StopCompletion = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 22:59:29 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 04:07:58 +00:00
|
|
|
private async Task<string> GetChatCompletion(Agent agent, List<RoleDialogModel> dialogs)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-08-28 18:42:51 +00:00
|
|
|
var (provider, model) = GetLlmProviderModel();
|
|
|
|
|
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model);
|
2025-07-01 04:07:58 +00:00
|
|
|
var response = await completion.GetChatCompletions(agent, dialogs);
|
|
|
|
|
return response.Content;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var error = $"Error when plotting chart. {ex.Message}";
|
|
|
|
|
_logger.LogWarning(ex, error);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 18:42:51 +00:00
|
|
|
|
2025-09-02 16:52:20 +00:00
|
|
|
private string GetChartPlotInstruction(string agentId)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
|
|
|
|
|
var templateContent = string.Empty;
|
|
|
|
|
var templateName = state.GetState("chart_plot_template");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(templateName))
|
|
|
|
|
{
|
|
|
|
|
templateContent = db.GetAgentTemplate(agentId, templateName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
templateName = "util-chart-plot_instruction";
|
|
|
|
|
templateContent = db.GetAgentTemplate(BuiltInAgentId.UtilityAssistant, templateName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return templateContent;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 18:42:51 +00:00
|
|
|
private (string, string) GetLlmProviderModel()
|
|
|
|
|
{
|
|
|
|
|
var provider = "openai";
|
|
|
|
|
var model = "gpt-5";
|
|
|
|
|
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var settings = _services.GetRequiredService<ChartHandlerSettings>();
|
|
|
|
|
provider = state.GetState("chart_plot_llm_provider")
|
|
|
|
|
.IfNullOrEmptyAs(settings.ChartPlot?.LlmProvider)
|
|
|
|
|
.IfNullOrEmptyAs(provider);
|
|
|
|
|
model = state.GetState("chart_plot_llm_model")
|
|
|
|
|
.IfNullOrEmptyAs(settings.ChartPlot?.LlmModel)
|
|
|
|
|
.IfNullOrEmptyAs(model);
|
|
|
|
|
|
|
|
|
|
return (provider, model);
|
|
|
|
|
}
|
2025-07-01 04:07:58 +00:00
|
|
|
}
|