discourse/plugins/discourse-ai/lib/agents/tools/add_reviewable_note.rb
Rafael dos Santos Silva b5ba10eb5f
FEATURE: Allow AI agents to add notes to reviewables (#40807)
Adds a new `add_reviewable_note` agent tool so agents can record a note
(their judgment/assessment) on a review queue item without changing its
  status. This is intended for external automation that lists pending
  reviewables and leaves a note on each.

  Also extends `list_reviewables` to support this workflow:

  - Each returned item now includes its existing `notes`.
- Adds a `has_notes` filter so automation can list only items that have
    not been noted on yet (or, conversely, only those that have).
2026-06-11 17:57:01 -03:00

87 lines
2.5 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Agents
module Tools
class AddReviewableNote < Tool
def self.signature
{
name: name,
description:
"Adds a note to a review queue item without changing its status. Use this to record an assessment or judgment about a reviewable so human moderators can see it. Use list_reviewables first to find items and their existing notes.",
parameters: [
{
name: "reviewable_id",
description: "The ID of the reviewable item to add a note to",
type: "integer",
required: true,
},
{
name: "note",
description: "The note content to add to the reviewable item",
type: "string",
required: true,
},
],
}
end
def self.name
"add_reviewable_note"
end
def self.requires_approval?
false
end
def invoke
if !guardian.can_see_review_queue?
return(
error_response(I18n.t("discourse_ai.ai_bot.add_reviewable_note.errors.not_allowed"))
)
end
if note_content.blank?
return(error_response(I18n.t("discourse_ai.ai_bot.add_reviewable_note.errors.no_note")))
end
reviewable = Reviewable.viewable_by(guardian.user).find_by(id: parameters[:reviewable_id])
if !reviewable
return(
error_response(I18n.t("discourse_ai.ai_bot.add_reviewable_note.errors.not_found"))
)
end
note = reviewable.reviewable_notes.build(content: note_content, user: acting_user)
if note.save
{
status: "success",
message:
I18n.t(
"discourse_ai.ai_bot.add_reviewable_note.success",
reviewable_id: reviewable.id,
),
note_id: note.id,
}
else
error_response(
note.errors.full_messages.join(", ").presence ||
I18n.t("discourse_ai.ai_bot.add_reviewable_note.errors.failed"),
)
end
end
def description_args
{ reviewable_id: parameters[:reviewable_id] }
end
private
def note_content
parameters[:note].to_s.strip
end
end
end
end
end