Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
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
/**
* Multi-Level Summary Generator
* Generates file, group, and PR-level summaries using analysis + LLM.
*/
import type { ReviewAnalysis } from "./pipeline";
import type { AIConfig } from "../ai-config";
import { getAIAdapter } from "../ai";
import { buildSummaryPrompt } from "./prompts";
import type { AIConfig as AdapterAIConfig } from "../ai/types";
export interface PRSummaries {
overall: {
title: string;
summary: string;
healthScore: number;
riskLevel: string;
keyChanges: string[];
testing建议: string[];
};
groups: {
groupId: string;
title: string;
summary: string;
files: string[];
risk: string;
}[];
metadata: {
totalAdditions: number;
totalDeletions: number;
filesChanged: number;
languages: string[];
estimatedReviewTime: string;
};
riskAssessment: {
level: string;
reasons: string[];
};
}
export interface ReviewPromptOptions {
prTitle: string;
prBody: string;
repoName: string;
owner: string;
branch: string;
filesChanged: number;
additions: number;
deletions: number;
}
function estimateReviewTime(additions: number, deletions: number): string {
// Rough estimate: ~200 lines per minute for experienced reviewer
const totalLines = additions + deletions;
const minutes = Math.max(1, Math.ceil(totalLines / 200));
if (minutes < 5) return "~2 min";
if (minutes < 15) return `~${minutes} min`;
if (minutes < 60) return `~${Math.round(minutes / 5) * 5} min`;
return `~${Math.round(minutes / 60 * 10) / 10} hr`;
}
export function generateFallbackSummaries(
analysis: ReviewAnalysis,
options: ReviewPromptOptions,
): PRSummaries {
return {
overall: {
title: options.prTitle,
summary: `PR changes ${options.filesChanged} file(s) with +${options.additions}/-${options.deletions} lines across ${analysis.archImpact.affectedLayers.length} layer(s).`,
healthScore: analysis.healthScore.score,
riskLevel: analysis.blastRadius.riskLevel,
keyChanges: analysis.groups.map(g => g.title),
testing建议: [],
},
groups: analysis.groups.map(g => ({
groupId: g.id,
title: g.title,
summary: `Changes ${g.files.length} file(s) in ${g.layer} layer.`,
files: g.files,
risk: g.riskLevel,
})),
metadata: {
totalAdditions: options.additions,
totalDeletions: options.deletions,
filesChanged: options.filesChanged,
languages: [...new Set(analysis.chunks.map(c => c.language.language))],
estimatedReviewTime: estimateReviewTime(options.additions, options.deletions),
},
riskAssessment: {
level: analysis.blastRadius.riskLevel,
reasons: [
...(analysis.archImpact.crossLayerChanges ? ["Cross-layer changes"] : []),
...(analysis.archImpact.securityImpact ? ["Security-related changes"] : []),
...(analysis.archImpact.databaseImpact ? ["Database changes"] : []),
...(analysis.complexity.aggregate.longFunctions.length > 0
? [`${analysis.complexity.aggregate.longFunctions.length} long function(s)`]
: []),
],
},
};
}
export async function generateAISummaries(
analysis: ReviewAnalysis,
options: ReviewPromptOptions,
aiConfig: AIConfig,
): Promise<PRSummaries> {
const prompt = buildSummaryPrompt(analysis, options);
try {
const apiKey = (aiConfig.apiKeys as any)[aiConfig.provider];
if (!apiKey) return generateFallbackSummaries(analysis, options);
const adapter = getAIAdapter(aiConfig.provider as any);
const adapterConfig: AdapterAIConfig = {
provider: aiConfig.provider as any,
model: aiConfig.model,
apiKey,
};
const result = await adapter.complete(
{ system: "You are a code review assistant. Always respond with valid JSON only, no markdown fences.", user: prompt },
adapterConfig,
);
const parsed = parseJSONResponse(result.content);
if (!parsed) return generateFallbackSummaries(analysis, options);
return {
overall: {
title: parsed.overall?.title || options.prTitle,
summary: parsed.overall?.summary || "Summary unavailable.",
healthScore: analysis.healthScore.score,
riskLevel: parsed.riskAssessment?.level || analysis.blastRadius.riskLevel,
keyChanges: parsed.overall?.keyChanges || analysis.groups.map(g => g.title),
testing建议: parsed.overall?.testing建议 || [],
},
groups: analysis.groups.map((g, i) => ({
groupId: g.id,
title: parsed.groups?.[i]?.title || g.title,
summary: parsed.groups?.[i]?.summary || `Changes in ${g.layer} layer.`,
files: g.files,
risk: g.riskLevel,
})),
metadata: {
totalAdditions: options.additions,
totalDeletions: options.deletions,
filesChanged: options.filesChanged,
languages: [...new Set(analysis.chunks.map(c => c.language.language))],
estimatedReviewTime: estimateReviewTime(options.additions, options.deletions),
},
riskAssessment: {
level: parsed.riskAssessment?.level || analysis.blastRadius.riskLevel,
reasons: parsed.riskAssessment?.reasons || [],
},
};
} catch (err) {
return generateFallbackSummaries(analysis, options);
}
}
function parseJSONResponse(text: string): any {
try {
// Try direct parse
return JSON.parse(text);
} catch {
// Try extracting JSON from markdown fences
const jsonMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/);
if (jsonMatch) {
try { return JSON.parse(jsonMatch[1]); } catch {}
}
// Try finding JSON object in text
const objMatch = text.match(/\{[\s\S]*\}/);
if (objMatch) {
try { return JSON.parse(objMatch[0]); } catch {}
}
return null;
}
}