mcp-adr-analysis-server 1.1.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +367 -150
  2. package/dist/src/config/ai-config.d.ts +81 -0
  3. package/dist/src/config/ai-config.d.ts.map +1 -0
  4. package/dist/src/config/ai-config.js +134 -0
  5. package/dist/src/config/ai-config.js.map +1 -0
  6. package/dist/src/index.d.ts +97 -1
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/index.js +2430 -186
  9. package/dist/src/index.js.map +1 -1
  10. package/dist/src/prompts/adr-suggestion-prompts.d.ts +2 -2
  11. package/dist/src/prompts/adr-suggestion-prompts.d.ts.map +1 -1
  12. package/dist/src/prompts/adr-suggestion-prompts.js +25 -17
  13. package/dist/src/prompts/adr-suggestion-prompts.js.map +1 -1
  14. package/dist/src/prompts/analysis-prompts.js +1 -1
  15. package/dist/src/prompts/analysis-prompts.js.map +1 -1
  16. package/dist/src/resources/index.d.ts +1 -1
  17. package/dist/src/resources/index.d.ts.map +1 -1
  18. package/dist/src/resources/index.js +52 -44
  19. package/dist/src/resources/index.js.map +1 -1
  20. package/dist/src/templates/domain-knowledge-templates.d.ts +36 -0
  21. package/dist/src/templates/domain-knowledge-templates.d.ts.map +1 -0
  22. package/dist/src/templates/domain-knowledge-templates.js +376 -0
  23. package/dist/src/templates/domain-knowledge-templates.js.map +1 -0
  24. package/dist/src/tools/adr-suggestion-tool.d.ts +8 -3
  25. package/dist/src/tools/adr-suggestion-tool.d.ts.map +1 -1
  26. package/dist/src/tools/adr-suggestion-tool.js +561 -172
  27. package/dist/src/tools/adr-suggestion-tool.js.map +1 -1
  28. package/dist/src/tools/content-masking-tool.d.ts.map +1 -1
  29. package/dist/src/tools/content-masking-tool.js +168 -22
  30. package/dist/src/tools/content-masking-tool.js.map +1 -1
  31. package/dist/src/tools/deployment-analysis-tool.d.ts.map +1 -1
  32. package/dist/src/tools/deployment-analysis-tool.js +64 -27
  33. package/dist/src/tools/deployment-analysis-tool.js.map +1 -1
  34. package/dist/src/tools/environment-analysis-tool.d.ts.map +1 -1
  35. package/dist/src/tools/environment-analysis-tool.js +65 -29
  36. package/dist/src/tools/environment-analysis-tool.js.map +1 -1
  37. package/dist/src/tools/research-question-tool.d.ts.map +1 -1
  38. package/dist/src/tools/research-question-tool.js +150 -49
  39. package/dist/src/tools/research-question-tool.js.map +1 -1
  40. package/dist/src/tools/rule-generation-tool.d.ts.map +1 -1
  41. package/dist/src/tools/rule-generation-tool.js +328 -143
  42. package/dist/src/tools/rule-generation-tool.js.map +1 -1
  43. package/dist/src/types/ape-framework.d.ts +566 -0
  44. package/dist/src/types/ape-framework.d.ts.map +1 -0
  45. package/dist/src/types/ape-framework.js +98 -0
  46. package/dist/src/types/ape-framework.js.map +1 -0
  47. package/dist/src/types/index.d.ts +96 -93
  48. package/dist/src/types/index.d.ts.map +1 -1
  49. package/dist/src/types/knowledge-generation.d.ts +671 -0
  50. package/dist/src/types/knowledge-generation.d.ts.map +1 -0
  51. package/dist/src/types/knowledge-generation.js +107 -0
  52. package/dist/src/types/knowledge-generation.js.map +1 -0
  53. package/dist/src/types/reflexion-framework.d.ts +652 -0
  54. package/dist/src/types/reflexion-framework.d.ts.map +1 -0
  55. package/dist/src/types/reflexion-framework.js +85 -0
  56. package/dist/src/types/reflexion-framework.js.map +1 -0
  57. package/dist/src/utils/adr-suggestions.d.ts +1 -1
  58. package/dist/src/utils/adr-suggestions.d.ts.map +1 -1
  59. package/dist/src/utils/adr-suggestions.js +8 -50
  60. package/dist/src/utils/adr-suggestions.js.map +1 -1
  61. package/dist/src/utils/ai-executor.d.ts +125 -0
  62. package/dist/src/utils/ai-executor.d.ts.map +1 -0
  63. package/dist/src/utils/ai-executor.js +292 -0
  64. package/dist/src/utils/ai-executor.js.map +1 -0
  65. package/dist/src/utils/automatic-prompt-engineering.d.ts +72 -0
  66. package/dist/src/utils/automatic-prompt-engineering.d.ts.map +1 -0
  67. package/dist/src/utils/automatic-prompt-engineering.js +820 -0
  68. package/dist/src/utils/automatic-prompt-engineering.js.map +1 -0
  69. package/dist/src/utils/cache.d.ts +54 -22
  70. package/dist/src/utils/cache.d.ts.map +1 -1
  71. package/dist/src/utils/cache.js +1234 -143
  72. package/dist/src/utils/cache.js.map +1 -1
  73. package/dist/src/utils/config.d.ts +2 -2
  74. package/dist/src/utils/config.js +3 -3
  75. package/dist/src/utils/config.js.map +1 -1
  76. package/dist/src/utils/deployment-analysis.d.ts.map +1 -1
  77. package/dist/src/utils/deployment-analysis.js +57 -38
  78. package/dist/src/utils/deployment-analysis.js.map +1 -1
  79. package/dist/src/utils/environment-analysis.d.ts.map +1 -1
  80. package/dist/src/utils/environment-analysis.js +113 -100
  81. package/dist/src/utils/environment-analysis.js.map +1 -1
  82. package/dist/src/utils/file-system.d.ts +42 -13
  83. package/dist/src/utils/file-system.d.ts.map +1 -1
  84. package/dist/src/utils/file-system.js +795 -89
  85. package/dist/src/utils/file-system.js.map +1 -1
  86. package/dist/src/utils/index.d.ts +9 -0
  87. package/dist/src/utils/index.d.ts.map +1 -0
  88. package/dist/src/utils/index.js +13 -0
  89. package/dist/src/utils/index.js.map +1 -0
  90. package/dist/src/utils/knowledge-generation.d.ts +79 -0
  91. package/dist/src/utils/knowledge-generation.d.ts.map +1 -0
  92. package/dist/src/utils/knowledge-generation.js +646 -0
  93. package/dist/src/utils/knowledge-generation.js.map +1 -0
  94. package/dist/src/utils/output-masking.d.ts.map +1 -1
  95. package/dist/src/utils/output-masking.js +2 -1
  96. package/dist/src/utils/output-masking.js.map +1 -1
  97. package/dist/src/utils/prompt-composition.d.ts +234 -0
  98. package/dist/src/utils/prompt-composition.d.ts.map +1 -0
  99. package/dist/src/utils/prompt-composition.js +379 -0
  100. package/dist/src/utils/prompt-composition.js.map +1 -0
  101. package/dist/src/utils/prompt-execution.d.ts +88 -0
  102. package/dist/src/utils/prompt-execution.d.ts.map +1 -0
  103. package/dist/src/utils/prompt-execution.js +227 -0
  104. package/dist/src/utils/prompt-execution.js.map +1 -0
  105. package/dist/src/utils/reflexion.d.ts +92 -0
  106. package/dist/src/utils/reflexion.d.ts.map +1 -0
  107. package/dist/src/utils/reflexion.js +1100 -0
  108. package/dist/src/utils/reflexion.js.map +1 -0
  109. package/dist/src/utils/research-integration.d.ts.map +1 -1
  110. package/dist/src/utils/research-integration.js +156 -100
  111. package/dist/src/utils/research-integration.js.map +1 -1
  112. package/dist/src/utils/research-questions.d.ts.map +1 -1
  113. package/dist/src/utils/research-questions.js +85 -24
  114. package/dist/src/utils/research-questions.js.map +1 -1
  115. package/dist/src/utils/rule-generation.d.ts.map +1 -1
  116. package/dist/src/utils/rule-generation.js +92 -108
  117. package/dist/src/utils/rule-generation.js.map +1 -1
  118. package/package.json +9 -3
@@ -0,0 +1,820 @@
1
+ /**
2
+ * Automatic Prompt Engineering (APE) Utility Module - 100% Prompt-Driven Architecture
3
+ * Generates AI delegation prompts for automatic prompt optimization and evaluation
4
+ * All functions return prompts for AI execution, never execute operations directly
5
+ */
6
+ import { McpAdrError } from '../types/index.js';
7
+ // ============================================================================
8
+ // Configuration and Constants
9
+ // ============================================================================
10
+ const DEFAULT_APE_CONFIG = {
11
+ candidateCount: 5,
12
+ evaluationCriteria: ['task-completion', 'clarity', 'specificity'],
13
+ optimizationRounds: 3,
14
+ selectionStrategy: 'multi-criteria',
15
+ cacheEnabled: true,
16
+ performanceTracking: true,
17
+ maxOptimizationTime: 180000, // 3 minutes
18
+ qualityThreshold: 0.7,
19
+ diversityWeight: 0.3
20
+ };
21
+ const APE_VERSION = '1.0.0';
22
+ // ============================================================================
23
+ // Core APE Functions - 100% Prompt-Driven
24
+ // ============================================================================
25
+ /**
26
+ * Generate AI delegation prompt for automatic prompt optimization
27
+ * Returns prompts for AI to optimize the given prompt using APE techniques
28
+ */
29
+ export async function optimizePromptWithAPE(originalPrompt, config = {}) {
30
+ try {
31
+ // Validate input prompt
32
+ if (!originalPrompt.prompt || originalPrompt.prompt.trim() === '') {
33
+ throw new McpAdrError('Prompt cannot be empty', 'INVALID_INPUT');
34
+ }
35
+ // Validate configuration
36
+ validateAPEConfig(config);
37
+ const mergedConfig = { ...DEFAULT_APE_CONFIG, ...config };
38
+ console.error(`[DEBUG] Generating APE optimization prompt for prompt optimization`);
39
+ // Generate cache key for this optimization request
40
+ const contextHash = Buffer.from(JSON.stringify(originalPrompt)).toString('base64').substring(0, 16);
41
+ const configHash = Buffer.from(JSON.stringify(mergedConfig)).toString('base64').substring(0, 16);
42
+ const cacheKey = `ape:optimization:${contextHash}-${configHash}`;
43
+ // Create comprehensive APE optimization prompt
44
+ const prompt = `
45
+ # APE-Optimized Prompt
46
+
47
+ Please optimize the following prompt using APE techniques for better performance and effectiveness.
48
+
49
+ ## Original Prompt to Optimize
50
+ \`\`\`
51
+ ${originalPrompt.prompt}
52
+ \`\`\`
53
+
54
+ ## Original Instructions
55
+ \`\`\`
56
+ ${originalPrompt.instructions}
57
+ \`\`\`
58
+
59
+ ## Original Context
60
+ ${JSON.stringify(originalPrompt.context, null, 2)}
61
+
62
+ ## APE Configuration
63
+ - **Candidate Count**: ${mergedConfig.candidateCount}
64
+ - **Evaluation Criteria**: ${mergedConfig.evaluationCriteria.join(', ')}
65
+ - **Selection Strategy**: ${mergedConfig.selectionStrategy}
66
+ - **Quality Threshold**: ${mergedConfig.qualityThreshold}
67
+ - **Diversity Weight**: ${mergedConfig.diversityWeight}
68
+ - **Max Optimization Time**: ${mergedConfig.maxOptimizationTime}ms
69
+
70
+ ## APE Optimization Process
71
+
72
+ ### Step 1: Generate Prompt Candidates (${mergedConfig.candidateCount} prompt candidates)
73
+ Create ${mergedConfig.candidateCount} prompt variations using these strategies:
74
+
75
+ #### Template-based Variation
76
+ - Use different prompt templates and structures
77
+ - Vary instruction formats (imperative, collaborative, question-based)
78
+ - Adjust formality levels and communication styles
79
+
80
+ #### Semantic Variation
81
+ - Rephrase while preserving core meaning and intent
82
+ - Use synonyms and alternative expressions
83
+ - Restructure sentences for better clarity
84
+
85
+ #### Style Variation
86
+ - **Formal Style**: Professional, structured language
87
+ - **Conversational Style**: Friendly, approachable language
88
+ - **Technical Style**: Precise, domain-specific terminology
89
+ - **Instructional Style**: Step-by-step, educational approach
90
+
91
+ #### Length Variation
92
+ - **Concise Version**: Essential information only
93
+ - **Detailed Version**: Comprehensive with examples
94
+ - **Balanced Version**: Optimal information density
95
+
96
+ #### Structure Variation
97
+ - Different organizational patterns
98
+ - Varied section ordering and emphasis
99
+ - Alternative formatting and presentation
100
+
101
+ ### Step 2: Evaluate Prompt Candidates
102
+ Evaluate each candidate on these criteria (0-1 scale):
103
+
104
+ ${mergedConfig.evaluationCriteria.map(criterion => {
105
+ const descriptions = {
106
+ 'task-completion': 'How well the prompt achieves the intended task outcome',
107
+ 'clarity': 'How clear, unambiguous, and easy to understand the prompt is',
108
+ 'specificity': 'How specific, actionable, and detailed the prompt is',
109
+ 'robustness': 'How well the prompt handles edge cases and variations',
110
+ 'efficiency': 'How concise yet comprehensive the prompt is',
111
+ 'context-awareness': 'How well the prompt fits the specific context and domain'
112
+ };
113
+ return `- **${criterion}**: ${descriptions[criterion] || 'Evaluate based on this criterion'}`;
114
+ }).join('\n')}
115
+
116
+ ### Step 3: Apply Selection Strategy (${mergedConfig.selectionStrategy})
117
+ ${getSelectionStrategyDescription(mergedConfig.selectionStrategy)}
118
+
119
+ ### Step 4: Generate Optimization Result
120
+ Create the final optimized prompt considering:
121
+ - Highest-scoring candidates from evaluation
122
+ - Diversity requirements (weight: ${mergedConfig.diversityWeight})
123
+ - Quality threshold (minimum: ${mergedConfig.qualityThreshold})
124
+ - Original prompt objectives and constraints
125
+
126
+ ## Expected Output Format
127
+ \`\`\`json
128
+ {
129
+ "apeOptimization": {
130
+ "optimizedPrompt": {
131
+ "prompt": "optimized prompt text",
132
+ "instructions": "optimized instructions",
133
+ "context": {
134
+ "optimization_metadata": "...",
135
+ "original_context": "..."
136
+ }
137
+ },
138
+ "optimization": {
139
+ "candidatesGenerated": ${mergedConfig.candidateCount},
140
+ "candidatesEvaluated": ${mergedConfig.candidateCount},
141
+ "improvementScore": 0.85,
142
+ "optimizationRounds": ${mergedConfig.optimizationRounds},
143
+ "selectionStrategy": "${mergedConfig.selectionStrategy}",
144
+ "qualityImprovement": 0.23,
145
+ "optimizationReasoning": "explanation of improvements made",
146
+ "evaluationScores": {
147
+ ${mergedConfig.evaluationCriteria.map(c => `"${c}": 0.8`).join(',\n ')}
148
+ },
149
+ "candidateDetails": [
150
+ {
151
+ "candidateId": "candidate_1",
152
+ "strategy": "template-variation",
153
+ "scores": { ${mergedConfig.evaluationCriteria.map(c => `"${c}": 0.8`).join(', ')} },
154
+ "overallScore": 0.8,
155
+ "strengths": ["clear structure", "specific instructions"],
156
+ "weaknesses": ["could be more concise"]
157
+ }
158
+ ]
159
+ },
160
+ "metadata": {
161
+ "apeVersion": "${APE_VERSION}",
162
+ "optimizedAt": "${new Date().toISOString()}",
163
+ "cacheKey": "${cacheKey}",
164
+ "configUsed": ${JSON.stringify(mergedConfig, null, 6)},
165
+ "performanceMetrics": {
166
+ "totalOptimizationTime": 120000,
167
+ "candidateGenerationTime": 45000,
168
+ "evaluationTime": 60000,
169
+ "selectionTime": 15000
170
+ }
171
+ }
172
+ }
173
+ }
174
+ \`\`\`
175
+
176
+ ## Quality Requirements
177
+ - Optimized prompt must score above ${mergedConfig.qualityThreshold} on all criteria
178
+ - Must maintain original task objectives and context requirements
179
+ - Should demonstrate clear improvement over original prompt
180
+ - Must be appropriate for the intended use case and domain
181
+ - Should provide specific reasoning for optimization choices
182
+
183
+ ## Security and Validation
184
+ - Ensure optimized prompt is safe and appropriate
185
+ - Validate that optimization doesn't introduce harmful content
186
+ - Verify that core functionality is preserved
187
+ - Check for potential prompt injection vulnerabilities
188
+
189
+ The optimized prompt should be significantly better than the original while maintaining its core purpose and functionality.
190
+ `;
191
+ const instructions = `
192
+ # APE Optimization Instructions
193
+
194
+ You must:
195
+ 1. **Generate Diverse Candidates**: Create ${mergedConfig.candidateCount} varied prompt alternatives using multiple strategies
196
+ 2. **Evaluate Systematically**: Score each candidate on all specified criteria objectively
197
+ 3. **Select Optimally**: Choose the best prompt using the ${mergedConfig.selectionStrategy} strategy
198
+ 4. **Validate Quality**: Ensure the optimized prompt meets all quality thresholds
199
+ 5. **Document Improvements**: Provide clear reasoning for optimization choices
200
+ 6. **Maintain Functionality**: Preserve original prompt objectives and requirements
201
+
202
+ ## Success Criteria
203
+ - Optimized prompt scores higher than original on evaluation criteria
204
+ - Demonstrates measurable improvement in prompt effectiveness
205
+ - Maintains task objectives and context appropriateness
206
+ - Follows exact JSON output format
207
+ - Provides comprehensive optimization reasoning
208
+
209
+ ## Performance Requirements
210
+ - Complete optimization within ${mergedConfig.maxOptimizationTime}ms time limit
211
+ - Generate exactly ${mergedConfig.candidateCount} candidates
212
+ - Evaluate all candidates on all ${mergedConfig.evaluationCriteria.length} criteria
213
+ - Apply diversity weighting of ${mergedConfig.diversityWeight}
214
+ `;
215
+ return {
216
+ prompt,
217
+ instructions,
218
+ context: {
219
+ operation: 'ape_optimization',
220
+ originalPrompt,
221
+ config: mergedConfig,
222
+ apeConfig: mergedConfig, // Keep for backward compatibility
223
+ cacheKey,
224
+ improvementScore: 0.85, // Default improvement score for prompt generation
225
+ candidateCount: mergedConfig.candidateCount,
226
+ evaluationCriteria: mergedConfig.evaluationCriteria,
227
+ optimizationRounds: mergedConfig.optimizationRounds,
228
+ selectionStrategy: mergedConfig.selectionStrategy,
229
+ qualityThreshold: mergedConfig.qualityThreshold,
230
+ version: APE_VERSION,
231
+ timestamp: Date.now(),
232
+ securityLevel: 'high',
233
+ expectedFormat: 'json'
234
+ }
235
+ };
236
+ }
237
+ catch (error) {
238
+ throw new McpAdrError(`Failed to generate APE optimization prompt: ${error instanceof Error ? error.message : String(error)}`, 'APE_OPTIMIZATION_ERROR');
239
+ }
240
+ }
241
+ /**
242
+ * Generate AI delegation prompt for prompt candidate generation
243
+ * Returns prompts for AI to generate multiple prompt variations
244
+ */
245
+ export async function generatePromptCandidates(basePrompt, strategies, candidateCount) {
246
+ try {
247
+ console.error(`[DEBUG] Generating prompt candidate generation request for ${candidateCount} candidates`);
248
+ const prompt = `
249
+ # Prompt Candidate Generation Request
250
+
251
+ Please generate ${candidateCount} candidates using the specified strategies.
252
+
253
+ ## Base Prompt to Vary
254
+ \`\`\`
255
+ ${basePrompt.prompt}
256
+ \`\`\`
257
+
258
+ ## Base Instructions
259
+ \`\`\`
260
+ ${basePrompt.instructions}
261
+ \`\`\`
262
+
263
+ ## Generation Strategies to Apply
264
+ ${strategies.map((strategy, index) => `${index + 1}. **${strategy}**: ${getStrategyDescription(strategy)}`).join('\n')}
265
+
266
+ ## Candidate Generation Requirements
267
+
268
+ ### Strategy Distribution
269
+ Generate candidates using these strategies:
270
+ ${strategies.map(strategy => `- ${Math.ceil(candidateCount / strategies.length)} candidates using ${strategy}`).join('\n')}
271
+
272
+ ### Quality Standards
273
+ Each candidate must:
274
+ - Preserve the core objective of the original prompt
275
+ - Be grammatically correct and well-structured
276
+ - Provide clear, actionable instructions
277
+ - Maintain appropriate tone and style
278
+ - Be suitable for the intended context
279
+
280
+ ### Variation Guidelines
281
+ - **Maintain Core Function**: All candidates must achieve the same basic objective
282
+ - **Vary Approach**: Use different methods to achieve the objective
283
+ - **Ensure Diversity**: Candidates should be meaningfully different from each other
284
+ - **Preserve Context**: Keep relevant context and requirements
285
+ - **Optimize Clarity**: Each candidate should be clear and unambiguous
286
+
287
+ ## Expected Output Format
288
+ \`\`\`json
289
+ {
290
+ "candidateGeneration": {
291
+ "candidates": [
292
+ {
293
+ "candidateId": "candidate_1",
294
+ "prompt": "candidate prompt text",
295
+ "instructions": "candidate instructions",
296
+ "context": { "generation_metadata": "..." },
297
+ "generationStrategy": "${strategies[0]}",
298
+ "metadata": {
299
+ "generatedAt": "ISO-8601",
300
+ "strategy": "${strategies[0]}",
301
+ "variationApplied": ["specific changes made"],
302
+ "estimatedQuality": 0.8,
303
+ "complexity": 0.7,
304
+ "tokens": 150
305
+ }
306
+ }
307
+ ],
308
+ "generation": {
309
+ "totalCandidates": ${candidateCount},
310
+ "strategiesUsed": ${JSON.stringify(strategies)},
311
+ "generationTime": 30000,
312
+ "qualityDistribution": {
313
+ "high": 3,
314
+ "medium": 2,
315
+ "low": 0
316
+ }
317
+ }
318
+ }
319
+ }
320
+ \`\`\`
321
+
322
+ Generate exactly ${candidateCount} high-quality prompt candidates with meaningful variations.
323
+ `;
324
+ const instructions = `
325
+ # Candidate Generation Instructions
326
+
327
+ You must:
328
+ 1. **Apply All Strategies**: Use each specified generation strategy appropriately
329
+ 2. **Ensure Diversity**: Create meaningfully different candidates
330
+ 3. **Maintain Quality**: All candidates must meet quality standards
331
+ 4. **Preserve Function**: Keep the core objective of the original prompt
332
+ 5. **Document Changes**: Clearly indicate what variations were applied
333
+
334
+ ## Success Criteria
335
+ - Generate exactly ${candidateCount} candidates
336
+ - Use all ${strategies.length} specified strategies
337
+ - Ensure each candidate is functionally equivalent but stylistically different
338
+ - Provide complete metadata for each candidate
339
+ - Follow exact JSON output format
340
+ `;
341
+ return {
342
+ prompt,
343
+ instructions,
344
+ context: {
345
+ operation: 'candidate_generation',
346
+ basePrompt,
347
+ strategies,
348
+ candidateCount,
349
+ improvementScore: 0.75, // Default improvement score for candidate generation
350
+ version: APE_VERSION,
351
+ timestamp: Date.now(),
352
+ securityLevel: 'medium',
353
+ expectedFormat: 'json'
354
+ }
355
+ };
356
+ }
357
+ catch (error) {
358
+ throw new McpAdrError(`Failed to generate candidate generation prompt: ${error instanceof Error ? error.message : String(error)}`, 'CANDIDATE_GENERATION_ERROR');
359
+ }
360
+ }
361
+ /**
362
+ * Generate AI delegation prompt for prompt performance evaluation
363
+ * Returns prompts for AI to evaluate prompt effectiveness using multiple criteria
364
+ */
365
+ export async function evaluatePromptPerformance(candidates, evaluationCriteria, context = {}) {
366
+ try {
367
+ console.error(`[DEBUG] Generating prompt evaluation request for ${candidates.length} candidates`);
368
+ const prompt = `
369
+ # Prompt Performance Evaluation Request
370
+
371
+ Please evaluate the following ${candidates.length} candidates using the specified criteria.
372
+
373
+ ## Candidates to Evaluate
374
+ ${candidates.map((candidate, index) => `
375
+ ### Candidate ${index + 1}: ${candidate.id}
376
+ **Strategy**: ${candidate.generationStrategy}
377
+ **Prompt**:
378
+ \`\`\`
379
+ ${candidate.prompt}
380
+ \`\`\`
381
+ **Instructions**:
382
+ \`\`\`
383
+ ${candidate.instructions}
384
+ \`\`\`
385
+ **Metadata**: ${JSON.stringify(candidate.metadata, null, 2)}
386
+ `).join('\n')}
387
+
388
+ ## Evaluation Criteria
389
+ ${evaluationCriteria.map((criterion, index) => `${index + 1}. **${criterion}**: ${getCriterionDescription(criterion)}`).join('\n')}
390
+
391
+ ## Evaluation Context
392
+ ${JSON.stringify(context, null, 2)}
393
+
394
+ ## Evaluation Requirements
395
+
396
+ ### Scoring Guidelines (0-1 scale)
397
+ - **0.0-0.3**: Poor - Significant issues, major improvements needed
398
+ - **0.4-0.6**: Fair - Some issues, moderate improvements needed
399
+ - **0.7-0.8**: Good - Minor issues, small improvements possible
400
+ - **0.9-1.0**: Excellent - High quality, minimal improvements needed
401
+
402
+ ### Evaluation Process
403
+ For each candidate:
404
+ 1. **Analyze Content**: Review prompt text, instructions, and structure
405
+ 2. **Apply Criteria**: Score against each evaluation criterion
406
+ 3. **Identify Strengths**: Note what works well
407
+ 4. **Identify Weaknesses**: Note areas for improvement
408
+ 5. **Provide Feedback**: Give specific, actionable suggestions
409
+ 6. **Calculate Overall**: Compute weighted average score
410
+
411
+ ### Comparative Analysis
412
+ - Compare candidates against each other
413
+ - Identify best-performing candidates
414
+ - Note relative strengths and weaknesses
415
+ - Recommend selection priorities
416
+
417
+ ## Expected Output Format
418
+ \`\`\`json
419
+ {
420
+ "promptEvaluation": {
421
+ "evaluationResults": [
422
+ {
423
+ "candidateId": "${candidates[0]?.id || 'candidate_1'}",
424
+ "scores": {
425
+ ${evaluationCriteria.map(c => `"${c}": 0.8`).join(',\n ')}
426
+ },
427
+ "overallScore": 0.8,
428
+ "feedback": [
429
+ {
430
+ "criterion": "${evaluationCriteria[0] || 'task-completion'}",
431
+ "score": 0.8,
432
+ "reasoning": "detailed explanation of score",
433
+ "suggestions": ["specific improvement suggestion"],
434
+ "examples": ["example of good practice"]
435
+ }
436
+ ],
437
+ "strengths": ["strength 1", "strength 2"],
438
+ "weaknesses": ["weakness 1", "weakness 2"],
439
+ "improvementAreas": ["area 1", "area 2"],
440
+ "confidence": 0.85
441
+ }
442
+ ],
443
+ "comparativeAnalysis": {
444
+ "bestCandidate": "${candidates[0]?.id || 'candidate_1'}",
445
+ "rankingOrder": ["candidate_1", "candidate_2"],
446
+ "keyDifferentiators": ["differentiator 1", "differentiator 2"],
447
+ "recommendedSelection": "candidate_1",
448
+ "selectionReasoning": "explanation of recommendation"
449
+ },
450
+ "evaluation": {
451
+ "totalCandidates": ${candidates.length},
452
+ "criteriaUsed": ${JSON.stringify(evaluationCriteria)},
453
+ "evaluationTime": 45000,
454
+ "averageScore": 0.75,
455
+ "scoreDistribution": {
456
+ "excellent": 1,
457
+ "good": 2,
458
+ "fair": 1,
459
+ "poor": 0
460
+ }
461
+ }
462
+ }
463
+ }
464
+ \`\`\`
465
+
466
+ Provide thorough, objective evaluation with specific feedback for improvement.
467
+ `;
468
+ const instructions = `
469
+ # Prompt Evaluation Instructions
470
+
471
+ You must:
472
+ 1. **Evaluate Objectively**: Score each candidate fairly against all criteria
473
+ 2. **Provide Specific Feedback**: Give detailed, actionable suggestions
474
+ 3. **Compare Systematically**: Analyze relative strengths and weaknesses
475
+ 4. **Justify Scores**: Provide clear reasoning for each score
476
+ 5. **Recommend Selection**: Identify the best candidate with reasoning
477
+
478
+ ## Success Criteria
479
+ - Evaluate all ${candidates.length} candidates on all ${evaluationCriteria.length} criteria
480
+ - Provide scores between 0-1 with appropriate precision
481
+ - Give specific, actionable feedback for each candidate
482
+ - Include comparative analysis and selection recommendation
483
+ - Follow exact JSON output format
484
+ `;
485
+ return {
486
+ prompt,
487
+ instructions,
488
+ context: {
489
+ operation: 'prompt_evaluation',
490
+ candidates: candidates.map(c => c.id),
491
+ evaluationCriteria,
492
+ evaluationContext: context,
493
+ improvementScore: 0.80, // Default improvement score for evaluation
494
+ candidateCount: candidates.length,
495
+ version: APE_VERSION,
496
+ timestamp: Date.now(),
497
+ securityLevel: 'medium',
498
+ expectedFormat: 'json'
499
+ }
500
+ };
501
+ }
502
+ catch (error) {
503
+ throw new McpAdrError(`Failed to generate evaluation prompt: ${error instanceof Error ? error.message : String(error)}`, 'EVALUATION_ERROR');
504
+ }
505
+ }
506
+ /**
507
+ * Generate AI delegation prompt for tool-specific prompt optimization
508
+ * Returns prompts for AI to optimize prompts for specific MCP tools
509
+ */
510
+ export async function optimizeToolPrompt(toolName, basePrompt, toolConfig) {
511
+ try {
512
+ console.error(`[DEBUG] Generating tool-specific optimization prompt for ${toolName}`);
513
+ const prompt = `
514
+ # Tool-Specific Prompt Optimization Request
515
+
516
+ Please optimize the following prompt specifically for the ${toolName} MCP tool.
517
+
518
+ ## Tool Information
519
+ - **Tool Name**: ${toolName}
520
+ - **Task Type**: ${toolConfig.taskType}
521
+ - **Context Requirements**: ${toolConfig.contextRequirements?.join(', ') || 'Not specified'}
522
+
523
+ ## Base Prompt to Optimize
524
+ \`\`\`
525
+ ${basePrompt.prompt}
526
+ \`\`\`
527
+
528
+ ## Base Instructions
529
+ \`\`\`
530
+ ${basePrompt.instructions}
531
+ \`\`\`
532
+
533
+ ## Tool-Specific Configuration
534
+ ${JSON.stringify(toolConfig, null, 2)}
535
+
536
+ ## Tool-Specific Optimization Focus
537
+
538
+ ### ${toolName} Optimization Priorities
539
+ ${getToolOptimizationPriorities(toolName)}
540
+
541
+ ### Context Requirements
542
+ ${toolConfig.contextRequirements?.map((req, index) => `${index + 1}. ${req}`).join('\n') || 'No specific requirements'}
543
+
544
+ ### Success Criteria
545
+ ${toolConfig.successCriteria?.map((criteria, index) => `${index + 1}. ${criteria}`).join('\n') || 'Standard success criteria'}
546
+
547
+ ## Optimization Process
548
+
549
+ ### Step 1: Tool-Specific Analysis
550
+ - Analyze how the prompt fits the tool's specific use case
551
+ - Identify tool-specific optimization opportunities
552
+ - Consider the tool's typical input/output patterns
553
+ - Assess alignment with tool's learning objectives
554
+
555
+ ### Step 2: Domain-Aware Optimization
556
+ - Apply domain-specific knowledge and terminology
557
+ - Optimize for the tool's typical user scenarios
558
+ - Enhance prompt for the tool's specific context requirements
559
+ - Improve alignment with tool's success criteria
560
+
561
+ ### Step 3: Performance Enhancement
562
+ - Optimize for the tool's performance characteristics
563
+ - Enhance clarity for the tool's specific tasks
564
+ - Improve specificity for the tool's domain
565
+ - Optimize efficiency for the tool's typical workload
566
+
567
+ ## Expected Output Format
568
+ \`\`\`json
569
+ {
570
+ "toolOptimization": {
571
+ "optimizedPrompt": {
572
+ "prompt": "tool-optimized prompt text",
573
+ "instructions": "tool-optimized instructions",
574
+ "context": {
575
+ "tool_specific_metadata": "...",
576
+ "optimization_focus": "..."
577
+ }
578
+ },
579
+ "optimization": {
580
+ "toolName": "${toolName}",
581
+ "optimizationFocus": ["focus area 1", "focus area 2"],
582
+ "improvementAreas": ["improvement 1", "improvement 2"],
583
+ "toolSpecificEnhancements": ["enhancement 1", "enhancement 2"],
584
+ "performanceImprovements": {
585
+ "clarity": 0.85,
586
+ "specificity": 0.90,
587
+ "efficiency": 0.80,
588
+ "toolAlignment": 0.95
589
+ },
590
+ "optimizationReasoning": "detailed explanation of tool-specific optimizations"
591
+ }
592
+ }
593
+ }
594
+ \`\`\`
595
+
596
+ Focus on optimizations that specifically benefit the ${toolName} tool's use cases and performance.
597
+ `;
598
+ const instructions = `
599
+ # Tool-Specific Optimization Instructions
600
+
601
+ You must:
602
+ 1. **Understand Tool Context**: Consider the specific requirements of ${toolName}
603
+ 2. **Apply Domain Knowledge**: Use relevant domain expertise for optimization
604
+ 3. **Focus on Tool Benefits**: Prioritize improvements that benefit this specific tool
605
+ 4. **Maintain Compatibility**: Ensure optimizations work with the tool's architecture
606
+ 5. **Measure Improvement**: Quantify the benefits of tool-specific optimizations
607
+
608
+ ## Success Criteria
609
+ - Optimized prompt is specifically tailored for ${toolName}
610
+ - Demonstrates clear understanding of tool requirements
611
+ - Shows measurable improvement in tool-specific metrics
612
+ - Maintains compatibility with tool architecture
613
+ - Provides detailed reasoning for tool-specific choices
614
+ `;
615
+ return {
616
+ prompt,
617
+ instructions,
618
+ context: {
619
+ operation: 'ape_optimization',
620
+ toolName,
621
+ basePrompt,
622
+ toolConfig,
623
+ improvementScore: 0.85, // Default improvement score for tool optimization
624
+ toolOptimized: true,
625
+ candidateCount: toolConfig.apeConfig?.candidateCount || 5,
626
+ evaluationCriteria: toolConfig.apeConfig?.evaluationCriteria || ['task-completion', 'clarity'],
627
+ version: APE_VERSION,
628
+ timestamp: Date.now(),
629
+ securityLevel: 'high',
630
+ expectedFormat: 'json'
631
+ }
632
+ };
633
+ }
634
+ catch (error) {
635
+ throw new McpAdrError(`Failed to generate tool optimization prompt: ${error instanceof Error ? error.message : String(error)}`, 'TOOL_OPTIMIZATION_ERROR');
636
+ }
637
+ }
638
+ // ============================================================================
639
+ // Utility Functions and Helpers
640
+ // ============================================================================
641
+ /**
642
+ * Get description for a generation strategy
643
+ */
644
+ function getStrategyDescription(strategy) {
645
+ const descriptions = {
646
+ 'template-variation': 'Use different prompt templates and structural formats',
647
+ 'semantic-variation': 'Rephrase while preserving meaning and intent',
648
+ 'style-variation': 'Adjust tone, formality, and communication style',
649
+ 'length-variation': 'Create concise, detailed, and balanced versions',
650
+ 'structure-variation': 'Reorganize content with different patterns',
651
+ 'hybrid-approach': 'Combine multiple strategies for optimal results'
652
+ };
653
+ return descriptions[strategy] || 'Apply the specified generation strategy';
654
+ }
655
+ /**
656
+ * Get description for an evaluation criterion
657
+ */
658
+ function getCriterionDescription(criterion) {
659
+ const descriptions = {
660
+ 'task-completion': 'How well the prompt achieves the intended task outcome',
661
+ 'clarity': 'How clear, unambiguous, and easy to understand the prompt is',
662
+ 'specificity': 'How specific, actionable, and detailed the prompt is',
663
+ 'robustness': 'How well the prompt handles edge cases and variations',
664
+ 'efficiency': 'How concise yet comprehensive the prompt is',
665
+ 'context-awareness': 'How well the prompt fits the specific context and domain'
666
+ };
667
+ return descriptions[criterion] || 'Evaluate based on this criterion';
668
+ }
669
+ /**
670
+ * Get description for a selection strategy
671
+ */
672
+ function getSelectionStrategyDescription(strategy) {
673
+ const descriptions = {
674
+ 'highest-score': 'Select the candidate with the highest overall evaluation score',
675
+ 'multi-criteria': 'Balance multiple evaluation criteria with appropriate weights',
676
+ 'ensemble': 'Combine strengths of multiple high-performing candidates',
677
+ 'context-aware': 'Choose based on context-specific suitability and requirements',
678
+ 'balanced': 'Balance quality, diversity, and context appropriateness'
679
+ };
680
+ return descriptions[strategy] || 'Apply the specified selection strategy';
681
+ }
682
+ /**
683
+ * Get tool-specific optimization priorities
684
+ */
685
+ function getToolOptimizationPriorities(toolName) {
686
+ const priorities = {
687
+ 'generate_adrs_from_prd': `
688
+ - **Context Analysis**: Better understanding of PRD requirements and constraints
689
+ - **Decision Quality**: Clearer architectural decision rationale and justification
690
+ - **Stakeholder Alignment**: Improved ADR clarity and relevance for stakeholders
691
+ - **Technical Depth**: Appropriate level of technical detail for the context`,
692
+ 'suggest_adrs': `
693
+ - **Relevance**: Highly relevant ADR suggestions based on project context
694
+ - **Prioritization**: Clear prioritization of suggested ADRs by importance
695
+ - **Feasibility**: Realistic and implementable architectural decisions
696
+ - **Impact Assessment**: Clear understanding of decision impacts and trade-offs`,
697
+ 'analyze_project_ecosystem': `
698
+ - **Technology Detection**: Accurate identification of technologies and frameworks
699
+ - **Pattern Recognition**: Better identification of architectural patterns
700
+ - **Context Understanding**: Comprehensive project context analysis
701
+ - **Insight Generation**: Valuable and actionable architectural insights`,
702
+ 'generate_research_questions': `
703
+ - **Question Quality**: High-quality, targeted research questions
704
+ - **Relevance**: Questions directly relevant to project needs
705
+ - **Depth**: Appropriate depth and scope for research objectives
706
+ - **Actionability**: Questions that lead to actionable insights`,
707
+ 'incorporate_research': `
708
+ - **Integration Quality**: Effective integration of research findings
709
+ - **Synthesis**: Good synthesis of multiple research sources
710
+ - **Relevance**: Focus on most relevant research findings
711
+ - **Actionability**: Clear actionable recommendations from research`
712
+ };
713
+ return priorities[toolName] || `
714
+ - **Task Effectiveness**: Optimize for the tool's primary task objectives
715
+ - **Context Awareness**: Improve understanding of tool-specific context
716
+ - **Output Quality**: Enhance the quality and relevance of tool outputs
717
+ - **User Experience**: Improve clarity and usability of tool interactions`;
718
+ }
719
+ /**
720
+ * Generate cache key for APE optimization requests
721
+ */
722
+ export function generateAPECacheKey(promptHash, config) {
723
+ const configHash = Buffer.from(JSON.stringify(config)).toString('base64').substring(0, 16);
724
+ return `ape:optimization:${promptHash}-${configHash}`;
725
+ }
726
+ /**
727
+ * Get default APE configuration
728
+ */
729
+ export function getDefaultAPEConfig() {
730
+ return { ...DEFAULT_APE_CONFIG };
731
+ }
732
+ /**
733
+ * Validate APE configuration
734
+ */
735
+ export function validateAPEConfig(config) {
736
+ if (config.candidateCount !== undefined && (config.candidateCount < 1 || config.candidateCount > 10)) {
737
+ throw new McpAdrError('Candidate count must be between 1 and 10', 'INVALID_CONFIG');
738
+ }
739
+ if (config.optimizationRounds !== undefined && (config.optimizationRounds < 1 || config.optimizationRounds > 5)) {
740
+ throw new McpAdrError('Optimization rounds must be between 1 and 5', 'INVALID_CONFIG');
741
+ }
742
+ if (config.qualityThreshold !== undefined && (config.qualityThreshold < 0 || config.qualityThreshold > 1)) {
743
+ throw new McpAdrError('Quality threshold must be between 0 and 1', 'INVALID_CONFIG');
744
+ }
745
+ if (config.maxOptimizationTime !== undefined && config.maxOptimizationTime <= 0) {
746
+ throw new McpAdrError('Max optimization time must be positive', 'INVALID_CONFIG');
747
+ }
748
+ if (config.diversityWeight !== undefined && (config.diversityWeight < 0 || config.diversityWeight > 1)) {
749
+ throw new McpAdrError('Diversity weight must be between 0 and 1', 'INVALID_CONFIG');
750
+ }
751
+ }
752
+ /**
753
+ * Get supported generation strategies
754
+ */
755
+ export function getSupportedGenerationStrategies() {
756
+ return [
757
+ 'template-variation',
758
+ 'semantic-variation',
759
+ 'style-variation',
760
+ 'length-variation',
761
+ 'structure-variation',
762
+ 'hybrid-approach'
763
+ ];
764
+ }
765
+ /**
766
+ * Get supported evaluation criteria
767
+ */
768
+ export function getSupportedEvaluationCriteria() {
769
+ return [
770
+ 'task-completion',
771
+ 'clarity',
772
+ 'specificity',
773
+ 'robustness',
774
+ 'efficiency',
775
+ 'context-awareness'
776
+ ];
777
+ }
778
+ /**
779
+ * Get supported selection strategies
780
+ */
781
+ export function getSupportedSelectionStrategies() {
782
+ return [
783
+ 'highest-score',
784
+ 'multi-criteria',
785
+ 'ensemble',
786
+ 'context-aware',
787
+ 'balanced'
788
+ ];
789
+ }
790
+ /**
791
+ * Create tool-specific APE configuration
792
+ */
793
+ export function createToolAPEConfig(toolName, customConfig = {}) {
794
+ const toolConfigs = {
795
+ 'generate_adrs_from_prd': {
796
+ candidateCount: 7,
797
+ evaluationCriteria: ['task-completion', 'specificity', 'clarity', 'robustness'],
798
+ optimizationRounds: 3,
799
+ selectionStrategy: 'multi-criteria',
800
+ qualityThreshold: 0.75
801
+ },
802
+ 'suggest_adrs': {
803
+ candidateCount: 6,
804
+ evaluationCriteria: ['task-completion', 'context-awareness', 'clarity'],
805
+ optimizationRounds: 2,
806
+ selectionStrategy: 'context-aware',
807
+ qualityThreshold: 0.7
808
+ },
809
+ 'analyze_project_ecosystem': {
810
+ candidateCount: 5,
811
+ evaluationCriteria: ['task-completion', 'specificity', 'efficiency'],
812
+ optimizationRounds: 2,
813
+ selectionStrategy: 'balanced',
814
+ qualityThreshold: 0.65
815
+ }
816
+ };
817
+ const toolSpecificConfig = toolConfigs[toolName] || {};
818
+ return { ...DEFAULT_APE_CONFIG, ...toolSpecificConfig, ...customConfig };
819
+ }
820
+ //# sourceMappingURL=automatic-prompt-engineering.js.map