import fs from "fs" import path from "path" import { fileURLToPath } from "url" import { dirname } from "path" const __filename = fileURLToPath(import.meta.url) const __dirname = dirname(__filename) const PR_HEADER_TEMPLATE = fs.readFileSync( path.join(__dirname, "optimization_results_header.md"), "utf8", ) const PR_DETAILS_TEMPLATE = fs.readFileSync( path.join(__dirname, "optimization_results_details.md"), "utf8", ) const PR_TEST_REPORT_TEMPLATE = fs.readFileSync( path.join(__dirname, "optimization_results_test_report.md"), "utf8", ) const summaryLinesRegex = new RegExp( /### 📄 ([\d,]+(\.\d+)?)% \(([\d,]+(\.\d+)?)x\) speedup for \*\*\*`(.+?)` in `(.+?)`\*\*\*/g, ) type ReportTable = Record< string, { passed: number failed: number } > type PrCommentFields = Record export function buildPrCommentBody( prCommentFields: PrCommentFields, existingTests: any, generatedTests: any, coverage_message: any, newBranchName: string, ): string { return ( `## ⚡️ Codeflash found optimizations for this PR\n` + `${buildResultHeader(prCommentFields)}\n` + `${buildResultDetails(prCommentFields)}\n` + `${buildResultTestReport(prCommentFields, existingTests, generatedTests, coverage_message)}\n` + `${buildMergeBranchMsg(newBranchName)}\n` ) } export function buildMergeBranchMsg(newBranchName: string): string { if (newBranchName?.length > 0) { return "To test or edit this optimization locally " + "`git merge " + newBranchName + "`\n\n" } return "" } export function buildResultHeader(fields: PrCommentFields): string { return PR_HEADER_TEMPLATE.replace(/\{best_runtime}/g, fields.best_runtime) .replace(/\{original_runtime}/g, fields.original_runtime) .replace(/\{function_name}/g, fields.function_name) .replace(/\{file_path}/g, fields.file_path) .replace(/\{speedup_x}/g, fields.speedup_x) .replace(/\{speedup_pct}/g, fields.speedup_pct) .replace(/\{num_runs}/g, fields.loop_count) } export function buildResultDetails(fields: PrCommentFields): string { return ( PR_DETAILS_TEMPLATE.replace(/\{optimization_explanation}/g, fields.optimization_explanation) + "\n" ) } export function buildResultFooter(newBranchName: string): string { return ( "To edit these changes " + "`git checkout " + newBranchName + "` and push.\n\n" + `[![Codeflash](https://img.shields.io/badge/Optimized%20with-Codeflash-yellow?style=flat&color=%23ffc428&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDgwIiBoZWlnaHQ9ImF1dG8iIHZpZXdCb3g9IjAgMCA0ODAgMjgwIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTI4Ni43IDAuMzc4NDE4SDIwMS43NTFMNTAuOTAxIDE0OC45MTFIMTM1Ljg1MUwwLjk2MDkzOCAyODEuOTk5SDk1LjQzNTJMMjgyLjMyNCA4OS45NjE2SDE5Ni4zNDVMMjg2LjcgMC4zNzg0MThaIiBmaWxsPSIjRkZDMDQzIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzExLjYwNyAwLjM3ODkwNkwyNTguNTc4IDU0Ljk1MjZIMzc5LjU2N0w0MzIuMzM5IDAuMzc4OTA2SDMxMS42MDdaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzA5LjU0NyA4OS45NjAxTDI1Ni41MTggMTQ0LjI3NkgzNzcuNTA2TDQzMC4wMjEgODkuNzAyNkgzMDkuNTQ3Vjg5Ljk2MDFaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMjQyLjg3MyAxNjQuNjZMMTg5Ljg0NCAyMTkuMjM0SDMxMC44MzNMMzYzLjM0NyAxNjQuNjZIMjQyLjg3M1oiIGZpbGw9IiMwQjBBMEEiLz4KPC9zdmc+Cg==)](https://codeflash.ai)` ) } export function buildResultTestReport( fields: PrCommentFields, existingTests: string, generatedTests: string, coverage_message: string, ): string { const reportTableDict: ReportTable = fields.report_table let reportTableMd: string = "" // Initialize the markdown table reportTableMd += "| Test | Status |\n" reportTableMd += "| --------------------------- | ----------------- |\n" // Loop through each test type and construct the table rows for (const [testType, { passed = 0, failed = 0 }] of Object.entries(reportTableDict)) { if (testType.includes("🎨")) { continue } let status = "" let detailsNote = "" // Determine the status based on passed counts if (passed > 0) { status = `✅ **${passed} Passed**` } else { status = "🔘 **None Found**" } // If there are details, note that they are available below if (passed > 0 && (testType.includes("Existing") || testType.includes("Generated"))) { detailsNote = "See below" } else { detailsNote = "" } // Add the row to the markdown table reportTableMd += `| ${testType} | ${status} |\n` } reportTableMd += `|📊 Tests Coverage | ${coverage_message} |\n` // Add detailed test outputs below the table for (const [testType, { passed = 0, failed = 0 }] of Object.entries(reportTableDict)) { // Only include details for Existing and Generated tests with results if (passed > 0 && (testType.includes("Existing") || testType.includes("Generated"))) { // Add a heading for the test type details reportTableMd += `
\n` reportTableMd += `${testType} Details\n\n` // Include the relevant test code if (testType.includes("Existing")) { reportTableMd += "```python\n" reportTableMd += existingTests.trim() reportTableMd += "\n```\n" } else if (testType.includes("Generated")) { reportTableMd += "```python\n" reportTableMd += generatedTests.trim() reportTableMd += "\n```\n" } else { reportTableMd += "_No additional details available._\n" } reportTableMd += `\n
\n\n` } } // Add the final markdown content (e.g., the feedback section) const finalMarkdown = `${reportTableMd}` return PR_TEST_REPORT_TEMPLATE.replace(/\{report_table}/g, finalMarkdown) } export function parseAndCreateOptimizationsDict( prBody: string, prComments: { body: string }[], ): Record> { const optimizations: Record> = {} const textsToParse = [prBody, ...prComments.map(comment => comment.body)] for (const text of textsToParse) { let match while ((match = summaryLinesRegex.exec(text)) !== null) { const functionName = match[5] const filePath = match[6] if (!optimizations[filePath]) { optimizations[filePath] = new Set() } optimizations[filePath].add(functionName) } } return optimizations } export function buildDependentPrTitle( functionName: string, speedupPct: string, pullNumber: number, baseBranch: string, ): string { return buildPrTitle(functionName, speedupPct) + ` in PR #${pullNumber} (\`${baseBranch}\`)` } export function buildPrTitle(functionName: string, speedupPct: string): string { const type = functionName.includes(".") ? "method" : "function" return `⚡️ Speed up ${type} \`${functionName}\` by ${speedupPct}` }