Skip to content

Commit d95dc05

Browse files
authored
Merge pull request #865 from hchen2020/master
WebUtility improvement
2 parents b29a8bc + 4092625 commit d95dc05

File tree

16 files changed

+239
-24
lines changed

16 files changed

+239
-24
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ namespace BotSharp.Abstraction.Browsing.Models;
66
[DebuggerStepThrough]
77
public class ElementActionArgs
88
{
9+
[JsonPropertyName("action")]
10+
[JsonConverter(typeof(JsonStringEnumConverter))]
911
public BroswerActionEnum Action { get; set; }
1012

13+
[JsonPropertyName("content")]
1114
public string? Content { get; set; }
1215

1316
public ElementPosition? Position { get; set; }
@@ -16,6 +19,8 @@ public class ElementActionArgs
1619
/// Delay milliseconds before pressing key
1720
/// </summary>
1821
public int DelayBeforePressingKey { get; set; }
22+
23+
[JsonPropertyName("press_key")]
1924
public string? PressKey { get; set; }
2025

2126
/// <summary>

src/Plugins/BotSharp.Plugin.SqlDriver/Enum/UtilityName.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ public void AddUtilities(List<AgentUtility> utilities)
1414
{
1515
new AgentUtility
1616
{
17-
Name = UtilityName.SqlTableDefinition,
18-
Functions = [new(SQL_TABLE_DEFINITION_FN)],
19-
Templates = [new($"{SQL_TABLE_DEFINITION_FN}.fn")]
20-
},
21-
new AgentUtility
22-
{
23-
Name = UtilityName.SqlDictionaryLookup,
24-
Functions = [new(VERIFY_DICTIONARY_TERM_FN)],
25-
Templates = [new($"{VERIFY_DICTIONARY_TERM_FN}.fn")]
26-
},
27-
new AgentUtility
28-
{
29-
Name = UtilityName.SqlExecutor,
30-
Functions = [new(SQL_SELECT_FN), new(SQL_TABLE_DEFINITION_FN)],
31-
Templates = [new($"{SQL_EXECUTOR_FN}.fn")]
17+
Name = "db.tools",
18+
Functions =
19+
[
20+
new(SQL_TABLE_DEFINITION_FN),
21+
new(VERIFY_DICTIONARY_TERM_FN),
22+
new(SQL_SELECT_FN),
23+
],
24+
Templates =
25+
[
26+
new($"{VERIFY_DICTIONARY_TERM_FN}.fn"),
27+
new($"{SQL_TABLE_DEFINITION_FN}.fn"),
28+
new($"{SQL_EXECUTOR_FN}.fn")
29+
]
3230
}
3331
};
3432

src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
global using BotSharp.Abstraction.Settings;
2424
global using BotSharp.Plugin.SqlDriver.Hooks;
2525
global using BotSharp.Plugin.SqlDriver.Services;
26-
global using BotSharp.Plugin.SqlDriver.Enum;
2726
global using BotSharp.Abstraction.Agents.Enums;
2827
global using BotSharp.Abstraction.Instructs;
2928
global using BotSharp.Abstraction.Instructs.Models;

src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-action_on_element.json">
42+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
43+
</Content>
44+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-close_browser.json">
45+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46+
</Content>
47+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-go_to_page.json">
48+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49+
</Content>
50+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-web-action_on_element.fn.liquid">
51+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
52+
</Content>
53+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-web-go_to_page.fn.liquid">
54+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
55+
</Content>
4156
<Content Include="data\agents\f3ae2a0f-e6ba-4ee1-a0b9-75d7431ff32b\agent.json">
4257
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4358
</Content>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public void SetServiceProvider(IServiceProvider services)
3333

3434
public async Task<IBrowserContext> GetContext(string ctxId)
3535
{
36+
if (!_contexts.ContainsKey(ctxId))
37+
{
38+
await InitContext(ctxId, new BrowserActionArgs());
39+
}
3640
return _contexts[ctxId];
3741
}
3842

@@ -235,6 +239,11 @@ public async Task Close(string ctxId)
235239

236240
public async Task CloseCurrentPage(string ctxId)
237241
{
242+
if (!_pages.ContainsKey(ctxId))
243+
{
244+
return;
245+
}
246+
238247
var pages = _pages[ctxId].ToArray();
239248
for (var i = 0; i < pages.Length; i++)
240249
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace BotSharp.Plugin.WebDriver.Hooks;
2+
3+
public class WebUtilityHook : IAgentUtilityHook
4+
{
5+
private const string PREFIX = "util-web-";
6+
private const string GO_TO_PAGE_FN = $"{PREFIX}go_to_page";
7+
private const string ACTION_ON_ELEMENT_FN = $"{PREFIX}action_on_element";
8+
9+
public void AddUtilities(List<AgentUtility> utilities)
10+
{
11+
var items = new List<AgentUtility>
12+
{
13+
new AgentUtility
14+
{
15+
Name = "web.tools",
16+
Functions =
17+
[
18+
new(GO_TO_PAGE_FN),
19+
new(ACTION_ON_ELEMENT_FN)
20+
],
21+
Templates =
22+
[
23+
new($"{GO_TO_PAGE_FN}.fn"),
24+
new($"{ACTION_ON_ELEMENT_FN}.fn")
25+
]
26+
}
27+
};
28+
29+
utilities.AddRange(items);
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace BotSharp.Plugin.WebDriver.UtilFunctions;
2+
3+
public class UtilWebActionOnElementFn : IFunctionCallback
4+
{
5+
public string Name => "util-web-action_on_element";
6+
public string Indication => "Do action on element.";
7+
private readonly IServiceProvider _services;
8+
private readonly ILogger _logger;
9+
10+
public UtilWebActionOnElementFn(
11+
IServiceProvider services,
12+
ILogger<UtilWebActionOnElementFn> logger)
13+
{
14+
_services = services;
15+
_logger = logger;
16+
}
17+
18+
public async Task<bool> Execute(RoleDialogModel message)
19+
{
20+
var locatorArgs = JsonSerializer.Deserialize<ElementLocatingArgs>(message.FunctionArgs);
21+
var actionArgs = JsonSerializer.Deserialize<ElementActionArgs>(message.FunctionArgs);
22+
if (actionArgs.Action == BroswerActionEnum.InputText)
23+
{
24+
actionArgs.PressKey = "Enter";
25+
}
26+
actionArgs.WaitTime = 2;
27+
28+
var conv = _services.GetRequiredService<IConversationService>();
29+
30+
var browser = _services.GetRequiredService<IWebBrowser>();
31+
var result = await browser.ActionOnElement(new MessageInfo
32+
{
33+
AgentId = message.CurrentAgentId,
34+
MessageId = message.MessageId,
35+
ContextId = message.CurrentAgentId,
36+
}, locatorArgs, actionArgs);
37+
38+
message.Content = $"{actionArgs.Action} executed {(result.IsSuccess ? "success" : "failed")}";
39+
40+
return true;
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace BotSharp.Plugin.WebDriver.UtilFunctions;
2+
3+
public class UtilWebCloseBrowserFn : IFunctionCallback
4+
{
5+
public string Name => "util-web-close_browser";
6+
public string Indication => "Closing web browser.";
7+
private readonly IServiceProvider _services;
8+
private readonly ILogger _logger;
9+
10+
public UtilWebCloseBrowserFn(
11+
IServiceProvider services,
12+
ILogger<UtilWebCloseBrowserFn> logger)
13+
{
14+
_services = services;
15+
_logger = logger;
16+
}
17+
18+
public async Task<bool> Execute(RoleDialogModel message)
19+
{
20+
var conv = _services.GetRequiredService<IConversationService>();
21+
22+
var browser = _services.GetRequiredService<IWebBrowser>();
23+
await browser.CloseBrowser(message.CurrentAgentId);
24+
25+
message.Content = $"Browser closed.";
26+
27+
return true;
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace BotSharp.Plugin.WebDriver.UtilFunctions;
2+
3+
public class UtilWebGoToPageFn : IFunctionCallback
4+
{
5+
public string Name => "util-web-go_to_page";
6+
public string Indication => "Open web page.";
7+
private readonly IServiceProvider _services;
8+
private readonly ILogger _logger;
9+
10+
public UtilWebGoToPageFn(
11+
IServiceProvider services,
12+
ILogger<UtilWebGoToPageFn> logger)
13+
{
14+
_services = services;
15+
_logger = logger;
16+
}
17+
18+
public async Task<bool> Execute(RoleDialogModel message)
19+
{
20+
var args = JsonSerializer.Deserialize<PageActionArgs>(message.FunctionArgs, new JsonSerializerOptions
21+
{
22+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
23+
});
24+
args.WaitForNetworkIdle = false;
25+
args.WaitTime = 5;
26+
args.OpenNewTab = true;
27+
28+
var conv = _services.GetRequiredService<IConversationService>();
29+
30+
var browser = _services.GetRequiredService<IWebBrowser>();
31+
var msg = new MessageInfo
32+
{
33+
AgentId = message.CurrentAgentId,
34+
MessageId = message.MessageId,
35+
ContextId = message.CurrentAgentId,
36+
};
37+
await browser.CloseCurrentPage(msg);
38+
await browser.GoToPage(msg, args);
39+
40+
message.Content = $"Open web page successfully.";
41+
42+
return true;
43+
}
44+
}

src/Plugins/BotSharp.Plugin.WebDriver/WebDriverPlugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3939

4040
services.AddScoped<WebDriverService>();
4141
services.AddScoped<IConversationHook, WebDriverConversationHook>();
42+
43+
services.AddScoped<IAgentUtilityHook, WebUtilityHook>();
4244
}
4345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "util-web-action_on_element",
3+
"description": "Do action on specific element located by selector",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"selector": {
8+
"type": "string",
9+
"description": "element selector in XPath, use syntax of Playwright in .NET"
10+
},
11+
"action": {
12+
"type": "string",
13+
"description": "action to perform on element",
14+
"enum": [ "Click", "InputText" ]
15+
},
16+
"content": {
17+
"type": "string",
18+
"description": "content to input in element"
19+
}
20+
},
21+
"required": [ "selector", "action" ]
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "util-web-close_browser",
3+
"description": "Close browser.",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
},
8+
"required": [ ]
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "util-web-go_to_page",
3+
"description": "Go to web page in specific url.",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"url": {
8+
"type": "string",
9+
"description": "Web URL"
10+
}
11+
},
12+
"required": [ "url" ]
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When user asks to do specific actions (click, fill content, scroll) on a web page, use tool of util-web-action_on_element.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When user asks to open a web page, use tool of util-web-go_to_page.

0 commit comments

Comments
 (0)