From 6af415f7f079b104fec6d99567b6d141c43d1bfd Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 27 Aug 2024 12:04:55 -0300 Subject: [PATCH] FEATURE: Triage rule can skip posts created via email (#775) --- config/locales/client.en.yml | 3 + discourse_automation/llm_triage.rb | 4 ++ .../discourse_automation/llm_triage_spec.rb | 34 ++++++++++ .../lib/modules/automation/llm_triage_spec.rb | 62 +++---------------- 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index faa9c1ea..b5d65417 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -99,6 +99,9 @@ en: model: label: "Model" description: "Language model used for triage" + skip_via_email: + label: "Skip via email" + description: "Don't triage posts created via email" discourse_ai: title: "AI" diff --git a/discourse_automation/llm_triage.rb b/discourse_automation/llm_triage.rb index b8b52fb4..611b62b4 100644 --- a/discourse_automation/llm_triage.rb +++ b/discourse_automation/llm_triage.rb @@ -19,6 +19,7 @@ if defined?(DiscourseAutomation) } field :category, component: :category field :tags, component: :tags + field :skip_via_email, component: :boolean field :hide_topic, component: :boolean field :flag_post, component: :boolean field :canned_reply, component: :message @@ -42,6 +43,9 @@ if defined?(DiscourseAutomation) hide_topic = fields.dig("hide_topic", "value") flag_post = fields.dig("flag_post", "value") + skip_via_email = fields.dig("skip_via_email", "value") + next if skip_via_email && post.via_email? + begin RateLimiter.new( Discourse.system_user, diff --git a/spec/lib/discourse_automation/llm_triage_spec.rb b/spec/lib/discourse_automation/llm_triage_spec.rb index 0a896592..ffe9521a 100644 --- a/spec/lib/discourse_automation/llm_triage_spec.rb +++ b/spec/lib/discourse_automation/llm_triage_spec.rb @@ -62,4 +62,38 @@ describe DiscourseAi::Automation::LlmTriage do last_post = post.topic.reload.posts.order(:post_number).last expect(last_post.raw).to eq post.raw end + + describe "posts created via email" do + fab!(:email_post) { Fabricate(:post_via_email) } + + context "when skip_via_email is enabled" do + before { add_automation_field("skip_via_email", true, type: "boolean") } + + it "does nothing" do + DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do + automation.running_in_background! + automation.trigger!({ "post" => email_post }) + end + + reviewable = ReviewablePost.find_by(target: email_post) + + expect(reviewable).to be_nil + end + end + + context "when skip_via_email is disabled" do + before { add_automation_field("skip_via_email", false, type: "boolean") } + + it "flags the post" do + DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do + automation.running_in_background! + automation.trigger!({ "post" => email_post }) + end + + reviewable = ReviewablePost.find_by(target: email_post) + + expect(reviewable).to be_present + end + end + end end diff --git a/spec/lib/modules/automation/llm_triage_spec.rb b/spec/lib/modules/automation/llm_triage_spec.rb index 36bc5f32..25d44786 100644 --- a/spec/lib/modules/automation/llm_triage_spec.rb +++ b/spec/lib/modules/automation/llm_triage_spec.rb @@ -4,19 +4,15 @@ describe DiscourseAi::Automation::LlmTriage do fab!(:llm_model) def triage(**args) - DiscourseAi::Automation::LlmTriage.handle(**args) + rule_args = { model: "custom:#{llm_model.id}", automation: nil, system_prompt: "test" }.merge( + args, + ) + DiscourseAi::Automation::LlmTriage.handle(**rule_args) end it "does nothing if it does not pass triage" do DiscourseAi::Completions::Llm.with_prepared_responses(["good"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - hide_topic: true, - system_prompt: "test %%POST%%", - search_for_text: "bad", - automation: nil, - ) + triage(post: post, hide_topic: true, search_for_text: "bad") end expect(post.topic.reload.visible).to eq(true) @@ -24,14 +20,7 @@ describe DiscourseAi::Automation::LlmTriage do it "can hide topics on triage" do DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - hide_topic: true, - system_prompt: "test %%POST%%", - search_for_text: "bad", - automation: nil, - ) + triage(post: post, hide_topic: true, search_for_text: "bad") end expect(post.topic.reload.visible).to eq(false) @@ -41,14 +30,7 @@ describe DiscourseAi::Automation::LlmTriage do category = Fabricate(:category) DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - category_id: category.id, - system_prompt: "test %%POST%%", - search_for_text: "bad", - automation: nil, - ) + triage(post: post, category_id: category.id, search_for_text: "bad") end expect(post.topic.reload.category_id).to eq(category.id) @@ -59,12 +41,9 @@ describe DiscourseAi::Automation::LlmTriage do DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do triage( post: post, - model: "custom:#{llm_model.id}", - system_prompt: "test %%POST%%", search_for_text: "bad", canned_reply: "test canned reply 123", canned_reply_user: user.username, - automation: nil, ) end @@ -76,14 +55,7 @@ describe DiscourseAi::Automation::LlmTriage do it "can add posts to the review queue" do DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - system_prompt: "test %%POST%%", - search_for_text: "bad", - flag_post: true, - automation: nil, - ) + triage(post: post, search_for_text: "bad", flag_post: true) end reviewable = ReviewablePost.last @@ -94,14 +66,7 @@ describe DiscourseAi::Automation::LlmTriage do it "can handle garbled output from LLM" do DiscourseAi::Completions::Llm.with_prepared_responses(["Bad.\n\nYo"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - system_prompt: "test %%POST%%", - search_for_text: "bad", - flag_post: true, - automation: nil, - ) + triage(post: post, search_for_text: "bad", flag_post: true) end reviewable = ReviewablePost.last @@ -111,14 +76,7 @@ describe DiscourseAi::Automation::LlmTriage do it "treats search_for_text as case-insensitive" do DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do - triage( - post: post, - model: "custom:#{llm_model.id}", - system_prompt: "test %%POST%%", - search_for_text: "BAD", - flag_post: true, - automation: nil, - ) + triage(post: post, search_for_text: "BAD", flag_post: true) end reviewable = ReviewablePost.last