Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow AI tagging automation to tag system-created topics (#35245)

Kris20552f0fa7c2
diff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rb
index 3fb8dd14d32..ba9630ec666 100644
--- a/plugins/automation/lib/discourse_automation/event_handlers.rb
+++ b/plugins/automation/lib/discourse_automation/event_handlers.rb
@@ -3,7 +3,8 @@
module DiscourseAutomation
module EventHandlers
def self.handle_post_created_edited(post, action)
- return if post.post_type != Post.types[:regular] || post.user_id < 0
+ return if post.post_type != Post.types[:regular]
+
topic = post.topic
return if topic.blank?
@@ -12,6 +13,8 @@ module DiscourseAutomation
DiscourseAutomation::Automation
.where(trigger: name, enabled: true)
.find_each do |automation|
+ # allow scripts to opt-in to system posts via allow_system_posts setting
+ next if post.user_id < 0 && !automation.script_field("allow_system_posts")&.dig("value")
action_type = automation.trigger_field("action_type")
selected_action = action_type["value"]&.to_sym
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 80895b1d0c6..c5f703a3915 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -215,6 +215,9 @@ en:
max_posts_for_context:
label: "Maximum Posts for Context"
description: "Maximum number of posts from the topic to include for analysis context (default: 5)"
+ allow_system_posts:
+ label: "Allow System Posts"
+ description: "Allow tagging of posts created by the system user."
discourse_ai:
title: "AI"
diff --git a/plugins/discourse-ai/discourse_automation/llm_tagger.rb b/plugins/discourse-ai/discourse_automation/llm_tagger.rb
index 6f354a5aa69..91090c1abf8 100644
--- a/plugins/discourse-ai/discourse_automation/llm_tagger.rb
+++ b/plugins/discourse-ai/discourse_automation/llm_tagger.rb
@@ -50,6 +50,8 @@ if defined?(DiscourseAutomation)
field :max_posts_for_context, component: :text, default: "5"
+ field :allow_system_posts, component: :boolean, default: false
+
script do |context, fields|
post = context["post"]
next if post&.user&.bot?
diff --git a/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb b/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
index 4ced5875421..8705baac16e 100644
--- a/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
+++ b/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
@@ -357,5 +357,31 @@ RSpec.describe DiscourseAi::Automation::LlmTagger do
expect(topic.reload.tags.map(&:name)).to include("question")
end
end
+
+ describe "with system posts" do
+ fab!(:system_topic) { Fabricate(:topic, user: Discourse.system_user) }
+ fab!(:system_post) do
+ Fabricate(:post, topic: system_topic, user: Discourse.system_user, post_number: 1)
+ end
+
+ it "can process posts from system users when automation has allow_system_posts enabled" do
+ mock_response = { "tags" => ["bug"], "confidence" => 90 }.to_json
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([mock_response]) do
+ described_class.handle(
+ post: system_post,
+ tagger_persona_id: ai_persona.id,
+ available_tags: available_tags,
+ confidence_threshold: 70,
+ max_tags: 3,
+ max_post_tokens: 4000,
+ allow_restricted_tags: false,
+ max_posts_for_context: 5,
+ )
+ end
+
+ expect(system_topic.reload.tags.map(&:name)).to include("bug")
+ end
+ end
end
end