A platform for community discussion. Free, open, simple.
FEATURE: Display automation scripts in the AI features page (#34254)
Roman Rizzi01875b3e1867
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjsnew file mode 100644index 00000000000..67e83c5d103--- /dev/null+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjs@@ -0,0 +1,182 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { concat } from "@ember/helper";+import { action } from "@ember/object";+import { LinkTo } from "@ember/routing";+import DButton from "discourse/components/d-button";+import { i18n } from "discourse-i18n";++class ExpandableList extends Component {+ @tracked isExpanded = false;++ get maxItemsToShow() {+ return this.args.maxItemsToShow ?? 5;+ }++ get hasMore() {+ return this.args.items?.length > this.maxItemsToShow;+ }++ get visibleItems() {+ if (!this.args.items) {+ return [];+ }+ return this.isExpanded+ ? this.args.items+ : this.args.items.slice(0, this.maxItemsToShow);+ }++ get remainingCount() {+ return this.args.items?.length - this.maxItemsToShow;+ }++ get expandToggleLabel() {+ if (this.isExpanded) {+ return i18n("discourse_ai.features.collapse_list");+ } else {+ return i18n("discourse_ai.features.expand_list", {+ count: this.remainingCount,+ });+ }+ }++ @action+ toggleExpanded() {+ this.isExpanded = !this.isExpanded;+ }++ @action+ isLastItem(index) {+ return index === this.visibleItems.length - 1;+ }++ <template>+ {{#each this.visibleItems as |item index|}}+ {{yield item index this.isLastItem}}+ {{/each}}++ {{#if this.hasMore}}+ <DButton+ class="btn-flat ai-expanded-list__toggle-button"+ @translatedLabel={{this.expandToggleLabel}}+ @action={{this.toggleExpanded}}+ />+ {{/if}}+ </template>+}++export default class AiFeatureCard extends Component {+ @action+ hasGroups(feature) {+ return this.groupList(feature).length > 0;+ }++ @action+ groupList(feature) {+ const groups = [];+ const groupIds = new Set();+ if (feature.personas) {+ feature.personas.forEach((persona) => {+ if (persona.allowed_groups) {+ persona.allowed_groups.forEach((group) => {+ if (!groupIds.has(group.id)) {+ groupIds.add(group.id);+ groups.push(group);+ }+ });+ }+ });+ }+ return groups;+ }++ <template>+ <div+ class="admin-section-landing-item ai-feature-card"+ data-feature-name={{@feature.name}}+ >+ <div class="admin-section-landing-item__content">+ <div class="ai-feature-card__feature-name">+ {{@localizedFeatureName}}+ {{#unless @feature.enabled}}+ <span>{{i18n "discourse_ai.features.disabled"}}</span>+ {{/unless}}+ </div>+ <div class="ai-feature-card__persona">+ <span class="ai-feature-card__label">+ {{i18n+ "discourse_ai.features.persona"+ }}+ </span>+ {{#if @feature.personas}}+ <ExpandableList+ @items={{@feature.personas}}+ @maxItemsToShow={{5}}+ as |persona index isLastItem|+ >+ <LinkTo+ @route="adminPlugins.show.discourse-ai-personas.edit"+ @model={{persona.id}}+ class="ai-feature-card__persona-link"+ >+ {{concat persona.name (unless (isLastItem index) ", ")}}+ </LinkTo>+ </ExpandableList>+ {{else}}+ <span class="ai-feature-card__label">+ {{i18n "discourse_ai.features.no_persona"}}+ </span>+ {{/if}}+ </div>+ <div class="ai-feature-card__llm">+ {{#if @feature.llm_models}}+ <span class="ai-feature-card__label">+ {{i18n+ "discourse_ai.features.llm"+ [email protected]_models.length+ }}+ </span>+ <ExpandableList+ @items={{@feature.llm_models}}+ @maxItemsToShow={{5}}+ as |llm index isLastItem|+ >+ <LinkTo+ @route="adminPlugins.show.discourse-ai-llms.edit"+ @model={{llm.id}}+ class="ai-feature-card__llm-link"+ >+ {{concat llm.name (unless (isLastItem index) ", ")}}+ </LinkTo>+ </ExpandableList>+ {{else}}+ <span class="ai-feature-card__label">+ {{i18n "discourse_ai.features.no_llm"}}+ </span>+ {{/if}}+ </div>+ {{#if @showGroups}}+ {{#if @feature.personas}}+ <div class="ai-feature-card__groups">+ <span class="ai-feature-card__label">+ {{i18n "discourse_ai.features.groups"}}+ </span>+ {{#if (this.hasGroups @feature)}}+ <ul class="ai-feature-card__item-groups">+ {{#each (this.groupList @feature) as |group|}}+ <li>{{group.name}}</li>+ {{/each}}+ </ul>+ {{else}}+ <span class="ai-feature-card__label">+ {{i18n "discourse_ai.features.no_groups"}}+ </span>+ {{/if}}+ </div>+ {{/if}}+ {{/if}}+ </div>+ </div>+ </template>+}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-features-list.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-features-list.gjsindex c80da869c6c..6c829c8ebd7 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-features-list.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-features-list.gjs@@ -1,71 +1,15 @@import Component from "@glimmer/component";-import { tracked } from "@glimmer/tracking";import { concat } from "@ember/helper";import { action } from "@ember/object";-import { LinkTo } from "@ember/routing";+import { service } from "@ember/service";+import { and, eq, not } from "truth-helpers";import DButton from "discourse/components/d-button";import { i18n } from "discourse-i18n";--class ExpandableList extends Component {- @tracked isExpanded = false;-- get maxItemsToShow() {- return this.args.maxItemsToShow ?? 5;- }-- get hasMore() {- return this.args.items?.length > this.maxItemsToShow;- }-- get visibleItems() {- if (!this.args.items) {- return [];- }- return this.isExpanded- ? this.args.items- : this.args.items.slice(0, this.maxItemsToShow);- }-- get remainingCount() {- return this.args.items?.length - this.maxItemsToShow;- }-- get expandToggleLabel() {- if (this.isExpanded) {- return i18n("discourse_ai.features.collapse_list");- } else {- return i18n("discourse_ai.features.expand_list", {- count: this.remainingCount,- });- }- }-- @action- toggleExpanded() {- this.isExpanded = !this.isExpanded;- }-- @action- isLastItem(index) {- return index === this.visibleItems.length - 1;- }-- <template>- {{#each this.visibleItems as |item index|}}- {{yield item index this.isLastItem}}- {{/each}}-- {{#if this.hasMore}}- <DButton- class="btn-flat ai-expanded-list__toggle-button"- @translatedLabel={{this.expandToggleLabel}}- @action={{this.toggleExpanded}}- />- {{/if}}- </template>-}+import AiFeatureCard from "./ai-feature-card";export default class AiFeaturesList extends Component {+ @service router;+get sortedModules() {const modules = this.args.modules?.toArray() || this.args.modules;return [...(modules || [])].toSorted((a, b) =>@@ -79,27 +23,27 @@ export default class AiFeaturesList extends Component {}@action- hasGroups(feature) {- return this.groupList(feature).length > 0;+ isAutomationModule(aModule) {+ return (+ aModule.module_name === "automation_reports" ||+ aModule.module_name === "automation_triage"+ );}@action- groupList(feature) {- const groups = [];- const groupIds = new Set();- if (feature.personas) {- feature.personas.forEach((persona) => {- if (persona.allowed_groups) {- persona.allowed_groups.forEach((group) => {- if (!groupIds.has(group.id)) {- groupIds.add(group.id);- groups.push(group);- }- });- }- });+ featureName(feature, aModule) {+ if (this.isAutomationModule(aModule)) {+ return feature.name;+ } else {+ return i18n(+ `discourse_ai.features.${aModule.module_name}.${feature.name}`+ );}- return groups;+ }++ @action+ transitionToAutomations() {+ this.router.transitionTo("/admin/plugins/automation/automation");}<template>@@ -117,6 +61,12 @@ export default class AiFeaturesList extends Component {@label="discourse_ai.features.edit"@route="adminPlugins.show.discourse-ai-spam"/>+ {{else if (this.isAutomationModule module)}}+ <DButton+ class="edit"+ @label="discourse_ai.features.edit"+ @action={{this.transitionToAutomations}}+ />{{else}}<DButtonclass="edit"@@ -134,107 +84,21 @@ export default class AiFeaturesList extends Component {</div><div class="admin-section-landing-wrapper ai-feature-cards">- {{#each module.features as |feature|}}- <div- class="admin-section-landing-item ai-feature-card"- data-feature-name={{feature.name}}- >- <div class="admin-section-landing-item__content">- <div class="ai-feature-card__feature-name">- {{i18n- (concat- "discourse_ai.features."- module.module_name- "."- feature.name- )- }}- {{#unless feature.enabled}}- <span>{{i18n "discourse_ai.features.disabled"}}</span>- {{/unless}}- </div>- <div class="ai-feature-card__persona">- <span class="ai-feature-card__label">- {{i18n- "discourse_ai.features.persona"- count=feature.personas.length- }}- </span>- {{#if feature.personas}}- <ExpandableList- @items={{feature.personas}}- @maxItemsToShow={{5}}- as |persona index isLastItem|- >- <LinkTo- @route="adminPlugins.show.discourse-ai-personas.edit"- @model={{persona.id}}- class="ai-feature-card__persona-link"- >- {{concat- persona.name- (unless (isLastItem index) ", ")- }}- </LinkTo>- </ExpandableList>- {{else}}- <span class="ai-feature-card__label">- {{i18n "discourse_ai.features.no_persona"}}- </span>- {{/if}}- </div>- <div class="ai-feature-card__llm">- {{#if feature.llm_models}}- <span class="ai-feature-card__label">- {{i18n- "discourse_ai.features.llm"- count=feature.llm_models.length- }}- </span>- {{/if}}- {{#if feature.llm_models}}- <ExpandableList- @items={{feature.llm_models}}- @maxItemsToShow={{5}}- as |llm index isLastItem|- >- <LinkTo- @route="adminPlugins.show.discourse-ai-llms.edit"- @model={{llm.id}}- class="ai-feature-card__llm-link"- >- {{concat llm.name (unless (isLastItem index) ", ")}}- </LinkTo>- </ExpandableList>- {{else}}- <span class="ai-feature-card__label">- {{i18n "discourse_ai.features.no_llm"}}- </span>- {{/if}}- </div>- {{#unless (this.isSpamModule module)}}- {{#if feature.personas}}- <div class="ai-feature-card__groups">- <span class="ai-feature-card__label">- {{i18n "discourse_ai.features.groups"}}- </span>- {{#if (this.hasGroups feature)}}- <ul class="ai-feature-card__item-groups">- {{#each (this.groupList feature) as |group|}}- <li>{{group.name}}</li>- {{/each}}- </ul>- {{else}}- <span class="ai-feature-card__label">- {{i18n "discourse_ai.features.no_groups"}}- </span>- {{/if}}- </div>- {{/if}}- {{/unless}}- </div>- </div>- {{/each}}+ {{#if+ (and+ (this.isAutomationModule module) (eq module.features.length 0)+ )+ }}+ <div>{{i18n "discourse_ai.features.no_automations"}}</div>+ {{else}}+ {{#each module.features as |feature|}}+ <AiFeatureCard+ @localizedFeatureName={{this.featureName feature module}}+ @feature={{feature}}+ @showGroups={{not (this.isSpamModule module)}}+ />+ {{/each}}+ {{/if}}</div></div>{{/each}}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex d0adfa464b1..5a64adbd111 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -217,6 +217,9 @@ en:text: "Search features, personas, LLMs, or groups..."no_results: "No features found matching your filters."reset: "Reset"++ no_automations: "There are no automations yet"+summarization:name: "Summaries"description: "Makes a summarization button available that allows visitors to summarize topics"@@ -268,6 +271,14 @@ en:description: "Identifies potential spam using the selected LLM and flags it for site moderators to inspect in the review queue"inspect_posts: "Inspect posts"+ automation_reports:+ name: "Automated reports"+ description: "Automatically creates concise overviews of activity, highlighting trends, key topics, and engagement"++ automation_triage:+ name: "Posts triage"+ description: "Automates moderation and management of posts by using configurable automation rules"+modals:select_option: "Select an option..."diff --git a/plugins/discourse-ai/lib/configuration/feature.rb b/plugins/discourse-ai/lib/configuration/feature.rbindex f250010c269..52a81588eee 100644--- a/plugins/discourse-ai/lib/configuration/feature.rb+++ b/plugins/discourse-ai/lib/configuration/feature.rb@@ -203,6 +203,82 @@ module DiscourseAi]end+ def ai_automation_report_scripts+ return [] if !SiteSetting.discourse_automation_enabled++ feature_cache[:automation_reports] ||= begin+ all_script_fields = DB.query(<<~SQL)+ SELECT (fields.metadata->>'value') AS value, automations.name AS automation_name, fields.name AS name+ FROM discourse_automation_fields fields+ INNER JOIN discourse_automation_automations automations ON automations.id = fields.automation_id+ WHERE fields.name IN ('model', 'persona_id')+ AND automations.script = 'llm_report'+ AND automations.enabled+ LIMIT 20+ SQL++ all_script_fields =+ all_script_fields+ .take(10)+ .reduce({}) do |memo, field|+ memo[field.automation_name] = {} if memo[field.automation_name].nil?++ memo[field.automation_name][field.name] = field.value++ memo+ end++ all_script_fields.map do |automation_name, fields|+ new(+ automation_name,+ nil,+ DiscourseAi::Configuration::Module::AUTOMATION_REPORTS_ID,+ DiscourseAi::Configuration::Module::AUTOMATION_REPORTS,+ persona_ids_lookup: -> { [fields.dig("persona_id")].compact.map(&:to_i) },+ llm_models_lookup: -> { [LlmModel.find_by(id: fields["model"])].compact },+ )+ end+ end+ end++ def ai_automation_triage_scripts+ return [] if !SiteSetting.discourse_automation_enabled++ feature_cache[:automation_triage] ||= begin+ all_script_fields = DB.query(<<~SQL)+ SELECT (fields.metadata->>'value') AS value, automations.name AS automation_name, fields.name AS name+ FROM discourse_automation_fields fields+ INNER JOIN discourse_automation_automations automations ON automations.id = fields.automation_id+ WHERE fields.name IN ('model', 'triage_persona')+ AND automations.script = 'llm_triage'+ AND automations.enabled+ LIMIT 20+ SQL++ all_script_fields =+ all_script_fields.reduce({}) do |memo, field|+ memo[field.automation_name] = {} if memo[field.automation_name].nil?++ memo[field.automation_name][field.name] = field.value++ memo+ end++ all_script_fields+ .take(10)+ .map do |automation_name, field|+ new(+ automation_name,+ nil,+ DiscourseAi::Configuration::Module::AUTOMATION_TRIAGE_ID,+ DiscourseAi::Configuration::Module::AUTOMATION_TRIAGE,+ persona_ids_lookup: -> { [field.dig("triage_persona")].compact.map(&:to_i) },+ llm_models_lookup: -> { [LlmModel.find_by(id: field["model"])].compact },+ )+ end+ end+ end+def all[summarization_features,@@ -214,6 +290,8 @@ module DiscourseAibot_features,spam_features,embeddings_features,+ ai_automation_report_scripts,+ ai_automation_triage_scripts,].flattenenddiff --git a/plugins/discourse-ai/lib/configuration/module.rb b/plugins/discourse-ai/lib/configuration/module.rbindex 2d3a26b622c..84e7db9a498 100644--- a/plugins/discourse-ai/lib/configuration/module.rb+++ b/plugins/discourse-ai/lib/configuration/module.rb@@ -12,6 +12,8 @@ module DiscourseAiBOT = "bot"SPAM = "spam"EMBEDDINGS = "embeddings"+ AUTOMATION_REPORTS = "automation_reports"+ AUTOMATION_TRIAGE = "automation_triage"NAMES = [SUMMARIZATION,@@ -23,6 +25,8 @@ module DiscourseAiBOT,SPAM,EMBEDDINGS,+ AUTOMATION_REPORTS,+ AUTOMATION_TRIAGE,].freezeSUMMARIZATION_ID = 1@@ -34,10 +38,12 @@ module DiscourseAiBOT_ID = 7SPAM_ID = 8EMBEDDINGS_ID = 9+ AUTOMATION_REPORTS_ID = 10+ AUTOMATION_TRIAGE_ID = 11class << selfdef all- [+ base_modules = [new(SUMMARIZATION_ID,SUMMARIZATION,@@ -95,6 +101,36 @@ module DiscourseAiextra_check: -> { SiteSetting.ai_embeddings_semantic_search_enabled },),]++ if SiteSetting.discourse_automation_enabled+ base_modules << new(+ AUTOMATION_REPORTS_ID,+ AUTOMATION_REPORTS,+ enabled_by_setting: "discourse_automation_enabled",+ features: DiscourseAi::Configuration::Feature.ai_automation_report_scripts,+ extra_check: -> { has_scripts?("llm_report") },+ )+ base_modules << new(+ AUTOMATION_TRIAGE_ID,+ AUTOMATION_TRIAGE,+ enabled_by_setting: "discourse_automation_enabled",+ features: DiscourseAi::Configuration::Feature.ai_automation_triage_scripts,+ extra_check: -> { has_scripts?("llm_triage") },+ )+ end++ base_modules+ end++ # Private+ def has_scripts?(script_name)+ DB+ .query_single(+ "SELECT COUNT(*) FROM discourse_automation_automations WHERE script = :name and enabled",+ name: script_name,+ )+ .first+ .to_i > 0enddef find_by(id:)diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex 455e8b14732..276d00e1ad4 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -146,4 +146,8 @@ after_initialize doface-angry]plugin_icons.each { |icon| register_svg_icon(icon) }++ add_model_callback(DiscourseAutomation::Automation, :after_save) do+ DiscourseAi::Configuration::Feature.feature_cache.flush!+ endenddiff --git a/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rbindex d77c30d72af..3326df4f770 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb@@ -22,6 +22,15 @@ RSpec.describe DiscourseAi::Admin::AiFeaturesController doexpect(response.status).to eq(200)expect(response.parsed_body["ai_features"].count).to eq(9)end++ it "includes automation-related features" do+ SiteSetting.discourse_automation_enabled = true++ get "/admin/plugins/discourse-ai/ai-features.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["ai_features"].count).to eq(11)+ endenddescribe "#edit" do