Skip to content

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

FEATURE: Add stalled_topic trigger to AI automation scripts (#37728)

Rafael dos Santos Silva882bdf82601a
diff --git a/plugins/discourse-ai/discourse_automation/ai_tool_action.rb b/plugins/discourse-ai/discourse_automation/ai_tool_action.rb
index 7f2da14e341..6eea1d66693 100644
--- a/plugins/discourse-ai/discourse_automation/ai_tool_action.rb
+++ b/plugins/discourse-ai/discourse_automation/ai_tool_action.rb
@@ -5,7 +5,7 @@ if defined?(DiscourseAutomation)
version 1
run_in_background
- triggerables %i[post_created_edited]
+ triggerables %i[post_created_edited stalled_topic]
# Select which AI tool to run
field :tool,
@@ -24,6 +24,7 @@ if defined?(DiscourseAutomation)
script do |context, fields, automation|
post = context["post"]
+ post ||= context["topic"]&.posts&.find_by(post_number: 1)
next if post.blank? || post.user&.bot?
begin
diff --git a/plugins/discourse-ai/discourse_automation/llm_persona_triage.rb b/plugins/discourse-ai/discourse_automation/llm_persona_triage.rb
index 684803e1c6c..e59c2b623c8 100644
--- a/plugins/discourse-ai/discourse_automation/llm_persona_triage.rb
+++ b/plugins/discourse-ai/discourse_automation/llm_persona_triage.rb
@@ -5,7 +5,7 @@ if defined?(DiscourseAutomation)
version 1
run_in_background
- triggerables %i[post_created_edited]
+ triggerables %i[post_created_edited stalled_topic]
field :persona,
component: :choices,
@@ -18,7 +18,9 @@ if defined?(DiscourseAutomation)
script do |context, fields|
post = context["post"]
- next if post&.user&.bot?
+ post ||= context["topic"]&.posts&.find_by(post_number: 1)
+ next if post.blank?
+ next if post.user&.bot?
persona_id = fields.dig("persona", "value")
whisper = !!fields.dig("whisper", "value")
diff --git a/plugins/discourse-ai/discourse_automation/llm_tagger.rb b/plugins/discourse-ai/discourse_automation/llm_tagger.rb
index a863217001f..4e7e8577037 100644
--- a/plugins/discourse-ai/discourse_automation/llm_tagger.rb
+++ b/plugins/discourse-ai/discourse_automation/llm_tagger.rb
@@ -7,7 +7,7 @@ if defined?(DiscourseAutomation)
placeholder :post
- triggerables %i[post_created_edited]
+ triggerables %i[post_created_edited stalled_topic]
field :tagger_persona,
component: :choices,
@@ -54,7 +54,9 @@ if defined?(DiscourseAutomation)
script do |context, fields|
post = context["post"]
- next if post&.user&.bot? && !fields.dig("allow_system_posts", "value")
+ post ||= context["topic"]&.posts&.find_by(post_number: 1)
+ next if post.blank?
+ next if post.user&.bot? && !fields.dig("allow_system_posts", "value")
next if post.post_number != 1
diff --git a/plugins/discourse-ai/discourse_automation/llm_triage.rb b/plugins/discourse-ai/discourse_automation/llm_triage.rb
index feb41df4421..a03c21a5024 100644
--- a/plugins/discourse-ai/discourse_automation/llm_triage.rb
+++ b/plugins/discourse-ai/discourse_automation/llm_triage.rb
@@ -7,7 +7,7 @@ if defined?(DiscourseAutomation)
placeholder :post
- triggerables %i[post_created_edited]
+ triggerables %i[post_created_edited stalled_topic]
# TODO move to triggerables
field :include_personal_messages, component: :boolean
@@ -58,7 +58,9 @@ if defined?(DiscourseAutomation)
script do |context, fields|
post = context["post"]
- next if post&.user&.bot?
+ post ||= context["topic"]&.posts&.find_by(post_number: 1)
+ next if post.blank?
+ next if post.user&.bot?
if post.topic.private_message?
include_personal_messages = fields.dig("include_personal_messages", "value")
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 8705baac16e..2b027fdfdcd 100644
--- a/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
+++ b/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
@@ -17,6 +17,46 @@ RSpec.describe DiscourseAi::Automation::LlmTagger do
Fabricate(:tag, name: "question")
end
+ describe "stalled_topic trigger" do
+ let(:automation) { Fabricate(:automation, script: "llm_tagger", enabled: true) }
+
+ before do
+ automation.fields.create!(
+ component: "choices",
+ name: "tagger_persona",
+ metadata: {
+ value: ai_persona.id,
+ },
+ target: "script",
+ )
+ automation.fields.create!(
+ component: "tags",
+ name: "available_tags",
+ metadata: {
+ value: %w[bug feature question],
+ },
+ target: "script",
+ )
+ end
+
+ it "resolves post from topic context and applies tags" do
+ mock_response = { "tags" => ["bug"], "confidence" => 90 }.to_json
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([mock_response]) do
+ automation.running_in_background!
+ automation.trigger!(
+ "kind" => DiscourseAutomation::Triggers::STALLED_TOPIC,
+ "topic" => topic,
+ "placeholders" => {
+ "topic_url" => topic.url,
+ },
+ )
+ end
+
+ expect(topic.reload.tags.map(&:name)).to include("bug")
+ end
+ end
+
describe ".handle" do
let(:available_tags) { %w[bug feature question] }
diff --git a/plugins/discourse-ai/spec/lib/discourse_automation/ai_tool_action_spec.rb b/plugins/discourse-ai/spec/lib/discourse_automation/ai_tool_action_spec.rb
index 323a9440bca..a740ae2a8eb 100644
--- a/plugins/discourse-ai/spec/lib/discourse_automation/ai_tool_action_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_automation/ai_tool_action_spec.rb
@@ -51,6 +51,34 @@ RSpec.describe DiscourseAi::Automation::AiToolAction do
expect(result).to be_nil
end
+ context "with stalled_topic trigger" do
+ let(:automation) { Fabricate(:automation, script: "ai_tool_action", enabled: true) }
+
+ before do
+ enable_current_plugin
+
+ automation.fields.create!(
+ component: "choices",
+ name: "tool",
+ metadata: {
+ value: context_tool.id,
+ },
+ target: "script",
+ )
+ end
+
+ it "resolves post from topic context" do
+ automation.running_in_background!
+ automation.trigger!(
+ "kind" => DiscourseAutomation::Triggers::STALLED_TOPIC,
+ "topic" => topic,
+ "placeholders" => {
+ "topic_url" => topic.url,
+ },
+ )
+ end
+ end
+
context "with LLM configured" do
fab!(:llm_tool) do
AiTool.create!(
diff --git a/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb b/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
index 7bbd78950cd..84bb6b7942c 100644
--- a/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_automation/llm_persona_triage_spec.rb
@@ -97,6 +97,27 @@ describe DiscourseAi::Automation::LlmPersonaTriage do
expect(last_post.post_type).to eq(Post.types[:regular]) # Not a whisper
end
+ it "can trigger via stalled_topic with topic context" do
+ post = Fabricate(:post, raw: "This is a stalled topic that needs triage")
+ response_text = "I noticed this topic has been stalled."
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([response_text]) do
+ automation.running_in_background!
+ automation.trigger!(
+ "kind" => DiscourseAutomation::Triggers::STALLED_TOPIC,
+ "topic" => post.topic,
+ "placeholders" => {
+ "topic_url" => post.topic.url,
+ },
+ )
+ end
+
+ topic = post.topic.reload
+ last_post = topic.posts.order(:post_number).last
+ expect(last_post.user_id).to eq(bot_user.id)
+ expect(last_post.raw).to eq(response_text)
+ end
+
it "hides thinking output when the persona is configured to do so" do
post = Fabricate(:post, raw: "This is a test post that needs triage")
diff --git a/plugins/discourse-ai/spec/lib/discourse_automation/llm_triage_spec.rb b/plugins/discourse-ai/spec/lib/discourse_automation/llm_triage_spec.rb
index aa2f9b260a1..d2506cff7f8 100644
--- a/plugins/discourse-ai/spec/lib/discourse_automation/llm_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_automation/llm_triage_spec.rb
@@ -82,6 +82,25 @@ describe DiscourseAi::Automation::LlmTriage do
expect(count).to be > (50)
end
+ it "can trigger via stalled_topic with topic context" do
+ post = Fabricate(:post)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do
+ automation.running_in_background!
+ automation.trigger!(
+ "kind" => DiscourseAutomation::Triggers::STALLED_TOPIC,
+ "topic" => post.topic,
+ "placeholders" => {
+ "topic_url" => post.topic.url,
+ },
+ )
+ end
+
+ topic = post.topic.reload
+ expect(topic.category_id).to eq(category.id)
+ expect(topic.tags.pluck(:name)).to contain_exactly("aaa", "bbb")
+ end
+
it "does not triage PMs by default" do
post = Fabricate(:post, topic: personal_message)
automation.running_in_background!