A platform for community discussion. Free, open, simple.
DEV: Allow admins to add quotas to seeded models (#36269)
Keegan George70570c71b166
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-llms/edit.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-llms/edit.jsindex c21f19eebdb..4d14a9d3718 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-llms/edit.js+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-llms/edit.js@@ -3,15 +3,6 @@ import DiscourseRoute from "discourse/routes/discourse";export default class AdminPluginsShowDiscourseAiLlmsEdit extends DiscourseRoute {async model(params) {const id = parseInt(params.id, 10);-- if (id < 0) {- // You shouldn't be able to access the edit page- // if the model is seeded- return this.router.transitionTo(- "adminPlugins.show.discourse-ai-llms.index"- );- }-const allLlms = this.modelFor("adminPlugins.show.discourse-ai-llms");const record = allLlms.content.find((item) => item.id === id);record.provider_params = record.provider_params || {};diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rbindex 1b2aa402dc7..b46d476715b 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_llms_controller.rb@@ -84,7 +84,10 @@ module DiscourseAihandle_feature_credit_costs_update(llm_model)if llm_model.seeded?- return render_json_error(I18n.t("discourse_ai.llm.cannot_edit_builtin"), status: 403)+ # For seeded models, only allow quota/credit updates (already handled above)+ # Return success with updated model, don't allow other param changes+ log_llm_model_update(llm_model, initial_attributes, initial_quotas)+ return render json: LlmModelSerializer.new(llm_model)endif llm_model.update(ai_llm_params(updating: llm_model))@@ -141,6 +144,15 @@ module DiscourseAidef testRateLimiter.new(current_user, "llm_test_#{current_user.id}", 3, 1.minute).performed!+ # For seeded models, test the existing model directly since provider/url/api_key are hidden+ if params.dig(:ai_llm, :id).present?+ existing_model = LlmModel.find_by(id: params[:ai_llm][:id])+ if existing_model&.seeded?+ DiscourseAi::Configuration::LlmValidator.new.run_test(existing_model)+ return render json: { success: true }+ end+ end+# We don't care about the display_name attr for testing.llm_model = LlmModel.new(ai_llm_params.merge(display_name: "LLM test"))diff --git a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb b/plugins/discourse-ai/app/serializers/llm_model_serializer.rbindex 4ee59b491e7..5ea8c7c2e42 100644--- a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb+++ b/plugins/discourse-ai/app/serializers/llm_model_serializer.rb@@ -22,7 +22,8 @@ class LlmModelSerializer < ApplicationSerializer:output_cost,:cached_input_cost,:cache_write_cost,- :used_by+ :used_by,+ :seededhas_one :user, serializer: BasicUserSerializer, embed: :objecthas_many :llm_quotas, serializer: LlmQuotaSerializer, embed: :objects@@ -63,4 +64,8 @@ class LlmModelSerializer < ApplicationSerializerdef include_credit_allocation?object.credit_system_enabled?end++ def seeded+ object.seeded?+ endenddiff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-feature-card.gjsindex a452e4b9347..67e83c5d103 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@@ -4,15 +4,8 @@ import { concat } from "@ember/helper";import { action } from "@ember/object";import { LinkTo } from "@ember/routing";import DButton from "discourse/components/d-button";-import DTooltip from "discourse/float-kit/components/d-tooltip";import { i18n } from "discourse-i18n";-function isPreseeded(llm) {- if (llm.id < 0) {- return true;- }-}-class ExpandableList extends Component {@tracked isExpanded = false;@@ -149,24 +142,13 @@ export default class AiFeatureCard extends Component {@maxItemsToShow={{5}}as |llm index isLastItem|>- {{#if (isPreseeded llm)}}- <DTooltip>- <:trigger>- {{concat llm.name (unless (isLastItem index) ", ")}}- </:trigger>- <:content>- {{i18n "discourse_ai.llms.seeded_warning"}}- </:content>- </DTooltip>- {{else}}- <LinkTo- @route="adminPlugins.show.discourse-ai-llms.edit"- @model={{llm.id}}- class="ai-feature-card__llm-link"- >- {{concat llm.name (unless (isLastItem index) ", ")}}- </LinkTo>- {{/if}}+ <LinkTo+ @route="adminPlugins.show.discourse-ai-llms.edit"+ @model={{llm.id}}+ class="ai-feature-card__llm-link"+ >+ {{concat llm.name (unless (isLastItem index) ", ")}}+ </LinkTo></ExpandableList>{{else}}<span class="ai-feature-card__label">diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjsindex 4244be8e1b1..607b591ed68 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-editor-form.gjs@@ -242,7 +242,11 @@ export default class AiLlmEditorForm extends Component {this.testRunning = true;try {- const configTestResult = await this.args.model.testConfig(data);+ // For seeded models, include the id so backend can test the existing model+ const testData = this.args.model.seeded+ ? { ...data, id: this.args.model.id }+ : data;+ const configTestResult = await this.args.model.testConfig(testData);this.testResult = configTestResult.success;if (this.testResult) {@@ -309,6 +313,7 @@ export default class AiLlmEditorForm extends Component {@validation="required|length:1,100"@format="large"@tooltip={{i18n "discourse_ai.llms.hints.display_name"}}+ @disabled={{@model.seeded}}as |field|><field.Input />@@ -320,192 +325,197 @@ export default class AiLlmEditorForm extends Component {@tooltip={{i18n "discourse_ai.llms.hints.name"}}@validation="required"@format="large"+ @disabled={{@model.seeded}}as |field|><field.Input /></form.Field>- <form.Field- @name="provider"- @title={{i18n "discourse_ai.llms.provider"}}- @format="large"- @validation="required"- @onSet={{this.setProvider}}- as |field|- >- <field.Select as |select|>- {{#each this.selectedProviders as |provider|}}- <select.Option- @value={{provider.id}}- >{{provider.name}}</select.Option>- {{/each}}- </field.Select>- </form.Field>-- {{#if (this.canEditURL data.provider)}}+ {{#unless @model.seeded}}<form.Field- @name="url"- @title={{i18n "discourse_ai.llms.url"}}- @validation="required"+ @name="provider"+ @title={{i18n "discourse_ai.llms.provider"}}@format="large"+ @validation="required"+ @onSet={{this.setProvider}}as |field|>- <field.Input />+ <field.Select as |select|>+ {{#each this.selectedProviders as |provider|}}+ <select.Option+ @value={{provider.id}}+ >{{provider.name}}</select.Option>+ {{/each}}+ </field.Select></form.Field>- {{/if}}- <form.Field- @name="api_key"- @title={{i18n "discourse_ai.llms.api_key"}}- @format="large"- as |field|- >- <field.Password autocomplete="off" data-1p-ignore />- </form.Field>+ {{#if (this.canEditURL data.provider)}}+ <form.Field+ @name="url"+ @title={{i18n "discourse_ai.llms.url"}}+ @validation="required"+ @format="large"+ as |field|+ >+ <field.Input />+ </form.Field>+ {{/if}}- <form.Object @name="provider_params" as |object providerParamsData|>- {{#each (this.providerParamsKeys providerParamsData) as |name|}}- {{#let- (get (this.metaProviderParams data.provider) name)- as |params|- }}- <object.Field- @name={{name}}- @title={{i18n (concat "discourse_ai.llms.provider_fields." name)}}- @format="large"- as |field|- >- {{#if (eq params.type "enum")}}- <field.Select @includeNone={{false}} as |select|>- {{#each params.values as |option|}}- <select.Option- @value={{option.id}}- >{{option.name}}</select.Option>- {{/each}}- </field.Select>- {{else if (eq params.type "checkbox")}}- <field.Checkbox />- {{else}}- <field.Input @type={{params.type}} />- {{/if}}- </object.Field>- {{/let}}- {{/each}}- </form.Object>+ <form.Field+ @name="api_key"+ @title={{i18n "discourse_ai.llms.api_key"}}+ @format="large"+ as |field|+ >+ <field.Password autocomplete="off" data-1p-ignore />+ </form.Field>- <form.Field- @name="tokenizer"- @title={{i18n "discourse_ai.llms.tokenizer"}}- @format="large"- @validation="required"- as |field|- >- <field.Select as |select|>- {{#each this.tokenizers as |tokenizer|}}- <select.Option- @value={{tokenizer.id}}- >{{tokenizer.name}}</select.Option>+ <form.Object @name="provider_params" as |object providerParamsData|>+ {{#each (this.providerParamsKeys providerParamsData) as |name|}}+ {{#let+ (get (this.metaProviderParams data.provider) name)+ as |params|+ }}+ <object.Field+ @name={{name}}+ @title={{i18n+ (concat "discourse_ai.llms.provider_fields." name)+ }}+ @format="large"+ as |field|+ >+ {{#if (eq params.type "enum")}}+ <field.Select @includeNone={{false}} as |select|>+ {{#each params.values as |option|}}+ <select.Option+ @value={{option.id}}+ >{{option.name}}</select.Option>+ {{/each}}+ </field.Select>+ {{else if (eq params.type "checkbox")}}+ <field.Checkbox />+ {{else}}+ <field.Input @type={{params.type}} />+ {{/if}}+ </object.Field>+ {{/let}}{{/each}}- </field.Select>- </form.Field>+ </form.Object>- <form.Field- @name="max_prompt_tokens"- @title={{i18n "discourse_ai.llms.max_prompt_tokens"}}- @tooltip={{i18n "discourse_ai.llms.hints.max_prompt_tokens"}}- @validation="required"- @format="large"- as |field|- >- <field.Input @type="number" step="any" min="0" lang="en" />- </form.Field>-- <form.InputGroup as |inputGroup|>- <inputGroup.Field- @name="input_cost"- @title={{i18n "discourse_ai.llms.cost_input"}}- @tooltip={{i18n "discourse_ai.llms.hints.cost_input"}}- @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ <form.Field+ @name="tokenizer"+ @title={{i18n "discourse_ai.llms.tokenizer"}}+ @format="large"+ @validation="required"as |field|>- <field.Input @type="number" step="any" min="0" lang="en" />- </inputGroup.Field>+ <field.Select as |select|>+ {{#each this.tokenizers as |tokenizer|}}+ <select.Option+ @value={{tokenizer.id}}+ >{{tokenizer.name}}</select.Option>+ {{/each}}+ </field.Select>+ </form.Field>- <inputGroup.Field- @name="cached_input_cost"- @title={{i18n "discourse_ai.llms.cost_cached_input"}}- @tooltip={{i18n "discourse_ai.llms.hints.cost_cached_input"}}- @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ <form.Field+ @name="max_prompt_tokens"+ @title={{i18n "discourse_ai.llms.max_prompt_tokens"}}+ @tooltip={{i18n "discourse_ai.llms.hints.max_prompt_tokens"}}+ @validation="required"+ @format="large"as |field|><field.Input @type="number" step="any" min="0" lang="en" />- </inputGroup.Field>+ </form.Field>++ <form.InputGroup as |inputGroup|>+ <inputGroup.Field+ @name="input_cost"+ @title={{i18n "discourse_ai.llms.cost_input"}}+ @tooltip={{i18n "discourse_ai.llms.hints.cost_input"}}+ @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ as |field|+ >+ <field.Input @type="number" step="any" min="0" lang="en" />+ </inputGroup.Field>++ <inputGroup.Field+ @name="cached_input_cost"+ @title={{i18n "discourse_ai.llms.cost_cached_input"}}+ @tooltip={{i18n "discourse_ai.llms.hints.cost_cached_input"}}+ @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ as |field|+ >+ <field.Input @type="number" step="any" min="0" lang="en" />+ </inputGroup.Field>++ <inputGroup.Field+ @name="cache_write_cost"+ @title={{i18n "discourse_ai.llms.cost_cache_write"}}+ @tooltip={{i18n "discourse_ai.llms.hints.cost_cache_write"}}+ @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ as |field|+ >+ <field.Input @type="number" step="any" min="0" lang="en" />+ </inputGroup.Field>++ <inputGroup.Field+ @name="output_cost"+ @title={{i18n "discourse_ai.llms.cost_output"}}+ @tooltip={{i18n "discourse_ai.llms.hints.cost_output"}}+ @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ as |field|+ >+ <field.Input @type="number" step="any" min="0" lang="en" />+ </inputGroup.Field>+ </form.InputGroup>- <inputGroup.Field- @name="cache_write_cost"- @title={{i18n "discourse_ai.llms.cost_cache_write"}}- @tooltip={{i18n "discourse_ai.llms.hints.cost_cache_write"}}- @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ <form.Field+ @name="max_output_tokens"+ @title={{i18n "discourse_ai.llms.max_output_tokens"}}+ @tooltip={{i18n "discourse_ai.llms.hints.max_output_tokens"}}+ @format="large"as |field|><field.Input @type="number" step="any" min="0" lang="en" />- </inputGroup.Field>+ </form.Field>- <inputGroup.Field- @name="output_cost"- @title={{i18n "discourse_ai.llms.cost_output"}}- @tooltip={{i18n "discourse_ai.llms.hints.cost_output"}}- @helpText={{i18n "discourse_ai.llms.hints.cost_measure"}}+ <form.Field+ @name="vision_enabled"+ @title={{i18n "discourse_ai.llms.vision_enabled"}}+ @tooltip={{i18n "discourse_ai.llms.hints.vision_enabled"}}+ @format="large"as |field|>- <field.Input @type="number" step="any" min="0" lang="en" />- </inputGroup.Field>- </form.InputGroup>-- <form.Field- @name="max_output_tokens"- @title={{i18n "discourse_ai.llms.max_output_tokens"}}- @tooltip={{i18n "discourse_ai.llms.hints.max_output_tokens"}}- @format="large"- as |field|- >- <field.Input @type="number" step="any" min="0" lang="en" />- </form.Field>-- <form.Field- @name="vision_enabled"- @title={{i18n "discourse_ai.llms.vision_enabled"}}- @tooltip={{i18n "discourse_ai.llms.hints.vision_enabled"}}- @format="large"- as |field|- >- <field.Checkbox />- </form.Field>+ <field.Checkbox />+ </form.Field>- <form.Field- @name="enabled_chat_bot"- @title={{i18n "discourse_ai.llms.enabled_chat_bot"}}- @tooltip={{i18n "discourse_ai.llms.hints.enabled_chat_bot"}}- @format="large"- as |field|- >- <field.Checkbox />- </form.Field>+ <form.Field+ @name="enabled_chat_bot"+ @title={{i18n "discourse_ai.llms.enabled_chat_bot"}}+ @tooltip={{i18n "discourse_ai.llms.hints.enabled_chat_bot"}}+ @format="large"+ as |field|+ >+ <field.Checkbox />+ </form.Field>- {{#if @model.user}}- <form.Container @title={{i18n "discourse_ai.llms.ai_bot_user"}}>- <a- class="avatar"- href={{@model.user.path}}- data-user-card={{@model.user.username}}- >- {{Avatar @model.user.avatar_template "small"}}- </a>- <LinkTo @route="adminUser" @model={{this.adminUser}}>- {{@model.user.username}}- </LinkTo>- </form.Container>- {{/if}}+ {{#if @model.user}}+ <form.Container @title={{i18n "discourse_ai.llms.ai_bot_user"}}>+ <a+ class="avatar"+ href={{@model.user.path}}+ data-user-card={{@model.user.username}}+ >+ {{Avatar @model.user.avatar_template "small"}}+ </a>+ <LinkTo @route="adminUser" @model={{this.adminUser}}>+ {{@model.user.username}}+ </LinkTo>+ </form.Container>+ {{/if}}+ {{/unless}}{{#if (gt data.llm_quotas.length 0)}}<form.Container @title={{i18n "discourse_ai.llms.quotas.title"}}>@@ -627,11 +637,13 @@ export default class AiLlmEditorForm extends Component {{{/if}}{{#unless @model.isNew}}- <form.Button- @action={{this.delete}}- @label="discourse_ai.llms.delete"- class="btn-danger"- />+ {{#unless @model.seeded}}+ <form.Button+ @action={{this.delete}}+ @label="discourse_ai.llms.delete"+ class="btn-danger"+ />+ {{/unless}}{{/unless}}</form.Actions>diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llms-list-editor.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llms-list-editor.gjsindex bba8498402c..81c00a34e6d 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llms-list-editor.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llms-list-editor.gjs@@ -7,7 +7,6 @@ import AdminSectionLandingWrapper from "discourse/admin/components/admin-sectionimport DBreadcrumbsItem from "discourse/components/d-breadcrumbs-item";import DButton from "discourse/components/d-button";import DPageSubheader from "discourse/components/d-page-subheader";-import DTooltip from "discourse/float-kit/components/d-tooltip";import icon from "discourse/helpers/d-icon";import I18n, { i18n } from "discourse-i18n";import AiCreditBar from "./ai-credit-bar";@@ -229,26 +228,12 @@ export default class AiLlmsListEditor extends Component {}}</td><td class="d-admin-row__controls">- {{#if (isPreseeded llm)}}- <DTooltip class="ai-llm-list__edit-disabled-tooltip">- <:trigger>- <DButton- class="btn btn-default btn-small disabled"- @label="discourse_ai.llms.edit"- />- </:trigger>- <:content>- {{i18n "discourse_ai.llms.seeded_warning"}}- </:content>- </DTooltip>- {{else}}- <DButton- class="btn btn-default btn-small ai-llm-list__delete-button"- @label="discourse_ai.llms.edit"- @route="adminPlugins.show.discourse-ai-llms.edit"- @routeModels={{llm.id}}- />- {{/if}}+ <DButton+ class="btn btn-default btn-small ai-llm-list__edit-button"+ @label="discourse_ai.llms.edit"+ @route="adminPlugins.show.discourse-ai-llms.edit"+ @routeModels={{llm.id}}+ /></td></tr>{{/each}}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 666e2b132de..19130991eb9 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -646,7 +646,6 @@ en:back: "Back"confirm_delete: Are you sure you want to delete this model?delete: Delete- seeded_warning: "This model is pre-configured on your site and cannot be edited."quotas:title: "Usage quotas"add_title: "Create new quota"diff --git a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rbindex 82bb861ce00..1176d56e68e 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb@@ -501,6 +501,24 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do)endend++ context "when testing a seeded model" do+ fab!(:seeded_llm) { Fabricate(:fake_model, id: -200) }++ it "tests the existing model directly instead of using params" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["a response"]) do+ get "/admin/plugins/discourse-ai/ai-llms/test.json",+ params: {+ ai_llm: {+ id: seeded_llm.id,+ },+ }++ expect(response).to be_successful+ expect(response.parsed_body["success"]).to eq(true)+ end+ end+ endenddescribe "DELETE #destroy" dodiff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rbindex 0bf3a0217bb..1fcd6b02a4e 100644--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb@@ -192,10 +192,5 @@ RSpec.describe "Managing LLM configurations", type: :system dotext: desc,)end-- it "seeded LLM has a disabled edit button" do- visit "/admin/plugins/discourse-ai/ai-llms"- expect(page).to have_css("[data-llm-id='cdck-hosted'] .ai-llm-list__edit-disabled-tooltip")- endendend