From 333b331eb9afa70cd426ad69042cf78eaf1743e5 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Mon, 27 May 2024 16:44:08 -0300 Subject: [PATCH] FEATURE: Allow deleting custom LLMs. (#643) This change allows us to delete custom models. It checks if there is no module using them. It also fixes a bug where the after-create transition wasn't working. While this prevents a model from being saved multiple times, endpoint validations are still needed (will be added in a separate PR).: --- .../discourse_ai/admin/ai_llms_controller.rb | 30 +++++++++++++++++ .../discourse/components/ai-llm-editor.gjs | 32 ++++++++++++++++--- config/locales/client.en.yml | 5 ++- config/locales/server.en.yml | 4 +++ config/routes.rb | 2 +- lib/configuration/llm_validator.rb | 8 ++--- .../requests/admin/ai_llms_controller_spec.rb | 21 ++++++++++++ 7 files changed, 92 insertions(+), 10 deletions(-) diff --git a/app/controllers/discourse_ai/admin/ai_llms_controller.rb b/app/controllers/discourse_ai/admin/ai_llms_controller.rb index 897fb152..54230fe0 100644 --- a/app/controllers/discourse_ai/admin/ai_llms_controller.rb +++ b/app/controllers/discourse_ai/admin/ai_llms_controller.rb @@ -49,6 +49,36 @@ module DiscourseAi end end + def destroy + llm_model = LlmModel.find(params[:id]) + + dependant_settings = %i[ai_helper_model ai_embeddings_semantic_search_hyde_model] + + in_use_by = [] + dependant_settings.each do |s_name| + in_use_by << s_name if SiteSetting.public_send(s_name) == "custom:#{llm_model.id}" + end + + if !in_use_by.empty? + return( + render_json_error( + I18n.t( + "discourse_ai.llm.delete_failed", + settings: in_use_by.join(", "), + count: in_use_by.length, + ), + status: 409, + ) + ) + end + + if llm_model.destroy + head :no_content + else + render_json_error llm_model + end + end + def test RateLimiter.new(current_user, "llm_test_#{current_user.id}", 3, 1.minute).performed! diff --git a/assets/javascripts/discourse/components/ai-llm-editor.gjs b/assets/javascripts/discourse/components/ai-llm-editor.gjs index dd92abc2..e1f16db0 100644 --- a/assets/javascripts/discourse/components/ai-llm-editor.gjs +++ b/assets/javascripts/discourse/components/ai-llm-editor.gjs @@ -16,6 +16,7 @@ import DTooltip from "float-kit/components/d-tooltip"; export default class AiLlmEditor extends Component { @service toasts; @service router; + @service dialog; @tracked isSaving = false; @@ -45,10 +46,7 @@ export default class AiLlmEditor extends Component { if (isNew) { this.args.llms.addObject(this.args.model); - this.router.transitionTo( - "adminPlugins.show.discourse-ai-llms.show", - this.args.model - ); + this.router.transitionTo("adminPlugins.show.discourse-ai-llms.index"); } else { this.toasts.success({ data: { message: I18n.t("discourse_ai.llms.saved") }, @@ -94,6 +92,24 @@ export default class AiLlmEditor extends Component { return this.testRunning || this.testResult !== null; } + @action + delete() { + return this.dialog.confirm({ + message: I18n.t("discourse_ai.llms.confirm_delete"), + didConfirm: () => { + return this.args.model + .destroyRecord() + .then(() => { + this.args.llms.removeObject(this.args.model); + this.router.transitionTo( + "adminPlugins.show.discourse-ai-llms.index" + ); + }) + .catch(popupAjaxError); + }, + }); + } +