-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview_processor.go
300 lines (250 loc) · 8.43 KB
/
review_processor.go
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"context"
"fmt"
"math"
"strconv"
"strings"
"github.com/XiaoConstantine/dspy-go/pkg/agents"
"github.com/XiaoConstantine/dspy-go/pkg/logging"
"golang.org/x/exp/maps"
)
type CodeReviewProcessor struct {
metrics MetricsCollector
}
func (p *CodeReviewProcessor) Process(ctx context.Context, task agents.Task, context map[string]interface{}) (interface{}, error) {
// Extract the handoff from metadata
handoffRaw, exists := task.Metadata["handoff"]
if !exists {
return nil, fmt.Errorf("missing review handoff in metadata")
}
handoff, ok := handoffRaw.(*ReviewHandoff)
if !ok {
return nil, fmt.Errorf("invalid handoff type: %T", handoffRaw)
}
var comments []PRReviewComment
// Convert validated issues into well-formatted comments
for _, issue := range handoff.ValidatedIssues {
// Create base comment
comment := PRReviewComment{
FilePath: issue.FilePath,
LineNumber: issue.LineRange.Start,
Category: issue.Category,
Severity: issue.Severity,
Content: formatCommentContent(issue),
Suggestion: formatSuggestion(issue),
}
// Track metrics for the comment
p.metrics.TrackReviewComment(ctx, comment, true)
comments = append(comments, comment)
}
// Return the formatted comments
return map[string]interface{}{
"comments": comments,
"metadata": map[string]interface{}{
"validation_score": calculateValidationScore(handoff),
"issue_count": len(comments),
"file_path": handoff.ChainOutput.ReviewMetadata.FilePath,
},
}, nil
}
// Helper function to format comment content with enhanced context.
func formatCommentContent(issue ValidatedIssue) string {
var content strings.Builder
// Start with the core issue description
content.WriteString(issue.Context)
// Add validation context if confidence is high
if issue.Confidence > 0.9 {
content.WriteString("\n\nThis issue was validated with high confidence based on:")
content.WriteString("\n• Context analysis")
content.WriteString("\n• Rule compliance verification")
content.WriteString("\n• Impact assessment")
}
return content.String()
}
// Helper function to format actionable suggestions.
func formatSuggestion(issue ValidatedIssue) string {
var suggestion strings.Builder
suggestion.WriteString("Recommended fix:\n")
suggestion.WriteString(issue.Suggestion)
// Add any additional context from rule compliance
if issue.ValidationDetails.RuleCompliant {
suggestion.WriteString("\n\nThis suggestion follows established patterns and best practices.")
}
return suggestion.String()
}
// // Helper functions.
func parseReviewComments(ctx context.Context, filePath string, commentsStr string, metric MetricsCollector) ([]PRReviewComment, error) {
var comments []PRReviewComment
// Parse the YAML-like format from the LLM response
sections := strings.Split(commentsStr, "\n-")
for _, section := range sections {
if strings.TrimSpace(section) == "" {
continue
}
// Extract comment fields
comment := PRReviewComment{FilePath: filePath}
// Parse each field
lines := strings.Split(section, "\n")
for _, line := range lines {
parts := strings.SplitN(strings.TrimSpace(line), ":", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
switch key {
case "line":
value = strings.TrimPrefix(value, "L") // Handle "L123" format
value = strings.Split(value, "-")[0] // Hand
if lineNum, err := strconv.Atoi(value); err == nil && lineNum > 0 {
comment.LineNumber = lineNum
}
case "severity":
comment.Severity = validateSeverity(value)
case "content":
comment.Content = value
case "suggestion":
comment.Suggestion = value
case "category":
comment.Category = validateCategory(value)
}
}
if isValidComment(comment) {
comments = append(comments, comment)
metric.TrackReviewComment(ctx, comment, true)
} else {
metric.TrackReviewComment(ctx, comment, true)
}
}
return comments, nil
}
func extractComments(ctx context.Context, result interface{}, filePath string, metric MetricsCollector) ([]PRReviewComment, error) {
logger := logging.GetLogger()
logger.Debug(ctx, "Extracting comments from result type: %T", result)
switch v := result.(type) {
case map[string]interface{}:
logger.Debug(ctx, "Processing map result with keys: %v", maps.Keys(v))
// First check if this is a direct comment result
if comments, ok := v["comments"].([]PRReviewComment); ok {
return comments, nil
}
// Check for handoff in metadata
if metadata, ok := v["metadata"].(map[string]interface{}); ok {
logger.Debug(ctx, "Found metadata with keys: %v", maps.Keys(metadata))
if handoffRaw, ok := metadata["handoff"]; ok {
logger.Debug(ctx, "Found handoff of type: %T", handoffRaw)
handoff, ok := handoffRaw.(*ReviewHandoff)
logger.Debug(ctx, "Handoff: %v", handoff)
if !ok {
return nil, fmt.Errorf("invalid handoff type: %T", handoffRaw)
}
// Convert validated issues into comments
var comments []PRReviewComment
for _, issue := range handoff.ValidatedIssues {
comment := PRReviewComment{
FilePath: issue.FilePath,
LineNumber: issue.LineRange.Start,
Content: issue.Context,
Severity: issue.Severity,
Category: issue.Category,
Suggestion: issue.Suggestion,
}
metric.TrackReviewComment(ctx, comment, true)
comments = append(comments, comment)
}
return comments, nil
}
}
// Legacy format handling for string-based comments
commentsRaw, exists := v["comments"]
if !exists {
return nil, fmt.Errorf("missing comments in result")
}
commentsStr, ok := commentsRaw.(string)
if !ok {
return nil, fmt.Errorf("invalid comments format: %T", commentsRaw)
}
return parseReviewComments(ctx, filePath, commentsStr, metric)
default:
return nil, fmt.Errorf("unexpected result type: %T", result)
}
}
func validateSeverity(severity string) string {
validSeverities := map[string]bool{
"critical": true,
"warning": true,
"suggestion": true,
}
severity = strings.ToLower(strings.TrimSpace(severity))
if validSeverities[severity] {
return severity
}
return "suggestion" // Default severity
}
func validateCategory(category string) string {
validCategories := map[string]bool{
"error-handling": true,
"code-style": true,
"performance": true,
"security": true,
"documentation": true,
"comment-response": true,
}
category = strings.ToLower(strings.TrimSpace(category))
if validCategories[category] {
return category
}
return "code-style" // Default category
}
func isValidComment(comment PRReviewComment) bool {
// A comment is considered actionable if it has:
// 1. A specific location (line number)
// 2. A clear suggestion for improvement
// 3. Non-empty content explaining the issue
if comment.LineNumber <= 0 ||
comment.Suggestion == "" ||
comment.Content == "" {
return false
}
// Check that the content provides meaningful explanation
if len(strings.TrimSpace(comment.Content)) < 10 {
return false // Too short to be meaningful
}
// Check that the suggestion is specific enough
if len(strings.TrimSpace(comment.Suggestion)) < 10 {
return false // Too short to be actionable
}
return true
}
// A helper function to calculate an overall validation score from the review handoff.
// This provides a numerical measure of how confident we are in our review findings.
func calculateValidationScore(handoff *ReviewHandoff) float64 {
if len(handoff.ValidatedIssues) == 0 {
return 0.0
}
// We'll calculate a weighted average considering multiple factors:
// - Individual issue confidence scores
// - Validation completeness
// - Number of validated issues
var totalScore float64
for _, issue := range handoff.ValidatedIssues {
// Start with the base confidence score
issueScore := issue.Confidence
// Weight the score based on validation completeness
if issue.ValidationDetails.ContextValid {
issueScore *= 0.4 // Context validation carries 40% weight
}
if issue.ValidationDetails.RuleCompliant {
issueScore *= 0.3 // Rule compliance carries 30% weight
}
if issue.ValidationDetails.IsActionable {
issueScore *= 0.3 // Actionability carries 30% weight
}
totalScore += issueScore
}
// Calculate the average score across all issues
// This gives us a final score between 0.0 and 1.0
averageScore := totalScore / float64(len(handoff.ValidatedIssues))
return math.Round(averageScore*100) / 100 // Round to 2 decimal places
}