Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down Expand Up @@ -31,6 +31,9 @@
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-close_browser.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-extract_data_from_page.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-go_to_page.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -103,4 +106,10 @@
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-extract-data.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ public async Task<string> ExtractData(BrowserActionParams actionParams)
await Task.Delay(3000);

// Retrieve the page raw html and infer the element path
var body = await _instance.GetPage(actionParams.ContextId).QuerySelectorAsync("body");
var content = await body.InnerTextAsync();
var pageContent = _instance.GetPage(actionParams.ContextId);
var body = await pageContent.QuerySelectorAsync("body");
var pageUrl = pageContent.Url;
var pageBody = await body.InnerTextAsync();
string content = $"Page URL: `{pageUrl}` <br/> PageBody: {pageBody}";

var driverService = _services.GetRequiredService<WebDriverService>();
var answer = await driverService.ExtraData(actionParams.Agent, content, actionParams.Context.Question, actionParams.MessageId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class WebUtilityHook : IAgentUtilityHook
private const string GO_TO_PAGE_FN = $"{PREFIX}go_to_page";
private const string LOCATE_ELEMENT_FN = $"{PREFIX}locate_element";
private const string ACTION_ON_ELEMENT_FN = $"{PREFIX}action_on_element";
private const string EXTRACT_DATA_FN = $"{PREFIX}extract_data_from_page";

public void AddUtilities(List<AgentUtility> utilities)
{
Expand All @@ -20,7 +21,8 @@ public void AddUtilities(List<AgentUtility> utilities)
new(CLOSE_BROWSER_FN),
new(GO_TO_PAGE_FN),
new(LOCATE_ELEMENT_FN),
new(ACTION_ON_ELEMENT_FN)
new(ACTION_ON_ELEMENT_FN),
new(EXTRACT_DATA_FN)
],
Templates =
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Playwright;

namespace BotSharp.Plugin.WebDriver.UtilFunctions
{
public class UtilWebExtractDataFn : IFunctionCallback
{
public string Name => "util-web-extract_data_from_page";
public string Indication => "Util Web Extract Data from web page";
private readonly IServiceProvider _services;
public UtilWebExtractDataFn(
IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var convService = _services.GetRequiredService<IConversationService>();
var args = JsonSerializer.Deserialize<BrowsingContextIn>(message.FunctionArgs);
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var _browser = _services.GetRequiredService<IWebBrowser>();
var webDriverService = _services.GetRequiredService<WebDriverService>();

var contextId = webDriverService.GetMessageContext(message);
var browerActionParams = new BrowserActionParams(agent, args, contextId, message.MessageId);
message.Content = await _browser.ExtractData(browerActionParams);

var path = webDriverService.GetScreenshotFilePath(message.MessageId);

message.Data = await _browser.ScreenshotAsync(new MessageInfo
{
AgentId = message.CurrentAgentId,
ContextId = contextId,
MessageId = message.MessageId
}, path);

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "util-web-extract_data_from_page",
"description": "Extract data from current web page.",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "the information user wants to know"
}
},
"required": [ "question" ]
}
}
Loading