A platform for community discussion. Free, open, simple.
FEATURE: Add AWS Bedrock Converse API provider (#38903)
Rafael dos Santos Silva18a0a8daeb2c
diff --git a/Gemfile b/Gemfileindex 1b2f247ec5d..6145632cb73 100644--- a/Gemfile+++ b/Gemfile@@ -64,6 +64,7 @@ gem "fastimage"gem "aws-sdk-s3", require: falsegem "aws-sdk-sns", require: falsegem "aws-sdk-mediaconvert", require: false+gem "aws-sdk-bedrockruntime", require: falsegem "excon"gem "unf", require: falsediff --git a/Gemfile.lock b/Gemfile.lockindex a2d7f6ea7ef..b76e1fa5fc9 100644--- a/Gemfile.lock+++ b/Gemfile.lock@@ -60,11 +60,15 @@ GEMast (2.4.3)aws-eventstream (1.4.0)aws-partitions (1.1134.0)- aws-sdk-core (3.227.0)+ aws-sdk-bedrockruntime (1.74.0)+ aws-sdk-core (~> 3, >= 3.244.0)+ aws-sigv4 (~> 1.5)+ aws-sdk-core (3.244.0)aws-eventstream (~> 1, >= 1.3.0)aws-partitions (~> 1, >= 1.992.0)aws-sigv4 (~> 1.9)base64+ bigdecimaljmespath (~> 1, >= 1.6.1)loggeraws-sdk-kms (1.99.0)@@ -777,6 +781,7 @@ DEPENDENCIESaddressableafmannotaterb+ aws-sdk-bedrockruntimeaws-sdk-mediaconvertaws-sdk-s3aws-sdk-sns@@ -946,7 +951,8 @@ CHECKSUMSast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383aws-eventstream (1.4.0) sha256=116bf85c436200d1060811e6f5d2d40c88f65448f2125bc77ffce5121e6e183baws-partitions (1.1134.0) sha256=28f5f6156777ac346904a79ce6cb3b14a521e3866fee12a0da86d7b500266d3e- aws-sdk-core (3.227.0) sha256=99071fc5e3ca9347873fd114574319740c3041745df3c74e27875bd912dfc79e+ aws-sdk-bedrockruntime (1.74.0) sha256=fc5eb0ab9485fc847bfcef058b8c50f2f7996d4a8266ce3562da2daa5bf1dea5+ aws-sdk-core (3.244.0) sha256=3e458c078b0c5bdee95bc370c3a483374b3224cf730c1f9f0faf849a5d9a18eaaws-sdk-kms (1.99.0) sha256=ba292fc3ffd672532aae2601fe55ff424eee78da8e23c23ba6ce4037138275a8aws-sdk-mediaconvert (1.165.0) sha256=d955a571cd8b0407c0778e1d0f2333a70bf9ab48e36c81da8c5b317db24001e4aws-sdk-s3 (1.182.0) sha256=d0fc3579395cb6cb69bf6e975240ce031fc673190e74c8dddbdd6c18572b450ddiff --git a/plugins/discourse-ai/app/models/ai_api_audit_log.rb b/plugins/discourse-ai/app/models/ai_api_audit_log.rbindex 5be11702f41..cfceddec4e5 100644--- a/plugins/discourse-ai/app/models/ai_api_audit_log.rb+++ b/plugins/discourse-ai/app/models/ai_api_audit_log.rb@@ -18,6 +18,7 @@ class AiApiAuditLog < ActiveRecord::BaseSambaNova = 8Mistral = 9OpenRouter = 10+ BedrockConverse = 11enddef next_log_iddiff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rbindex 41c9f3d2fc4..cf2eabe9cd1 100644--- a/plugins/discourse-ai/app/models/llm_model.rb+++ b/plugins/discourse-ai/app/models/llm_model.rb@@ -7,6 +7,7 @@ class LlmModel < ActiveRecord::BaseFIRST_BOT_USER_ID = -1200BEDROCK_PROVIDER_NAME = "aws_bedrock"+ BEDROCK_CONVERSE_PROVIDER_NAME = "aws_bedrock_converse"DEFAULT_ALLOWED_ATTACHMENT_TYPES = [].freezeATTACHMENT_TYPE_ALIASES = {"md" => "markdown",@@ -24,7 +25,9 @@ class LlmModel < ActiveRecord::Basevalidates :display_name, presence: true, length: { maximum: 100 }validates :tokenizer, presence: true, inclusion: DiscourseAi::Completions::Llm.tokenizer_namesvalidates :provider, presence: true, inclusion: DiscourseAi::Completions::Llm.provider_names- validates :url, presence: true, unless: -> { provider == BEDROCK_PROVIDER_NAME }+ validates :url,+ presence: true,+ unless: -> { provider.in?([BEDROCK_PROVIDER_NAME, BEDROCK_CONVERSE_PROVIDER_NAME]) }validates :name, presence: truevalidate :api_key_or_secret_presentvalidates :max_prompt_tokens, numericality: { greater_than: 0 }@@ -89,6 +92,40 @@ class LlmModel < ActiveRecord::Basedefault: "tool_results",},},+ aws_bedrock_converse: {+ access_key_id: :secret,+ role_arn: :text,+ region: :text,+ enable_reasoning: :checkbox,+ adaptive_thinking: {+ type: :checkbox,+ depends_on: :enable_reasoning,+ },+ reasoning_tokens: {+ type: :number,+ depends_on: :enable_reasoning,+ hidden_if: :adaptive_thinking,+ },+ effort: {+ type: :enum,+ values: %w[default low medium high max],+ default: "default",+ },+ disable_temperature: {+ type: :checkbox,+ hidden_if: %i[enable_reasoning adaptive_thinking],+ },+ disable_top_p: {+ type: :checkbox,+ hidden_if: %i[enable_reasoning adaptive_thinking],+ },+ prompt_caching: {+ type: :enum,+ values: %w[never tool_results always],+ default: "tool_results",+ },+ extra_model_fields: :text,+ },anthropic: {enable_reasoning: :checkbox,adaptive_thinking: {@@ -404,6 +441,8 @@ class LlmModel < ActiveRecord::Basedef api_key_or_secret_presentreturn if seeded?+ # Converse provider supports auto-resolved credentials (env vars, instance profile)+ return if provider == BEDROCK_CONVERSE_PROVIDER_NAMEif ai_secret_id.present?unless AiSecret.exists?(ai_secret_id)errors.add(:ai_secret_id, I18n.t("discourse_ai.llm_models.secret_not_found"))@@ -415,16 +454,19 @@ class LlmModel < ActiveRecord::Baseenddef required_provider_params- return if provider != BEDROCK_PROVIDER_NAME-- # Region is always required- if lookup_custom_param("region").blank?- errors.add(:base, I18n.t("discourse_ai.llm_models.missing_provider_param", param: "region"))- end+ if provider == BEDROCK_PROVIDER_NAME+ if lookup_custom_param("region").blank?+ errors.add(:base, I18n.t("discourse_ai.llm_models.missing_provider_param", param: "region"))+ end- # Either access_key_id or role_arn must be present- if lookup_custom_param("access_key_id").blank? && lookup_custom_param("role_arn").blank?- errors.add(:base, I18n.t("discourse_ai.llm_models.bedrock_missing_auth"))+ if lookup_custom_param("access_key_id").blank? && lookup_custom_param("role_arn").blank?+ errors.add(:base, I18n.t("discourse_ai.llm_models.bedrock_missing_auth"))+ end+ elsif provider == BEDROCK_CONVERSE_PROVIDER_NAME+ if lookup_custom_param("region").blank?+ errors.add(:base, I18n.t("discourse_ai.llm_models.missing_provider_param", param: "region"))+ end+ # access_key_id and role_arn are optional — SDK can auto-resolve credentialsendendenddiff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 3180d81b5ef..6cfe0dc9fc0 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -950,6 +950,7 @@ en:cost_measure: "$/1M tokens"providers:aws_bedrock: "AWS Bedrock"+ aws_bedrock_converse: "AWS Bedrock (Converse API)"anthropic: "Anthropic"vllm: "vLLM"hugging_face: "Hugging Face"diff --git a/plugins/discourse-ai/lib/completions/converse_message_processor.rb b/plugins/discourse-ai/lib/completions/converse_message_processor.rbnew file mode 100644index 00000000000..a144d01c9b8--- /dev/null+++ b/plugins/discourse-ai/lib/completions/converse_message_processor.rb@@ -0,0 +1,221 @@+# frozen_string_literal: true++class DiscourseAi::Completions::ConverseMessageProcessor+ PROVIDER_KEY = :bedrock_converse++ class ConverseToolCall+ attr_reader :name, :raw_json, :id++ def initialize(name, id, partial_tool_calls: false)+ @name = name+ @id = id+ @raw_json = +""+ @tool_call = DiscourseAi::Completions::ToolCall.new(id: id, name: name, parameters: {})+ @streaming_parser =+ DiscourseAi::Completions::JsonStreamingTracker.new(self) if partial_tool_calls+ end++ def append(json)+ @raw_json << json+ @streaming_parser << json if @streaming_parser+ end++ def notify_progress(key, value)+ @tool_call.partial = true+ @tool_call.parameters[key.to_sym] = value+ @has_new_data = true+ end++ def has_partial?+ @has_new_data+ end++ def partial_tool_call+ @has_new_data = false+ @tool_call+ end++ def to_tool_call+ parameters = {}+ parameters = JSON.parse(raw_json, symbolize_names: true) if raw_json.present?+ @tool_call = @tool_call.dup+ @tool_call.partial = false+ @tool_call.parameters = parameters+ @tool_call+ end+ end++ attr_reader :tool_calls,+ :input_tokens,+ :output_tokens,+ :cache_read_input_tokens,+ :cache_write_input_tokens,+ :output_thinking++ def initialize(streaming_mode:, partial_tool_calls: false, output_thinking: false)+ @streaming_mode = streaming_mode+ @tool_calls = []+ @current_tool_call = nil+ @partial_tool_calls = partial_tool_calls+ @output_thinking = output_thinking+ @thinking = nil+ end++ def to_tool_calls+ @tool_calls.map { |tool_call| tool_call.to_tool_call }+ end++ # Processes a streamed event from the Converse API.+ # Events are hashes with symbolized keys matching the SDK event structure.+ def process_streamed_message(parsed)+ return if !parsed+ result = nil++ type = parsed[:type]++ case type+ when :content_block_start+ start_data = parsed[:start]+ if start_data&.dig(:tool_use)+ tool = start_data[:tool_use]+ result = @current_tool_call.to_tool_call if @current_tool_call+ @current_tool_call =+ ConverseToolCall.new(+ tool[:name],+ tool[:tool_use_id],+ partial_tool_calls: @partial_tool_calls,+ )+ end+ when :content_block_delta+ delta = parsed[:delta]+ if delta&.key?(:tool_use)+ @current_tool_call&.append(delta[:tool_use][:input].to_s)+ result = @current_tool_call.partial_tool_call if @current_tool_call&.has_partial?+ elsif delta&.key?(:reasoning_content)+ if @output_thinking+ reasoning = delta[:reasoning_content]++ if reasoning[:redacted_content]+ result =+ DiscourseAi::Completions::Thinking.new(+ message: nil,+ partial: false,+ provider_info: {+ PROVIDER_KEY => {+ redacted: true,+ redacted_content: reasoning[:redacted_content],+ },+ },+ )+ elsif reasoning[:signature]+ # Signature delta — append to current thinking's provider_info+ if @thinking+ info = (@thinking.provider_info[PROVIDER_KEY] ||= { signature: +"", redacted: false })+ info[:signature] ||= +""+ info[:signature] << reasoning[:signature]+ end+ elsif reasoning[:text]+ text = reasoning[:text].to_s+ if @thinking+ @thinking.message << text+ result = DiscourseAi::Completions::Thinking.new(message: text, partial: true)+ else+ provider_info = { PROVIDER_KEY => { signature: +"", redacted: false } }+ @thinking =+ DiscourseAi::Completions::Thinking.new(+ message: +text,+ partial: true,+ provider_info: provider_info,+ )+ result = @thinking.dup+ end+ end+ end+ elsif delta&.key?(:text)+ text = delta[:text].to_s+ result = text unless text.empty?+ end+ when :content_block_stop+ if @thinking+ @thinking.partial = false+ result = @thinking+ @thinking = nil+ elsif @current_tool_call+ result = @current_tool_call.to_tool_call+ @current_tool_call = nil+ end+ when :message_stop+ # nothing to do+ when :metadata+ usage = parsed[:usage]+ if usage+ @input_tokens = usage[:input_tokens]+ @output_tokens = usage[:output_tokens]+ @cache_read_input_tokens = usage[:cache_read_input_tokens]+ @cache_write_input_tokens = usage[:cache_write_input_tokens]+ end+ when :message_start+ # nothing to do+ end++ result+ end++ # Processes a complete (non-streaming) Converse API response hash.+ def process_message(payload)+ parsed = payload+ parsed = JSON.parse(payload, symbolize_names: true) if payload.is_a?(String)++ result = []+ content = parsed.dig(:output, :message, :content)++ if content.is_a?(Array)+ content.each do |block|+ if block.key?(:text)+ result << block[:text]+ elsif block.key?(:tool_use)+ tool = block[:tool_use]+ call = ConverseToolCall.new(tool[:name], tool[:tool_use_id])+ call.append(tool[:input].to_json) if tool[:input]+ result << call.to_tool_call+ elsif block.key?(:reasoning_content)+ if @output_thinking+ reasoning = block[:reasoning_content]+ if reasoning[:redacted_content]+ result << DiscourseAi::Completions::Thinking.new(+ message: nil,+ provider_info: {+ PROVIDER_KEY => {+ redacted: true,+ redacted_content: reasoning[:redacted_content],+ },+ },+ )+ elsif reasoning[:reasoning_text]+ rt = reasoning[:reasoning_text]+ result << DiscourseAi::Completions::Thinking.new(+ message: rt[:text],+ provider_info: {+ PROVIDER_KEY => {+ signature: rt[:signature],+ redacted: false,+ },+ },+ )+ end+ end+ end+ end+ end++ usage = parsed[:usage]+ if usage+ @input_tokens = usage[:input_tokens]+ @output_tokens = usage[:output_tokens]+ @cache_read_input_tokens = usage[:cache_read_input_tokens]+ @cache_write_input_tokens = usage[:cache_write_input_tokens]+ end++ result+ end+enddiff --git a/plugins/discourse-ai/lib/completions/dialects/converse.rb b/plugins/discourse-ai/lib/completions/dialects/converse.rbnew file mode 100644index 00000000000..2492cedc76c--- /dev/null+++ b/plugins/discourse-ai/lib/completions/dialects/converse.rb@@ -0,0 +1,182 @@+# frozen_string_literal: true++module DiscourseAi+ module Completions+ module Dialects+ class Converse < Dialect+ class << self+ def can_translate?(llm_model)+ llm_model.provider == "aws_bedrock_converse"+ end+ end++ class ConversePrompt+ attr_reader :system, :messages, :tool_config++ def initialize(system, messages, tool_config = nil)+ @system = system+ @messages = messages+ @tool_config = tool_config+ end++ def system_prompt+ system.to_s+ end++ def has_tools?+ tool_config.present?+ end+ end++ def translate+ messages = super++ system = messages.shift[:content] if messages.first&.dig(:role) == "system"+ converse_messages =+ messages.map { |msg| { role: msg[:role], content: build_content(msg) } }++ # Converse API requires alternating user/assistant roles+ interleaved = []+ previous_message = nil+ converse_messages.each do |message|+ if previous_message+ if previous_message[:role] == "user" && message[:role] == "user"+ interleaved << { role: "assistant", content: [{ text: "OK" }] }+ elsif previous_message[:role] == "assistant" && message[:role] == "assistant"+ interleaved << { role: "user", content: [{ text: "OK" }] }+ end+ end+ interleaved << message+ previous_message = message+ end++ tool_config = tools_dialect.translated_tools++ ConversePrompt.new(system.presence && [{ text: system }], interleaved, tool_config)+ end++ def max_prompt_tokens+ llm_model.max_prompt_tokens+ end++ def native_tool_support?+ true+ end++ def tools_dialect+ @tools_dialect ||= DiscourseAi::Completions::Dialects::ConverseTools.new(prompt.tools)+ end++ private++ def build_content(msg)+ content = []++ existing_content = msg[:content]++ if existing_content.is_a?(Array)+ content.concat(existing_content)+ elsif existing_content.is_a?(Hash)+ content << existing_content+ elsif existing_content.is_a?(String)+ content << { text: existing_content }+ end++ msg[:images]&.each { |image| content << image }++ content+ end++ def detect_format(mime_type)+ case mime_type+ when "image/jpeg"+ "jpeg"+ when "image/png"+ "png"+ when "image/gif"+ "gif"+ when "image/webp"+ "webp"+ else+ "jpeg"+ end+ end++ def system_msg(msg)+ { role: "system", content: msg[:content] }+ end++ def user_msg(msg)+ images = nil+ if vision_support?+ encoded_uploads = prompt.encoded_uploads(msg)+ encoded_uploads&.each do |upload|+ images ||= []+ images << {+ image: {+ format: upload[:format] || detect_format(upload[:mime_type]),+ source: {+ bytes: upload[:base64],+ },+ },+ }+ end+ end++ { role: "user", content: DiscourseAi::Completions::Prompt.text_only(msg), images: images }+ end++ def model_msg(msg)+ content = []++ provider_info = converse_reasoning(msg)+ if provider_info.present?+ if msg[:thinking] && provider_info[:signature]+ content << {+ reasoning_content: {+ reasoning_text: {+ text: msg[:thinking],+ signature: provider_info[:signature],+ },+ },+ }+ end++ if provider_info[:redacted_content]+ content << {+ reasoning_content: {+ redacted_content: provider_info[:redacted_content],+ },+ }+ end+ end++ text = msg[:content]+ if text.is_a?(String)+ content << { text: text }+ elsif text.is_a?(Array)+ content.concat(text)+ end++ { role: "assistant", content: content }+ end++ def converse_reasoning(message)+ info = message[:thinking_provider_info]+ return if info.blank?+ info[:bedrock_converse] || info["bedrock_converse"]+ end++ def tool_msg(msg)+ translated = tools_dialect.from_raw_tool(msg)+ { role: "user", content: translated }+ end++ def tool_call_msg(msg)+ translated = tools_dialect.from_raw_tool_call(msg)+ { role: "assistant", content: translated }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/completions/dialects/converse_tools.rb b/plugins/discourse-ai/lib/completions/dialects/converse_tools.rbnew file mode 100644index 00000000000..0ed85c6eb57--- /dev/null+++ b/plugins/discourse-ai/lib/completions/dialects/converse_tools.rb@@ -0,0 +1,100 @@+# frozen_string_literal: true++module DiscourseAi+ module Completions+ module Dialects+ class ConverseTools+ def initialize(tools)+ @raw_tools = tools+ end++ def translated_tools+ return if !@raw_tools.present?++ {+ tools:+ @raw_tools.map do |tool|+ {+ tool_spec: {+ name: tool.name,+ description: tool.description,+ input_schema: {+ json: deep_stringify(tool.parameters_json_schema),+ },+ },+ }+ end,+ }+ end++ def from_raw_tool_call(raw_message)+ result = []++ provider_info = converse_reasoning(raw_message)+ if provider_info.present?+ if raw_message[:thinking] && provider_info[:signature]+ result << {+ reasoning_content: {+ reasoning_text: {+ text: raw_message[:thinking],+ signature: provider_info[:signature],+ },+ },+ }+ end++ if provider_info[:redacted_content]+ result << {+ reasoning_content: {+ redacted_content: provider_info[:redacted_content],+ },+ }+ end+ end++ result << {+ tool_use: {+ tool_use_id: raw_message[:id],+ name: raw_message[:name],+ input: JSON.parse(raw_message[:content])["arguments"],+ },+ }++ result+ end++ def from_raw_tool(raw_message)+ [+ {+ tool_result: {+ tool_use_id: raw_message[:id],+ content: [{ json: JSON.parse(raw_message[:content]) }],+ },+ },+ ]+ end++ private++ def deep_stringify(obj)+ case obj+ when Hash+ obj.transform_keys(&:to_s).transform_values { |v| deep_stringify(v) }+ when Array+ obj.map { |v| deep_stringify(v) }+ when Symbol+ obj.to_s+ else+ obj+ end+ end++ def converse_reasoning(message)+ info = message[:thinking_provider_info]+ return if info.blank?+ info[:bedrock_converse] || info["bedrock_converse"]+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rbindex 1e4537aee18..d0cadc06509 100644--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb@@ -14,6 +14,7 @@ module DiscourseAiDiscourseAi::Completions::Dialects::OpenAiResponses,DiscourseAi::Completions::Dialects::ChatGpt,DiscourseAi::Completions::Dialects::Gemini,+ DiscourseAi::Completions::Dialects::Converse,DiscourseAi::Completions::Dialects::Claude,DiscourseAi::Completions::Dialects::Command,DiscourseAi::Completions::Dialects::Ollama,diff --git a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rbnew file mode 100644index 00000000000..0eabc0b1a75--- /dev/null+++ b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb@@ -0,0 +1,498 @@+# frozen_string_literal: true++module DiscourseAi+ module Completions+ module Endpoints+ class AwsBedrockConverse < Base+ def self.can_contact?(llm_model)+ llm_model.provider == "aws_bedrock_converse"+ end++ def provider_id+ AiApiAuditLog::Provider::BedrockConverse+ end++ def perform_completion!(+ dialect,+ user,+ model_params = {},+ feature_name: nil,+ feature_context: nil,+ partial_tool_calls: false,+ output_thinking: false,+ cancel_manager: nil,+ execution_context: nil,+ &blk+ )+ LlmQuota.check_quotas!(@llm_model, user)+ LlmCreditAllocation.check_credits!(@llm_model, feature_name)++ start_time = Time.now++ return if cancel_manager&.cancelled?++ @partial_tool_calls = partial_tool_calls+ @output_thinking = output_thinking+ @streaming_mode = block_given?++ if block_given? && disable_streaming?+ result =+ perform_completion!(+ dialect,+ user,+ model_params,+ feature_name: feature_name,+ feature_context: feature_context,+ partial_tool_calls: partial_tool_calls,+ output_thinking: output_thinking,+ cancel_manager: cancel_manager,+ execution_context: execution_context,+ )++ wrapped = result+ wrapped = [result] if !result.is_a?(Array)+ wrapped.each do |partial|+ blk.call(partial)+ break if cancel_manager&.cancelled?+ end+ return result+ end++ max_tokens = enforce_max_output_tokens(model_params[:max_tokens])+ model_params[:max_tokens] = max_tokens if max_tokens+ model_params = normalize_model_params(model_params)++ prompt = dialect.translate++ structured_output = nil+ if model_params[:response_format].present?+ schema_properties =+ model_params[:response_format].dig(:json_schema, :schema, :properties)+ if schema_properties.present?+ structured_output = DiscourseAi::Completions::StructuredOutput.new(schema_properties)+ end+ end++ call_status = :error+ cancelled = false+ cancel_manager_callback = nil+ response_data = +""+ partials_raw = +""+ raw_response = +""+ log = nil++ sdk_params = build_converse_params(prompt, model_params, dialect)+ request_body = sdk_params.to_json++ log =+ start_log(+ provider_id: provider_id,+ request_body: request_body,+ dialect: dialect,+ prompt: prompt,+ user: user,+ feature_name: feature_name,+ feature_context: feature_context,+ )++ return if cancel_manager&.cancelled?++ processor =+ DiscourseAi::Completions::ConverseMessageProcessor.new(+ streaming_mode: @streaming_mode,+ partial_tool_calls: partial_tool_calls,+ output_thinking: output_thinking,+ )++ client = build_sdk_client++ begin+ if @streaming_mode+ orig_blk = blk+ blk =+ lambda do |partial|+ partials_raw << partial.to_s+ response_data << partial if partial.is_a?(String)+ if partial.is_a?(String) && structured_output.present?+ structured_output << partial if !partial.empty?+ partial = structured_output+ end+ orig_blk.call(partial) if partial+ end++ if cancel_manager+ cancel_manager_callback =+ lambda do+ cancelled = true+ call_status = :cancelled+ end+ cancel_manager.add_callback(cancel_manager_callback)+ end++ catch(:cancelled) do+ handler = build_stream_handler(processor, blk) { throw :cancelled if cancelled }++ client.converse_stream(sdk_params.merge(event_stream_handler: handler))+ end++ unless cancelled+ if structured_output+ structured_output.finish+ if structured_output.broken?+ blk.call("")+ else+ blk.call(structured_output)+ end+ end+ end++ call_status = :success unless cancelled+ response_data+ else+ resp = client.converse(sdk_params)+ raw_response << resp.to_h.to_json++ results = processor.process_message(resp.to_h.deep_symbolize_keys)+ results.each { |partial| partials_raw << partial.to_s }++ if structured_output.present?+ results.each { |data| structured_output << data if data.is_a?(String) }+ structured_output.finish+ call_status = :success+ return structured_output+ end++ response_data = results.length == 1 ? results.first : results+ response_data = "" if response_data.nil?+ call_status = :success+ response_data+ end+ rescue Aws::BedrockRuntime::Errors::ServiceError => e+ Rails.logger.error("#{self.class.name}: #{e.class}: #{e.message}")+ raise CompletionFailed, e.message+ ensure+ should_log = log && call_status != :cancelled++ if should_log+ log.raw_response_payload = raw_response if raw_response.present?+ log.request_tokens = processor.input_tokens if processor.input_tokens+ log.response_tokens = processor.output_tokens if processor.output_tokens+ log.cache_read_tokens =+ processor.cache_read_input_tokens if processor.cache_read_input_tokens+ log.cache_write_tokens =+ processor.cache_write_input_tokens if processor.cache_write_input_tokens+ log.response_tokens = tokenizer.size(partials_raw) if log.response_tokens.blank?+ log.response_status ||= 200+ log.created_at = start_time+ log.updated_at = Time.now+ log.duration_msecs = (Time.now - start_time) * 1000+ log.save!++ execution_context&.token_usage_tracker&.add_from_audit_log(log)++ AiApiRequestStat.record_from_audit_log(log, llm_model: @llm_model)+ LlmQuota.log_usage(@llm_model, user, log.request_tokens, log.response_tokens)+ LlmCreditAllocation.deduct_credits!(+ @llm_model,+ feature_name,+ log.request_tokens,+ log.response_tokens,+ )++ DiscourseAi::Completions::LlmMetric.record(+ llm_model: @llm_model,+ feature_name: feature_name,+ request_tokens: log.request_tokens || 0,+ response_tokens: log.response_tokens || 0,+ duration_ms: log.duration_msecs,+ status: call_status,+ )+ end++ track_failures(call_status)++ if cancel_manager && cancel_manager_callback+ cancel_manager.remove_callback(cancel_manager_callback)+ end+ end+ rescue IOError, StandardError+ raise if !cancelled+ end++ def default_options(dialect)+ options = {}++ if llm_model.lookup_custom_param("adaptive_thinking")+ options[:thinking] = { type: "adaptive" }+ elsif llm_model.lookup_custom_param("enable_reasoning")+ reasoning_tokens =+ llm_model.lookup_custom_param("reasoning_tokens").to_i.clamp(1024, 32_768)+ options[:thinking] = { type: "enabled", budget_tokens: reasoning_tokens }+ end++ effort = llm_model.lookup_custom_param("effort")+ options[:output_config] = { effort: effort } if %w[low medium high max].include?(effort)++ options+ end++ private++ def normalize_model_params(model_params)+ model_params = model_params.dup++ thinking_enabled =+ llm_model.lookup_custom_param("adaptive_thinking") ||+ llm_model.lookup_custom_param("enable_reasoning")++ if thinking_enabled+ model_params.delete(:temperature)+ model_params.delete(:top_p)+ else+ model_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")+ if llm_model.lookup_custom_param("disable_temperature")+ model_params.delete(:temperature)+ end+ end++ model_params+ end++ def prompt_size(prompt)+ tokenizer.size(prompt.system_prompt.to_s + " " + prompt.messages.to_s)+ end++ def build_sdk_client+ require "aws-sdk-bedrockruntime" unless defined?(Aws::BedrockRuntime)++ region = llm_model.lookup_custom_param("region")+ client_options = { region: region, http_read_timeout: TIMEOUT }++ role_arn = llm_model.lookup_custom_param("role_arn")+ access_key_id = llm_model.lookup_custom_param("access_key_id")++ if role_arn.present?+ require "aws-sdk-sts" unless defined?(Aws::STS)+ client_options[:credentials] = Aws::AssumeRoleCredentials.new(+ role_arn: role_arn,+ role_session_name: "discourse-bedrock-converse-#{Process.pid}",+ client: Aws::STS::Client.new(region: region),+ )+ elsif access_key_id.present?+ client_options[:credentials] = Aws::Credentials.new(access_key_id, llm_model.api_key)+ elsif llm_model.api_key.present?+ # Bedrock API key auth — Bearer token+ client_options[:token_provider] = Aws::StaticTokenProvider.new(llm_model.api_key)+ client_options[:auth_scheme_preference] = ["httpBearerAuth"]+ end+ # If nothing is set, SDK auto-resolves from env/instance profile/ECS++ Aws::BedrockRuntime::Client.new(client_options)+ end++ def build_converse_params(prompt, model_params, dialect)+ options = default_options(dialect).merge(model_params.except(:response_format))++ params = { model_id: llm_model.name, messages: prompt.messages }++ params[:system] = prompt.system if prompt.system.present?++ inference_config = {}+ inference_config[:max_tokens] = options[:max_tokens] if options[:max_tokens]+ inference_config[:temperature] = options[:temperature] if options[:temperature]+ inference_config[:top_p] = options[:top_p] if options[:top_p]+ params[:inference_config] = inference_config if inference_config.present?++ if prompt.has_tools? && prompt.tool_config+ tool_config = prompt.tool_config.dup+ if dialect.tool_choice == :none+ tool_config = nil+ elsif dialect.tool_choice.is_a?(String) || dialect.tool_choice.is_a?(Symbol)+ choice = dialect.tool_choice.to_s+ if choice == "any"+ tool_config[:tool_choice] = { any: {} }+ elsif choice != "auto" && choice != "none"+ tool_config[:tool_choice] = { tool: { name: choice } }+ end+ end+ params[:tool_config] = tool_config if tool_config+ end++ additional = {}+ additional[:thinking] = options[:thinking] if options[:thinking]+ additional[:output_config] = options[:output_config] if options[:output_config]++ if model_params[:response_format].present?+ response_format = model_params[:response_format].deep_symbolize_keys+ json_schema = response_format.dig(:json_schema, :schema)+ if json_schema.present?+ schema_str = json_schema.is_a?(String) ? json_schema : JSON.generate(json_schema)+ params[:output_config] = {+ text_format: {+ type: "json_schema",+ structure: {+ json_schema: {+ schema: schema_str,+ name: response_format.dig(:json_schema, :name) || "response_schema",+ },+ },+ },+ }+ end+ end++ extra = llm_model.lookup_custom_param("extra_model_fields")+ if extra.present?+ begin+ additional.deep_merge!(JSON.parse(extra).deep_symbolize_keys)+ rescue JSON::ParserError+ # ignore malformed JSON+ end+ end++ params[:additional_model_request_fields] = additional if additional.present?++ apply_cache_points!(params, prompt) if should_apply_caching?(prompt)++ params+ end++ def should_apply_caching?(prompt)+ caching_mode = llm_model.lookup_custom_param("prompt_caching") || "never"+ return false if caching_mode == "never"++ case caching_mode+ when "always"+ true+ when "tool_results"+ prompt+ .messages+ .last(5)+ .any? do |msg|+ content = msg[:content]+ if content.is_a?(Array)+ content.any? { |c| c.is_a?(Hash) && c[:tool_result] }+ elsif content.is_a?(Hash)+ content[:tool_result].present?+ else+ false+ end+ end+ else+ false+ end+ end++ def apply_cache_points!(params, prompt)+ if params[:messages].present?+ last_message = params[:messages].last+ if last_message[:content].is_a?(Array)+ last_message[:content] << { cache_point: { type: "default" } }+ elsif last_message[:content].is_a?(String)+ last_message[:content] = [+ { text: last_message[:content] },+ { cache_point: { type: "default" } },+ ]+ end+ end+ end++ def build_stream_handler(processor, blk, &cancel_check)+ handler = Aws::BedrockRuntime::EventStreams::ConverseStreamOutput.new++ handler.on_content_block_start_event do |event|+ cancel_check.call++ parsed = event_to_parsed(:content_block_start, event)+ result = processor.process_streamed_message(parsed)+ blk.call(result) if result+ end++ handler.on_content_block_delta_event do |event|+ cancel_check.call++ parsed = event_to_parsed(:content_block_delta, event)+ result = processor.process_streamed_message(parsed)+ blk.call(result) if result+ end++ handler.on_content_block_stop_event do |_event|+ cancel_check.call++ result = processor.process_streamed_message({ type: :content_block_stop })+ blk.call(result) if result+ end++ handler.on_message_start_event { |_event| cancel_check.call }++ handler.on_message_stop_event { |_event| cancel_check.call }++ handler.on_metadata_event do |event|+ usage = event.usage+ if usage+ parsed = {+ type: :metadata,+ usage: {+ input_tokens: usage.input_tokens,+ output_tokens: usage.output_tokens,+ cache_read_input_tokens:+ (+ if usage.respond_to?(:cache_read_input_tokens)+ usage.cache_read_input_tokens+ else+ nil+ end+ ),+ cache_write_input_tokens:+ if usage.respond_to?(:cache_write_input_tokens)+ usage.cache_write_input_tokens+ end,+ },+ }+ processor.process_streamed_message(parsed)+ end+ end++ handler.on_error_event do |event|+ Rails.logger.error("#{self.class.name}: stream error: #{event.inspect}")+ end++ handler+ end++ def event_to_parsed(type, event)+ parsed = { type: type }++ case type+ when :content_block_start+ start_data = event.start+ if start_data.respond_to?(:tool_use) && start_data.tool_use+ tool = start_data.tool_use+ parsed[:start] = { tool_use: { name: tool.name, tool_use_id: tool.tool_use_id } }+ end+ when :content_block_delta+ delta = event.delta+ if delta+ if delta.respond_to?(:text) && delta.text+ parsed[:delta] = { text: delta.text }+ elsif delta.respond_to?(:tool_use) && delta.tool_use+ parsed[:delta] = { tool_use: { input: delta.tool_use.input } }+ elsif delta.respond_to?(:reasoning_content) && delta.reasoning_content+ rc = delta.reasoning_content+ rc_hash = {}+ rc_hash[:text] = rc.text if rc.respond_to?(:text) && rc.text+ rc_hash[:signature] = rc.signature if rc.respond_to?(:signature) && rc.signature+ if rc.respond_to?(:redacted_content) && rc.redacted_content+ rc_hash[:redacted_content] = rc.redacted_content+ end+ parsed[:delta] = { reasoning_content: rc_hash }+ end+ end+ end++ parsed+ end+ end+ end+ end+enddiff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rbindex ca0df8be651..4a462a4025b 100644--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb@@ -18,6 +18,7 @@ module DiscourseAiclass << selfdef endpoint_for(llm_model)endpoints = [+ DiscourseAi::Completions::Endpoints::AwsBedrockConverse,DiscourseAi::Completions::Endpoints::AwsBedrock,DiscourseAi::Completions::Endpoints::OpenAi,DiscourseAi::Completions::Endpoints::OpenAiResponses,diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rbindex b844e3bc75d..8cf9d11ba85 100644--- a/plugins/discourse-ai/lib/completions/llm.rb+++ b/plugins/discourse-ai/lib/completions/llm.rb@@ -25,6 +25,7 @@ module DiscourseAidef provider_namesproviders = %w[aws_bedrock+ aws_bedrock_converseanthropicvllmhugging_facediff --git a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rbindex 5d8bec763f8..a7d377594b0 100644--- a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb+++ b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb@@ -76,6 +76,14 @@ Fabricator(:bedrock_model, from: :anthropic_model) doprovider_params { { region: "us-east-1", access_key_id: "123456" } }end+Fabricator(:bedrock_converse_model, from: :anthropic_model) do+ url ""+ provider "aws_bedrock_converse"+ api_key "asd-asd-asd"+ name "claude-3-sonnet"+ provider_params { { region: "us-east-1", access_key_id: "123456" } }+end+Fabricator(:nova_model, from: :llm_model) dodisplay_name "Amazon Nova pro"name "amazon.nova-pro-v1:0"diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rbnew file mode 100644index 00000000000..c54c5e748ca--- /dev/null+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rb@@ -0,0 +1,280 @@+# frozen_string_literal: true++require "aws-sdk-bedrockruntime"++RSpec.describe DiscourseAi::Completions::Endpoints::AwsBedrockConverse do+ subject(:endpoint) { described_class.new(model) }++ fab!(:user)+ fab!(:model, :bedrock_converse_model)++ before { enable_current_plugin }++ def mock_converse_response(text: "Hello world", input_tokens: 10, output_tokens: 5)+ response =+ Aws::BedrockRuntime::Types::ConverseResponse.new(+ output:+ Aws::BedrockRuntime::Types::ConverseOutput.new(+ message:+ Aws::BedrockRuntime::Types::Message.new(+ role: "assistant",+ content: [Aws::BedrockRuntime::Types::ContentBlock.new(text: text)],+ ),+ ),+ stop_reason: "end_turn",+ usage:+ Aws::BedrockRuntime::Types::TokenUsage.new(+ input_tokens: input_tokens,+ output_tokens: output_tokens,+ ),+ )+ response+ end++ def stub_sdk_client(response: nil, &stream_block)+ client = instance_double(Aws::BedrockRuntime::Client)++ allow(client).to receive(:converse).and_return(response) if response++ if stream_block+ allow(client).to receive(:converse_stream) do |params|+ handler = params[:event_stream_handler]+ listeners = handler.event_emitter.instance_variable_get(:@listeners)+ stream_block.call(listeners)+ end+ end++ allow(Aws::BedrockRuntime::Client).to receive(:new).and_return(client)+ client+ end++ def fire_event(listeners, type, event)+ listeners[type]&.each { |cb| cb.call(event) }+ end++ def fire_content_block_delta(listeners, text:, index: 0)+ event =+ Aws::BedrockRuntime::Types::ContentBlockDeltaEvent.new(+ delta: Aws::BedrockRuntime::Types::ContentBlockDelta.new(text: text),+ content_block_index: index,+ )+ fire_event(listeners, :content_block_delta, event)+ end++ def fire_content_block_stop(listeners, index: 0)+ event = Aws::BedrockRuntime::Types::ContentBlockStopEvent.new(content_block_index: index)+ fire_event(listeners, :content_block_stop, event)+ end++ def fire_message_start(listeners)+ event = Aws::BedrockRuntime::Types::MessageStartEvent.new(role: "assistant")+ fire_event(listeners, :message_start, event)+ end++ def fire_message_stop(listeners)+ event = Aws::BedrockRuntime::Types::MessageStopEvent.new(stop_reason: "end_turn")+ fire_event(listeners, :message_stop, event)+ end++ def fire_metadata(listeners, input_tokens: 10, output_tokens: 5)+ event =+ Aws::BedrockRuntime::Types::ConverseStreamMetadataEvent.new(+ usage:+ Aws::BedrockRuntime::Types::TokenUsage.new(+ input_tokens: input_tokens,+ output_tokens: output_tokens,+ ),+ )+ fire_event(listeners, :metadata, event)+ end++ describe "can_contact?" do+ it "returns true for aws_bedrock_converse provider" do+ expect(described_class.can_contact?(model)).to eq(true)+ end++ it "returns false for other providers" do+ model.provider = "aws_bedrock"+ expect(described_class.can_contact?(model)).to eq(false)+ end+ end++ describe "provider_id" do+ it "returns BedrockConverse" do+ expect(endpoint.provider_id).to eq(AiApiAuditLog::Provider::BedrockConverse)+ end+ end++ describe "default_options" do+ it "does not set max_tokens by default" do+ prompt = DiscourseAi::Completions::Prompt.new("hello")+ dialect = DiscourseAi::Completions::Dialects::Converse.new(prompt, model)++ expect(endpoint.default_options(dialect)).not_to have_key(:max_tokens)+ end++ it "configures adaptive thinking" do+ model.provider_params["enable_reasoning"] = true+ model.provider_params["adaptive_thinking"] = true++ prompt = DiscourseAi::Completions::Prompt.new("hello")+ dialect = DiscourseAi::Completions::Dialects::Converse.new(prompt, model)++ options = endpoint.default_options(dialect)+ expect(options[:thinking]).to eq({ type: "adaptive" })+ end++ it "configures reasoning with budget tokens" do+ model.provider_params["enable_reasoning"] = true+ model.provider_params["reasoning_tokens"] = 4096++ prompt = DiscourseAi::Completions::Prompt.new("hello")+ dialect = DiscourseAi::Completions::Dialects::Converse.new(prompt, model)++ options = endpoint.default_options(dialect)+ expect(options[:thinking]).to eq({ type: "enabled", budget_tokens: 4096 })+ end++ it "configures effort" do+ model.provider_params["effort"] = "high"++ prompt = DiscourseAi::Completions::Prompt.new("hello")+ dialect = DiscourseAi::Completions::Dialects::Converse.new(prompt, model)++ options = endpoint.default_options(dialect)+ expect(options[:output_config]).to eq({ effort: "high" })+ end+ end++ describe "non-streaming completion" do+ it "completes a simple prompt" do+ response = mock_converse_response(text: "Test response", input_tokens: 15, output_tokens: 8)+ client = stub_sdk_client(response: response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ result = llm.generate("hello", user: user)++ expect(result).to eq("Test response")+ expect(AiApiAuditLog.last.request_tokens).to eq(15)+ expect(AiApiAuditLog.last.response_tokens).to eq(8)+ end++ it "passes model name directly as model_id to SDK" do+ response = mock_converse_response+ client = stub_sdk_client(response: response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user)++ expect(client).to have_received(:converse) do |params|+ expect(params[:model_id]).to eq("claude-3-sonnet")+ expect(params[:system]).to be_present+ end+ end+ end++ describe "streaming completion" do+ it "streams text responses" do+ partials = []++ stub_sdk_client do |listeners|+ fire_message_start(listeners)+ fire_content_block_delta(listeners, text: "Hello")+ fire_content_block_delta(listeners, text: " ")+ fire_content_block_delta(listeners, text: "world")+ fire_content_block_stop(listeners)+ fire_message_stop(listeners)+ fire_metadata(listeners, input_tokens: 10, output_tokens: 3)+ end++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user) { |partial| partials << partial }++ expect(partials).to eq(["Hello", " ", "world"])+ expect(AiApiAuditLog.last.request_tokens).to eq(10)+ expect(AiApiAuditLog.last.response_tokens).to eq(3)+ end+ end++ describe "credential resolution" do+ it "uses static credentials when access_key_id is provided" do+ stub_sdk_client(response: mock_converse_response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user)++ expect(Aws::BedrockRuntime::Client).to have_received(:new) do |params|+ expect(params[:region]).to eq("us-east-1")+ expect(params[:credentials]).to be_an_instance_of(Aws::Credentials)+ end+ end++ it "uses role-based credentials when role_arn is provided" do+ model.update!(+ provider_params: {+ "role_arn" => "arn:aws:iam::123456:role/test",+ "region" => "us-east-1",+ },+ )++ sts_client = instance_double(Aws::STS::Client)+ allow(Aws::STS::Client).to receive(:new).and_return(sts_client)+ assume_role_creds = instance_double(Aws::AssumeRoleCredentials)+ allow(Aws::AssumeRoleCredentials).to receive(:new).and_return(assume_role_creds)++ stub_sdk_client(response: mock_converse_response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user)++ expect(Aws::BedrockRuntime::Client).to have_received(:new) do |params|+ expect(params[:credentials]).to eq(assume_role_creds)+ end+ end++ it "uses Bearer token auth when only api_key is provided" do+ model.update!(provider_params: { "region" => "us-east-1" }, api_key: "br-abc123")++ stub_sdk_client(response: mock_converse_response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user)++ expect(Aws::BedrockRuntime::Client).to have_received(:new) do |params|+ expect(params).not_to have_key(:credentials)+ expect(params[:token_provider]).to be_an_instance_of(Aws::StaticTokenProvider)+ expect(params[:token_provider].token.token).to eq("br-abc123")+ end+ end++ it "auto-resolves credentials when nothing is provided" do+ model.update!(provider_params: { "region" => "us-east-1" }, api_key: nil)++ stub_sdk_client(response: mock_converse_response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user)++ expect(Aws::BedrockRuntime::Client).to have_received(:new) do |params|+ expect(params).not_to have_key(:credentials)+ expect(params).not_to have_key(:token_provider)+ end+ end+ end++ describe "error handling" do+ it "raises CompletionFailed on SDK errors" do+ client = instance_double(Aws::BedrockRuntime::Client)+ allow(client).to receive(:converse).and_raise(+ Aws::BedrockRuntime::Errors::ThrottlingException.new(nil, "Rate exceeded"),+ )+ allow(Aws::BedrockRuntime::Client).to receive(:new).and_return(client)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")++ expect { llm.generate("hello", user: user) }.to raise_error(+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,+ )+ end+ end+end