UNPKG

donobu

Version:

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

79 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtractPaymentProviderKeyTool = void 0; const Tool_1 = require("./Tool"); class ExtractPaymentProviderKeyTool extends Tool_1.Tool { constructor() { super(ExtractPaymentProviderKeyTool.NAME, 'Extract multiple publishable payment provider keys from the current page.', 'ExtractPaymentProviderToolCoreParameters', 'ExtractPaymentProviderToolGptParameters'); } async call(context, _parameters) { const page = context.page; // Retrieve all <script> tags' content, handling potential empty or null attributes const scriptContents = await page.evaluate(() => Array.from(document.querySelectorAll('script')) .map((script) => script.hasAttribute('src') ? '' : script.innerHTML || script.innerText || script.textContent) .join('\n')); // Define regex patterns for different payment provider keys const stripePattern = /(pk_(test|live)_[A-Za-z0-9]+)/i; const paypalPattern = /(?!(pk_(test|live)_))A[A-Za-z0-9-_]{75,}/i; const stripeKeys = new Set(); const paypalKeys = new Set(); // Search for Stripe keys let match; const stripeMatches = scriptContents.matchAll(new RegExp(stripePattern, 'gi')); for (match of stripeMatches) { stripeKeys.add(match[1]); } // Search for PayPal client IDs const paypalMatches = scriptContents.matchAll(new RegExp(paypalPattern, 'gi')); for (match of paypalMatches) { const key = match[0]; // Skip keys that match the Stripe key pattern if (key.match(stripePattern)) { continue; } // Verify that the key contains either "_" or "-" and is related to PayPal if ((key.includes('_') || key.includes('-')) && this.verifyPayPalPresence(scriptContents, key)) { paypalKeys.add(key); } } // If no keys were found, return failed result if (stripeKeys.size === 0 && paypalKeys.size === 0) { const error = 'No publishable keys found on the current page.'; return { isSuccessful: false, forLlm: error, metadata: { error: error, }, }; } else { // Signal to end the flow context.metadata.nextState = 'SUCCESS'; const result = { stripe: Array.from(stripeKeys), paypal: Array.from(paypalKeys), checkoutPageUrl: page.url(), }; return { isSuccessful: true, forLlm: JSON.stringify(result, null, 2), metadata: result, }; } } async callFromGpt(context, parameters) { return this.call(context, parameters); } verifyPayPalPresence(scriptContents, key) { const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const contextPattern = new RegExp(`PayPal[\\s\\S]*?${escapedKey}|${escapedKey}[\\s\\S]*?PayPal|PayPal[\\s\\S]*?client_id\\s*[:=]\\s*['"]${escapedKey}['"]`, 'i'); return contextPattern.test(scriptContents); } } exports.ExtractPaymentProviderKeyTool = ExtractPaymentProviderKeyTool; ExtractPaymentProviderKeyTool.NAME = 'extractPaymentProviderKey'; //# sourceMappingURL=ExtractPaymentProviderKeyTool.js.map