Skip to content

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

UX: refresh LLM in AI feature card when changing the default (#39982)

Kris0302b1dd083e
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-default-llm-selector.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-default-llm-selector.gjs
index 0fd2c0411de..a5caf53d082 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-default-llm-selector.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-default-llm-selector.gjs
@@ -68,11 +68,17 @@ export default class AiDefaultLlmSelector extends Component {
this.isSaving = true;
try {
- const backendValue = value === "none" ? "" : value;
- await ajax("/admin/site_settings/ai_default_llm_model", {
- type: "PUT",
- data: { ai_default_llm_model: backendValue },
- });
+ try {
+ const backendValue = value === "none" ? "" : value;
+ await ajax("/admin/site_settings/ai_default_llm_model", {
+ type: "PUT",
+ data: { ai_default_llm_model: backendValue },
+ });
+ } catch (error) {
+ this.selectedValue = previousValue;
+ popupAjaxError(error);
+ return;
+ }
this.toasts.success({
duration: 3000,
@@ -80,9 +86,12 @@ export default class AiDefaultLlmSelector extends Component {
message: i18n("discourse_ai.llm_selector.saved"),
},
});
- } catch (error) {
- this.selectedValue = previousValue;
- popupAjaxError(error);
+
+ try {
+ await this.args.onChange?.();
+ } catch (error) {
+ popupAjaxError(error);
+ }
} finally {
this.isSaving = false;
}
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 cbd9d3e604d..ba4ec020a6d 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
@@ -18,16 +18,18 @@ const NOT_ENABLED = "not enabled";
export default class AiFeatures extends Component {
@service adminPluginNavManager;
+ @service store;
@tracked filterValue = "";
@tracked selectedFeatureGroup = ENABLED;
+ @tracked refreshedFeatures = null;
constructor() {
super(...arguments);
// if there are features but none are configured, show unconfigured
- if (this.args.features?.length > 0) {
- const configuredCount = this.args.features.filter(
+ if (this.features?.length > 0) {
+ const configuredCount = this.features.filter(
(f) => f.module_enabled === true
).length;
if (configuredCount === 0) {
@@ -36,6 +38,10 @@ export default class AiFeatures extends Component {
}
}
+ get features() {
+ return this.refreshedFeatures ?? this.args.features;
+ }
+
get featureGroupOptions() {
return [
{ value: ALL, label: i18n("discourse_ai.features.filters.all") },
@@ -51,11 +57,11 @@ export default class AiFeatures extends Component {
}
get filteredFeatures() {
- if (!this.args.features || this.args.features.length === 0) {
+ if (!this.features || this.features.length === 0) {
return [];
}
- let features = this.args.features;
+ let features = this.features;
if (this.selectedFeatureGroup === ENABLED) {
features = features.filter((feature) => feature.module_enabled === true);
@@ -168,6 +174,12 @@ export default class AiFeatures extends Component {
document.querySelector(".admin-filter__input").focus();
}
+ @action
+ async refreshFeatures() {
+ const features = await this.store.findAll("ai-feature");
+ this.refreshedFeatures = features.content;
+ }
+
<template>
<DBreadcrumbsItem
@path="/admin/plugins/{{this.adminPluginNavManager.currentPlugin.name}}/ai-features"
@@ -203,7 +215,7 @@ export default class AiFeatures extends Component {
/>
</div>
- <AiDefaultLlmSelector />
+ <AiDefaultLlmSelector @onChange={{this.refreshFeatures}} />
{{#if this.filteredFeatures.length}}
<AiFeaturesList @modules={{this.filteredFeatures}} />
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-default-llm-selector-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-default-llm-selector-test.gjs
index b26ea5626fd..8310b0b247c 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-default-llm-selector-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-default-llm-selector-test.gjs
@@ -136,4 +136,51 @@ module("Integration | Component | ai-default-llm-selector", function (hooks) {
assert.strictEqual(savedValue, "1", "saves model ID to backend");
});
+
+ test("invokes @onChange after a successful save", async function (assert) {
+ pretender.put("/admin/site_settings/ai_default_llm_model", () => {
+ return response({});
+ });
+
+ let onChangeCalls = 0;
+ const onChange = () => {
+ onChangeCalls += 1;
+ };
+
+ await render(
+ <template><AiDefaultLlmSelector @onChange={{onChange}} /></template>
+ );
+
+ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");
+ await selector.expand();
+ await selector.selectRowByValue("1");
+ await settled();
+
+ assert.strictEqual(onChangeCalls, 1, "calls @onChange once after save");
+ });
+
+ test("does not roll back the selection when @onChange fails after save", async function (assert) {
+ pretender.put("/admin/site_settings/ai_default_llm_model", () => {
+ return response({});
+ });
+
+ const onChange = () => {
+ throw { responseJSON: { errors: ["Refresh failed"] } };
+ };
+
+ await render(
+ <template><AiDefaultLlmSelector @onChange={{onChange}} /></template>
+ );
+
+ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");
+ await selector.expand();
+ await selector.selectRowByValue("1");
+ await settled();
+
+ assert.strictEqual(
+ selector.header().value(),
+ "1",
+ "keeps the saved selection even when the follow-up refresh fails"
+ );
+ });
});
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
new file mode 100644
index 00000000000..269ebc166e7
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-features-test.gjs
@@ -0,0 +1,115 @@
+import Service from "@ember/service";
+import { 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";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
+import AiFeatures from "discourse/plugins/discourse-ai/discourse/components/ai-features";
+
+class AdminPluginNavManagerStub extends Service {
+ currentPlugin = { name: "discourse-ai" };
+}
+
+function aiFeaturesPayload(llmModel) {
+ return {
+ ai_features: [
+ {
+ id: 1,
+ module_name: "summarization",
+ module_enabled: true,
+ features: [
+ {
+ name: "topic_summaries",
+ agents: [],
+ llm_models: [llmModel],
+ enabled: true,
+ },
+ ],
+ },
+ ],
+ };
+}
+
+module("Integration | Component | ai-features", function (hooks) {
+ setupRenderingTest(hooks, { stubRouter: true });
+
+ hooks.beforeEach(function () {
+ this.owner.unregister("service:admin-plugin-nav-manager");
+ this.owner.register(
+ "service:admin-plugin-nav-manager",
+ AdminPluginNavManagerStub
+ );
+
+ pretender.get("/admin/plugins/discourse-ai/ai-llms.json", () => {
+ return response({
+ ai_llms: [
+ { id: 1, display_name: "GPT-5" },
+ { id: 2, display_name: "Claude" },
+ ],
+ });
+ });
+
+ pretender.get("/admin/config/site_settings.json", () => {
+ return response({
+ site_settings: [
+ {
+ setting: "ai_default_llm_model",
+ value: "1",
+ default: "",
+ type: "enum",
+ },
+ ],
+ });
+ });
+ });
+
+ test("refreshes rendered LLM links after saving a new default LLM", async function (assert) {
+ let aiFeaturesRequestCount = 0;
+
+ pretender.get("/admin/plugins/discourse-ai/ai-features", () => {
+ aiFeaturesRequestCount += 1;
+
+ if (aiFeaturesRequestCount === 1) {
+ return response(aiFeaturesPayload({ id: 1, name: "GPT-5" }));
+ }
+
+ return response(aiFeaturesPayload({ id: 2, name: "Claude" }));
+ });
+
+ pretender.put("/admin/site_settings/ai_default_llm_model", () => {
+ return response({});
+ });
+
+ const store = this.owner.lookup("service:store");
+ this.features = (await store.findAll("ai-feature")).content;
+
+ await render(
+ <template><AiFeatures @features={{this.features}} /></template>
+ );
+
+ assert
+ .dom(
+ ".ai-feature-card[data-feature-name='topic_summaries'] .ai-feature-card__llm-link"
+ )
+ .hasText("GPT-5", "renders the initial LLM from the route payload");
+
+ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");
+ await selector.expand();
+ await selector.selectRowByValue("2");
+ await settled();
+
+ assert.strictEqual(
+ aiFeaturesRequestCount,
+ 2,
+ "reloads the AI features after the default LLM save succeeds"
+ );
+ assert
+ .dom(
+ ".ai-feature-card[data-feature-name='topic_summaries'] .ai-feature-card__llm-link"
+ )
+ .hasText(
+ "Claude",
+ "updates the rendered LLM link from the refreshed payload"
+ );
+ });
+});