diff --git a/js/cf-api/github/create-pr-from-diffcontents.ts b/js/cf-api/github/create-pr-from-diffcontents.ts
index f98985461..1e4e1ffcf 100644
--- a/js/cf-api/github/create-pr-from-diffcontents.ts
+++ b/js/cf-api/github/create-pr-from-diffcontents.ts
@@ -38,6 +38,7 @@ export interface PrCommentFields {
benchmark_details?: BenchmarkDetail[]
original_async_throughput?: string
best_async_throughput?: string
+ language?: string
}
// Dependencies interface for easier testing
diff --git a/js/cf-api/github/pr-changes-utils.ts b/js/cf-api/github/pr-changes-utils.ts
index 8ed0e2847..0806da073 100644
--- a/js/cf-api/github/pr-changes-utils.ts
+++ b/js/cf-api/github/pr-changes-utils.ts
@@ -4,6 +4,10 @@ import { fileURLToPath } from "url"
import { PrCommentFields } from "./create-pr-from-diffcontents.js"
import { OptimizationReview } from "../OptimizationReview.js"
+function isJsTsLanguage(language?: string): boolean {
+ return language === "javascript" || language === "typescript"
+}
+
export function generateOptimizationReviewTemplate(optimizationReview: string): string {
// Return empty string if optimizationReview is empty
if (!optimizationReview || optimizationReview === "") {
@@ -292,7 +296,39 @@ export function buildResultHeader(fields: PrCommentFields, isUnifiedReview?: boo
.replace(/\{num_runs}/g, fields.loop_count)
}
+function extractTldr(explanation: string): { summary: string; details: string } {
+ // Remove leading markdown bold header like "**Optimization Explanation:**\n"
+ const cleaned = explanation.replace(/^\*\*[^*]+\*\*:?\s*\n?/, "").trim()
+
+ // Split on double newline to get paragraphs
+ const paragraphs = cleaned.split(/\n\n+/)
+
+ if (paragraphs.length <= 1) {
+ // Single paragraph — use first two sentences as summary
+ const sentences = cleaned.match(/[^.!?\n]+[.!?]+/g)
+ if (sentences && sentences.length > 2) {
+ const summary = sentences.slice(0, 2).join("").trim()
+ const rest = cleaned.slice(summary.length).trim()
+ return { summary, details: rest }
+ }
+ return { summary: cleaned, details: "" }
+ }
+
+ // Use first paragraph as summary, rest as details
+ return { summary: paragraphs[0].trim(), details: paragraphs.slice(1).join("\n\n").trim() }
+}
+
export function buildResultDetails(fields: PrCommentFields, isCollapsed: boolean = false): string {
+ // For JS/TS: use TL;DR format with summary up front and details collapsed
+ if (isJsTsLanguage(fields.language)) {
+ const { summary, details } = extractTldr(fields.optimization_explanation)
+ let result = `#### 📝 Summary\n\n${summary}\n`
+ if (details) {
+ result += `\n\nFull explanation and details
\n\n${details}\n \n`
+ }
+ return result + "\n"
+ }
+
return isCollapsed
? getPrDetailsTemplateCollapsed().replace(
/\{optimization_explanation}/g,
@@ -392,8 +428,8 @@ export function buildResultTestReport(
const trimmedGeneratedTests = generatedTests.trim()
// Check if generatedTests already contains backticks
if (!trimmedGeneratedTests.includes("`")) {
- // Wrap in Python markdown block
- reportTableMd += "```python\n" + trimmedGeneratedTests + "\n```"
+ const codeBlockLang = isJsTsLanguage(fields.language) ? "typescript" : "python"
+ reportTableMd += "```" + codeBlockLang + "\n" + trimmedGeneratedTests + "\n```"
} else {
reportTableMd += trimmedGeneratedTests
}
diff --git a/js/cf-webapp/src/lib/types.ts b/js/cf-webapp/src/lib/types.ts
index 41d591070..14d808cdb 100644
--- a/js/cf-webapp/src/lib/types.ts
+++ b/js/cf-webapp/src/lib/types.ts
@@ -8,16 +8,17 @@ export interface DiffContents {
}
export interface PrCommentFields {
- file_path?: string // Likely the main Python file being optimized
+ file_path?: string
speedup_x?: string
loop_count?: number
speedup_pct?: string
best_runtime?: string
report_table?: Record
- function_name?: string // The Python function optimized
+ function_name?: string
original_runtime?: string
benchmark_details?: any
optimization_explanation?: string
+ language?: string
}
export interface ExperimentMetadata {