UNPKG

ai-ctrf

Version:

Generate AI summaries of test results using a wide range of AI models like OpenAI, Anthropic, Gemini, Mistral, Grok, DeepSeek, Azure, Perplexity, and OpenRouter

58 lines (57 loc) 3.92 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.openAISummary = openAISummary; const openai_1 = __importDefault(require("openai")); const common_1 = require("./common"); function openAISummary(report, file, args) { return __awaiter(this, void 0, void 0, function* () { var _a; const client = new openai_1.default({ apiKey: process.env.OPENAI_API_KEY, }); const failedTests = report.results.tests.filter(test => test.status === 'failed'); for (const test of failedTests) { const systemPrompt = args.systemPrompt || `You will receive a CTRF report test object containing an error message and a stack trace. Your task is to generate a clear and concise summary of the failure, specifically designed to assist a human in debugging the issue. The summary should: - Identify the likely cause of the failure based on the provided information. - Suggest specific steps for resolution directly related to the failure. - Start the summary with "The test failed because" - It is critical that you do not alter or interpret the error message or stack trace; instead, focus on analyzing the exact content provided. Avoid: - Including any code in your response. - Adding generic conclusions or advice such as "By following these steps..." - Using headings, bullet points, or markdown-like formatting. - Writing in a way that resembles a technical document; instead, keep the tone conversational and natural.`; const prompt = `Report:\n${JSON.stringify(test, null, 2)}.\n\nTool:${report.results.tool.name}.\n\n Please provide a human-readable failure summary that identifies the cause and suggests specific debugging steps.`; try { const response = yield client.chat.completions.create(Object.assign(Object.assign({ model: args.model || "gpt-3.5-turbo", messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: prompt } ], max_tokens: args.maxTokens || null, frequency_penalty: args.frequencyPenalty, presence_penalty: args.presencePenalty }, (args.temperature !== undefined ? { temperature: args.temperature } : {})), (args.topP !== undefined ? { top_p: args.topP } : {}))); const aiResponse = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content; if (aiResponse) { test.ai = aiResponse; if (args.log) { console.log(`AI summary for test: ${test.name}\n`, aiResponse); } } } catch (error) { console.error(`Error generating summary for test ${test.name}:`, error); test.ai = 'Failed to generate summary due to an error.'; } } (0, common_1.saveUpdatedReport)(file, report); }); }