2025-05-13 16:45:19 +00:00
|
|
|
using BotSharp.Abstraction.Functions;
|
2025-08-01 19:41:17 +00:00
|
|
|
using BotSharp.Abstraction.Models;
|
|
|
|
|
using BotSharp.Abstraction.SideCar;
|
2025-07-28 22:41:28 +00:00
|
|
|
using BotSharp.Core.MessageHub;
|
2025-07-31 22:48:51 +00:00
|
|
|
using System.Text.Json.Serialization;
|
2025-05-13 16:45:19 +00:00
|
|
|
|
2025-05-16 03:59:57 +00:00
|
|
|
namespace BotSharp.Core.Demo.Functions;
|
2025-05-13 16:45:19 +00:00
|
|
|
|
|
|
|
|
public class GetWeatherFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public GetWeatherFn(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name => "get_weather";
|
|
|
|
|
public string Indication => "Querying weather";
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
2025-07-31 22:48:51 +00:00
|
|
|
var args = JsonSerializer.Deserialize<WeatherLocation>(message.FunctionArgs);
|
2025-07-28 22:41:28 +00:00
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2025-07-29 18:55:53 +00:00
|
|
|
var messageHub = _services.GetRequiredService<MessageHub<HubObserveData<RoleDialogModel>>>();
|
2025-07-28 22:41:28 +00:00
|
|
|
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
|
2025-07-31 22:48:51 +00:00
|
|
|
message.Indication = $"Start querying weather data in {args?.City}";
|
2025-07-28 22:41:28 +00:00
|
|
|
messageHub.Push(new()
|
|
|
|
|
{
|
|
|
|
|
EventName = ChatEvent.OnIndicationReceived,
|
|
|
|
|
Data = message,
|
2025-08-01 18:23:46 +00:00
|
|
|
RefId = conv.ConversationId
|
2025-07-28 22:41:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1500);
|
|
|
|
|
|
2025-08-01 18:23:46 +00:00
|
|
|
message.Indication = $"Still working on it... Hold on, {args?.City}";
|
2025-07-28 22:41:28 +00:00
|
|
|
messageHub.Push(new()
|
|
|
|
|
{
|
|
|
|
|
EventName = ChatEvent.OnIndicationReceived,
|
|
|
|
|
Data = message,
|
2025-08-01 18:23:46 +00:00
|
|
|
RefId = conv.ConversationId
|
2025-07-28 22:41:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1500);
|
|
|
|
|
|
2025-05-16 04:02:38 +00:00
|
|
|
message.Content = $"It is a sunny day!";
|
2025-06-24 02:43:25 +00:00
|
|
|
message.StopCompletion = false;
|
2025-08-01 19:41:17 +00:00
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
var sidecar = _services.GetService<IConversationSideCar>();
|
|
|
|
|
if (sidecar != null)
|
|
|
|
|
{
|
|
|
|
|
var text = $"I want to know fun events in {args?.City}";
|
|
|
|
|
var states = new List<MessageState>
|
|
|
|
|
{
|
|
|
|
|
new() { Key = "channel", Value = "email" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var msg = await sidecar.SendMessage(message.CurrentAgentId, text, states: states);
|
|
|
|
|
message.Content = $"{message.Content} {msg.Content}";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-05-13 16:45:19 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2025-07-31 22:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WeatherLocation
|
|
|
|
|
{
|
|
|
|
|
[JsonPropertyName("city")]
|
|
|
|
|
public string City { get; set; }
|
2025-05-13 16:45:19 +00:00
|
|
|
}
|