UNPKG

locatai-ts

Version:

Enterprise-grade AI-powered element locator for Selenium WebDriver - TypeScript implementation

179 lines (177 loc) 6.86 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AnthropicProvider = void 0; const sdk_1 = require("@anthropic-ai/sdk"); const readline = __importStar(require("readline")); class AnthropicProvider { constructor(config) { const apiKey = config?.apiKey; if (!apiKey) { throw new Error('API key is not set. Please check your appsettings.json file'); } this.model = config?.model || 'claude-3-opus-20240229'; this.client = new sdk_1.Anthropic({ apiKey }); } createInterface() { return readline.createInterface({ input: process.stdin, output: process.stdout }); } async getUserInput(prompt) { const rl = this.createInterface(); return new Promise((resolve) => { rl.question(prompt, (answer) => { rl.close(); resolve(answer); }); }); } async generateResponse(userInput, systemPrompt) { try { // Optimize the prompt to reduce token consumption when we have HTML let optimizedInput = userInput; if (userInput.includes('DOM Content:')) { optimizedInput = this.optimizeHtmlContent(userInput); } // Create the message request with system prompt if provided const messageRequest = { model: this.model, max_tokens: 1024, temperature: 0.7, messages: [ { role: "user", content: optimizedInput } ] }; // Add system prompt if provided if (systemPrompt) { messageRequest.system = systemPrompt; } const message = await this.client.messages.create(messageRequest); if (message.content[0].type === 'text') { const response = message.content[0].text; return this.cleanLocatorResponse(response); } throw new Error('Unexpected response type from Anthropic API'); } catch (error) { console.error('Error calling Anthropic API:', error); throw error; } } async interact() { console.log(`Claude ${this.model} Chat (Type 'exit' to quit)`); while (true) { const userInput = await this.getUserInput("\nYou: "); if (userInput.toLowerCase() === 'exit') { console.log("Goodbye!"); break; } console.log("\nThinking..."); try { const response = await this.generateResponse(userInput); console.log(`\nAI: ${response}`); } catch (error) { console.log("Sorry, there was an error generating a response."); } } } /** * Optimizes HTML content in the prompt to reduce token usage */ optimizeHtmlContent(prompt) { // Extract element description from the prompt const elementMatch = prompt.match(/Generate a locator for: (.+?)$/m); if (!elementMatch) return prompt; const elementDescription = elementMatch[1]; // Extract HTML content between DOM Content: and Generate a locator const match = prompt.match(/DOM Content:\s*(.+?)(?=\s*\n\s*Generate a locator)/s); if (match && match[1]) { const fullHtml = match[1]; // Truncate HTML to reduce token count let truncatedHtml = fullHtml; if (fullHtml.length > 5000) { truncatedHtml = fullHtml.substring(0, 5000) + "..."; } return `DOM Content (truncated for efficiency): ${truncatedHtml} Generate a locator for: ${elementDescription} Return the locator in this format: locatorType=value|confidence=0.XX For example: id=username|confidence=0.9 You can provide multiple locators with confidence scores.`; } return prompt; } /** * Cleans and standardizes the locator response */ cleanLocatorResponse(response) { if (!response) return ''; // Remove markdown code formatting and explanations let cleaned = response.replace(/```[\s\S]*?```/g, ''); cleaned = cleaned.replace(/`/g, ''); // Split by lines and keep only those that seem to contain locators const lines = cleaned.split('\n').filter(line => { const trimmed = line.trim(); return (trimmed.includes('=') || trimmed.includes(':') || /^[a-z]+ selector/i.test(trimmed)); }); // Convert "xpath: //div" format to "xpath=//div" if needed const standardized = lines.map(line => { if (line.includes(':') && !line.includes('=')) { return line.replace(/([a-z-]+):\s+/gi, '$1='); } return line; }); // Add confidence scores to lines that don't have them const withConfidence = standardized.map(line => { if (!line.includes('confidence=') && !line.includes('|')) { return `${line}|confidence=0.8`; } return line; }); return withConfidence.join('\n'); } } exports.AnthropicProvider = AnthropicProvider; //# sourceMappingURL=AnthropicProvider.js.map