diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj index 2f391853..07c3ff7d 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.WebDriver/BotSharp.Plugin.WebDriver.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) @@ -31,6 +31,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest @@ -103,4 +106,10 @@ + + + PreserveNewest + + + diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.ExtractData.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.ExtractData.cs index b716b7ba..c0958b13 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.ExtractData.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.ExtractData.cs @@ -9,8 +9,11 @@ public partial class PlaywrightWebDriver 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 = $"PageUrl: {pageUrl}
PageBody: {pageBody}"; var driverService = _services.GetRequiredService(); var answer = await driverService.ExtraData(actionParams.Agent, content, actionParams.Context.Question, actionParams.MessageId); diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs index 63637119..a46547ac 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs @@ -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 utilities) { @@ -20,7 +21,8 @@ public class WebUtilityHook : IAgentUtilityHook 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 = [ diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/UtilFunctions/UtilWebExtractDataFn.cs b/src/Plugins/BotSharp.Plugin.WebDriver/UtilFunctions/UtilWebExtractDataFn.cs new file mode 100644 index 00000000..b5268281 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WebDriver/UtilFunctions/UtilWebExtractDataFn.cs @@ -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 Execute(RoleDialogModel message) + { + var convService = _services.GetRequiredService(); + var args = JsonSerializer.Deserialize(message.FunctionArgs); + var agentService = _services.GetRequiredService(); + var agent = await agentService.LoadAgent(message.CurrentAgentId); + var _browser = _services.GetRequiredService(); + var webDriverService = _services.GetRequiredService(); + + 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; + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-extract_data_from_page.json b/src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-extract_data_from_page.json new file mode 100644 index 00000000..a354c37b --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WebDriver/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-web-extract_data_from_page.json @@ -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" ] + } +} \ No newline at end of file