-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path05-complex-example.ts
More file actions
283 lines (253 loc) · 9.14 KB
/
05-complex-example.ts
File metadata and controls
283 lines (253 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* Example 5: Complex Example - Code Review Assistant
*
* This example demonstrates a sophisticated code review assistant that adapts
* based on programming language, code complexity, reviewer expertise level,
* and review focus areas. It showcases conditional logic, parameter substitution,
* and dynamic content generation.
*/
import { PromptBuilder, createCondition, PersonaComponent, ToneComponent } from '../dist';
interface CodeReviewContext {
language: string;
framework?: string;
codeComplexity: 'simple' | 'moderate' | 'complex';
reviewerLevel: 'junior' | 'mid' | 'senior' | 'expert';
focusAreas?: string[];
codeSnippet: string;
fileName: string;
authorName: string;
}
function buildCodeReviewPrompt(context: CodeReviewContext) {
const {
language,
framework,
codeComplexity,
reviewerLevel,
focusAreas = [],
codeSnippet,
fileName,
authorName,
} = context;
// Determine review depth based on complexity and reviewer level
const isDeepReview = codeComplexity === 'complex' || reviewerLevel === 'senior' || reviewerLevel === 'expert';
const isBeginnerFriendly = reviewerLevel === 'junior';
// Build focus areas string
const focusAreasStr = focusAreas.length > 0
? focusAreas.join(', ')
: 'code quality, best practices, performance, security, maintainability';
const builder = new PromptBuilder()
.role('You are an expert code reviewer with deep knowledge of software engineering best practices')
.goal(`Provide a comprehensive code review for ${authorName}'s code in ${fileName}`)
// Persona based on reviewer level
.persona('Mentor', {
condition: createCondition(
(params) => params.reviewerLevel === 'junior' || params.reviewerLevel === 'mid',
new PersonaComponent(`You are a supportive mentor. Explain concepts clearly, provide learning opportunities, and be encouraging while pointing out areas for improvement.`)
)
})
.persona('Expert Consultant', {
condition: createCondition(
(params) => params.reviewerLevel === 'senior' || params.reviewerLevel === 'expert',
new PersonaComponent(`You are an expert consultant. Provide deep technical insights, architectural considerations, and advanced optimization suggestions. Be direct but respectful.`)
)
})
// Tone based on complexity and reviewer level
.tone('Educational and Encouraging', {
condition: createCondition(
(params) => params.isBeginnerFriendly === true,
new ToneComponent('Use clear, educational language. Explain the "why" behind suggestions. Be encouraging and supportive.')
)
})
.tone('Professional and Direct', {
condition: createCondition(
(params) => params.isBeginnerFriendly === false,
new ToneComponent('Use professional, concise language. Focus on actionable feedback without over-explaining basics.')
)
})
// Context about the codebase
.context(`Code Review Context:
- Programming Language: ${language}
${framework ? `- Framework: ${framework}` : ''}
- Code Complexity: ${codeComplexity}
- Reviewer Level: ${reviewerLevel}
- File: ${fileName}
- Author: ${authorName}
- Review Date: ${new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})}`)
// Input - the code to review
.input(`Code to Review:
\`\`\`${language}
${codeSnippet}
\`\`\`
Focus Areas: ${focusAreasStr}`)
// Tasks based on review depth - using array for auto-prefixing
.tasks((params) => {
if (params.isDeepReview) {
return [
'Analyze code structure and architecture',
'Review code quality and best practices',
'Check for performance bottlenecks',
'Identify security vulnerabilities',
'Assess maintainability and readability',
'Review error handling and edge cases',
'Check test coverage considerations',
'Provide specific improvement suggestions with code examples'
];
} else {
return [
'Review code quality and best practices',
'Check for common issues and bugs',
'Assess readability and maintainability',
'Provide improvement suggestions'
];
}
})
// Steps for the review process - using array for auto-prefixing
.steps((params) => {
if (params.isDeepReview) {
return [
'High-level architecture review',
'Line-by-line code analysis',
'Performance and optimization review',
'Security audit',
'Best practices compliance check',
'Documentation and comments review',
'Compile comprehensive feedback'
];
} else {
return [
'Quick code scan for obvious issues',
'Best practices check',
'Provide feedback'
];
}
})
// Guardrails specific to code review - using array for auto-prefixing
.guardrails([
'Be constructive and respectful at all times',
'Focus on the code, not the person',
'Provide actionable feedback, not just criticism',
'Consider the context and constraints of the project',
"Don't suggest changes that would break existing functionality without discussion",
'Respect the author\'s coding style while suggesting improvements',
'If unsure about a suggestion, mark it as "consider" rather than "must change"'
])
// Constraints based on reviewer level - using array for auto-prefixing
.constraints((params) => {
const constraints = [
`Review should be appropriate for ${params.reviewerLevel} level reviewer`,
`Focus on ${params.focusAreasStr}`,
];
if (params.isBeginnerFriendly) {
constraints.push('Explain technical terms when used');
constraints.push('Provide examples for complex suggestions');
}
if (params.isDeepReview) {
constraints.push('Include architectural considerations');
constraints.push('Provide performance metrics if applicable');
}
return constraints;
})
// Output format
.output((params) => {
const sections = [
'## Summary',
'Provide a brief overview (2-3 sentences)',
'',
'## Strengths',
'List what the code does well',
'',
'## Areas for Improvement',
'Organize by priority (Critical, High, Medium, Low)',
'',
'## Specific Suggestions',
'For each suggestion, include:',
'- The issue or improvement',
'- Why it matters',
'- Suggested code change (if applicable)',
'',
'## Questions',
'Any questions or clarifications needed',
];
if (params.isDeepReview) {
sections.push('', '## Architecture Notes', 'High-level considerations and patterns');
sections.push('', '## Performance Considerations', 'Any performance-related observations');
}
return sections.join('\n');
});
return builder.build({
reviewerLevel,
isBeginnerFriendly,
isDeepReview,
language,
framework: framework || '',
codeComplexity,
focusAreasStr,
});
}
// Example 1: Simple code review for junior developer
const prompt1 = buildCodeReviewPrompt({
language: 'TypeScript',
framework: 'React',
codeComplexity: 'simple',
reviewerLevel: 'junior',
focusAreas: ['readability', 'best practices'],
codeSnippet: `function add(a: number, b: number) {
return a + b;
}`,
fileName: 'utils.ts',
authorName: 'Alice',
});
console.log('=== Complex Example: Code Review Assistant (Junior Reviewer) ===');
console.log(prompt1);
console.log('\n');
// Example 2: Complex code review for senior developer
const prompt2 = buildCodeReviewPrompt({
language: 'TypeScript',
framework: 'NestJS',
codeComplexity: 'complex',
reviewerLevel: 'senior',
focusAreas: ['performance', 'security', 'architecture'],
codeSnippet: `@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
private cacheService: CacheService,
) {}
async findUserById(id: string): Promise<User> {
const cached = await this.cacheService.get(\`user:\${id}\`);
if (cached) return cached;
const user = await this.userRepository.findOne({ where: { id } });
await this.cacheService.set(\`user:\${id}\`, user, 3600);
return user;
}
}`,
fileName: 'user.service.ts',
authorName: 'Bob',
});
console.log('=== Complex Example: Code Review Assistant (Senior Reviewer) ===');
console.log(prompt2);
console.log('\n');
// Example 3: Moderate complexity for mid-level developer
const prompt3 = buildCodeReviewPrompt({
language: 'Python',
codeComplexity: 'moderate',
reviewerLevel: 'mid',
focusAreas: ['error handling', 'code quality'],
codeSnippet: `def process_data(data: list) -> dict:
result = {}
for item in data:
key = item.get('id')
value = item.get('value')
result[key] = value * 2
return result`,
fileName: 'data_processor.py',
authorName: 'Charlie',
});
console.log('=== Complex Example: Code Review Assistant (Mid-Level Reviewer) ===');
console.log(prompt3);
console.log('\n');