open-code-review/internal/tool/code_comment.go
kite 77e73e53f8 feat(agent): add LLM-driven file grouping for multi-file co-review
Group functionally related files (e.g., impl + test, handler + model) into
a single review context using an LLM clustering step. This improves review
quality by giving the model cross-file awareness within related changes.

Key changes:
- Add file grouping agent with threshold-based decisions and recursive splitting
- Refactor dispatchSubtasks/executeSubtask to operate on DiffGroup instead of
  individual diffs, supporting combined multi-file diffs with FILE separators
- Add path validation in code_comment tool for multi-file groups
- Add StripDiffHeaders to reduce token usage in prompts
- Add extractJSONArray fallback for filter response parsing robustness
- Persist file group metadata to JSONL and render groups in viewer UI
- Merge intermediate tool-only agent rounds in viewer for cleaner display
- Update prompt templates: remove {{current_file_path}}, use <pending_review_diff>
2026-06-20 21:31:14 +08:00

86 lines
2.2 KiB
Go

package tool
import (
"context"
"encoding/json"
"fmt"
"github.com/open-code-review/open-code-review/internal/model"
)
// CodeCommentProvider submits review comments to the per-Agent CommentCollector.
type CodeCommentProvider struct {
Collector *CommentCollector
}
func (p *CodeCommentProvider) Tool() Tool { return CodeComment }
func (p *CodeCommentProvider) Execute(_ context.Context, args map[string]any) (string, error) {
if p.Collector == nil {
return "Error: comment collector is not configured", nil
}
comments, errMsg := ParseComments(args)
if errMsg != "" {
return errMsg, nil
}
for i := range comments {
p.Collector.Add(comments[i])
}
return CommentSucceed, nil
}
// ParseComments extracts LlmComment entries from tool call arguments without writing
// to the Collector. Returns parsed comments and an error message (empty on success).
func ParseComments(args map[string]any) ([]model.LlmComment, string) {
var rawComments []any
if arr, ok := args["comments"].([]any); ok && len(arr) > 0 {
rawComments = arr
} else if s, ok := args["comments"].(string); ok && s != "" {
if err := json.Unmarshal([]byte(s), &rawComments); err != nil {
return nil, fmt.Sprintf("Error: failed to parse 'comments' JSON string: %v", err)
}
}
if len(rawComments) == 0 {
raw, _ := json.Marshal(args)
return nil, fmt.Sprintf("Error: 'comments' array is required. Got args: %s", string(raw))
}
topLevelPath, _ := args["path"].(string)
var comments []model.LlmComment
for _, raw := range rawComments {
obj, ok := raw.(map[string]any)
if !ok {
continue
}
cm := model.LlmComment{}
if content, ok := obj["content"].(string); ok {
cm.Content = content
}
if suggestion, ok := obj["suggestion_code"].(string); ok {
cm.SuggestionCode = suggestion
}
if existing, ok := obj["existing_code"].(string); ok {
cm.ExistingCode = existing
}
if thinking, ok := obj["thinking"].(string); ok {
cm.Thinking = thinking
}
if perPath, ok := obj["path"].(string); ok && perPath != "" {
cm.Path = perPath
} else {
cm.Path = topLevelPath
}
if cm.Path == "" || cm.Content == "" {
continue
}
comments = append(comments, cm)
}
return comments, ""
}