Skip to content

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

FEATURE: Add a "Post language detected" workflow trigger (#45164)

Régis Hanol4089f06014f8
diff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
index 95fcb4bd425..75ccf5cdb6d 100644
--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb
@@ -32,9 +32,16 @@ module Jobs
# the backfill job will handle failures
return
end
+
+ return if detected_locale.blank?
+
+ DiscourseEvent.trigger(
+ :post_locale_detected,
+ post,
+ { skip_workflows: args[:skip_workflows] },
+ )
end
- return if detected_locale.blank?
locales = DiscourseAi::Translation.locales
return if locales.blank?
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 1d186b4e94a..1a0fba98f1a 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -122,12 +122,15 @@ en:
node_unavailable:
requires_ai: "Requires the AI plugin to be enabled"
requires_decision_model: "Requires a decision model"
+ requires_ai_translation: "Requires AI translation to be configured"
nodes:
"action:ai_agent": "AI Agent"
"action:classify": "Classify"
+ "trigger:post_locale_detected": "Post language detected"
node_descriptions:
"action:ai_agent": "Run an AI agent to process data or generate content"
"action:classify": "Ask a decision model questions about each item"
+ "trigger:post_locale_detected": "Fires when the language of a new or edited post is detected"
discourse_automation:
scriptables:
llm_report:
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/post_locale_detected/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/post_locale_detected/v1.rb
new file mode 100644
index 00000000000..1285c280393
--- /dev/null
+++ b/plugins/discourse-ai/discourse_workflows/nodes/post_locale_detected/v1.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+
+if defined?(DiscourseWorkflows)
+ module DiscourseWorkflows
+ module Nodes
+ module PostLocaleDetected
+ class V1 < DiscourseWorkflows::NodeType
+ include PostLifecycle
+
+ description(
+ name: "trigger:post_locale_detected",
+ version: "1.0",
+ defaults: {
+ icon: "language",
+ color: "violet",
+ },
+ group: "discourse_triggers",
+ event: :post_locale_detected,
+ available: -> { DiscourseAi::Translation.enabled? },
+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_ai_translation",
+ i18n_scope: PostLifecycle::I18N_SCOPE,
+ output_contracts: [
+ {
+ schema:
+ Schema.merge(
+ Schema::POST_SCHEMA,
+ Schema::TOPIC_LIST_ITEM_SCHEMA,
+ Schema::POST_AUTHOR_SCHEMA,
+ ),
+ },
+ ],
+ properties: PostLifecycle::PROPERTIES,
+ )
+
+ def output
+ {
+ post: serialize_post(@post),
+ topic: serialize_topic_list_item(topic),
+ user: serialize_user(@post.user),
+ }
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/translation/entry_point.rb b/plugins/discourse-ai/lib/translation/entry_point.rb
index 5c35200f489..59ac0fa17a9 100644
--- a/plugins/discourse-ai/lib/translation/entry_point.rb
+++ b/plugins/discourse-ai/lib/translation/entry_point.rb
@@ -4,9 +4,13 @@ module DiscourseAi
module Translation
class EntryPoint
def inject_into(plugin)
- plugin.on(:post_created) do |post|
+ plugin.on(:post_created) do |post, opts|
if DiscourseAi::Translation.enabled?
- Jobs.enqueue(:detect_translate_post, post_id: post.id)
+ Jobs.enqueue(
+ :detect_translate_post,
+ post_id: post.id,
+ skip_workflows: opts&.dig(:skip_workflows),
+ )
end
end
@@ -28,7 +32,14 @@ module DiscourseAi
Jobs.enqueue_in(grace, :detect_translate_topic, topic_id: post.topic_id)
end
- Jobs.enqueue_in(grace, :detect_translate_post, post_id: post.id) if raw_changed
+ if raw_changed
+ Jobs.enqueue_in(
+ grace,
+ :detect_translate_post,
+ post_id: post.id,
+ skip_workflows: revisor.opts[:skip_workflows],
+ )
+ end
end
end
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index ce176e6b6c3..c9d4d2843b7 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -127,6 +127,7 @@ after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node { DiscourseWorkflows::Nodes::AiAgent::V1 }
register_discourse_workflows_node { DiscourseWorkflows::Nodes::Classify::V1 }
+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::PostLocaleDetected::V1 }
end
add_admin_route("discourse_ai.title", "discourse-ai", { use_new_show_route: true })
diff --git a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb
index 00ee76ced9e..930f0801cd4 100644
--- a/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/regular/detect_translate_post_spec.rb
@@ -71,12 +71,26 @@ describe Jobs::DetectTranslatePost do
end
end
- it "detects locale" do
+ it "detects locale and triggers post_locale_detected" do
allow(DiscourseAi::Translation::PostLocaleDetector).to receive(:detect_locale).with(
post,
).and_return("zh_CN")
- job.execute({ post_id: post.id })
+ event =
+ DiscourseEvent.track(:post_locale_detected) do
+ job.execute({ post_id: post.id, skip_workflows: true })
+ end
+
+ expect(event[:params].first).to eq(post)
+ expect(event[:params].last[:skip_workflows]).to eq(true)
+ end
+
+ it "does not trigger post_locale_detected when no locale is detected" do
+ allow(DiscourseAi::Translation::PostLocaleDetector).to receive(:detect_locale).and_return(nil)
+
+ event = DiscourseEvent.track(:post_locale_detected) { job.execute({ post_id: post.id }) }
+
+ expect(event).to be_nil
end
it "skips locale detection when post has a locale" do
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/post_locale_detected_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/post_locale_detected_spec.rb
new file mode 100644
index 00000000000..e89eba323c9
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/post_locale_detected_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::PostLocaleDetected::V1, discourse_workflows: true do
+ before do
+ enable_current_plugin
+ graph = build_workflow_graph { |builder| builder.node "detected", described_class.identifier }
+ Fabricate(
+ :discourse_workflows_workflow,
+ created_by: Fabricate(:admin),
+ published: true,
+ **graph,
+ )
+ end
+
+ it "dispatches workflows with the detected post locale" do
+ post = Fabricate(:post, locale: "fr")
+
+ DiscourseEvent.trigger(:post_locale_detected, post, {})
+
+ trigger_data = Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.sole["args"].first["trigger_data"]
+ expect(trigger_data.dig("post", "locale")).to eq("fr")
+ expect(trigger_data.dig("user", "id")).to eq(post.user_id)
+ expect(trigger_data).to match_node_output_schema(described_class)
+ end
+
+ it "does not dispatch workflows for posts created by workflows" do
+ DiscourseEvent.trigger(:post_locale_detected, Fabricate(:post), { skip_workflows: true })
+
+ expect(Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs).to be_empty
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb
index a894d3f666e..8b077678f8d 100644
--- a/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/entry_point_spec.rb
@@ -15,6 +15,24 @@ describe DiscourseAi::Translation::EntryPoint do
expect_job_enqueued(job: :detect_translate_post, args: { post_id: post.id })
end
+ it "passes skip_workflows to the detection job" do
+ post =
+ PostCreator.create!(
+ Fabricate(:user),
+ raw: "post",
+ title: "topic",
+ skip_validations: true,
+ skip_workflows: true,
+ )
+ expect_job_enqueued(
+ job: :detect_translate_post,
+ args: {
+ post_id: post.id,
+ skip_workflows: true,
+ },
+ )
+ end
+
it "does not enqueue if setting disabled" do
SiteSetting.ai_translation_enabled = false
post = Fabricate(:post)
@@ -165,6 +183,19 @@ describe DiscourseAi::Translation::EntryPoint do
)
expect(job_enqueued?(job: :detect_translate_topic)).to eq false
end
+
+ it "passes skip_workflows to the detection job" do
+ post = Fabricate(:post, post_number: 2)
+ post.revise(post.user, { raw: "new raw" }, skip_workflows: true)
+
+ expect_job_enqueued(
+ job: :detect_translate_post,
+ args: {
+ post_id: post.id,
+ skip_workflows: true,
+ },
+ )
+ end
end
describe "upon category updated" do