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, OpenRouter, and custom OpenAI-compatible APIs

1,699 lines • 71.4 kB
#!/usr/bin/env node

// src/index.ts
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";

// src/common.ts
import { randomUUID } from "crypto";
import { isDeepStrictEqual } from "util";
import fs from "fs";
function validateCtrfFile(filePath) {
  try {
    const fileContent = fs.readFileSync(filePath, "utf8");
    const jsonData = JSON.parse(fileContent);
    if (jsonData.results?.summary == null || jsonData.results.tests == null) {
      console.warn("Warning: The file does not contain valid CTRF data.");
      return null;
    }
    return jsonData;
  } catch (error) {
    console.error("Failed to read or process the file:", error);
    return null;
  }
}
function saveUpdatedReport(filePath, report) {
  try {
    const previous = fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, "utf8")) : void 0;
    if (!isDeepStrictEqual(previous, report)) {
      report.reportId = randomUUID();
      report.timestamp = (/* @__PURE__ */ new Date()).toISOString();
    }
    fs.writeFileSync(filePath, JSON.stringify(report, null, 2), "utf8");
    console.log(`Updated report saved to ${filePath}`);
  } catch (error) {
    console.error("Failed to save the updated report:", error);
  }
}
function ansiRegex({ onlyFirst = false } = {}) {
  const pattern = [
    "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
    "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))"
  ].join("|");
  return new RegExp(pattern, onlyFirst ? void 0 : "g");
}
function stripAnsi(message) {
  if (typeof message !== "string") {
    throw new TypeError(`Expected a \`string\`, got \`${typeof message}\``);
  }
  return message.replace(ansiRegex(), "");
}

// src/constants.ts
var FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT = `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:
- It is critical that you do not alter or interpret the error message or stack trace; instead, focus on analyzing the exact content provided.
- 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"
- keep the tone conversational and natural.

Avoid:
- Including any code in your response.
- Adding generic conclusions or advice such as "By following these steps..."
- headings, bullet points, or special formatting.`;

// src/models/openai.ts
import OpenAI6 from "openai";

// src/models/claude.ts
import { Anthropic } from "@anthropic-ai/sdk";
async function claudeAI(systemPrompt, prompt, args) {
  const client = new Anthropic({
    apiKey: process.env.ANTHROPIC_API_KEY
  });
  try {
    const response = await client.messages.create({
      system: systemPrompt,
      messages: [{ role: "user", content: stripAnsi(prompt) }],
      max_tokens: args.maxTokens ?? 300,
      model: args.model ?? "claude-3-5-sonnet-20240620",
      temperature: args.temperature ?? 1
    });
    const aiResponseArray = response.content;
    const aiResponse = aiResponseArray.filter((block) => block.type === "text").map((block) => block.text).join(" ");
    return aiResponse !== "" ? aiResponse : null;
  } catch (error) {
    console.error(`Error invoking Claude AI`, error);
    return null;
  }
}
async function claudeFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await claudeAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          `
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`
        );
        console.log(`\u2728 AI Test Reporter Summary`);
        console.log(
          `\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "claude", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/azure-openai.ts
import { AzureOpenAI } from "openai";
async function azureOpenAI(systemPrompt, prompt, args) {
  const apiKey = process.env.AZURE_OPENAI_API_KEY;
  const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
  const deployment = args.deploymentId ?? process.env.AZURE_OPENAI_DEPLOYMENT_NAME;
  if (apiKey == null || apiKey === "" || endpoint == null || endpoint === "" || deployment == null || deployment === "") {
    console.error(
      "Missing Azure OpenAI configuration. Please set AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, and AZURE_OPENAI_DEPLOYMENT_NAME environment variables or provide them as arguments."
    );
    return null;
  }
  const client = new AzureOpenAI({
    apiKey,
    endpoint,
    apiVersion: "2024-05-01-preview"
  });
  try {
    const response = await client.chat.completions.create({
      model: deployment,
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking Azure OpenAI`, error);
    return null;
  }
}
async function azureOpenAIFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await azureOpenAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "azure", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/grok.ts
import OpenAI from "openai";
async function grokAI(systemPrompt, prompt, args) {
  const client = new OpenAI({
    apiKey: process.env.GROK_API_KEY,
    baseURL: process.env.GROK_API_BASE_URL ?? "https://api.x.ai/v1"
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "grok-beta",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking Grok`, error);
    return null;
  }
}
async function grokFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await grokAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "grok", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/deepseek.ts
import OpenAI2 from "openai";
async function deepseekAI(systemPrompt, prompt, args) {
  const client = new OpenAI2({
    apiKey: process.env.DEEPSEEK_API_KEY,
    baseURL: process.env.DEEPSEEK_API_BASE_URL ?? "https://api.deepseek.com/v1"
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "deepseek-reasoner",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking DeepSeek`, error);
    return null;
  }
}
async function deepseekFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await deepseekAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "deepseek", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/gemini.ts
import { GoogleGenerativeAI } from "@google/generative-ai";
async function gemini(systemPrompt, prompt, args) {
  const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY ?? "");
  const model = genAI.getGenerativeModel({ model: args.model ?? "gemini-pro" });
  try {
    const combinedPrompt = `${systemPrompt}

${stripAnsi(prompt)}`;
    const result = await model.generateContent(combinedPrompt);
    const response = result.response;
    const text = response.text();
    return text !== "" ? text : null;
  } catch (error) {
    console.error(`Error invoking Gemini`, error);
    return null;
  }
}
async function geminiFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await gemini(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "gemini", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/perplexity.ts
import OpenAI3 from "openai";
async function perplexity(systemPrompt, prompt, args) {
  const client = new OpenAI3({
    apiKey: process.env.PERPLEXITY_API_KEY,
    baseURL: "https://api.perplexity.ai"
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "llama-3.1-sonar-small-128k-online",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking Perplexity`, error);
    return null;
  }
}
async function perplexityFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await perplexity(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "perplexity", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/openrouter.ts
import OpenAI4 from "openai";
async function openRouter(systemPrompt, prompt, args) {
  const client = new OpenAI4({
    apiKey: process.env.OPENROUTER_API_KEY,
    baseURL: "https://openrouter.ai/api/v1",
    defaultHeaders: {
      "HTTP-Referer": process.env.OPENROUTER_REFERER ?? "http://localhost:3000",
      "X-Title": "AI Test Reporter"
    }
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "openai/gpt-3.5-turbo",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? void 0,
      temperature: args.temperature ?? void 0,
      ...args.frequencyPenalty !== void 0 && {
        frequency_penalty: args.frequencyPenalty
      },
      ...args.presencePenalty !== void 0 && {
        presence_penalty: args.presencePenalty
      },
      ...args.topP !== void 0 && { top_p: args.topP }
    });
    if (response?.choices?.[0]?.message?.content == null) {
      return null;
    }
    return response.choices[0].message.content;
  } catch (error) {
    console.error(`Error invoking OpenRouter`, error);
    return null;
  }
}
async function openRouterFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await openRouter(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "openrouter", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/bedrock.ts
async function bedrock(systemPrompt, prompt, args) {
  const region = process.env.AWS_REGION ?? "us-west-2";
  const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
  const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
  const sessionToken = process.env.AWS_SESSION_TOKEN;
  if (accessKeyId == null || secretAccessKey == null) {
    console.error(
      "AWS credentials not found. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables."
    );
    return null;
  }
  try {
    const { BedrockRuntimeClient, InvokeModelCommand } = await import("@aws-sdk/client-bedrock-runtime");
    const client = new BedrockRuntimeClient({
      region,
      credentials: {
        accessKeyId,
        secretAccessKey,
        ...sessionToken != null && { sessionToken }
      }
    });
    const modelId = args.model ?? "anthropic.claude-3-5-sonnet-20240620-v1:0";
    const body = JSON.stringify({
      anthropic_version: "bedrock-2023-05-31",
      max_tokens: args.maxTokens ?? 4e3,
      system: systemPrompt,
      messages: [
        {
          role: "user",
          content: stripAnsi(prompt)
        }
      ],
      ...args.temperature != null && { temperature: args.temperature },
      ...args.topP != null && { top_p: args.topP }
    });
    const command = new InvokeModelCommand({
      modelId,
      body,
      contentType: "application/json",
      accept: "application/json"
    });
    const response = await client.send(command);
    const responseBody = JSON.parse(new TextDecoder().decode(response.body));
    if (responseBody.content?.[0]?.text != null) {
      return responseBody.content[0].text;
    }
    return null;
  } catch (error) {
    console.error("Error invoking Bedrock:", error);
    return null;
  }
}
async function bedrockFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await bedrock(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "bedrock", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/custom.ts
import OpenAI5 from "openai";
async function customService(systemPrompt, prompt, args, customUrl) {
  const baseURL = customUrl ?? args.customUrl ?? process.env.AI_CTRF_CUSTOM_URL;
  if (baseURL == null) {
    console.error(
      "Error: Custom URL is required. Please provide it via --url option, customUrl parameter, or AI_CTRF_CUSTOM_URL environment variable."
    );
    return null;
  }
  const apiKey = process.env.AI_CTRF_CUSTOM_API_KEY ?? process.env.OPENAI_API_KEY ?? "not-needed";
  const client = new OpenAI5({
    apiKey,
    baseURL
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "gpt-4o",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking Custom API`, error);
    return null;
  }
}
async function customFailedTestSummary(report, args, file2, _log = false, customUrl) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await customService(systemPrompt, prompt, args, customUrl);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          `
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`
        );
        console.log(`\u2728 AI Test Reporter Summary`);
        console.log(
          `\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "custom", args, customUrl);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/consolidated-summary.ts
async function generateConsolidatedSummary(report, model, args, customUrl) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  const aiSummaries = [];
  for (const test of failedTests) {
    if (test.ai != null && test.ai.trim() !== "") {
      aiSummaries.push(`Test Name: ${test.name}
AI Summary: ${test.ai}
`);
    }
  }
  let systemPrompt = `You are tasked with summarizing the results of a test run that contains test failures. Your goal is to provide a concise, high-level overview of what went wrong in the test run. Focus on identifying patterns or root causes that might explain why these tests failed. Keep the summary brief and informative, without repeating the test details or providing step-by-step instructions. Avoid unnecessary verbosity and focus on delivering actionable insights.   
                Avoid:
                 - Including any code in your response.
                 - Adding generic conclusions or advice such as "By following these steps..."
                 - headings, bullet points, or special formatting.`;
  if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
    systemPrompt += `

${args.additionalSystemPromptContext}`;
  }
  let consolidatedPrompt = `The following tests failed in the suite:

${aiSummaries.join("\n")}

A total of ${failedTests.length} tests failed in this test suite. Please provide a high-level summary of what went wrong across the suite and suggest what might be the root causes or patterns.`;
  if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
    consolidatedPrompt += `

Additional Context:
${args.additionalPromptContext}`;
  }
  let consolidatedSummary = "";
  if (model === "openai") {
    consolidatedSummary = await openAI(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "claude") {
    consolidatedSummary = await claudeAI(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "azure") {
    consolidatedSummary = await azureOpenAI(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "grok") {
    consolidatedSummary = await grokAI(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "deepseek") {
    consolidatedSummary = await deepseekAI(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "gemini") {
    consolidatedSummary = await gemini(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "perplexity") {
    consolidatedSummary = await perplexity(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "openrouter") {
    consolidatedSummary = await openRouter(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "bedrock") {
    consolidatedSummary = await bedrock(systemPrompt, consolidatedPrompt, args) ?? "";
  } else if (model === "custom") {
    consolidatedSummary = await customService(
      systemPrompt,
      consolidatedPrompt,
      args,
      customUrl
    ) ?? "";
  }
  if (consolidatedSummary !== "") {
    if (args.log === true) {
      console.log(
        `
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`
      );
      console.log(`\u{1F4DD} Overall Summary:
`);
      console.log(`${consolidatedSummary}
`);
    }
    report.results.extra = report.results.extra ?? {};
    report.results.extra.ai = consolidatedSummary;
  }
}

// src/models/openai.ts
async function openAI(systemPrompt, prompt, args) {
  const client = new OpenAI6({
    apiKey: process.env.OPENAI_API_KEY
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "gpt-4o",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking OpenAI`, error);
    return null;
  }
}
async function openAIFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await openAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          `
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`
        );
        console.log(`\u2728 AI Test Reporter Summary`);
        console.log(
          `\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "openai", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/json-summary.ts
async function generateJsonSummary(report, model, args, customUrl) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  const testDetails = [];
  let testCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && testCount >= args.maxMessages) {
      break;
    }
    let detail = `Test Name: ${test.name}
`;
    if (test.message != null && test.message.trim() !== "") {
      detail += `Message: ${test.message}
`;
    }
    if (test.trace != null && test.trace.trim() !== "") {
      detail += `Trace: ${test.trace}
`;
    }
    if (test.ai != null && test.ai.trim() !== "") {
      detail += `AI Summary: ${test.ai}
`;
    }
    testDetails.push(detail);
    testCount++;
  }
  let systemPrompt = `You are a test analysis expert. Your task is to analyze test failures and provide structured output in JSON format.
Avoid:
 - Including any code in your response.
 - Adding generic conclusions or advice such as "By following these steps..."
 - headings, bullet points, or special formatting.

You must respond ONLY with valid JSON in the following structure:
{
  "summary": "High-level overview of what went wrong",
  "code_issues": "Detailed description of code-related issues that occurred",
  "timeout_issues": "Detailed description of timeout and performance issues",
  "application_issues": "Detailed description of application-level issues",
  "recommendations": "Detailed recommendations for fixing the issues"
}

Guidelines:
- "summary": Provide a concise overview of the test run failures
- "code_issues": Describe specific code-related problems (bugs, logic errors, assertion failures, etc.) in a paragraph. If there are no code-related issues, use an empty string.
- "timeout_issues": Describe any timeout, performance, or timing-related issues in a paragraph. If there are no timeout-related issues, use an empty string.
- "application_issues": Describe application-level problems (configuration, environment, dependencies, etc.) in a paragraph. If there are no application-related issues, use an empty string.
- "recommendations": Provide actionable recommendations to fix the issues in a paragraph. If there are no recommendations, use an empty string.

Important:
- Each field should be a string
- If no issues exist for a category, use an empty string ""
- Keep descriptions clear and actionable
- Do not include code snippets
- Respond ONLY with the JSON object, no additional text`;
  if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
    systemPrompt += `

Additional Context:
${args.additionalSystemPromptContext}`;
  }
  let analysisPrompt = `Analyze the following test failures and provide a structured JSON response.

Test Environment: ${report.results.environment != null ? JSON.stringify(report.results.environment) : "Not specified"}
Test Tool: ${report.results.tool.name}
Total Tests Run: ${report.results.tests.length}
Failed Tests: ${failedTests.length}
Passed Tests: ${report.results.summary.passed}
${testCount < failedTests.length ? `
Note: Showing ${testCount} of ${failedTests.length} failed tests to stay within token limits.
` : ""}
Failed Test Details:
${testDetails.join("\n")}

Provide your analysis in the specified JSON format.`;
  if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
    analysisPrompt += `

Additional Context:
${args.additionalPromptContext}`;
  }
  let jsonResponse = "";
  if (model === "openai") {
    jsonResponse = await openAI(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "claude") {
    jsonResponse = await claudeAI(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "azure") {
    jsonResponse = await azureOpenAI(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "grok") {
    jsonResponse = await grokAI(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "deepseek") {
    jsonResponse = await deepseekAI(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "gemini") {
    jsonResponse = await gemini(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "perplexity") {
    jsonResponse = await perplexity(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "openrouter") {
    jsonResponse = await openRouter(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "bedrock") {
    jsonResponse = await bedrock(systemPrompt, analysisPrompt, args) ?? "";
  } else if (model === "custom") {
    jsonResponse = await customService(systemPrompt, analysisPrompt, args, customUrl) ?? "";
  }
  if (jsonResponse === "") {
    console.error("Failed to generate JSON summary: empty response from model");
    return null;
  }
  try {
    let cleanedResponse = jsonResponse.trim();
    if (cleanedResponse.startsWith("```json")) {
      cleanedResponse = cleanedResponse.replace(/^```json\n?/, "").replace(/\n?```$/, "");
    } else if (cleanedResponse.startsWith("```")) {
      cleanedResponse = cleanedResponse.replace(/^```\n?/, "").replace(/\n?```$/, "");
    }
    const parsedResponse = JSON.parse(cleanedResponse);
    if (typeof parsedResponse.summary !== "string" || typeof parsedResponse.code_issues !== "string" || typeof parsedResponse.timeout_issues !== "string" || typeof parsedResponse.application_issues !== "string" || typeof parsedResponse.recommendations !== "string") {
      throw new Error("Invalid JSON structure returned from model");
    }
    if (args.log === true) {
      console.log(
        `
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
`
      );
      console.log(`\u{1F4CA} Structured Analysis Summary:
`);
      console.log(`\u{1F4DD} Summary:
${parsedResponse.summary}
`);
      if (parsedResponse.code_issues.trim() !== "") {
        console.log(`\u{1F41B} Code Issues:
${parsedResponse.code_issues}
`);
      }
      if (parsedResponse.timeout_issues.trim() !== "") {
        console.log(`\u23F1\uFE0F  Timeout Issues:
${parsedResponse.timeout_issues}
`);
      }
      if (parsedResponse.application_issues.trim() !== "") {
        console.log(
          `\u2699\uFE0F  Application Issues:
${parsedResponse.application_issues}
`
        );
      }
      if (parsedResponse.recommendations.trim() !== "") {
        console.log(`\u{1F4A1} Recommendations:
${parsedResponse.recommendations}
`);
      }
    }
    return parsedResponse;
  } catch (error) {
    console.error("Failed to parse JSON response from model:", error);
    console.error("Raw response:", jsonResponse);
    return null;
  }
}

// src/models/mistral.ts
import OpenAI7 from "openai";
async function mistralAI(systemPrompt, prompt, args) {
  const client = new OpenAI7({
    apiKey: process.env.MISTRAL_API_KEY,
    baseURL: "https://api.mistral.ai/v1"
  });
  try {
    const response = await client.chat.completions.create({
      model: args.model ?? "mistral-large-latest",
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: stripAnsi(prompt) }
      ],
      max_tokens: args.maxTokens ?? null,
      frequency_penalty: args.frequencyPenalty,
      presence_penalty: args.presencePenalty,
      ...args.temperature !== void 0 ? { temperature: args.temperature } : {},
      ...args.topP !== void 0 ? { top_p: args.topP } : {}
    });
    return response.choices[0].message?.content ?? null;
  } catch (error) {
    console.error(`Error invoking Mistral`, error);
    return null;
  }
}
async function mistralFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await mistralAI(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "mistral", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/models/ollama.ts
async function ollama(systemPrompt, prompt, args) {
  const baseURL = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
  try {
    const response = await fetch(`${baseURL}/api/generate`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        model: args.model ?? "llama2",
        prompt: `${systemPrompt}

${stripAnsi(prompt)}`,
        stream: false
      })
    });
    const data = await response.json();
    return data.response ?? null;
  } catch (error) {
    console.error("Error invoking Ollama", error);
    return null;
  }
}
async function ollamaFailedTestSummary(report, args, file2, _log = false) {
  const failedTests = report.results.tests.filter(
    (test) => test.status === "failed"
  );
  failedTests.forEach((test) => {
    if (test.extra != null) {
      delete test.extra;
    }
  });
  let logged = false;
  let messageCount = 0;
  for (const test of failedTests) {
    if (args.maxMessages != null && messageCount >= args.maxMessages) {
      break;
    }
    let prompt = `Report:
${JSON.stringify(test, null, 2)}.

Tool:${report.results.tool.name}.

 Please provide a human-readable failure summary that explains why you think the test might have failed and ways to fix`;
    if (args.additionalPromptContext != null && args.additionalPromptContext !== "") {
      prompt += `

Additional Context:
${args.additionalPromptContext}`;
    }
    let systemPrompt = args.systemPrompt ?? FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT;
    if (args.additionalSystemPromptContext != null && args.additionalSystemPromptContext !== "") {
      systemPrompt += `

${args.additionalSystemPromptContext}`;
    }
    const response = await ollama(systemPrompt, prompt, args);
    if (response != null) {
      test.ai = response;
      messageCount++;
      if (args.log === true && !logged) {
        console.log(
          "\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"
        );
        console.log("\u2728 AI Test Reporter Summary");
        console.log(
          "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n"
        );
        logged = true;
      }
      if (args.log === true) {
        console.log(`\u274C Failed Test: ${test.name}
`);
        console.log(`${response}
`);
      }
    }
  }
  if (args.consolidate === true) {
    await generateConsolidatedSummary(report, "ollama", args);
  }
  if (file2 !== void 0) {
    saveUpdatedReport(file2, report);
  }
  return report;
}

// src/index.ts
var argv = yargs(hideBin(process.argv)).command(
  "openai <file>",
  "Generate test summary from a CTRF report",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "OpenAI model to use",
      type: "string",
      default: "gpt-4o"
    });
  }
).command(
  "claude <file>",
  "Generate test summary from a CTRF report",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Claude model to use",
      type: "string",
      default: "claude-3-5-sonnet-20240620"
    });
  }
).command(
  "ollama <file>",
  "Generate test summary from a CTRF report using Ollama",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Ollama model to use",
      type: "string",
      default: "llama2"
    });
  }
).command(
  "azure-openai <file>",
  "Generate test summary from a CTRF report using Azure OpenAI",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("deploymentId", {
      describe: "Deployment ID for Azure OpenAI",
      type: "string"
    }).option("model", {
      describe: "Model to use",
      type: "string",
      default: "gpt-4o"
    });
  }
).command(
  "grok <file>",
  "Generate test summary from a CTRF report using Grok",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Grok model to use",
      type: "string",
      default: "grok-2-latest"
    });
  }
).command(
  "deepseek <file>",
  "Generate test summary from a CTRF report using DeepSeek",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "DeepSeek model to use",
      type: "string",
      default: "deepseek-reasoner"
    });
  }
).command(
  "mistral <file>",
  "Generate test summary from a CTRF report using Mistral",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Mistral model to use",
      type: "string",
      default: "mistral-medium"
    });
  }
).command(
  "gemini <file>",
  "Generate test summary from a CTRF report using Google Gemini",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Gemini model to use",
      type: "string",
      default: "gemini-pro"
    });
  }
).command(
  "perplexity <file>",
  "Generate test summary from a CTRF report using Perplexity",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Perplexity model to use",
      type: "string",
      default: "pplx-7b-online"
    });
  }
).command(
  "openrouter <file>",
  "Generate test summary from a CTRF report using OpenRouter",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "OpenRouter model to use",
      type: "string",
      default: "anthropic/claude-3-opus"
    });
  }
).command(
  "bedrock <file>",
  "Generate test summary from a CTRF report using Amazon Bedrock Claude",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("model", {
      describe: "Bedrock model to use",
      type: "string",
      default: "anthropic.claude-3-5-sonnet-20240620-v1:0"
    });
  }
).command(
  "custom <file>",
  "Generate test summary from a CTRF report using a custom OpenAI-compatible API",
  (yargs2) => {
    return yargs2.positional("file", {
      describe: "Path to the CTRF file",
      type: "string"
    }).option("url", {
      describe: "Base URL for the custom OpenAI-compatible API (e.g., http://localhost:8080/v1)",
      type: "string",
      demandOption: false
    }).option("model", {
      describe: "Model to use",
      type: "string",
      default: "gpt-4o"
    });
  }
).option("systemPrompt", {
  describe: "System prompt to guide the AI",
  type: "string",
  default: FAILED_TEST_SUMMARY_SYSTEM_PROMPT_CURRENT
}).option("additionalSystemPromptContext", {
  describe: "Additional context to append to the system prompt",
  type: "string"
}).option("additionalPromptContext", {
  describe: "Additional context to append to the user prompt",
  type: "string"
}).option("frequencyPenalty", {
  describe: "Frequency penalty parameter for the model",
  type: "number",
  default: 0
}).option("maxTokens", {
  describe: "Maximum number of tokens to generate",
  type: "number"
}).option("presencePenalty", {
  describe: "Presence penalty parameter for the model",
  type: "number",
  default: 0
}).option("temperature", {
  describe: "Sampling temperature",
  type: "number",
  conflicts: "topP"
}).option("topP", {
  describe: "Top-p sampling parameter",
  type: "number",
  conflicts: "temperature"
}).option("log", {
  describe: "Log the AI responses to the console",
  type: "boolean",
  default: true
}).option("maxMessages", {
  describe: "Limit the number of failing tests to send for summarization in the LLM request. This helps avoid overwhelming the model when dealing with reports that have many failing tests.",
  type: "number",
  default: 10
}).option("consolidate", {
  describe: "Consolidate and summarize multiple AI summaries into a higher-level overview",
  type: "boolean",
  default: true
}).option("json-analysis", {
  describe: "Generate structured JSON analysis with categorized issues (code, timeout, application) and recommendations",
  type: "boolean",
  default: false
}).help().alias("help", "h").parseSync();
var file = argv.file ?? "ctrf/ctrf-report.json";
var executeCommand = async (command, modelFunction) => {
  if (argv._.includes(command) && argv.file != null) {
    try {
      const report = validateCtrfFile(argv.file);
      if (report !== null) {
        await modelFunction(report, argv, file, true);
        if (argv.jsonAnalysis === true) {
          const result = await generateJsonSummary(
            report,
            command,
            argv,
            argv.customUrl
          );
          if (result !== null) {
            console.log(JSON.stringify(result, null, 2));
          }
        }
      }
    } catch (error) {
      console.error("Failed to read file:", error);
    }
  }
};
if (argv.url != null) {
  argv.customUrl = argv.url;
}
void Promise.resolve().then(async () => {
  await executeCommand("openai", openAIFailedTestSummary);
  await executeCommand("claude", claudeFailedTestSummary);
  await executeCommand("azure-openai", azureOpenAIFailedTestSummary);
  await executeCommand("grok", grokFailedTestSummary);
  await executeCommand("deepseek", deepseekFailedTestSummary);
  await executeCommand("mistral", mistralFailedTestSummary);
  await executeCommand("gemini", geminiFailedTestSummary);
  await executeCommand("perplexity", perplexityFailedTestSummary);
  await executeCommand("openrouter", openRouterFailedTestSummary);
  await executeCommand("bedrock", bedrockFailedTestSummary);
  await executeCommand("ollama", ollamaFailedTestSummary);
  await executeCommand("custom", customFailedTestSummary);
});
export {
  azureOpenAIFailedTestSummary,
  bedrockFailedTestSummary,
  claudeFailedTestSummary,
  customFailedTestSummary,
  deepseekFailedTestSummary,
  geminiFailedTestSummary,
  generateJsonSummary,
  grokFailedTestSummary,
  mistralFailedTestSummary,
  ollamaFailedTestSummary,
  openAIFailedTestSummary,
  openRouterFailedTestSummary,
  perplexityFailedTestSummary
};