Skip to content

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

FEATURE: Improve workflow node filtering (#40977)

Sam8d5ba0dd844d
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index c9fa9a4c69a..24672d0b6dc 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3035,13 +3035,22 @@ en:
topic_before: "Show posts in topics created before a date (YYYY-MM-DD or days ago)"
keywords: "Show posts containing any of the specified keywords"
topic_keywords: "Show posts in topics containing any of the specified keywords"
- post_type: "Show first posts or replies"
+ post_type: "Filter posts by type or position"
+ post_type_regular: "Regular posts only"
+ post_type_all: "All visible post types"
post_type_first: "First posts only"
post_type_reply: "Replies only"
+ post_type_moderator_action: "Moderator action posts only"
+ post_type_small_action: "Small action posts only"
+ post_type_whisper: "Whispers only"
status: "Filter posts by topic status"
status_open: "Open topics"
status_closed: "Closed topics"
status_archived: "Archived topics"
+ status_listed: "Listed topics"
+ status_unlisted: "Unlisted topics"
+ status_deleted: "Deleted topics"
+ status_public: "Topics in public categories"
status_noreplies: "Topics with no replies"
status_single_user: "Topics with a single user"
order: "Sort posts"
diff --git a/lib/posts_filter.rb b/lib/posts_filter.rb
index d249ebbd57b..da673862c79 100644
--- a/lib/posts_filter.rb
+++ b/lib/posts_filter.rb
@@ -30,8 +30,20 @@ class PostsFilter
private_constant :TOKENIZER_PATTERN
ORDER_VALUES = %w[latest oldest latest_topic oldest_topic likes].freeze
- POST_TYPE_VALUES = %w[first reply].freeze
- STATUS_VALUES = %w[open closed archived noreplies single_user].freeze
+ POST_TYPE_VALUES = %w[regular all first reply moderator_action small_action whisper].freeze
+ STORED_POST_TYPE_VALUES = %w[regular moderator_action small_action whisper].freeze
+ POST_TYPE_SCOPE_VALUES = (STORED_POST_TYPE_VALUES + %w[all]).freeze
+ STATUS_VALUES = %w[
+ open
+ closed
+ archived
+ listed
+ unlisted
+ deleted
+ public
+ noreplies
+ single_user
+ ].freeze
CUSTOM_FILTER_PREFIXES = [nil, ""].freeze
private_constant :CUSTOM_FILTER_PREFIXES
@@ -282,11 +294,21 @@ class PostsFilter
end
def secure_base_relation
+ include_deleted_topics = include_deleted_topics?
+
+ base_scope = @base_scope
+ base_scope = base_scope.with_deleted if include_deleted_topics
+
+ topic_scope = Topic.secured(@guardian)
+ topic_scope = topic_scope.with_deleted if include_deleted_topics
+
+ # Use a raw topic join because the association join applies Topic's deleted_at
+ # default scope in the ON clause, which would hide status:deleted matches.
relation =
- @base_scope
+ base_scope
.secured(@guardian)
- .joins(:topic)
- .merge(Topic.secured(@guardian))
+ .joins("INNER JOIN topics ON topics.id = posts.topic_id")
+ .merge(topic_scope)
.where("topics.archetype = ?", Archetype.default)
relation = @guardian.filter_hidden_posts(relation)
@@ -304,25 +326,54 @@ class PostsFilter
@guardian.anonymous? || [email protected]_see_unlisted_topics?
end
+ def include_deleted_topics?
+ return false if [email protected]_see_deleted_topics?(nil)
+
+ parsed_filters = @filters + @or_groups.flatten
+ parsed_filters.any? { |filter| deleted_status_filter?(filter) }
+ end
+
+ def deleted_status_filter?(filter)
+ filter[:key] == "status" && filter[:value].casecmp?("deleted")
+ end
+
def filtered_relation(base_relation)
if @or_groups.any?
or_relations =
@or_groups.map do |or_group|
- or_group.reduce(base_relation) do |relation, parsed_filter|
- apply_filter(relation, parsed_filter)
- end
+ or_group.reduce(
+ relation_for_filter_group(base_relation, or_group),
+ ) { |relation, parsed_filter| apply_filter(relation, parsed_filter) }
end
union_sql =
or_relations.map { |relation| relation.reselect("posts.id").to_sql }.join(" UNION ")
base_relation.where("posts.id IN (#{union_sql})")
else
- @filters.reduce(base_relation) do |relation, parsed_filter|
- apply_filter(relation, parsed_filter)
- end
+ @filters.reduce(
+ relation_for_filter_group(base_relation, @filters),
+ ) { |relation, parsed_filter| apply_filter(relation, parsed_filter) }
end
end
+ def relation_for_filter_group(base_relation, filters)
+ relation = base_relation
+
+ if include_deleted_topics? && filters.none? { |filter| deleted_status_filter?(filter) }
+ relation = relation.where(posts: { deleted_at: nil }, topics: { deleted_at: nil })
+ end
+
+ if filters.none? { |filter| post_type_scope_filter?(filter) }
+ relation = relation.where(posts: { post_type: Post.types[:regular] })
+ end
+
+ relation
+ end
+
+ def post_type_scope_filter?(filter)
+ filter[:key] == "post_type" && POST_TYPE_SCOPE_VALUES.include?(filter[:value].downcase)
+ end
+
def order_relation(relation)
case @order
when :latest_post
@@ -463,6 +514,18 @@ class PostsFilter
relation.where("topics.closed = true")
when "archived"
relation.where("topics.archived = true")
+ when "listed"
+ relation.where("topics.visible = true")
+ when "unlisted"
+ relation.where("topics.visible = false")
+ when "deleted"
+ return relation.where("1 = 0") if [email protected]_see_deleted_topics?(nil)
+
+ relation.where.not(topics: { deleted_at: nil })
+ when "public"
+ relation.joins("LEFT JOIN categories ON categories.id = topics.category_id").where(
+ "topics.category_id IS NULL OR NOT categories.read_restricted",
+ )
when "noreplies"
relation.where("topics.posts_count = 1")
when "single_user"
@@ -588,13 +651,17 @@ class PostsFilter
end
def filter_post_type(relation, post_type)
- case post_type.downcase
+ post_type_value = post_type.downcase
+
+ case post_type_value
when "first"
relation.where("posts.post_number = 1")
when "reply"
relation.where("posts.post_number > 1")
- else
+ when "all"
relation
+ else
+ relation.where(posts: { post_type: Post.types[post_type_value.to_sym] })
end
end
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 0efabc7dc96..71f7037bb26 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -235,6 +235,10 @@ en:
discourse_workflows:
ai_agent:
agent_id: "Agent"
+ forced_llm_notice_description: "This agent is locked to its configured default LLM. The workflow cannot override it."
+ llm_model_id: "LLM model"
+ llm_model_default: "Use agent default"
+ llm_model_default_with_name: "Use agent default (%{value})"
prompt: "Prompt"
select_agent: "Select an agent..."
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 6033353f156..3fe654e7481 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -273,6 +273,13 @@ en:
copied: "Copied to clipboard"
unknown_model: "Unknown AI model"
+ discourse_workflows:
+ ai_agent:
+ errors:
+ locked_default_llm_missing: "Agent '%{agent}' is locked to its default LLM, but no valid default LLM is configured"
+ llm_not_found: "LLM model with id %{llm_model_id} not found"
+ no_llm_configured: "No LLM is configured for agent '%{agent}'. Select an LLM model or configure an agent/site default LLM."
+
tools:
custom_name: "%{name} (custom)"
presets:
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
index 42d75e8504d..ad1ebab4791 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -38,8 +38,11 @@ if defined?(DiscourseWorkflows)
filterable: true,
value_property: :id,
name_property: :name,
+ resets: %w[llm_model_id],
set_from_option: {
agent_name: "name",
+ agent_force_default_llm: "force_default_llm",
+ agent_resolved_llm_name: "resolved_llm_name",
},
},
},
@@ -49,6 +52,52 @@ if defined?(DiscourseWorkflows)
hidden: true,
},
},
+ agent_force_default_llm: {
+ type: :boolean,
+ default: false,
+ ui: {
+ hidden: true,
+ },
+ },
+ agent_resolved_llm_name: {
+ type: :string,
+ ui: {
+ hidden: true,
+ },
+ },
+ llm_model_id: {
+ type: :integer,
+ required: false,
+ type_options: {
+ load_options_method: "llm_models",
+ },
+ no_data_expression: true,
+ ui: {
+ control: :combo_box,
+ },
+ control_options: {
+ filterable: true,
+ value_property: :id,
+ name_property: :name,
+ none: "discourse_ai.discourse_workflows.ai_agent.llm_model_default",
+ none_label_field: "agent_resolved_llm_name",
+ none_label_i18n_key:
+ "discourse_ai.discourse_workflows.ai_agent.llm_model_default_with_name",
+ },
+ display_options: {
+ hide: {
+ agent_force_default_llm: [true],
+ },
+ },
+ },
+ forced_llm_notice: {
+ type: :notice,
+ display_options: {
+ show: {
+ agent_force_default_llm: [true],
+ },
+ },
+ },
prompt: {
type: :string,
ui: {
@@ -66,15 +115,52 @@ if defined?(DiscourseWorkflows)
case context.method_name
when "agents"
agent_options.select { |agent| context.matches_filter?(agent[:name]) }
+ when "llm_models"
+ llm_model_options(context)
end
end
def self.agent_options
+ default_llm = default_llm_model
+
::AiAgent
.where(enabled: true)
.order(:name)
- .pluck(:id, :name)
- .map { |id, name| { id: id, name: name } }
+ .pluck(:id, :name, :default_llm_id, :force_default_llm)
+ .map do |id, name, default_llm_id, force_default_llm|
+ configured_llm = find_llm_model(default_llm_id)
+ resolved_llm = force_default_llm ? configured_llm : configured_llm || default_llm
+ {
+ id: id,
+ name: name,
+ default_llm_id: default_llm_id,
+ force_default_llm: force_default_llm,
+ resolved_llm_id: resolved_llm&.id,
+ resolved_llm_name: resolved_llm&.display_name,
+ }
+ end
+ end
+
+ def self.llm_model_options(context)
+ ::LlmModel
+ .order(:display_name)
+ .pluck(:id, :display_name)
+ .filter_map do |id, display_name|
+ next if display_name.blank?
+
+ { id: id, name: display_name }
+ end
+ .select { |llm_model| context.matches_filter?(llm_model[:name]) }
+ end
+
+ def self.default_llm_model
+ find_llm_model(SiteSetting.ai_default_llm_model)
+ end
+
+ def self.find_llm_model(id)
+ return if id.blank?
+
+ ::LlmModel.find_by(id: id)
end
def execute(exec_ctx)
@@ -82,6 +168,7 @@ if defined?(DiscourseWorkflows)
exec_ctx.input_items.map.with_index do |item, item_index|
config = {
"agent_id" => exec_ctx.get_node_parameter("agent_id", item_index),
+ "llm_model_id" => exec_ctx.get_node_parameter("llm_model_id", item_index),
"prompt" => exec_ctx.get_node_parameter("prompt", item_index),
}
result = run_agent(config, exec_ctx.log)
@@ -94,6 +181,45 @@ if defined?(DiscourseWorkflows)
private
+ def resolve_llm_model(agent_record, requested_llm_model_id)
+ if agent_record.force_default_llm?
+ llm_model =
+ ::LlmModel.find_by(id: agent_record.default_llm_id) if agent_record.default_llm_id
+ return llm_model if llm_model.present?
+
+ raise_node_error!(
+ I18n.t(
+ "discourse_ai.discourse_workflows.ai_agent.errors.locked_default_llm_missing",
+ agent: agent_record.name,
+ ),
+ )
+ end
+
+ if requested_llm_model_id.present?
+ llm_model = ::LlmModel.find_by(id: requested_llm_model_id)
+ return llm_model if llm_model.present?
+
+ raise_node_error!(
+ I18n.t(
+ "discourse_ai.discourse_workflows.ai_agent.errors.llm_not_found",
+ llm_model_id: requested_llm_model_id,
+ ),
+ )
+ end
+
+ [agent_record.default_llm_id, SiteSetting.ai_default_llm_model].each do |llm_model_id|
+ llm_model = ::LlmModel.find_by(id: llm_model_id) if llm_model_id.present?
+ return llm_model if llm_model.present?
+ end
+
+ raise_node_error!(
+ I18n.t(
+ "discourse_ai.discourse_workflows.ai_agent.errors.no_llm_configured",
+ agent: agent_record.name,
+ ),
+ )
+ end
+
def run_agent(config, log)
agent_id = config["agent_id"]
prompt = config["prompt"].to_s
@@ -105,12 +231,19 @@ if defined?(DiscourseWorkflows)
raise_node_error!("AI Agent '#{agent_record.name}' is disabled")
end
+ agent_instance = agent_record.class_instance.new
+ llm_model = resolve_llm_model(agent_record, config["llm_model_id"])
+
log.info("Agent: #{agent_record.name}")
- log.info("LLM: #{agent_record.default_llm_id}")
+ log.info("LLM: #{llm_model.display_name} (#{llm_model.id})")
log.info("Prompt: #{prompt.to_s[0..200]}")
- agent_instance = agent_record.class_instance.new
- bot = DiscourseAi::Agents::Bot.as(Discourse.system_user, agent: agent_instance)
+ bot =
+ DiscourseAi::Agents::Bot.as(
+ Discourse.system_user,
+ agent: agent_instance,
+ model: llm_model,
+ )
bot_context =
DiscourseAi::Agents::BotContext.new(
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
index 9ea0792c210..3c4c2f15ff9 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
@@ -1,15 +1,23 @@
# frozen_string_literal: true
RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
- fab!(:agent) { Fabricate(:ai_agent, name: "Workflow agent", enabled: true) }
+ fab!(:llm_model) { Fabricate(:llm_model, display_name: "Workflow LLM") }
+ fab!(:site_llm_model) { Fabricate(:llm_model, display_name: "Site LLM") }
+ fab!(:agent) do
+ Fabricate(:ai_agent, name: "Workflow agent", enabled: true, default_llm_id: llm_model.id)
+ end
let(:bot) { instance_double(DiscourseAi::Agents::Bot) }
+ let(:bot_models) { [] }
let(:prompts) { [] }
before do
SiteSetting.discourse_ai_enabled = true
- allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(bot)
+ allow(DiscourseAi::Agents::Bot).to receive(:as) do |_user, agent:, model:|
+ bot_models << model
+ bot
+ end
allow(bot).to receive(:reply) do |bot_context, &block|
prompt = bot_context.messages.first[:content]
@@ -20,32 +28,70 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
describe ".load_options_context" do
fab!(:disabled_agent) { Fabricate(:ai_agent, name: "Disabled agent", enabled: false) }
- fab!(:matching_agent) { Fabricate(:ai_agent, name: "Alpha agent", enabled: true) }
- fab!(:other_agent) { Fabricate(:ai_agent, name: "Gamma agent", enabled: true) }
+ fab!(:matching_agent) do
+ Fabricate(:ai_agent, name: "Alpha agent", enabled: true, default_llm_id: llm_model.id)
+ end
+ fab!(:other_agent) do
+ Fabricate(:ai_agent, name: "Gamma agent", enabled: true, default_llm_id: llm_model.id)
+ end
- def load_options(filter: nil)
+ def load_options(method_name: "agents", filter: nil, parameters: {})
context =
DiscourseWorkflows::LoadOptionsContext.new(
- method_name: "agents",
+ method_name: method_name,
filter: filter,
+ parameters: parameters,
node_class: described_class,
)
described_class.load_options_context(context)
end
- it "returns enabled AI agents for the chooser" do
+ it "returns enabled AI agents for the chooser and includes resolved LLM metadata" do
option_ids = [agent.id, disabled_agent.id]
options = load_options.select { |option| option_ids.include?(option[:id]) }
- expect(options).to contain_exactly({ id: agent.id, name: agent.name })
+ expect(options).to contain_exactly(
+ {
+ id: agent.id,
+ name: agent.name,
+ default_llm_id: llm_model.id,
+ force_default_llm: false,
+ resolved_llm_id: llm_model.id,
+ resolved_llm_name: llm_model.display_name,
+ },
+ )
+ end
+
+ it "includes the site default LLM in agent metadata when the agent has no default" do
+ agent.update!(default_llm_id: nil)
+ SiteSetting.stubs(:ai_default_llm_model).returns(site_llm_model.id)
+
+ option = load_options.find { |agent_option| agent_option[:id] == agent.id }
+
+ expect(option).to include(
+ name: agent.name,
+ default_llm_id: nil,
+ resolved_llm_id: site_llm_model.id,
+ resolved_llm_name: site_llm_model.display_name,
+ )
end
it "filters AI agents by the filter term" do
option_ids = [matching_agent.id, other_agent.id]
options = load_options(filter: "alpha").select { |option| option_ids.include?(option[:id]) }
- expect(options).to contain_exactly({ id: matching_agent.id, name: matching_agent.name })
+ expect(options.first[:id]).to eq(matching_agent.id)
+ end
+
+ it "returns LLM models for the override chooser" do
+ override_model = Fabricate(:llm_model, display_name: "Override LLM")
+
+ expect(load_options(method_name: "llm_models").pluck(:id)).to include(
+ llm_model.id,
+ site_llm_model.id,
+ override_model.id,
+ )
end
end
@@ -73,4 +119,68 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
expect(prompts).to eq([""])
expect(items.first["json"]["result"]).to eq("Reply to ")
end
+
+ it "uses the selected LLM override when the agent does not force its default LLM" do
+ override_model = Fabricate(:llm_model, display_name: "Override LLM")
+
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "llm_model_id" => override_model.id,
+ "prompt" => "Hello",
+ },
+ )
+
+ expect(bot_models).to contain_exactly(override_model)
+ end
+
+ it "ignores the selected LLM override when the agent forces its default LLM" do
+ override_model = Fabricate(:llm_model, display_name: "Override LLM")
+ agent.update!(force_default_llm: true)
+
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "llm_model_id" => override_model.id,
+ "prompt" => "Hello",
+ },
+ )
+
+ expect(bot_models).to contain_exactly(llm_model)
+ end
+
+ it "raises when the selected LLM override does not exist" do
+ expect do
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "llm_model_id" => -999,
+ "prompt" => "Hello",
+ },
+ )
+ end.to raise_error(DiscourseWorkflows::NodeError, /LLM model with id -999 not found/)
+ end
+
+ it "raises when a forced agent has no valid default LLM" do
+ agent.update_columns(force_default_llm: true, default_llm_id: -999)
+
+ expect do
+ execute_node_output(configuration: { "agent_id" => agent.id, "prompt" => "Hello" })
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /locked to its default LLM, but no valid default LLM is configured/,
+ )
+ end
+
+ it "raises when no selected, agent, or site default LLM is configured" do
+ agent.update!(default_llm_id: nil)
+ SiteSetting.stubs(:ai_default_llm_model).returns(nil)
+
+ expect do
+ execute_node_output(configuration: { "agent_id" => agent.id, "prompt" => "Hello" })
+ end.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /No LLM is configured for agent '#{agent.name}'/,
+ )
+ end
end
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
index 9156ddc816f..299191d0721 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
@@ -4,6 +4,7 @@ import { action } from "@ember/object";
import { service } from "@ember/service";
import ComboBox from "discourse/select-kit/components/combo-box";
import DButton from "discourse/ui-kit/d-button";
+import { i18n } from "discourse-i18n";
import {
fieldType,
formatOptionValue,
@@ -94,6 +95,21 @@ export default class ComboBoxField extends Component {
);
}
+ get translatedNone() {
+ const labelField = this.controlOptions.none_label_field;
+ const labelKey = this.controlOptions.none_label_i18n_key;
+
+ if (!labelField || !labelKey) {
+ return null;
+ }
+
+ const value =
+ (this.args.nodeParameters || this.args.configuration || {})[labelField] ||
+ null;
+
+ return value ? i18n(labelKey, { value }) : null;
+ }
+
get filterable() {
return Boolean(this.controlOptions.filterable);
}
@@ -199,20 +215,18 @@ export default class ComboBoxField extends Component {
}
@action
- handleChange(value) {
+ handleChange(value, selectedItem = null) {
this.args.field.set(value);
- const selectedOption = this.options.find(
- (option) => String(option.id) === String(value)
- );
+ const selectedOption =
+ selectedItem ||
+ this.options.find((option) => String(option.id) === String(value));
for (const [fieldName, propertyName] of Object.entries(
this.setFromOption
)) {
- this.args.formApi?.set(
- fieldName,
- selectedOption?.original?.[propertyName] || ""
- );
+ const selectedOptionValue = selectedOption?.original?.[propertyName];
+ this.args.formApi?.set(fieldName, selectedOptionValue ?? "");
}
const schema = this.args.nodeDefinition?.properties || {};
@@ -253,6 +267,7 @@ export default class ComboBoxField extends Component {
@options={{hash
filterable=this.filterable
none=this.none
+ translatedNone=this.translatedNone
castInteger=this.castInteger
}}
/>
@@ -274,6 +289,7 @@ export default class ComboBoxField extends Component {
@options={{hash
filterable=this.filterable
none=this.none
+ translatedNone=this.translatedNone
castInteger=this.castInteger
}}
/>
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 610ad9b779b..04c9637d380 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -328,15 +328,23 @@ en:
groups_description: "Group names separated by commas"
post_type: "Post type"
post_types:
- all: "All posts"
+ regular: "Regular posts only"
+ all: "All visible post types"
first: "First posts only"
reply: "Replies only"
+ moderator_action: "Moderator action posts only"
+ small_action: "Small action posts only"
+ whisper: "Whispers only"
status: "Status"
statuses:
any: "Any status"
open: "Open"
closed: "Closed"
archived: "Archived"
+ listed: "Listed"
+ unlisted: "Unlisted"
+ deleted: "Deleted"
+ public: "Public"
noreplies: "No replies"
single_user: "Single user"
keywords: "Keywords"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
index c47325d1d88..7dfa0917faf 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
@@ -5,8 +5,19 @@ module DiscourseWorkflows
module Post
class V1 < NodeType
OPERATIONS = %w[create edit get list].freeze
- STATUS_OPTIONS = %w[any open closed archived noreplies single_user].freeze
- POST_TYPE_OPTIONS = %w[all first reply].freeze
+ STATUS_OPTIONS = %w[
+ any
+ open
+ closed
+ archived
+ listed
+ unlisted
+ deleted
+ public
+ noreplies
+ single_user
+ ].freeze
+ POST_TYPE_OPTIONS = %w[regular all first reply moderator_action small_action whisper].freeze
ORDER_OPTIONS = %w[latest oldest latest_topic oldest_topic likes].freeze
DEFAULT_LIMIT = 30
MAX_LIMIT = 500
@@ -181,7 +192,7 @@ module DiscourseWorkflows
type: :options,
required: false,
options: POST_TYPE_OPTIONS,
- default: "all",
+ default: "regular",
ui: {
hidden: true,
},
@@ -309,7 +320,7 @@ module DiscourseWorkflows
"topics" => exec_ctx.get_node_parameter("topics", item_index),
"usernames" => exec_ctx.get_node_parameter("usernames", item_index),
"groups" => exec_ctx.get_node_parameter("groups", item_index),
- "post_type" => exec_ctx.get_node_parameter("post_type", item_index, default: "all"),
+ "post_type" => exec_ctx.get_node_parameter("post_type", item_index, default: "regular"),
"status" => exec_ctx.get_node_parameter("status", item_index, default: "any"),
"keywords" => exec_ctx.get_node_parameter("keywords", item_index),
"topic_keywords" => exec_ctx.get_node_parameter("topic_keywords", item_index),
@@ -450,7 +461,7 @@ module DiscourseWorkflows
add_query_part(parts, "groups", config["groups"])
add_query_part(parts, "keywords", config["keywords"])
add_query_part(parts, "topic_keywords", config["topic_keywords"])
- if POST_TYPE_OPTIONS.include?(config["post_type"]) && config["post_type"] != "all"
+ if POST_TYPE_OPTIONS.include?(config["post_type"]) && config["post_type"] != "regular"
add_query_part(parts, "post_type", config["post_type"])
end
if STATUS_OPTIONS.include?(config["status"]) && config["status"] != "any"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
index 05eba611947..b41253222c5 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
@@ -78,6 +78,8 @@ module DiscourseWorkflows
lang
name_property
none
+ none_label_field
+ none_label_i18n_key
option_format
set_from_option
resets
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
index 90b15e4c94e..5623e418f73 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb
@@ -348,6 +348,60 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do
)
end
+ it "lists regular posts by default and explicit action posts" do
+ topic = Fabricate(:topic, category: category, user: user)
+ regular_post = Fabricate(:post, topic: topic, user: user, post_number: 1, raw: "regular body")
+ small_action_post =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ post_number: 2,
+ post_type: Post.types[:small_action],
+ )
+
+ default_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+ all_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "post_type" => "all",
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+ small_action_result =
+ execute_node_output(
+ configuration: {
+ "operation" => "list",
+ "post_type" => "small_action",
+ "limit" => "10",
+ },
+ item: item,
+ ).first
+
+ expect(default_result.map { |output_item| output_item.dig("json", "post", "id") }).to include(
+ regular_post.id,
+ )
+ expect(
+ default_result.map { |output_item| output_item.dig("json", "post", "id") },
+ ).not_to include(small_action_post.id)
+ expect(all_result.map { |output_item| output_item.dig("json", "post", "id") }).to include(
+ regular_post.id,
+ small_action_post.id,
+ )
+ expect(
+ small_action_result.map { |output_item| output_item.dig("json", "post", "id") },
+ ).to contain_exactly(small_action_post.id)
+ end
+
it "respects actor permissions when listing posts" do
restricted = Fabricate(:category, read_restricted: true)
Fabricate(:category_group, category: restricted, group: Group[:staff], permission_type: 0)
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
index dbce9fd53d8..f27dcc0e11f 100644
--- a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
@@ -824,7 +824,12 @@ module("Integration | Component | workflows property engine", function (hooks) {
test("renders combo boxes from metadata and applies option patches", async function (assert) {
this.setProperties({
- configuration: { agent_id: 2, agent_name: "" },
+ configuration: {
+ agent_force_default_llm: true,
+ agent_id: 2,
+ agent_name: "",
+ llm_model_id: 99,
+ },
formApi: null,
nodeType: "action:ai_agent",
nodeTypes: [
@@ -832,8 +837,8 @@ module("Integration | Component | workflows property engine", function (hooks) {
identifier: "action:ai_agent",
metadata: {
agents: [
- { id: 1, name: "Support Bot" },
- { id: 2, name: "Helper Bot" },
+ { id: 1, name: "Support Bot", force_default_llm: false },
+ { id: 2, name: "Helper Bot", force_default_llm: true },
],
i18n_prefix: "discourse_ai.discourse_workflows",
},
@@ -854,7 +859,9 @@ module("Integration | Component | workflows property engine", function (hooks) {
filterable: true,
name_property: "name",
none: "discourse_ai.discourse_workflows.ai_agent.select_agent",
+ resets: ["llm_model_id"],
set_from_option: {
+ agent_force_default_llm: "force_default_llm",
agent_name: "name",
},
value_property: "id",
@@ -894,7 +901,190 @@ module("Integration | Component | workflows property engine", function (hooks) {
await selector.selectRowByValue("1");
assert.strictEqual(String(this.formApi.get("agent_id")), "1");
+ assert.false(this.formApi.get("agent_force_default_llm"));
assert.strictEqual(this.formApi.get("agent_name"), "Support Bot");
+ assert.strictEqual(this.formApi.get("llm_model_id"), null);
+ });
+
+ test("applies option patches from remote combo box options", async function (assert) {
+ const requests = [];
+ pretender.post(
+ "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
+ (request) => {
+ const body = JSON.parse(request.requestBody);
+ requests.push(body);
+
+ return response([
+ {
+ id: 1,
+ name: "Support Bot",
+ force_default_llm: false,
+ resolved_llm_name: "Default LLM",
+ },
+ ]);
+ }
+ );
+
+ this.setProperties({
+ configuration: {
+ agent_force_default_llm: true,
+ agent_id: null,
+ agent_name: "",
+ agent_resolved_llm_name: "",
+ llm_model_id: 99,
+ },
+ formApi: null,
+ node: {
+ clientId: "node-1",
+ type: "action:ai_agent",
+ typeVersion: "1.0",
+ },
+ nodeType: "action:ai_agent",
+ nodeTypes: [
+ {
+ identifier: "action:ai_agent",
+ name: "action:ai_agent",
+ version: "1.0",
+ },
+ ],
+ schema: {
+ agent_id: {
+ type: "integer",
+ required: true,
+ type_options: {
+ load_options_method: "agents",
+ },
+ no_data_expression: true,
+ ui: {
+ control: "combo_box",
+ },
+ control_options: {
+ filterable: true,
+ name_property: "name",
+ resets: ["llm_model_id"],
+ set_from_option: {
+ agent_force_default_llm: "force_default_llm",
+ agent_name: "name",
+ agent_resolved_llm_name: "resolved_llm_name",
+ },
+ value_property: "id",
+ },
+ },
+ agent_force_default_llm: {
+ type: "boolean",
+ },
+ agent_name: {
+ type: "string",
+ },
+ agent_resolved_llm_name: {
+ type: "string",
+ },
+ llm_model_id: {
+ type: "integer",
+ },
+ },
+ registerApi: (api) => {
+ this.set("formApi", api);
+ },
+ });
+
+ await render(
+ <template>
+ <Form
+ @data={{this.configuration}}
+ @onRegisterApi={{this.registerApi}}
+ as |form transientData|
+ >
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @formApi={{this.formApi}}
+ @configuration={{transientData}}
+ @node={{this.node}}
+ @nodeType={{this.nodeType}}
+ @nodeTypes={{this.nodeTypes}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const selector = selectKit(".combo-box");
+ await selector.expand();
+ await waitFor(".combo-box .select-kit-row[data-value='1']");
+ await selector.selectRowByValue("1");
+
+ assert.strictEqual(String(this.formApi.get("agent_id")), "1");
+ assert.false(this.formApi.get("agent_force_default_llm"));
+ assert.strictEqual(this.formApi.get("agent_name"), "Support Bot");
+ assert.strictEqual(
+ this.formApi.get("agent_resolved_llm_name"),
+ "Default LLM"
+ );
+ assert.strictEqual(this.formApi.get("llm_model_id"), null);
+ assert.true(requests.length >= 1);
+ assert.strictEqual(requests[0].methodName, "agents");
+ });
+
+ test("renders combo box dynamic none labels from configuration", async function (assert) {
+ this.setProperties({
+ configuration: {
+ agent_resolved_llm_name: "Workflow LLM",
+ llm_model_id: null,
+ },
+ nodeType: "action:ai_agent",
+ nodeTypes: [
+ {
+ identifier: "action:ai_agent",
+ metadata: {
+ llm_models: [{ id: 1, name: "Override LLM" }],
+ },
+ },
+ ],
+ schema: {
+ llm_model_id: {
+ type: "integer",
+ required: false,
+ type_options: {
+ load_options_method: "llm_models",
+ },
+ no_data_expression: true,
+ ui: {
+ control: "combo_box",
+ },
+ control_options: {
+ name_property: "name",
+ none: "discourse_ai.discourse_workflows.ai_agent.llm_model_default",
+ none_label_field: "agent_resolved_llm_name",
+ none_label_i18n_key:
+ "discourse_ai.discourse_workflows.ai_agent.llm_model_default_with_name",
+ value_property: "id",
+ },
+ },
+ },
+ });
+
+ await render(
+ <template>
+ <Form @data={{this.configuration}} as |form transientData|>
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @configuration={{transientData}}
+ @nodeType={{this.nodeType}}
+ @nodeTypes={{this.nodeTypes}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const selector = selectKit(".combo-box");
+
+ assert.strictEqual(
+ selector.header().label(),
+ "Use agent default (Workflow LLM)"
+ );
});
test("renders remote multi-select options from load options", async function (assert) {
diff --git a/spec/lib/posts_filter_spec.rb b/spec/lib/posts_filter_spec.rb
index e56ef9a887b..9b41ffabac6 100644
--- a/spec/lib/posts_filter_spec.rb
+++ b/spec/lib/posts_filter_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe PostsFilter do
fab!(:user)
fab!(:user2, :user)
+ fab!(:admin)
fab!(:feature_tag) { Fabricate(:tag, name: "feature") }
fab!(:bug_tag) { Fabricate(:tag, name: "bug") }
fab!(:announcement_category) { Fabricate(:category, name: "Announcements") }
@@ -66,7 +67,28 @@ RSpec.describe PostsFilter do
{ name: "order:", description: I18n.t("posts_filter.description.order"), priority: 1 },
{ name: "order:latest", description: I18n.t("posts_filter.description.order_latest") },
{ name: "status:open", description: I18n.t("posts_filter.description.status_open") },
+ { name: "status:listed", description: I18n.t("posts_filter.description.status_listed") },
+ { name: "status:unlisted", description: I18n.t("posts_filter.description.status_unlisted") },
+ { name: "status:deleted", description: I18n.t("posts_filter.description.status_deleted") },
+ { name: "status:public", description: I18n.t("posts_filter.description.status_public") },
+ {
+ name: "post_type:regular",
+ description: I18n.t("posts_filter.description.post_type_regular"),
+ },
+ { name: "post_type:all", description: I18n.t("posts_filter.description.post_type_all") },
{ name: "post_type:first", description: I18n.t("posts_filter.description.post_type_first") },
+ {
+ name: "post_type:moderator_action",
+ description: I18n.t("posts_filter.description.post_type_moderator_action"),
+ },
+ {
+ name: "post_type:small_action",
+ description: I18n.t("posts_filter.description.post_type_small_action"),
+ },
+ {
+ name: "post_type:whisper",
+ description: I18n.t("posts_filter.description.post_type_whisper"),
+ },
)
expect(options.find { |option| option[:name] == "order:" }).not_to include(
:type,
@@ -159,7 +181,7 @@ RSpec.describe PostsFilter do
expect(filtered_post_ids("username:aאb")).to contain_exactly(unicode_post.id)
end
- it "filters by topic, post type, status, dates, and limits" do
+ it "filters posts by topic, post type, status, dates, and limits" do
feature_post.update_columns(created_at: 3.days.ago)
bug_post.update_columns(created_at: 2.days.ago)
feature_bug_post.update_columns(created_at: 1.day.ago)
@@ -188,6 +210,160 @@ RSpec.describe PostsFilter do
expect(filtered_post_ids("category:Feedback max_results:1", limit: 5).length).to eq(1)
end
+ it "defaults to regular posts and allows explicit post types" do
+ SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff].to_s
+
+ topic = Fabricate(:topic, user: user, category: announcement_category)
+ regular_post = Fabricate(:post, topic: topic, user: user, post_number: 1)
+ reply_post = Fabricate(:post, topic: topic, user: user, post_number: 2)
+ moderator_action_post =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ post_number: 3,
+ post_type: Post.types[:moderator_action],
+ )
+ small_action_post =
+ Fabricate(
+ :post,
+ topic: topic,
+ user: admin,
+ post_number: 4,
+ post_type: Post.types[:small_action],
+ )
+ whisper_post =
+ Fabricate(:post, topic: topic, user: admin, post_number: 5, post_type: Post.types[:whisper])
+ post_ids = [
+ regular_post.id,
+ reply_post.id,
+ moderator_action_post.id,
+ small_action_post.id,
+ whisper_post.id,
+ ]
+ scope = Post.where(id: post_ids)
+
+ expect(filtered_post_ids("", guardian: admin.guardian, scope: scope)).to contain_exactly(
+ regular_post.id,
+ reply_post.id,
+ )
+ expect(
+ filtered_post_ids("post_type:all", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(*post_ids)
+ expect(
+ filtered_post_ids("post_type:moderator_action", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(moderator_action_post.id)
+ expect(
+ filtered_post_ids("post_type:small_action", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(small_action_post.id)
+ expect(
+ filtered_post_ids("post_type:whisper", guardian: user.guardian, scope: scope),
+ ).to be_empty
+ expect(
+ filtered_post_ids("post_type:whisper", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(whisper_post.id)
+ expect(
+ filtered_post_ids("post_type:first", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(regular_post.id)
+ expect(
+ filtered_post_ids("post_type:reply", guardian: admin.guardian, scope: scope),
+ ).to contain_exactly(reply_post.id)
+ expect(
+ filtered_post_ids(
+ "post_type:small_action OR username:#{user.username}",
+ guardian: admin.guardian,
+ scope: scope,
+ ),
+ ).to contain_exactly(small_action_post.id, regular_post.id, reply_post.id)
+ end
+
+ it "keeps post_type all within guardian visibility" do
+ SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff].to_s
+
+ topic = Fabricate(:topic, user: user, category: announcement_category)
+ regular_post = Fabricate(:post, topic: topic, user: user)
+ small_action_post =
+ Fabricate(:post, topic: topic, user: admin, post_type: Post.types[:small_action])
+ hidden_post = Fabricate(:post, topic: topic, user: user2)
+ hidden_post.update_columns(hidden: true)
+ whisper_post = Fabricate(:post, topic: topic, user: admin, post_type: Post.types[:whisper])
+ secure_category = Fabricate(:private_category, group: Fabricate(:group))
+ secure_topic = Fabricate(:topic, user: user, category: secure_category)
+ secure_post = Fabricate(:post, topic: secure_topic, user: user)
+ pm_topic = Fabricate(:private_message_topic, user: user, recipient: user2)
+ pm_post = Fabricate(:post, topic: pm_topic, user: user)
+ scope =
+ Post.where(
+ id: [
+ regular_post.id,
+ small_action_post.id,
+ hidden_post.id,
+ whisper_post.id,
+ secure_post.id,
+ pm_post.id,
+ ],
+ )
+
+ expect(
+ filtered_post_ids("post_type:all", guardian: user.guardian, scope: scope),
+ ).to contain_exactly(regular_post.id, small_action_post.id)
+ end
+
+ it "filters posts by listed, unlisted, deleted, and public topic statuses" do
+ listed_topic = Fabricate(:topic, user: user, category: announcement_category)
+ listed_post = Fabricate(:post, topic: listed_topic, user: user)
+ unlisted_topic = Fabricate(:topic, user: user, category: announcement_category, visible: false)
+ unlisted_post = Fabricate(:post, topic: unlisted_topic, user: user)
+ deleted_topic = Fabricate(:topic, user: user, category: announcement_category)
+ deleted_post = Fabricate(:post, topic: deleted_topic, user: user)
+ PostDestroyer.new(admin, deleted_post, context: "spec").destroy
+
+ status_scope = Post.where(id: [listed_post.id, unlisted_post.id, deleted_post.id])
+
+ expect(
+ filtered_post_ids("status:listed", guardian: admin.guardian, scope: status_scope),
+ ).to contain_exactly(listed_post.id)
+ expect(
+ filtered_post_ids("status:unlisted", guardian: admin.guardian, scope: status_scope),
+ ).to contain_exactly(unlisted_post.id)
+ expect(
+ filtered_post_ids("status:deleted", guardian: admin.guardian, scope: status_scope),
+ ).to contain_exactly(deleted_post.id)
+ expect(
+ filtered_post_ids("status:deleted", guardian: user.guardian, scope: status_scope),
+ ).to be_empty
+
+ secure_group = Fabricate(:group)
+ secure_group.add(user)
+ secure_category = Fabricate(:private_category, group: secure_group)
+ secure_topic = Fabricate(:topic, user: user, category: secure_category)
+ secure_post = Fabricate(:post, topic: secure_topic, user: user)
+ uncategorized_topic = Fabricate(:topic, user: user, category: nil)
+ uncategorized_post = Fabricate(:post, topic: uncategorized_topic, user: user)
+ public_scope = Post.where(id: [listed_post.id, secure_post.id, uncategorized_post.id])
+
+ expect(filtered_post_ids("", guardian: user.guardian, scope: public_scope)).to contain_exactly(
+ listed_post.id,
+ secure_post.id,
+ uncategorized_post.id,
+ )
+ expect(
+ filtered_post_ids("status:public", guardian: user.guardian, scope: public_scope),
+ ).to contain_exactly(listed_post.id, uncategorized_post.id)
+
+ deleted_reply = Fabricate(:post, topic: listed_topic, user: user)
+ PostDestroyer.new(admin, deleted_reply, context: "spec").destroy
+
+ or_scope = Post.where(id: [listed_post.id, secure_post.id, deleted_post.id, deleted_reply.id])
+ expect(
+ filtered_post_ids(
+ "status:deleted OR status:public",
+ guardian: admin.guardian,
+ scope: or_scope,
+ ),
+ ).to contain_exactly(deleted_post.id, listed_post.id)
+ end
+
it "orders posts" do
feature_post.update_columns(created_at: 3.days.ago, like_count: 1)
bug_post.update_columns(created_at: 2.days.ago, like_count: 5)