A platform for community discussion. Free, open, simple.
FEATURE: Add option to flag + delete for llm triage (#34590)
Sam062f13ab1c5e
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 4f62716a98f..d393476b9b8 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -165,6 +165,15 @@ en:flag_post:label: "Flag post"description: "Flags post (either as spam or for review)"+ notify_author_pm:+ label: "Notify author via PM"+ description: "Send a personal message to the post author when their post is queued and deleted"+ notify_author_pm_user:+ label: "PM sender"+ description: "User who will send the PM (defaults to system)"+ notify_author_pm_message:+ label: "PM message"+ description: "Optional custom message to send to the author"include_personal_messages:label: "Include personal messages"description: "Also scan and triage personal messages"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 94f178d31c8..fa85bc95d16 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -4,6 +4,7 @@ en:flag_types:review: "Add post to review queue"review_hide: "Add post to review queue and hide post"+ review_delete: "Add post to review queue and delete post"spam: "Flag as spam and hide post"spam_silence: "Flag as spam, hide post and silence user"scriptables:@@ -20,6 +21,14 @@ en:<div>Response from the model:</div><p>%%LLM_RESPONSE%%</p><b>Triggered by the <a href="%{base_path}/admin/plugins/discourse-automation/%%AUTOMATION_ID%%">%%AUTOMATION_NAME%%</a> rule.</b>+ notify_author_pm:+ subject: "Your post was temporarily removed for review"+ body: |+ Hi @%{username},++ Your post in “%{topic_title}” was temporarily removed and placed in our review queue for a moderator to look at. If approved, it will be restored automatically.++ Reference: %{post_url}llm_report:title: Periodic report using AIdescription: "Periodic report based on a large language model"diff --git a/plugins/discourse-ai/discourse_automation/llm_triage.rb b/plugins/discourse-ai/discourse_automation/llm_triage.rbindex 253021ff88f..aec62697068 100644--- a/plugins/discourse-ai/discourse_automation/llm_triage.rb+++ b/plugins/discourse-ai/discourse_automation/llm_triage.rb@@ -52,6 +52,9 @@ if defined?(DiscourseAutomation)),}field :whisper, component: :boolean+ field :notify_author_pm, component: :boolean+ field :notify_author_pm_user, component: :user+ field :notify_author_pm_message, component: :messagescript do |context, fields|post = context["post"]@@ -68,6 +71,9 @@ if defined?(DiscourseAutomation)canned_reply_user = fields.dig("canned_reply_user", "value")reply_persona_id = fields.dig("reply_persona", "value")whisper = fields.dig("whisper", "value")+ notify_author_pm = fields.dig("notify_author_pm", "value")+ notify_author_pm_user = fields.dig("notify_author_pm_user", "value")+ notify_author_pm_message = fields.dig("notify_author_pm_message", "value")# nothing to do if we already repliednext if post.user.username == canned_reply_user@@ -122,6 +128,9 @@ if defined?(DiscourseAutomation)automation: self.automation,max_output_tokens: max_output_tokens,action: context["action"],+ notify_author_pm: notify_author_pm,+ notify_author_pm_user: notify_author_pm_user,+ notify_author_pm_message: notify_author_pm_message,)rescue => eDiscourse.warn_exception(diff --git a/plugins/discourse-ai/lib/automation.rb b/plugins/discourse-ai/lib/automation.rbindex bd23b3bf75e..9c57e93b719 100644--- a/plugins/discourse-ai/lib/automation.rb+++ b/plugins/discourse-ai/lib/automation.rb@@ -9,6 +9,10 @@ module DiscourseAiid: "review_hide",translated_name: I18n.t("discourse_automation.ai.flag_types.review_hide"),},+ {+ id: "review_delete",+ translated_name: I18n.t("discourse_automation.ai.flag_types.review_delete"),+ },{ id: "spam", translated_name: I18n.t("discourse_automation.ai.flag_types.spam") },{id: "spam_silence",diff --git a/plugins/discourse-ai/lib/automation/llm_triage.rb b/plugins/discourse-ai/lib/automation/llm_triage.rbindex 934b45996f3..a693325ac03 100644--- a/plugins/discourse-ai/lib/automation/llm_triage.rb+++ b/plugins/discourse-ai/lib/automation/llm_triage.rb@@ -20,7 +20,10 @@ module DiscourseAiwhisper: nil,reply_persona_id: nil,max_output_tokens: nil,- action: nil+ action: nil,+ notify_author_pm: nil,+ notify_author_pm_user: nil,+ notify_author_pm_message: nil)if category_id.blank? && tags.blank? && canned_reply.blank? && hide_topic.blank? &&flag_post.blank? && reply_persona_id.blank?@@ -170,7 +173,53 @@ module DiscourseAi# We cannot do this through the PostActionCreator because hiding a post is reserved for auto action flags.# Those flags are off_topic, inappropiate, and spam. We want a more generic type for triage, so none of those# fit here.- post.hide!(PostActionType.types[:notify_moderators]) if flag_type == :review_hide+ if flag_type == :review_hide+ post.hide!(PostActionType.types[:notify_moderators])+ elsif flag_type == :review_delete+ # Soft-delete the post so it is hidden from users until a moderator handles it in review.+ PostDestroyer.new(Discourse.system_user, post, context: "llm_triage").destroy+ end++ if notify_author_pm && action != :edit+ begin+ pm_sender =+ if notify_author_pm_user.present?+ User.find_by_username(notify_author_pm_user)+ else+ nil+ end+ pm_sender ||= Discourse.system_user++ subject =+ I18n.t("discourse_automation.scriptables.llm_triage.notify_author_pm.subject")++ default_body =+ I18n.t(+ "discourse_automation.scriptables.llm_triage.notify_author_pm.body",+ username: post.user.username,+ topic_title: post.topic.title,+ post_url: post.url,+ )++ body = notify_author_pm_message.presence || default_body++ PostCreator.create!(+ pm_sender,+ title: subject,+ raw: body,+ archetype: Archetype.private_message,+ target_usernames: post.user.username,+ skip_validations: true,+ )+ rescue StandardError => e+ Discourse.warn_exception(+ e,+ message:+ "Error sending PM notification for triage on: #{post&.url} in LlmTriage.handle",+ )+ raise e if Rails.env.test?+ end+ endendendenddiff --git a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rbindex c51251b2b2c..0d2658c5419 100644--- a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb@@ -91,7 +91,8 @@ describe DiscourseAi::Automation::LlmTriage doreviewable = ReviewablePost.last- expect(reviewable.target).to eq(post)+ expect(reviewable.target_id).to eq(post.id)+ expect(reviewable.target_type).to eq("Post")expect(reviewable.reviewable_scores.first.reason).to include("bad")end@@ -142,11 +143,102 @@ describe DiscourseAi::Automation::LlmTriage doreviewable = ReviewablePost.last- expect(reviewable.target).to eq(post)+ expect(reviewable.target_id).to eq(post.id)+ expect(reviewable.target_type).to eq("Post")expect(reviewable.reviewable_scores.first.reason).to include("bad")expect(post.reload).to be_hiddenend+ it "can handle flag + delete" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do+ triage(+ post: post,+ triage_persona_id: ai_persona.id,+ search_for_text: "bad",+ flag_post: true,+ flag_type: :review_delete,+ automation: nil,+ )+ end++ reviewable = ReviewablePost.last++ expect(reviewable.target_id).to eq(post.id)+ expect(reviewable.target_type).to eq("Post")+ expect(reviewable.reviewable_scores.first.reason).to include("bad")+ expect(post.reload.trashed?).to eq(true)+ end++ it "restores deleted post when moderator approves" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do+ triage(+ post: post,+ triage_persona_id: ai_persona.id,+ search_for_text: "bad",+ flag_post: true,+ flag_type: :review_delete,+ automation: nil,+ )+ end++ reviewable = ReviewablePost.last+ expect(post.reload.trashed?).to eq(true)+ topic = Topic.with_deleted.find_by(id: post.topic_id)+ expect(topic.trashed?).to eq(true)++ moderator = Fabricate(:moderator)+ result = reviewable.perform(moderator, :approve_and_restore)+ expect(result).to be_success++ # Post and topic should be restored+ expect(post.reload.trashed?).to eq(false)+ expect(post.topic.reload.trashed?).to eq(false)+ end++ it "sends author a PM when notify_author_pm is enabled" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do+ triage(+ post: post,+ triage_persona_id: ai_persona.id,+ search_for_text: "bad",+ flag_post: true,+ flag_type: :review_delete,+ automation: nil,+ notify_author_pm: true,+ )+ end++ pm_topic = Topic.where(archetype: Archetype.private_message).order(:id).last+ expect(pm_topic).to be_present+ expect(pm_topic.allowed_users).to include(post.user)+ end++ it "uses custom PM message when provided" do+ custom_message = "Your post is pending review."+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do+ triage(+ post: post,+ triage_persona_id: ai_persona.id,+ search_for_text: "bad",+ flag_post: true,+ flag_type: :review_delete,+ automation: nil,+ notify_author_pm: true,+ notify_author_pm_message: custom_message,+ )+ end++ pm_post =+ Post+ .where(+ "posts.topic_id IN (?)",+ Topic.where(archetype: Archetype.private_message).select(:id),+ )+ .order(:id)+ .last+ expect(pm_post.raw).to include(custom_message)+ end+it "does not silence the user if the flag fails" doFabricate(:post_action,