UNPKG

@craftapit/tester

Version:

A focused, LLM-powered testing framework for natural language test scenarios

160 lines (151 loc) 5.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenAIAdapter = void 0; const BaseAdapter_1 = require("./BaseAdapter"); const openai_1 = __importDefault(require("openai")); class OpenAIAdapter extends BaseAdapter_1.BaseAdapter { constructor(config) { super(config); this.apiKey = config.apiKey || process.env.OPENAI_API_KEY || ''; this.baseUrl = config.baseUrl || 'https://api.openai.com/v1'; this.model = config.model || 'gpt-4'; this.client = new openai_1.default({ apiKey: this.apiKey, baseURL: this.baseUrl }); } async initialize() { console.log('Initializing OpenAI adapter'); if (!this.apiKey) { throw new Error('OpenAI API key is required'); } } async cleanup() { console.log('Cleaning up OpenAI adapter'); // Nothing to clean up } /** * Complete a prompt with the LLM * @param prompt The prompt to complete * @returns The completion text */ async complete(prompt) { console.log(`Completing prompt with OpenAI: ${prompt.substring(0, 50)}...`); try { const response = await this.client.chat.completions.create({ model: this.model, messages: [{ role: 'user', content: prompt }], temperature: 0.2 }); const content = response.choices[0].message.content; if (!content) { throw new Error('Empty response from OpenAI API'); } return content; } catch (error) { console.error('Error calling OpenAI API:', error); throw new Error(`Failed to get completion from OpenAI: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async suggestAction(instruction, screenState) { console.log(`Suggesting action for: ${instruction}`); const prompt = ` You are an AI assistant helping to automate UI testing. Given the following instruction from a test scenario: "${instruction}" And the current screen state: ${JSON.stringify(screenState, null, 2)} Determine the most appropriate action to take. Return a JSON object with: - actionType: "click", "input", "navigate", "wait", "assert", etc. - target: Element to interact with (if applicable) - value: Value to input (if applicable) - reasoning: Why this action was chosen - confidence: A number between 0 and 1 indicating your confidence Response (JSON only): `; try { const response = await this.client.chat.completions.create({ model: this.model, messages: [{ role: 'user', content: prompt }], temperature: 0.2, response_format: { type: 'json_object' } }); const content = response.choices[0].message.content; if (!content) { throw new Error('Empty response from OpenAI API'); } try { const actionJson = JSON.parse(content); return { actionType: actionJson.actionType, target: actionJson.target, value: actionJson.value, reasoning: actionJson.reasoning, confidence: actionJson.confidence }; } catch (parseError) { throw new Error(`Could not parse JSON from OpenAI response: ${parseError instanceof Error ? parseError.message : String(parseError)}`); } } catch (error) { console.error('Error calling OpenAI API:', error); // Fallback action return { actionType: 'click', target: { text: 'Submit' }, reasoning: 'Fallback action due to API error', confidence: 0.5 }; } } async verifyCondition(condition, screenState) { console.log(`Verifying condition: ${condition}`); const prompt = ` You are an AI assistant helping to automate UI testing. Given the following condition to verify: "${condition}" And the current screen state: ${JSON.stringify(screenState, null, 2)} Determine if the condition is met. Return a JSON object with: - success: true or false - reason: Explanation of why the condition is met or not met Response (JSON only): `; try { const response = await this.client.chat.completions.create({ model: this.model, messages: [{ role: 'user', content: prompt }], temperature: 0.2, response_format: { type: 'json_object' } }); const content = response.choices[0].message.content; if (!content) { throw new Error('Empty response from OpenAI API'); } try { const resultJson = JSON.parse(content); return { success: resultJson.success, reason: resultJson.reason }; } catch (parseError) { throw new Error(`Could not parse JSON from OpenAI response: ${parseError instanceof Error ? parseError.message : String(parseError)}`); } } catch (error) { console.error('Error calling OpenAI API:', error); // Fallback verification return { success: true, reason: 'Fallback verification due to API error' }; } } } exports.OpenAIAdapter = OpenAIAdapter;