Add webdriver hook to support file uploads

This commit is contained in:
vguruparan 2025-04-18 16:12:30 -05:00
parent a84e24aec9
commit 16e0553eb0
6 changed files with 31 additions and 3 deletions

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Browsing.Models;
namespace BotSharp.Abstraction.Browsing;
public interface IWebDriverHook
{
Task<List<string>> GetUploadFiles(MessageInfo message);
}

View file

@ -12,6 +12,7 @@ public class MessageInfo : ICacheKey
public string? MessageId { get; set; }
public string? TaskId { get; set; }
public string StepId { get; set; } = Guid.NewGuid().ToString();
public string? FunctionArgs { get; set; }
public string GetCacheKey()
=> $"{nameof(MessageInfo)}";

View file

@ -80,8 +80,20 @@ public partial class PlaywrightWebDriver
}
else if (action.Action == BroswerActionEnum.FileUpload)
{
if (action.FileUrl.Length == 0)
var _states = _services.GetRequiredService<IConversationStateService>();
var files = new List<string>();
if (action.FileUrl != null && action.FileUrl.Length > 0)
{
files.AddRange(action.FileUrl);
}
var hooks = _services.GetServices<IWebDriverHook>();
foreach (var hook in hooks)
{
files.AddRange(await hook.GetUploadFiles(message));
}
if (files.Count == 0)
{
Serilog.Log.Warning($"No files found to upload: {action.Content}");
return;
}
var fileChooser = await page.RunAndWaitForFileChooserAsync(async () =>
@ -97,7 +109,7 @@ public partial class PlaywrightWebDriver
Directory.CreateDirectory(directory);
var localPaths = new List<string>();
using var httpClient = new HttpClient();
foreach (var fileUrl in action.FileUrl)
foreach (var fileUrl in files)
{
var bytes = await httpClient.GetByteArrayAsync(fileUrl);
var fileName = new Uri(fileUrl).AbsolutePath;

View file

@ -71,7 +71,7 @@ public partial class PlaywrightWebDriver : IWebBrowser
public void SetServiceProvider(IServiceProvider services)
{
_instance.SetServiceProvider(_services);
_instance.SetServiceProvider(services);
}
public async Task PressKey(MessageInfo message, string key)

View file

@ -42,7 +42,10 @@ public class UtilWebActionOnElementFn : IFunctionCallback
AgentId = message.CurrentAgentId,
MessageId = message.MessageId,
ContextId = webDriverService.GetMessageContext(message),
FunctionArgs = message.FunctionArgs
};
browser.SetServiceProvider(_services);
var _states = _services.GetRequiredService<IConversationStateService>();
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}.";

View file

@ -37,6 +37,10 @@
"wait_time": {
"type": "number",
"description": "wait time after action in seconds"
},
"metadata": {
"type": "string",
"description": "meta data information if user provided"
}
},
"required": [ "selector", "action" ]