Class: RailsAiContext::Tools::ReviewChanges

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_context/tools/review_changes.rb

Constant Summary collapse

MAX_DIFF_LINES_PER_FILE =
30

Constants inherited from BaseTool

BaseTool::SESSION_CONTEXT, BaseTool::SHARED_CACHE

Class Method Summary collapse

Methods inherited from BaseTool

abstract!, abstract?, cache_key, cached_context, config, extract_method_source_from_file, extract_method_source_from_string, find_closest_match, fuzzy_find_key, inherited, not_found_response, paginate, rails_app, registered_tools, reset_all_caches!, reset_cache!, session_queries, session_record, session_reset!, set_call_params, text_response

Class Method Details

.call(ref: "HEAD", files: nil, server_context: nil) ⇒ Object



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
# File 'lib/rails_ai_context/tools/review_changes.rb', line 32

def self.call(ref: "HEAD", files: nil, server_context: nil)
  root = Rails.root.to_s

  # Verify git is available
  _, status = Open3.capture2("git", "rev-parse", "--git-dir", chdir: root)
  unless status.success?
    return text_response("Not a git repository. `rails_review_changes` requires a git repository.\n\n**To initialize:** `git init && git add -A && git commit -m 'Initial commit'`")
  end

  changed = get_changed_files(ref, root)
  changed = changed.select { |f| files.any? { |filter| f.include?(filter) } } if files&.any?

  if changed.empty?
    return text_response("No changes found for ref '#{ref}'.#{files ? " Filter: #{files.join(', ')}" : ""}")
  end

  # Classify files
  classified = changed.map { |f| { file: f, type: classify_file(f) } }

  # Get commit log
  commits = get_commit_log(ref, root)

  # Build output
  lines = [ "# Review: #{ref}", "" ]

  # Summary
  type_counts = classified.group_by { |c| c[:type] }.transform_values(&:size)
  summary_parts = type_counts.map { |type, count| "#{count} #{type}" }
  lines << "**#{changed.size} files changed** (#{summary_parts.join(', ')})"
  lines << ""

  if commits
    lines << "## Commits"
    lines << "```"
    lines << commits
    lines << "```"
    lines << ""
  end

  # Detect warnings
  warnings = detect_warnings(classified, root, ref)
  if warnings.any?
    lines << "## Warnings"
    warnings.each { |w| lines << "- #{w}" }
    lines << ""
  end

  # File-by-file context — cap at 20 files to prevent overflow
  max_files = 20
  show_files = classified.first(max_files)
  lines << "## File-by-File Context (#{show_files.size} of #{classified.size})"
  lines << ""

  show_files.each do |entry|
    file_lines = gather_file_context(entry[:file], entry[:type], root, ref)
    lines.concat(file_lines)
  end

  if classified.size > max_files
    remaining = classified[max_files..].map { |e| e[:file] }
    lines << "## Remaining #{remaining.size} files (not shown)"
    remaining.each { |f| lines << "- #{f}" }
    lines << ""
  end

  # Next steps
  rb_files = classified.select { |c| c[:file].end_with?(".rb") }.map { |c| c[:file] }
  if rb_files.any?
    file_list = rb_files.first(10).map { |f| "\"#{f}\"" }.join(", ")
    lines << "_Next: `rails_validate(files:[#{file_list}], level:\"rails\")` to validate all changes._"
  end

  text_response(lines.join("\n"))
rescue => e
  text_response("Review error: #{e.message}")
end