cline-sdk
Version:
Comprehensive SDK for Cline - AI-powered development assistant with database integration, execution modes, and custom functions
184 lines • 7.42 kB
JavaScript
;
/**
* Prompt Optimizer Utility
* Provides tools for analyzing and improving user prompts
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptOptimizer = void 0;
class PromptOptimizer {
constructor() {
this.patterns = {
// Weak indicators
weakWords: ['help', 'fix', 'make', 'do', 'thing', 'stuff', 'something'],
// Strong indicators
strongWords: ['create', 'implement', 'generate', 'design', 'build', 'develop'],
// Structure indicators
hasSteps: /\d+\./,
hasRequirements: /requirements?:|must:|should:|need/i,
hasFormat: /format:|output:|return:|response:/i,
hasContext: /context:|background:|given:|assuming:/i
};
}
/**
* Analyze a prompt for quality and completeness
*/
analyzePrompt(prompt) {
const words = prompt.toLowerCase().split(/\s+/);
const length = prompt.length;
// Calculate clarity (specific vs vague language)
const weakCount = words.filter(word => this.patterns.weakWords.includes(word)).length;
const strongCount = words.filter(word => this.patterns.strongWords.includes(word)).length;
const clarity = Math.max(1, Math.min(10, 7 - weakCount + strongCount));
// Calculate specificity (detailed vs general)
const hasNumbers = /\d/.test(prompt);
const hasExamples = /example|such as|like|e\.g\./i.test(prompt);
const hasConstraints = /within|limit|maximum|minimum|exactly/i.test(prompt);
let specificity = 5;
if (hasNumbers)
specificity += 1;
if (hasExamples)
specificity += 2;
if (hasConstraints)
specificity += 2;
if (length > 100)
specificity += 1;
// Calculate completeness (has all necessary parts)
let completeness = 3;
if (this.patterns.hasRequirements.test(prompt))
completeness += 2;
if (this.patterns.hasFormat.test(prompt))
completeness += 2;
if (this.patterns.hasContext.test(prompt))
completeness += 2;
if (length > 50)
completeness += 1;
// Calculate structure (organized vs rambling)
let structure = 5;
if (this.patterns.hasSteps.test(prompt))
structure += 2;
if (/\n/.test(prompt))
structure += 1;
if (prompt.includes(':'))
structure += 1;
if (words.length > 50 && !this.patterns.hasSteps.test(prompt))
structure -= 2;
// Overall score
const overall = Math.round((clarity + specificity + completeness + structure) / 4);
return {
clarity: Math.max(1, Math.min(10, clarity)),
specificity: Math.max(1, Math.min(10, specificity)),
completeness: Math.max(1, Math.min(10, completeness)),
structure: Math.max(1, Math.min(10, structure)),
overall: Math.max(1, Math.min(10, overall))
};
}
/**
* Generate suggestions for prompt improvement
*/
generateSuggestions(prompt, analysis) {
const suggestions = [];
// Clarity suggestions
if (analysis.clarity < 6) {
suggestions.push({
type: 'clarity',
message: 'Replace vague words with specific action verbs',
example: 'Instead of "help me with", use "create", "implement", or "generate"'
});
}
// Specificity suggestions
if (analysis.specificity < 6) {
suggestions.push({
type: 'specificity',
message: 'Add specific details, numbers, or examples',
example: 'Specify quantities, formats, technologies, or constraints'
});
}
// Structure suggestions
if (analysis.structure < 6) {
suggestions.push({
type: 'structure',
message: 'Break complex requests into numbered steps',
example: '1. Create the component\n2. Add styling\n3. Implement functionality'
});
}
// Context suggestions
if (analysis.completeness < 6) {
suggestions.push({
type: 'context',
message: 'Provide background information and requirements',
example: 'Explain the purpose, target audience, and constraints'
});
}
// Format suggestions
if (!this.patterns.hasFormat.test(prompt)) {
suggestions.push({
type: 'format',
message: 'Specify the expected output format',
example: 'Request JSON, code files, documentation, or specific structure'
});
}
return suggestions;
}
/**
* Create an improved version of the prompt
*/
improvePrompt(prompt) {
const analysis = this.analyzePrompt(prompt);
// Basic improvements
let improved = prompt;
// Add structure if missing
if (analysis.structure < 6 && !this.patterns.hasSteps.test(prompt)) {
improved = `Task: ${improved}\n\nRequirements:\n1. [Specify requirement 1]\n2. [Specify requirement 2]\n\nOutput Format: [Specify desired format]`;
}
// Add context section if missing
if (analysis.completeness < 6 && !this.patterns.hasContext.test(prompt)) {
improved = `Context: [Provide relevant background]\n\n${improved}`;
}
return improved;
}
/**
* Complete optimization workflow
*/
optimizePrompt(prompt) {
const analysis = this.analyzePrompt(prompt);
const suggestions = this.generateSuggestions(prompt, analysis);
const improvedPrompt = this.improvePrompt(prompt);
// Calculate confidence based on how much improvement is possible
const improvementPotential = 10 - analysis.overall;
const confidence = Math.max(0.5, Math.min(1.0, 1 - (improvementPotential / 10)));
return {
original: prompt,
analysis,
suggestions,
improvedPrompt,
confidence
};
}
/**
* Format optimization results for display
*/
formatResults(result) {
const { analysis, suggestions } = result;
let output = `📊 Prompt Analysis:\n`;
output += ` Clarity: ${analysis.clarity}/10\n`;
output += ` Specificity: ${analysis.specificity}/10\n`;
output += ` Completeness: ${analysis.completeness}/10\n`;
output += ` Structure: ${analysis.structure}/10\n`;
output += ` Overall: ${analysis.overall}/10\n\n`;
if (suggestions.length > 0) {
output += `💡 Suggestions:\n`;
suggestions.forEach((suggestion, index) => {
output += ` ${index + 1}. ${suggestion.message}\n`;
if (suggestion.example) {
output += ` Example: ${suggestion.example}\n`;
}
});
output += '\n';
}
output += `✨ Improved Prompt:\n${result.improvedPrompt}\n\n`;
output += `🎯 Confidence: ${Math.round(result.confidence * 100)}%`;
return output;
}
}
exports.PromptOptimizer = PromptOptimizer;
//# sourceMappingURL=prompt-optimizer.js.map