Skip to content

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

1.1 KB
Raw Blame
# frozen_string_literal: true

class ProblemCheck::AiCreditSoftLimit < ProblemCheck
  self.priority = "low"
  self.perform_every = 1.hour
  self.targets = -> do
    LlmModel.joins(:llm_credit_allocation).where("llm_models.id < 0").pluck("llm_models.id")
  end

  def call
    return no_problem if !SiteSetting.discourse_ai_enabled

    model = LlmModel.where("id < 0").includes(:llm_credit_allocation).find_by(id: target)

    return no_problem if model.llm_credit_allocation.blank?

    allocation = model.llm_credit_allocation

    return no_problem if !allocation.soft_limit_reached?
    return no_problem if allocation.hard_limit_reached?

    soft_limit_problem(model, allocation)
  end

  private

  def soft_limit_problem(model, allocation)
    override_data = {
      model_id: model.id,
      model_name: model.display_name,
      percentage_remaining: allocation.percentage_remaining.round,
      reset_date: format_reset_date(allocation.next_reset_at),
      url: "#{Discourse.base_path}/admin/plugins/discourse-ai/ai-models?type=llm",
    }

    problem(model, override_data:)
  end

  def format_reset_date(date)
    I18n.l(date, format: :long)
  end
end