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.
- package/README.md +367 -150
- package/dist/src/config/ai-config.d.ts +81 -0
- package/dist/src/config/ai-config.d.ts.map +1 -0
- package/dist/src/config/ai-config.js +134 -0
- package/dist/src/config/ai-config.js.map +1 -0
- package/dist/src/index.d.ts +97 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2430 -186
- package/dist/src/index.js.map +1 -1
- package/dist/src/prompts/adr-suggestion-prompts.d.ts +2 -2
- package/dist/src/prompts/adr-suggestion-prompts.d.ts.map +1 -1
- package/dist/src/prompts/adr-suggestion-prompts.js +25 -17
- package/dist/src/prompts/adr-suggestion-prompts.js.map +1 -1
- package/dist/src/prompts/analysis-prompts.js +1 -1
- package/dist/src/prompts/analysis-prompts.js.map +1 -1
- package/dist/src/resources/index.d.ts +1 -1
- package/dist/src/resources/index.d.ts.map +1 -1
- package/dist/src/resources/index.js +52 -44
- package/dist/src/resources/index.js.map +1 -1
- package/dist/src/templates/domain-knowledge-templates.d.ts +36 -0
- package/dist/src/templates/domain-knowledge-templates.d.ts.map +1 -0
- package/dist/src/templates/domain-knowledge-templates.js +376 -0
- package/dist/src/templates/domain-knowledge-templates.js.map +1 -0
- package/dist/src/tools/adr-suggestion-tool.d.ts +8 -3
- package/dist/src/tools/adr-suggestion-tool.d.ts.map +1 -1
- package/dist/src/tools/adr-suggestion-tool.js +561 -172
- package/dist/src/tools/adr-suggestion-tool.js.map +1 -1
- package/dist/src/tools/content-masking-tool.d.ts.map +1 -1
- package/dist/src/tools/content-masking-tool.js +168 -22
- package/dist/src/tools/content-masking-tool.js.map +1 -1
- package/dist/src/tools/deployment-analysis-tool.d.ts.map +1 -1
- package/dist/src/tools/deployment-analysis-tool.js +64 -27
- package/dist/src/tools/deployment-analysis-tool.js.map +1 -1
- package/dist/src/tools/environment-analysis-tool.d.ts.map +1 -1
- package/dist/src/tools/environment-analysis-tool.js +65 -29
- package/dist/src/tools/environment-analysis-tool.js.map +1 -1
- package/dist/src/tools/research-question-tool.d.ts.map +1 -1
- package/dist/src/tools/research-question-tool.js +150 -49
- package/dist/src/tools/research-question-tool.js.map +1 -1
- package/dist/src/tools/rule-generation-tool.d.ts.map +1 -1
- package/dist/src/tools/rule-generation-tool.js +328 -143
- package/dist/src/tools/rule-generation-tool.js.map +1 -1
- package/dist/src/types/ape-framework.d.ts +566 -0
- package/dist/src/types/ape-framework.d.ts.map +1 -0
- package/dist/src/types/ape-framework.js +98 -0
- package/dist/src/types/ape-framework.js.map +1 -0
- package/dist/src/types/index.d.ts +96 -93
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/knowledge-generation.d.ts +671 -0
- package/dist/src/types/knowledge-generation.d.ts.map +1 -0
- package/dist/src/types/knowledge-generation.js +107 -0
- package/dist/src/types/knowledge-generation.js.map +1 -0
- package/dist/src/types/reflexion-framework.d.ts +652 -0
- package/dist/src/types/reflexion-framework.d.ts.map +1 -0
- package/dist/src/types/reflexion-framework.js +85 -0
- package/dist/src/types/reflexion-framework.js.map +1 -0
- package/dist/src/utils/adr-suggestions.d.ts +1 -1
- package/dist/src/utils/adr-suggestions.d.ts.map +1 -1
- package/dist/src/utils/adr-suggestions.js +8 -50
- package/dist/src/utils/adr-suggestions.js.map +1 -1
- package/dist/src/utils/ai-executor.d.ts +125 -0
- package/dist/src/utils/ai-executor.d.ts.map +1 -0
- package/dist/src/utils/ai-executor.js +292 -0
- package/dist/src/utils/ai-executor.js.map +1 -0
- package/dist/src/utils/automatic-prompt-engineering.d.ts +72 -0
- package/dist/src/utils/automatic-prompt-engineering.d.ts.map +1 -0
- package/dist/src/utils/automatic-prompt-engineering.js +820 -0
- package/dist/src/utils/automatic-prompt-engineering.js.map +1 -0
- package/dist/src/utils/cache.d.ts +54 -22
- package/dist/src/utils/cache.d.ts.map +1 -1
- package/dist/src/utils/cache.js +1234 -143
- package/dist/src/utils/cache.js.map +1 -1
- package/dist/src/utils/config.d.ts +2 -2
- package/dist/src/utils/config.js +3 -3
- package/dist/src/utils/config.js.map +1 -1
- package/dist/src/utils/deployment-analysis.d.ts.map +1 -1
- package/dist/src/utils/deployment-analysis.js +57 -38
- package/dist/src/utils/deployment-analysis.js.map +1 -1
- package/dist/src/utils/environment-analysis.d.ts.map +1 -1
- package/dist/src/utils/environment-analysis.js +113 -100
- package/dist/src/utils/environment-analysis.js.map +1 -1
- package/dist/src/utils/file-system.d.ts +42 -13
- package/dist/src/utils/file-system.d.ts.map +1 -1
- package/dist/src/utils/file-system.js +795 -89
- package/dist/src/utils/file-system.js.map +1 -1
- package/dist/src/utils/index.d.ts +9 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +13 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/knowledge-generation.d.ts +79 -0
- package/dist/src/utils/knowledge-generation.d.ts.map +1 -0
- package/dist/src/utils/knowledge-generation.js +646 -0
- package/dist/src/utils/knowledge-generation.js.map +1 -0
- package/dist/src/utils/output-masking.d.ts.map +1 -1
- package/dist/src/utils/output-masking.js +2 -1
- package/dist/src/utils/output-masking.js.map +1 -1
- package/dist/src/utils/prompt-composition.d.ts +234 -0
- package/dist/src/utils/prompt-composition.d.ts.map +1 -0
- package/dist/src/utils/prompt-composition.js +379 -0
- package/dist/src/utils/prompt-composition.js.map +1 -0
- package/dist/src/utils/prompt-execution.d.ts +88 -0
- package/dist/src/utils/prompt-execution.d.ts.map +1 -0
- package/dist/src/utils/prompt-execution.js +227 -0
- package/dist/src/utils/prompt-execution.js.map +1 -0
- package/dist/src/utils/reflexion.d.ts +92 -0
- package/dist/src/utils/reflexion.d.ts.map +1 -0
- package/dist/src/utils/reflexion.js +1100 -0
- package/dist/src/utils/reflexion.js.map +1 -0
- package/dist/src/utils/research-integration.d.ts.map +1 -1
- package/dist/src/utils/research-integration.js +156 -100
- package/dist/src/utils/research-integration.js.map +1 -1
- package/dist/src/utils/research-questions.d.ts.map +1 -1
- package/dist/src/utils/research-questions.js +85 -24
- package/dist/src/utils/research-questions.js.map +1 -1
- package/dist/src/utils/rule-generation.d.ts.map +1 -1
- package/dist/src/utils/rule-generation.js +92 -108
- package/dist/src/utils/rule-generation.js.map +1 -1
- package/package.json +9 -3
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript interfaces for Reflexion Framework
|
|
3
|
+
* Implements Actor-Evaluator-Self-Reflection pattern for continuous learning
|
|
4
|
+
*/
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
export const ReflexionConfigSchema = z.object({
|
|
7
|
+
memoryEnabled: z.boolean(),
|
|
8
|
+
maxMemoryEntries: z.number().min(1).max(1000),
|
|
9
|
+
reflectionDepth: z.enum(['basic', 'detailed', 'comprehensive']),
|
|
10
|
+
evaluationCriteria: z.array(z.enum([
|
|
11
|
+
'task-success',
|
|
12
|
+
'quality',
|
|
13
|
+
'efficiency',
|
|
14
|
+
'accuracy',
|
|
15
|
+
'completeness',
|
|
16
|
+
'relevance',
|
|
17
|
+
'clarity',
|
|
18
|
+
'innovation'
|
|
19
|
+
])),
|
|
20
|
+
learningRate: z.number().min(0).max(1),
|
|
21
|
+
memoryRetention: z.number().min(1).max(365),
|
|
22
|
+
feedbackIntegration: z.boolean(),
|
|
23
|
+
autoCleanup: z.boolean(),
|
|
24
|
+
relevanceThreshold: z.number().min(0).max(1),
|
|
25
|
+
confidenceThreshold: z.number().min(0).max(1),
|
|
26
|
+
});
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// Export Schemas for Runtime Validation
|
|
29
|
+
// ============================================================================
|
|
30
|
+
export const TaskAttemptSchema = z.object({
|
|
31
|
+
attemptId: z.string(),
|
|
32
|
+
taskType: z.string(),
|
|
33
|
+
context: z.any(),
|
|
34
|
+
action: z.string(),
|
|
35
|
+
outcome: z.object({
|
|
36
|
+
success: z.boolean(),
|
|
37
|
+
result: z.any(),
|
|
38
|
+
errors: z.array(z.string()),
|
|
39
|
+
warnings: z.array(z.string()),
|
|
40
|
+
executionTime: z.number(),
|
|
41
|
+
resourcesUsed: z.object({
|
|
42
|
+
memoryAccessed: z.number(),
|
|
43
|
+
memoryCreated: z.number(),
|
|
44
|
+
processingTime: z.number(),
|
|
45
|
+
promptTokens: z.number(),
|
|
46
|
+
cacheHits: z.number(),
|
|
47
|
+
cacheMisses: z.number()
|
|
48
|
+
}),
|
|
49
|
+
qualityMetrics: z.object({
|
|
50
|
+
accuracy: z.number().min(0).max(1),
|
|
51
|
+
completeness: z.number().min(0).max(1),
|
|
52
|
+
relevance: z.number().min(0).max(1),
|
|
53
|
+
clarity: z.number().min(0).max(1),
|
|
54
|
+
innovation: z.number().min(0).max(1),
|
|
55
|
+
efficiency: z.number().min(0).max(1)
|
|
56
|
+
})
|
|
57
|
+
}),
|
|
58
|
+
evaluation: z.object({
|
|
59
|
+
overallScore: z.number().min(0).max(1),
|
|
60
|
+
criteriaScores: z.record(z.number()),
|
|
61
|
+
feedback: z.array(z.any()),
|
|
62
|
+
strengths: z.array(z.string()),
|
|
63
|
+
weaknesses: z.array(z.string()),
|
|
64
|
+
improvementAreas: z.array(z.string()),
|
|
65
|
+
confidence: z.number().min(0).max(1),
|
|
66
|
+
evaluationTime: z.number(),
|
|
67
|
+
evaluatorVersion: z.string()
|
|
68
|
+
}),
|
|
69
|
+
reflection: z.object({
|
|
70
|
+
reflectionId: z.string(),
|
|
71
|
+
reflectionText: z.string(),
|
|
72
|
+
lessonsLearned: z.array(z.any()),
|
|
73
|
+
actionableInsights: z.array(z.any()),
|
|
74
|
+
futureStrategies: z.array(z.any()),
|
|
75
|
+
knowledgeGaps: z.array(z.any()),
|
|
76
|
+
confidenceLevel: z.number().min(0).max(1),
|
|
77
|
+
applicability: z.array(z.string()),
|
|
78
|
+
reflectionTime: z.number()
|
|
79
|
+
}),
|
|
80
|
+
timestamp: z.string(),
|
|
81
|
+
metadata: z.any(),
|
|
82
|
+
relatedMemories: z.array(z.string()),
|
|
83
|
+
generatedMemories: z.array(z.string())
|
|
84
|
+
});
|
|
85
|
+
//# sourceMappingURL=reflexion-framework.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reflexion-framework.js","sourceRoot":"","sources":["../../../src/types/reflexion-framework.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA2DxB,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5C,aAAa,EAAE,CAAC,CAAC,OAAO,EAAE;IAC1B,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;IAC7C,eAAe,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,eAAe,CAAC,CAAC;IAC/D,kBAAkB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;QACjC,cAAc;QACd,SAAS;QACT,YAAY;QACZ,UAAU;QACV,cAAc;QACd,WAAW;QACX,SAAS;QACT,YAAY;KACb,CAAC,CAAC;IACH,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACtC,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;IAC3C,mBAAmB,EAAE,CAAC,CAAC,OAAO,EAAE;IAChC,WAAW,EAAE,CAAC,CAAC,OAAO,EAAE;IACxB,kBAAkB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5C,mBAAmB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC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C,MAAM,EAAE;QAC1B,cAAc,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QAChC,kBAAkB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QACpC,gBAAgB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QAClC,aAAa,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QAC/B,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACzC,aAAa,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAClC,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;KAC3B,CAAC;IACF,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;IACrB,QAAQ,EAAE,CAAC,CAAC,GAAG,EAAE;IACjB,eAAe,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACpC,iBAAiB,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;CACvC,CAAC,CAAC"}
|
|
@@ -66,7 +66,7 @@ export interface GeneratedAdr {
|
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
69
|
+
* Generate prompt for AI to analyze project for implicit architectural decisions
|
|
70
70
|
*/
|
|
71
71
|
export declare function analyzeImplicitDecisions(projectPath: string, existingAdrs?: string[]): Promise<{
|
|
72
72
|
analysisPrompt: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adr-suggestions.d.ts","sourceRoot":"","sources":["../../../src/utils/adr-suggestions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,K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EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC,CA+C7D;AAGD;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,MAAM,CAmBpE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAgB5E"}
|
|
@@ -4,19 +4,12 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { McpAdrError } from '../types/index.js';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* Generate prompt for AI to analyze project for implicit architectural decisions
|
|
8
8
|
*/
|
|
9
9
|
export async function analyzeImplicitDecisions(projectPath, existingAdrs) {
|
|
10
10
|
try {
|
|
11
|
-
const { analyzeProjectStructure } = await import('./file-system.js');
|
|
12
|
-
const { generateAnalysisContext } = await import('../prompts/analysis-prompts.js');
|
|
13
11
|
const { generateImplicitDecisionDetectionPrompt } = await import('../prompts/adr-suggestion-prompts.js');
|
|
14
|
-
|
|
15
|
-
const projectStructure = await analyzeProjectStructure(projectPath);
|
|
16
|
-
const analysisContext = generateAnalysisContext(projectStructure);
|
|
17
|
-
// Extract code patterns from project structure
|
|
18
|
-
const codePatterns = extractCodePatterns(projectStructure);
|
|
19
|
-
const analysisPrompt = generateImplicitDecisionDetectionPrompt(analysisContext, codePatterns, existingAdrs);
|
|
12
|
+
const analysisPrompt = generateImplicitDecisionDetectionPrompt(projectPath, existingAdrs);
|
|
20
13
|
const instructions = `
|
|
21
14
|
# Implicit Decision Analysis Instructions
|
|
22
15
|
|
|
@@ -24,9 +17,7 @@ This analysis will identify architectural decisions that are implicit in the cod
|
|
|
24
17
|
|
|
25
18
|
## Analysis Scope
|
|
26
19
|
- **Project Path**: ${projectPath}
|
|
27
|
-
- **
|
|
28
|
-
- **Directories**: ${projectStructure.totalDirectories} directories
|
|
29
|
-
- **Existing ADRs**: ${existingAdrs?.length || 0} ADRs
|
|
20
|
+
- **Existing ADRs**: ${existingAdrs?.length || 0} ADRs provided
|
|
30
21
|
|
|
31
22
|
## Next Steps
|
|
32
23
|
1. **Submit the analysis prompt** to an AI agent for decision detection
|
|
@@ -54,7 +45,7 @@ const result = await analyzeImplicitDecisions(projectPath, existingAdrs);
|
|
|
54
45
|
};
|
|
55
46
|
}
|
|
56
47
|
catch (error) {
|
|
57
|
-
throw new McpAdrError(`Failed to
|
|
48
|
+
throw new McpAdrError(`Failed to generate analysis prompt: ${error instanceof Error ? error.message : String(error)}`, 'ANALYSIS_ERROR');
|
|
58
49
|
}
|
|
59
50
|
}
|
|
60
51
|
/**
|
|
@@ -150,41 +141,6 @@ const result = await generateAdrFromDecision(decisionData, 'nygard', existingAdr
|
|
|
150
141
|
throw new McpAdrError(`Failed to generate ADR: ${error instanceof Error ? error.message : String(error)}`, 'GENERATION_ERROR');
|
|
151
142
|
}
|
|
152
143
|
}
|
|
153
|
-
/**
|
|
154
|
-
* Extract code patterns from project structure for analysis
|
|
155
|
-
*/
|
|
156
|
-
function extractCodePatterns(projectStructure) {
|
|
157
|
-
const patterns = [];
|
|
158
|
-
try {
|
|
159
|
-
// Extract patterns from file structure
|
|
160
|
-
if (projectStructure.filesByType) {
|
|
161
|
-
Object.entries(projectStructure.filesByType).forEach(([type, files]) => {
|
|
162
|
-
if (Array.isArray(files) && files.length > 0) {
|
|
163
|
-
patterns.push(`${type} files: ${files
|
|
164
|
-
.slice(0, 5)
|
|
165
|
-
.map((f) => f.name || f)
|
|
166
|
-
.join(', ')}${files.length > 5 ? ` and ${files.length - 5} more` : ''}`);
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
// Extract patterns from directory structure
|
|
171
|
-
if (projectStructure.directories) {
|
|
172
|
-
const dirNames = projectStructure.directories.slice(0, 10).join(', ');
|
|
173
|
-
patterns.push(`Directory structure: ${dirNames}${projectStructure.directories.length > 10 ? ` and ${projectStructure.directories.length - 10} more` : ''}`);
|
|
174
|
-
}
|
|
175
|
-
// Extract patterns from detected technologies
|
|
176
|
-
if (projectStructure.detectedTechnologies) {
|
|
177
|
-
patterns.push(`Technologies: ${projectStructure.detectedTechnologies.join(', ')}`);
|
|
178
|
-
}
|
|
179
|
-
// Add basic project metrics
|
|
180
|
-
patterns.push(`Project metrics: ${projectStructure.totalFiles} files, ${projectStructure.totalDirectories} directories`);
|
|
181
|
-
return patterns;
|
|
182
|
-
}
|
|
183
|
-
catch (error) {
|
|
184
|
-
console.warn('Failed to extract code patterns:', error);
|
|
185
|
-
return ['Unable to extract code patterns from project structure'];
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
144
|
/**
|
|
189
145
|
* Generate next ADR number based on existing ADRs
|
|
190
146
|
*/
|
|
@@ -202,7 +158,8 @@ export function generateNextAdrNumber(existingAdrs) {
|
|
|
202
158
|
return `ADR-${nextNumber.toString().padStart(4, '0')}`;
|
|
203
159
|
}
|
|
204
160
|
catch (error) {
|
|
205
|
-
|
|
161
|
+
// Log to stderr to avoid corrupting MCP protocol
|
|
162
|
+
console.error('[WARN] Failed to generate ADR number:', error);
|
|
206
163
|
return 'ADR-0001';
|
|
207
164
|
}
|
|
208
165
|
}
|
|
@@ -221,7 +178,8 @@ export function suggestAdrFilename(title, adrNumber) {
|
|
|
221
178
|
return `${number.toLowerCase()}-${cleanTitle}.md`;
|
|
222
179
|
}
|
|
223
180
|
catch (error) {
|
|
224
|
-
|
|
181
|
+
// Log to stderr to avoid corrupting MCP protocol
|
|
182
|
+
console.error('[WARN] Failed to suggest filename:', error);
|
|
225
183
|
return 'adr-new-decision.md';
|
|
226
184
|
}
|
|
227
185
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adr-suggestions.js","sourceRoot":"","sources":["../../../src/utils/adr-suggestions.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAsEhD;;GAEG;AACH,MAAM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,EAAE,CAAC,CAAC;QAEzB,OAAO,GAAG,MAAM,CAAC,WAAW,EAAE,IAAI,UAAU,KAAK,CAAC;IACpD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,iDAAiD;QACjD,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;QAC3D,OAAO,qBAAqB,CAAC;IAC/B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Executor Service for OpenRouter.ai Integration
|
|
3
|
+
*
|
|
4
|
+
* This service handles the execution of prompts using OpenRouter.ai,
|
|
5
|
+
* transforming the MCP server from returning prompts to returning actual results.
|
|
6
|
+
*/
|
|
7
|
+
import { AIConfig } from '../config/ai-config.js';
|
|
8
|
+
export interface AIExecutionResult {
|
|
9
|
+
/** The AI-generated response content */
|
|
10
|
+
content: string;
|
|
11
|
+
/** Model used for generation */
|
|
12
|
+
model: string;
|
|
13
|
+
/** Token usage information */
|
|
14
|
+
usage?: {
|
|
15
|
+
promptTokens: number;
|
|
16
|
+
completionTokens: number;
|
|
17
|
+
totalTokens: number;
|
|
18
|
+
};
|
|
19
|
+
/** Execution metadata */
|
|
20
|
+
metadata: {
|
|
21
|
+
executionTime: number;
|
|
22
|
+
cached: boolean;
|
|
23
|
+
retryCount: number;
|
|
24
|
+
timestamp: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export interface AIExecutionError extends Error {
|
|
28
|
+
code: string;
|
|
29
|
+
retryable: boolean;
|
|
30
|
+
originalError?: unknown;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* AI Executor Service Class
|
|
34
|
+
*/
|
|
35
|
+
export declare class AIExecutor {
|
|
36
|
+
private client;
|
|
37
|
+
private config;
|
|
38
|
+
private cache;
|
|
39
|
+
constructor(config?: AIConfig);
|
|
40
|
+
/**
|
|
41
|
+
* Initialize OpenAI client for OpenRouter
|
|
42
|
+
*/
|
|
43
|
+
private initializeClient;
|
|
44
|
+
/**
|
|
45
|
+
* Check if AI execution is available
|
|
46
|
+
*/
|
|
47
|
+
isAvailable(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Reload configuration if environment variables have changed
|
|
50
|
+
*/
|
|
51
|
+
private reloadConfigIfNeeded;
|
|
52
|
+
/**
|
|
53
|
+
* Execute a prompt and return the AI response
|
|
54
|
+
*/
|
|
55
|
+
executePrompt(prompt: string, options?: {
|
|
56
|
+
model?: string;
|
|
57
|
+
temperature?: number;
|
|
58
|
+
maxTokens?: number;
|
|
59
|
+
systemPrompt?: string;
|
|
60
|
+
}): Promise<AIExecutionResult>;
|
|
61
|
+
/**
|
|
62
|
+
* Execute a structured prompt that expects JSON response
|
|
63
|
+
*/
|
|
64
|
+
executeStructuredPrompt<T = any>(prompt: string, schema?: any, options?: {
|
|
65
|
+
model?: string;
|
|
66
|
+
temperature?: number;
|
|
67
|
+
maxTokens?: number;
|
|
68
|
+
systemPrompt?: string;
|
|
69
|
+
}): Promise<{
|
|
70
|
+
data: T;
|
|
71
|
+
raw: AIExecutionResult;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Extract JSON content from AI response, handling markdown code blocks
|
|
75
|
+
*/
|
|
76
|
+
private extractJsonFromResponse;
|
|
77
|
+
/**
|
|
78
|
+
* Generate cache key for a prompt execution
|
|
79
|
+
*/
|
|
80
|
+
private generateCacheKey;
|
|
81
|
+
/**
|
|
82
|
+
* Get cached result if available and not expired
|
|
83
|
+
*/
|
|
84
|
+
private getCachedResult;
|
|
85
|
+
/**
|
|
86
|
+
* Cache a result
|
|
87
|
+
*/
|
|
88
|
+
private setCachedResult;
|
|
89
|
+
/**
|
|
90
|
+
* Clean up expired cache entries
|
|
91
|
+
*/
|
|
92
|
+
private cleanupCache;
|
|
93
|
+
/**
|
|
94
|
+
* Create a standardized AI execution error
|
|
95
|
+
*/
|
|
96
|
+
private createError;
|
|
97
|
+
/**
|
|
98
|
+
* Get current configuration
|
|
99
|
+
*/
|
|
100
|
+
getConfig(): AIConfig;
|
|
101
|
+
/**
|
|
102
|
+
* Update configuration
|
|
103
|
+
*/
|
|
104
|
+
updateConfig(newConfig: Partial<AIConfig>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Clear cache
|
|
107
|
+
*/
|
|
108
|
+
clearCache(): void;
|
|
109
|
+
/**
|
|
110
|
+
* Get cache statistics
|
|
111
|
+
*/
|
|
112
|
+
getCacheStats(): {
|
|
113
|
+
size: number;
|
|
114
|
+
hitRate: number;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get or create the global AI executor instance
|
|
119
|
+
*/
|
|
120
|
+
export declare function getAIExecutor(): AIExecutor;
|
|
121
|
+
/**
|
|
122
|
+
* Reset the global AI executor (useful for testing)
|
|
123
|
+
*/
|
|
124
|
+
export declare function resetAIExecutor(): void;
|
|
125
|
+
//# sourceMappingURL=ai-executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ai-executor.d.ts","sourceRoot":"","sources":["../../../src/utils/ai-executor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,QAAQ,EAAwD,MAAM,wBAAwB,CAAC;AAExG,MAAM,WAAW,iBAAiB;IAChC,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,8BAA8B;IAC9B,KAAK,CAAC,EAAE;QACN,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;QACzB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,yBAAyB;IACzB,QAAQ,EAAE;QACR,aAAa,EAAE,MAAM,CAAC;QACtB,MAAM,EAAE,OAAO,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAED,MAAM,WAAW,gBAAiB,SA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;OAEG;IACI,SAAS,IAAI,QAAQ;IAI5B;;OAEG;IACI,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,IAAI;IAKvD;;OAEG;IACI,UAAU,IAAI,IAAI;IAIzB;;OAEG;IACI,aAAa,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;CAO1D;AAOD;;GAEG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAK1C;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,IAAI,CAEtC"}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AI Executor Service for OpenRouter.ai Integration
|
|
3
|
+
*
|
|
4
|
+
* This service handles the execution of prompts using OpenRouter.ai,
|
|
5
|
+
* transforming the MCP server from returning prompts to returning actual results.
|
|
6
|
+
*/
|
|
7
|
+
import OpenAI from 'openai';
|
|
8
|
+
import { loadAIConfig, validateAIConfig, isAIExecutionEnabled } from '../config/ai-config.js';
|
|
9
|
+
/**
|
|
10
|
+
* AI Executor Service Class
|
|
11
|
+
*/
|
|
12
|
+
export class AIExecutor {
|
|
13
|
+
client = null;
|
|
14
|
+
config;
|
|
15
|
+
cache = new Map();
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.config = config || loadAIConfig();
|
|
18
|
+
this.initializeClient();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Initialize OpenAI client for OpenRouter
|
|
22
|
+
*/
|
|
23
|
+
initializeClient() {
|
|
24
|
+
if (!isAIExecutionEnabled(this.config)) {
|
|
25
|
+
console.log('AI execution disabled - running in prompt-only mode');
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
validateAIConfig(this.config);
|
|
30
|
+
this.client = new OpenAI({
|
|
31
|
+
baseURL: this.config.baseURL,
|
|
32
|
+
apiKey: this.config.apiKey,
|
|
33
|
+
timeout: this.config.timeout,
|
|
34
|
+
maxRetries: this.config.maxRetries,
|
|
35
|
+
defaultHeaders: {
|
|
36
|
+
'HTTP-Referer': this.config.siteUrl || '',
|
|
37
|
+
'X-Title': this.config.siteName || '',
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
console.log(`AI Executor initialized with model: ${this.config.defaultModel}`);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error('Failed to initialize AI Executor:', error);
|
|
44
|
+
this.client = null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if AI execution is available
|
|
49
|
+
*/
|
|
50
|
+
isAvailable() {
|
|
51
|
+
// Reload configuration to pick up environment variable changes
|
|
52
|
+
this.reloadConfigIfNeeded();
|
|
53
|
+
return this.client !== null && isAIExecutionEnabled(this.config);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Reload configuration if environment variables have changed
|
|
57
|
+
*/
|
|
58
|
+
reloadConfigIfNeeded() {
|
|
59
|
+
const currentConfig = loadAIConfig();
|
|
60
|
+
// Check if key configuration has changed
|
|
61
|
+
const configChanged = this.config.apiKey !== currentConfig.apiKey ||
|
|
62
|
+
this.config.executionMode !== currentConfig.executionMode ||
|
|
63
|
+
this.config.defaultModel !== currentConfig.defaultModel;
|
|
64
|
+
if (configChanged) {
|
|
65
|
+
console.log('AI configuration changed, reinitializing...');
|
|
66
|
+
this.config = currentConfig;
|
|
67
|
+
this.initializeClient();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Execute a prompt and return the AI response
|
|
72
|
+
*/
|
|
73
|
+
async executePrompt(prompt, options = {}) {
|
|
74
|
+
// Ensure configuration is up to date before execution
|
|
75
|
+
this.reloadConfigIfNeeded();
|
|
76
|
+
if (!this.isAvailable()) {
|
|
77
|
+
throw this.createError('AI execution not available - check configuration', 'AI_UNAVAILABLE', false);
|
|
78
|
+
}
|
|
79
|
+
const startTime = Date.now();
|
|
80
|
+
const model = options.model || this.config.defaultModel;
|
|
81
|
+
const cacheKey = this.generateCacheKey(prompt, model, options);
|
|
82
|
+
// Check cache first
|
|
83
|
+
if (this.config.cacheEnabled) {
|
|
84
|
+
const cached = this.getCachedResult(cacheKey);
|
|
85
|
+
if (cached) {
|
|
86
|
+
return cached;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
let retryCount = 0;
|
|
90
|
+
const maxRetries = this.config.maxRetries;
|
|
91
|
+
while (retryCount <= maxRetries) {
|
|
92
|
+
try {
|
|
93
|
+
const messages = [];
|
|
94
|
+
if (options.systemPrompt) {
|
|
95
|
+
messages.push({ role: 'system', content: options.systemPrompt });
|
|
96
|
+
}
|
|
97
|
+
messages.push({ role: 'user', content: prompt });
|
|
98
|
+
const completion = await this.client.chat.completions.create({
|
|
99
|
+
model,
|
|
100
|
+
messages,
|
|
101
|
+
temperature: options.temperature ?? this.config.temperature,
|
|
102
|
+
max_tokens: options.maxTokens ?? this.config.maxTokens,
|
|
103
|
+
});
|
|
104
|
+
const result = {
|
|
105
|
+
content: completion.choices[0]?.message?.content || '',
|
|
106
|
+
model: completion.model,
|
|
107
|
+
metadata: {
|
|
108
|
+
executionTime: Date.now() - startTime,
|
|
109
|
+
cached: false,
|
|
110
|
+
retryCount,
|
|
111
|
+
timestamp: new Date().toISOString(),
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
if (completion.usage) {
|
|
115
|
+
result.usage = {
|
|
116
|
+
promptTokens: completion.usage.prompt_tokens,
|
|
117
|
+
completionTokens: completion.usage.completion_tokens,
|
|
118
|
+
totalTokens: completion.usage.total_tokens,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// Cache the result
|
|
122
|
+
if (this.config.cacheEnabled) {
|
|
123
|
+
this.setCachedResult(cacheKey, result);
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
retryCount++;
|
|
129
|
+
if (retryCount > maxRetries) {
|
|
130
|
+
throw this.createError(`AI execution failed after ${maxRetries} retries: ${error}`, 'AI_EXECUTION_FAILED', false, error);
|
|
131
|
+
}
|
|
132
|
+
// Wait before retry (exponential backoff)
|
|
133
|
+
const delay = Math.min(1000 * Math.pow(2, retryCount - 1), 10000);
|
|
134
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
throw this.createError('Unexpected error in AI execution', 'AI_UNEXPECTED_ERROR', false);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Execute a structured prompt that expects JSON response
|
|
141
|
+
*/
|
|
142
|
+
async executeStructuredPrompt(prompt, schema, options = {}) {
|
|
143
|
+
const systemPrompt = options.systemPrompt ||
|
|
144
|
+
'You are a helpful assistant that responds with valid JSON. Always return properly formatted JSON that matches the requested schema. Do not wrap the JSON in markdown code blocks.';
|
|
145
|
+
const result = await this.executePrompt(prompt, {
|
|
146
|
+
...options,
|
|
147
|
+
systemPrompt,
|
|
148
|
+
temperature: options.temperature ?? 0.1, // Lower temperature for structured output
|
|
149
|
+
});
|
|
150
|
+
try {
|
|
151
|
+
// Extract JSON from response, handling markdown code blocks
|
|
152
|
+
const jsonContent = this.extractJsonFromResponse(result.content);
|
|
153
|
+
const data = JSON.parse(jsonContent);
|
|
154
|
+
// Basic schema validation if provided
|
|
155
|
+
if (schema && typeof schema.parse === 'function') {
|
|
156
|
+
schema.parse(data);
|
|
157
|
+
}
|
|
158
|
+
return { data, raw: result };
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
throw this.createError(`Failed to parse JSON response: ${error}`, 'AI_JSON_PARSE_ERROR', false, error);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Extract JSON content from AI response, handling markdown code blocks
|
|
166
|
+
*/
|
|
167
|
+
extractJsonFromResponse(content) {
|
|
168
|
+
// Remove leading/trailing whitespace
|
|
169
|
+
content = content.trim();
|
|
170
|
+
// Check if content is wrapped in markdown code blocks
|
|
171
|
+
const codeBlockMatch = content.match(/^```(?:json)?\s*\n?([\s\S]*?)\n?```$/);
|
|
172
|
+
if (codeBlockMatch && codeBlockMatch[1]) {
|
|
173
|
+
return codeBlockMatch[1].trim();
|
|
174
|
+
}
|
|
175
|
+
// Check for inline code blocks
|
|
176
|
+
const inlineCodeMatch = content.match(/^`([\s\S]*?)`$/);
|
|
177
|
+
if (inlineCodeMatch && inlineCodeMatch[1]) {
|
|
178
|
+
return inlineCodeMatch[1].trim();
|
|
179
|
+
}
|
|
180
|
+
// Try to find JSON object/array in the content
|
|
181
|
+
const jsonMatch = content.match(/(\{[\s\S]*\}|\[[\s\S]*\])/);
|
|
182
|
+
if (jsonMatch && jsonMatch[1]) {
|
|
183
|
+
return jsonMatch[1].trim();
|
|
184
|
+
}
|
|
185
|
+
// Return as-is if no patterns match
|
|
186
|
+
return content;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Generate cache key for a prompt execution
|
|
190
|
+
*/
|
|
191
|
+
generateCacheKey(prompt, model, options) {
|
|
192
|
+
const key = JSON.stringify({ prompt, model, options });
|
|
193
|
+
return Buffer.from(key).toString('base64').slice(0, 32);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get cached result if available and not expired
|
|
197
|
+
*/
|
|
198
|
+
getCachedResult(cacheKey) {
|
|
199
|
+
const cached = this.cache.get(cacheKey);
|
|
200
|
+
if (!cached)
|
|
201
|
+
return null;
|
|
202
|
+
if (Date.now() > cached.expiry) {
|
|
203
|
+
this.cache.delete(cacheKey);
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
// Mark as cached
|
|
207
|
+
const result = { ...cached.result };
|
|
208
|
+
result.metadata = { ...result.metadata, cached: true };
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Cache a result
|
|
213
|
+
*/
|
|
214
|
+
setCachedResult(cacheKey, result) {
|
|
215
|
+
const expiry = Date.now() + (this.config.cacheTTL * 1000);
|
|
216
|
+
this.cache.set(cacheKey, { result, expiry });
|
|
217
|
+
// Clean up expired entries periodically
|
|
218
|
+
if (this.cache.size > 100) {
|
|
219
|
+
this.cleanupCache();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Clean up expired cache entries
|
|
224
|
+
*/
|
|
225
|
+
cleanupCache() {
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
for (const [key, value] of this.cache.entries()) {
|
|
228
|
+
if (now > value.expiry) {
|
|
229
|
+
this.cache.delete(key);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Create a standardized AI execution error
|
|
235
|
+
*/
|
|
236
|
+
createError(message, code, retryable, originalError) {
|
|
237
|
+
const error = new Error(message);
|
|
238
|
+
error.code = code;
|
|
239
|
+
error.retryable = retryable;
|
|
240
|
+
error.originalError = originalError;
|
|
241
|
+
return error;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Get current configuration
|
|
245
|
+
*/
|
|
246
|
+
getConfig() {
|
|
247
|
+
return { ...this.config };
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Update configuration
|
|
251
|
+
*/
|
|
252
|
+
updateConfig(newConfig) {
|
|
253
|
+
this.config = { ...this.config, ...newConfig };
|
|
254
|
+
this.initializeClient();
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Clear cache
|
|
258
|
+
*/
|
|
259
|
+
clearCache() {
|
|
260
|
+
this.cache.clear();
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get cache statistics
|
|
264
|
+
*/
|
|
265
|
+
getCacheStats() {
|
|
266
|
+
// This is a simplified implementation
|
|
267
|
+
return {
|
|
268
|
+
size: this.cache.size,
|
|
269
|
+
hitRate: 0 // Would need to track hits/misses for accurate calculation
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Global AI executor instance
|
|
275
|
+
*/
|
|
276
|
+
let globalExecutor = null;
|
|
277
|
+
/**
|
|
278
|
+
* Get or create the global AI executor instance
|
|
279
|
+
*/
|
|
280
|
+
export function getAIExecutor() {
|
|
281
|
+
if (!globalExecutor) {
|
|
282
|
+
globalExecutor = new AIExecutor();
|
|
283
|
+
}
|
|
284
|
+
return globalExecutor;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Reset the global AI executor (useful for testing)
|
|
288
|
+
*/
|
|
289
|
+
export function resetAIExecutor() {
|
|
290
|
+
globalExecutor = null;
|
|
291
|
+
}
|
|
292
|
+
//# sourceMappingURL=ai-executor.js.map
|