FEATURE: Triage rule can skip posts created via email (#775)

This commit is contained in:
Roman Rizzi 2024-08-27 12:04:55 -03:00 committed by GitHub
parent 1b209f5fb1
commit 6af415f7f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 52 deletions

View File

@ -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"

View File

@ -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,

View File

@ -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

View File

@ -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