wiggum-cli 0.2.6 → 0.3.1
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 +73 -60
- package/dist/ai/agents/codebase-analyst.d.ts +11 -0
- package/dist/ai/agents/codebase-analyst.d.ts.map +1 -0
- package/dist/ai/agents/codebase-analyst.js +146 -0
- package/dist/ai/agents/codebase-analyst.js.map +1 -0
- package/dist/ai/agents/index.d.ts +16 -0
- package/dist/ai/agents/index.d.ts.map +1 -0
- package/dist/ai/agents/index.js +85 -0
- package/dist/ai/agents/index.js.map +1 -0
- package/dist/ai/agents/orchestrator.d.ts +15 -0
- package/dist/ai/agents/orchestrator.d.ts.map +1 -0
- package/dist/ai/agents/orchestrator.js +181 -0
- package/dist/ai/agents/orchestrator.js.map +1 -0
- package/dist/ai/agents/stack-researcher.d.ts +15 -0
- package/dist/ai/agents/stack-researcher.d.ts.map +1 -0
- package/dist/ai/agents/stack-researcher.js +269 -0
- package/dist/ai/agents/stack-researcher.js.map +1 -0
- package/dist/ai/agents/types.d.ts +123 -0
- package/dist/ai/agents/types.d.ts.map +1 -0
- package/dist/ai/agents/types.js +6 -0
- package/dist/ai/agents/types.js.map +1 -0
- package/dist/ai/enhancer.d.ts +39 -1
- package/dist/ai/enhancer.d.ts.map +1 -1
- package/dist/ai/enhancer.js +78 -36
- package/dist/ai/enhancer.js.map +1 -1
- package/dist/ai/index.d.ts +4 -2
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/index.js +5 -1
- package/dist/ai/index.js.map +1 -1
- package/dist/ai/prompts.d.ts +2 -2
- package/dist/ai/prompts.d.ts.map +1 -1
- package/dist/ai/prompts.js +66 -4
- package/dist/ai/prompts.js.map +1 -1
- package/dist/ai/providers.d.ts +28 -0
- package/dist/ai/providers.d.ts.map +1 -1
- package/dist/ai/providers.js +40 -0
- package/dist/ai/providers.js.map +1 -1
- package/dist/ai/tools/context7.d.ts +34 -0
- package/dist/ai/tools/context7.d.ts.map +1 -0
- package/dist/ai/tools/context7.js +135 -0
- package/dist/ai/tools/context7.js.map +1 -0
- package/dist/ai/tools/index.d.ts +7 -0
- package/dist/ai/tools/index.d.ts.map +1 -0
- package/dist/ai/tools/index.js +7 -0
- package/dist/ai/tools/index.js.map +1 -0
- package/dist/ai/tools/tavily.d.ts +27 -0
- package/dist/ai/tools/tavily.d.ts.map +1 -0
- package/dist/ai/tools/tavily.js +75 -0
- package/dist/ai/tools/tavily.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +14 -12
- package/dist/cli.js.map +1 -1
- package/dist/commands/init.d.ts +2 -5
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +233 -154
- package/dist/commands/init.js.map +1 -1
- package/dist/utils/colors.d.ts.map +1 -1
- package/dist/utils/colors.js +10 -3
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/header.d.ts +1 -1
- package/dist/utils/header.js +3 -3
- package/dist/utils/header.js.map +1 -1
- package/dist/utils/json-repair.d.ts +14 -0
- package/dist/utils/json-repair.d.ts.map +1 -0
- package/dist/utils/json-repair.js +103 -0
- package/dist/utils/json-repair.js.map +1 -0
- package/dist/utils/tracing.d.ts +25 -0
- package/dist/utils/tracing.d.ts.map +1 -0
- package/dist/utils/tracing.js +64 -0
- package/dist/utils/tracing.js.map +1 -0
- package/package.json +5 -3
- package/src/ai/agents/codebase-analyst.ts +169 -0
- package/src/ai/agents/index.ts +147 -0
- package/src/ai/agents/orchestrator.ts +218 -0
- package/src/ai/agents/stack-researcher.ts +294 -0
- package/src/ai/agents/types.ts +132 -0
- package/src/ai/enhancer.ts +128 -38
- package/src/ai/index.ts +31 -1
- package/src/ai/prompts.ts +67 -4
- package/src/ai/providers.ts +48 -0
- package/src/ai/tools/context7.ts +167 -0
- package/src/ai/tools/index.ts +17 -0
- package/src/ai/tools/tavily.ts +101 -0
- package/src/cli.ts +14 -12
- package/src/commands/init.ts +278 -173
- package/src/utils/colors.ts +11 -3
- package/src/utils/header.ts +3 -3
- package/src/utils/json-repair.ts +113 -0
- package/src/utils/tracing.ts +76 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrator Agent
|
|
3
|
+
* Coordinates the multi-agent analysis and merges results
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { LanguageModel } from 'ai';
|
|
7
|
+
import type {
|
|
8
|
+
CodebaseAnalysis,
|
|
9
|
+
StackResearch,
|
|
10
|
+
MultiAgentAnalysis,
|
|
11
|
+
McpRecommendations,
|
|
12
|
+
OrchestratorInput,
|
|
13
|
+
} from './types.js';
|
|
14
|
+
import type { DetectedStack } from '../../scanner/types.js';
|
|
15
|
+
import { isReasoningModel } from '../providers.js';
|
|
16
|
+
import { logger } from '../../utils/logger.js';
|
|
17
|
+
import { parseJsonSafe } from '../../utils/json-repair.js';
|
|
18
|
+
import { getTracedAI } from '../../utils/tracing.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* System prompt for the Orchestrator
|
|
22
|
+
*/
|
|
23
|
+
const ORCHESTRATOR_SYSTEM_PROMPT = `You are an Orchestrator agent that merges analysis results into final recommendations.
|
|
24
|
+
|
|
25
|
+
Your job is to:
|
|
26
|
+
1. Review the codebase analysis and stack research
|
|
27
|
+
2. Recommend appropriate MCP servers for the stack
|
|
28
|
+
3. Produce a final merged recommendation
|
|
29
|
+
|
|
30
|
+
## MCP Server Selection Guidelines
|
|
31
|
+
Based on the detected stack, recommend these MCP servers:
|
|
32
|
+
|
|
33
|
+
**Always Essential:**
|
|
34
|
+
- filesystem: For file operations (all projects)
|
|
35
|
+
- git: For version control (all projects)
|
|
36
|
+
|
|
37
|
+
**By Project Type:**
|
|
38
|
+
- MCP Server: memory (for context)
|
|
39
|
+
- REST API: fetch (external APIs), postgres/sqlite (databases)
|
|
40
|
+
- React/Next.js: fetch (data fetching), memory (state persistence)
|
|
41
|
+
- CLI Tool: filesystem (primary), memory (caching)
|
|
42
|
+
- Library: git (versioning)
|
|
43
|
+
|
|
44
|
+
**By Technology:**
|
|
45
|
+
- Docker in use → docker MCP server
|
|
46
|
+
- PostgreSQL → postgres MCP server
|
|
47
|
+
- SQLite → sqlite MCP server
|
|
48
|
+
- AWS services → aws-kb-retrieval MCP server
|
|
49
|
+
- GitHub workflows → github MCP server
|
|
50
|
+
|
|
51
|
+
## Output Format
|
|
52
|
+
Output ONLY valid JSON:
|
|
53
|
+
{
|
|
54
|
+
"mcpServers": {
|
|
55
|
+
"essential": ["filesystem", "git"],
|
|
56
|
+
"recommended": ["docker", "postgres"]
|
|
57
|
+
}
|
|
58
|
+
}`;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Run the Orchestrator to merge results and recommend MCP servers
|
|
62
|
+
*/
|
|
63
|
+
export async function runOrchestrator(
|
|
64
|
+
model: LanguageModel,
|
|
65
|
+
modelId: string,
|
|
66
|
+
input: OrchestratorInput,
|
|
67
|
+
verbose: boolean = false
|
|
68
|
+
): Promise<McpRecommendations> {
|
|
69
|
+
const prompt = createOrchestratorPrompt(input);
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const { generateText } = getTracedAI();
|
|
73
|
+
|
|
74
|
+
const result = await generateText({
|
|
75
|
+
model,
|
|
76
|
+
system: ORCHESTRATOR_SYSTEM_PROMPT,
|
|
77
|
+
prompt,
|
|
78
|
+
maxOutputTokens: 1000,
|
|
79
|
+
...(isReasoningModel(modelId) ? {} : { temperature: 0.2 }),
|
|
80
|
+
experimental_telemetry: {
|
|
81
|
+
isEnabled: true,
|
|
82
|
+
metadata: { agent: 'orchestrator', projectType: input.codebaseAnalysis.projectContext.projectType },
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const mcpServers = parseMcpRecommendations(result.text, input.stack, verbose);
|
|
87
|
+
return mcpServers;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (verbose) {
|
|
90
|
+
logger.error(`Orchestrator error: ${error instanceof Error ? error.message : String(error)}`);
|
|
91
|
+
}
|
|
92
|
+
// Return default recommendations based on project type
|
|
93
|
+
return getDefaultMcpRecommendations(input.codebaseAnalysis.projectContext.projectType, input.stack);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Create the orchestrator prompt
|
|
99
|
+
*/
|
|
100
|
+
function createOrchestratorPrompt(input: OrchestratorInput): string {
|
|
101
|
+
const { codebaseAnalysis, stackResearch, stack } = input;
|
|
102
|
+
|
|
103
|
+
// Summarize the stack
|
|
104
|
+
const stackSummary: string[] = [];
|
|
105
|
+
if (stack.framework) stackSummary.push(`Framework: ${stack.framework.name}`);
|
|
106
|
+
if (stack.database) stackSummary.push(`Database: ${stack.database.name}`);
|
|
107
|
+
if (stack.orm) stackSummary.push(`ORM: ${stack.orm.name}`);
|
|
108
|
+
if (stack.deployment?.length) stackSummary.push(`Deployment: ${stack.deployment.map(d => d.name).join(', ')}`);
|
|
109
|
+
if (stack.mcp?.isProject) stackSummary.push('This is an MCP Server project');
|
|
110
|
+
|
|
111
|
+
return `Analyze these results and recommend MCP servers:
|
|
112
|
+
|
|
113
|
+
## Project Type
|
|
114
|
+
${codebaseAnalysis.projectContext.projectType}
|
|
115
|
+
|
|
116
|
+
## Detected Stack
|
|
117
|
+
${stackSummary.join('\n') || 'Unknown'}
|
|
118
|
+
|
|
119
|
+
## Testing Tools Identified
|
|
120
|
+
${stackResearch.testingTools.join(', ') || 'None identified'}
|
|
121
|
+
|
|
122
|
+
## Debugging Tools
|
|
123
|
+
${stackResearch.debuggingTools.join(', ') || 'None identified'}
|
|
124
|
+
|
|
125
|
+
Based on this analysis, recommend essential and optional MCP servers.
|
|
126
|
+
Output as JSON with "mcpServers" containing "essential" and "recommended" arrays.`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Parse MCP recommendations from orchestrator response
|
|
131
|
+
*/
|
|
132
|
+
function parseMcpRecommendations(
|
|
133
|
+
text: string,
|
|
134
|
+
stack: DetectedStack,
|
|
135
|
+
verbose: boolean
|
|
136
|
+
): McpRecommendations {
|
|
137
|
+
if (!text || text.trim() === '') {
|
|
138
|
+
return getDefaultMcpRecommendations('Unknown', stack);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Use safe JSON parser with repair capabilities
|
|
142
|
+
const parsed = parseJsonSafe<{ mcpServers?: McpRecommendations }>(text);
|
|
143
|
+
|
|
144
|
+
if (!parsed) {
|
|
145
|
+
if (verbose) {
|
|
146
|
+
logger.warn('Orchestrator: Failed to parse JSON response');
|
|
147
|
+
}
|
|
148
|
+
return getDefaultMcpRecommendations('Unknown', stack);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (parsed.mcpServers) {
|
|
152
|
+
return {
|
|
153
|
+
essential: parsed.mcpServers.essential || ['filesystem', 'git'],
|
|
154
|
+
recommended: parsed.mcpServers.recommended || [],
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return getDefaultMcpRecommendations('Unknown', stack);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Get default MCP recommendations based on project type and stack
|
|
163
|
+
*/
|
|
164
|
+
function getDefaultMcpRecommendations(projectType: string, stack: DetectedStack): McpRecommendations {
|
|
165
|
+
const essential: string[] = ['filesystem', 'git'];
|
|
166
|
+
const recommended: string[] = [];
|
|
167
|
+
|
|
168
|
+
// Add based on project type
|
|
169
|
+
const lowerType = projectType.toLowerCase();
|
|
170
|
+
|
|
171
|
+
if (lowerType.includes('mcp')) {
|
|
172
|
+
recommended.push('memory');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (lowerType.includes('api') || lowerType.includes('server')) {
|
|
176
|
+
recommended.push('fetch');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Add based on detected stack
|
|
180
|
+
if (stack.database) {
|
|
181
|
+
const dbName = stack.database.name.toLowerCase();
|
|
182
|
+
if (dbName.includes('postgres')) {
|
|
183
|
+
recommended.push('postgres');
|
|
184
|
+
} else if (dbName.includes('sqlite')) {
|
|
185
|
+
recommended.push('sqlite');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (stack.deployment?.some(d => d.name.toLowerCase().includes('docker'))) {
|
|
190
|
+
recommended.push('docker');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Add from existing MCP recommendations in stack
|
|
194
|
+
if (stack.mcp?.recommended) {
|
|
195
|
+
for (const rec of stack.mcp.recommended) {
|
|
196
|
+
if (!essential.includes(rec) && !recommended.includes(rec)) {
|
|
197
|
+
recommended.push(rec);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return { essential, recommended };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Merge all agent results into a final MultiAgentAnalysis
|
|
207
|
+
*/
|
|
208
|
+
export function mergeAgentResults(
|
|
209
|
+
codebaseAnalysis: CodebaseAnalysis,
|
|
210
|
+
stackResearch: StackResearch,
|
|
211
|
+
mcpServers: McpRecommendations
|
|
212
|
+
): MultiAgentAnalysis {
|
|
213
|
+
return {
|
|
214
|
+
codebaseAnalysis,
|
|
215
|
+
stackResearch,
|
|
216
|
+
mcpServers,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stack Researcher Agent
|
|
3
|
+
* Researches best practices and tools for the detected stack
|
|
4
|
+
* Gracefully degrades when optional services are unavailable
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { stepCountIs, type LanguageModel, type Tool } from 'ai';
|
|
8
|
+
import type { StackResearch, StackResearcherInput, AgentCapabilities } from './types.js';
|
|
9
|
+
import type { DetectedStack } from '../../scanner/types.js';
|
|
10
|
+
import { createTavilySearchTool } from '../tools/tavily.js';
|
|
11
|
+
import { createContext7Tool } from '../tools/context7.js';
|
|
12
|
+
import { isReasoningModel } from '../providers.js';
|
|
13
|
+
import { logger } from '../../utils/logger.js';
|
|
14
|
+
import { parseJsonSafe } from '../../utils/json-repair.js';
|
|
15
|
+
import { getTracedAI } from '../../utils/tracing.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* System prompt for Stack Researcher with tools
|
|
19
|
+
*/
|
|
20
|
+
const STACK_RESEARCHER_WITH_TOOLS_PROMPT = `You are a Stack Researcher agent with access to web search and documentation lookup tools.
|
|
21
|
+
|
|
22
|
+
## Your Mission
|
|
23
|
+
Research the detected technology stack to find:
|
|
24
|
+
1. Current best practices
|
|
25
|
+
2. Common anti-patterns to avoid
|
|
26
|
+
3. Testing tools and patterns
|
|
27
|
+
4. Debugging approaches
|
|
28
|
+
5. Useful documentation links
|
|
29
|
+
|
|
30
|
+
## Tools Available
|
|
31
|
+
- tavilySearch: Search the web for current best practices and patterns
|
|
32
|
+
- context7Lookup: Look up library documentation
|
|
33
|
+
|
|
34
|
+
## Research Strategy
|
|
35
|
+
1. Search for "[technology] best practices 2024"
|
|
36
|
+
2. Search for "[project type] testing patterns"
|
|
37
|
+
3. Look up documentation for key dependencies
|
|
38
|
+
4. Search for "[framework] anti-patterns"
|
|
39
|
+
|
|
40
|
+
## Output Format
|
|
41
|
+
After research, output ONLY valid JSON with this structure:
|
|
42
|
+
{
|
|
43
|
+
"bestPractices": [
|
|
44
|
+
"Use TypeScript strict mode",
|
|
45
|
+
"Implement proper error boundaries"
|
|
46
|
+
],
|
|
47
|
+
"antiPatterns": [
|
|
48
|
+
"Don't use any type",
|
|
49
|
+
"Avoid prop drilling"
|
|
50
|
+
],
|
|
51
|
+
"testingTools": [
|
|
52
|
+
"npx vitest",
|
|
53
|
+
"npx playwright test"
|
|
54
|
+
],
|
|
55
|
+
"debuggingTools": [
|
|
56
|
+
"React DevTools",
|
|
57
|
+
"DEBUG=* npm run dev"
|
|
58
|
+
],
|
|
59
|
+
"documentationHints": [
|
|
60
|
+
"React docs: react.dev",
|
|
61
|
+
"Vitest: vitest.dev"
|
|
62
|
+
],
|
|
63
|
+
"researchMode": "full"
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Keep each item concise (5-15 words max). Max 5 items per array.`;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* System prompt for Stack Researcher without tools (knowledge-only)
|
|
70
|
+
*/
|
|
71
|
+
const STACK_RESEARCHER_KNOWLEDGE_ONLY_PROMPT = `You are a Stack Researcher agent. You don't have web access, so rely on your training knowledge.
|
|
72
|
+
|
|
73
|
+
## Your Mission
|
|
74
|
+
Based on your knowledge, provide:
|
|
75
|
+
1. Best practices for the detected technologies
|
|
76
|
+
2. Common anti-patterns to avoid
|
|
77
|
+
3. Testing tools commonly used with this stack
|
|
78
|
+
4. Debugging approaches
|
|
79
|
+
5. Documentation hints
|
|
80
|
+
|
|
81
|
+
## Important Notes
|
|
82
|
+
- Be explicit about what you're confident about vs uncertain
|
|
83
|
+
- Focus on well-established practices from your training
|
|
84
|
+
- Mention if something might be outdated
|
|
85
|
+
|
|
86
|
+
## Output Format
|
|
87
|
+
Output ONLY valid JSON with this structure:
|
|
88
|
+
{
|
|
89
|
+
"bestPractices": [
|
|
90
|
+
"Use TypeScript strict mode",
|
|
91
|
+
"Implement proper error boundaries"
|
|
92
|
+
],
|
|
93
|
+
"antiPatterns": [
|
|
94
|
+
"Don't use any type",
|
|
95
|
+
"Avoid prop drilling"
|
|
96
|
+
],
|
|
97
|
+
"testingTools": [
|
|
98
|
+
"npm test",
|
|
99
|
+
"npx vitest"
|
|
100
|
+
],
|
|
101
|
+
"debuggingTools": [
|
|
102
|
+
"console.log debugging",
|
|
103
|
+
"Node.js inspector"
|
|
104
|
+
],
|
|
105
|
+
"documentationHints": [
|
|
106
|
+
"Check official docs for updates",
|
|
107
|
+
"Framework docs: [URL]"
|
|
108
|
+
],
|
|
109
|
+
"researchMode": "knowledge-only"
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
Keep each item concise (5-15 words max). Max 5 items per array.
|
|
113
|
+
Note: Research mode should reflect that you're using training knowledge only.`;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create the research prompt based on stack and project type
|
|
117
|
+
*/
|
|
118
|
+
function createResearchPrompt(stack: DetectedStack, projectType: string, hasTools: boolean): string {
|
|
119
|
+
const technologies: string[] = [];
|
|
120
|
+
|
|
121
|
+
// Collect all detected technologies
|
|
122
|
+
if (stack.framework) technologies.push(stack.framework.name);
|
|
123
|
+
if (stack.testing?.unit) technologies.push(stack.testing.unit.name);
|
|
124
|
+
if (stack.testing?.e2e) technologies.push(stack.testing.e2e.name);
|
|
125
|
+
if (stack.orm) technologies.push(stack.orm.name);
|
|
126
|
+
if (stack.database) technologies.push(stack.database.name);
|
|
127
|
+
if (stack.stateManagement) technologies.push(stack.stateManagement.name);
|
|
128
|
+
if (stack.auth) technologies.push(stack.auth.name);
|
|
129
|
+
if (stack.mcp?.isProject) technologies.push('MCP Server');
|
|
130
|
+
|
|
131
|
+
const techList = technologies.length > 0 ? technologies.join(', ') : 'Unknown stack';
|
|
132
|
+
|
|
133
|
+
if (hasTools) {
|
|
134
|
+
return `Research best practices for this stack:
|
|
135
|
+
|
|
136
|
+
Project Type: ${projectType}
|
|
137
|
+
Technologies: ${techList}
|
|
138
|
+
|
|
139
|
+
Use the available tools to search for:
|
|
140
|
+
1. Current best practices for ${projectType} projects
|
|
141
|
+
2. Testing patterns for ${techList}
|
|
142
|
+
3. Common anti-patterns to avoid
|
|
143
|
+
|
|
144
|
+
Then produce your research findings as JSON.`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return `Based on your knowledge, provide best practices for this stack:
|
|
148
|
+
|
|
149
|
+
Project Type: ${projectType}
|
|
150
|
+
Technologies: ${techList}
|
|
151
|
+
|
|
152
|
+
Provide:
|
|
153
|
+
1. Best practices for ${projectType} projects
|
|
154
|
+
2. Testing tools commonly used with ${techList}
|
|
155
|
+
3. Anti-patterns to avoid
|
|
156
|
+
4. Debugging approaches
|
|
157
|
+
|
|
158
|
+
Output your findings as JSON. Be clear this is based on training knowledge.`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Determine research mode based on capabilities
|
|
163
|
+
*/
|
|
164
|
+
function getResearchMode(capabilities: AgentCapabilities): StackResearch['researchMode'] {
|
|
165
|
+
if (capabilities.hasTavily && capabilities.hasContext7) return 'full';
|
|
166
|
+
if (capabilities.hasTavily) return 'web-only';
|
|
167
|
+
if (capabilities.hasContext7) return 'docs-only';
|
|
168
|
+
return 'knowledge-only';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Run the Stack Researcher agent
|
|
173
|
+
*/
|
|
174
|
+
export async function runStackResearcher(
|
|
175
|
+
model: LanguageModel,
|
|
176
|
+
modelId: string,
|
|
177
|
+
input: StackResearcherInput,
|
|
178
|
+
options: { tavilyApiKey?: string; context7ApiKey?: string },
|
|
179
|
+
verbose: boolean = false
|
|
180
|
+
): Promise<StackResearch | null> {
|
|
181
|
+
const tools: Record<string, Tool> = {};
|
|
182
|
+
|
|
183
|
+
// Add tools based on available keys
|
|
184
|
+
if (options.tavilyApiKey) {
|
|
185
|
+
tools.tavilySearch = createTavilySearchTool(options.tavilyApiKey);
|
|
186
|
+
}
|
|
187
|
+
if (options.context7ApiKey) {
|
|
188
|
+
tools.context7Lookup = createContext7Tool(options.context7ApiKey);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const hasTools = Object.keys(tools).length > 0;
|
|
192
|
+
const researchMode = getResearchMode(input.capabilities);
|
|
193
|
+
|
|
194
|
+
if (verbose) {
|
|
195
|
+
logger.info(`Stack Researcher running in ${researchMode} mode`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const systemPrompt = hasTools
|
|
199
|
+
? STACK_RESEARCHER_WITH_TOOLS_PROMPT
|
|
200
|
+
: STACK_RESEARCHER_KNOWLEDGE_ONLY_PROMPT;
|
|
201
|
+
|
|
202
|
+
const prompt = createResearchPrompt(input.stack, input.projectType, hasTools);
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const { generateText } = getTracedAI();
|
|
206
|
+
|
|
207
|
+
const result = await generateText({
|
|
208
|
+
model,
|
|
209
|
+
system: systemPrompt,
|
|
210
|
+
prompt,
|
|
211
|
+
...(hasTools ? { tools, stopWhen: stepCountIs(8) } : {}),
|
|
212
|
+
maxOutputTokens: 2000,
|
|
213
|
+
...(isReasoningModel(modelId) ? {} : { temperature: 0.3 }),
|
|
214
|
+
experimental_telemetry: {
|
|
215
|
+
isEnabled: true,
|
|
216
|
+
metadata: { agent: 'stack-researcher', researchMode, projectType: input.projectType },
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Parse the response
|
|
221
|
+
const research = parseStackResearch(result.text, result.steps, researchMode, verbose);
|
|
222
|
+
return research;
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (verbose) {
|
|
225
|
+
logger.error(`Stack Researcher error: ${error instanceof Error ? error.message : String(error)}`);
|
|
226
|
+
}
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Parse the stack research from agent response
|
|
233
|
+
*/
|
|
234
|
+
function parseStackResearch(
|
|
235
|
+
text: string,
|
|
236
|
+
steps: Array<{ text?: string }> | undefined,
|
|
237
|
+
researchMode: StackResearch['researchMode'],
|
|
238
|
+
verbose: boolean
|
|
239
|
+
): StackResearch | null {
|
|
240
|
+
// Try to get text from the result or steps
|
|
241
|
+
let textToParse = text;
|
|
242
|
+
|
|
243
|
+
if (!textToParse || textToParse.trim() === '') {
|
|
244
|
+
const stepsList = steps || [];
|
|
245
|
+
for (let i = stepsList.length - 1; i >= 0; i--) {
|
|
246
|
+
const step = stepsList[i];
|
|
247
|
+
if (step.text && step.text.trim() !== '') {
|
|
248
|
+
textToParse = step.text;
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!textToParse || textToParse.trim() === '') {
|
|
255
|
+
if (verbose) {
|
|
256
|
+
logger.warn('Stack Researcher: No text output found');
|
|
257
|
+
}
|
|
258
|
+
return getDefaultStackResearch(researchMode);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Use safe JSON parser with repair capabilities
|
|
262
|
+
const parsed = parseJsonSafe<Partial<StackResearch>>(textToParse);
|
|
263
|
+
|
|
264
|
+
if (!parsed) {
|
|
265
|
+
if (verbose) {
|
|
266
|
+
logger.warn('Stack Researcher: Failed to parse JSON response');
|
|
267
|
+
}
|
|
268
|
+
return getDefaultStackResearch(researchMode);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Build result with defaults for missing fields
|
|
272
|
+
return {
|
|
273
|
+
bestPractices: parsed.bestPractices || [],
|
|
274
|
+
antiPatterns: parsed.antiPatterns || [],
|
|
275
|
+
testingTools: parsed.testingTools || [],
|
|
276
|
+
debuggingTools: parsed.debuggingTools || [],
|
|
277
|
+
documentationHints: parsed.documentationHints || [],
|
|
278
|
+
researchMode: researchMode,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Get default stack research when parsing fails
|
|
284
|
+
*/
|
|
285
|
+
function getDefaultStackResearch(researchMode: StackResearch['researchMode']): StackResearch {
|
|
286
|
+
return {
|
|
287
|
+
bestPractices: ['Follow project conventions', 'Write tests for new code'],
|
|
288
|
+
antiPatterns: ['Avoid skipping tests', 'Don\'t ignore type errors'],
|
|
289
|
+
testingTools: ['npm test'],
|
|
290
|
+
debuggingTools: ['console.log', 'debugger statement'],
|
|
291
|
+
documentationHints: ['Check package.json for dependencies'],
|
|
292
|
+
researchMode,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Types and Interfaces
|
|
3
|
+
* Defines the structure for multi-agent analysis
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ScanResult, DetectedStack } from '../../scanner/types.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Codebase analysis result from the Codebase Analyst agent
|
|
10
|
+
*/
|
|
11
|
+
export interface CodebaseAnalysis {
|
|
12
|
+
/** Project structure and context */
|
|
13
|
+
projectContext: {
|
|
14
|
+
/** Key entry point files */
|
|
15
|
+
entryPoints: string[];
|
|
16
|
+
/** Important directories and their purposes */
|
|
17
|
+
keyDirectories: Record<string, string>;
|
|
18
|
+
/** Naming conventions used */
|
|
19
|
+
namingConventions?: string;
|
|
20
|
+
/** The primary project type (MCP Server, REST API, React SPA, CLI, Library) */
|
|
21
|
+
projectType: string;
|
|
22
|
+
};
|
|
23
|
+
/** Detected commands from package.json */
|
|
24
|
+
commands: {
|
|
25
|
+
test?: string;
|
|
26
|
+
lint?: string;
|
|
27
|
+
typecheck?: string;
|
|
28
|
+
build?: string;
|
|
29
|
+
dev?: string;
|
|
30
|
+
format?: string;
|
|
31
|
+
};
|
|
32
|
+
/** Short, actionable implementation guidelines */
|
|
33
|
+
implementationGuidelines: string[];
|
|
34
|
+
/** Additional technologies that may have been missed */
|
|
35
|
+
possibleMissedTechnologies?: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Stack research result from the Stack Researcher agent
|
|
40
|
+
*/
|
|
41
|
+
export interface StackResearch {
|
|
42
|
+
/** Best practices for the detected stack */
|
|
43
|
+
bestPractices: string[];
|
|
44
|
+
/** Anti-patterns to avoid */
|
|
45
|
+
antiPatterns: string[];
|
|
46
|
+
/** Technology-specific testing tools */
|
|
47
|
+
testingTools: string[];
|
|
48
|
+
/** Technology-specific debugging tools */
|
|
49
|
+
debuggingTools: string[];
|
|
50
|
+
/** Documentation hints and links */
|
|
51
|
+
documentationHints: string[];
|
|
52
|
+
/** Whether research was performed with tools or knowledge-only */
|
|
53
|
+
researchMode: 'full' | 'web-only' | 'docs-only' | 'knowledge-only';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* MCP server recommendations
|
|
58
|
+
*/
|
|
59
|
+
export interface McpRecommendations {
|
|
60
|
+
/** Essential MCP servers for this stack */
|
|
61
|
+
essential: string[];
|
|
62
|
+
/** Recommended but optional MCP servers */
|
|
63
|
+
recommended: string[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Combined result from all agents
|
|
68
|
+
*/
|
|
69
|
+
export interface MultiAgentAnalysis {
|
|
70
|
+
/** Codebase analysis from the Codebase Analyst */
|
|
71
|
+
codebaseAnalysis: CodebaseAnalysis;
|
|
72
|
+
/** Stack research from the Stack Researcher */
|
|
73
|
+
stackResearch: StackResearch;
|
|
74
|
+
/** MCP server recommendations (merged from both agents) */
|
|
75
|
+
mcpServers: McpRecommendations;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Agent capabilities based on available API keys
|
|
80
|
+
*/
|
|
81
|
+
export interface AgentCapabilities {
|
|
82
|
+
/** Tavily web search available */
|
|
83
|
+
hasTavily: boolean;
|
|
84
|
+
/** Context7 documentation lookup available */
|
|
85
|
+
hasContext7: boolean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Options for running agents
|
|
90
|
+
*/
|
|
91
|
+
export interface AgentOptions {
|
|
92
|
+
/** Tavily API key (optional) */
|
|
93
|
+
tavilyApiKey?: string;
|
|
94
|
+
/** Context7 API key (optional) */
|
|
95
|
+
context7ApiKey?: string;
|
|
96
|
+
/** Enable verbose logging */
|
|
97
|
+
verbose?: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Input for the Codebase Analyst agent
|
|
102
|
+
*/
|
|
103
|
+
export interface CodebaseAnalystInput {
|
|
104
|
+
/** The scan result from the scanner */
|
|
105
|
+
scanResult: ScanResult;
|
|
106
|
+
/** Project root directory */
|
|
107
|
+
projectRoot: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Input for the Stack Researcher agent
|
|
112
|
+
*/
|
|
113
|
+
export interface StackResearcherInput {
|
|
114
|
+
/** The detected stack */
|
|
115
|
+
stack: DetectedStack;
|
|
116
|
+
/** The identified project type */
|
|
117
|
+
projectType: string;
|
|
118
|
+
/** Agent capabilities */
|
|
119
|
+
capabilities: AgentCapabilities;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Input for the Orchestrator agent
|
|
124
|
+
*/
|
|
125
|
+
export interface OrchestratorInput {
|
|
126
|
+
/** Codebase analysis result */
|
|
127
|
+
codebaseAnalysis: CodebaseAnalysis;
|
|
128
|
+
/** Stack research result */
|
|
129
|
+
stackResearch: StackResearch;
|
|
130
|
+
/** The detected stack */
|
|
131
|
+
stack: DetectedStack;
|
|
132
|
+
}
|