discourse/plugins/discourse-ai/app/controllers/concerns/ai_credit_limit_handler.rb
Keegan George 2fbafb7c8f
DEV: Unify locale copy and use absolute date (#36443)
## 🔍 Overview

This update ensures that we unify locales to all show the absolute
date/time when showing the reset time for credit limits.
Reset dates will show consistently as i.e. "December 1st at 4:00pm"
instead of "in 3h"
2025-12-04 13:48:38 -08:00

39 lines
949 B
Ruby
Vendored

# frozen_string_literal: true
module AiCreditLimitHandler
extend ActiveSupport::Concern
included do
rescue_from LlmCreditAllocation::CreditLimitExceeded do |e|
render_credit_limit_error(e)
end
end
private
def render_credit_limit_error(exception)
allocation = exception.allocation
details = {}
if allocation
details[:reset_time_relative] = allocation.relative_reset_time
details[:reset_time_absolute] = allocation.formatted_reset_time
end
user_type = current_user&.admin? ? "admin" : "user"
reset_time = allocation&.formatted_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: message,
details: details,
},
status: :too_many_requests
end
end