Skip to content

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

7.3 KB
Raw Blame
# frozen_string_literal: true

module DiscourseAi
  module Completions
    module Dialects
      class Claude < Dialect
        class << self
          def can_translate?(llm_model)
            llm_model.provider == "anthropic" ||
              (llm_model.provider == "aws_bedrock") &&
                (llm_model.name.include?("anthropic") || llm_model.name.include?("claude"))
          end
        end

        class ClaudePrompt
          attr_reader :system_prompt, :messages, :tools, :tool_choice

          def initialize(system_prompt, messages, tools, tool_choice)
            @system_prompt = system_prompt
            @messages = messages
            @tools = tools
            @tool_choice = tool_choice
          end

          def has_tools?
            tools.present?
          end
        end

        def translate
          messages = super

          system_prompt = messages.shift[:content] if messages.first[:role] == "system"

          if !system_prompt && !native_tool_support?
            system_prompt = tools_dialect.instructions.presence
          end

          interleving_messages = []
          previous_message = nil

          messages.each do |message|
            if previous_message
              if previous_message[:role] == "user" && message[:role] == "user"
                interleving_messages << { role: "assistant", content: "OK" }
              elsif previous_message[:role] == "assistant" && message[:role] == "assistant"
                interleving_messages << { role: "user", content: "OK" }
              end
            end
            interleving_messages << message
            previous_message = message
          end

          tools = nil
          tools = tools_dialect.translated_tools if native_tool_support?

          tools = (tools || []).concat(native_tools) if native_tools.present?

          ClaudePrompt.new(system_prompt.presence, interleving_messages, tools, tool_choice)
        end

        def native_tools
          tools = []
          if prompt.native_tool?(DiscourseAi::Completions::NativeTools::WEB_SEARCH)
            tools << { type: "web_search_20250305", name: "web_search" }
          end
          if prompt.native_tool?(DiscourseAi::Completions::NativeTools::WEB_FETCH)
            tools << { type: "web_fetch_20260209", name: "web_fetch", allowed_callers: %w[direct] }
          end
          tools
        end

        def max_prompt_tokens
          max_prompt_tokens_with_reserved_output
        end

        def native_tool_support?
          !llm_model.lookup_custom_param("disable_native_tools")
        end

        private

        def tools_dialect
          if native_tool_support?
            @tools_dialect ||= DiscourseAi::Completions::Dialects::ClaudeTools.new(prompt.tools)
          else
            super
          end
        end

        def tool_call_msg(msg)
          translated = tools_dialect.from_raw_tool_call(msg)
          { role: "assistant", content: translated }
        end

        def tool_msg(msg)
          translated = tools_dialect.from_raw_tool(msg)
          { role: "user", content: translated }
        end

        def model_msg(msg)
          anthropic = anthropic_reasoning(msg)
          if (content_blocks = anthropic&.dig(:content_blocks)).present?
            content_blocks = content_blocks.deep_dup
            if msg[:thinking] && anthropic[:signature] &&
                 !content_blocks.any? { |block| content_block_type(block) == "thinking" }
              content_blocks.unshift(
                { type: "thinking", thinking: msg[:thinking], signature: anthropic[:signature] },
              )
            end
            if anthropic[:redacted_signature] &&
                 !content_blocks.any? { |block| content_block_type(block) == "redacted_thinking" }
              content_blocks.unshift(
                { type: "redacted_thinking", data: anthropic[:redacted_signature] },
              )
            end
            return { role: "assistant", content: content_blocks }
          end

          content_array = []

          if anthropic.present?
            if msg[:thinking] && anthropic[:signature]
              content_array << {
                type: "thinking",
                thinking: msg[:thinking],
                signature: anthropic[:signature],
              }
            end

            if anthropic[:redacted_signature]
              content_array << { type: "redacted_thinking", data: anthropic[:redacted_signature] }
            end
          end

          # other encoder is used to pass through thinking
          content_array =
            to_encoded_content_array(
              content: [content_array, msg[:content]].flatten,
              upload_encoder: ->(_details) {},
              text_encoder: ->(text) { { type: "text", text: text } },
              other_encoder: ->(details) { details },
              allow_images: false,
              allow_documents: false,
            )

          { role: "assistant", content: no_array_if_only_text(content_array) }
        end

        def content_block_type(block)
          block[:type] || block["type"]
        end

        def anthropic_reasoning(message)
          info = message[:thinking_provider_info]
          return if info.blank?
          info[:anthropic] || info["anthropic"]
        end

        def system_msg(msg)
          msg = { role: "system", content: msg[:content] }

          if tools_dialect.instructions.present?
            msg[:content] = msg[:content].dup << "\n\n#{tools_dialect.instructions}"
          end

          msg
        end

        def user_msg(msg)
          content_array = []
          if (prefix = user_id_prefix(msg))
            content_array << prefix
          end
          content_array.concat([msg[:content]].flatten)

          content_array =
            to_encoded_content_array(
              content: content_array,
              upload_encoder: ->(details) { upload_node(details) },
              text_encoder: ->(text) { { type: "text", text: text } },
              allow_images: vision_support?,
              allow_documents: true,
              allowed_attachment_types: llm_model.allowed_attachment_types,
              upload_filter: ->(encoded) { document_allowed?(encoded) },
            )

          { role: "user", content: no_array_if_only_text(content_array) }
        end

        # keeping our payload as backward compatible as possible
        def no_array_if_only_text(content_array)
          if content_array.length == 1 && content_array.first[:type] == "text"
            content_array.first[:text]
          else
            content_array
          end
        end

        def image_node(details)
          {
            source: {
              type: "base64",
              data: details[:base64],
              media_type: details[:mime_type],
            },
            type: "image",
          }
        end

        def upload_node(details)
          return if details.blank?
          return { type: "text", text: details[:text] } if details[:text].present?

          if details[:kind] == :document || details[:mime_type] == "application/pdf"
            {
              type: "document",
              source: {
                type: "base64",
                data: details[:base64],
                media_type: details[:mime_type],
              },
            }
          else
            image_node(details)
          end
        end
      end
    end
  end
end