UNPKG

donobu

Version:

Create browser automations with an LLM agent and replay them as Playwright scripts.

197 lines 10.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolManager = void 0; const Logger_1 = require("../utils/Logger"); const PlaywrightUtils_1 = require("../utils/PlaywrightUtils"); const ToolRequiresGptException_1 = require("../exceptions/ToolRequiresGptException"); const ChooseSelectOptionTool_1 = require("../tools/ChooseSelectOptionTool"); const ClickTool_1 = require("../tools/ClickTool"); const HandleBrowserDialogTool_1 = require("../tools/HandleBrowserDialogTool"); const InputRandomizedEmailAddressTool_1 = require("../tools/InputRandomizedEmailAddressTool"); const MarkObjectiveCompleteTool_1 = require("../tools/MarkObjectiveCompleteTool"); const GoToWebpageTool_1 = require("../tools/GoToWebpageTool"); const PressKeyTool_1 = require("../tools/PressKeyTool"); const RunAccessibilityTestTool_1 = require("../tools/RunAccessibilityTestTool"); const ScrollPageTool_1 = require("../tools/ScrollPageTool"); const AssertTool_1 = require("../tools/AssertTool"); const InputTextTool_1 = require("../tools/InputTextTool"); const AnalyzePageTextTool_1 = require("../tools/AnalyzePageTextTool"); const CreateBrowserCookieReportTool_1 = require("../tools/CreateBrowserCookieReportTool"); const MarkObjectiveNotCompletableTool_1 = require("../tools/MarkObjectiveNotCompletableTool"); const RunInlineJavaScriptCodeTool_1 = require("../tools/RunInlineJavaScriptCodeTool"); const RunSandboxedJavaScriptCodeTool_1 = require("../tools/RunSandboxedJavaScriptCodeTool"); const SummarizeLearningsTool_1 = require("../tools/SummarizeLearningsTool"); const TriggerDonobuFlowTool_1 = require("../tools/TriggerDonobuFlowTool"); const ChangeWebBrowserTabTool_1 = require("../tools/ChangeWebBrowserTabTool"); const AggregateExtractedStreetviewDataTool_1 = require("../tools/AggregateExtractedStreetviewDataTool"); const ExtractGoogleStreetviewEntityDataTool_1 = require("../tools/ExtractGoogleStreetviewEntityDataTool"); const ExtractPaymentProviderKeyTool_1 = require("../tools/ExtractPaymentProviderKeyTool"); const ExtractPublicFacebookEntityDataTool_1 = require("../tools/ExtractPublicFacebookEntityDataTool"); const GetEntityDataFromGoogleMapResult_1 = require("../tools/GetEntityDataFromGoogleMapResult"); const GoForwardOrBackTool_1 = require("../tools/GoForwardOrBackTool"); const GoToGoogleMapsStreetViewTool_1 = require("../tools/GoToGoogleMapsStreetViewTool"); const MakeCommentTool_1 = require("../tools/MakeCommentTool"); const NavigateWithinStreetView_1 = require("../tools/NavigateWithinStreetView"); const PauseForUserInteractionTool_1 = require("../tools/PauseForUserInteractionTool"); const ReloadPageTool_1 = require("../tools/ReloadPageTool"); const SaveWebpageAsPdfTool_1 = require("../tools/SaveWebpageAsPdfTool"); const SetRunModeTool_1 = require("../tools/SetRunModeTool"); const DownloadPdfTool_1 = require("../tools/DownloadPdfTool"); const WaitTool_1 = require("../tools/WaitTool"); const HoverOverElementTool_1 = require("../tools/HoverOverElementTool"); const DetectBrokenLinksTool_1 = require("../tools/DetectBrokenLinksTool"); const JsonUtils_1 = require("../utils/JsonUtils"); const AssertPageTextTool_1 = require("../tools/AssertPageTextTool"); /** * Manages calls to a given list of tools. */ class ToolManager { constructor(tools) { this.tools = tools; this.tools = tools; } async invokeTool(context, toolName, toolParameters, isFromGpt) { const page = context.page; const startedAt = new Date().getTime(); const initialPageUrl = page.url(); Logger_1.appLogger.info(`Taking action: ${toolName}`); let toolCallResult; try { const tool = this.tools.find((t) => t.name === toolName); toolCallResult = tool ? await this.executeToolCall(context, tool, toolParameters, isFromGpt) : { isSuccessful: false, forLlm: `FAILED! No tool named '${toolName}' found.`, metadata: null, }; } catch (error) { Logger_1.appLogger.error(`Exception while calling the ${toolName} tool with parameters ${JSON.stringify(toolParameters)}`, error); toolCallResult = { isSuccessful: false, forLlm: `FAILED! ${typeof error}: ${error.message}`, metadata: null, }; } const postCallImage = await PlaywrightUtils_1.PlaywrightUtils.takePngScreenshot(page); const postCallImageId = await context.persistence.savePngScreenShot(context.metadata.id, postCallImage); Logger_1.appLogger.info(`The ${toolName} tool completed with outcome ${JSON.stringify(toolCallResult)}`); const completedAt = new Date().getTime(); return { id: context.toolCallId, toolName: toolName, parameters: toolParameters, outcome: toolCallResult, postCallImageId: postCallImageId, page: initialPageUrl, startedAt: startedAt, completedAt: completedAt, }; } async executeToolCall(context, tool, toolParameters, isFromGpt) { const flowMetadata = context.metadata; if (flowMetadata.isControlPanelEnabled) { await PlaywrightUtils_1.PlaywrightUtils.updateControlPanel(context.page, flowMetadata, tool.controlPanelMessage); } try { if (tool.requiresGpt && !context.gptClient) { const exception = new ToolRequiresGptException_1.ToolRequiresGptException(tool.name); return { isSuccessful: false, forLlm: exception.userFacingMessage, metadata: JsonUtils_1.JsonUtils.objectToJson(exception), }; } else { return isFromGpt ? await tool.callFromGpt(context, toolParameters) : await tool.call(context, toolParameters); } } catch (error) { if (PlaywrightUtils_1.PlaywrightUtils.isPageClosedError(error)) { return { isSuccessful: false, forLlm: `FAILED! This tool call failed because the webpage in which this operation was being formed had closed.`, metadata: null, }; } else if (error instanceof Error) { Logger_1.appLogger.error(`Exception while calling the ${tool.name} tool with parameters ${JSON.stringify(toolParameters)}: ${error.message}`, error); return { isSuccessful: false, forLlm: `FAILED! ${error.constructor.name}: ${error.message}`, metadata: null, }; } throw error; } } } exports.ToolManager = ToolManager; ToolManager.ALL_TOOLS = [ new AnalyzePageTextTool_1.AnalyzePageTextTool(), new AssertTool_1.AssertTool(), new AssertPageTextTool_1.AssertPageTextTool(), new CreateBrowserCookieReportTool_1.CreateBrowserCookieReportTool(), new ChangeWebBrowserTabTool_1.ChangeWebBrowserTabTool(), new ChooseSelectOptionTool_1.ChooseSelectOptionTool(), new ClickTool_1.ClickTool(), new DownloadPdfTool_1.DownloadPdfTool(), new ExtractGoogleStreetviewEntityDataTool_1.ExtractGoogleStreetviewEntityDataTool(), new ExtractPaymentProviderKeyTool_1.ExtractPaymentProviderKeyTool(), new ExtractPublicFacebookEntityDataTool_1.ExtractPublicFacebookEntityDataTool(), new DetectBrokenLinksTool_1.DetectBrokenLinksTool(), new GetEntityDataFromGoogleMapResult_1.GetEntityDataFromGoogleMapResultTool(), new GoForwardOrBackTool_1.GoForwardOrBackTool(), new GoToGoogleMapsStreetViewTool_1.GoToGoogleMapsStreetViewTool(), new GoToWebpageTool_1.GoToWebpageTool(), new HandleBrowserDialogTool_1.HandleBrowserDialogTool(), new HoverOverElementTool_1.HoverOverElementTool(), new InputRandomizedEmailAddressTool_1.InputRandomizedEmailAddressTool(), new InputTextTool_1.InputTextTool(), new MakeCommentTool_1.MakeCommentTool(), new NavigateWithinStreetView_1.NavigateWithinStreetViewTool(), new MarkObjectiveCompleteTool_1.MarkObjectiveCompleteTool(), new MarkObjectiveNotCompletableTool_1.MarkObjectiveNotCompletableTool(), new PauseForUserInteractionTool_1.PauseForUserInteractionTool(), new AggregateExtractedStreetviewDataTool_1.AggregateExtractedStreetviewDataTool(), new PressKeyTool_1.PressKeyTool(), new ReloadPageTool_1.ReloadPageTool(), new RunAccessibilityTestTool_1.RunAccessibilityTestTool(), new RunInlineJavaScriptCodeTool_1.RunInlineJavaScriptCodeTool(), new RunSandboxedJavaScriptCodeTool_1.RunSandboxedJavaScriptCodeTool(), new SaveWebpageAsPdfTool_1.SaveWebpageAsPdfTool(), new ScrollPageTool_1.ScrollPageTool(), new SetRunModeTool_1.SetRunModeTool(), new SummarizeLearningsTool_1.SummarizeLearningsTool(), new TriggerDonobuFlowTool_1.TriggerDonobuFlowTool(), new WaitTool_1.WaitTool(), ]; ToolManager.DEFAULT_TOOLS = [ new AnalyzePageTextTool_1.AnalyzePageTextTool(), new AssertTool_1.AssertTool(), new AssertPageTextTool_1.AssertPageTextTool(), new CreateBrowserCookieReportTool_1.CreateBrowserCookieReportTool(), new ChangeWebBrowserTabTool_1.ChangeWebBrowserTabTool(), new ChooseSelectOptionTool_1.ChooseSelectOptionTool(), new ClickTool_1.ClickTool(), new DetectBrokenLinksTool_1.DetectBrokenLinksTool(), new GoForwardOrBackTool_1.GoForwardOrBackTool(), new GoToWebpageTool_1.GoToWebpageTool(), new HandleBrowserDialogTool_1.HandleBrowserDialogTool(), new HoverOverElementTool_1.HoverOverElementTool(), new InputRandomizedEmailAddressTool_1.InputRandomizedEmailAddressTool(), new InputTextTool_1.InputTextTool(), new MakeCommentTool_1.MakeCommentTool(), new MarkObjectiveCompleteTool_1.MarkObjectiveCompleteTool(), new MarkObjectiveNotCompletableTool_1.MarkObjectiveNotCompletableTool(), new PauseForUserInteractionTool_1.PauseForUserInteractionTool(), new PressKeyTool_1.PressKeyTool(), new RunAccessibilityTestTool_1.RunAccessibilityTestTool(), new SetRunModeTool_1.SetRunModeTool(), new ScrollPageTool_1.ScrollPageTool(), new SummarizeLearningsTool_1.SummarizeLearningsTool(), ]; //# sourceMappingURL=ToolManager.js.map