Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | 10x 10x 10x 10x 10x 10x 10x 10x 2x 2x 10x 10x 10x 10x 10x 10x 10x 10x 10x 8x 8x 2x 6x 3x 3x 3x 1x 1x 3x 2x 2x 2x 10x 10x 10x 10x 10x 10x 9x 9x 1x 5x 2x 2x 2x 2x 2x 2x 1x 1x 2x 1x 1x 1x 7x 7x 7x 7x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 15x 15x 3x 3x 7x 7x 7x 7x 4x 4x 8x 8x 8x 2x 6x 3x 3x 8x 8x 2x 2x 2x 2x 2x 1x 1x 8x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 1x 1x 3x 8x 8x 8x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 3x 1x 2x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 4x 4x 1x 3x 2x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 1x 3x 1x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | /**
* Intent Builder - Constructs intent messages for AI assistant
*
* These builders create structured messages that the AI agent can parse
* to understand what the user wants to address and take appropriate action.
*
* DRY: Common sections are extracted into helper functions.
*/
import type {
HighlightIntent,
ObjectIntent,
TooltipAlert,
TooltipOperation,
TooltipFinding,
} from "../extension-commands";
// ============================================================================
// SHARED HELPER FUNCTIONS (DRY - used by multiple builders)
// ============================================================================
/**
* Build the context section (Job ID, SQL IDs, Node IDs, URL)
*/
function buildContextSection(intent: HighlightIntent): string[] {
const sections: string[] = [
"## Context Available",
`- **Job ID**: ${intent.jobId} (use with MCP tools for more data)`,
`- **SQL Queries**: ${intent.sqlIds.join(", ") || "none"}`,
`- **Node IDs**: ${intent.nodeIds.join(", ") || "none"}`,
];
if (intent.url) {
sections.push(`- **DataFlint Dashboard**: ${intent.url}`);
}
sections.push("");
return sections;
}
/**
* Build the alerts section
*/
function buildAlertsSection(
alerts: TooltipAlert[] | undefined,
headerText: string = "## Alerts",
includeSuggestions: boolean = true
): string[] {
if (!alerts || alerts.length === 0) {
return [];
}
const sections: string[] = [headerText];
for (const alert of alerts) {
sections.push(`- **${alert.title}** (${alert.severity})`);
sections.push(` - ${alert.message}`);
if (includeSuggestions && alert.suggestion) {
sections.push(` - 💡 Suggestion: ${alert.suggestion}`);
}
}
sections.push("");
return sections;
}
/**
* Build the operations section
*/
function buildOperationsSection(
operations: TooltipOperation[] | undefined,
headerText: string = "## Operations",
includeFailedMarker: boolean = true
): string[] {
if (!operations || operations.length === 0) {
return [];
}
const sections: string[] = [headerText];
for (const op of operations) {
const hotspotMarker = op.isHotspot ? " 🔥" : "";
const failedMarker = includeFailedMarker && op.isFailed ? " ❌" : "";
sections.push(
`- **Q${op.sqlId}: ${op.nodeName}** - ${op.durationPct.toFixed(1)}%${hotspotMarker}${failedMarker}`
);
if (op.metricsLine) {
sections.push(` - ${op.metricsLine}`);
}
}
sections.push("");
return sections;
}
/**
* Build the findings section
*/
function buildFindingsSection(
findings: TooltipFinding[] | undefined
): string[] {
if (!findings || findings.length === 0) {
return [];
}
const sections: string[] = ["## AI Findings"];
for (const finding of findings) {
// Use title as primary identifier, message as detail
const header = finding.title || finding.message || "Finding";
sections.push(`- **${header}** (${finding.type})`);
// Add message as detail if it's different from title
if (finding.message && finding.message !== finding.title) {
sections.push(` - ${finding.message}`);
}
if (finding.suggestion) {
sections.push(` - 💡 ${finding.suggestion}`);
}
}
sections.push("");
return sections;
}
/**
* Build the code section
*/
function buildCodeSection(codeText: string | undefined): string[] {
if (!codeText) {
return [];
}
return ["## Current Code", "```", codeText, "```"];
}
/**
* Determine issue type label based on intent
*/
function getIssueTypeLabel(intent: HighlightIntent): string {
if (intent.target.isHotspot) return "🔥 Performance Hotspot";
if (intent.target.alerts && intent.target.alerts.length > 0)
return "⚠️ Performance Warning";
return "📊 Spark Issue";
}
/**
* Build clicked object details for ObjectIntent
*/
function buildClickedObjectSection(intent: ObjectIntent): string[] {
const objectType =
intent.scope === "alert"
? "Alert"
: intent.scope === "operation"
? "Operation"
: "Finding";
const sections: string[] = [`## Clicked ${objectType}`];
if (intent.scope === "alert") {
const alert = intent.clickedObject as TooltipAlert;
sections.push(`- **Title**: ${alert.title}`);
sections.push(`- **Severity**: ${alert.severity}`);
sections.push(`- **Message**: ${alert.message}`);
if (alert.suggestion) {
sections.push(`- **Suggestion**: ${alert.suggestion}`);
}
} else if (intent.scope === "operation") {
const op = intent.clickedObject as TooltipOperation;
sections.push(`- **Node**: ${op.nodeName}`);
sections.push(`- **SQL Query**: Q${op.sqlId}`);
sections.push(`- **Duration**: ${op.durationPct.toFixed(1)}%`);
sections.push(`- **Is Hotspot**: ${op.isHotspot ? "Yes 🔥" : "No"}`);
if (op.metricsLine) {
sections.push(`- **Metrics**: ${op.metricsLine}`);
}
} else if (intent.scope === "finding") {
const finding = intent.clickedObject as TooltipFinding;
sections.push(`- **Type**: ${finding.type}`);
sections.push(`- **Title**: ${finding.title}`);
if (finding.message) {
sections.push(`- **Message**: ${finding.message}`);
}
if (finding.suggestion) {
sections.push(`- **Suggestion**: ${finding.suggestion}`);
}
if (finding.estimatedImpact) {
sections.push(`- **Estimated Impact**: ${finding.estimatedImpact}`);
}
}
sections.push("");
return sections;
}
// ============================================================================
// PUBLIC BUILDERS
// ============================================================================
/**
* Build a Fix Intent message for the AI assistant
*/
export function buildFixIntentMessage(intent: HighlightIntent): string {
const issueType = getIssueTypeLabel(intent);
const sections: string[] = [
`# DataFlint: Fix ${issueType}`,
"",
"## Your Role",
"Act as a **Spark expert** helping the user fix this performance issue.",
'The user is asking: *"How do I fix this? Give me the code changes."*',
"",
"## Issue Location",
`- **File**: \`${intent.filePath}\``,
`- **Line**: ${intent.codeLine}`,
`- **Issue**: ${intent.target.displayText}`,
"",
...buildContextSection(intent),
...buildAlertsSection(intent.target.alerts, "## Alerts", true),
...buildOperationsSection(
intent.target.operations,
"## Operations",
true
),
...buildFindingsSection(intent.target.findings),
"## What You Should Do",
"1. **Analyze the issue** using Spark expertise and the context provided",
"2. **Propose specific code changes** with before/after comparison",
"3. **Explain why** the fix addresses the performance issue",
"4. **Be skeptical** of alert suggestions - verify with actual plan data before recommending",
"",
"## Tools You Can Use",
"1. **Fetch more context** via `dataflint__get_job_plan` or `dataflint__get_highlight_context`",
"2. **Get expertise** via `dataflint__get_spark_expertise` for specific topics (skew, shuffle, memory, etc.)",
"",
...buildCodeSection(intent.codeText),
];
return sections.join("\n");
}
/**
* Build an Explain Intent message for the AI assistant
*/
export function buildExplainIntentMessage(intent: HighlightIntent): string {
const itemType = intent.target.isHotspot
? "🔥 Performance Hotspot"
: intent.target.alerts && intent.target.alerts.length > 0
? "⚠️ Alert"
: "📊 Spark Behavior";
const sections: string[] = [
`# DataFlint: Explain ${itemType}`,
"",
"## Your Role",
"Act as a **Spark expert** helping the user understand what they're looking at.",
'The user is asking: *"What does this mean? Help me understand."*',
"",
"## What the User is Looking At",
`- **File**: \`${intent.filePath}\``,
`- **Line**: ${intent.codeLine}`,
`- **Item**: ${intent.target.displayText}`,
"",
...buildContextSection(intent),
...buildAlertsSection(
intent.target.alerts,
"## Alerts to Explain",
false
),
...buildOperationsSection(
intent.target.operations,
"## Operations to Explain",
false
),
"## What to Explain",
"1. **WHAT** is happening - describe the Spark execution behavior",
"2. **WHY** this matters - explain the performance implications",
"3. **HOW** it connects to the user's code - trace from code to execution",
"",
"## Tools You Can Use",
"1. **Fetch more context** via `dataflint__get_job_plan` for detailed metrics",
"2. **Get expertise** via `dataflint__get_spark_expertise` for educational content",
"",
...buildCodeSection(intent.codeText),
];
return sections.join("\n");
}
/**
* Build a Fix Intent message for a specific object (alert, operation, finding)
*/
export function buildObjectFixIntentMessage(intent: ObjectIntent): string {
const objectType =
intent.scope === "alert"
? "Alert"
: intent.scope === "operation"
? "Operation"
: "Finding";
const sections: string[] = [
`# DataFlint: Fix ${objectType}`,
"",
"## Your Role",
`Act as a **Spark expert** helping the user fix this specific ${objectType.toLowerCase()}.`,
'The user is asking: *"How do I fix this? Give me the code changes."*',
"",
"## Issue Location",
`- **File**: \`${intent.filePath}\``,
`- **Line**: ${intent.codeLine}`,
"",
...buildClickedObjectSection(intent),
"## Context",
`- **Job ID**: ${intent.jobId}`,
`- **SQL Queries**: ${intent.sqlIds.join(", ") || "none"}`,
`- **Node IDs**: ${intent.nodeIds.join(", ") || "none"}`,
"",
"## What You Should Do",
`1. **Focus on this ${objectType.toLowerCase()}** - the user clicked it specifically`,
"2. **Propose specific code changes** with before/after comparison",
"3. **Explain why** the fix addresses the issue",
"4. **Be skeptical** - verify suggestions with actual plan data",
"",
"## Tools You Can Use",
"1. **Fetch more context** via `dataflint__get_job_plan` or `dataflint__get_highlight_context`",
"2. **Get expertise** via `dataflint__get_spark_expertise` for specific topics",
"",
...buildCodeSection(intent.codeText),
];
return sections.join("\n");
}
/**
* Build an Explain Intent message for a specific object
*/
export function buildObjectExplainIntentMessage(intent: ObjectIntent): string {
const objectType =
intent.scope === "alert"
? "Alert"
: intent.scope === "operation"
? "Operation"
: "Finding";
const sections: string[] = [
`# DataFlint: Explain ${objectType}`,
"",
"## Your Role",
`Act as a **Spark expert** helping the user understand this specific ${objectType.toLowerCase()}.`,
'The user is asking: *"What does this mean? Help me understand."*',
"",
"## What the User is Looking At",
`- **File**: \`${intent.filePath}\``,
`- **Line**: ${intent.codeLine}`,
"",
...buildClickedObjectSection(intent),
"## Context",
`- **Job ID**: ${intent.jobId}`,
`- **SQL Queries**: ${intent.sqlIds.join(", ") || "none"}`,
"",
"## What to Explain",
`1. **WHAT** this ${objectType.toLowerCase()} means in Spark execution`,
"2. **WHY** it's happening - the underlying cause",
"3. **HOW** it impacts performance - the consequences",
"",
"## Tools You Can Use",
"1. **Fetch more context** via `dataflint__get_job_plan` for detailed metrics",
"2. **Get expertise** via `dataflint__get_spark_expertise` for educational content",
"",
...buildCodeSection(intent.codeText),
];
return sections.join("\n");
}
|