A platform for community discussion. Free, open, simple.
UX: update default LLM selector (#37616)
Kris5b42280cc1a4
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-default-llm-selector.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-default-llm-selector.gjsindex e4b7589ecdd..326b8057a33 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-default-llm-selector.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-default-llm-selector.gjs@@ -1,31 +1,91 @@import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";-import SiteSettingComponent from "discourse/admin/components/site-setting";-import SiteSetting from "discourse/admin/models/site-setting";+import { hash } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import ComboBox from "discourse/select-kit/components/combo-box";import { i18n } from "discourse-i18n";export default class AiDefaultLlmSelector extends Component {- @tracked defaultLlmSetting = null;+ @service toasts;++ @tracked llmModels = [];+ @tracked selectedValue = "";+ @tracked isSaving = false;constructor() {super(...arguments);- this.#loadDefaultLlmSetting();+ this.#loadData();}- async #loadDefaultLlmSetting() {- const { site_settings } = await ajax("/admin/config/site_settings.json", {- data: {- plugin: "discourse-ai",- category: "discourse_ai",- },- });+ async #loadData() {+ try {+ const modelsResponse = await ajax(+ "/admin/plugins/discourse-ai/ai-llms.json"+ );+ this.llmModels = modelsResponse.ai_llms || [];++ const { site_settings } = await ajax("/admin/config/site_settings.json", {+ data: {+ plugin: "discourse-ai",+ category: "discourse_ai",+ },+ });- const defaultLlmSetting = site_settings.find(- (setting) => setting.setting === "ai_default_llm_model"- );+ const defaultLlmSetting = site_settings.find(+ (setting) => setting.setting === "ai_default_llm_model"+ );- this.defaultLlmSetting = SiteSetting.create(defaultLlmSetting);+ const rawValue = defaultLlmSetting?.value;+ this.selectedValue =+ rawValue === null || rawValue === undefined || rawValue === ""+ ? "none"+ : String(rawValue);+ } catch (error) {+ popupAjaxError(error);+ }+ }++ get content() {+ const noneOption = {+ id: "none",+ name: i18n("discourse_ai.llm_selector.none"),+ };+ const models = this.llmModels.map((model) => ({+ id: String(model.id),+ name: model.display_name,+ }));+ return [noneOption, ...models];+ }++ @action+ async onChange(value) {+ const previousValue = this.selectedValue;+ this.selectedValue = value;+ 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 },+ });++ this.toasts.success({+ duration: 3000,+ data: {+ message: i18n("discourse_ai.llm_selector.saved"),+ },+ });+ } catch (error) {+ this.selectedValue = previousValue;+ popupAjaxError(error);+ } finally {+ this.isSaving = false;+ }}<template>@@ -35,12 +95,17 @@ export default class AiDefaultLlmSelector extends Component {<p>{{i18n "discourse_ai.default_llm.description"}}</p></div>- {{#if this.defaultLlmSetting}}- <SiteSettingComponent- @setting={{this.defaultLlmSetting}}- class="ai-configure-default-llm__setting"+ <div class="ai-configure-default-llm__setting">+ <ComboBox+ @value={{this.selectedValue}}+ @content={{this.content}}+ @onChange={{this.onChange}}+ @valueProperty="id"+ @nameProperty="name"+ @options={{hash disabled=this.isSaving}}/>- {{/if}}+ <ConditionalLoadingSpinner @condition={{this.isSaving}} @size="small" />+ </div></div></template>}diff --git a/plugins/discourse-ai/assets/stylesheets/common/ai-features.scss b/plugins/discourse-ai/assets/stylesheets/common/ai-features.scssindex 8db99154b2b..8ed48879b7d 100644--- a/plugins/discourse-ai/assets/stylesheets/common/ai-features.scss+++ b/plugins/discourse-ai/assets/stylesheets/common/ai-features.scss@@ -1,3 +1,5 @@+@use "lib/viewport";+.ai-features-list {margin-block: 2rem;display: flex;@@ -158,22 +160,39 @@.ai-configure-default-llm {display: flex;align-items: center;- gap: var(--space-2);+ gap: var(--space-4);background: var(--primary-very-low);padding: 1rem;margin-block: 1rem;border-radius: var(--d-border-radius);+ @include viewport.until(sm) {+ flex-direction: column;+ }+&__header {- flex: 3;+ flex: 1 1 auto;+ max-width: 75ch;++ p {+ margin: 0;+ }}&__setting {- flex: 2;+ flex: 1 1 auto;display: flex;gap: var(--space-2);align-items: center;- justify-content: center;+ min-width: 10em;++ @include viewport.until(sm) {+ width: 100%;+ }++ .select-kit {+ width: 100%;+ }.setting-controls__undo {display: none;diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 99d02abddc3..df2728592b9 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -226,6 +226,10 @@ en:title: "Default LLM model"description: "The default LLM model to use for all AI features. This will be used if no LLM is specified in the feature configuration or persona. If no default LLM is specified, the last created LLM will be used."+ llm_selector:+ none: "None"+ saved: "Default LLM model updated successfully"+features:short_title: "Features"description: "These are the AI features available to visitors on your site. These can be configured to use specific personas and LLMs, and can be access controlled by groups."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.gjsnew file mode 100644index 00000000000..b26ea5626fd--- /dev/null+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-default-llm-selector-test.gjs@@ -0,0 +1,139 @@+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 AiDefaultLlmSelector from "discourse/plugins/discourse-ai/discourse/components/ai-default-llm-selector";++module("Integration | Component | ai-default-llm-selector", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ 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: "",+ default: "",+ type: "enum",+ },+ ],+ });+ });+ });++ test("renders dropdown with None option and models", async function (assert) {+ await render(<template><AiDefaultLlmSelector /></template>);++ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");+ await selector.expand();++ assert.strictEqual(selector.rows().length, 3, "shows None plus two models");+ assert.strictEqual(+ selector.rowByIndex(0).name(),+ "None",+ "first option is None"+ );+ assert.strictEqual(+ selector.rowByIndex(1).name(),+ "GPT-5",+ "second option is GPT-5"+ );+ assert.strictEqual(+ selector.rowByIndex(2).name(),+ "Claude",+ "third option is Claude"+ );+ });++ test("displays None when value is empty", async function (assert) {+ await render(<template><AiDefaultLlmSelector /></template>);++ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");++ assert.strictEqual(+ selector.header().value(),+ "none",+ "None is selected when value is empty"+ );+ assert.strictEqual(selector.header().label(), "None", "displays None text");+ });++ test("displays selected model when value is set", async function (assert) {+ pretender.get("/admin/config/site_settings.json", () => {+ return response({+ site_settings: [+ {+ setting: "ai_default_llm_model",+ value: "1",+ default: "",+ type: "enum",+ },+ ],+ });+ });++ await render(<template><AiDefaultLlmSelector /></template>);++ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");++ assert.strictEqual(selector.header().value(), "1", "model ID is selected");+ assert.strictEqual(+ selector.header().label(),+ "GPT-5",+ "displays model name"+ );+ });++ test("saves empty string when None is selected", async function (assert) {+ let savedValue = null;++ pretender.put("/admin/site_settings/ai_default_llm_model", (request) => {+ const params = new URLSearchParams(request.requestBody);+ savedValue = params.get("ai_default_llm_model");+ return response({});+ });++ await render(<template><AiDefaultLlmSelector /></template>);++ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");+ await selector.expand();+ await selector.selectRowByValue("none");+ await settled();++ assert.strictEqual(+ savedValue,+ "",+ "saves empty string to backend when None selected"+ );+ });++ test("saves model ID when model is selected", async function (assert) {+ let savedValue = null;++ pretender.put("/admin/site_settings/ai_default_llm_model", (request) => {+ const params = new URLSearchParams(request.requestBody);+ savedValue = params.get("ai_default_llm_model");+ return response({});+ });++ await render(<template><AiDefaultLlmSelector /></template>);++ const selector = selectKit(".ai-configure-default-llm__setting .combo-box");+ await selector.expand();+ await selector.selectRowByValue("1");+ await settled();++ assert.strictEqual(savedValue, "1", "saves model ID to backend");+ });+});