Skip to content

Commit 69a5813

Browse files
authored
Merge pull request #1016 from visagang/features/vguruparan
Add webdriver hook to support file uploads
2 parents 3e33be3 + 74d38a5 commit 69a5813

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using BotSharp.Abstraction.Browsing.Models;
2+
3+
namespace BotSharp.Abstraction.Browsing;
4+
5+
public interface IWebDriverHook
6+
{
7+
Task<List<string>> GetUploadFiles(MessageInfo message);
8+
}

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/MessageInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class MessageInfo : ICacheKey
1212
public string? MessageId { get; set; }
1313
public string? TaskId { get; set; }
1414
public string StepId { get; set; } = Guid.NewGuid().ToString();
15+
public string? FunctionArgs { get; set; }
1516

1617
public string GetCacheKey()
1718
=> $"{nameof(MessageInfo)}";

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,20 @@ await locator.ClickAsync(new LocatorClickOptions
8080
}
8181
else if (action.Action == BroswerActionEnum.FileUpload)
8282
{
83-
if (action.FileUrl.Length == 0)
83+
var _states = _services.GetRequiredService<IConversationStateService>();
84+
var files = new List<string>();
85+
if (action.FileUrl != null && action.FileUrl.Length > 0)
8486
{
87+
files.AddRange(action.FileUrl);
88+
}
89+
var hooks = _services.GetServices<IWebDriverHook>();
90+
foreach (var hook in hooks)
91+
{
92+
files.AddRange(await hook.GetUploadFiles(message));
93+
}
94+
if (files.Count == 0)
95+
{
96+
Serilog.Log.Warning($"No files found to upload: {action.Content}");
8597
return;
8698
}
8799
var fileChooser = await page.RunAndWaitForFileChooserAsync(async () =>
@@ -97,7 +109,7 @@ await locator.ClickAsync(new LocatorClickOptions
97109
Directory.CreateDirectory(directory);
98110
var localPaths = new List<string>();
99111
using var httpClient = new HttpClient();
100-
foreach (var fileUrl in action.FileUrl)
112+
foreach (var fileUrl in files)
101113
{
102114
var bytes = await httpClient.GetByteArrayAsync(fileUrl);
103115
var fileName = new Uri(fileUrl).AbsolutePath;

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void SetAgent(Agent agent)
7171

7272
public void SetServiceProvider(IServiceProvider services)
7373
{
74-
_instance.SetServiceProvider(_services);
74+
_instance.SetServiceProvider(services);
7575
}
7676

7777
public async Task PressKey(MessageInfo message, string key)

src/Plugins/BotSharp.Plugin.WebDriver/UtilFunctions/UtilWebActionOnElementFn.cs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,52 @@ public async Task<bool> Execute(RoleDialogModel message)
1919
{
2020
var locatorArgs = JsonSerializer.Deserialize<ElementLocatingArgs>(message.FunctionArgs);
2121
var actionArgs = JsonSerializer.Deserialize<ElementActionArgs>(message.FunctionArgs);
22-
if (actionArgs.Action == BroswerActionEnum.InputText)
22+
try
2323
{
24-
// Replace variable in input text
25-
if (actionArgs.Content.StartsWith("@"))
24+
if (actionArgs.Action == BroswerActionEnum.InputText)
2625
{
27-
var config = _services.GetRequiredService<IConfiguration>();
28-
var key = actionArgs.Content.Replace("@", string.Empty);
29-
actionArgs.Content = key.Replace(key, config[key]);
26+
// Replace variable in input text
27+
if (actionArgs.Content.StartsWith("@"))
28+
{
29+
var config = _services.GetRequiredService<IConfiguration>();
30+
var key = actionArgs.Content.Replace("@", string.Empty);
31+
actionArgs.Content = key.Replace(key, config[key]);
32+
}
3033
}
31-
}
32-
33-
actionArgs.WaitTime = actionArgs.WaitTime > 0 ? actionArgs.WaitTime : 2;
3434

35-
var conv = _services.GetRequiredService<IConversationService>();
35+
actionArgs.WaitTime = actionArgs.WaitTime > 0 ? actionArgs.WaitTime : 2;
3636

37-
var services = _services.CreateScope().ServiceProvider;
38-
var browser = services.GetRequiredService<IWebBrowser>();
39-
var webDriverService = _services.GetRequiredService<WebDriverService>();
40-
var msg = new MessageInfo
41-
{
42-
AgentId = message.CurrentAgentId,
43-
MessageId = message.MessageId,
44-
ContextId = webDriverService.GetMessageContext(message),
45-
};
46-
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
37+
var services = _services.CreateScope().ServiceProvider;
38+
var browser = services.GetRequiredService<IWebBrowser>();
39+
var webDriverService = _services.GetRequiredService<WebDriverService>();
40+
var msg = new MessageInfo
41+
{
42+
AgentId = message.CurrentAgentId,
43+
MessageId = message.MessageId,
44+
ContextId = webDriverService.GetMessageContext(message),
45+
FunctionArgs = message.FunctionArgs
46+
};
47+
browser.SetServiceProvider(_services);
48+
var result = await browser.ActionOnElement(msg, locatorArgs, actionArgs);
4749

48-
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}.";
50+
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}.";
4951

50-
// Add Current Url info to the message
51-
if (actionArgs.ShowCurrentUrl)
52-
{
53-
message.Content += $" Current page url: '{result.UrlAfterAction}'.";
54-
}
52+
// Add Current Url info to the message
53+
if (actionArgs.ShowCurrentUrl)
54+
{
55+
message.Content += $" Current page url: '{result.UrlAfterAction}'.";
56+
}
5557

56-
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
58+
var path = webDriverService.GetScreenshotFilePath(message.MessageId);
5759

58-
message.Data = await browser.ScreenshotAsync(msg, path);
60+
message.Data = await browser.ScreenshotAsync(msg, path);
5961

62+
}
63+
catch (Exception ex)
64+
{
65+
message.Data = $"{actionArgs.Action} execution failed.";
66+
_logger.LogError($"UtilWebActionOnElementFn exception: {ex.Message}. StackTrace: {ex.StackTrace}");
67+
}
6068
return true;
6169
}
6270
}

src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-action_on_element.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
"wait_time": {
3838
"type": "number",
3939
"description": "wait time after action in seconds"
40+
},
41+
"metadata": {
42+
"type": "string",
43+
"description": "meta data information if user provided"
4044
}
4145
},
4246
"required": [ "selector", "action" ]

0 commit comments

Comments
 (0)