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,1100 @@
1
+ /**
2
+ * Reflexion Utility Module - 100% Prompt-Driven Architecture
3
+ * Implements Actor-Evaluator-Self-Reflection pattern for continuous learning
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_REFLEXION_CONFIG = {
11
+ memoryEnabled: true,
12
+ maxMemoryEntries: 100,
13
+ reflectionDepth: 'detailed',
14
+ evaluationCriteria: ['task-success', 'quality', 'efficiency'],
15
+ learningRate: 0.7,
16
+ memoryRetention: 90, // days
17
+ feedbackIntegration: true,
18
+ autoCleanup: true,
19
+ relevanceThreshold: 0.6,
20
+ confidenceThreshold: 0.7
21
+ };
22
+ const REFLEXION_VERSION = '1.0.0';
23
+ // ============================================================================
24
+ // Core Reflexion Functions - 100% Prompt-Driven
25
+ // ============================================================================
26
+ /**
27
+ * Generate AI delegation prompt for executing a task with reflexion learning
28
+ * Returns prompts for AI to execute tasks with memory-enhanced context
29
+ */
30
+ export async function executeWithReflexion(basePrompt, config = {}) {
31
+ try {
32
+ // Validate input prompt
33
+ if (!basePrompt.prompt || basePrompt.prompt.trim() === '') {
34
+ throw new McpAdrError('Prompt cannot be empty', 'INVALID_INPUT');
35
+ }
36
+ // Validate configuration
37
+ validateReflexionConfig(config);
38
+ const mergedConfig = { ...DEFAULT_REFLEXION_CONFIG, ...config };
39
+ console.error(`[DEBUG] Generating reflexion execution prompt with learning enabled`);
40
+ // Generate cache key for this reflexion request
41
+ const contextHash = Buffer.from(JSON.stringify(basePrompt)).toString('base64').substring(0, 16);
42
+ const configHash = Buffer.from(JSON.stringify(mergedConfig)).toString('base64').substring(0, 16);
43
+ const cacheKey = `reflexion:execution:${contextHash}-${configHash}`;
44
+ // Create comprehensive reflexion execution prompt
45
+ const prompt = `
46
+ # Reflexion-Enhanced Task Execution Request
47
+
48
+ Please execute the following task using the Reflexion learning pattern with memory-enhanced context.
49
+
50
+ ## Original Task
51
+ \`\`\`
52
+ ${basePrompt.prompt}
53
+ \`\`\`
54
+
55
+ ## Original Instructions
56
+ \`\`\`
57
+ ${basePrompt.instructions}
58
+ \`\`\`
59
+
60
+ ## Original Context
61
+ ${JSON.stringify(basePrompt.context, null, 2)}
62
+
63
+ ## Reflexion Configuration
64
+ - Memory Enabled: ${mergedConfig.memoryEnabled}
65
+ - Max Memory Entries: ${mergedConfig.maxMemoryEntries}
66
+ - Reflection Depth: ${mergedConfig.reflectionDepth}
67
+ - Evaluation Criteria: ${mergedConfig.evaluationCriteria.join(', ')}
68
+ - Learning Rate: ${mergedConfig.learningRate}
69
+ - Memory Retention: ${mergedConfig.memoryRetention} days
70
+ - Relevance Threshold: ${mergedConfig.relevanceThreshold}
71
+ - Confidence Threshold: ${mergedConfig.confidenceThreshold}
72
+
73
+ ## Reflexion Learning Process
74
+
75
+ ### Step 1: Memory Retrieval and Context Enhancement
76
+ 1. **Retrieve Relevant Memories**: Search for past experiences related to this task type
77
+ 2. **Analyze Past Lessons**: Review lessons learned from similar situations
78
+ 3. **Identify Successful Patterns**: Find approaches that worked well previously
79
+ 4. **Note Failure Patterns**: Identify mistakes and issues to avoid
80
+ 5. **Enhance Context**: Integrate memory insights into current task context
81
+
82
+ ### Step 2: Memory-Informed Task Execution
83
+ 1. **Apply Past Lessons**: Use lessons learned to inform current approach
84
+ 2. **Avoid Known Pitfalls**: Actively avoid previously identified mistakes
85
+ 3. **Leverage Successful Strategies**: Build on approaches that worked well
86
+ 4. **Adapt to Current Context**: Modify strategies based on current situation
87
+ 5. **Document Execution Path**: Record detailed steps and decision rationale
88
+
89
+ ### Step 3: Performance Evaluation
90
+ Evaluate the task execution on these criteria (0-1 scale):
91
+
92
+ ${mergedConfig.evaluationCriteria.map(criterion => {
93
+ const descriptions = {
94
+ 'task-success': 'Did the task achieve its intended outcome successfully?',
95
+ 'quality': 'How well was the task executed in terms of accuracy and completeness?',
96
+ 'efficiency': 'Was the approach optimal in terms of time and resource usage?',
97
+ 'accuracy': 'How accurate and correct were the results produced?',
98
+ 'completeness': 'Was the task fully completed with all requirements met?',
99
+ 'relevance': 'How relevant and appropriate was the output for the context?',
100
+ 'clarity': 'How clear and understandable was the communication and output?',
101
+ 'innovation': 'Was the approach creative or novel in addressing the task?'
102
+ };
103
+ return `- **${criterion}**: ${descriptions[criterion] || 'Evaluate based on this criterion'}`;
104
+ }).join('\n')}
105
+
106
+ ### Step 4: Self-Reflection and Lesson Extraction
107
+ Generate ${mergedConfig.reflectionDepth} self-reflection including:
108
+
109
+ #### Success Analysis
110
+ - What worked well and why?
111
+ - Which strategies were most effective?
112
+ - What patterns led to positive outcomes?
113
+ - How did past lessons contribute to success?
114
+
115
+ #### Failure Analysis (if applicable)
116
+ - What went wrong and why?
117
+ - Which approaches were ineffective?
118
+ - What mistakes were made?
119
+ - How can similar issues be prevented?
120
+
121
+ #### Lesson Extraction
122
+ - What new lessons can be learned from this experience?
123
+ - How can successful approaches be generalized?
124
+ - What knowledge gaps were identified?
125
+ - What strategies should be refined or improved?
126
+
127
+ #### Future Strategy Development
128
+ - How can the approach be improved for next time?
129
+ - What new strategies should be considered?
130
+ - How can lessons be applied to similar tasks?
131
+ - What additional knowledge or skills are needed?
132
+
133
+ ### Step 5: Memory Integration and Persistence
134
+ 1. **Create New Memories**: Generate memory entries for significant lessons
135
+ 2. **Update Existing Memories**: Strengthen or modify existing memories based on new evidence
136
+ 3. **Link Related Memories**: Connect new memories to related existing ones
137
+ 4. **Categorize Memories**: Assign appropriate memory types and categories
138
+ 5. **Set Memory Persistence**: Determine memory retention and access patterns
139
+
140
+ ## Expected Output Format
141
+ \`\`\`json
142
+ {
143
+ "reflexionExecution": {
144
+ "enhancedPrompt": {
145
+ "prompt": "memory-enhanced task execution prompt",
146
+ "instructions": "enhanced instructions with memory context",
147
+ "context": {
148
+ "memory_enhanced": true,
149
+ "memories_used": ["memory_id_1", "memory_id_2"],
150
+ "learning_applied": true,
151
+ "original_context": "..."
152
+ }
153
+ },
154
+ "taskAttempt": {
155
+ "attemptId": "attempt_${Date.now()}",
156
+ "taskType": "task_classification",
157
+ "context": ${JSON.stringify(basePrompt.context)},
158
+ "action": "detailed description of actions taken",
159
+ "outcome": {
160
+ "success": true,
161
+ "result": "task execution result",
162
+ "errors": [],
163
+ "warnings": [],
164
+ "executionTime": 5000,
165
+ "resourcesUsed": {
166
+ "memoryAccessed": 3,
167
+ "memoryCreated": 1,
168
+ "processingTime": 5000,
169
+ "promptTokens": 1500,
170
+ "cacheHits": 2,
171
+ "cacheMisses": 1
172
+ },
173
+ "qualityMetrics": {
174
+ "accuracy": 0.85,
175
+ "completeness": 0.90,
176
+ "relevance": 0.88,
177
+ "clarity": 0.82,
178
+ "innovation": 0.75,
179
+ "efficiency": 0.80
180
+ }
181
+ },
182
+ "evaluation": {
183
+ "overallScore": 0.85,
184
+ "criteriaScores": {
185
+ ${mergedConfig.evaluationCriteria.map(c => `"${c}": 0.8`).join(',\n ')}
186
+ },
187
+ "feedback": [
188
+ {
189
+ "criterion": "${mergedConfig.evaluationCriteria[0]}",
190
+ "score": 0.8,
191
+ "reasoning": "detailed explanation of score",
192
+ "suggestions": ["specific improvement suggestion"],
193
+ "severity": "medium"
194
+ }
195
+ ],
196
+ "strengths": ["strength 1", "strength 2"],
197
+ "weaknesses": ["weakness 1", "weakness 2"],
198
+ "improvementAreas": ["area 1", "area 2"],
199
+ "confidence": 0.85,
200
+ "evaluationTime": 2000,
201
+ "evaluatorVersion": "${REFLEXION_VERSION}"
202
+ },
203
+ "reflection": {
204
+ "reflectionId": "reflection_${Date.now()}",
205
+ "reflectionText": "comprehensive self-reflection on the task execution",
206
+ "lessonsLearned": [
207
+ {
208
+ "lesson": "specific lesson learned",
209
+ "category": "strategy",
210
+ "importance": "high",
211
+ "evidence": ["evidence supporting this lesson"],
212
+ "applicableContexts": ["context where this applies"],
213
+ "confidence": 0.9,
214
+ "generalizability": 0.8
215
+ }
216
+ ],
217
+ "actionableInsights": [
218
+ {
219
+ "insight": "actionable insight for improvement",
220
+ "action": "specific action to take",
221
+ "priority": "high",
222
+ "timeframe": "immediate",
223
+ "expectedImpact": 0.7,
224
+ "riskLevel": "low"
225
+ }
226
+ ],
227
+ "futureStrategies": [
228
+ {
229
+ "strategy": "improved strategy for future tasks",
230
+ "description": "detailed strategy description",
231
+ "applicableScenarios": ["scenario 1", "scenario 2"],
232
+ "expectedOutcomes": ["outcome 1", "outcome 2"],
233
+ "successMetrics": ["metric 1", "metric 2"]
234
+ }
235
+ ],
236
+ "knowledgeGaps": [
237
+ {
238
+ "gap": "identified knowledge gap",
239
+ "category": "technical",
240
+ "impact": "medium",
241
+ "learningPriority": 0.7,
242
+ "suggestedResources": ["resource 1", "resource 2"]
243
+ }
244
+ ],
245
+ "confidenceLevel": 0.8,
246
+ "applicability": ["context 1", "context 2"],
247
+ "reflectionTime": 3000
248
+ },
249
+ "timestamp": "${new Date().toISOString()}",
250
+ "relatedMemories": ["memory_id_1", "memory_id_2"],
251
+ "generatedMemories": ["new_memory_id_1"]
252
+ },
253
+ "learningOutcome": {
254
+ "lessonsLearned": 2,
255
+ "memoriesCreated": 1,
256
+ "memoriesUpdated": 2,
257
+ "improvementAchieved": 0.15,
258
+ "knowledgeGapsIdentified": 1,
259
+ "strategiesRefined": 1,
260
+ "confidenceChange": 0.1
261
+ },
262
+ "memoryUpdates": [
263
+ {
264
+ "memoryId": "memory_id_1",
265
+ "updateType": "strengthen",
266
+ "changes": ["increased confidence", "added evidence"],
267
+ "reason": "successful application in current task",
268
+ "impact": 0.2
269
+ }
270
+ ],
271
+ "metadata": {
272
+ "reflexionVersion": "${REFLEXION_VERSION}",
273
+ "executedAt": "${new Date().toISOString()}",
274
+ "cacheKey": "${cacheKey}",
275
+ "configUsed": ${JSON.stringify(mergedConfig, null, 6)},
276
+ "performanceMetrics": {
277
+ "totalReflexionTime": 10000,
278
+ "memoryRetrievalTime": 2000,
279
+ "evaluationTime": 2000,
280
+ "reflectionTime": 3000,
281
+ "memoryIntegrationTime": 1000,
282
+ "memoriesAccessed": 3,
283
+ "memoriesCreated": 1,
284
+ "learningEfficiency": 0.8
285
+ }
286
+ }
287
+ }
288
+ }
289
+ \`\`\`
290
+
291
+ ## Quality Requirements
292
+ - Apply relevant lessons from past experiences
293
+ - Demonstrate clear learning from previous mistakes
294
+ - Show improvement over baseline approaches without memory
295
+ - Generate valuable new insights for future learning
296
+ - Create high-quality memories for future reference
297
+
298
+ ## Security and Validation
299
+ - Ensure all generated memories are safe and appropriate
300
+ - Validate that lessons learned are accurate and beneficial
301
+ - Verify that reflection content is constructive and helpful
302
+ - Check for potential biases in learning and memory creation
303
+
304
+ Execute the task with full reflexion learning and document the complete learning process.
305
+ `;
306
+ const instructions = `
307
+ # Reflexion Execution Instructions
308
+
309
+ You must:
310
+ 1. **Retrieve Relevant Memories**: Find and apply past experiences related to this task
311
+ 2. **Execute with Memory Context**: Use memory insights to inform task execution
312
+ 3. **Evaluate Performance**: Score the execution against all specified criteria
313
+ 4. **Generate Deep Reflection**: Create comprehensive self-reflection with lessons learned
314
+ 5. **Update Memory System**: Create and update memories based on the experience
315
+ 6. **Document Learning**: Provide clear evidence of learning and improvement
316
+
317
+ ## Success Criteria
318
+ - Task execution shows clear application of past lessons
319
+ - Evaluation provides objective assessment of performance
320
+ - Reflection generates valuable insights and lessons
321
+ - Memory updates are appropriate and beneficial
322
+ - Overall learning outcome demonstrates improvement
323
+ - Follows exact JSON output format
324
+
325
+ ## Learning Requirements
326
+ - Must demonstrate learning from past experiences
327
+ - Should show improvement over non-reflexion approaches
328
+ - Must generate actionable insights for future tasks
329
+ - Should identify and address knowledge gaps
330
+ - Must create valuable memories for future reference
331
+ `;
332
+ return {
333
+ prompt,
334
+ instructions,
335
+ context: {
336
+ operation: 'reflexion_execution',
337
+ originalPrompt: basePrompt,
338
+ config: mergedConfig,
339
+ reflexionConfig: mergedConfig, // Keep for backward compatibility
340
+ cacheKey,
341
+ memoriesUsed: [], // Required by validation function - empty array for prompt generation
342
+ reflectionDepth: mergedConfig.reflectionDepth,
343
+ evaluationCriteria: mergedConfig.evaluationCriteria,
344
+ memoryEnabled: mergedConfig.memoryEnabled,
345
+ maxMemoryEntries: mergedConfig.maxMemoryEntries,
346
+ learningRate: mergedConfig.learningRate,
347
+ relevanceThreshold: mergedConfig.relevanceThreshold,
348
+ confidenceThreshold: mergedConfig.confidenceThreshold,
349
+ version: REFLEXION_VERSION,
350
+ timestamp: Date.now(),
351
+ securityLevel: 'high',
352
+ expectedFormat: 'json'
353
+ }
354
+ };
355
+ }
356
+ catch (error) {
357
+ throw new McpAdrError(`Failed to generate reflexion execution prompt: ${error instanceof Error ? error.message : String(error)}`, 'REFLEXION_EXECUTION_ERROR');
358
+ }
359
+ }
360
+ /**
361
+ * Generate AI delegation prompt for retrieving relevant memories
362
+ * Returns prompts for AI to search and retrieve relevant past experiences
363
+ */
364
+ export async function retrieveRelevantMemories(taskType, context, query = {}) {
365
+ try {
366
+ console.error(`[DEBUG] Generating memory retrieval prompt for task type: ${taskType}`);
367
+ const prompt = `
368
+ # Memory Retrieval Request
369
+
370
+ Please search and retrieve relevant memories for the current task context.
371
+
372
+ ## Task Information
373
+ - **Task Type**: ${taskType}
374
+ - **Context**: ${JSON.stringify(context, null, 2)}
375
+
376
+ ## Memory Query Parameters
377
+ - Memory Types: ${query.memoryTypes?.join(', ') || 'All types'}
378
+ - Keywords: ${query.keywords?.join(', ') || 'Auto-detect from context'}
379
+ - Time Range: ${query.timeRange ? `${query.timeRange.start} to ${query.timeRange.end}` : 'All time'}
380
+ - Relevance Threshold: ${query.relevanceThreshold || 0.6}
381
+ - Max Results: ${query.maxResults || 10}
382
+
383
+ ## Memory Search Process
384
+
385
+ ### Step 1: Context Analysis
386
+ 1. **Extract Key Concepts**: Identify important concepts, technologies, and patterns from the context
387
+ 2. **Determine Task Category**: Classify the task type and domain
388
+ 3. **Identify Search Terms**: Generate relevant search terms and keywords
389
+ 4. **Assess Context Similarity**: Prepare criteria for matching similar contexts
390
+
391
+ ### Step 2: Memory Search Strategy
392
+ 1. **Keyword Matching**: Search for memories containing relevant keywords
393
+ 2. **Context Similarity**: Find memories from similar task contexts
394
+ 3. **Pattern Matching**: Look for memories with similar patterns or approaches
395
+ 4. **Temporal Relevance**: Consider recency and temporal patterns
396
+ 5. **Success Correlation**: Prioritize memories from successful past attempts
397
+
398
+ ### Step 3: Relevance Scoring
399
+ Score each memory on relevance (0-1 scale) based on:
400
+ - **Context Similarity**: How similar is the memory context to current context
401
+ - **Task Type Match**: How well does the memory task type match current task
402
+ - **Keyword Overlap**: How many relevant keywords are present
403
+ - **Success Rate**: How successful were the approaches in the memory
404
+ - **Recency**: How recent and relevant is the memory
405
+
406
+ ### Step 4: Memory Selection and Ranking
407
+ 1. **Apply Relevance Threshold**: Filter memories below relevance threshold
408
+ 2. **Rank by Relevance**: Sort memories by relevance score
409
+ 3. **Diversify Selection**: Ensure variety in memory types and approaches
410
+ 4. **Limit Results**: Return top memories up to max results limit
411
+
412
+ ## Expected Output Format
413
+ \`\`\`json
414
+ {
415
+ "memoryRetrieval": {
416
+ "searchResults": [
417
+ {
418
+ "memoryId": "memory_001",
419
+ "memoryType": "episodic",
420
+ "relevanceScore": 0.85,
421
+ "content": {
422
+ "summary": "brief summary of the memory",
423
+ "details": "detailed memory content",
424
+ "context": { "task_context": "..." },
425
+ "lessons": ["lesson 1", "lesson 2"],
426
+ "applicableScenarios": ["scenario 1", "scenario 2"],
427
+ "evidence": ["evidence 1", "evidence 2"],
428
+ "outcomes": ["outcome 1", "outcome 2"],
429
+ "strategies": ["strategy 1", "strategy 2"]
430
+ },
431
+ "metadata": {
432
+ "source": "task_execution",
433
+ "quality": 0.8,
434
+ "reliability": 0.9,
435
+ "generalizability": 0.7,
436
+ "category": "strategy",
437
+ "importance": "high"
438
+ },
439
+ "accessCount": 5,
440
+ "lastAccessed": "2024-01-01T00:00:00Z",
441
+ "createdAt": "2023-12-01T00:00:00Z"
442
+ }
443
+ ],
444
+ "searchMetadata": {
445
+ "totalFound": 15,
446
+ "searchTime": 1500,
447
+ "relevanceScores": {
448
+ "memory_001": 0.85,
449
+ "memory_002": 0.78
450
+ },
451
+ "searchStrategy": "hybrid_keyword_context",
452
+ "indexesUsed": ["keyword_index", "context_index"],
453
+ "cacheHits": 3,
454
+ "searchQuality": 0.8
455
+ },
456
+ "searchSummary": {
457
+ "taskType": "${taskType}",
458
+ "contextAnalysis": "analysis of the provided context",
459
+ "keyConceptsExtracted": ["concept 1", "concept 2"],
460
+ "searchTermsUsed": ["term 1", "term 2"],
461
+ "memoryTypesFound": ["episodic", "semantic"],
462
+ "averageRelevance": 0.75,
463
+ "recommendedMemories": ["memory_001", "memory_002"]
464
+ }
465
+ }
466
+ }
467
+ \`\`\`
468
+
469
+ ## Search Quality Requirements
470
+ - Find memories with relevance score above ${query.relevanceThreshold || 0.6}
471
+ - Prioritize memories from successful past experiences
472
+ - Include diverse memory types when available
473
+ - Provide clear relevance reasoning for each memory
474
+ - Ensure memories are applicable to the current context
475
+
476
+ ## Memory File Locations
477
+ Search in these directories:
478
+ - **Episodic**: docs/reflexion-memory/episodic/
479
+ - **Semantic**: docs/reflexion-memory/semantic/
480
+ - **Procedural**: docs/reflexion-memory/procedural/
481
+ - **Meta**: docs/reflexion-memory/meta/
482
+ - **Indexes**: docs/reflexion-memory/indexes/
483
+
484
+ Return the most relevant memories that can help inform the current task execution.
485
+ `;
486
+ const instructions = `
487
+ # Memory Retrieval Instructions
488
+
489
+ You must:
490
+ 1. **Analyze Context**: Understand the task context and requirements
491
+ 2. **Search Systematically**: Use multiple search strategies to find relevant memories
492
+ 3. **Score Relevance**: Provide accurate relevance scores for each memory
493
+ 4. **Select Appropriately**: Choose the most helpful memories for the current task
494
+ 5. **Document Search**: Provide clear search metadata and reasoning
495
+
496
+ ## Success Criteria
497
+ - Find memories with high relevance to the current task
498
+ - Provide accurate relevance scoring and reasoning
499
+ - Include diverse memory types when available
500
+ - Return memories that can actually help with task execution
501
+ - Follow exact JSON output format
502
+
503
+ ## Search Effectiveness
504
+ - Prioritize memories from successful past attempts
505
+ - Consider both exact matches and similar contexts
506
+ - Balance recency with relevance and quality
507
+ - Ensure returned memories are actionable and helpful
508
+ `;
509
+ return {
510
+ prompt,
511
+ instructions,
512
+ context: {
513
+ operation: 'memory_retrieval',
514
+ taskType,
515
+ searchContext: context,
516
+ queryParameters: query,
517
+ memoriesUsed: [], // Required by validation function - empty for search operation
518
+ version: REFLEXION_VERSION,
519
+ timestamp: Date.now(),
520
+ securityLevel: 'medium',
521
+ expectedFormat: 'json'
522
+ }
523
+ };
524
+ }
525
+ catch (error) {
526
+ throw new McpAdrError(`Failed to generate memory retrieval prompt: ${error instanceof Error ? error.message : String(error)}`, 'MEMORY_RETRIEVAL_ERROR');
527
+ }
528
+ }
529
+ /**
530
+ * Generate AI delegation prompt for persisting reflexion memories
531
+ * Returns prompts for AI to save memories using file system operations
532
+ */
533
+ export async function persistReflexionMemory(memory) {
534
+ try {
535
+ console.error(`[DEBUG] Generating memory persistence prompt for memory: ${memory.memoryId}`);
536
+ const prompt = `
537
+ # Memory Persistence Request
538
+
539
+ Please save the following Reflexion memory to the appropriate file location using the file system.
540
+
541
+ ## Memory Details
542
+ - **Memory ID**: ${memory.memoryId}
543
+ - **Memory Type**: ${memory.memoryType}
544
+ - **Category**: ${memory.metadata.category}
545
+ - **Importance**: ${memory.metadata.importance}
546
+ - **Created**: ${memory.createdAt}
547
+ - **Expires**: ${memory.expiresAt || 'Never'}
548
+
549
+ ## Memory Content
550
+ \`\`\`json
551
+ ${JSON.stringify(memory, null, 2)}
552
+ \`\`\`
553
+
554
+ ## File System Operations Required
555
+
556
+ ### Step 1: Determine File Path
557
+ Based on memory type and category:
558
+ - **Episodic**: docs/reflexion-memory/episodic/{category}/{memoryId}.json
559
+ - **Semantic**: docs/reflexion-memory/semantic/{category}/{memoryId}.json
560
+ - **Procedural**: docs/reflexion-memory/procedural/{category}/{memoryId}.json
561
+ - **Meta**: docs/reflexion-memory/meta/{category}/{memoryId}.json
562
+ - **Feedback**: docs/reflexion-memory/feedback/{category}/{memoryId}.json
563
+
564
+ ### Step 2: Create Directory Structure
565
+ Ensure the following directories exist:
566
+ - docs/reflexion-memory/
567
+ - docs/reflexion-memory/${memory.memoryType}/
568
+ - docs/reflexion-memory/${memory.memoryType}/${memory.metadata.category}/
569
+ - docs/reflexion-memory/indexes/
570
+
571
+ ### Step 3: Save Memory File
572
+ 1. **Create JSON File**: Save memory as formatted JSON
573
+ 2. **Set Permissions**: Ensure appropriate file permissions
574
+ 3. **Validate Content**: Verify file was saved correctly
575
+ 4. **Create Backup**: Create backup if updating existing memory
576
+
577
+ ### Step 4: Update Memory Indexes
578
+ Update the following index files:
579
+ 1. **Memory Catalog**: docs/reflexion-memory/indexes/memory-catalog.json
580
+ 2. **Type Index**: docs/reflexion-memory/indexes/${memory.memoryType}-index.json
581
+ 3. **Category Index**: docs/reflexion-memory/indexes/category-index.json
582
+ 4. **Temporal Index**: docs/reflexion-memory/indexes/temporal-index.json
583
+
584
+ ### Step 5: Maintain Index Integrity
585
+ Ensure indexes contain:
586
+ - Memory ID and file path mapping
587
+ - Memory metadata for quick search
588
+ - Relevance and quality scores
589
+ - Access patterns and statistics
590
+ - Expiration and cleanup information
591
+
592
+ ## Expected File Structure
593
+ \`\`\`
594
+ docs/reflexion-memory/
595
+ ├── ${memory.memoryType}/
596
+ │ └── ${memory.metadata.category}/
597
+ │ └── ${memory.memoryId}.json
598
+ └── indexes/
599
+ ├── memory-catalog.json
600
+ ├── ${memory.memoryType}-index.json
601
+ ├── category-index.json
602
+ └── temporal-index.json
603
+ \`\`\`
604
+
605
+ ## Index Entry Format
606
+ Add this entry to memory-catalog.json:
607
+ \`\`\`json
608
+ {
609
+ "memoryId": "${memory.memoryId}",
610
+ "filePath": "docs/reflexion-memory/${memory.memoryType}/${memory.metadata.category}/${memory.memoryId}.json",
611
+ "memoryType": "${memory.memoryType}",
612
+ "category": "${memory.metadata.category}",
613
+ "importance": "${memory.metadata.importance}",
614
+ "quality": ${memory.metadata.quality},
615
+ "relevanceScore": ${memory.relevanceScore},
616
+ "createdAt": "${memory.createdAt}",
617
+ "expiresAt": "${memory.expiresAt || 'null'}",
618
+ "tags": ${JSON.stringify(memory.tags)},
619
+ "accessCount": ${memory.accessCount},
620
+ "lastAccessed": "${memory.lastAccessed}"
621
+ }
622
+ \`\`\`
623
+
624
+ ## Validation Requirements
625
+ - Verify file was created successfully
626
+ - Confirm JSON is valid and properly formatted
627
+ - Ensure indexes were updated correctly
628
+ - Validate directory structure is correct
629
+ - Check file permissions are appropriate
630
+
631
+ Please execute these file operations and confirm successful memory persistence.
632
+ `;
633
+ const instructions = `
634
+ # Memory Persistence Instructions
635
+
636
+ You must:
637
+ 1. **Create Directory Structure**: Ensure all required directories exist
638
+ 2. **Save Memory File**: Write memory as properly formatted JSON
639
+ 3. **Update All Indexes**: Maintain index integrity and searchability
640
+ 4. **Validate Storage**: Confirm successful file creation and content
641
+ 5. **Handle Errors**: Provide clear error messages if operations fail
642
+
643
+ ## Success Criteria
644
+ - Memory file is saved in correct location with proper format
645
+ - All relevant indexes are updated with new memory entry
646
+ - Directory structure is created if it doesn't exist
647
+ - File permissions and access are set correctly
648
+ - Validation confirms successful storage
649
+
650
+ ## Error Handling
651
+ - Report any file system errors clearly
652
+ - Suggest solutions for common issues
653
+ - Ensure partial failures don't corrupt indexes
654
+ - Provide rollback information if needed
655
+ `;
656
+ return {
657
+ prompt,
658
+ instructions,
659
+ context: {
660
+ operation: 'memory_persistence',
661
+ memoryId: memory.memoryId,
662
+ memoryType: memory.memoryType,
663
+ filePath: `docs/reflexion-memory/${memory.memoryType}/${memory.metadata.category}/${memory.memoryId}.json`,
664
+ securityLevel: 'medium',
665
+ expectedFormat: 'confirmation'
666
+ }
667
+ };
668
+ }
669
+ catch (error) {
670
+ throw new McpAdrError(`Failed to generate memory persistence prompt: ${error instanceof Error ? error.message : String(error)}`, 'MEMORY_PERSISTENCE_ERROR');
671
+ }
672
+ }
673
+ /**
674
+ * Generate AI delegation prompt for tracking learning progress
675
+ * Returns prompts for AI to analyze and report learning progress
676
+ */
677
+ export async function getLearningProgress(taskType) {
678
+ try {
679
+ console.error(`[DEBUG] Generating learning progress analysis prompt for task type: ${taskType}`);
680
+ const prompt = `
681
+ # Learning Progress Analysis Request
682
+
683
+ Please analyze the learning progress for the specified task type using available memory data.
684
+
685
+ ## Task Type
686
+ **${taskType}**
687
+
688
+ ## Analysis Requirements
689
+
690
+ ### Step 1: Memory Collection
691
+ 1. **Gather Task Memories**: Collect all memories related to ${taskType}
692
+ 2. **Organize by Time**: Sort memories chronologically
693
+ 3. **Categorize by Type**: Group by episodic, semantic, procedural, meta
694
+ 4. **Filter by Quality**: Focus on high-quality, reliable memories
695
+
696
+ ### Step 2: Performance Trend Analysis
697
+ 1. **Success Rate Calculation**: Calculate success rate over time
698
+ 2. **Score Progression**: Analyze evaluation score trends
699
+ 3. **Improvement Detection**: Identify periods of improvement or decline
700
+ 4. **Pattern Recognition**: Find recurring patterns in performance
701
+
702
+ ### Step 3: Learning Effectiveness Assessment
703
+ 1. **Lesson Application**: How well are lessons being applied?
704
+ 2. **Knowledge Retention**: Are lessons being retained over time?
705
+ 3. **Strategy Evolution**: How are strategies improving?
706
+ 4. **Error Reduction**: Are similar mistakes being avoided?
707
+
708
+ ### Step 4: Knowledge Gap Identification
709
+ 1. **Persistent Issues**: What problems keep recurring?
710
+ 2. **Missing Knowledge**: What knowledge areas need development?
711
+ 3. **Skill Gaps**: What skills need improvement?
712
+ 4. **Learning Opportunities**: Where can learning be enhanced?
713
+
714
+ ### Step 5: Plateau Detection
715
+ 1. **Performance Stagnation**: Is learning progress stagnating?
716
+ 2. **Plateau Duration**: How long has performance been flat?
717
+ 3. **Intervention Needs**: What interventions might help?
718
+ 4. **Alternative Approaches**: What new approaches should be tried?
719
+
720
+ ## Expected Output Format
721
+ \`\`\`json
722
+ {
723
+ "learningProgress": {
724
+ "taskType": "${taskType}",
725
+ "totalAttempts": 25,
726
+ "successRate": 0.84,
727
+ "averageScore": 0.78,
728
+ "improvementTrend": 0.15,
729
+ "lastImprovement": "2024-01-15T10:30:00Z",
730
+ "keyLessons": [
731
+ "Always validate assumptions with stakeholders",
732
+ "Use domain-specific terminology for clarity",
733
+ "Consider scalability implications early"
734
+ ],
735
+ "persistentIssues": [
736
+ "Difficulty with complex multi-stakeholder scenarios",
737
+ "Inconsistent handling of legacy system constraints"
738
+ ],
739
+ "nextFocusAreas": [
740
+ "Stakeholder communication strategies",
741
+ "Legacy system integration patterns",
742
+ "Performance optimization techniques"
743
+ ],
744
+ "learningVelocity": 0.12,
745
+ "plateauDetection": {
746
+ "isOnPlateau": false,
747
+ "plateauDuration": 0,
748
+ "plateauConfidence": 0.2,
749
+ "suggestedInterventions": [],
750
+ "alternativeApproaches": [
751
+ "Try different evaluation criteria",
752
+ "Introduce complexity variations",
753
+ "Seek external feedback"
754
+ ]
755
+ },
756
+ "metadata": {
757
+ "trackingStarted": "2023-11-01T00:00:00Z",
758
+ "lastUpdated": "2024-01-20T15:45:00Z",
759
+ "dataQuality": 0.85,
760
+ "sampleSize": 25,
761
+ "confidenceInterval": 0.92,
762
+ "statisticalSignificance": 0.88,
763
+ "trendAnalysis": {
764
+ "shortTermTrend": 0.08,
765
+ "mediumTermTrend": 0.15,
766
+ "longTermTrend": 0.22,
767
+ "volatility": 0.12,
768
+ "predictability": 0.78
769
+ }
770
+ },
771
+ "performanceBreakdown": {
772
+ "byEvaluationCriteria": {
773
+ "task-success": 0.85,
774
+ "quality": 0.78,
775
+ "efficiency": 0.72,
776
+ "accuracy": 0.82,
777
+ "completeness": 0.80
778
+ },
779
+ "byTimeperiod": {
780
+ "last7Days": 0.82,
781
+ "last30Days": 0.78,
782
+ "last90Days": 0.75,
783
+ "overall": 0.78
784
+ },
785
+ "byComplexity": {
786
+ "simple": 0.92,
787
+ "moderate": 0.78,
788
+ "complex": 0.65
789
+ }
790
+ },
791
+ "recommendations": [
792
+ "Focus on improving complex scenario handling",
793
+ "Develop stakeholder communication templates",
794
+ "Create decision trees for legacy system scenarios",
795
+ "Implement regular knowledge gap assessments"
796
+ ]
797
+ }
798
+ }
799
+ \`\`\`
800
+
801
+ ## Analysis Quality Requirements
802
+ - Use all available memory data for comprehensive analysis
803
+ - Provide accurate statistical calculations and trends
804
+ - Identify actionable insights and recommendations
805
+ - Detect learning plateaus and suggest interventions
806
+ - Ensure recommendations are specific and implementable
807
+
808
+ ## Memory Data Sources
809
+ Analyze data from:
810
+ - **Episodic Memories**: docs/reflexion-memory/episodic/
811
+ - **Performance Records**: Task attempt outcomes and evaluations
812
+ - **Learning Outcomes**: Lessons learned and strategy refinements
813
+ - **Temporal Patterns**: Time-based performance trends
814
+
815
+ Provide comprehensive learning progress analysis with actionable insights.
816
+ `;
817
+ const instructions = `
818
+ # Learning Progress Analysis Instructions
819
+
820
+ You must:
821
+ 1. **Collect Comprehensive Data**: Gather all relevant memory and performance data
822
+ 2. **Analyze Trends**: Calculate accurate performance trends and patterns
823
+ 3. **Identify Insights**: Find actionable insights and learning opportunities
824
+ 4. **Detect Issues**: Identify persistent problems and learning plateaus
825
+ 5. **Provide Recommendations**: Suggest specific, implementable improvements
826
+
827
+ ## Success Criteria
828
+ - Analysis covers all available data comprehensively
829
+ - Trends and statistics are accurate and meaningful
830
+ - Insights are actionable and specific to the task type
831
+ - Plateau detection is accurate with helpful suggestions
832
+ - Recommendations are practical and implementable
833
+ - Follows exact JSON output format
834
+
835
+ ## Analysis Quality
836
+ - Use statistical methods for trend analysis
837
+ - Consider both short-term and long-term patterns
838
+ - Balance optimism with realistic assessment
839
+ - Focus on actionable insights over general observations
840
+ `;
841
+ return {
842
+ prompt,
843
+ instructions,
844
+ context: {
845
+ operation: 'learning_progress_analysis',
846
+ taskType,
847
+ analysisScope: 'comprehensive',
848
+ securityLevel: 'low',
849
+ expectedFormat: 'json'
850
+ }
851
+ };
852
+ }
853
+ catch (error) {
854
+ throw new McpAdrError(`Failed to generate learning progress prompt: ${error instanceof Error ? error.message : String(error)}`, 'LEARNING_PROGRESS_ERROR');
855
+ }
856
+ }
857
+ // ============================================================================
858
+ // Utility Functions and Helpers
859
+ // ============================================================================
860
+ /**
861
+ * Generate cache key for reflexion requests
862
+ */
863
+ export function generateReflexionCacheKey(promptHash, config) {
864
+ const configHash = Buffer.from(JSON.stringify(config)).toString('base64').substring(0, 16);
865
+ return `reflexion:execution:${promptHash}-${configHash}`;
866
+ }
867
+ /**
868
+ * Get default reflexion configuration
869
+ */
870
+ export function getDefaultReflexionConfig() {
871
+ return { ...DEFAULT_REFLEXION_CONFIG };
872
+ }
873
+ /**
874
+ * Validate reflexion configuration
875
+ */
876
+ export function validateReflexionConfig(config) {
877
+ if (config.maxMemoryEntries !== undefined && (config.maxMemoryEntries < 1 || config.maxMemoryEntries > 1000)) {
878
+ throw new McpAdrError('Max memory entries must be between 1 and 1000', 'INVALID_CONFIG');
879
+ }
880
+ if (config.learningRate && (config.learningRate < 0 || config.learningRate > 1)) {
881
+ throw new McpAdrError('Learning rate must be between 0 and 1', 'INVALID_CONFIG');
882
+ }
883
+ if (config.memoryRetention && (config.memoryRetention < 1 || config.memoryRetention > 365)) {
884
+ throw new McpAdrError('Memory retention must be between 1 and 365 days', 'INVALID_CONFIG');
885
+ }
886
+ if (config.relevanceThreshold && (config.relevanceThreshold < 0 || config.relevanceThreshold > 1)) {
887
+ throw new McpAdrError('Relevance threshold must be between 0 and 1', 'INVALID_CONFIG');
888
+ }
889
+ if (config.confidenceThreshold && (config.confidenceThreshold < 0 || config.confidenceThreshold > 1)) {
890
+ throw new McpAdrError('Confidence threshold must be between 0 and 1', 'INVALID_CONFIG');
891
+ }
892
+ }
893
+ /**
894
+ * Get supported memory types
895
+ */
896
+ export function getSupportedMemoryTypes() {
897
+ return ['episodic', 'semantic', 'procedural', 'meta', 'feedback'];
898
+ }
899
+ /**
900
+ * Get supported evaluation criteria
901
+ */
902
+ export function getSupportedEvaluationCriteria() {
903
+ return [
904
+ 'task-success',
905
+ 'quality',
906
+ 'efficiency',
907
+ 'accuracy',
908
+ 'completeness',
909
+ 'relevance',
910
+ 'clarity',
911
+ 'innovation'
912
+ ];
913
+ }
914
+ /**
915
+ * Get supported reflection depths
916
+ */
917
+ export function getSupportedReflectionDepths() {
918
+ return ['basic', 'detailed', 'comprehensive'];
919
+ }
920
+ /**
921
+ * Create tool-specific reflexion configuration
922
+ */
923
+ export function createToolReflexionConfig(toolName, customConfig = {}) {
924
+ const toolConfigs = {
925
+ 'generate_adrs_from_prd': {
926
+ maxMemoryEntries: 100,
927
+ reflectionDepth: 'comprehensive',
928
+ evaluationCriteria: ['task-success', 'relevance', 'clarity', 'completeness'],
929
+ learningRate: 0.8,
930
+ memoryRetention: 90,
931
+ relevanceThreshold: 0.7,
932
+ confidenceThreshold: 0.75
933
+ },
934
+ 'suggest_adrs': {
935
+ maxMemoryEntries: 75,
936
+ reflectionDepth: 'detailed',
937
+ evaluationCriteria: ['task-success', 'relevance', 'efficiency'],
938
+ learningRate: 0.7,
939
+ memoryRetention: 60,
940
+ relevanceThreshold: 0.6,
941
+ confidenceThreshold: 0.7
942
+ },
943
+ 'analyze_project_ecosystem': {
944
+ maxMemoryEntries: 60,
945
+ reflectionDepth: 'detailed',
946
+ evaluationCriteria: ['accuracy', 'completeness', 'efficiency'],
947
+ learningRate: 0.7,
948
+ memoryRetention: 60,
949
+ relevanceThreshold: 0.6,
950
+ confidenceThreshold: 0.65
951
+ },
952
+ 'generate_research_questions': {
953
+ maxMemoryEntries: 50,
954
+ reflectionDepth: 'detailed',
955
+ evaluationCriteria: ['quality', 'relevance', 'innovation'],
956
+ learningRate: 0.75,
957
+ memoryRetention: 120,
958
+ relevanceThreshold: 0.65,
959
+ confidenceThreshold: 0.7
960
+ }
961
+ };
962
+ const toolSpecificConfig = toolConfigs[toolName] || {};
963
+ return { ...DEFAULT_REFLEXION_CONFIG, ...toolSpecificConfig, ...customConfig };
964
+ }
965
+ /**
966
+ * Generate memory ID for new memories
967
+ */
968
+ export function generateMemoryId(memoryType, category) {
969
+ const timestamp = Date.now();
970
+ const randomSuffix = Math.random().toString(36).substring(2, 8);
971
+ return `${memoryType}_${category}_${timestamp}_${randomSuffix}`;
972
+ }
973
+ /**
974
+ * Calculate memory expiration date
975
+ */
976
+ export function calculateMemoryExpiration(retentionDays, importance = 'medium') {
977
+ const multipliers = {
978
+ 'low': 0.5,
979
+ 'medium': 1.0,
980
+ 'high': 2.0,
981
+ 'critical': 5.0
982
+ };
983
+ const adjustedDays = retentionDays * multipliers[importance];
984
+ const expirationDate = new Date();
985
+ expirationDate.setDate(expirationDate.getDate() + adjustedDays);
986
+ return expirationDate.toISOString();
987
+ }
988
+ /**
989
+ * Create memory query from context
990
+ */
991
+ export function createMemoryQuery(taskType, context, options = {}) {
992
+ // Extract keywords from context
993
+ const keywords = extractKeywordsFromContext(context);
994
+ return {
995
+ taskType,
996
+ context,
997
+ keywords,
998
+ memoryTypes: options.memoryTypes || ['episodic', 'semantic', 'procedural'],
999
+ relevanceThreshold: options.relevanceThreshold || 0.6,
1000
+ maxResults: options.maxResults || 10,
1001
+ includeExpired: options.includeExpired || false,
1002
+ ...options
1003
+ };
1004
+ }
1005
+ /**
1006
+ * Extract keywords from context for memory search
1007
+ */
1008
+ function extractKeywordsFromContext(context) {
1009
+ const keywords = [];
1010
+ // Extract from common context fields
1011
+ if (context.technologies) {
1012
+ keywords.push(...context.technologies);
1013
+ }
1014
+ if (context.patterns) {
1015
+ keywords.push(...context.patterns);
1016
+ }
1017
+ if (context.domain) {
1018
+ keywords.push(context.domain);
1019
+ }
1020
+ if (context.projectType) {
1021
+ keywords.push(context.projectType);
1022
+ }
1023
+ // Extract from string fields
1024
+ const stringFields = ['description', 'summary', 'title', 'category'];
1025
+ for (const field of stringFields) {
1026
+ if (context[field] && typeof context[field] === 'string') {
1027
+ const words = context[field].toLowerCase().split(/\s+/);
1028
+ keywords.push(...words.filter(word => word.length > 3));
1029
+ }
1030
+ }
1031
+ // Remove duplicates and return
1032
+ return Array.from(new Set(keywords));
1033
+ }
1034
+ /**
1035
+ * Enhance prompt with memory context
1036
+ */
1037
+ export async function enhancePromptWithMemories(basePrompt, memories) {
1038
+ try {
1039
+ const memoryContext = memories.map(memory => ({
1040
+ id: memory.memoryId,
1041
+ type: memory.memoryType,
1042
+ summary: memory.content.summary,
1043
+ lessons: memory.content.lessons,
1044
+ strategies: memory.content.strategies,
1045
+ applicableScenarios: memory.content.applicableScenarios,
1046
+ relevance: memory.relevanceScore,
1047
+ quality: memory.metadata.quality
1048
+ }));
1049
+ const enhancedPrompt = `
1050
+ # Memory-Enhanced Task Execution
1051
+
1052
+ ## Original Task
1053
+ ${basePrompt.prompt}
1054
+
1055
+ ## Relevant Past Experiences
1056
+ ${memoryContext.map((mem, index) => `
1057
+ ### Memory ${index + 1}: ${mem.type} (Relevance: ${mem.relevance})
1058
+ **Summary**: ${mem.summary}
1059
+ **Key Lessons**: ${mem.lessons.join('; ')}
1060
+ **Successful Strategies**: ${mem.strategies.join('; ')}
1061
+ **Applicable to**: ${mem.applicableScenarios.join(', ')}
1062
+ `).join('\n')}
1063
+
1064
+ ## Memory-Informed Approach
1065
+ Based on past experiences, please:
1066
+ 1. **Apply Relevant Lessons**: Use the lessons learned from similar situations
1067
+ 2. **Leverage Successful Strategies**: Build on approaches that have worked well
1068
+ 3. **Avoid Known Pitfalls**: Be aware of common mistakes and failure patterns
1069
+ 4. **Adapt to Current Context**: Modify strategies based on current context differences
1070
+
1071
+ ## Enhanced Instructions
1072
+ ${basePrompt.instructions}
1073
+
1074
+ ## Success Criteria
1075
+ - Apply at least 2 relevant lessons from past experiences
1076
+ - Demonstrate learning from previous mistakes
1077
+ - Show improvement over baseline approaches
1078
+ - Generate new insights for future learning
1079
+
1080
+ Execute the task with memory-informed decision making and document how past experiences influenced your approach.
1081
+ `;
1082
+ return {
1083
+ prompt: enhancedPrompt,
1084
+ instructions: basePrompt.instructions,
1085
+ context: {
1086
+ ...basePrompt.context,
1087
+ operation: 'reflexion_execution', // Add operation for validation
1088
+ memoriesUsed: memories.map(m => m.memoryId),
1089
+ memoryEnhanced: true,
1090
+ memoryCount: memories.length,
1091
+ version: REFLEXION_VERSION,
1092
+ timestamp: Date.now()
1093
+ }
1094
+ };
1095
+ }
1096
+ catch (error) {
1097
+ throw new McpAdrError(`Failed to enhance prompt with memories: ${error instanceof Error ? error.message : String(error)}`, 'MEMORY_ENHANCEMENT_ERROR');
1098
+ }
1099
+ }
1100
+ //# sourceMappingURL=reflexion.js.map