Class: Collavre::AiAgent::ReviewHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/ai_agent/review_handler.rb

Overview

Handles AI agent review workflows: eligibility checks, in-place content updates, and completion reactions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_comment, agent) ⇒ ReviewHandler

Returns a new instance of ReviewHandler.



8
9
10
11
# File 'app/services/collavre/ai_agent/review_handler.rb', line 8

def initialize(original_comment, agent)
  @original_comment = original_comment
  @agent = agent
end

Class Method Details

.eligible?(original_comment, agent) ⇒ Boolean

Class-level convenience for eligibility checks.

Returns:

  • (Boolean)


14
15
16
# File 'app/services/collavre/ai_agent/review_handler.rb', line 14

def self.eligible?(original_comment, agent)
  new(original_comment, agent).eligible?
end

Instance Method Details

#add_completion_reactionObject

Add a completion reaction emoji to the review comment.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/services/collavre/ai_agent/review_handler.rb', line 70

def add_completion_reaction
  reaction = begin
    CommentReaction.find_or_create_by!(
      comment: @original_comment,
      user: @agent,
      emoji: ""
    )
  rescue ActiveRecord::RecordNotUnique
    CommentReaction.find_by(comment: @original_comment, user: @agent, emoji: "")
  end

  CommentReaction.broadcast_reaction_update(@original_comment) if reaction
rescue StandardError => e
  Rails.logger.warn("[AiAgent::ReviewHandler] Failed to add review reaction: #{e.message}")
end

#eligible?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/collavre/ai_agent/review_handler.rb', line 18

def eligible?
  return false unless @original_comment&.review_message?

  quoted_comment = @original_comment.quoted_comment
  return false unless quoted_comment
  return false unless quoted_comment.user_id == @agent.id
  return false unless quoted_comment.creative_id == @original_comment.creative_id
  return false if quoted_comment.private?
  return false unless quoted_comment.topic_id == @original_comment.topic_id

  true
end

#handle(response_content, task:) ⇒ Object

Update the quoted comment with the AI response content. Returns true if the review was handled, false otherwise.



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
# File 'app/services/collavre/ai_agent/review_handler.rb', line 33

def handle(response_content, task:)
  return false unless eligible?

  quoted_comment = @original_comment.quoted_comment

  # Save old content as a version if this is the first review (no versions yet)
  if quoted_comment.comment_versions.empty?
    quoted_comment.comment_versions.create!(
      content: quoted_comment.content,
      version_number: quoted_comment.next_version_number
    )
  end

  # Save new content as the latest version
  new_version = quoted_comment.comment_versions.create!(
    content: response_content,
    version_number: quoted_comment.next_version_number,
    review_comment: @original_comment
  )

  # Update content and point to the new version
  quoted_comment.update!(content: response_content, selected_version_id: new_version.id)

  task.task_actions.create!(
    action_type: "review_updated",
    payload: {
      quoted_comment_id: quoted_comment.id,
      original_comment_id: @original_comment.id,
      content: response_content
    },
    status: "done"
  )

  true
end