Skip to content

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

FEATURE: Add configurable sentiment classifiers (#44941)

Sam589325e2b846
diff --git a/.skills/discourse-migration/SKILL.md b/.skills/discourse-migration/SKILL.md
index 9931ee48ecf..9294cbb90fe 100644
--- a/.skills/discourse-migration/SKILL.md
+++ b/.skills/discourse-migration/SKILL.md
@@ -329,14 +329,14 @@ The same pattern works for plugin migrations — just adjust the `require` path
## Regenerating structure.sql and annotations
-After any schema-altering migration (columns, tables, indexes), regenerate both `db/structure.sql` and model annotations. CI fails if either is stale.
+After every migration change, including data-only migrations:
```bash
-bin/rake db:dump_structure # rewrites db/structure.sql from a clean migrated DB
-bin/rake annotate:clean # rewrites all model annotations (core + bundled plugins)
+bin/rake db:dump_structure # includes migration versions
+bin/rake db:check_structure_dump # checks schema, unexpected rows, and advanced sequences
```
-Both tasks spin up a disposable database and load all bundled plugins, so output is invariant to the local `plugins/` directory. Commit the regenerated files alongside the migration.
+After schema changes, also run `bin/rake annotate:clean`. These tasks use disposable databases with all bundled plugins. Include regenerated files with the migration.
## Review checklist
@@ -353,7 +353,7 @@ Both tasks spin up a disposable database and load all bundled plugins, so output
11. No application code (models, `SiteSetting`) — query DB directly
12. `execute` for SQL; `DB.exec`/`DB.query` only when param binding or return values needed
13. No rows inserted on fresh installs — seed data lives in `db/fixtures/`; inserts that exist only to preserve behavior on upgrades are gated on `Migration::Helpers.existing_site?`
-14. After schema-altering migrations, `bin/rake db:dump_structure` and `bin/rake annotate:clean` were run and the regenerated files committed
+14. Structure dump regenerated and checked for all migrations; annotations regenerated for schema changes
15. Transaction-disabled migrations (`disable_ddl_transaction!`) contain only the operation that requires it (concurrent index or batched backfill), no other schema changes
16. Data the new code or its seeds need at boot is changed pre-deploy, and renamed tables get a view with the old name
17. Pre-deploy migrations don't depend on post-deploy ones (promote those early if needed)
diff --git a/db/structure.sql b/db/structure.sql
index 1f5c71ac8f9..e0f5b609aac 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -26166,6 +26166,7 @@ SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20261021063131'),
('20261021063122'),
+('20261021061850'),
('20261021060627'),
('20261021060617'),
('20261020161054'),
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-features.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-features.gjs
index cb04558222d..de10a61e9a1 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-features.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-features.gjs
@@ -95,7 +95,20 @@ export default class AiFeatures extends Component {
)
);
- return agentMatches || llmMatches || groupMatches;
+ const decisionMatches = feature.decision_model?.display_name
+ ?.toLowerCase()
+ .includes(term);
+ const classificationMatches = feature.classification_model?.model_name
+ ?.toLowerCase()
+ .includes(term);
+
+ return (
+ agentMatches ||
+ llmMatches ||
+ groupMatches ||
+ decisionMatches ||
+ classificationMatches
+ );
} catch (error) {
// eslint-disable-next-line no-console
console.error(`Error filtering features`, error);
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-model-new.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-model-new.gjs
index 86cbf39b260..8a26a78a038 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-model-new.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-model-new.gjs
@@ -24,7 +24,7 @@ export default class AiModelNew extends Component {
@service router;
get types() {
- return modelTypes(this.args.decisionModels);
+ return modelTypes();
}
get selectedType() {
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-models-list.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-models-list.gjs
index 6f39212fdfe..74999710b77 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-models-list.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-models-list.gjs
@@ -17,11 +17,8 @@ import AiLlmUsages from "./ai-llm-usages.gjs";
const ALL = "all";
-// Decision models only exist where workflows are enabled, so the route passes none otherwise.
-export function modelTypes(decisionModels) {
- return decisionModels
- ? ["llm", "embedding", "decision"]
- : ["llm", "embedding"];
+export function modelTypes() {
+ return ["llm", "embedding", "decision"];
}
function llmDescription(llm) {
@@ -49,7 +46,7 @@ export default class AiModelsList extends Component {
@tracked filterValue = "";
get types() {
- return modelTypes(this.args.decisionModels);
+ return modelTypes();
}
get selectedType() {
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/controllers/admin-plugins/show/discourse-ai-features/edit.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/controllers/admin-plugins/show/discourse-ai-features/edit.js
index e0b7c5594ff..f2052a60465 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/controllers/admin-plugins/show/discourse-ai-features/edit.js
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/controllers/admin-plugins/show/discourse-ai-features/edit.js
@@ -2,9 +2,24 @@ import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import SiteSetting from "discourse/admin/models/site-setting";
+import SiteSetting, {
+ isSettingValueTrue,
+} from "discourse/admin/models/site-setting";
import { i18n } from "discourse-i18n";
+const SENTIMENT_SETTING_LABELS = {
+ ai_sentiment_enabled: "enabled",
+ ai_sentiment_emotion_enabled: "enabled",
+ ai_sentiment_sentiment_classification_strategy: "backend",
+ ai_sentiment_sentiment_agent: "agent",
+ ai_sentiment_sentiment_decision_model: "decision_model",
+ ai_sentiment_emotion_classification_strategy: "backend",
+ ai_sentiment_emotion_agent: "agent",
+ ai_sentiment_emotion_decision_model: "decision_model",
+ ai_sentiment_backfill_maximum_posts_per_hour: "posts_per_hour",
+ ai_sentiment_backfill_post_max_age_days: "post_age_limit",
+};
+
export default class AdminPluginsShowDiscourseAiFeaturesEditController extends Controller {
@service toasts;
@service router;
@@ -21,6 +36,82 @@ export default class AdminPluginsShowDiscourseAiFeaturesEditController extends C
return this.settings.find((s) => s.setting === settingName);
}
+ @action
+ settingDefinition(setting) {
+ const labelKey = SENTIMENT_SETTING_LABELS[setting.setting];
+ if (!labelKey) {
+ return setting.definition;
+ }
+
+ const definition = {
+ ...setting.definition,
+ label: i18n(`discourse_ai.features.sentiment.${labelKey}`),
+ };
+
+ switch (labelKey) {
+ case "backend":
+ definition.description = undefined;
+ definition.valid_values = definition.valid_values.map((choice) => {
+ const value = typeof choice === "object" ? choice.value : choice;
+ return {
+ value,
+ name: i18n(`discourse_ai.features.backends.${value}`),
+ };
+ });
+ break;
+ case "agent":
+ definition.description = undefined;
+ break;
+ case "decision_model":
+ definition.description =
+ setting.setting === "ai_sentiment_emotion_decision_model"
+ ? i18n("discourse_ai.features.sentiment.emotion_scores")
+ : undefined;
+ break;
+ case "posts_per_hour":
+ definition.description = i18n(
+ "discourse_ai.features.sentiment.posts_per_hour_description"
+ );
+ break;
+ }
+
+ return definition;
+ }
+
+ @action
+ sentimentBackendGroup(data, backend) {
+ return [
+ ["emotion", "ai_sentiment_emotion_enabled"],
+ ["sentiment", "ai_sentiment_enabled"],
+ ].find(
+ ([type, setting]) =>
+ isSettingValueTrue(data[setting]) &&
+ data[`ai_sentiment_${type}_classification_strategy`] === backend
+ )?.[0];
+ }
+
+ @action
+ isSettingVisible(setting, data) {
+ if (!setting) {
+ return false;
+ }
+ if (setting.depends_behavior !== "hidden") {
+ return true;
+ }
+
+ return (setting.depends_on || []).every((name) => {
+ const parent = this.findSetting(name);
+ if (!parent) {
+ return true;
+ }
+ const value = Object.hasOwn(data, name) ? data[name] : parent.value;
+ const allowedValues = setting.depends_on_values?.[name];
+ return allowedValues
+ ? allowedValues.map(String).includes(String(value))
+ : isSettingValueTrue(value);
+ });
+ }
+
@action
async save(data) {
if (this.formApi) {
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-models.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-models.js
index fa7df65dc9a..c5efb02501a 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-models.js
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-models.js
@@ -1,16 +1,11 @@
-import { service } from "@ember/service";
import DiscourseRoute from "discourse/routes/discourse";
export default class DiscourseAiModelsRoute extends DiscourseRoute {
- @service siteSettings;
-
async model() {
const [llms, embeddings, decisionModels] = await Promise.all([
this.store.findAll("ai-llm"),
this.store.findAll("ai-embedding"),
- this.siteSettings.enable_discourse_workflows
- ? this.store.findAll("ai-decision-model")
- : null,
+ this.store.findAll("ai-decision-model"),
]);
return { llms, embeddings, decisionModels };
}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-features/edit.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-features/edit.gjs
index a6e182a2baf..287b8734646 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-features/edit.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-features/edit.gjs
@@ -1,4 +1,5 @@
-import { concat } from "@ember/helper";
+import { concat, hash } from "@ember/helper";
+import { LinkTo } from "@ember/routing";
import AdminConfigAreaCard from "discourse/admin/components/admin-config-area-card";
import BackButton from "discourse/components/back-button";
import Form from "discourse/components/form";
@@ -23,19 +24,26 @@ export default <template>
@data={{@model.formData}}
@onRegisterApi={{@controller.onRegisterFormApi}}
@onSubmit={{@controller.save}}
- as |form|
+ as |form data|
>
{{#if @model.settingGroups.length}}
{{#each @model.settingGroups as |group|}}
<form.Section @title={{i18n group.titleKey}}>
+ {{#if group.descriptionKey}}
+ <p class="ai-feature-editor__section-description">
+ {{i18n group.descriptionKey}}
+ </p>
+ {{/if}}
{{#each group.settings as |settingName|}}
{{#let
(@controller.findSetting settingName)
as |setting|
}}
- {{#if setting}}
+ {{#if (@controller.isSettingVisible setting data)}}
<SettingDefinitionField
- @definition={{setting.definition}}
+ @definition={{@controller.settingDefinition
+ setting
+ }}
@disabled={{setting.disabled}}
@form={{form}}
/>
@@ -43,14 +51,65 @@ export default <template>
{{/let}}
{{/each}}
</form.Section>
+ {{#if (eq @model.module_name "sentiment")}}
+ {{#if
+ (eq
+ group.key
+ (@controller.sentimentBackendGroup
+ data "decision_model"
+ )
+ )
+ }}
+ <p class="ai-feature-editor__model-change-warning">
+ {{i18n
+ "discourse_ai.features.sentiment.model_change_warning"
+ }}
+ </p>
+ <LinkTo
+ class="ai-feature-editor__decision-models-link"
+ @query={{hash type="decision"}}
+ @route="adminPlugins.show.discourse-ai-models.index"
+ >
+ {{i18n
+ "discourse_ai.features.sentiment.manage_decision_models"
+ }}
+ </LinkTo>
+ {{/if}}
+ {{#if
+ (eq
+ group.key
+ (@controller.sentimentBackendGroup
+ data "classification_model"
+ )
+ )
+ }}
+ <p class="ai-feature-editor__dedicated-description">
+ {{i18n
+ "discourse_ai.features.sentiment.dedicated_description"
+ }}
+ <LinkTo
+ class="ai-feature-editor__dedicated-link"
+ @model="all_results"
+ @query={{hash filter="ai_sentiment_model_configs"}}
+ @route="adminSiteSettingsCategory"
+ >
+ {{i18n
+ "discourse_ai.features.sentiment.configure_dedicated"
+ }}
+ </LinkTo>
+ </p>
+ {{/if}}
+ {{/if}}
{{/each}}
{{else}}
{{#each @model.feature_settings as |setting|}}
- <SettingDefinitionField
- @definition={{setting.definition}}
- @disabled={{setting.disabled}}
- @form={{form}}
- />
+ {{#if (@controller.isSettingVisible setting data)}}
+ <SettingDefinitionField
+ @definition={{@controller.settingDefinition setting}}
+ @disabled={{setting.disabled}}
+ @form={{form}}
+ />
+ {{/if}}
{{/each}}
{{/if}}
<form.Actions>
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_features_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_features_controller.rb
index faacce73bd7..ea3a851f577 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_features_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_features_controller.rb
@@ -45,7 +45,7 @@ module DiscourseAi
{ id: llm_model.id, name: llm_model.display_name }
end,
enabled: feature.enabled?,
- }
+ }.merge(feature.metadata)
end
def serialize_agent(agent)
diff --git a/plugins/discourse-ai/app/jobs/regular/post_sentiment_analysis.rb b/plugins/discourse-ai/app/jobs/regular/post_sentiment_analysis.rb
index 768a1d37c47..d2e91016467 100644
--- a/plugins/discourse-ai/app/jobs/regular/post_sentiment_analysis.rb
+++ b/plugins/discourse-ai/app/jobs/regular/post_sentiment_analysis.rb
@@ -5,7 +5,7 @@ module Jobs
sidekiq_options queue: "low"
def execute(args)
- return unless SiteSetting.ai_sentiment_enabled
+ return unless DiscourseAi::Sentiment::PostClassification.any_enabled?
return if (post_id = args[:post_id]).blank?
post = Post.find_by(id: post_id, post_type: Post.types[:regular])
diff --git a/plugins/discourse-ai/app/jobs/scheduled/sentiment_backfill.rb b/plugins/discourse-ai/app/jobs/scheduled/sentiment_backfill.rb
index 5f952676498..ca5e7e361e7 100644
--- a/plugins/discourse-ai/app/jobs/scheduled/sentiment_backfill.rb
+++ b/plugins/discourse-ai/app/jobs/scheduled/sentiment_backfill.rb
@@ -6,7 +6,7 @@ module Jobs
cluster_concurrency 1
def execute(_args)
- return if !SiteSetting.ai_sentiment_enabled
+ return if !DiscourseAi::Sentiment::PostClassification.any_enabled?
base_budget = SiteSetting.ai_sentiment_backfill_maximum_posts_per_hour
return if base_budget.zero?
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.gjs
index a6af00245dc..9ec7b6ead15 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjs
@@ -1,6 +1,6 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
-import { concat } from "@ember/helper";
+import { concat, hash } from "@ember/helper";
import { action } from "@ember/object";
import { LinkTo } from "@ember/routing";
import GroupLink from "discourse/components/group-link";
@@ -68,6 +68,14 @@ class ExpandableList extends Component {
}
export default class AiFeatureCard extends Component {
+ get showAgentConfiguration() {
+ return !this.args.feature.backend || this.args.feature.backend === "agent";
+ }
+
+ get backendLabel() {
+ return i18n(`discourse_ai.features.backends.${this.args.feature.backend}`);
+ }
+
@action
hasGroups(feature) {
return this.groupList(feature).length > 0;
@@ -104,57 +112,106 @@ export default class AiFeatureCard extends Component {
<span>{{i18n "discourse_ai.features.disabled"}}</span>
{{/unless}}
</div>
- <div class="ai-feature-card__agent">
- <span class="ai-feature-card__label">
- {{i18n "discourse_ai.features.agent" [email protected]}}
- </span>
- {{#if @feature.agents}}
- <ExpandableList
- @items={{@feature.agents}}
- @maxItemsToShow={{5}}
- as |agent index isLastItem|
- >
- <LinkTo
- class="ai-feature-card__agent-link"
- @model={{agent.id}}
- @route="adminPlugins.show.discourse-ai-agents.edit"
- >
- {{concat agent.name (unless (isLastItem index) ", ")}}
- </LinkTo>
- </ExpandableList>
- {{else}}
+ {{#if @feature.backend}}
+ <div class="ai-feature-card__backend">
<span class="ai-feature-card__label">
- {{i18n "discourse_ai.features.no_agent"}}
+ {{i18n "discourse_ai.features.backend"}}
</span>
+ {{this.backendLabel}}
+ </div>
+ {{#if (eq @feature.ready false)}}
+ <div class="ai-feature-card__readiness">
+ {{i18n "discourse_ai.features.not_configured"}}
+ </div>
{{/if}}
- </div>
- <div class="ai-feature-card__llm">
- {{#if @feature.llm_models}}
+ {{/if}}
+ {{#if this.showAgentConfiguration}}
+ <div class="ai-feature-card__agent">
<span class="ai-feature-card__label">
{{i18n
- "discourse_ai.features.llm"
- [email protected]_models.length
+ "discourse_ai.features.agent"
}}
</span>
- <ExpandableList
- @items={{@feature.llm_models}}
- @maxItemsToShow={{5}}
- as |llm index isLastItem|
- >
+ {{#if @feature.agents}}
+ <ExpandableList
+ @items={{@feature.agents}}
+ @maxItemsToShow={{5}}
+ as |agent index isLastItem|
+ >
+ <LinkTo
+ class="ai-feature-card__agent-link"
+ @model={{agent.id}}
+ @route="adminPlugins.show.discourse-ai-agents.edit"
+ >
+ {{concat agent.name (unless (isLastItem index) ", ")}}
+ </LinkTo>
+ </ExpandableList>
+ {{else}}
+ <span class="ai-feature-card__label">
+ {{i18n "discourse_ai.features.no_agent"}}
+ </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
+ class="ai-feature-card__llm-link"
+ @model={{llm.id}}
+ @route="adminPlugins.show.discourse-ai-llms.edit"
+ >
+ {{concat llm.name (unless (isLastItem index) ", ")}}
+ </LinkTo>
+ </ExpandableList>
+ {{else}}
+ <span class="ai-feature-card__label">
+ {{i18n "discourse_ai.features.no_llm"}}
+ </span>
+ {{/if}}
+ </div>
+ {{else if (eq @feature.backend "decision_model")}}
+ {{#if @feature.decision_model}}
+ <div class="ai-feature-card__decision-model">
+ <span class="ai-feature-card__label">
+ {{i18n "discourse_ai.features.decision_model"}}
+ </span>
<LinkTo
- class="ai-feature-card__llm-link"
- @model={{llm.id}}
- @route="adminPlugins.show.discourse-ai-llms.edit"
+ class="ai-feature-card__decision-model-link"
+ @model={{@feature.decision_model.id}}
+ @route="adminPlugins.show.discourse-ai-decision-models.edit"
>
- {{concat llm.name (unless (isLastItem index) ", ")}}
+ {{@feature.decision_model.display_name}}
</LinkTo>
- </ExpandableList>
- {{else}}
- <span class="ai-feature-card__label">
- {{i18n "discourse_ai.features.no_llm"}}
- </span>
+ </div>
{{/if}}
- </div>
+ {{else if (eq @feature.backend "classification_model")}}
+ {{#if @feature.classification_model}}
+ <div class="ai-feature-card__classification-model">
+ <span class="ai-feature-card__label">
+ {{i18n "discourse_ai.features.classification_model"}}
+ </span>
+ <LinkTo
+ class="ai-feature-card__classification-model-link"
+ @model="all_results"
+ @query={{hash filter="ai_sentiment_model_configs"}}
+ @route="adminSiteSettingsCategory"
+ >
+ {{@feature.classification_model.model_name}}
+ </LinkTo>
+ </div>
+ {{/if}}
+ {{/if}}
{{#if @showGroups}}
{{#if @feature.agents}}
<div class="ai-feature-card__groups">
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.gjs
index 5b62899f075..afd2ec01dd0 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
@@ -20,6 +20,11 @@ export default class AiFeaturesList extends Component {
return aModule.module_name === "spam";
}
+ @action
+ showGroups(aModule) {
+ return !["spam", "sentiment"].includes(aModule.module_name);
+ }
+
@action
isAutomationModule(aModule) {
return (
@@ -95,7 +100,7 @@ export default class AiFeaturesList extends Component {
<AiFeatureCard
@feature={{feature}}
@localizedFeatureName={{this.featureName feature module}}
- @showGroups={{not (this.isSpamModule module)}}
+ @showGroups={{this.showGroups module}}
/>
{{/each}}
{{/if}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-dashboard-tabs-after/admin-sentiment-dashbboard.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-dashboard-tabs-after/admin-sentiment-dashbboard.gjs
index 28e05bd7507..97af714df6a 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-dashboard-tabs-after/admin-sentiment-dashbboard.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/admin-dashboard-tabs-after/admin-sentiment-dashbboard.gjs
@@ -4,7 +4,10 @@ import { i18n } from "discourse-i18n";
export default class AISentimentDashboard extends Component {
static shouldRender(args, context) {
- return context.siteSettings.ai_sentiment_enabled;
+ return (
+ context.siteSettings.ai_sentiment_enabled ||
+ context.siteSettings.ai_sentiment_emotion_enabled
+ );
}
<template>
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
index a4607e4b93c..64767bbe4bf 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-feature-setting-groups.js
@@ -136,6 +136,31 @@ export const AI_FEATURE_SETTING_GROUPS = {
},
],
+ sentiment: [
+ ...["sentiment", "emotion"].map((type) => ({
+ key: type,
+ titleKey: `discourse_ai.features.sentiment.setting_groups.${type}`,
+ settings: [
+ type === "sentiment"
+ ? "ai_sentiment_enabled"
+ : "ai_sentiment_emotion_enabled",
+ `ai_sentiment_${type}_classification_strategy`,
+ `ai_sentiment_${type}_agent`,
+ `ai_sentiment_${type}_decision_model`,
+ ],
+ })),
+ {
+ key: "background_processing",
+ titleKey:
+ "discourse_ai.features.sentiment.setting_groups.background_processing",
+ descriptionKey: "discourse_ai.features.sentiment.background_description",
+ settings: [
+ "ai_sentiment_backfill_maximum_posts_per_hour",
+ "ai_sentiment_backfill_post_max_age_days",
+ ],
+ },
+ ],
+
summarization: [
{
key: "settings",
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index de95af8294f..1d186b4e94a 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -79,7 +79,7 @@ en:
fetch_error: "Ask AI metrics could not be loaded."
emotion:
title: "Emotion"
- description: "The table lists a count of posts classified with a determined emotion. Depending on the selected emotion classification method, posts are classified with either the configured emotion classification model ('SamLowe/roberta-base-go_emotions') or the configured emotion classification agent."
+ description: "The table counts posts classified with each emotion using the selected emotion classification backend."
reports:
filters:
group_by:
@@ -389,7 +389,7 @@ en:
features:
short_title: "Features"
- description: "These are the AI features available to visitors on your site. These can be configured to use specific agents and LLMs, and can be access controlled by groups."
+ description: "Configure AI features for your site, including their agents, models, and access permissions where supported."
back: "Back to AI features"
settings_save_error: "Some settings could not be saved."
disabled: "(disabled)"
@@ -403,6 +403,14 @@ en:
no_llm: "No LLM selected"
no_agent: "Not set"
no_groups: "None"
+ backend: "Backend:"
+ backends:
+ agent: "LLM agent"
+ decision_model: "Decision model"
+ classification_model: "Dedicated classification endpoint"
+ decision_model: "Decision model:"
+ classification_model: "Model:"
+ not_configured: "Not configured"
edit: "Settings"
expand_list:
one: "(%{count} more)"
@@ -422,12 +430,34 @@ en:
not_enabled: "Not enabled"
filters:
all: "All"
- text: "Search features, agents, LLMs, or groups..."
+ text: "Search features, agents, models, or groups..."
no_results: "No features found matching your filters."
reset: "Reset"
no_automations: "There are no automations yet"
+ sentiment:
+ name: "Sentiment and emotion"
+ description: "Classifies the sentiment and emotion of posts in the background"
+ sentiment: "Sentiment"
+ emotion: "Emotion"
+ backend: "Backend"
+ enabled: "Enabled"
+ agent: "Agent"
+ decision_model: "Decision model"
+ posts_per_hour: "Posts per hour"
+ post_age_limit: "Post age limit (days)"
+ posts_per_hour_description: "Approximate rate. 0 stops the scan, including new posts. Edited posts are checked separately."
+ background_description: "Checks unclassified posts every 5 minutes, within these shared limits. Each enabled classifier may incur a request per post. Refusals may be retried."
+ emotion_scores: "Scores are distributed across 28 emotions."
+ model_change_warning: "Changing the selected model does not reclassify existing results."
+ dedicated_description: "Dedicated classification endpoints are configured in site settings."
+ configure_dedicated: "Configure dedicated classification endpoints"
+ manage_decision_models: "Manage decision models"
+ setting_groups:
+ sentiment: "Sentiment"
+ emotion: "Emotion"
+ background_processing: "Background processing"
summarization:
name: "Summaries"
description: "Makes a summarization button available that allows visitors to summarize topics"
@@ -1196,7 +1226,7 @@ en:
remove: "Remove header"
edit: "Edit"
delete: "Delete"
- confirm_delete: "Are you sure you want to delete this decision model? Workflows that use it will stop working."
+ confirm_delete: "Are you sure you want to delete this decision model? Features and workflows that use it will stop working."
saved: "Decision model saved"
test: "Run test"
test_success: "The decision model answered the test question (yes: %{percentage}%)."
@@ -1311,7 +1341,7 @@ en:
type_descriptions:
llm: "Generates text for features like the AI bot, summaries, and the AI helper"
embedding: "Turns content into vectors for semantic search and related topics"
- decision: "Answers typed questions about content (yes/no, choice, score) with probabilities, for use in workflows"
+ decision: "Answers typed questions about content (yes/no, choice, score) with probabilities, for use in AI features and workflows"
type_filters:
llm: "Large language models"
embedding: "Embedding models"
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index c81cf1d050b..4a44a0c2bba 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -88,15 +88,20 @@ en:
ai_toxicity_flag_threshold_sexual_explicit: "Sexual Explicit"
ai_toxicity_groups_bypass: "Users on those groups will not have their posts classified by the toxicity module."
- ai_sentiment_enabled: "Enable the sentiment module."
+ ai_sentiment_enabled: "Classify posts as positive, neutral, or negative."
+ ai_sentiment_emotion_enabled: "Classify the emotions expressed in posts."
ai_sentiment_inference_service_api_endpoint: "URL where the API is running for the sentiment module"
ai_sentiment_inference_service_api_key: "API key for the sentiment API"
ai_sentiment_model_configs: "Classification model endpoints to use for inference. Sentiment classifies posts as positive, neutral, or negative. Emotion classifies posts across supported emotion labels."
ai_sentiment_models: "Classification model endpoints to use for inference. Sentiment classifies posts as positive, neutral, or negative. Emotion classifies posts across supported emotion labels."
ai_sentiment_sentiment_classification_strategy: "Sentiment classification backend."
- ai_sentiment_sentiment_agent: "Agent to use for sentiment classification when the sentiment backend is agent."
+ ai_sentiment_sentiment_agent: "Agent for sentiment classification."
+ ai_sentiment_sentiment_decision_model: "Decision model for sentiment classification. Changing it does not reclassify existing results."
+ ai_sentiment_emotion_decision_model: "Decision model for emotion classification. Scores are distributed across 28 emotions. Changing it does not reclassify existing results."
ai_sentiment_emotion_classification_strategy: "Emotion classification backend."
- ai_sentiment_emotion_agent: "Agent to use for emotion classification when the emotion backend is agent."
+ ai_sentiment_emotion_agent: "Agent for emotion classification."
+ ai_sentiment_backfill_maximum_posts_per_hour: "Approximate hourly post limit for the 5-minute scan. Each enabled classifier may incur a request per post. Refusals may be retried. 0 stops scheduled scanning, including new posts; edited posts are checked separately."
+ ai_sentiment_backfill_post_max_age_days: "Only scan posts created within this many days. 0 means today, not unlimited history."
ai_nsfw_detection_enabled: "Enable the NSFW module."
ai_nsfw_inference_service_api_endpoint: "URL where the API is running for the NSFW module"
@@ -217,93 +222,93 @@ en:
description: "This report provides sentiment analysis for posts, grouped by category, with positive, negative, and neutral scores for each post and category."
overall_sentiment:
title: "Overall sentiment"
- description: "The chart compares the number of posts classified as either positive or negative. These are calculated when positive or negative scores > the set threshold score. This means neutral posts are not shown. Personal messages (PMs) are also excluded. Depending on the selected sentiment classification method, posts are classified with either the configured sentiment classification model ('cardiffnlp/twitter-roberta-base-sentiment-latest') or the configured sentiment classification agent."
+ description: "The chart shows the number of positive posts minus the number of negative posts each day. Posts count as positive or negative when their respective score exceeds the threshold. Neutral posts and personal messages (PMs) are excluded. Scores come from the selected sentiment classification backend."
xaxis: "Positive(%)"
yaxis: "Date"
emotion_admiration:
title: 🤩 Admiration
- description: "Posts classified with the emotion admiration via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion admiration using the selected emotion classification backend."
emotion_amusement:
title: 😄 Amusement
- description: "Posts classified with the emotion amusement via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion amusement using the selected emotion classification backend."
emotion_anger:
title: 😠 Anger
- description: "Posts classified with the emotion anger via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion anger using the selected emotion classification backend."
emotion_annoyance:
title: 😒 Annoyance
- description: "Posts classified with the emotion annoyance via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion annoyance using the selected emotion classification backend."
emotion_approval:
title: 👍 Approval
- description: "Posts classified with the emotion approval via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion approval using the selected emotion classification backend."
emotion_caring:
title: 🤗 Caring
- description: "Posts classified with the emotion caring via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion caring using the selected emotion classification backend."
emotion_confusion:
title: 😕 Confusion
- description: "Posts classified with the emotion confusion via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion confusion using the selected emotion classification backend."
emotion_curiosity:
title: 🤔 Curiosity
- description: "Posts classified with the emotion curiosity via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion curiosity using the selected emotion classification backend."
emotion_desire:
title: 😍 Desire
- description: "Posts classified with the emotion desire via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion desire using the selected emotion classification backend."
emotion_disappointment:
title: 😞 Disappointment
- description: "Posts classified with the emotion disappointment via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion disappointment using the selected emotion classification backend."
emotion_disapproval:
title: 👎 Disapproval
- description: "Posts classified with the emotion disapproval via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion disapproval using the selected emotion classification backend."
emotion_disgust:
title: 🤢 Disgust
- description: "Posts classified with the emotion disgust via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion disgust using the selected emotion classification backend."
emotion_embarrassment:
title: 😳 Embarrassment
- description: "Posts classified with the emotion embarrassment via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion embarrassment using the selected emotion classification backend."
emotion_excitement:
title: 🤪 Excitement
- description: "Posts classified with the emotion excitement via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion excitement using the selected emotion classification backend."
emotion_fear:
title: 😨 Fear
- description: "Posts classified with the emotion fear via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion fear using the selected emotion classification backend."
emotion_gratitude:
title: 🙏 Gratitude
- description: "Posts classified with the emotion gratitude via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion gratitude using the selected emotion classification backend."
emotion_grief:
title: 😢 Grief
- description: "Posts classified with the emotion grief via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion grief using the selected emotion classification backend."
emotion_joy:
title: 😊 Joy
- description: "Posts classified with the emotion joy via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion joy using the selected emotion classification backend."
emotion_love:
title: ❤️ Love
- description: "Posts classified with the emotion love via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion love using the selected emotion classification backend."
emotion_nervousness:
title: 😰 Nervousness
- description: "Posts classified with the emotion nervousness via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion nervousness using the selected emotion classification backend."
emotion_neutral:
title: 😐 Neutral
- description: "Posts classified with the emotion neutral via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion neutral using the selected emotion classification backend."
emotion_optimism:
title: 🌟 Optimism
- description: "Posts classified with the emotion optimism via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion optimism using the selected emotion classification backend."
emotion_pride:
title: 🦁 Pride
- description: "Posts classified with the emotion pride via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion pride using the selected emotion classification backend."
emotion_realization:
title: 💡 Realization
- description: "Posts classified with the emotion realization via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion realization using the selected emotion classification backend."
emotion_relief:
title: 😌 Relief
- description: "Posts classified with the emotion relief via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion relief using the selected emotion classification backend."
emotion_remorse:
title: 😔 Remorse
- description: "Posts classified with the emotion remorse via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion remorse using the selected emotion classification backend."
emotion_sadness:
title: 😭 Sadness
- description: "Posts classified with the emotion sadness via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion sadness using the selected emotion classification backend."
emotion_surprise:
title: 😲 Surprise
- description: "Posts classified with the emotion surprise via AI, using the model 'SamLowe/roberta-base-go_emotions'."
+ description: "Posts classified with the emotion surprise using the selected emotion classification backend."
discourse_ai:
ask_ai_reports:
diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.yml
index eb2721e6a3e..8a0fd0d941d 100644
--- a/plugins/discourse-ai/config/settings.yml
+++ b/plugins/discourse-ai/config/settings.yml
@@ -21,6 +21,11 @@ discourse_ai:
ai_sentiment_enabled:
default: false
client: true
+ area: "ai-features/sentiment"
+ ai_sentiment_emotion_enabled:
+ default: false
+ client: true
+ area: "ai-features/sentiment"
ai_sentiment_model_configs:
default: ""
secret: true
@@ -31,28 +36,79 @@ discourse_ai:
choices:
- classification_model
- agent
+ - decision_model
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_enabled
+ depends_behavior: "hidden"
ai_sentiment_sentiment_agent:
default: "-36"
type: enum
enum: "DiscourseAi::Configuration::AgentEnumerator"
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_enabled
+ - ai_sentiment_sentiment_classification_strategy
+ depends_on_values:
+ ai_sentiment_sentiment_classification_strategy:
+ - agent
+ depends_behavior: "hidden"
+ ai_sentiment_sentiment_decision_model:
+ default: ""
+ type: enum
+ enum: "DiscourseAi::Configuration::DecisionModelEnumerator"
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_enabled
+ - ai_sentiment_sentiment_classification_strategy
+ depends_on_values:
+ ai_sentiment_sentiment_classification_strategy:
+ - decision_model
+ depends_behavior: "hidden"
ai_sentiment_emotion_classification_strategy:
default: "classification_model"
type: enum
choices:
- classification_model
- agent
+ - decision_model
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_emotion_enabled
+ depends_behavior: "hidden"
ai_sentiment_emotion_agent:
default: "-37"
type: enum
enum: "DiscourseAi::Configuration::AgentEnumerator"
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_emotion_enabled
+ - ai_sentiment_emotion_classification_strategy
+ depends_on_values:
+ ai_sentiment_emotion_classification_strategy:
+ - agent
+ depends_behavior: "hidden"
+ ai_sentiment_emotion_decision_model:
+ default: ""
+ type: enum
+ enum: "DiscourseAi::Configuration::DecisionModelEnumerator"
+ area: "ai-features/sentiment"
+ depends_on:
+ - ai_sentiment_emotion_enabled
+ - ai_sentiment_emotion_classification_strategy
+ depends_on_values:
+ ai_sentiment_emotion_classification_strategy:
+ - decision_model
+ depends_behavior: "hidden"
ai_sentiment_backfill_maximum_posts_per_hour:
default: 2500
min: 0
max: 10000
- hidden: true
+ area: "ai-features/sentiment"
ai_sentiment_backfill_post_max_age_days:
default: 60
- hidden: true
+ min: 0
+ area: "ai-features/sentiment"
ai_openai_embeddings_url:
hidden: true
diff --git a/plugins/discourse-ai/db/migrate/20261021061850_copy_sentiment_enabled_to_emotion_enabled.rb b/plugins/discourse-ai/db/migrate/20261021061850_copy_sentiment_enabled_to_emotion_enabled.rb
new file mode 100644
index 00000000000..36ec3c45032
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20261021061850_copy_sentiment_enabled_to_emotion_enabled.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class CopySentimentEnabledToEmotionEnabled < ActiveRecord::Migration[8.1]
+ def up
+ execute <<~SQL
+ INSERT INTO site_settings (name, value, data_type, created_at, updated_at)
+ SELECT 'ai_sentiment_emotion_enabled', value, data_type, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
+ FROM site_settings
+ WHERE name = 'ai_sentiment_enabled'
+ ON CONFLICT (name) DO NOTHING
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-ai/docs/chat-tool-approval.md b/plugins/discourse-ai/docs/chat-tool-approval.md
deleted file mode 100644
index 9ed959ec34b..00000000000
--- a/plugins/discourse-ai/docs/chat-tool-approval.md
+++ /dev/null
@@ -1,47 +0,0 @@
-# Chat tool approval cards
-
-Tools that require approval are shown in bot direct-message channels as a Chat
-`confirmation` block: a heading identifying the target, a preview of the
-change, the approval question and Yes/No buttons. The chat message body holds
-the plain Markdown description and is rendered inside the card while it is
-pending.
-
-## Building a card
-
-`DiscourseAi::Agents::Bot#enqueue_tool_for_approval` yields a `:chat_approval`
-hash built from the tool's presentation methods. Responders pass that hash to
-`DiscourseAi::AiBot::ChatToolApproval.pending_blocks(reviewable_id, info:)` and
-use `info[:details]` as the message body. Calling `pending_blocks` without
-`info:` keeps the legacy actions-only layout.
-
-A card must be posted in the source request's channel and thread. A thread's
-original message is also shown in the main channel, so its card may stay there;
-`ReviewableAiToolAction` checks this placement before executing an action.
-
-## Tool presentation methods
-
-Every tool inherits defaults from `DiscourseAi::Agents::Tools::Tool`:
-
-- `approval_title` — heading; defaults to the tool summary.
-- `approval_changes` — `[{ label:, before:, after:, before_color:, after_color: }]`
- diffs; empty by default.
-- `approval_details` — Markdown message body; defaults to the tool description.
-- `approval_description_label` / `approval_show_description?` — label for, or
- suppression of, the body inside the card.
-- `approval_question` — defaults to "Do you want to make this change?".
-- `approval_parameters` — `[{ label:, value:, color: }]`; defaults to the tool
- parameters minus `reason`, with a swatch for six-digit hex colours.
-- `approval_resolved_title` — optional replacement heading after the action
- ran, for tools that rename their target.
-
-Previews run before the tool's own permission checks, so overrides must only
-describe targets the requesting user can see (`previewable?`) and must never
-mutate anything. Values are truncated to the schema limit; the Markdown fields
-are cooked by Chat when the message is serialized.
-
-## Resolution
-
-Approving or rejecting keeps the card and replaces its footer with the outcome;
-an execution failure is stored in the card's `error` field and the buttons stay
-for a retry. `ChatToolApproval.transcript_text(message)` renders a card as plain
-text for consumers that only read the message body, such as model replay.
diff --git a/plugins/discourse-ai/lib/configuration/feature.rb b/plugins/discourse-ai/lib/configuration/feature.rb
index a02413cab90..fd23b210394 100644
--- a/plugins/discourse-ai/lib/configuration/feature.rb
+++ b/plugins/discourse-ai/lib/configuration/feature.rb
@@ -202,6 +202,26 @@ module DiscourseAi
]
end
+ def sentiment_features
+ feature_cache[:sentiment] ||= %w[sentiment emotion].map do |type|
+ summary = -> { DiscourseAi::Sentiment::PostClassification.classifier_summary_for(type) }
+
+ new(
+ type,
+ "ai_sentiment_#{type}_agent",
+ DiscourseAi::Configuration::Module::SENTIMENT_ID,
+ DiscourseAi::Configuration::Module::SENTIMENT,
+ enabled_by_setting:
+ type == "sentiment" ? "ai_sentiment_enabled" : "ai_sentiment_emotion_enabled",
+ agent_ids_lookup: -> { summary.call.fetch(:agent_ids, []) },
+ llm_models_lookup: -> { summary.call.fetch(:llm_models, []) },
+ metadata_lookup: -> do
+ summary.call.slice(:backend, :ready, :decision_model, :classification_model)
+ end,
+ )
+ end
+ end
+
def lookup_bot_agent_ids
AiAgent
.where(enabled: true)
@@ -357,6 +377,7 @@ module DiscourseAi
spam_features,
embeddings_features,
admin_dashboard_features,
+ sentiment_features,
ai_automation_report_scripts,
ai_automation_triage_scripts,
].flatten
@@ -390,6 +411,7 @@ module DiscourseAi
enabled_by_setting: "",
agent_ids_lookup: nil,
llm_models_lookup: nil,
+ metadata_lookup: nil,
require_enabled_agent: false,
visible_if: nil
)
@@ -400,10 +422,15 @@ module DiscourseAi
@enabled_by_setting = enabled_by_setting
@agent_ids_lookup = agent_ids_lookup
@llm_models_lookup = llm_models_lookup
+ @metadata_lookup = metadata_lookup
@require_enabled_agent = require_enabled_agent
@visible_if = visible_if
end
+ def metadata
+ @metadata_lookup ? @metadata_lookup.call : {}
+ end
+
def llm_models
return @llm_models_lookup.call if @llm_models_lookup
return if !agent_ids
diff --git a/plugins/discourse-ai/lib/configuration/module.rb b/plugins/discourse-ai/lib/configuration/module.rb
index 2f094fb9576..9c03aa2f2c9 100644
--- a/plugins/discourse-ai/lib/configuration/module.rb
+++ b/plugins/discourse-ai/lib/configuration/module.rb
@@ -16,6 +16,7 @@ module DiscourseAi
AUTOMATION_REPORTS = "automation_reports"
AUTOMATION_TRIAGE = "automation_triage"
ADMIN_DASHBOARD = "admin_dashboard"
+ SENTIMENT = "sentiment"
NAMES = [
SUMMARIZATION,
@@ -31,6 +32,7 @@ module DiscourseAi
AUTOMATION_REPORTS,
AUTOMATION_TRIAGE,
ADMIN_DASHBOARD,
+ SENTIMENT,
].freeze
SUMMARIZATION_ID = 1
@@ -46,6 +48,7 @@ module DiscourseAi
AUTOMATION_TRIAGE_ID = 11
ADMIN_DASHBOARD_ID = 12
IMAGE_CAPTION_ID = 13
+ SENTIMENT_ID = 14
class << self
def external_module_id(module_name)
@@ -125,6 +128,12 @@ module DiscourseAi
end,
visible: false,
),
+ new(
+ SENTIMENT_ID,
+ SENTIMENT,
+ features: DiscourseAi::Configuration::Feature.sentiment_features,
+ extra_check: -> { DiscourseAi::Sentiment::PostClassification.any_enabled? },
+ ),
]
if SiteSetting.discourse_automation_enabled
diff --git a/plugins/discourse-ai/lib/sentiment/classification_question.rb b/plugins/discourse-ai/lib/sentiment/classification_question.rb
new file mode 100644
index 00000000000..5dc85b0a635
--- /dev/null
+++ b/plugins/discourse-ai/lib/sentiment/classification_question.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Sentiment
+ class ClassificationQuestion
+ def self.for(classification_type)
+ sentiment = classification_type.to_s == "sentiment"
+ subject = sentiment ? "sentiment" : "emotions"
+ instructions = <<~PROMPT
+ Classify the author's #{subject} in this Discourse forum post, not quoted text or replies from other people.
+ Ignore markup, links, signatures, logs, and code blocks unless they clearly express #{subject}.
+ Interpret sarcasm and irony by their implied meaning.
+ Use neutral as the highest probability for factual announcements, questions, or technical debugging with little affect.
+ For mixed #{subject}, distribute probability across labels instead of forcing a single winner.
+ Use the full post content provided. The label probabilities must sum to 1.0.
+ PROMPT
+
+ DiscourseAi::Decisions::Question.new(
+ name: classification_type.to_s,
+ type: :choice,
+ instructions: instructions,
+ choices: sentiment ? %w[negative neutral positive] : Emotions::LIST,
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/sentiment/constants.rb b/plugins/discourse-ai/lib/sentiment/constants.rb
index 91f348c3569..432b3f6e42b 100644
--- a/plugins/discourse-ai/lib/sentiment/constants.rb
+++ b/plugins/discourse-ai/lib/sentiment/constants.rb
@@ -7,6 +7,9 @@ module DiscourseAi
EMOTION_MODEL = "SamLowe/roberta-base-go_emotions"
SENTIMENT_AGENT_MODEL = "discourse-ai/sentiment-agent"
EMOTION_AGENT_MODEL = "discourse-ai/emotion-agent"
+ SENTIMENT_DECISION_MODEL = "discourse-ai/sentiment-decision"
+ EMOTION_DECISION_MODEL = "discourse-ai/emotion-decision"
+ DECISION_MODEL_STRATEGY = "decision_model"
CLASSIFICATION_MODEL_STRATEGY = "classification_model"
AGENT_STRATEGY = "agent"
SENTIMENT_THRESHOLD = 0.6
diff --git a/plugins/discourse-ai/lib/sentiment/entry_point.rb b/plugins/discourse-ai/lib/sentiment/entry_point.rb
index 041ccea430c..49813e23945 100644
--- a/plugins/discourse-ai/lib/sentiment/entry_point.rb
+++ b/plugins/discourse-ai/lib/sentiment/entry_point.rb
@@ -6,7 +6,7 @@ module DiscourseAi
def inject_into(plugin)
sentiment_analysis_cb =
Proc.new do |post|
- if SiteSetting.ai_sentiment_enabled
+ if DiscourseAi::Sentiment::PostClassification.any_enabled?
Jobs.enqueue(:post_sentiment_analysis, post_id: post.id)
end
end
@@ -14,7 +14,8 @@ module DiscourseAi
plugin.on(:post_edited, &sentiment_analysis_cb)
plugin.add_to_serializer(:current_user, :can_see_sentiment_reports) do
- ClassificationResult.has_sentiment_classification? && SiteSetting.ai_sentiment_enabled
+ ClassificationResult.has_sentiment_classification? &&
+ DiscourseAi::Sentiment::PostClassification.any_enabled?
end
# TODO we need new interfaces to conditionally register depending on site in the multisite
diff --git a/plugins/discourse-ai/lib/sentiment/post_classification.rb b/plugins/discourse-ai/lib/sentiment/post_classification.rb
index 41ecb44fe46..499494a301a 100644
--- a/plugins/discourse-ai/lib/sentiment/post_classification.rb
+++ b/plugins/discourse-ai/lib/sentiment/post_classification.rb
@@ -5,6 +5,21 @@ module DiscourseAi
class PostClassification
include Constants
+ def self.enabled_for?(classification_type)
+ case classification_type.to_s
+ when "sentiment"
+ SiteSetting.ai_sentiment_enabled
+ when "emotion"
+ SiteSetting.ai_sentiment_emotion_enabled
+ else
+ false
+ end
+ end
+
+ def self.any_enabled?
+ enabled_for?(:sentiment) || enabled_for?(:emotion)
+ end
+
def self.backfill_query(from_post_id: nil, max_age_days: nil)
available_classifier_names = active_classifier_names
return Post.none if available_classifier_names.blank?
@@ -45,7 +60,7 @@ module DiscourseAi
unioned_queries = queries.map(&:to_sql).join(" UNION ")
- Post.from(Arel.sql("(#{unioned_queries}) as posts"))
+ Post.from(Arel.sql("(#{unioned_queries}) as posts")).includes(:sentiment_classifications)
end
def self.active_classifier_names
@@ -55,13 +70,12 @@ module DiscourseAi
def self.active_model_name_for(classification_type)
classification_type = classification_type.to_s
- if strategy_for(classification_type) == Constants::AGENT_STRATEGY
+ case strategy_for(classification_type)
+ when Constants::AGENT_STRATEGY
+ return classification_type == "sentiment" ? SENTIMENT_AGENT_MODEL : EMOTION_AGENT_MODEL
+ when Constants::DECISION_MODEL_STRATEGY
return(
- if classification_type == "sentiment"
- Constants::SENTIMENT_AGENT_MODEL
- else
- Constants::EMOTION_AGENT_MODEL
- end
+ classification_type == "sentiment" ? SENTIMENT_DECISION_MODEL : EMOTION_DECISION_MODEL
)
end
@@ -125,6 +139,54 @@ module DiscourseAi
end
end
+ def self.agent_configuration_for(classification_type)
+ agent_id = SiteSetting.public_send("ai_sentiment_#{classification_type}_agent")
+ ai_agent = AiAgent.find_by_id_from_cache(agent_id)
+ return { agent: nil, model: nil } if ai_agent.blank?
+
+ model_id = ai_agent.default_llm_id || SiteSetting.ai_default_llm_model
+ model = model_id.present? ? LlmModel.find_by(id: model_id) : LlmModel.last
+ { agent: ai_agent, model: model }
+ end
+
+ def self.decision_model_for(classification_type)
+ model_id = SiteSetting.public_send("ai_sentiment_#{classification_type}_decision_model")
+ DecisionModel.find_by(id: model_id) if model_id.present?
+ end
+
+ def self.classifier_summary_for(classification_type)
+ backend = strategy_for(classification_type)
+ case backend
+ when Constants::AGENT_STRATEGY
+ configuration = agent_configuration_for(classification_type)
+ {
+ backend: backend,
+ ready: configuration.values.all?(&:present?),
+ agent_ids: [configuration[:agent]&.id].compact,
+ llm_models: [configuration[:model]].compact,
+ }
+ when Constants::DECISION_MODEL_STRATEGY
+ model = decision_model_for(classification_type)
+ {
+ backend: backend,
+ ready: model.present?,
+ decision_model: model && { id: model.id, display_name: model.display_name },
+ }
+ else
+ model_name = active_model_name_for(classification_type)
+ config =
+ SentimentSiteSettingJsonSchema.values.find { |value| value.model_name == model_name }
+ {
+ backend: backend,
+ ready: config.present? && config.endpoint.present?,
+ classification_model: {
+ model_name: model_name,
+ },
+ }
+ end
+ end
+
+ MAX_INPUT_WORDS = 3000
CONCURRENT_CLASSFICATIONS = 40
CONCURRENT_AGENT_CLASSIFICATIONS = 5
@@ -134,7 +196,7 @@ module DiscourseAi
max_threads =
(
- if available_classifiers.any? { |c| c[:provider] == :agent }
+ if available_classifiers.any? { |c| c[:provider] != :classification_model }
CONCURRENT_AGENT_CLASSIFICATIONS
else
CONCURRENT_CLASSFICATIONS
@@ -150,7 +212,7 @@ module DiscourseAi
text = prepare_text(record)
next if text.blank?
- already_classified = record.sentiment_classifications.pluck(&:model_used)
+ already_classified = record.sentiment_classifications.map(&:model_used)
missing_classifiers =
available_classifiers.reject { |ac| already_classified.include?(ac[:model_name]) }
@@ -220,7 +282,7 @@ module DiscourseAi
end
def classifiers
- hugging_face_classifiers + agent_classifiers
+ hugging_face_classifiers + agent_classifiers + decision_classifiers
end
def has_classifiers?
@@ -230,17 +292,22 @@ module DiscourseAi
private
def hugging_face_classifiers
- return [] if agent_strategy_for?(:sentiment) && agent_strategy_for?(:emotion)
+ if %i[sentiment emotion].none? { |type|
+ self.class.enabled_for?(type) && dedicated_strategy_for?(type)
+ }
+ return []
+ end
configs = DiscourseAi::Sentiment::SentimentSiteSettingJsonSchema.values
- legacy_sentiment_model = self.class.untyped_custom_model_name(configs, "sentiment")
+ untyped_sentiment_model = self.class.untyped_custom_model_name(configs, "sentiment")
configs.filter_map do |config|
classification_type = classification_type_for(config)
effective_type =
classification_type.presence ||
- ("sentiment" if config.model_name == legacy_sentiment_model)
- next if effective_type.present? && agent_strategy_for?(effective_type)
+ ("sentiment" if config.model_name == untyped_sentiment_model)
+ next unless self.class.enabled_for?(effective_type.presence || :sentiment)
+ next if effective_type.present? && !dedicated_strategy_for?(effective_type)
api_endpoint = config.endpoint
@@ -260,56 +327,64 @@ module DiscourseAi
end
def agent_classifiers
- [
- agent_classifier(
- :sentiment,
- SiteSetting.ai_sentiment_sentiment_agent,
- Constants::SENTIMENT_AGENT_MODEL,
- ),
- agent_classifier(
- :emotion,
- SiteSetting.ai_sentiment_emotion_agent,
- Constants::EMOTION_AGENT_MODEL,
- ),
- ].compact
- end
-
- def agent_classifier(classification_type, agent_id, model_name)
- return if !agent_strategy_for?(classification_type)
+ %i[sentiment emotion].filter_map { |type| agent_classifier(type) }
+ end
- ai_agent = AiAgent.find_by_id_from_cache(agent_id)
- return if ai_agent.blank?
+ def agent_classifier(classification_type)
+ return unless self.class.enabled_for?(classification_type)
+ return if self.class.strategy_for(classification_type) != Constants::AGENT_STRATEGY
- agent_klass = ai_agent.class_instance
- model_id = agent_klass.default_llm_id || SiteSetting.ai_default_llm_model
- model = model_id.present? ? LlmModel.find_by(id: model_id) : LlmModel.last
- return if model.blank?
+ configuration = self.class.agent_configuration_for(classification_type)
+ ai_agent, model = configuration.values_at(:agent, :model)
+ return if ai_agent.blank? || model.blank?
{
classification_type: classification_type.to_s,
- model_name: model_name,
- agent: agent_klass.new,
+ model_name: self.class.active_model_name_for(classification_type),
+ agent: ai_agent.class_instance.new,
user: ai_agent.user || Discourse.system_user,
model: model,
provider: :agent,
}
end
+ def decision_classifiers
+ %i[sentiment emotion].filter_map do |type|
+ next unless self.class.enabled_for?(type)
+ next if self.class.strategy_for(type) != Constants::DECISION_MODEL_STRATEGY
+
+ model = self.class.decision_model_for(type)
+ next if model.blank?
+
+ {
+ classification_type: type.to_s,
+ model_name: self.class.active_model_name_for(type),
+ model: model,
+ provider: :decision_model,
+ }
+ end
+ end
+
def classification_type_for(config)
self.class.classification_type_for(config)
end
- def agent_strategy_for?(classification_type)
- self.class.strategy_for(classification_type) == Constants::AGENT_STRATEGY
+ def dedicated_strategy_for?(classification_type)
+ self.class.strategy_for(classification_type) == Constants::CLASSIFICATION_MODEL_STRATEGY
end
def prepare_text(target)
- content =
- if target.post_number == 1
- "#{target.topic.title}\n#{target.raw}"
- else
- target.raw
- end
+ if target.post_number == 1
+ "#{target.topic.title}\n#{target.raw}"
+ else
+ target.raw
+ end
+ end
+
+ def prepare_classifier_input(classifier, content)
+ if classifier[:provider] != :classification_model
+ return content.truncate_words(MAX_INPUT_WORDS, omission: "")
+ end
DiscourseAi::Tokenizer::BertTokenizer.truncate(
content,
@@ -319,8 +394,13 @@ module DiscourseAi
end
def request_with(classifier, target, content)
+ content = prepare_classifier_input(classifier, content)
return request_with_agent(classifier, target, content) if classifier[:provider] == :agent
+ if classifier[:provider] == :decision_model
+ return request_with_decision_model(classifier, content)
+ end
+
result = classifier[:client].classify_by_sentiment!(content)
transform_result(result)
@@ -366,6 +446,19 @@ module DiscourseAi
)
end
+ def request_with_decision_model(classifier, content)
+ type = classifier[:classification_type]
+ result =
+ classifier[:model].decide(
+ content,
+ questions: [ClassificationQuestion.for(type)],
+ user: Discourse.system_user,
+ feature_name: type,
+ )
+ answer = result[type]
+ answer.probabilities unless answer.refusal?
+ end
+
def transform_agent_result(structured_output, raw_result, labels)
parsed_result = parse_raw_agent_result(raw_result)
diff --git a/plugins/discourse-ai/spec/configuration/feature_spec.rb b/plugins/discourse-ai/spec/configuration/feature_spec.rb
index 4537d250e87..6cf159f6850 100644
--- a/plugins/discourse-ai/spec/configuration/feature_spec.rb
+++ b/plugins/discourse-ai/spec/configuration/feature_spec.rb
@@ -12,6 +12,53 @@ RSpec.describe DiscourseAi::Configuration::Feature do
before { enable_current_plugin }
+ describe ".sentiment_features" do
+ it "toggles each capability separately and keeps module enablement as their union" do
+ sentiment, emotion = described_class.sentiment_features
+ sentiment_module =
+ DiscourseAi::Configuration::Module.find_by(
+ id: DiscourseAi::Configuration::Module::SENTIMENT_ID,
+ )
+
+ expect(sentiment_module.enabled_by_setting).to be_nil
+
+ [
+ [false, false],
+ [true, false],
+ [false, true],
+ [true, true],
+ ].each do |sentiment_enabled, emotion_enabled|
+ SiteSetting.ai_sentiment_enabled = sentiment_enabled
+ SiteSetting.ai_sentiment_emotion_enabled = emotion_enabled
+
+ expect(sentiment.enabled?).to eq(sentiment_enabled)
+ expect(emotion.enabled?).to eq(emotion_enabled)
+ expect(sentiment_module.enabled?).to eq(sentiment_enabled || emotion_enabled)
+ end
+ end
+
+ it "only reports agent usage for capabilities using the agent backend" do
+ SiteSetting.ai_sentiment_sentiment_agent = ai_agent.id
+ SiteSetting.ai_sentiment_emotion_agent = ai_agent.id
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+
+ sentiment, emotion = described_class.sentiment_features
+
+ expect(sentiment.agent_ids).to eq([ai_agent.id])
+ expect(sentiment.llm_models).to eq([llm_model])
+ expect(sentiment.metadata).to eq(backend: "agent", ready: true)
+ expect(emotion.agent_ids).to eq([])
+ expect(emotion.llm_models).to eq([])
+ expect(emotion.metadata).to eq(backend: "decision_model", ready: false, decision_model: nil)
+ expect(
+ described_class
+ .find_features_using(agent_id: ai_agent.id)
+ .select { |feature| feature.module_name == "sentiment" },
+ ).to eq([sentiment])
+ end
+ end
+
describe "#llm_model" do
context "when agent is not found" do
it "returns nil when agent_id is invalid" do
diff --git a/plugins/discourse-ai/spec/jobs/scheduled/sentiment_backfill_spec.rb b/plugins/discourse-ai/spec/jobs/scheduled/sentiment_backfill_spec.rb
index db56f30d1b2..f901fb0b958 100644
--- a/plugins/discourse-ai/spec/jobs/scheduled/sentiment_backfill_spec.rb
+++ b/plugins/discourse-ai/spec/jobs/scheduled/sentiment_backfill_spec.rb
@@ -10,6 +10,7 @@ RSpec.describe Jobs::SentimentBackfill do
before do
SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
SiteSetting.ai_sentiment_backfill_maximum_posts_per_hour = 100
SiteSetting.ai_sentiment_model_configs =
"[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]"
@@ -24,6 +25,32 @@ RSpec.describe Jobs::SentimentBackfill do
expect(ClassificationResult.where(target: post).count).to eq(expected_analysis)
end
+ it "backfills only missing decision results using stable strategy keys" do
+ model = Fabricate(:decision_model)
+ SiteSetting.ai_sentiment_model_configs = ""
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = model.id
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_decision_model = model.id
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ model_used: "discourse-ai/sentiment-decision",
+ )
+
+ DiscourseAi::Decisions.with_canned_answers(emotion: "joy") do |canned|
+ job.execute({})
+ job.execute({})
+
+ expect(canned.calls.map { |call| call[:feature_name] }).to eq(["emotion"])
+ end
+
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to contain_exactly(
+ "discourse-ai/sentiment-decision",
+ "discourse-ai/emotion-decision",
+ )
+ end
+
it "does nothing when batch size is zero" do
SiteSetting.ai_sentiment_backfill_maximum_posts_per_hour = 0
@@ -32,14 +59,28 @@ RSpec.describe Jobs::SentimentBackfill do
expect(ClassificationResult.count).to be_zero
end
- it "does nothing when sentiment is disabled" do
+ it "does nothing when both capabilities are disabled" do
SiteSetting.ai_sentiment_enabled = false
+ SiteSetting.ai_sentiment_emotion_enabled = false
job.execute({})
expect(ClassificationResult.count).to be_zero
end
+ it "processes only posts created today when the age limit is zero" do
+ today = DB.query_single("SELECT CURRENT_DATE::timestamp").first
+ post.update_columns(created_at: today + 1.minute)
+ yesterday_post = Fabricate(:post, created_at: today - 1.minute)
+ SiteSetting.ai_sentiment_backfill_post_max_age_days = 0
+ SentimentInferenceStubs.stub_classification(post)
+
+ job.execute({})
+
+ expect(ClassificationResult.where(target: post).count).to eq(expected_analysis)
+ expect(ClassificationResult.where(target: yesterday_post).count).to be_zero
+ end
+
it "respects the ai_sentiment_backfill_post_max_age_days setting" do
SentimentInferenceStubs.stub_classification(post)
SiteSetting.ai_sentiment_backfill_post_max_age_days = 80
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/capability_flags_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/capability_flags_spec.rb
new file mode 100644
index 00000000000..db9ea15c6a3
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/capability_flags_spec.rb
@@ -0,0 +1,157 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Sentiment::PostClassification do
+ fab!(:post)
+ fab!(:decision_model)
+ fab!(:llm_model, :fake_model)
+ fab!(:agent) do
+ Fabricate(
+ :ai_agent,
+ default_llm: llm_model,
+ response_format:
+ %w[negative neutral positive joy].map { |label| { "key" => label, "type" => "number" } },
+ )
+ end
+
+ before do
+ enable_current_plugin
+ DiscourseAi::Utils::DnsSrv.stubs(:lookup).raises(RuntimeError, "Unexpected SRV lookup")
+ end
+
+ %w[classification_model agent decision_model].each do |backend|
+ describe "independent enable flags with #{backend}" do
+ before do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = backend
+ SiteSetting.ai_sentiment_emotion_classification_strategy = backend
+ SiteSetting.ai_sentiment_sentiment_agent = agent.id
+ SiteSetting.ai_sentiment_emotion_agent = agent.id
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_model_configs =
+ %w[sentiment emotion]
+ .map do |type|
+ {
+ classification_type: type,
+ model_name: "custom/#{type}",
+ endpoint: "https://#{type}.example.com",
+ api_key: "secret",
+ }
+ end
+ .to_json
+ end
+
+ [[false, false], [true, false], [false, true], [true, true]].each do |sentiment, emotion|
+ it "runs only enabled capabilities with sentiment=#{sentiment} and emotion=#{emotion}" do
+ SiteSetting.ai_sentiment_enabled = sentiment
+ SiteSetting.ai_sentiment_emotion_enabled = emotion
+ enabled_types = []
+ enabled_types << "sentiment" if sentiment
+ enabled_types << "emotion" if emotion
+ model_names = enabled_types.map { |type| described_class.active_model_name_for(type) }
+ classifier = described_class.new
+
+ expect(described_class.enabled_for?(:sentiment)).to eq(sentiment)
+ expect(described_class.enabled_for?("emotion")).to eq(emotion)
+ expect(described_class.any_enabled?).to eq(sentiment || emotion)
+ expect(described_class.active_classifier_names).to match_array(model_names)
+ expect(classifier.has_classifiers?).to eq(sentiment || emotion)
+ expect(described_class.backfill_query).to match_array(enabled_types.empty? ? [] : [post])
+ %w[sentiment emotion].each do |type|
+ expect(described_class.classifier_summary_for(type)[:ready]).to eq(true)
+ end
+
+ SentimentInferenceStubs.stub_classification(post)
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ negative: 0.1, neutral: 0.2, positive: 0.7, joy: 0.7 }] * 2,
+ ) do
+ DiscourseAi::Decisions.with_canned_answers(sentiment: "positive", emotion: "joy") do
+ classifier.classify!(post)
+ classifier.bulk_classify!(Post.where(id: post.id))
+ end
+ end
+
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to match_array(
+ model_names,
+ )
+ expect(described_class.backfill_query).to be_empty
+ end
+ end
+ end
+ end
+
+ describe "dedicated capability attribution" do
+ before do
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ model_name: DiscourseAi::Sentiment::Constants::SENTIMENT_MODEL,
+ endpoint: "srv://disabled-sentiment.example.com",
+ api_key: "secret",
+ },
+ {
+ model_name: DiscourseAi::Sentiment::Constants::EMOTION_MODEL,
+ endpoint: "https://emotion.example.com",
+ api_key: "secret",
+ },
+ {
+ model_name: "unknown/one",
+ endpoint: "srv://ambiguous-one.example.com",
+ api_key: "secret",
+ },
+ {
+ model_name: "unknown/two",
+ endpoint: "srv://ambiguous-two.example.com",
+ api_key: "secret",
+ },
+ ].to_json
+ end
+
+ it "excludes default-named sentiment and ambiguous configs before resolving disabled endpoints" do
+ SiteSetting.ai_sentiment_enabled = false
+ SiteSetting.ai_sentiment_emotion_enabled = true
+
+ expect(described_class.active_classifier_names).to eq(
+ [DiscourseAi::Sentiment::Constants::EMOTION_MODEL],
+ )
+ end
+
+ it "excludes a lone untyped custom config when sentiment is disabled" do
+ SiteSetting.ai_sentiment_enabled = false
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ SiteSetting.ai_sentiment_model_configs = [
+ { model_name: "unknown/only", endpoint: "srv://disabled.example.com", api_key: "secret" },
+ ].to_json
+
+ expect(described_class.new.classifiers).to be_empty
+ expect(described_class.backfill_query).to be_empty
+ end
+ end
+
+ describe ".backfill_query with disabled capabilities" do
+ before do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ model_used: "discourse-ai/sentiment-decision",
+ )
+ end
+
+ it "selects missing results only for enabled capabilities" do
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = false
+ expect(described_class.backfill_query).to be_empty
+
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ expect(described_class.backfill_query).to contain_exactly(post)
+
+ SiteSetting.ai_sentiment_enabled = false
+ expect(described_class.backfill_query).to contain_exactly(post)
+
+ SiteSetting.ai_sentiment_emotion_enabled = false
+ expect(described_class.backfill_query).to be_empty
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/classifier_summary_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/classifier_summary_spec.rb
new file mode 100644
index 00000000000..da61e668bff
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/classifier_summary_spec.rb
@@ -0,0 +1,178 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Sentiment::PostClassification do
+ before do
+ enable_current_plugin
+ DiscourseAi::Utils::DnsSrv.stubs(:lookup).raises(RuntimeError, "Unexpected SRV lookup")
+ end
+
+ describe ".classifier_summary_for" do
+ fab!(:older_model) { Fabricate(:fake_model, name: "older") }
+ fab!(:latest_model) { Fabricate(:fake_model, name: "latest") }
+ fab!(:agent) { Fabricate(:ai_agent, enabled: false, default_llm: older_model) }
+
+ it "selects the agent's default model even when the agent and module are disabled" do
+ SiteSetting.ai_sentiment_enabled = false
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_sentiment_agent = agent.id
+ SiteSetting.ai_default_llm_model = latest_model.id
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "agent",
+ ready: true,
+ agent_ids: [agent.id],
+ llm_models: [older_model],
+ )
+ expect(described_class.new.classifiers).to be_empty
+ end
+
+ it "matches runtime's site-default and latest-model fallbacks" do
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ agent.update!(default_llm: nil)
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_emotion_agent = agent.id
+ SiteSetting.ai_default_llm_model = older_model.id
+
+ expect(described_class.classifier_summary_for("emotion")[:llm_models]).to eq([older_model])
+ expect(described_class.new.classifiers.sole[:model]).to eq(older_model)
+
+ SiteSetting.ai_default_llm_model = ""
+
+ expect(described_class.classifier_summary_for(:emotion)[:llm_models]).to eq([latest_model])
+ expect(described_class.new.classifiers.sole[:model]).to eq(latest_model)
+ end
+
+ it "reports an unresolved effective LLM as not ready without substituting the latest model" do
+ agent.update!(default_llm: nil)
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_sentiment_agent = agent.id
+ SiteSetting.ai_default_llm_model = older_model.id
+ older_model.destroy!
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "agent",
+ ready: false,
+ agent_ids: [agent.id],
+ llm_models: [],
+ )
+ expect(described_class.new.classifiers).to be_empty
+ end
+
+ it "reports an unavailable agent as not ready" do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_sentiment_agent = agent.id
+ agent.destroy!
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "agent",
+ ready: false,
+ agent_ids: [],
+ llm_models: [],
+ )
+ expect(described_class.new.classifiers).to be_empty
+ end
+
+ it "exposes only decision model identity without resolving its SRV endpoint or credentials" do
+ model =
+ Fabricate(
+ :decision_model,
+ url: "srv://decision.example.com",
+ custom_headers: [{ name: "X-Secret", value: "private" }],
+ )
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = model.id
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "decision_model",
+ ready: true,
+ decision_model: {
+ id: model.id,
+ display_name: model.display_name,
+ },
+ )
+ expect(described_class.new.classifiers.sole[:model]).to eq(model)
+ end
+
+ it "reports blank and deleted decision models as not ready" do
+ model = Fabricate(:decision_model)
+ model.destroy!
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+
+ ["", model.id.to_s].each do |model_id|
+ SiteSetting.ai_sentiment_emotion_decision_model = model_id
+ expect(described_class.classifier_summary_for(:emotion)).to eq(
+ backend: "decision_model",
+ ready: false,
+ decision_model: nil,
+ )
+ expect(described_class.new.classifiers).to be_empty
+ end
+ end
+
+ it "exposes only the selected dedicated name without resolving SRV or exposing secrets" do
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ classification_type: "sentiment",
+ model_name: "private/sentiment",
+ endpoint: "srv://sentiment.example.com",
+ api_key: "private-key",
+ },
+ ].to_json
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "classification_model",
+ ready: true,
+ classification_model: {
+ model_name: "private/sentiment",
+ },
+ )
+ end
+
+ it "matches the single untyped custom dedicated fallback" do
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ model_name: "private/custom",
+ endpoint: "https://sentiment.example.com",
+ api_key: "private-key",
+ },
+ ].to_json
+
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "classification_model",
+ ready: true,
+ classification_model: {
+ model_name: "private/custom",
+ },
+ )
+ expect(described_class.new.classifiers).to be_empty
+ end
+
+ it "reports missing or blank dedicated endpoints as not ready" do
+ expect(described_class.classifier_summary_for(:sentiment)).to eq(
+ backend: "classification_model",
+ ready: false,
+ classification_model: {
+ model_name: DiscourseAi::Sentiment::Constants::SENTIMENT_MODEL,
+ },
+ )
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ classification_type: "emotion",
+ model_name: "private/emotion",
+ endpoint: "",
+ api_key: "private-key",
+ },
+ ].to_json
+
+ expect(described_class.classifier_summary_for(:emotion)).to eq(
+ backend: "classification_model",
+ ready: false,
+ classification_model: {
+ model_name: "private/emotion",
+ },
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/decision_classification_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/decision_classification_spec.rb
new file mode 100644
index 00000000000..43e01e8ff0c
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/decision_classification_spec.rb
@@ -0,0 +1,320 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Sentiment::PostClassification do
+ subject(:post_classification) { described_class.new }
+
+ fab!(:decision_model)
+ fab!(:post)
+
+ let(:sentiment_scores) { { "negative" => 0.15, "neutral" => 0.2, "positive" => 0.65 } }
+ let(:emotion_scores) do
+ DiscourseAi::Sentiment::Emotions::LIST.index_with do |label|
+ { "joy" => 0.7, "approval" => 0.2, "neutral" => 0.1 }.fetch(label, 0.0)
+ end
+ end
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ SiteSetting.ai_sentiment_model_configs = ""
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ end
+
+ describe "#classify! with decision models" do
+ it "stores the exact three-way distribution and requests as the system user" do
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores) do |canned|
+ post_classification.classify!(post)
+
+ expect(canned.calls).to contain_exactly(
+ {
+ state: "#{post.topic.title}\n#{post.raw}",
+ questions: [
+ have_attributes(
+ name: "sentiment",
+ type: :choice,
+ choices: %w[negative neutral positive],
+ ),
+ ],
+ images: [],
+ user: Discourse.system_user,
+ feature_name: "sentiment",
+ },
+ )
+ end
+
+ expect(ClassificationResult.where(target: post).sole).to have_attributes(
+ model_used: "discourse-ai/sentiment-decision",
+ classification_type: "sentiment",
+ classification: sentiment_scores,
+ )
+ end
+
+ it "stores all 28 emotion probabilities in a single distribution" do
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+
+ DiscourseAi::Decisions.with_canned_answers(
+ sentiment: sentiment_scores,
+ emotion: emotion_scores,
+ ) do |canned|
+ post_classification.classify!(post)
+
+ expect(canned.calls.map { |call| call[:feature_name] }).to eq(%w[sentiment emotion])
+ expect(canned.calls.last[:questions]).to contain_exactly(
+ have_attributes(
+ name: "emotion",
+ type: :choice,
+ choices: DiscourseAi::Sentiment::Emotions::LIST,
+ ),
+ )
+ end
+
+ expect(
+ ClassificationResult.find_by!(target: post, model_used: "discourse-ai/emotion-decision"),
+ ).to have_attributes(classification: emotion_scores, classification_type: "sentiment")
+ end
+
+ it "preserves reply content below the word limit without prepending a title" do
+ post.update!(post_number: 2, raw: "A technical question.\n\n" * 600)
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores) do |canned|
+ post_classification.classify!(post)
+
+ expect(canned.calls.sole[:state]).to eq(post.raw)
+ end
+ end
+
+ it "stores nothing for refusals and retries those posts during backfill" do
+ DiscourseAi::Decisions.with_canned_answers(sentiment: :refusal) do
+ post_classification.classify!(post)
+ end
+
+ expect(ClassificationResult.where(target: post)).to be_empty
+ expect(described_class.backfill_query).to contain_exactly(post)
+ end
+
+ it "keeps stored results when the selected decision model changes" do
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores) do
+ post_classification.classify!(post)
+ end
+ SiteSetting.ai_sentiment_sentiment_decision_model = Fabricate(:decision_model).id
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: "negative") do |canned|
+ post_classification.classify!(post.reload)
+ expect(canned.calls).to be_empty
+ end
+
+ expect(ClassificationResult.where(target: post).sole.classification).to eq(sentiment_scores)
+ expect(described_class.backfill_query).to be_empty
+ end
+
+ it "keeps decision and agent backends independent" do
+ llm_model = Fabricate(:fake_model)
+ agent =
+ Fabricate(
+ :ai_agent,
+ default_llm: llm_model,
+ response_format:
+ DiscourseAi::Sentiment::Emotions::LIST.map do |label|
+ { "key" => label, "type" => "number" }
+ end,
+ )
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_emotion_agent = agent.id
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([emotion_scores]) do
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores) do
+ post_classification.classify!(post)
+ end
+ end
+
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to contain_exactly(
+ "discourse-ai/sentiment-decision",
+ "discourse-ai/emotion-agent",
+ )
+ end
+
+ it "runs the dedicated backend only for the other selected classification type" do
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ classification_type: "sentiment",
+ model_name: "custom/sentiment",
+ endpoint: "srv://excluded.example.com",
+ api_key: "secret",
+ },
+ {
+ classification_type: "emotion",
+ model_name: "custom/emotion",
+ endpoint: "https://emotion.example.com",
+ api_key: "secret",
+ },
+ ].to_json
+ request =
+ stub_request(:post, "https://emotion.example.com").to_return(
+ body: [{ label: "joy", score: 0.7 }].to_json,
+ )
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores) do
+ post_classification.classify!(post)
+ end
+
+ expect(request).to have_been_requested.once
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to contain_exactly(
+ "discourse-ai/sentiment-decision",
+ "custom/emotion",
+ )
+ end
+
+ it "skips blank and deleted decision models without falling back to dedicated" do
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ model_name: "custom/dedicated",
+ endpoint: "srv://excluded.example.com",
+ api_key: "secret",
+ },
+ ].to_json
+ decision_model.destroy!
+
+ [decision_model.id.to_s, ""].each do |model_id|
+ SiteSetting.ai_sentiment_sentiment_decision_model = model_id
+ post_classification.classify!(post)
+
+ expect(post_classification.classifiers).to be_empty
+ expect(described_class.backfill_query).to be_empty
+ expect(ClassificationResult.where(target: post)).to be_empty
+ end
+ end
+
+ it "propagates provider failures without storing a substitute result" do
+ request =
+ stub_request(:post, decision_model.url).to_return(status: 400, body: "Invalid request")
+
+ expect { post_classification.classify!(post) }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ )
+
+ expect(request).to have_been_requested.once
+ expect(ClassificationResult.where(target: post)).to be_empty
+ end
+
+ it "audits and charges each classification type using the default feature cost" do
+ decision_model.update!(id: -501)
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+ Fabricate(
+ :decision_model_credit_allocation,
+ decision_model: decision_model,
+ daily_credits: 1000,
+ )
+ DecisionModelFeatureCreditCost.create!(
+ decision_model: decision_model,
+ feature_name: "default",
+ credits_per_token: 2,
+ )
+ request =
+ stub_request(:post, decision_model.url).to_return do |http_request|
+ type = JSON.parse(http_request.body)["questions"].keys.sole
+ scores = type == "sentiment" ? sentiment_scores : emotion_scores
+ {
+ body: {
+ answers: {
+ type => {
+ type: "choice",
+ choice: scores.max_by(&:last).first,
+ confidence: 0.99,
+ probabilities: scores,
+ },
+ },
+ usage: {
+ input_tokens: 10,
+ output_tokens: 2,
+ },
+ }.to_json,
+ }
+ end
+
+ post_classification.classify!(post)
+
+ expect(request).to have_been_requested.twice
+ expect(AiApiAuditLog.order(:id).pluck(:feature_name, :user_id)).to eq(
+ [["sentiment", Discourse.system_user.id], ["emotion", Discourse.system_user.id]],
+ )
+ expect(DecisionModelCreditDailyUsage.usage_for_date(decision_model.id, Date.current)).to eq(
+ 48,
+ )
+ expect(
+ ClassificationResult.find_by!(
+ target: post,
+ model_used: "discourse-ai/sentiment-decision",
+ ).classification,
+ ).to eq(sentiment_scores)
+ expect(
+ ClassificationResult.find_by!(
+ target: post,
+ model_used: "discourse-ai/emotion-decision",
+ ).classification,
+ ).to eq(emotion_scores)
+ end
+
+ it "propagates credit limits without storing results" do
+ decision_model.update!(id: -502)
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ Fabricate(
+ :decision_model_credit_allocation,
+ decision_model: decision_model,
+ daily_credits: 10,
+ )
+ DecisionModelCreditDailyUsage.increment_usage!(decision_model, 10)
+
+ expect { post_classification.classify!(post) }.to raise_error(
+ DiscourseAi::Decisions::CreditLimitExceeded,
+ )
+ expect(ClassificationResult.where(target: post)).to be_empty
+ end
+ end
+
+ describe "#classifiers with decision models" do
+ it "excludes all dedicated configs when neither type selects the dedicated strategy" do
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_model_configs = [
+ { model_name: "unknown/one", endpoint: "srv://excluded.example.com", api_key: "secret" },
+ { model_name: "unknown/two", endpoint: "srv://excluded.example.com", api_key: "secret" },
+ ].to_json
+
+ expect(post_classification.classifiers.map { |classifier| classifier[:model_name] }).to eq(
+ ["discourse-ai/sentiment-decision"],
+ )
+ end
+
+ it "preserves multiple untyped dedicated configs while one type still selects dedicated" do
+ SiteSetting.ai_sentiment_model_configs = [
+ { model_name: "unknown/one", endpoint: "https://one.example.com", api_key: "secret" },
+ { model_name: "unknown/two", endpoint: "https://two.example.com", api_key: "secret" },
+ ].to_json
+
+ expect(
+ post_classification.classifiers.map { |classifier| classifier[:model_name] },
+ ).to contain_exactly("unknown/one", "unknown/two", "discourse-ai/sentiment-decision")
+ end
+ end
+
+ describe "#bulk_classify! with decision models" do
+ it "stores successful distributions while leaving refused classifications missing" do
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: sentiment_scores, emotion: :refusal) do
+ post_classification.bulk_classify!(Post.where(id: post.id))
+ end
+
+ expect(ClassificationResult.where(target: post).sole).to have_attributes(
+ model_used: "discourse-ai/sentiment-decision",
+ classification: sentiment_scores,
+ )
+ expect(described_class.backfill_query).to contain_exactly(post)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/emotion_filter_order_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/emotion_filter_order_spec.rb
index 0807de325fc..99ee1171295 100644
--- a/plugins/discourse-ai/spec/lib/modules/sentiment/emotion_filter_order_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/emotion_filter_order_spec.rb
@@ -85,6 +85,32 @@ RSpec.describe DiscourseAi::Sentiment::EmotionFilterOrder do
end
end
+ it "filters topics using decision results instead of inactive dedicated results" do
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ decision_post = Fabricate(:post)
+ dedicated_post = Fabricate(:post)
+ Fabricate(
+ :sentiment_classification,
+ target: decision_post,
+ model_used: "discourse-ai/emotion-decision",
+ classification: {
+ joy: 0.7,
+ },
+ )
+ Fabricate(
+ :sentiment_classification,
+ target: dedicated_post,
+ model_used: DiscourseAi::Sentiment::Constants::EMOTION_MODEL,
+ classification: {
+ joy: 0.9,
+ },
+ )
+
+ expect(
+ TopicsFilter.new(guardian:).filter_from_query_string("order:emotion_joy").pluck(:id),
+ ).to eq([decision_post.topic_id])
+ end
+
context "with classified topics" do
fab!(:post_1, :post)
fab!(:post_2, :post)
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb
index 82fd738c4ac..f668a7a5c90 100644
--- a/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/entry_point_spec.rb
@@ -201,4 +201,33 @@ RSpec.describe DiscourseAi::Sentiment::EntryPoint do
end
end
end
+
+ describe "independent capability enable flags" do
+ fab!(:post)
+ fab!(:admin)
+
+ [[false, false], [true, false], [false, true], [true, true]].each do |sentiment, emotion|
+ it "gates edited-post jobs and report visibility with sentiment=#{sentiment} and emotion=#{emotion}" do
+ SiteSetting.ai_sentiment_enabled = sentiment
+ SiteSetting.ai_sentiment_emotion_enabled = emotion
+ Fabricate(:sentiment_classification, target: post)
+
+ PostRevisor.new(post).revise!(post.user, raw: "#{post.raw} Edited content")
+
+ expect(job_enqueued?(job: :post_sentiment_analysis, args: { post_id: post.id })).to eq(
+ sentiment || emotion,
+ )
+ serializer = CurrentUserSerializer.new(admin, scope: admin.guardian)
+ expect(serializer.can_see_sentiment_reports).to eq(sentiment || emotion)
+ end
+ end
+
+ it "keeps report visibility off without stored results even when emotion is enabled" do
+ SiteSetting.ai_sentiment_emotion_enabled = true
+
+ serializer = CurrentUserSerializer.new(admin, scope: admin.guardian)
+
+ expect(serializer.can_see_sentiment_reports).to eq(false)
+ end
+ end
end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/input_preparation_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/input_preparation_spec.rb
new file mode 100644
index 00000000000..17c7b5eb897
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/input_preparation_spec.rb
@@ -0,0 +1,120 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Sentiment::PostClassification do
+ fab!(:post)
+ fab!(:decision_model)
+ fab!(:llm_model) { Fabricate(:fake_model, max_prompt_tokens: 32_000) }
+ fab!(:agent) do
+ Fabricate(
+ :ai_agent,
+ default_llm: llm_model,
+ response_format:
+ %w[negative neutral positive].map { |label| { "key" => label, "type" => "number" } },
+ )
+ end
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = false
+ SiteSetting.ai_sentiment_model_configs = ""
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_sentiment_agent = agent.id
+ end
+
+ %i[classify! bulk_classify!].each do |operation|
+ describe "##{operation} input budgets" do
+ def classify_post(operation)
+ target = operation == :classify! ? post : Post.where(id: post.id)
+ described_class.new.public_send(operation, target)
+ end
+
+ it "preserves a complete post and title for decision models without BERT normalization" do
+ post.update!(
+ raw: "Opening CASE!\n\n#{"A thoughtful observation. " * 200}\n\nAn UNHAPPY ending.",
+ )
+ expected = "#{post.topic.title}\n#{post.raw}"
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: "negative") do |canned|
+ classify_post(operation)
+
+ expect(canned.calls.sole[:state]).to eq(expected)
+ end
+ end
+
+ it "caps decision input at 3000 words while retaining the dedicated 512-token limit" do
+ post.update!(
+ raw: "Opening CASE!\n\n#{(1..3100).map { |number| "word#{number}" }.join(" ")}",
+ )
+ full_text = "#{post.topic.title}\n#{post.raw}"
+ dedicated_text =
+ DiscourseAi::Tokenizer::BertTokenizer.truncate(
+ full_text,
+ 512,
+ strict: SiteSetting.ai_strict_token_counting,
+ )
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "classification_model"
+ SiteSetting.ai_sentiment_model_configs = [
+ {
+ classification_type: "emotion",
+ model_name: "custom/emotion",
+ endpoint: "https://emotion.example.com",
+ api_key: "secret",
+ },
+ ].to_json
+ request =
+ stub_request(:post, "https://emotion.example.com").with(
+ body: { inputs: dedicated_text, truncate: true }.to_json,
+ ).to_return(body: [{ label: "neutral", score: 0.9 }].to_json)
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: "neutral") do |canned|
+ classify_post(operation)
+
+ expect(canned.calls.sole[:state].split).to eq(full_text.split.first(3000))
+ expect(canned.calls.sole[:state]).to start_with("#{post.topic.title}\nOpening CASE!\n\n")
+ end
+ expect(request).to have_been_requested.once
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to contain_exactly(
+ "discourse-ai/sentiment-decision",
+ "custom/emotion",
+ )
+ end
+
+ it "preserves full agent input below 3000 words" do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ post.update!(
+ raw: "Opening CASE!\n\n#{"A thoughtful observation. " * 200}\n\nAn UNHAPPY ending.",
+ )
+ expected = "#{post.topic.title}\n#{post.raw}"
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ negative: 0.8, neutral: 0.1, positive: 0.1 }],
+ ) do |_, _, prompts|
+ classify_post(operation)
+
+ expect(prompts.sole.messages.map { |message| message[:content] }).to include(expected)
+ end
+ end
+
+ it "caps agent input at 3000 words including the title" do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
+ post.update!(
+ raw: "Opening CASE!\n\n#{(1..3100).map { |number| "word#{number}" }.join(" ")}",
+ )
+ full_text = "#{post.topic.title}\n#{post.raw}"
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(
+ [{ negative: 0.1, neutral: 0.8, positive: 0.1 }],
+ ) do |_, _, prompts|
+ classify_post(operation)
+
+ content = prompts.sole.messages.find { |message| message[:type] == :user }[:content]
+ expect(content.split).to eq(full_text.split.first(3000))
+ expect(content).to start_with("#{post.topic.title}\nOpening CASE!\n\n")
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/regular/post_sentiment_analysis_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/regular/post_sentiment_analysis_spec.rb
index cce1452b2ec..70663833560 100644
--- a/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/regular/post_sentiment_analysis_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/regular/post_sentiment_analysis_spec.rb
@@ -10,13 +10,15 @@ describe Jobs::PostSentimentAnalysis do
before do
SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
SiteSetting.ai_sentiment_model_configs =
"[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]"
end
describe "scenarios where we return early without doing anything" do
- it "does nothing when ai_sentiment_enabled is disabled" do
+ it "does nothing when both capabilities are disabled" do
SiteSetting.ai_sentiment_enabled = false
+ SiteSetting.ai_sentiment_emotion_enabled = false
job.execute({ post_id: post.id })
@@ -44,6 +46,58 @@ describe Jobs::PostSentimentAnalysis do
end
end
+ it "classifies using a decision model without enabling workflows" do
+ model = Fabricate(:decision_model)
+ SiteSetting.enable_discourse_workflows = false
+ SiteSetting.ai_sentiment_model_configs = ""
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = model.id
+
+ DiscourseAi::Decisions.with_canned_answers(
+ sentiment: {
+ negative: 0.1,
+ neutral: 0.2,
+ positive: 0.7,
+ },
+ ) { job.execute({ post_id: post.id }) }
+
+ expect(ClassificationResult.where(target: post).sole).to have_attributes(
+ model_used: "discourse-ai/sentiment-decision",
+ classification: {
+ "negative" => 0.1,
+ "neutral" => 0.2,
+ "positive" => 0.7,
+ },
+ )
+ end
+
+ [[true, false], [false, true]].each do |sentiment, emotion|
+ it "processes only enabled decisions with sentiment=#{sentiment} and emotion=#{emotion}" do
+ model = Fabricate(:decision_model)
+ SiteSetting.ai_sentiment_enabled = sentiment
+ SiteSetting.ai_sentiment_emotion_enabled = emotion
+ SiteSetting.ai_sentiment_model_configs = ""
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = model.id
+ SiteSetting.ai_sentiment_emotion_decision_model = model.id
+
+ DiscourseAi::Decisions.with_canned_answers(
+ sentiment: "positive",
+ emotion: "joy",
+ ) do |canned|
+ job.execute(post_id: post.id)
+
+ expect(canned.calls.map { |call| call[:feature_name] }).to eq(
+ [sentiment ? "sentiment" : "emotion"],
+ )
+ end
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to eq(
+ [sentiment ? "discourse-ai/sentiment-decision" : "discourse-ai/emotion-decision"],
+ )
+ end
+ end
+
it "successfully classifies the post" do
expected_analysis = DiscourseAi::Sentiment::PostClassification.new.classifiers.length
SentimentInferenceStubs.stub_classification(post)
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/scheduled/sentiment_backfill_capability_flags_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/scheduled/sentiment_backfill_capability_flags_spec.rb
new file mode 100644
index 00000000000..e633f4729ac
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/jobs/scheduled/sentiment_backfill_capability_flags_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::SentimentBackfill do
+ subject(:job) { described_class.new }
+
+ fab!(:post)
+ fab!(:decision_model)
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+ end
+
+ [[false, false], [true, false], [false, true], [true, true]].each do |sentiment, emotion|
+ it "backfills only enabled decisions with sentiment=#{sentiment} and emotion=#{emotion}" do
+ SiteSetting.ai_sentiment_enabled = sentiment
+ SiteSetting.ai_sentiment_emotion_enabled = emotion
+ enabled_types = []
+ enabled_types << "sentiment" if sentiment
+ enabled_types << "emotion" if emotion
+
+ DiscourseAi::Decisions.with_canned_answers(sentiment: "positive", emotion: "joy") do |canned|
+ job.execute({})
+ job.execute({})
+
+ expect(canned.calls.map { |call| call[:feature_name] }).to match_array(enabled_types)
+ end
+ expect(ClassificationResult.where(target: post).pluck(:model_used)).to match_array(
+ enabled_types.map { |type| "discourse-ai/#{type}-decision" },
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/sentiment/post_classification_spec.rb b/plugins/discourse-ai/spec/lib/modules/sentiment/post_classification_spec.rb
index 980463cac11..0f87e8de0a2 100644
--- a/plugins/discourse-ai/spec/lib/modules/sentiment/post_classification_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/sentiment/post_classification_spec.rb
@@ -6,6 +6,7 @@ RSpec.describe DiscourseAi::Sentiment::PostClassification do
before do
enable_current_plugin
SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
SiteSetting.ai_sentiment_model_configs =
"[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]"
end
@@ -24,7 +25,7 @@ RSpec.describe DiscourseAi::Sentiment::PostClassification do
expect(described_class.active_model_name_for(:sentiment)).to eq("custom/sentiment-model")
end
- it "uses a single legacy untyped custom classifier for sentiment read paths" do
+ it "uses a single dedicated untyped custom classifier for sentiment read paths" do
SiteSetting.ai_sentiment_model_configs = [
{
model_name: "custom/classifier",
@@ -36,7 +37,7 @@ RSpec.describe DiscourseAi::Sentiment::PostClassification do
expect(described_class.active_model_name_for(:sentiment)).to eq("custom/classifier")
end
- it "prefers the default emotion classifier when multiple legacy emotion classifiers are configured" do
+ it "prefers the default emotion classifier when multiple dedicated emotion classifiers are configured" do
expect(described_class.active_model_name_for(:emotion)).to eq(
DiscourseAi::Sentiment::Constants::EMOTION_MODEL,
)
@@ -163,16 +164,20 @@ RSpec.describe DiscourseAi::Sentiment::PostClassification do
)
end
- it "skips a lone legacy untyped sentiment config when sentiment strategy is agent" do
+ it "skips a lone dedicated untyped sentiment config when sentiment strategy is agent" do
SiteSetting.ai_sentiment_model_configs = [
- { model_name: "custom/legacy", endpoint: "https://legacy.example.com", api_key: "123" },
+ {
+ model_name: "custom/dedicated",
+ endpoint: "https://dedicated.example.com",
+ api_key: "123",
+ },
].to_json
SiteSetting.ai_sentiment_sentiment_classification_strategy = "agent"
SiteSetting.ai_sentiment_sentiment_agent = ai_agent.id
model_names = post_classification.classifiers.map { |c| c[:model_name] }
expect(model_names).to include(DiscourseAi::Sentiment::Constants::SENTIMENT_AGENT_MODEL)
- expect(model_names).not_to include("custom/legacy")
+ expect(model_names).not_to include("custom/dedicated")
end
it "skips storing when the agent returns no usable classification" do
@@ -213,12 +218,9 @@ RSpec.describe DiscourseAi::Sentiment::PostClassification do
SiteSetting.ai_sentiment_sentiment_agent = agent_without_default_llm.id
classifier =
- post_classification.send(
- :agent_classifier,
- :sentiment,
- agent_without_default_llm.id,
- DiscourseAi::Sentiment::Constants::SENTIMENT_AGENT_MODEL,
- )
+ post_classification.classifiers.find do |entry|
+ entry[:classification_type] == "sentiment"
+ end
expect(classifier[:model]).to eq(newest_llm_model)
end
diff --git a/plugins/discourse-ai/spec/migrations/copy_sentiment_enabled_to_emotion_enabled_spec.rb b/plugins/discourse-ai/spec/migrations/copy_sentiment_enabled_to_emotion_enabled_spec.rb
new file mode 100644
index 00000000000..8530fb2ff9e
--- /dev/null
+++ b/plugins/discourse-ai/spec/migrations/copy_sentiment_enabled_to_emotion_enabled_spec.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "plugins/discourse-ai/db/migrate/20261021061850_copy_sentiment_enabled_to_emotion_enabled.rb",
+ )
+
+RSpec.describe CopySentimentEnabledToEmotionEnabled do
+ before do
+ @original_verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ DB.exec(<<~SQL)
+ DELETE FROM site_settings
+ WHERE name IN ('ai_sentiment_enabled', 'ai_sentiment_emotion_enabled')
+ SQL
+ end
+
+ after { ActiveRecord::Migration.verbose = @original_verbose }
+
+ %w[t f].each do |value|
+ it "copies the stored #{value} value and datatype idempotently" do
+ DB.exec(<<~SQL, value: value)
+ INSERT INTO site_settings (name, value, data_type, created_at, updated_at)
+ VALUES ('ai_sentiment_enabled', :value, 5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
+ SQL
+
+ 2.times { described_class.new.up }
+
+ expect(DB.query_single(<<~SQL)).to eq([value, value])
+ SELECT value FROM site_settings
+ WHERE name IN ('ai_sentiment_enabled', 'ai_sentiment_emotion_enabled')
+ SQL
+ expect(DB.query_single(<<~SQL)).to eq([5])
+ SELECT data_type FROM site_settings WHERE name = 'ai_sentiment_emotion_enabled'
+ SQL
+ end
+
+ it "preserves an explicit new #{value} setting" do
+ DB.exec(<<~SQL, value: value, old_value: value == "t" ? "f" : "t")
+ INSERT INTO site_settings (name, value, data_type, created_at, updated_at)
+ VALUES ('ai_sentiment_enabled', :old_value, 5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
+ ('ai_sentiment_emotion_enabled', :value, 5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
+ SQL
+
+ described_class.new.up
+
+ expect(DB.query_single(<<~SQL)).to eq([value])
+ SELECT value FROM site_settings WHERE name = 'ai_sentiment_emotion_enabled'
+ SQL
+ end
+ end
+
+ it "inserts nothing without a stored source setting" do
+ described_class.new.up
+
+ expect(DB.query_single(<<~SQL)).to be_empty
+ SELECT name FROM site_settings
+ WHERE name IN ('ai_sentiment_enabled', 'ai_sentiment_emotion_enabled')
+ SQL
+ end
+
+ it "cannot be reversed" do
+ expect { described_class.new.down }.to raise_error(ActiveRecord::IrreversibleMigration)
+ end
+end
diff --git a/plugins/discourse-ai/spec/reports/sentiment_analysis_spec.rb b/plugins/discourse-ai/spec/reports/sentiment_analysis_spec.rb
index 7a3aadd470d..abe3be15955 100644
--- a/plugins/discourse-ai/spec/reports/sentiment_analysis_spec.rb
+++ b/plugins/discourse-ai/spec/reports/sentiment_analysis_spec.rb
@@ -32,4 +32,72 @@ RSpec.describe DiscourseAi::Sentiment::SentimentAnalysisReport do
expect(report.data).to all(be_a(Hash))
expect { Report.cache(report) }.not_to raise_error
end
+
+ it "selects decision results in grouped and overall reports without counting dedicated results" do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ classification: {
+ negative: 0.8,
+ neutral: 0.1,
+ positive: 0.1,
+ },
+ )
+ Fabricate(
+ :sentiment_classification,
+ target: post_2,
+ classification: {
+ negative: 0.8,
+ neutral: 0.1,
+ positive: 0.1,
+ },
+ )
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ model_used: "discourse-ai/sentiment-decision",
+ classification: {
+ negative: 0.15,
+ neutral: 0.2,
+ positive: 0.65,
+ },
+ )
+
+ report = Report.find("sentiment_analysis")
+
+ expect(report.data).to contain_exactly(
+ {
+ category_id: category.id,
+ category_name: category.name,
+ positive_count: 1,
+ negative_count: 0,
+ total_count: 1,
+ },
+ )
+ expect(Report.find("overall_sentiment").data.sole[:data].map { |point| point[:y] }).to eq([1])
+ end
+
+ it "uses the emotion decision key without changing emotion report thresholds" do
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "decision_model"
+ Fabricate(
+ :sentiment_classification,
+ target: post_2,
+ model_used: DiscourseAi::Sentiment::Constants::EMOTION_MODEL,
+ classification: {
+ joy: 0.9,
+ },
+ )
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ model_used: "discourse-ai/emotion-decision",
+ classification: {
+ joy: 0.15,
+ approval: 0.85,
+ },
+ )
+
+ expect(Report.find("emotion_joy").data.map { |point| point[:y] }).to eq([1])
+ end
end
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_features_controller_spec.rb
index 7086f1e95f6..ed0a26d0f55 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
@@ -63,6 +63,92 @@ RSpec.describe DiscourseAi::Admin::AiFeaturesController do
)
end
+ it "lists mixed sentiment backends independently of workflows without exposing endpoint credentials" do
+ decision_model = Fabricate(:decision_model)
+ endpoint_config = {
+ classification_type: "emotion",
+ model_name: "custom/emotion",
+ endpoint: "srv://private-classifier.example.com",
+ api_key: "private-classifier-key",
+ }
+ SiteSetting.enable_discourse_workflows = false
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_model_configs = [endpoint_config].to_json
+
+ get "/admin/plugins/discourse-ai/ai-features.json"
+
+ expect(response.status).to eq(200)
+ sentiment_module =
+ response.parsed_body["ai_features"].find { |entry| entry["module_name"] == "sentiment" }
+ expect(sentiment_module["module_enabled"]).to eq(true)
+ expect(sentiment_module["features"]).to contain_exactly(
+ {
+ "name" => "sentiment",
+ "agents" => [],
+ "llm_models" => [],
+ "enabled" => true,
+ "backend" => "decision_model",
+ "ready" => true,
+ "decision_model" => {
+ "id" => decision_model.id,
+ "display_name" => decision_model.display_name,
+ },
+ },
+ {
+ "name" => "emotion",
+ "agents" => [],
+ "llm_models" => [],
+ "enabled" => true,
+ "backend" => "classification_model",
+ "ready" => true,
+ "classification_model" => {
+ "model_name" => endpoint_config[:model_name],
+ },
+ },
+ )
+ expect(response.body).not_to include(
+ endpoint_config[:endpoint],
+ endpoint_config[:api_key],
+ decision_model.url,
+ decision_model.api_key,
+ )
+ end
+
+ it "keeps enabled sentiment discoverable when its decision model is unconfigured" do
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+
+ get "/admin/plugins/discourse-ai/ai-features.json"
+
+ sentiment_module =
+ response.parsed_body["ai_features"].find { |entry| entry["module_name"] == "sentiment" }
+ expect(sentiment_module["module_enabled"]).to eq(true)
+ expect(sentiment_module["features"].find { |entry| entry["name"] == "sentiment" }).to include(
+ "enabled" => true,
+ "ready" => false,
+ "decision_model" => nil,
+ )
+ end
+
+ it "lists the module as enabled for either capability without coupling the cards" do
+ [[true, false], [false, true]].each do |sentiment, emotion|
+ SiteSetting.ai_sentiment_enabled = sentiment
+ SiteSetting.ai_sentiment_emotion_enabled = emotion
+
+ get "/admin/plugins/discourse-ai/ai-features.json"
+
+ sentiment_module =
+ response.parsed_body["ai_features"].find { |entry| entry["module_name"] == "sentiment" }
+ expect(sentiment_module["module_enabled"]).to eq(true)
+ expect(
+ sentiment_module["features"].to_h { |entry| [entry["name"], entry["enabled"]] },
+ ).to eq("sentiment" => sentiment, "emotion" => emotion)
+ end
+ end
+
it "includes automation-related features" do
SiteSetting.discourse_automation_enabled = true
@@ -75,7 +161,51 @@ RSpec.describe DiscourseAi::Admin::AiFeaturesController do
end
end
+ describe "sentiment background processing settings" do
+ it "exposes both controls in the sentiment settings area" do
+ get "/admin/config/site_settings.json", params: { filter_area: "ai-features/sentiment" }
+
+ expect(response.status).to eq(200)
+ setting_names = response.parsed_body["site_settings"].map { |setting| setting["setting"] }
+ expect(setting_names).to include(
+ "ai_sentiment_backfill_maximum_posts_per_hour",
+ "ai_sentiment_backfill_post_max_age_days",
+ )
+ end
+
+ it "rejects a negative post age while allowing zero" do
+ original_age = SiteSetting.ai_sentiment_backfill_post_max_age_days
+
+ put "/admin/site_settings/ai_sentiment_backfill_post_max_age_days.json",
+ params: {
+ ai_sentiment_backfill_post_max_age_days: -1,
+ }
+
+ expect(response.status).to eq(422)
+ expect(SiteSetting.ai_sentiment_backfill_post_max_age_days).to eq(original_age)
+
+ put "/admin/site_settings/ai_sentiment_backfill_post_max_age_days.json",
+ params: {
+ ai_sentiment_backfill_post_max_age_days: 0,
+ }
+
+ expect(response.status).to eq(204)
+ expect(SiteSetting.ai_sentiment_backfill_post_max_age_days).to eq(0)
+ end
+ end
+
describe "#edit" do
+ it "returns the sentiment configuration module" do
+ get "/admin/plugins/discourse-ai/ai-features/#{DiscourseAi::Configuration::Module::SENTIMENT_ID}/edit.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["module_name"]).to eq("sentiment")
+ expect(response.parsed_body["module_enabled"]).to eq(false)
+ expect(response.parsed_body["features"].map { |entry| entry["enabled"] }).to eq(
+ [false, false],
+ )
+ end
+
it "returns a success response" do
get "/admin/plugins/discourse-ai/ai-features/1/edit.json"
expect(response.parsed_body["module_name"]).to eq("summarization")
diff --git a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
index f9fdc86e617..322735b1df7 100644
--- a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
@@ -36,6 +36,44 @@ RSpec.describe DiscourseAi::Sentiment::SentimentController do
end
end
+ it "selects decision results for drilldown while preserving the sentiment threshold" do
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ Fabricate(
+ :sentiment_classification,
+ target: post,
+ model_used: "discourse-ai/sentiment-decision",
+ classification: {
+ negative: 0.8,
+ neutral: 0.1,
+ positive: 0.1,
+ },
+ )
+ Fabricate(
+ :sentiment_classification,
+ target: post_2,
+ model_used: "discourse-ai/sentiment-decision",
+ classification: {
+ negative: 0.1,
+ neutral: 0.3,
+ positive: 0.6,
+ },
+ )
+
+ get "/discourse-ai/sentiment/posts",
+ params: {
+ group_by: "category",
+ group_value: category.name,
+ }
+
+ expect(response).to be_successful
+ expect(
+ response.parsed_body["posts"].map { |result| result.slice("post_id", "sentiment") },
+ ).to contain_exactly(
+ { "post_id" => post.id, "sentiment" => "negative" },
+ { "post_id" => post_2.id, "sentiment" => "neutral" },
+ )
+ end
+
context "when signed in as a moderator" do
fab!(:moderator)
diff --git a/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
index c77fd5a2e2a..d52d5ca0368 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe "Managing decision models" do
before do
enable_current_plugin
- SiteSetting.enable_discourse_workflows = true
+ SiteSetting.enable_discourse_workflows = false
sign_in(admin)
end
diff --git a/plugins/discourse-ai/spec/system/admin_ai_features_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_features_spec.rb
index c36a8b8d09d..a74ed090889 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_features_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_features_spec.rb
@@ -153,6 +153,91 @@ RSpec.describe "Admin AI features configuration" do
expect(field.component).to have_content("Test LLM Beta")
end
+ it "lets admins discover mixed sentiment backends without workflows" do
+ decision_model = Fabricate(:decision_model)
+ SiteSetting.enable_discourse_workflows = false
+ SiteSetting.ai_sentiment_enabled = true
+ SiteSetting.ai_sentiment_emotion_enabled = true
+ SiteSetting.ai_sentiment_sentiment_classification_strategy = "decision_model"
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_classification_strategy = "agent"
+ SiteSetting.ai_sentiment_emotion_agent = summarization_agent.id
+
+ ai_features_page.visit
+ ai_features_page.toggle_enabled
+
+ expect(ai_features_page).to have_feature_decision_model("sentiment", decision_model)
+ expect(ai_features_page).to have_feature_agent("emotion", summarization_agent.name)
+ expect(ai_features_page).to have_no_feature_groups("sentiment")
+ expect(ai_features_page).to have_no_feature_groups("emotion")
+ end
+
+ it "lets admins switch sentiment backends before saving and retain their selections" do
+ decision_model = Fabricate(:decision_model)
+ SiteSetting.ai_sentiment_sentiment_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_emotion_decision_model = decision_model.id
+ SiteSetting.ai_sentiment_sentiment_agent = summarization_agent.id
+
+ page.visit(
+ "/admin/plugins/discourse-ai/ai-features/#{DiscourseAi::Configuration::Module::SENTIMENT_ID}/edit",
+ )
+
+ expect(form.field("ai_sentiment_enabled")).to be_unchecked
+ expect(form.field("ai_sentiment_emotion_enabled")).to be_unchecked
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_classification_strategy")
+ form.field("ai_sentiment_enabled").check
+ expect(form.field("ai_sentiment_emotion_enabled")).to be_unchecked
+ expect(form).to have_no_field_with_name("ai_sentiment_emotion_classification_strategy")
+ expect(ai_features_page).to have_dedicated_configuration_link
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_agent")
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_decision_model")
+ expect(form).to have_no_field_with_name("ai_sentiment_model_configs")
+ expect(form.field("ai_sentiment_backfill_maximum_posts_per_hour")).to have_value("2500")
+ expect(form.field("ai_sentiment_backfill_post_max_age_days")).to have_value("60")
+ form.field("ai_sentiment_backfill_maximum_posts_per_hour").fill_in("120")
+ form.field("ai_sentiment_backfill_post_max_age_days").fill_in("0")
+
+ form.field("ai_sentiment_sentiment_classification_strategy").select("agent")
+ expect(form.field("ai_sentiment_sentiment_agent")).to have_value(summarization_agent.id.to_s)
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_decision_model")
+
+ form.field("ai_sentiment_sentiment_classification_strategy").select("decision_model")
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_agent")
+ expect(form.field("ai_sentiment_sentiment_decision_model")).to have_value(
+ decision_model.id.to_s,
+ )
+ form.submit
+ expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.saved"))
+
+ page.refresh
+ expect(form.field("ai_sentiment_enabled")).to be_checked
+ expect(form.field("ai_sentiment_emotion_enabled")).to be_unchecked
+ expect(form).to have_no_field_with_name("ai_sentiment_emotion_classification_strategy")
+ expect(form.field("ai_sentiment_backfill_maximum_posts_per_hour")).to have_value("120")
+ expect(form.field("ai_sentiment_backfill_post_max_age_days")).to have_value("0")
+ expect(form.field("ai_sentiment_sentiment_classification_strategy")).to have_value(
+ "decision_model",
+ )
+ expect(form.field("ai_sentiment_sentiment_decision_model")).to have_value(
+ decision_model.id.to_s,
+ )
+
+ form.field("ai_sentiment_sentiment_classification_strategy").select("agent")
+ expect(form.field("ai_sentiment_sentiment_agent")).to have_value(summarization_agent.id.to_s)
+
+ form.field("ai_sentiment_enabled").uncheck
+ form.field("ai_sentiment_emotion_enabled").check
+ form.field("ai_sentiment_emotion_classification_strategy").select("decision_model")
+ form.submit
+ expect(PageObjects::Components::Toasts.new).to have_success(I18n.t("js.saved"))
+
+ page.refresh
+ expect(form.field("ai_sentiment_enabled")).to be_unchecked
+ expect(form.field("ai_sentiment_emotion_enabled")).to be_checked
+ expect(form).to have_no_field_with_name("ai_sentiment_sentiment_classification_strategy")
+ expect(form.field("ai_sentiment_emotion_decision_model")).to have_value(decision_model.id.to_s)
+ end
+
context "with external AI features" do
let(:fake_plugin) do
plugin = Plugin::Instance.new
diff --git a/plugins/discourse-ai/spec/system/page_objects/pages/admin_ai_features.rb b/plugins/discourse-ai/spec/system/page_objects/pages/admin_ai_features.rb
index ba50299c08b..502fbefafdc 100644
--- a/plugins/discourse-ai/spec/system/page_objects/pages/admin_ai_features.rb
+++ b/plugins/discourse-ai/spec/system/page_objects/pages/admin_ai_features.rb
@@ -52,6 +52,27 @@ module PageObjects
list_items.sort == groups.sort
end
+ def has_feature_decision_model?(feature_name, decision_model)
+ page.find(
+ "#{FEATURES_PAGE} .ai-feature-card[data-feature-name='#{feature_name}']",
+ ).has_link?(
+ decision_model.display_name,
+ href: "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}/edit",
+ )
+ end
+
+ def has_no_feature_groups?(feature_name)
+ page.has_no_css?(
+ "#{FEATURES_PAGE} .ai-feature-card[data-feature-name='#{feature_name}'] .ai-feature-card__groups",
+ )
+ end
+
+ def has_dedicated_configuration_link?
+ page.has_link?(
+ href: "/admin/site_settings/category/all_results?filter=ai_sentiment_model_configs",
+ )
+ end
+
def click_edit_module(module_name)
page.find("#{FEATURES_PAGE} .ai-module[data-module-name='#{module_name}'] .edit").click
end
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-card-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-card-test.gjs
index 73ed08fa9b3..c492e6e67e6 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-card-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-card-test.gjs
@@ -2,6 +2,7 @@ import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setPrefix } from "discourse/lib/get-url";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { i18n } from "discourse-i18n";
import AiFeatureCard from "discourse/plugins/discourse-ai/discourse/components/ai-feature-card";
module("Integration | Component | AiFeatureCard", function (hooks) {
@@ -42,6 +43,9 @@ module("Integration | Component | AiFeatureCard", function (hooks) {
</template>
);
+ assert
+ .dom(".ai-feature-card__backend")
+ .doesNotExist("does not change cards without backend metadata");
assert
.dom(".ai-feature-card__item-groups li")
.exists({ count: 2 }, "renders each group once");
@@ -61,4 +65,91 @@ module("Integration | Component | AiFeatureCard", function (hooks) {
.exists({ count: 1 }, "renders the other group")
.hasAttribute("href", "/forum/g/staff", "prefixes every group path");
});
+ for (const backend of ["agent", "decision_model", "classification_model"]) {
+ test(`renders the ${backend} configuration`, async function (assert) {
+ const feature = {
+ name: "sentiment",
+ enabled: true,
+ backend,
+ ready: true,
+ agents: [{ id: 1, name: "Classifier" }],
+ llm_models: [{ id: 2, name: "Classifier LLM" }],
+ decision_model: { id: 3, display_name: "Fast decisions" },
+ classification_model: { model_name: "dedicated/sentiment" },
+ };
+ await render(<template><AiFeatureCard @feature={{feature}} /></template>);
+ assert
+ .dom(".ai-feature-card__backend")
+ .includesText(
+ i18n(`discourse_ai.features.backends.${backend}`),
+ "labels the selected backend"
+ );
+ assert
+ .dom(".ai-feature-card__readiness")
+ .doesNotExist("does not warn when configured");
+ if (backend === "agent") {
+ assert
+ .dom(".ai-feature-card__agent-link")
+ .hasText("Classifier", "shows the selected agent");
+ assert
+ .dom(".ai-feature-card__llm-link")
+ .hasText("Classifier LLM", "shows the effective LLM");
+ } else {
+ assert
+ .dom(".ai-feature-card__agent")
+ .doesNotExist("does not show irrelevant agents");
+ assert
+ .dom(".ai-feature-card__llm")
+ .doesNotExist("does not show irrelevant LLMs");
+ assert
+ .dom(
+ `.ai-feature-card__${backend === "decision_model" ? "decision-model" : "classification-model"}-link`
+ )
+ .hasText(
+ backend === "decision_model"
+ ? "Fast decisions"
+ : "dedicated/sentiment",
+ "links the configured model"
+ );
+ }
+ });
+
+ test(`shows unconfigured ${backend} separately from enabled`, async function (assert) {
+ const feature = {
+ name: "sentiment",
+ backend,
+ ready: false,
+ enabled: true,
+ agents: [],
+ llm_models: [],
+ };
+ await render(<template><AiFeatureCard @feature={{feature}} /></template>);
+ assert
+ .dom(".ai-feature-card__readiness")
+ .hasText(
+ i18n("discourse_ai.features.not_configured"),
+ "shows configuration readiness"
+ );
+ assert
+ .dom(".ai-feature-card__feature-name")
+ .doesNotIncludeText(
+ "disabled",
+ "does not conflate readiness with enablement"
+ );
+ if (backend !== "agent") {
+ assert
+ .dom(".ai-feature-card")
+ .doesNotIncludeText(
+ i18n("discourse_ai.features.no_llm"),
+ "no missing LLM warning"
+ );
+ assert
+ .dom(".ai-feature-card")
+ .doesNotIncludeText(
+ i18n("discourse_ai.features.no_agent"),
+ "no missing agent warning"
+ );
+ }
+ });
+ }
});
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-settings-editor-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-settings-editor-test.gjs
new file mode 100644
index 00000000000..7b43bceea3c
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-feature-settings-editor-test.gjs
@@ -0,0 +1,368 @@
+import { findAll, render, settled } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import SiteSetting from "discourse/admin/models/site-setting";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import form from "discourse/tests/helpers/form-kit-helper";
+import { i18n } from "discourse-i18n";
+import { getSettingGroupsForFeature } from "discourse/plugins/discourse-ai/discourse/lib/ai-feature-setting-groups";
+import FeatureEditor from "discourse/plugins/discourse-ai/discourse/templates/admin-plugins/show/discourse-ai-features/edit";
+
+module(
+ "Integration | Component | AI feature settings editor",
+ function (hooks) {
+ setupRenderingTest(hooks, { stubRouter: true });
+
+ test("keeps backend guidance beside the last active section using that backend", async function (assert) {
+ const settings = [];
+ for (const type of ["sentiment", "emotion"]) {
+ const enabled =
+ type === "sentiment"
+ ? "ai_sentiment_enabled"
+ : "ai_sentiment_emotion_enabled";
+ const strategy = `ai_sentiment_${type}_classification_strategy`;
+ settings.push(
+ SiteSetting.create({ setting: enabled, type: "bool", value: true }),
+ SiteSetting.create({
+ setting: strategy,
+ type: "enum",
+ value: "decision_model",
+ valid_values: ["agent", "decision_model", "classification_model"],
+ depends_on: [enabled],
+ depends_behavior: "hidden",
+ }),
+ SiteSetting.create({
+ setting: `ai_sentiment_${type}_decision_model`,
+ type: "enum",
+ value: "1",
+ valid_values: ["1"],
+ depends_on: [enabled, strategy],
+ depends_on_values: { [strategy]: ["decision_model"] },
+ depends_behavior: "hidden",
+ })
+ );
+ }
+ const controller = this.owner.lookup(
+ "controller:admin-plugins/show/discourse-ai-features/edit"
+ );
+ controller.settings = settings;
+ const model = {
+ module_name: "sentiment",
+ feature_settings: settings,
+ settingGroups: getSettingGroupsForFeature("sentiment"),
+ formData: Object.fromEntries(
+ settings.map((setting) => [setting.setting, setting.value])
+ ),
+ };
+ await render(
+ <template>
+ <FeatureEditor @controller={{controller}} @model={{model}} />
+ </template>
+ );
+ const [sentimentSection, emotionSection] = findAll(".form-kit__section");
+ const assertDecisionAfter = (section, message) => {
+ assert
+ .dom(".ai-feature-editor__model-change-warning")
+ .exists({ count: 1 }, "shows the shared warning exactly once");
+ assert
+ .dom(".ai-feature-editor__decision-models-link")
+ .exists(
+ { count: 1 },
+ "shows the shared management link exactly once"
+ );
+ assert
+ .dom(section.nextElementSibling)
+ .hasClass("ai-feature-editor__model-change-warning", message);
+ assert
+ .dom(section.nextElementSibling.nextElementSibling)
+ .hasClass(
+ "ai-feature-editor__decision-models-link",
+ "management follows the applicable model warning"
+ );
+ };
+ const assertDedicatedAfter = (section, message) => {
+ assert
+ .dom(".ai-feature-editor__dedicated-link")
+ .exists({ count: 1 }, "shows dedicated configuration exactly once")
+ .hasText(
+ i18n("discourse_ai.features.sentiment.configure_dedicated"),
+ "labels dedicated endpoint configuration"
+ );
+ assert
+ .dom(section.nextElementSibling)
+ .hasClass("ai-feature-editor__dedicated-description", message);
+ };
+
+ assertDecisionAfter(
+ emotionSection,
+ "both decision classifiers share guidance after the last model controls"
+ );
+ await form().field("ai_sentiment_emotion_enabled").toggle();
+ assertDecisionAfter(
+ sentimentSection,
+ "sentiment-only guidance is not attached to disabled emotion"
+ );
+ await form().field("ai_sentiment_emotion_enabled").toggle();
+ await form().field("ai_sentiment_enabled").toggle();
+ assertDecisionAfter(
+ emotionSection,
+ "emotion-only guidance stays beside emotion"
+ );
+ await form().field("ai_sentiment_enabled").toggle();
+ await form()
+ .field("ai_sentiment_sentiment_classification_strategy")
+ .fillIn("classification_model");
+ assertDedicatedAfter(
+ sentimentSection,
+ "dedicated sentiment guidance stays with sentiment"
+ );
+ assertDecisionAfter(
+ emotionSection,
+ "decision emotion guidance stays with emotion in mixed mode"
+ );
+ await form()
+ .field("ai_sentiment_sentiment_classification_strategy")
+ .fillIn("decision_model");
+ await form()
+ .field("ai_sentiment_emotion_classification_strategy")
+ .fillIn("classification_model");
+ assertDecisionAfter(
+ sentimentSection,
+ "decision sentiment guidance stays with sentiment in mixed mode"
+ );
+ assertDedicatedAfter(
+ emotionSection,
+ "dedicated emotion guidance stays with emotion"
+ );
+ await form()
+ .field("ai_sentiment_sentiment_classification_strategy")
+ .fillIn("classification_model");
+ assertDedicatedAfter(
+ emotionSection,
+ "both dedicated classifiers share one link after the last applicable section"
+ );
+ assert
+ .dom(".ai-feature-editor__model-change-warning")
+ .doesNotExist("dedicated-only mode has no decision warning");
+ await form().field("ai_sentiment_emotion_enabled").toggle();
+ assertDedicatedAfter(
+ sentimentSection,
+ "sentiment-only dedicated guidance moves away from disabled emotion"
+ );
+ await form().field("ai_sentiment_enabled").toggle();
+ assert
+ .dom(".ai-feature-editor__dedicated-link")
+ .doesNotExist("disabled classifiers have no dedicated guidance");
+ assert
+ .dom(".ai-feature-editor__decision-models-link")
+ .doesNotExist("disabled classifiers have no decision guidance");
+ });
+
+ test("switches controls using draft dependencies and retains inactive configuration", async function (assert) {
+ const settings = [
+ SiteSetting.create({
+ setting: "ai_sentiment_backfill_maximum_posts_per_hour",
+ type: "integer",
+ value: 2500,
+ min: 0,
+ max: 10000,
+ }),
+ SiteSetting.create({
+ setting: "ai_sentiment_backfill_post_max_age_days",
+ type: "integer",
+ value: 60,
+ min: 0,
+ }),
+ ];
+ for (const type of ["sentiment", "emotion"]) {
+ const enabled =
+ type === "sentiment"
+ ? "ai_sentiment_enabled"
+ : "ai_sentiment_emotion_enabled";
+ const strategy = `ai_sentiment_${type}_classification_strategy`;
+ settings.push(
+ SiteSetting.create({ setting: enabled, type: "bool", value: true }),
+ SiteSetting.create({
+ setting: strategy,
+ type: "enum",
+ value: "agent",
+ valid_values: ["agent", "decision_model", "classification_model"],
+ depends_on: [enabled],
+ depends_behavior: "hidden",
+ })
+ );
+ for (const backend of ["agent", "decision_model"]) {
+ settings.push(
+ SiteSetting.create({
+ setting: `ai_sentiment_${type}_${backend}`,
+ type: "enum",
+ value: backend === "agent" ? "1" : "2",
+ valid_values: ["1", "2", "3"],
+ depends_on: [enabled, strategy],
+ depends_on_values: { [strategy]: [backend] },
+ depends_behavior: "hidden",
+ })
+ );
+ }
+ }
+ const controller = this.owner.lookup(
+ "controller:admin-plugins/show/discourse-ai-features/edit"
+ );
+ controller.settings = settings;
+ const model = {
+ module_name: "sentiment",
+ feature_settings: settings,
+ settingGroups: getSettingGroupsForFeature("sentiment"),
+ formData: Object.fromEntries(
+ settings.map((setting) => [setting.setting, setting.value])
+ ),
+ };
+ await render(
+ <template>
+ <FeatureEditor @controller={{controller}} @model={{model}} />
+ </template>
+ );
+ assert
+ .dom('[data-name="ai_sentiment_backfill_maximum_posts_per_hour"] label')
+ .includesText(
+ "Posts per hour",
+ "shows the background rate with a short label"
+ );
+ assert
+ .dom('[data-name="ai_sentiment_backfill_post_max_age_days"] label')
+ .includesText(
+ "Post age limit (days)",
+ "shows the age limit with a short label"
+ );
+ assert
+ .dom('[data-name="ai_sentiment_backfill_post_max_age_days"] input')
+ .hasAttribute(
+ "min",
+ "0",
+ "allows today-only processing but not negative ages"
+ );
+ assert
+ .dom(".ai-feature-editor__section-description")
+ .hasText(
+ i18n("discourse_ai.features.sentiment.background_description"),
+ "explains scan timing, request costs, and retries once"
+ );
+ for (const type of ["sentiment", "emotion"]) {
+ const field = `[data-name="ai_sentiment_${type}_classification_strategy"]`;
+ assert
+ .dom(`${field} label`)
+ .includesText("Backend", "uses a concise backend label");
+ for (const [value, label] of Object.entries({
+ agent: "LLM agent",
+ decision_model: "Decision model",
+ classification_model: "Dedicated classification endpoint",
+ })) {
+ assert
+ .dom(`${field} option[value="${value}"]`)
+ .hasText(
+ label,
+ "translates the backend without changing its stored value"
+ );
+ }
+ }
+ assert
+ .dom('[data-name="ai_sentiment_sentiment_agent"]')
+ .exists("shows the selected agent backend");
+ assert
+ .dom('[data-name="ai_sentiment_sentiment_decision_model"]')
+ .doesNotExist("hides the inactive decision selector");
+ assert
+ .dom(".ai-feature-editor__dedicated-link")
+ .doesNotExist(
+ "no endpoint link when neither capability uses dedicated"
+ );
+ await form().field("ai_sentiment_sentiment_agent").fillIn("3");
+ await form()
+ .field("ai_sentiment_sentiment_classification_strategy")
+ .fillIn("decision_model");
+ assert
+ .dom('[data-name="ai_sentiment_sentiment_agent"]')
+ .doesNotExist("immediately hides the agent selector");
+ assert
+ .dom('[data-name="ai_sentiment_sentiment_decision_model"]')
+ .exists("immediately shows the decision selector before saving");
+ assert
+ .dom('[data-name="ai_sentiment_emotion_agent"]')
+ .exists("does not change the independent emotion backend");
+ assert
+ .dom(".ai-feature-editor__decision-models-link")
+ .exists("offers decision model management");
+ await form()
+ .field("ai_sentiment_emotion_classification_strategy")
+ .fillIn("decision_model");
+ assert
+ .dom(".ai-feature-editor__model-change-warning")
+ .exists(
+ { count: 1 },
+ "shows one model-change warning for both capabilities"
+ );
+ assert
+ .dom('[data-name="ai_sentiment_emotion_decision_model"]')
+ .includesText(
+ i18n("discourse_ai.features.sentiment.emotion_scores"),
+ "preserves the 28-emotion distribution note"
+ );
+ await form()
+ .field("ai_sentiment_emotion_classification_strategy")
+ .fillIn("classification_model");
+ assert
+ .dom(".ai-feature-editor__dedicated-link")
+ .exists("either dedicated capability exposes endpoint configuration");
+ assert
+ .dom('[data-name="ai_sentiment_model_configs"]')
+ .doesNotExist("never renders a masked JSON control");
+ await form()
+ .field("ai_sentiment_sentiment_classification_strategy")
+ .fillIn("agent");
+ assert
+ .form()
+ .field("ai_sentiment_sentiment_agent")
+ .hasValue("3", "keeps the unsaved inactive agent selection");
+ assert.strictEqual(
+ controller.formApi.get("ai_sentiment_sentiment_decision_model"),
+ "2",
+ "keeps the inactive decision model"
+ );
+ assert.strictEqual(
+ model.formData.ai_sentiment_sentiment_agent,
+ "1",
+ "does not mutate the saved form data"
+ );
+ controller.formApi.set(
+ "ai_sentiment_emotion_classification_strategy",
+ "agent"
+ );
+ await settled();
+ assert
+ .dom(".ai-feature-editor__dedicated-link")
+ .doesNotExist("hides the endpoint link when no longer needed");
+
+ await form().field("ai_sentiment_enabled").toggle();
+ assert
+ .dom('[data-name="ai_sentiment_sentiment_classification_strategy"]')
+ .doesNotExist("disabling sentiment hides only its controls");
+ assert
+ .dom('[data-name="ai_sentiment_emotion_classification_strategy"]')
+ .exists("emotion remains independently enabled");
+ await form().field("ai_sentiment_emotion_enabled").toggle();
+ assert
+ .dom('[data-name="ai_sentiment_emotion_classification_strategy"]')
+ .doesNotExist("both capabilities can be disabled");
+ assert
+ .dom('[data-name="ai_sentiment_backfill_maximum_posts_per_hour"]')
+ .exists("processing limits remain configurable before enabling");
+ await form().field("ai_sentiment_enabled").toggle();
+ assert
+ .form()
+ .field("ai_sentiment_sentiment_agent")
+ .hasValue("3", "re-enabling retains the selected agent");
+ assert
+ .dom('[data-name="ai_sentiment_emotion_agent"]')
+ .doesNotExist("re-enabling sentiment leaves emotion off");
+ });
+ }
+);
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-features-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-features-test.gjs
index cd55a15ee39..ee821486b63 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-features-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-features-test.gjs
@@ -1,5 +1,5 @@
import Service from "@ember/service";
-import { render, settled } from "@ember/test-helpers";
+import { fillIn, render, settled } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
@@ -112,4 +112,57 @@ module("Integration | Component | AiFeatures", function (hooks) {
"updates the rendered LLM link from the refreshed payload"
);
});
+ test("filters sentiment cards by decision and dedicated model names without groups", async function (assert) {
+ const features = [
+ {
+ id: 14,
+ module_name: "sentiment",
+ module_enabled: true,
+ features: [
+ {
+ name: "sentiment",
+ enabled: true,
+ backend: "decision_model",
+ ready: true,
+ agents: [],
+ llm_models: [],
+ decision_model: { id: 3, display_name: "Fast decisions" },
+ },
+ {
+ name: "emotion",
+ enabled: true,
+ backend: "classification_model",
+ ready: true,
+ agents: [
+ {
+ id: 1,
+ name: "Unused",
+ allowed_groups: [{ id: 1, name: "staff" }],
+ },
+ ],
+ llm_models: [],
+ classification_model: { model_name: "dedicated/emotions" },
+ },
+ ],
+ },
+ ];
+ await render(<template><AiFeatures @features={{features}} /></template>);
+ assert
+ .dom(".ai-feature-card__groups")
+ .doesNotExist("background classifiers do not display groups");
+ await fillIn(".ai-features__controls input", "FAST DECISIONS");
+ assert
+ .dom(".ai-feature-card")
+ .exists({ count: 1 }, "only the matching decision capability remains");
+ assert
+ .dom('[data-feature-name="sentiment"]')
+ .exists("matches decision display name case insensitively");
+ await fillIn(".ai-features__controls input", "dedicated/emotions");
+ assert
+ .dom(".ai-feature-card")
+ .exists({ count: 1 }, "only the matching dedicated capability remains");
+ assert
+ .dom('[data-feature-name="emotion"]')
+ .exists("matches the dedicated model identifier");
+ });
});
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-model-new-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-model-new-test.gjs
index 4420bc6e8e2..646a01a85a0 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-model-new-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-model-new-test.gjs
@@ -1,6 +1,7 @@
import Service from "@ember/service";
-import { render } from "@ember/test-helpers";
+import { click, render } from "@ember/test-helpers";
import { module, test } from "qunit";
+import sinon from "sinon";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import AiModelNew from "discourse/plugins/discourse-ai/discourse/components/ai-model-new";
@@ -46,4 +47,37 @@ module("Integration | Component | AiModelNew", function (hooks) {
)
.hasAttribute("href", /\/414738$/);
});
+ test("offers decision creation with workflows disabled and no configured models", async function (assert) {
+ this.siteSettings.enable_discourse_workflows = false;
+ const empty = { content: [], resultSetMeta: { presets: [] } };
+ await render(
+ <template>
+ <AiModelNew
+ @decisionModels={{empty}}
+ @embeddings={{empty}}
+ @llms={{empty}}
+ @type="decision"
+ />
+ </template>
+ );
+ const transitionTo = sinon.spy();
+ this.owner.lookup("service:router").transitionTo = transitionTo;
+ assert
+ .dom(".admin-section-landing-item")
+ .exists("offers a manual decision model template");
+ assert
+ .dom(".admin-section-landing-item")
+ .includesText(
+ "Manual configuration",
+ "can configure a decision model without a preset"
+ );
+ await click('[data-template-id="none"] button');
+ assert.true(
+ transitionTo.calledWith(
+ "adminPlugins.show.discourse-ai-decision-models.new",
+ { queryParams: { preset: null } }
+ ),
+ "opens decision model creation"
+ );
+ });
});
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-models-list-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-models-list-test.gjs
index d29e9ffba30..c3d9f98b09e 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-models-list-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-models-list-test.gjs
@@ -5,7 +5,9 @@ import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { usageRoute } from "discourse/plugins/discourse-ai/discourse/components/ai-llm-usages";
-import AiModelsList from "discourse/plugins/discourse-ai/discourse/components/ai-models-list";
+import AiModelsList, {
+ modelTypes,
+} from "discourse/plugins/discourse-ai/discourse/components/ai-models-list";
class FilterState {
@tracked type = null;
@@ -195,7 +197,7 @@ module("Integration | Component | AiModelsList", function (hooks) {
assert.dom(".ai-models-list__row[data-model-type='embedding']").exists();
});
- test("hides decision models when none are passed", async function (assert) {
+ test("offers decision models when none are passed", async function (assert) {
this.llms = { content: [], resultSetMeta: { presets: [] } };
this.embeddings = {
content: [{ id: 2, display_name: "Test embedding", provider: "open_ai" }],
@@ -210,7 +212,7 @@ module("Integration | Component | AiModelsList", function (hooks) {
assert
.dom(".ai-models-list__type-filter option[value='decision']")
- .doesNotExist();
+ .exists("offers the decision type even without models");
});
test("shows the default LLM setting only when filtered to LLMs", async function (assert) {
@@ -261,4 +263,31 @@ module("Integration | Component | AiModelsList", function (hooks) {
assert.dom(".ai-configure-default-llm").exists();
});
+ test("model types are unconditional", function (assert) {
+ assert.deepEqual(
+ modelTypes(),
+ ["llm", "embedding", "decision"],
+ "all types are discoverable without workflow availability"
+ );
+ });
+
+ test("offers model creation when every list is empty and workflows are disabled", async function (assert) {
+ this.siteSettings.enable_discourse_workflows = false;
+ const empty = { content: [], resultSetMeta: { presets: [] } };
+ await render(
+ <template>
+ <AiModelsList
+ @decisionModels={{empty}}
+ @embeddings={{empty}}
+ @llms={{empty}}
+ />
+ </template>
+ );
+ assert
+ .dom(".ai-models-list__new-button")
+ .exists("keeps the header creation action");
+ assert
+ .dom(".ai-models-list__empty-new-button")
+ .exists("offers creation from the empty state");
+ });
});
diff --git a/plugins/discourse-ai/test/javascripts/unit/connectors/admin-sentiment-dashboard-test.js b/plugins/discourse-ai/test/javascripts/unit/connectors/admin-sentiment-dashboard-test.js
new file mode 100644
index 00000000000..f2450df949b
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/connectors/admin-sentiment-dashboard-test.js
@@ -0,0 +1,27 @@
+import { module, test } from "qunit";
+import AISentimentDashboard from "discourse/plugins/discourse-ai/discourse/connectors/admin-dashboard-tabs-after/admin-sentiment-dashbboard";
+
+module("Unit | Connector | admin-sentiment-dashboard", function () {
+ test("offers reports when either capability is enabled", function (assert) {
+ for (const [sentiment, emotion, expected] of [
+ [false, false, false],
+ [true, false, true],
+ [false, true, true],
+ [true, true, true],
+ ]) {
+ assert.strictEqual(
+ AISentimentDashboard.shouldRender(
+ {},
+ {
+ siteSettings: {
+ ai_sentiment_enabled: sentiment,
+ ai_sentiment_emotion_enabled: emotion,
+ },
+ }
+ ),
+ expected,
+ `reports follow independent flags: sentiment=${sentiment}, emotion=${emotion}`
+ );
+ }
+ });
+});
diff --git a/plugins/discourse-ai/test/javascripts/unit/controllers/admin-plugins-show-discourse-ai-features-edit-test.js b/plugins/discourse-ai/test/javascripts/unit/controllers/admin-plugins-show-discourse-ai-features-edit-test.js
index 7079624d039..63393bd0f00 100644
--- a/plugins/discourse-ai/test/javascripts/unit/controllers/admin-plugins-show-discourse-ai-features-edit-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/controllers/admin-plugins-show-discourse-ai-features-edit-test.js
@@ -1,5 +1,6 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
+import { i18n } from "discourse-i18n";
module(
"Unit | Controller | admin-plugins/show/discourse-ai-features/edit",
@@ -92,5 +93,122 @@ module(
"should return undefined for non-existent setting"
);
});
+ test("uses concise sentiment labels and help without changing other settings", function (assert) {
+ const controller = this.owner.lookup(
+ "controller:admin-plugins/show/discourse-ai-features/edit"
+ );
+ const definition = {
+ label: "Original label",
+ description: "Original description",
+ valid_values: [
+ "agent",
+ { value: "decision_model", name: "Original choice" },
+ ],
+ };
+ const labels = {
+ ai_sentiment_enabled: "Enabled",
+ ai_sentiment_sentiment_classification_strategy: "Backend",
+ ai_sentiment_sentiment_agent: "Agent",
+ ai_sentiment_sentiment_decision_model: "Decision model",
+ ai_sentiment_emotion_classification_strategy: "Backend",
+ ai_sentiment_emotion_agent: "Agent",
+ ai_sentiment_emotion_decision_model: "Decision model",
+ ai_sentiment_backfill_maximum_posts_per_hour: "Posts per hour",
+ ai_sentiment_backfill_post_max_age_days: "Post age limit (days)",
+ };
+
+ for (const [setting, label] of Object.entries(labels)) {
+ assert.strictEqual(
+ controller.settingDefinition({ setting, definition }).label,
+ label,
+ `${setting} uses a short label`
+ );
+ }
+ for (const setting of [
+ "ai_sentiment_sentiment_classification_strategy",
+ "ai_sentiment_emotion_classification_strategy",
+ "ai_sentiment_sentiment_agent",
+ "ai_sentiment_emotion_agent",
+ "ai_sentiment_sentiment_decision_model",
+ ]) {
+ assert.strictEqual(
+ controller.settingDefinition({ setting, definition }).description,
+ undefined,
+ `${setting} omits redundant help`
+ );
+ }
+ assert.strictEqual(
+ controller.settingDefinition({
+ setting: "ai_sentiment_emotion_decision_model",
+ definition,
+ }).description,
+ i18n("discourse_ai.features.sentiment.emotion_scores"),
+ "retains the emotion distribution explanation"
+ );
+ assert.strictEqual(
+ controller.settingDefinition({ setting: "ai_bot_enabled", definition }),
+ definition,
+ "leaves unrelated settings untouched"
+ );
+ assert.strictEqual(
+ definition.description,
+ "Original description",
+ "preserves descriptions for the core settings editor"
+ );
+ });
+
+ test("hidden dependencies use draft boolean and enum values", function (assert) {
+ const controller = this.owner.lookup(
+ "controller:admin-plugins/show/discourse-ai-features/edit"
+ );
+ controller.settings = [
+ { setting: "enabled", value: true },
+ { setting: "scope", value: "public" },
+ ];
+ const setting = {
+ depends_behavior: "hidden",
+ depends_on: ["enabled", "scope"],
+ depends_on_values: { scope: ["include", "exclude"] },
+ };
+ assert.true(
+ controller.isSettingVisible(setting, {
+ enabled: true,
+ scope: "include",
+ }),
+ "unsaved enum selection reveals a dependent field"
+ );
+ assert.false(
+ controller.isSettingVisible(setting, {
+ enabled: "false",
+ scope: "include",
+ }),
+ "string false does not satisfy a boolean dependency"
+ );
+ assert.false(
+ controller.isSettingVisible(setting, {
+ enabled: true,
+ scope: "public",
+ }),
+ "all parent dependencies must match"
+ );
+ assert.true(
+ controller.isSettingVisible(
+ { ...setting, depends_on: ["missing"] },
+ {}
+ ),
+ "parents outside the area do not hide settings"
+ );
+ assert.true(
+ controller.isSettingVisible(
+ { ...setting, depends_behavior: undefined },
+ {}
+ ),
+ "non-hidden settings retain their visibility"
+ );
+ assert.false(
+ controller.isSettingVisible(undefined, {}),
+ "missing settings are not rendered"
+ );
+ });
}
);
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-feature-setting-groups-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-feature-setting-groups-test.js
index a1e94a2c3e0..f2de6715b88 100644
--- a/plugins/discourse-ai/test/javascripts/unit/lib/ai-feature-setting-groups-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-feature-setting-groups-test.js
@@ -132,4 +132,36 @@ module("Unit | Lib | ai-feature-setting-groups", function () {
"unknown feature should return empty array"
);
});
+ test("sentiment groups include backend selectors and background processing", function (assert) {
+ const groups = getSettingGroupsForFeature("sentiment");
+ assert.deepEqual(
+ groups.map((group) => group.key),
+ ["sentiment", "emotion", "background_processing"],
+ "groups both independent capabilities and background controls"
+ );
+ assert.deepEqual(
+ groups.flatMap((group) => group.settings),
+ [
+ "ai_sentiment_enabled",
+ "ai_sentiment_sentiment_classification_strategy",
+ "ai_sentiment_sentiment_agent",
+ "ai_sentiment_sentiment_decision_model",
+ "ai_sentiment_emotion_enabled",
+ "ai_sentiment_emotion_classification_strategy",
+ "ai_sentiment_emotion_agent",
+ "ai_sentiment_emotion_decision_model",
+ "ai_sentiment_backfill_maximum_posts_per_hour",
+ "ai_sentiment_backfill_post_max_age_days",
+ ],
+ "exposes background controls without secret endpoint JSON"
+ );
+ assert.deepEqual(
+ groups[2].settings,
+ [
+ "ai_sentiment_backfill_maximum_posts_per_hour",
+ "ai_sentiment_backfill_post_max_age_days",
+ ],
+ "places both controls in the background processing section"
+ );
+ });
});
diff --git a/plugins/discourse-ai/test/javascripts/unit/routes/admin-plugins-show-discourse-ai-models-test.js b/plugins/discourse-ai/test/javascripts/unit/routes/admin-plugins-show-discourse-ai-models-test.js
new file mode 100644
index 00000000000..b5101b35504
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/routes/admin-plugins-show-discourse-ai-models-test.js
@@ -0,0 +1,34 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+
+module(
+ "Unit | Route | admin-plugins/show/discourse-ai-models",
+ function (hooks) {
+ setupTest(hooks);
+
+ test("loads decision models with workflows disabled", async function (assert) {
+ this.owner.lookup("service:site-settings").enable_discourse_workflows =
+ false;
+ const route = this.owner.lookup(
+ "route:admin-plugins/show/discourse-ai-models"
+ );
+ const findAll = sinon.stub(route.store, "findAll");
+ const empty = { content: [], resultSetMeta: { presets: [] } };
+ findAll.resolves(empty);
+
+ const model = await route.model();
+
+ assert.deepEqual(
+ findAll.args,
+ [["ai-llm"], ["ai-embedding"], ["ai-decision-model"]],
+ "loads every model type regardless of workflow availability"
+ );
+ assert.strictEqual(
+ model.decisionModels,
+ empty,
+ "keeps the empty result and its creation presets"
+ );
+ });
+ }
+);