Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ en:
hide_topic:
label: "Hide topic"
description: "Make topic non visible to the public if triggered"
flag_type:
label: "Flag type"
description: "Type of flag to apply to the post (spam or simply raise for review)"
flag_post:
label: "Send to review"
description: "Puts the post into the review queue if triggered, for moderators to triage"
label: "Flag post"
description: "Flags post (either as spam or for review)"
model:
label: "Model"
description: "Language model used for triage"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
en:
discourse_automation:
ai:
flag_types:
review: "Add post to review queue"
spam: "Flag as spam and hide post"
scriptables:
llm_triage:
title: Triage posts using AI
Expand Down
9 changes: 9 additions & 0 deletions discourse_automation/llm_triage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
field :tags, component: :tags
field :hide_topic, component: :boolean
field :flag_post, component: :boolean
field :flag_type,
component: :choices,
required: false,
extra: {
content: DiscourseAi::Automation.flag_types,
},
default: "review"
field :canned_reply, component: :message
field :canned_reply_user, component: :user

Expand All @@ -41,6 +48,7 @@
tags = fields.dig("tags", "value")
hide_topic = fields.dig("hide_topic", "value")
flag_post = fields.dig("flag_post", "value")
flag_type = fields.dig("flag_type", "value")

begin
RateLimiter.new(
Expand Down Expand Up @@ -68,6 +76,7 @@
canned_reply_user: canned_reply_user,
hide_topic: hide_topic,
flag_post: flag_post,
flag_type: flag_type.to_s.to_sym,
automation: self.automation,
)
rescue => e
Expand Down
6 changes: 6 additions & 0 deletions lib/automation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module DiscourseAi
module Automation
def self.flag_types
[
{ id: "review", translated_name: I18n.t("discourse_automation.ai.flag_types.review") },
{ id: "spam", translated_name: I18n.t("discourse_automation.ai.flag_types.spam") },
]
end
def self.available_models
values = DB.query_hash(<<~SQL)
SELECT display_name AS translated_name, id AS id
Expand Down
29 changes: 20 additions & 9 deletions lib/automation/llm_triage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.handle(
canned_reply_user: nil,
hide_topic: nil,
flag_post: nil,
flag_type: nil,
automation: nil
)
if category_id.blank? && tags.blank? && canned_reply.blank? && hide_topic.blank? &&
Expand Down Expand Up @@ -65,22 +66,32 @@ def self.handle(
post.topic.update!(visible: false) if hide_topic

if flag_post
reviewable =
ReviewablePost.needs_review!(target: post, created_by: Discourse.system_user)

score_reason =
I18n
.t("discourse_automation.scriptables.llm_triage.flagged_post")
.sub("%%LLM_RESPONSE%%", result)
.sub("%%AUTOMATION_ID%%", automation&.id.to_s)
.sub("%%AUTOMATION_NAME%%", automation&.name.to_s)

reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:needs_approval],
reason: score_reason,
force_review: true,
)
if flag_type == :spam
PostActionCreator.new(
Discourse.system_user,
post,
PostActionType.types[:spam],
message: score_reason,
queue_for_review: true,
).perform
else
reviewable =
ReviewablePost.needs_review!(target: post, created_by: Discourse.system_user)

reviewable.add_score(
Discourse.system_user,
ReviewableScore.types[:needs_approval],
reason: score_reason,
force_review: true,
)
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/modules/automation/llm_triage_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
describe DiscourseAi::Automation::LlmTriage do
fab!(:post)
fab!(:reply) { Fabricate(:post, topic: post.topic, user: Fabricate(:user)) }
fab!(:llm_model)

def triage(**args)
Expand Down Expand Up @@ -92,6 +93,23 @@ def triage(**args)
expect(reviewable.reviewable_scores.first.reason).to include("bad")
end

it "can handle spam flags" 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,
flag_type: :spam,
automation: nil,
)
end

expect(post.reload).to be_hidden
expect(post.topic.reload.visible).to eq(false)
end

it "can handle garbled output from LLM" do
DiscourseAi::Completions::Llm.with_prepared_responses(["Bad.\n\nYo"]) do
triage(
Expand Down