cursorai-errorprompter
Version:
AI-powered runtime error fixing for developers using Cursor
155 lines (140 loc) • 5.47 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptBuilder = void 0;
const fs = __importStar(require("fs"));
const gptService_1 = require("./gptService");
class PromptBuilder {
constructor(outputPath = '.cursor_prompt.md', gptConfig) {
this.outputPath = outputPath;
if (gptConfig?.apiKey) {
try {
this.gptService = new gptService_1.GPTService(gptConfig);
console.log('✨ GPT integration enabled');
}
catch (error) {
console.warn('⚠️ Failed to initialize GPT service:', error instanceof Error ? error.message : 'Unknown error');
console.warn('⚠️ Falling back to template-only mode');
}
}
else {
console.log('ℹ️ GPT integration not configured - running in template-only mode');
}
}
async buildPrompt(error) {
let gptAnalysis = '';
let template = PromptBuilder.PROMPT_TEMPLATE;
if (this.gptService) {
try {
console.log('🔄 Requesting GPT analysis...');
const gptResponse = await this.gptService.getErrorFix(error);
gptAnalysis = `
💡 Suggested Fix:
\`\`\`typescript
${gptResponse.suggestion}
\`\`\`
📝 Explanation:
${gptResponse.explanation}
🎯 Confidence: ${(gptResponse.confidence * 100).toFixed(0)}%`;
console.log('✅ GPT analysis completed successfully');
}
catch (error) {
console.error('❌ GPT analysis failed:', error instanceof Error ? error.message : 'Unknown error');
template = PromptBuilder.FALLBACK_TEMPLATE;
gptAnalysis = `⚠️ GPT analysis failed: ${error instanceof Error ? error.message : 'Unknown error'}`;
}
}
else {
template = PromptBuilder.FALLBACK_TEMPLATE;
}
const prompt = template
.replace('{errorType}', error.type)
.replace('{message}', error.message)
.replace('{filePath}', error.filePath)
.replace('{lineNumber}', error.lineNumber.toString())
.replace('{beforeLines}', error.codeSnippet.before.join('\n'))
.replace('{errorLine}', error.codeSnippet.error)
.replace('{afterLines}', error.codeSnippet.after.join('\n'))
.replace('{gptAnalysis}', gptAnalysis);
try {
fs.writeFileSync(this.outputPath, prompt);
console.log(`📝 Prompt written to ${this.outputPath}`);
}
catch (error) {
console.error('❌ Failed to write prompt file:', error instanceof Error ? error.message : 'Unknown error');
throw new Error(`Failed to write prompt file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
}
exports.PromptBuilder = PromptBuilder;
PromptBuilder.PROMPT_TEMPLATE = `🎯 Goal: Fix runtime {errorType} from dev server
📛 Error:
{errorType}: {message}
at {filePath}:{lineNumber}
📂 File:
{filePath}
🔍 Context:
\`\`\`typescript
{beforeLines}
{errorLine}
{afterLines}
\`\`\`
🤖 GPT Analysis:
{gptAnalysis}
🔧 Steps:
- Analyze the error and its context
- Identify the root cause
- Propose a fix that addresses the issue
- Ensure the fix maintains code quality and follows best practices
💡 Note: After implementing the fix, re-run the development server to verify the error is resolved.`;
PromptBuilder.FALLBACK_TEMPLATE = `🎯 Goal: Fix runtime {errorType} from dev server
📛 Error:
{errorType}: {message}
at {filePath}:{lineNumber}
📂 File:
{filePath}
🔍 Context:
\`\`\`typescript
{beforeLines}
{errorLine}
{afterLines}
\`\`\`
⚠️ Note: GPT analysis is not available. Please analyze the error manually.
🔧 Steps:
- Analyze the error and its context
- Identify the root cause
- Propose a fix that addresses the issue
- Ensure the fix maintains code quality and follows best practices
💡 Note: After implementing the fix, re-run the development server to verify the error is resolved.`;