import { z } from 'zod';
/**
* Schema for code review prompt arguments
*/
const CodeReviewArgsSchema = z.object({
code: z.string().describe('The code to review'),
language: z.string().optional().describe('Programming language'),
focus: z
.enum(['security', 'performance', 'readability', 'all'])
.default('all')
.describe('What aspect to focus on')
});
type CodeReviewArgs = z.infer<typeof CodeReviewArgsSchema>;
/**
* Generate the code review prompt
*/
function generatePrompt(args: CodeReviewArgs): string {
const focusInstructions: Record<string, string> = {
security: `
Focus on SECURITY issues:
- SQL injection vulnerabilities
- XSS vulnerabilities
- Authentication/authorization flaws
- Sensitive data exposure
- Input validation issues`,
performance: `
Focus on PERFORMANCE issues:
- Algorithmic complexity (Big O)
- Memory leaks
- Unnecessary loops/iterations
- Database query optimization
- Caching opportunities`,
readability: `
Focus on READABILITY:
- Code organization
- Naming conventions
- Comments and documentation
- Function/method length
- Code duplication`,
all: `
Review ALL aspects:
1. Security vulnerabilities
2. Performance bottlenecks
3. Code readability
4. Best practices
5. Error handling`
};
return `You are an expert code reviewer. Please review the following ${args.language || ''} code.
${focusInstructions[args.focus]}
## Code to Review:
\`\`\`${args.language || ''}
${args.code}
\`\`\`
## Instructions:
1. Identify issues and rate severity (Critical/High/Medium/Low)
2. Explain WHY each issue is a problem
3. Provide specific code fixes
4. Summarize with actionable recommendations
Please be thorough but constructive.`;
}
/**
* Code Review Prompt definition
*/
export const codeReviewPrompt = {
name: 'code-review',
description: 'Generate a comprehensive code review prompt',
arguments: [
{
name: 'code',
description: 'The code snippet to review',
required: true
},
{
name: 'language',
description: 'Programming language (e.g., typescript, python)',
required: false
},
{
name: 'focus',
description: 'Review focus: security, performance, readability, or all',
required: false
}
],
load: async (args: Record<string, string>) => {
const parsed = CodeReviewArgsSchema.parse({
code: args.code,
language: args.language,
focus: args.focus || 'all'
});
return {
messages: [
{
role: 'user' as const,
content: {
type: 'text' as const,
text: generatePrompt(parsed)
}
}
]
};
}
};