BotSharp/src/Plugins/BotSharp.Plugin.WebDriver/Functions/GoToPageFn.cs

39 lines
1.3 KiB
C#
Raw Normal View History

2024-02-01 03:29:13 +00:00
namespace BotSharp.Plugin.WebDriver.Functions;
public class GoToPageFn : IFunctionCallback
{
public string Name => "go_to_page";
private readonly IServiceProvider _services;
2024-02-06 05:05:52 +00:00
private readonly IWebBrowser _browser;
2024-02-01 03:29:13 +00:00
public GoToPageFn(IServiceProvider services,
2024-02-06 05:05:52 +00:00
IWebBrowser browser)
2024-02-01 03:29:13 +00:00
{
_services = services;
2024-02-06 05:05:52 +00:00
_browser = browser;
2024-02-01 03:29:13 +00:00
}
public async Task<bool> Execute(RoleDialogModel message)
{
var convService = _services.GetRequiredService<IConversationService>();
2024-02-01 03:29:13 +00:00
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
2024-02-06 14:07:09 +00:00
var webDriverService = _services.GetRequiredService<WebDriverService>();
2024-02-10 18:14:18 +00:00
var url = webDriverService.ReplaceToken(args.Url);
url = url.Replace("https://https://", "https://");
var result = await _browser.GoToPage(convService.ConversationId, url);
2024-03-08 13:17:36 +00:00
message.Content = result.IsSuccess ? $"Page {url} is open." : $"Page {url} open failed. {result.ErrorMessage}";
2024-02-10 18:14:18 +00:00
2024-02-06 20:42:35 +00:00
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
2024-02-06 14:07:09 +00:00
message.Data = await _browser.ScreenshotAsync(convService.ConversationId, path);
2024-02-06 14:07:09 +00:00
2024-03-08 13:17:36 +00:00
return result.IsSuccess;
2024-02-01 03:29:13 +00:00
}
}