2025-03-10 21:54:51 +00:00
|
|
|
using BotSharp.Abstraction.Browsing.Settings;
|
|
|
|
|
|
2024-01-03 04:43:37 +00:00
|
|
|
namespace BotSharp.Plugin.WebDriver.Functions;
|
|
|
|
|
|
|
|
|
|
public class OpenBrowserFn : IFunctionCallback
|
|
|
|
|
{
|
|
|
|
|
public string Name => "open_browser";
|
|
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
2024-02-06 05:05:52 +00:00
|
|
|
private readonly IWebBrowser _browser;
|
2025-03-10 21:54:51 +00:00
|
|
|
private readonly WebBrowsingSettings _webBrowsingSettings;
|
2024-01-03 04:43:37 +00:00
|
|
|
|
|
|
|
|
public OpenBrowserFn(IServiceProvider services,
|
2025-03-10 21:54:51 +00:00
|
|
|
IWebBrowser browser,
|
|
|
|
|
WebBrowsingSettings webBrowsingSettings)
|
2024-01-03 04:43:37 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2024-02-06 05:05:52 +00:00
|
|
|
_browser = browser;
|
2025-03-10 21:54:51 +00:00
|
|
|
_webBrowsingSettings = webBrowsingSettings;
|
2024-01-03 04:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> Execute(RoleDialogModel message)
|
|
|
|
|
{
|
2024-02-12 17:11:05 +00:00
|
|
|
var convService = _services.GetRequiredService<IConversationService>();
|
2024-01-03 04:43:37 +00:00
|
|
|
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
|
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://");
|
2024-06-10 15:51:14 +00:00
|
|
|
var msgInfo = new MessageInfo
|
2024-05-31 19:26:27 +00:00
|
|
|
{
|
|
|
|
|
AgentId = message.CurrentAgentId,
|
|
|
|
|
ContextId = convService.ConversationId,
|
|
|
|
|
MessageId = message.MessageId
|
2024-06-10 15:51:14 +00:00
|
|
|
};
|
2024-06-30 13:05:57 +00:00
|
|
|
var result = await _browser.LaunchBrowser(msgInfo, new BrowserActionArgs
|
|
|
|
|
{
|
2025-03-10 21:54:51 +00:00
|
|
|
Headless = _webBrowsingSettings.Headless
|
2024-06-30 13:05:57 +00:00
|
|
|
});
|
2024-06-10 15:51:14 +00:00
|
|
|
result = await _browser.GoToPage(msgInfo, new PageActionArgs
|
|
|
|
|
{
|
|
|
|
|
Url = url
|
|
|
|
|
});
|
2024-02-10 18:14:18 +00:00
|
|
|
|
2024-03-08 13:17:36 +00:00
|
|
|
if (result.IsSuccess)
|
2024-02-10 18:14:18 +00:00
|
|
|
{
|
|
|
|
|
message.Content = string.IsNullOrEmpty(url) ? $"Launch browser with blank page successfully." : $"Open website {url} successfully.";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-04-08 15:09:08 +00:00
|
|
|
message.Content = $"Launch browser failed. {result.Message}";
|
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
|
|
|
|
2024-05-31 19:26:27 +00:00
|
|
|
message.Data = await _browser.ScreenshotAsync(new MessageInfo
|
|
|
|
|
{
|
|
|
|
|
AgentId = message.CurrentAgentId,
|
|
|
|
|
ContextId = convService.ConversationId,
|
|
|
|
|
MessageId = message.MessageId
|
|
|
|
|
}, path);
|
2024-02-06 14:07:09 +00:00
|
|
|
|
2024-03-08 13:17:36 +00:00
|
|
|
return result.IsSuccess;
|
2024-01-03 04:43:37 +00:00
|
|
|
}
|
|
|
|
|
}
|