UNPKG

donobu

Version:

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

94 lines (91 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoToWebpageTool = exports.GoToWebpageToolGptParameters = exports.GoToWebpageToolCoreParameters = exports.DEFAULT_WAIT_UNTIL = exports.DEFAULT_TIMEOUT_MILLIS = void 0; const v4_1 = require("zod/v4"); const ToolSchema_1 = require("../models/ToolSchema"); const TargetUtils_1 = require("../utils/TargetUtils"); const Tool_1 = require("./Tool"); exports.DEFAULT_TIMEOUT_MILLIS = 30000; exports.DEFAULT_WAIT_UNTIL = 'load'; exports.GoToWebpageToolCoreParameters = v4_1.z.object({ /** * The URL to navigate to. Must use the http or https (example is https://google.com). */ url: v4_1.z .string() .describe('The URL to navigate to. Must use the http or https (example is https://google.com).'), timeout: v4_1.z .number() .optional() .describe(`Optional (defaults to ${exports.DEFAULT_TIMEOUT_MILLIS}ms) - Maximum operation time in milliseconds.`), waitUntil: v4_1.z .enum(['commit', 'domcontentloaded', 'load', 'networkidle']) .optional().describe(`Optional (defaults to '${exports.DEFAULT_WAIT_UNTIL}') - 1. "commit" - Trigger: As soon as the HTTP response is received and the browser starts loading the document. - Scope: Very early - you just know that navigation started, but the DOM may not exist yet. - Use case: Rare. Only when you want to act immediately after a navigation is committed, regardless of whether the page is usable. 2. "domcontentloaded" - Trigger: When the browser fires the DOMContentLoaded event. - Meaning: The initial HTML has been parsed and the DOM tree is built, but: * Stylesheets, images, and subframes might still be loading. * Some JavaScript execution may not have run yet. - Use case: Common if you only need the DOM structure ready (e.g., selectors available), not waiting for all resources. 3. "load" - Trigger: When the browser fires the standard load event. - Meaning: * DOMContentLoaded has already fired. * All synchronous resources (stylesheets, images, scripts, iframes, etc.) are loaded. * Does not guarantee app-specific JS initialization is complete. - Use case: Safer than domcontentloaded if you depend on resources like images/iframes being present. 4. "networkidle" (discouraged) - Trigger: When there have been no network connections for at least 500ms. - Meaning: Suggests the app is "quiet" but: * Single-page apps (SPAs) often open long-lived connections (e.g., WebSockets), so this may never fire. * Some apps load content lazily, so the page might look "ready" before network becomes idle. - Use case: Historically used to "guess" readiness of SPAs, but Playwright discourages it - better to explicitly wait for UI assertions.`), }); exports.GoToWebpageToolGptParameters = v4_1.z.object({ ...ToolSchema_1.BaseGptArgsSchema.shape, ...exports.GoToWebpageToolCoreParameters.shape, }); /** Directly navigate to the specified URL. */ class GoToWebpageTool extends Tool_1.Tool { constructor() { super(GoToWebpageTool.NAME, 'Go directly to the specified URL.', exports.GoToWebpageToolCoreParameters, exports.GoToWebpageToolGptParameters, false, undefined, ['web']); } async call(context, parameters) { const page = (0, TargetUtils_1.webPage)(context); const url = parameters.url.trim(); if (GoToWebpageTool.VALID_PROTOCOLS.some((protocol) => url.startsWith(protocol))) { await page.goto(url, { timeout: parameters.timeout ?? exports.DEFAULT_TIMEOUT_MILLIS, waitUntil: parameters.waitUntil ?? exports.DEFAULT_WAIT_UNTIL, }); const pageTitle = (await page.title()).trim(); const pageTitleNote = pageTitle ? `\nPage Title: ${pageTitle}` : ''; return { isSuccessful: true, forLlm: `Successfully navigated to ${url}${pageTitleNote}`, metadata: { pageTitle: pageTitle, resolvedUrl: page.url(), }, }; } else { return { isSuccessful: false, forLlm: `Will not navigate a URL not using the following prefixes: ${GoToWebpageTool.VALID_PROTOCOLS}`, metadata: null, }; } } async callFromGpt(context, parameters) { return this.call(context, parameters); } } exports.GoToWebpageTool = GoToWebpageTool; GoToWebpageTool.NAME = 'goToWebpage'; GoToWebpageTool.VALID_PROTOCOLS = ['https://', 'http://']; //# sourceMappingURL=GoToWebpageTool.js.map