Skip to content

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

2.3 KB
Raw Blame
# frozen_string_literal: true

module DiscourseAi
  module Translation
    class LanguageDetector
      # reject non-language code responses by IETF language tag: https://datatracker.ietf.org/doc/html/rfc5646
      LANGUAGE_TAG_REGEXP = /\A[A-Za-z]{2,4}(-[A-Za-z]{4})?(-([A-Za-z]{2}|[0-9]{3}))?\z/

      DETECTION_CHAR_LIMIT = 1000

      def initialize(text, topic: nil, post: nil)
        @text = text
        @topic = topic
        @post = post
      end

      def detect
        return nil if !SiteSetting.ai_translation_enabled
        return nil if @text.blank?

        if (decision_model = DecisionLocaleDetector.decision_model)
          begin
            locale = DecisionLocaleDetector.new(decision_model).detect(@text)
            return locale if locale
          rescue DiscourseAi::Decisions::CreditLimitExceeded
            # falling back would move the spend to the LLM; detection resumes when credits reset
            return nil
          rescue DiscourseAi::Decisions::Error => e
            VerboseLogger.log("Decision model locale detection failed: #{e.message}")
          end
        end

        detect_with_agent
      end

      private

      def detect_with_agent
        if (
             ai_agent =
               AiAgent.find_by_id_from_cache(SiteSetting.ai_translation_locale_detector_agent)
           ).blank?
          return nil
        end

        agent_klass = ai_agent.class_instance
        agent = agent_klass.new

        llm_model = DiscourseAi::Translation::BaseTranslator.preferred_llm_model(agent_klass)
        return nil if llm_model.blank?

        bot =
          DiscourseAi::Agents::Bot.as(
            ai_agent.user || Discourse.system_user,
            agent: agent,
            model: llm_model,
          )

        context =
          DiscourseAi::Agents::BotContext.new(
            user: ai_agent.user || Discourse.system_user,
            skip_show_thinking: true,
            feature_name: "translation",
            messages: [{ type: :user, content: @text }],
            topic: @topic,
            post: @post,
          )

        result = +""
        bot.reply(context) do |partial, _, type|
          next if type == :thinking || partial.strip.blank?

          result << partial
        end

        result = result.strip.delete_suffix("\\")
        result.match?(LANGUAGE_TAG_REGEXP) ? result : nil
      end
    end
  end
end