Skip to content

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

UX: Improvements to flow when credit limits are exceeded (#36360)

Keegan Georgea963b5f7ef5d
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
index c4e3f2786b6..e409748762b 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb
@@ -70,10 +70,12 @@ module DiscourseAi
current_user,
),
status: :ok
+ rescue LlmCreditAllocation::CreditLimitExceeded => e
+ render_credit_limit_error(e)
+ rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed
+ render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
+ status: 502
end
- rescue DiscourseAi::Completions::Endpoints::Base::CompletionFailed
- render_json_error I18n.t("discourse_ai.ai_helper.errors.completion_request_failed"),
- status: 502
end
def suggest_category
@@ -155,6 +157,8 @@ module DiscourseAi
else
render json: { thumbnails: result[:thumbnails] }, status: :ok
end
+ rescue LlmCreditAllocation::CreditLimitExceeded => e
+ render_credit_limit_error(e)
end
end
diff --git a/plugins/discourse-ai/app/services/discourse_ai/ai_helper/generate_thumbnails.rb b/plugins/discourse-ai/app/services/discourse_ai/ai_helper/generate_thumbnails.rb
index 0923fef0f3f..18816548c03 100644
--- a/plugins/discourse-ai/app/services/discourse_ai/ai_helper/generate_thumbnails.rb
+++ b/plugins/discourse-ai/app/services/discourse_ai/ai_helper/generate_thumbnails.rb
@@ -67,6 +67,8 @@ module DiscourseAi
context[:captured_custom_raw] = captured_custom_raw
fail!("no_image_generated") if captured_custom_raw.blank?
+ rescue LlmCreditAllocation::CreditLimitExceeded
+ raise
rescue => e
Rails.logger.error(
"Image generation failed: #{e.class} - #{e.message}\n#{e.backtrace.first(5).join("\n")}",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-blinking-animation.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-blinking-animation.gjs
index 5b9ed1d271a..5c57f2df221 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-blinking-animation.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-blinking-animation.gjs
@@ -31,8 +31,8 @@ export default class AiBlinkingAnimation extends Component {
@action
setupAnimation() {
- this.blocks.firstObject.show = true;
- this.blocks.firstObject.shown = true;
+ this.blocks[0].show = true;
+ this.blocks[0].shown = true;
}
@action
@@ -72,8 +72,8 @@ export default class AiBlinkingAnimation extends Component {
this.#nextBlock(block).show = true;
this.#nextBlock(block).shown = true;
- if (this.blocks.lastObject === block) {
- this.blocks.firstObject.blinking = true;
+ if (this.blocks.at(-1) === block) {
+ this.blocks[0].blinking = true;
}
},
250
@@ -88,10 +88,10 @@ export default class AiBlinkingAnimation extends Component {
}
#nextBlock(currentBlock) {
- if (currentBlock === this.blocks.lastObject) {
- return this.blocks.firstObject;
+ if (currentBlock === this.blocks.at(-1)) {
+ return this.blocks[0];
} else {
- return this.blocks.objectAt(this.blocks.indexOf(currentBlock) + 1);
+ return this.blocks[this.blocks.indexOf(currentBlock) + 1];
}
}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
index 9fb7ad86b62..70e4a2ae51d 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/diff-modal.gjs
@@ -110,8 +110,8 @@ export default class ModalDiffModal extends Component {
updateResult(result) {
if (isAiCreditLimitError(result)) {
this.loading = false;
+ this.cleanupAndClose();
popupAiCreditLimitError(result);
- this.cleanup();
return;
}
@@ -150,6 +150,7 @@ export default class ModalDiffModal extends Component {
this.progressChannel = result.progress_channel;
} catch (e) {
if (isAiCreditLimitError(e)) {
+ this.cleanupAndClose();
popupAiCreditLimitError(e);
} else {
popupAjaxError(e);
@@ -183,6 +184,7 @@ export default class ModalDiffModal extends Component {
@action
cleanupAndClose() {
+ this.cleanup();
this.#resetState();
this.loading = false;
this.args.closeModal();
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/thumbnail-suggestions.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/thumbnail-suggestions.gjs
index bf1269b456b..90ee7e498bf 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/modal/thumbnail-suggestions.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/modal/thumbnail-suggestions.gjs
@@ -44,6 +44,7 @@ export default class ThumbnailSuggestions extends Component {
this.thumbnails = thumbnails.thumbnails;
} catch (error) {
if (isAiCreditLimitError(error)) {
+ this.args.closeModal();
popupAiCreditLimitError(error);
} else {
popupAjaxError(error);
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
index a6de2b24f64..09f1f9670c5 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/suggestion-menus/ai-title-suggester.gjs
@@ -9,6 +9,10 @@ import DMenu from "discourse/float-kit/components/d-menu";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { i18n } from "discourse-i18n";
+import {
+ isAiCreditLimitError,
+ popupAiCreditLimitError,
+} from "../../lib/ai-errors";
import {
MIN_CHARACTER_COUNT,
showSuggestionsError,
@@ -92,7 +96,11 @@ export default class AiTitleSuggester extends Component {
return;
}
} catch (error) {
- popupAjaxError(error);
+ if (isAiCreditLimitError(error)) {
+ popupAiCreditLimitError(error);
+ } else {
+ popupAjaxError(error);
+ }
} finally {
this.loading = false;
this.triggerIcon = "rotate";
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-expanded-after/ai-summary-trigger.gjs b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-expanded-after/ai-summary-trigger.gjs
index 41528a29ae4..1a634d59677 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-expanded-after/ai-summary-trigger.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/connectors/topic-map-expanded-after/ai-summary-trigger.gjs
@@ -1,18 +1,78 @@
import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import { service } from "@ember/service";
+import { htmlSafe } from "@ember/template";
+import { modifier } from "ember-modifier";
import DButton from "discourse/components/d-button";
import AiSummaryModal from "../../components/modal/ai-summary-modal";
export default class AiSummaryTrigger extends Component {
+ @service aiCredits;
@service modal;
+ @service tooltip;
+
+ @tracked creditStatus = null;
+ @tracked creditCheckComplete = false;
+
+ creditLimitTooltipModifier = modifier((element) => {
+ if (!this.isDisabled) {
+ return;
+ }
+
+ const instance = this.tooltip.register(element, {
+ identifier: "ai-summary-credit-limit-tooltip",
+ content: htmlSafe(
+ this.aiCredits.getCreditLimitMessage(this.creditStatus)
+ ),
+ placement: "top",
+ triggers: "hover",
+ interactive: true,
+ });
+
+ return () => instance.destroy();
+ });
get isAiConversation() {
return this.args.outletArgs.topic.is_bot_pm;
}
+ get hasCachedSummary() {
+ return this.args.outletArgs.topic.has_cached_summary;
+ }
+
+ get isDisabled() {
+ if (this.hasCachedSummary) {
+ return false;
+ }
+ if (!this.creditCheckComplete) {
+ return false;
+ }
+ return this.creditStatus?.hard_limit_reached === true;
+ }
+
+ @action
+ async checkCredits() {
+ this.creditStatus = null;
+ this.creditCheckComplete = false;
+
+ try {
+ this.creditStatus =
+ await this.aiCredits.getFeatureCreditStatus("topic_summaries");
+ } catch {
+ this.creditStatus = null;
+ } finally {
+ this.creditCheckComplete = true;
+ }
+ }
+
@action
openAiSummaryModal() {
+ if (this.isDisabled) {
+ return;
+ }
this.modal.show(AiSummaryModal, {
model: {
topic: this.args.outletArgs.topic,
@@ -24,11 +84,17 @@ export default class AiSummaryTrigger extends Component {
<template>
{{#unless this.isAiConversation}}
{{#if @outletArgs.topic.summarizable}}
- <section class="topic-map__additional-contents toggle-summary">
+ <section
+ class="topic-map__additional-contents toggle-summary"
+ {{didInsert this.checkCredits}}
+ {{didUpdate this.checkCredits @outletArgs.topic.id}}
+ {{this.creditLimitTooltipModifier}}
+ >
<DButton
@label="summary.buttons.generate"
@icon="discourse-sparkles"
@action={{this.openAiSummaryModal}}
+ @disabled={{this.isDisabled}}
class="btn-default ai-summarization-button"
/>
</section>
diff --git a/plugins/discourse-ai/lib/summarization/entry_point.rb b/plugins/discourse-ai/lib/summarization/entry_point.rb
index b600701f03c..24c8a503e3f 100644
--- a/plugins/discourse-ai/lib/summarization/entry_point.rb
+++ b/plugins/discourse-ai/lib/summarization/entry_point.rb
@@ -19,6 +19,10 @@ module DiscourseAi
scope.can_see_summary?(object.topic)
end
+ plugin.add_to_serializer(:topic_view, :has_cached_summary) do
+ AiSummary.exists?(target: object.topic, summary_type: AiSummary.summary_types[:complete])
+ end
+
plugin.add_to_serializer(:web_hook_topic_view, :summarizable) do
scope.can_see_summary?(object.topic)
end
diff --git a/plugins/discourse-ai/test/javascripts/acceptance/topic-summary-test.js b/plugins/discourse-ai/test/javascripts/acceptance/topic-summary-test.js
index be8a3e8753c..cb65aa055cf 100644
--- a/plugins/discourse-ai/test/javascripts/acceptance/topic-summary-test.js
+++ b/plugins/discourse-ai/test/javascripts/acceptance/topic-summary-test.js
@@ -30,6 +30,14 @@ acceptance("Topic - Summary", function (needs) {
});
});
+ server.get("/discourse-ai/credits/status", () => {
+ return helper.response({
+ topic_summaries: {
+ hard_limit_reached: false,
+ },
+ });
+ });
+
server.get("/discourse-ai/ai-bot/conversations.json", () => {});
});
@@ -130,6 +138,14 @@ acceptance("Topic - Summary - Anon", function (needs) {
},
});
});
+
+ server.get("/discourse-ai/credits/status", () => {
+ return helper.response({
+ topic_summaries: {
+ hard_limit_reached: false,
+ },
+ });
+ });
});
test("displays cached summary immediately", async function (assert) {