prompt-optimizer.ts•5.28 kB
export type TaskType = 'analysis' | 'generation' | 'reasoning' | 'coding';
export type OptimizationLevel = 'balanced' | 'creative' | 'precise';
export interface SystemPromptConfig {
taskType: TaskType;
domain?: string;
context?: Record<string, any>;
optimizationLevel?: OptimizationLevel;
claudeContext?: string;
}
export interface SystemPromptTemplate {
base: string;
[key: string]: string;
}
export class PromptOptimizer {
private readonly systemPrompts: Record<TaskType, SystemPromptTemplate> = {
analysis: {
base: "You are an expert analyst providing thorough, evidence-based analysis. Focus on identifying patterns, root causes, and actionable insights.",
coding: "Analyze code for quality, performance, security vulnerabilities, and best practices. Provide specific recommendations with examples.",
data: "Examine data for statistical accuracy, trends, anomalies, and meaningful insights. Use quantitative methods when applicable.",
security: "Conduct comprehensive security analysis focusing on vulnerabilities, threat vectors, and compliance requirements.",
performance: "Analyze performance bottlenecks, optimization opportunities, and scalability concerns with measurable metrics.",
architecture: "Review system architecture for scalability, maintainability, and design pattern compliance."
},
generation: {
base: "You are a creative and precise content generator. Create high-quality, purposeful content that meets specific requirements.",
code: "Generate clean, efficient, well-documented code following industry best practices and security guidelines.",
documentation: "Create comprehensive, clear documentation with practical examples and proper structure.",
design: "Generate user-centered designs focusing on usability, accessibility, and modern design principles.",
content: "Produce engaging, accurate content tailored to the target audience and purpose.",
api: "Design RESTful APIs with proper endpoints, error handling, and documentation."
},
reasoning: {
base: "You are a logical reasoning expert. Think systematically and provide step-by-step analysis.",
problem_solving: "Break down complex problems into manageable components. Provide structured solutions with clear reasoning.",
debugging: "Systematically identify root causes using logical deduction. Provide actionable debugging steps.",
optimization: "Evaluate trade-offs methodically. Recommend optimal approaches with supporting rationale.",
decision: "Analyze options using structured decision-making frameworks. Consider multiple perspectives and consequences."
},
coding: {
base: "You are a senior software engineer with expertise across multiple programming languages and frameworks.",
implementation: "Write production-ready code with proper error handling, testing, and documentation.",
refactoring: "Improve code quality while maintaining functionality. Focus on readability, performance, and maintainability.",
review: "Conduct thorough code reviews checking for bugs, security issues, and adherence to best practices.",
architecture: "Design scalable software architecture with proper separation of concerns and design patterns."
}
};
private readonly optimizationInstructions: Record<OptimizationLevel, string> = {
balanced: "Balance creativity with accuracy. Provide practical, well-reasoned responses.",
creative: "Emphasize innovative approaches and creative solutions while maintaining feasibility.",
precise: "Prioritize accuracy, detail, and technical precision. Be thorough and methodical."
};
optimizeSystemPrompt(config: SystemPromptConfig): string {
const templates = this.systemPrompts[config.taskType];
const basePrompt = templates.base;
const domainPrompt = config.domain ? (templates[config.domain] || '') : '';
const optimizationLevel = config.optimizationLevel || 'balanced';
let systemPrompt = basePrompt;
if (domainPrompt) {
systemPrompt += `\n\n${domainPrompt}`;
}
systemPrompt += `\n\n${this.optimizationInstructions[optimizationLevel]}`;
if (config.claudeContext) {
systemPrompt += `\n\nContext from Claude session:\n${config.claudeContext}`;
}
if (config.context) {
const contextStr = this.formatContext(config.context);
systemPrompt += `\n\nAdditional context:\n${contextStr}`;
}
systemPrompt += `\n\nRespond in a way that complements Claude's capabilities and provides additional perspective or expertise.`;
return systemPrompt;
}
private formatContext(context: Record<string, any>): string {
return Object.entries(context)
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
.join('\n');
}
getAvailableTaskTypes(): TaskType[] {
return Object.keys(this.systemPrompts) as TaskType[];
}
getAvailableDomains(taskType: TaskType): string[] {
const templates = this.systemPrompts[taskType];
return Object.keys(templates).filter(key => key !== 'base');
}
generateDefaultPrompt(taskType: TaskType): string {
return this.optimizeSystemPrompt({
taskType,
optimizationLevel: 'balanced'
});
}
}