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

44 lines
1.5 KiB
C#
Raw Normal View History

2024-01-03 19:40:41 +00:00
namespace BotSharp.Plugin.WebDriver.Functions;
public class InputUserTextFn : IFunctionCallback
{
public string Name => "input_user_text";
private readonly IServiceProvider _services;
2024-02-06 05:05:52 +00:00
private readonly IWebBrowser _browser;
2024-01-03 19:40:41 +00:00
public InputUserTextFn(IServiceProvider services,
2024-02-06 05:05:52 +00:00
IWebBrowser browser)
2024-01-03 19:40:41 +00:00
{
_services = services;
2024-02-06 05:05:52 +00:00
_browser = browser;
2024-01-03 19:40:41 +00:00
}
public async Task<bool> Execute(RoleDialogModel message)
{
var convService = _services.GetRequiredService<IConversationService>();
2024-01-03 19:40:41 +00:00
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var result = await _browser.InputUserText(new BrowserActionParams(agent, args, convService.ConversationId, message.MessageId));
2024-01-03 19:40:41 +00:00
2024-02-06 20:42:35 +00:00
var content = $"Input '{args.InputText}' in element '{args.ElementText}'";
if (args.PressEnter != null && args.PressEnter == true)
{
content += " and pressed Enter";
}
message.Content = result ?
2024-02-08 21:39:56 +00:00
content + " successfully" :
content + " failed";
2024-02-06 14:07:09 +00:00
var webDriverService = _services.GetRequiredService<WebDriverService>();
2024-02-06 20:42:35 +00:00
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
message.Data = await _browser.ScreenshotAsync(convService.ConversationId, path);
2024-02-06 14:07:09 +00:00
2024-01-03 19:40:41 +00:00
return true;
}
}