Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc321be658 |
+18
-12
@@ -9,7 +9,8 @@ import (
|
||||
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
|
||||
)
|
||||
|
||||
const defaultChunkSize = 60000
|
||||
// DefaultChunkSize is the fallback maximum size (in bytes) for a diff chunk when no explicit value is configured.
|
||||
const DefaultChunkSize = 60000
|
||||
|
||||
type Comment struct {
|
||||
File string `json:"file"`
|
||||
@@ -26,36 +27,41 @@ func (s *Service) judgePR(ctx context.Context, diff io.Reader) (comments []Comme
|
||||
// Determine chunk size (use default if not set)
|
||||
maxSize := s.maxChunkSize
|
||||
if maxSize <= 0 {
|
||||
maxSize = defaultChunkSize // default 60KB ~ 15k tokens
|
||||
maxSize = DefaultChunkSize // default 60KB ~ 15k tokens
|
||||
}
|
||||
|
||||
chunks := splitDiffIntoChunks(diffBytes, maxSize)
|
||||
allComments := []Comment{}
|
||||
|
||||
// Build optional guidelines text
|
||||
// Build optional guidelines text (added as a separate section with a clear delimiter)
|
||||
guidelinesText := ""
|
||||
if len(s.guidelines) > 0 {
|
||||
guidelinesText = "Project guidelines:\n"
|
||||
// Two newlines ensure the guidelines start on a fresh paragraph.
|
||||
guidelinesText = "\n\nProject guidelines:\n"
|
||||
for _, g := range s.guidelines {
|
||||
guidelinesText += "- " + g + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
// System prompt that instructs the LLM precisely.
|
||||
baseSystem := strings.TrimSpace(`
|
||||
You are a very strict senior software architect.
|
||||
You review **only** newly added or modified lines in a unified diff, together with the immediate hunk context.
|
||||
You do **not** report issues that appear **solely** in deleted lines (“-") or that have already been fixed by the change.
|
||||
No comments are made on pure formatting/whitespace changes or reordering that does not alter the program’s behavior.`) + guidelinesText
|
||||
You are a strict senior software architect.
|
||||
Only comment on newly added or modified lines in the diff; ignore deletions, pure formatting, or re‑ordering that does not change behavior.
|
||||
For each issue output a JSON object with fields "file", "line", and "message" (message should be concise, ≤2 sentences, and actionable).
|
||||
If project guidelines are provided, treat them as hard rules that must be respected.`) + guidelinesText
|
||||
|
||||
for i, chunk := range chunks {
|
||||
// Add a small header so the model knows this is a fragment
|
||||
header := fmt.Sprintf("\n--- Chunk %d of %d ---\n", i+1, len(chunks))
|
||||
userContent := fmt.Sprintf("Hello please review my PR. Write comments where improvements are necessary in new lines.%s\nHere is the git diff of it: %s", header, chunk)
|
||||
// Include the chunk identifier in the system message only if there are multiple chunks.
|
||||
systemContent := baseSystem
|
||||
if len(chunks) > 1 {
|
||||
systemContent = fmt.Sprintf("%s\nChunk %d of %d.", baseSystem, i+1, len(chunks))
|
||||
}
|
||||
userContent := chunk
|
||||
|
||||
var chunkComments []Comment
|
||||
err = s.chat.GenerateStructured(ctx, []chatter.Message{{
|
||||
Role: chatter.RoleSystem,
|
||||
Content: baseSystem,
|
||||
Content: systemContent,
|
||||
}, {
|
||||
Role: chatter.RoleUser,
|
||||
Content: userContent,
|
||||
|
||||
@@ -26,15 +26,18 @@ func (s *Service) MakeReview(ctx context.Context, organisation string, repo stri
|
||||
|
||||
for _, c := range comments {
|
||||
c.Message = fmt.Sprintf("%s (Generated by: %s)", c.Message, model)
|
||||
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
|
||||
c.File, c.Line, c.Message, "---")
|
||||
|
||||
if s.disableComments {
|
||||
log.Printf("dry-run: not posting comment for %s:%d", c.File, c.Line)
|
||||
} else if err := s.git.AddComment(ctx, organisation, repo, prID, c); err != nil {
|
||||
log.Printf("Failed to add comment: %v", err)
|
||||
// Dry‑run: just log what would have been posted.
|
||||
log.Printf("dry‑run: %s:%d => %s", c.File, c.Line, c.Message)
|
||||
} else {
|
||||
// Normal mode: print to stdout and post the comment to the VCS.
|
||||
fmt.Printf("File: %s\nLine: %d\nMessage: %s\n%s\n",
|
||||
c.File, c.Line, c.Message, "---")
|
||||
if err := s.git.AddComment(ctx, organisation, repo, prID, c); err != nil {
|
||||
log.Printf("Failed to add comment: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user