A platform for community discussion. Free, open, simple.
DEV: Separate admin/user credit limit errors (#36302)
Keegan George57240f514142
diff --git a/plugins/discourse-ai/app/controllers/concerns/ai_credit_limit_handler.rb b/plugins/discourse-ai/app/controllers/concerns/ai_credit_limit_handler.rbindex 3b501c089fd..d211e5f9ba2 100644--- a/plugins/discourse-ai/app/controllers/concerns/ai_credit_limit_handler.rb+++ b/plugins/discourse-ai/app/controllers/concerns/ai_credit_limit_handler.rb@@ -20,9 +20,18 @@ module AiCreditLimitHandlerdetails[:reset_time_absolute] = allocation.formatted_reset_timeend+ user_type = current_user&.admin? ? "admin" : "user"+ reset_time = allocation&.relative_reset_time.presence || ""++ message =+ I18n.t(+ "discourse_ai.llm_credit_allocation.limit_exceeded_#{user_type}",+ reset_time: reset_time,+ )+render json: {error: "credit_limit_exceeded",- message: exception.message,+ message: message,details: details,},status: :too_many_requestsdiff --git a/plugins/discourse-ai/app/models/llm_credit_allocation.rb b/plugins/discourse-ai/app/models/llm_credit_allocation.rbindex 2e74eb17def..13b501dc992 100644--- a/plugins/discourse-ai/app/models/llm_credit_allocation.rb+++ b/plugins/discourse-ai/app/models/llm_credit_allocation.rb@@ -93,15 +93,7 @@ class LlmCreditAllocation < ActiveRecord::Baseenddef check_credits!- if hard_limit_reached?- raise CreditLimitExceeded.new(- I18n.t(- "discourse_ai.llm_credit_allocation.limit_exceeded",- reset_time: format_reset_time,- ),- allocation: self,- )- end+ raise CreditLimitExceeded.new("Credit limit exceeded", allocation: self) if hard_limit_reached?enddef self.credits_available?(llm_model)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 81c00a34e6d..6a611dd8378 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@@ -2,6 +2,7 @@ import Component from "@glimmer/component";import { concat, fn } from "@ember/helper";import { action } from "@ember/object";import { service } from "@ember/service";+import { htmlSafe } from "@ember/template";import AdminSectionLandingItem from "discourse/admin/components/admin-section-landing-item";import AdminSectionLandingWrapper from "discourse/admin/components/admin-section-landing-wrapper";import DBreadcrumbsItem from "discourse/components/d-breadcrumbs-item";@@ -198,10 +199,12 @@ export default class AiLlmsListEditor extends Component {{{#if llm.llm_credit_allocation.hard_limit_reached}}<div class="alert alert-danger ai-credit-warning">{{icon "circle-info"}}- {{i18n- "discourse_ai.llms.credit_allocation.hard_limit_warning"- reset_date=(this.formatResetDate- llm.llm_credit_allocation.next_reset_at+ {{htmlSafe+ (i18n+ "discourse_ai.llms.credit_allocation.hard_limit_warning"+ reset_date=(this.formatResetDate+ llm.llm_credit_allocation.next_reset_at+ ))}}</div>@@ -210,9 +213,11 @@ export default class AiLlmsListEditor extends Component {}}<div class="alert alert-warning ai-credit-warning">{{icon "circle-info"}}- {{i18n- "discourse_ai.llms.credit_allocation.soft_limit_warning"- percentage=llm.llm_credit_allocation.percentage_remaining+ {{htmlSafe+ (i18n+ "discourse_ai.llms.credit_allocation.soft_limit_warning"+ percentage=llm.llm_credit_allocation.percentage_remaining+ )}}</div>{{/if}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-errors.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-errors.jsindex 1e32fdc589c..ccc3f6f473e 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-errors.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-errors.js@@ -1,3 +1,4 @@+import { htmlSafe } from "@ember/template";import { getOwnerWithFallback } from "discourse/lib/get-owner";import { i18n } from "discourse-i18n";@@ -31,27 +32,6 @@ export function isAiCreditLimitError(errorOrPayload) {return false;}-/**- * Format credit limit message with reset time.- *- * @param {Object} details - Details object containing reset time info- * @returns {string} - Formatted message- */-function formatCreditLimitMessage(details) {- const resetTime =- details?.reset_time_absolute ||- details?.reset_time_relative ||- details?.reset_time;-- if (resetTime && resetTime.length > 0) {- return i18n("discourse_ai.errors.credit_limit_dialog.message", {- reset_time: resetTime,- });- }-- return i18n("discourse_ai.errors.credit_limit_dialog.message_without_time");-}-/*** Show credit limit dialog to user.* Similar to popupAjaxError but specialized for AI credit limits.@@ -60,12 +40,35 @@ function formatCreditLimitMessage(details) {*/export function popupAiCreditLimitError(errorOrPayload) {const dialog = getOwnerWithFallback(this).lookup("service:dialog");+ const currentUser = getOwnerWithFallback(this).lookup("service:current-user");const details =errorOrPayload.jqXHR?.responseJSON?.details || errorOrPayload.details || {};+ const resetTime =+ details?.reset_time_absolute ||+ details?.reset_time_relative ||+ details?.reset_time;++ // Choose message key based on user role+ const userType = currentUser?.admin ? "admin" : "user";+ let message;++ if (resetTime && resetTime.length > 0) {+ message = i18n(+ `discourse_ai.errors.credit_limit_dialog.message_${userType}`,+ {+ reset_time: resetTime,+ }+ );+ } else {+ message = i18n(+ `discourse_ai.errors.credit_limit_dialog.message_without_time_${userType}`+ );+ }+dialog.alert({title: i18n("discourse_ai.errors.credit_limit_dialog.title"),- message: formatCreditLimitMessage(details),+ message: htmlSafe(message),});}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 19130991eb9..04ece6b669f 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -837,8 +837,10 @@ en:errors:credit_limit_dialog:title: "AI credit limit reached"- message: "You've hit the AI credit limit for your plan. Responses will be unavailable until your limit resets at %{reset_time}."- message_without_time: "You've hit the AI credit limit for your plan. Responses will be unavailable until your limit resets."+ message_user: "You have reached the AI credit limit for your plan. Responses will be unavailable until your limit resets at %{reset_time}. Please contact your site administrator for more information."+ message_without_time_user: "You have reached the AI credit limit for your plan. Responses will be unavailable until your limit resets. Please contact your site administrator for more information."+ message_admin: "You have reached the AI credit limit for your plan. Responses will be unavailable until your limit resets at %{reset_time}."+ message_without_time_admin: "You have reached the AI credit limit for your plan. Responses will be unavailable until your limit resets."reviewables:model_used: "Model used:"diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex fef000f6586..22709ea3954 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -671,7 +671,8 @@ en:all_posts_translated: "All posts in this topic are already translated."llm_credit_allocation:- limit_exceeded: "You have reached your AI credit limit. Please try again in %{reset_time}."+ limit_exceeded_user: "You have reached your AI credit limit. Please try again in %{reset_time}. Please contact your site administrator for more information."+ limit_exceeded_admin: "You have reached your AI credit limit. Please try again in %{reset_time}."errors:quota_exceeded: "You have exceeded the quota for this model. Please try again in %{relative_time}."diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.jsindex f82518e3a6f..cba91ac52ba 100644--- a/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-errors-test.js@@ -98,8 +98,10 @@ module("Unit | Utility | ai-errors", function (hooks) {assert.true(alertStub.calledOnce, "Dialog should be shown");const callArgs = alertStub.firstCall.args[0];+ // Convert htmlSafe string to regular string for testing+ const messageStr = callArgs.message.toString();assert.true(- callArgs.message.includes("5:40pm on Dec 25, 2024"),+ messageStr.includes("5:40pm on Dec 25, 2024"),"Message should include reset time");assert.strictEqual(@@ -124,12 +126,14 @@ module("Unit | Utility | ai-errors", function (hooks) {assert.true(alertStub.calledOnce, "Dialog should be shown");const callArgs = alertStub.firstCall.args[0];+ // Convert htmlSafe string to regular string for testing+ const messageStr = callArgs.message.toString();assert.false(- callArgs.message.includes("at"),- "Message should not include 'at' for time"+ /\bat\b\s+\d/.test(messageStr),+ "Message should not include time formatted as 'at [time]'");assert.true(- callArgs.message.includes("until your limit resets"),+ messageStr.includes("until your limit resets"),"Message should mention reset");@@ -151,8 +155,10 @@ module("Unit | Utility | ai-errors", function (hooks) {assert.true(alertStub.calledOnce, "Dialog should be shown");const callArgs = alertStub.firstCall.args[0];+ // Convert htmlSafe string to regular string for testing+ const messageStr = callArgs.message.toString();assert.true(- callArgs.message.includes("in 2 hours"),+ messageStr.includes("in 2 hours"),"Message should include relative time");