Skip to content

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

FEATURE: Add Gemini Interactions API support (#42206)

Sam7b04c3dab2e2
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
index c5cecddb1d9..d1ffe1c4551 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs
@@ -1,6 +1,6 @@
import Component from "@glimmer/component";
import { cached, tracked } from "@glimmer/tracking";
-import { concat, fn, get } from "@ember/helper";
+import { fn, get } from "@ember/helper";
import { action } from "@ember/object";
import { LinkTo } from "@ember/routing";
import { later } from "@ember/runloop";
@@ -22,6 +22,7 @@ import AiLlmAttachmentTypes from "discourse/plugins/discourse-ai/discourse/compo
import {
isProviderParamHidden,
normalizeProviderParams,
+ providerParamLabel,
} from "discourse/plugins/discourse-ai/discourse/lib/llm-provider-param-helpers";
import DurationSelector from "./ai-quota-duration-selector";
import AiSecretSelector from "./ai-secret-selector";
@@ -410,9 +411,7 @@ export default class AiLlmEditorForm extends Component {
}}
<object.Field
@name={{name}}
- @title={{i18n
- (concat "discourse_ai.llms.provider_fields." name)
- }}
+ @title={{i18n (providerParamLabel name params)}}
@tooltip={{if params.tooltip (i18n params.tooltip)}}
@helpText={{if params.helpText (i18n params.helpText)}}
@showTitle={{not (eq params.type "checkbox")}}
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rb
index a916e34078c..8a0f8ca0ec2 100644
--- a/plugins/discourse-ai/app/models/llm_model.rb
+++ b/plugins/discourse-ai/app/models/llm_model.rb
@@ -287,6 +287,25 @@ class LlmModel < ActiveRecord::Base
mistral: {
disable_native_tools: :checkbox,
},
+ gemini_interactions: {
+ disable_native_tools: :checkbox,
+ thinking_level: {
+ type: :enum,
+ values: %w[default minimal low medium high],
+ default: "default",
+ label: "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ },
+ disable_temperature: {
+ type: :checkbox,
+ hidden_if: :thinking_level,
+ },
+ disable_top_p: :checkbox,
+ service_tier: {
+ type: :enum,
+ values: %w[default standard flex priority],
+ default: "default",
+ },
+ },
google: {
disable_native_tools: :checkbox,
enable_thinking: :checkbox,
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/llm-provider-param-helpers.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/llm-provider-param-helpers.js
index 56d987cc62a..5f1f9ffd88a 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/llm-provider-param-helpers.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/llm-provider-param-helpers.js
@@ -34,6 +34,10 @@ export function isProviderParamHidden(paramMeta, providerParamsData) {
return false;
}
+export function providerParamLabel(field, paramMeta) {
+ return paramMeta.label || `discourse_ai.llms.provider_fields.${field}`;
+}
+
// Normalizes raw provider_params metadata (from the server) into a
// consistent shape for the editor form.
export function normalizeProviderParams(rawParams) {
@@ -63,6 +67,7 @@ export function normalizeProviderParams(rawParams) {
default: value.default ?? undefined,
hidden_if: value.hidden_if ?? undefined,
depends_on: value.depends_on ?? undefined,
+ label: value.label ?? undefined,
tooltip: value.tooltip ?? undefined,
helpText: value.helpText ?? value.help_text ?? undefined,
};
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index dde949f4f14..cc19b706eab 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1062,8 +1062,8 @@ en:
anthropic-claude-opus-4-7: "Anthropic's most intelligent model"
anthropic-claude-sonnet-4-6: "Optimal balance of speed and cost"
anthropic-claude-haiku-4-5: "Fast and cost-effective"
- google-gemini-3-1-pro: "Google's most intelligent model"
- google-gemini-3-flash: "Fast and cost-efficient with multimodal reasoning"
+ google-gemini-3-1-pro-preview: "Google's most intelligent model"
+ google-gemini-3-6-flash: "Fast, frontier-level intelligence for agentic workloads"
open_ai-gpt-5-6-sol: "OpenAI's frontier model for complex professional work"
open_ai-gpt-5-6-terra: "Balances intelligence and cost for demanding workloads"
open_ai-gpt-5-6-luna: "Optimized for cost-sensitive, high-volume workloads"
@@ -1117,7 +1117,8 @@ en:
hugging_face: "Hugging Face"
cohere: "Cohere"
open_ai: "OpenAI"
- google: "Google"
+ google: "Google Gemini (Legacy API)"
+ gemini_interactions: "Google Gemini"
google_vertex_ai: "Google Vertex AI"
azure: "Azure"
ollama: "Ollama"
@@ -1152,6 +1153,7 @@ en:
adaptive_thinking: "Enable adaptive thinking (recommended for Claude 4.6 models)"
enable_thinking: "Enable thinking (Gemini 2.5 models - uses token budget)"
thinking_level: "Thinking level (Gemini 3 models - controls reasoning depth)"
+ gemini_interactions_thinking_level: "Thinking level"
thinking_tokens: "Number of tokens used for thinking"
reasoning_tokens: "Number of tokens used for reasoning"
disable_temperature: "Disable temperature (some thinking models don't support temperature)"
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 90318a5e18a..66d11b59ea3 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -620,6 +620,12 @@ en:
web_fetch:
name: "Web Fetch (native)"
help: "Lets the model open and read web pages using the provider's built-in web access, when the agent is forced to a supported LLM."
+ google_maps:
+ name: "Google Maps (native)"
+ help: "Lets the model use current Google Maps place and location data, when the agent is forced to a supported Gemini LLM."
+ code_execution:
+ name: "Code execution (native)"
+ help: "Lets the model write and run Python code in the provider's sandbox, when the agent is forced to a supported Gemini LLM."
tool_summary:
read_artifact: "Read a web artifact"
search_uploaded_documents: "Search uploaded documents"
diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rb
index ae3870207e4..d44e5706b9f 100644
--- a/plugins/discourse-ai/lib/agents/bot.rb
+++ b/plugins/discourse-ai/lib/agents/bot.rb
@@ -282,7 +282,7 @@ module DiscourseAi
private
def enable_gemini_thought_summaries!(llm_kwargs, current_llm, context)
- return if current_llm.llm_model.provider != "google"
+ return if !%w[google gemini_interactions].include?(current_llm.llm_model.provider)
return if context.skip_show_thinking != false
extra_model_params = (llm_kwargs[:extra_model_params] || {}).dup
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 7c5c2acfeab..0a1ab9fdc68 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -614,6 +614,7 @@ module DiscourseAi
reply << "<details class='ai-thinking'><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>\n\n"
started_thinking = true
elsif should_stop_thinking?(partial:, context:, type:, started_thinking:, placeholder:)
+ reply << "\n" if !reply.end_with?("\n")
reply << "</details>\n\n"
started_thinking = false
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
index 3285c9b586d..20460fd22e9 100644
--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
@@ -13,6 +13,7 @@ module DiscourseAi
[
DiscourseAi::Completions::Dialects::OpenAiResponses,
DiscourseAi::Completions::Dialects::ChatGpt,
+ DiscourseAi::Completions::Dialects::GeminiInteractions,
DiscourseAi::Completions::Dialects::Gemini,
DiscourseAi::Completions::Dialects::Converse,
DiscourseAi::Completions::Dialects::Claude,
diff --git a/plugins/discourse-ai/lib/completions/dialects/gemini_interactions.rb b/plugins/discourse-ai/lib/completions/dialects/gemini_interactions.rb
new file mode 100644
index 00000000000..18c61e62096
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/dialects/gemini_interactions.rb
@@ -0,0 +1,164 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Completions
+ module Dialects
+ class GeminiInteractions < Dialect
+ PROVIDER_KEY = :gemini_interactions
+
+ class << self
+ def can_translate?(llm_model)
+ llm_model.provider == "gemini_interactions"
+ end
+ end
+
+ def native_tool_support?
+ !llm_model.lookup_custom_param("disable_native_tools")
+ end
+
+ def max_prompt_tokens
+ llm_model.max_prompt_tokens
+ end
+
+ def translate
+ messages = super.flatten
+ system_instruction = nil
+
+ messages.reject! do |message|
+ if message[:type] == "system_instruction"
+ system_instruction = message[:content]
+ true
+ else
+ false
+ end
+ end
+
+ { input: messages, system_instruction: }
+ end
+
+ def tools
+ return if prompt.tools.blank? && prompt.native_tools.blank?
+
+ result = []
+ if prompt.tools.present?
+ result.concat(
+ prompt.tools.map do |tool|
+ declaration = { type: "function", name: tool.name, description: tool.description }
+ declaration[:parameters] = tool.parameters_json_schema if tool.parameters
+ declaration
+ end,
+ )
+ end
+
+ if prompt.native_tool?(DiscourseAi::Completions::NativeTools::WEB_SEARCH)
+ result << { type: "google_search" }
+ end
+ if prompt.native_tool?(DiscourseAi::Completions::NativeTools::WEB_FETCH)
+ result << { type: "url_context" }
+ end
+ if prompt.native_tool?(DiscourseAi::Completions::NativeTools::GOOGLE_MAPS)
+ result << { type: "google_maps" }
+ end
+ if prompt.native_tool?(DiscourseAi::Completions::NativeTools::CODE_EXECUTION)
+ result << { type: "code_execution" }
+ end
+
+ result.presence
+ end
+
+ def strip_upload_markdown_mode
+ llm_model.name.include?("image") ? :all : :model_only
+ end
+
+ protected
+
+ def calculate_message_token(context)
+ llm_model.tokenizer_class.size(context[:content].to_s + context[:name].to_s)
+ end
+
+ private
+
+ def system_msg(msg)
+ content = msg[:content]
+ if !native_tool_support? && tools_dialect.instructions.present?
+ content = content.to_s + "\n\n#{tools_dialect.instructions}"
+ end
+ { type: "system_instruction", content: }
+ end
+
+ def user_msg(msg)
+ input_step("user_input", msg, allow_images: vision_support?)
+ end
+
+ def model_msg(msg)
+ stored_steps = replay_steps(msg)
+ output_step = input_step("model_output", msg, allow_images: vision_support?)
+ stored_steps.present? ? [*stored_steps, output_step] : output_step
+ end
+
+ def tool_call_msg(msg)
+ return super if !native_tool_support?
+
+ stored_steps = replay_steps(msg)
+ return stored_steps if stored_steps.present?
+
+ call_details = JSON.parse(msg[:content], symbolize_names: true)
+ {
+ type: "function_call",
+ id: msg[:id],
+ name: msg[:name] || call_details[:name],
+ arguments: call_details[:arguments] || {},
+ }
+ end
+
+ def tool_msg(msg)
+ return super if !native_tool_support?
+
+ {
+ type: "function_result",
+ call_id: msg[:id],
+ name: msg[:name],
+ result: msg[:content],
+ }.compact
+ end
+
+ def input_step(type, msg, allow_images:)
+ content = []
+ content << "#{msg[:id]}: " if msg[:id]
+ content << msg[:content]
+ content.flatten!
+
+ encoded =
+ to_encoded_content_array(
+ content:,
+ upload_encoder: ->(details) { upload_node(details) },
+ text_encoder: ->(text) { { type: "text", text: } },
+ allow_images:,
+ allow_documents: true,
+ allowed_attachment_types: llm_model.allowed_attachment_types,
+ upload_filter: ->(upload) { document_allowed?(upload) },
+ )
+
+ { type:, content: encoded }
+ end
+
+ def upload_node(details)
+ return { type: "text", text: details[:text] } if details[:text].present?
+
+ type = details[:kind] == :document ? "document" : "image"
+ { type:, mime_type: details[:mime_type], data: details[:base64] }
+ end
+
+ def replay_steps(message)
+ provider_data = message[:provider_data]
+ provider_info = message[:thinking_provider_info]
+ data =
+ provider_data&.deep_symbolize_keys&.dig(PROVIDER_KEY) ||
+ provider_info&.deep_symbolize_keys&.dig(PROVIDER_KEY)
+ steps = data&.dig(:steps)
+ steps&.deep_dup
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rb
index f3b5d1bd057..badce0f8ea1 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb
@@ -58,6 +58,7 @@ module DiscourseAi
DiscourseAi::Completions::Endpoints::OpenAi,
DiscourseAi::Completions::Endpoints::OpenAiResponses,
DiscourseAi::Completions::Endpoints::HuggingFace,
+ DiscourseAi::Completions::Endpoints::GeminiInteractions,
DiscourseAi::Completions::Endpoints::Gemini,
DiscourseAi::Completions::Endpoints::GoogleVertexAi,
DiscourseAi::Completions::Endpoints::Vllm,
diff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb
new file mode 100644
index 00000000000..06cc9c87731
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb
@@ -0,0 +1,316 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Completions
+ module Endpoints
+ class GeminiInteractions < Gemini
+ MINIMAL_HIGH_THINKING_LEVEL_BY_EFFORT =
+ THINKING_LEVEL_BY_EFFORT.merge("low" => "minimal", "medium" => "high").freeze
+
+ def self.can_contact?(llm_model)
+ llm_model.provider == "gemini_interactions"
+ end
+
+ def default_options
+ # Discourse owns conversation history and replays signed steps between requests.
+ { store: false }
+ end
+
+ def normalize_model_params(model_params)
+ model_params = model_params.dup
+ @include_thought_summaries =
+ output_thinking && !!model_params.delete(:include_thought_summaries)
+
+ model_params[:max_output_tokens] = model_params.delete(:max_tokens) if model_params[
+ :max_tokens
+ ]
+ if thinking_configured? || llm_model.lookup_custom_param("disable_temperature")
+ model_params.delete(:temperature)
+ end
+ model_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")
+ model_params
+ end
+
+ def resolve_thinking_config(model_params)
+ effort =
+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(
+ model_params[:thinking_effort],
+ )
+ effort = legacy_thinking_level if effort.blank?
+ return DiscourseAi::Completions::ThinkingConfig.disabled if effort.blank?
+ return DiscourseAi::Completions::ThinkingConfig.explicit_none if effort == "none"
+
+ provider_effort = thinking_level_for_interactions(effort)
+ if provider_effort.blank?
+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)
+ end
+
+ DiscourseAi::Completions::ThinkingConfig.new(
+ canonical_effort: effort,
+ provider_effort:,
+ enabled: true,
+ strip_temperature: true,
+ )
+ end
+
+ private
+
+ def model_uri
+ URI(llm_model.url)
+ end
+
+ def prepare_payload(prompt, model_params, dialect)
+ @native_tool_support = dialect.native_tool_support?
+ payload = default_options.merge(model: llm_model.name, input: prompt[:input])
+ payload[:system_instruction] = prompt[:system_instruction] if prompt[
+ :system_instruction
+ ].present?
+ payload[:stream] = true if @streaming_mode
+
+ tools = dialect.tools if @native_tool_support
+ payload[:tools] = tools if tools.present?
+
+ generation_config = model_params.except(:response_format)
+ apply_thinking_config!(generation_config)
+ generation_config[:thinking_summaries] = "auto" if @include_thought_summaries
+ has_function_tools = tools&.any? { |tool| tool[:type] == "function" }
+ apply_tool_choice!(generation_config, dialect, tools:) if has_function_tools
+ payload[:generation_config] = generation_config if generation_config.present?
+
+ response_format = model_params[:response_format]
+ schema = response_format&.dig(:json_schema, :schema)
+ if schema.present?
+ payload[:response_format] = { type: "text", mime_type: "application/json", schema: }
+ elsif response_format.present?
+ payload[:response_format] = response_format
+ end
+
+ payload[:service_tier] = service_tier if service_tier.present?
+ payload
+ end
+
+ def apply_thinking_config!(generation_config)
+ return if thinking_config.blank? || thinking_config.unsupported?
+
+ if thinking_config.explicit_none?
+ # Interactions has no disabled thinking level for Gemini 2.5; omission preserves the
+ # model default, including disabled thinking for Flash Lite.
+ return if gemini_2_5_model?
+
+ generation_config[:thinking_level] = thinking_level_for_interactions("minimal")
+ elsif thinking_config.provider_effort.present?
+ generation_config[:thinking_level] = thinking_config.provider_effort
+ end
+ end
+
+ def apply_tool_choice!(generation_config, dialect, tools:)
+ generation_config[:tool_choice] = case dialect.tool_choice
+ when :none
+ "none"
+ when nil
+ # Mixed server-side and function tools reject auto circulation.
+ tools.any? { |tool| tool[:type] != "function" } ? "validated" : "auto"
+ else
+ { allowed_tools: { mode: "any", tools: [dialect.tool_choice] } }
+ end
+ end
+
+ def thinking_level_for_interactions(effort)
+ return if !THINKING_LEVEL_BY_EFFORT.key?(effort)
+
+ if gemini_3_1_flash_lite_image_model?
+ MINIMAL_HIGH_THINKING_LEVEL_BY_EFFORT[effort]
+ elsif gemini_model_id.include?("image") || gemini_3_pro_preview_model?
+ LOW_HIGH_THINKING_LEVEL_BY_EFFORT[effort]
+ elsif gemini_2_5_model? || (gemini_3_model? && !supports_interactions_minimal_thinking?)
+ THINKING_LEVEL_WITHOUT_MINIMAL_BY_EFFORT[effort]
+ else
+ THINKING_LEVEL_BY_EFFORT[effort]
+ end
+ end
+
+ def gemini_3_1_flash_lite_image_model?
+ gemini_model_id.include?("gemini-3.1-flash-lite-image")
+ end
+
+ def gemini_2_5_model?
+ gemini_model_id.include?("gemini-2.5")
+ end
+
+ def supports_interactions_minimal_thinking?
+ gemini_model_id.match?(/gemini-3(?:\.\d+)?-flash(?:\s|\z|-)/) ||
+ gemini_model_id.include?("gemini-3.1-flash-lite")
+ end
+
+ def legacy_thinking_level
+ level = llm_model.lookup_custom_param("thinking_level")
+ level if THINKING_LEVELS.include?(level)
+ end
+
+ def prepare_request(payload)
+ if @streaming_mode
+ @processor = nil
+ @streaming_decoder = nil
+ @pending_stream_error = nil
+ end
+
+ headers = { "Content-Type" => "application/json", "x-goog-api-key" => llm_model.api_key }
+ headers.merge!(extra_request_headers)
+ Net::HTTP::Post.new(model_uri, headers).tap { |request| request.body = payload }
+ end
+
+ def decode(response_raw)
+ payload = JSON.parse(response_raw, symbolize_names: true)
+ raise_interaction_error!(payload) if %w[failed cancelled].include?(payload[:status])
+ processor.process_message(payload)
+ end
+
+ def decode_chunk(chunk)
+ raise_pending_stream_error!
+
+ result = []
+ streaming_decoder
+ .decode(chunk)
+ .each do |event|
+ result.concat(Array(process_stream_event(event)))
+ rescue CompletionFailed => error
+ raise if result.blank?
+
+ @pending_stream_error = error
+ break
+ end
+ result.compact
+ end
+
+ def decode_chunk_finish
+ raise_pending_stream_error!
+
+ events = streaming_decoder.finish.flat_map { |event| process_stream_event(event) }
+ events.concat(processor.finish).compact
+ end
+
+ def process_stream_event(event)
+ case event[:event_type]
+ when "error"
+ raise_interaction_error!(event)
+ when "interaction.status_update"
+ raise_interaction_error!(event) if %w[failed cancelled].include?(event[:status])
+ when "interaction.completed"
+ interaction = event[:interaction] || {}
+ if %w[failed cancelled].include?(interaction[:status])
+ raise_interaction_error!(interaction)
+ end
+ end
+
+ processor.process_streamed_message(event)
+ end
+
+ def raise_pending_stream_error!
+ return if @pending_stream_error.blank?
+
+ error = @pending_stream_error
+ @pending_stream_error = nil
+ raise error
+ end
+
+ def raise_interaction_error!(payload)
+ error = payload[:error] || payload.dig(:interaction, :error)
+ message = error&.dig(:message) || "Gemini interaction #{payload[:status] || "failed"}"
+ raise CompletionFailed, message
+ end
+
+ def processor
+ @processor ||=
+ DiscourseAi::Completions::GeminiInteractionsMessageProcessor.new(
+ partial_tool_calls:,
+ output_thinking:,
+ ) { |content| decode_content(content) }
+ end
+
+ def decode_content(content)
+ case content[:type]
+ when "text"
+ content[:text]
+ when "image"
+ inline_data_to_upload_markdown(mimeType: content[:mime_type], data: content[:data])
+ end
+ end
+
+ def final_log_update(log)
+ log.request_tokens = processor.prompt_tokens if processor.prompt_tokens
+ log.response_tokens = processor.completion_tokens if processor.completion_tokens
+ log.cache_read_tokens = processor.cache_read_tokens if processor.cache_read_tokens
+ end
+
+ def extract_prompt_for_tokenizer(prompt)
+ prompt.deep_dup.tap { |value| remove_inline_data!(value) }.to_json
+ end
+
+ def remove_inline_data!(value)
+ case value
+ when Array
+ value.each { |item| remove_inline_data!(item) }
+ when Hash
+ value.each do |key, item|
+ if key.to_s == "data"
+ value[key] = ""
+ else
+ remove_inline_data!(item)
+ end
+ end
+ end
+ end
+
+ def prompt_has_images?(translated_prompt)
+ Array(translated_prompt[:input]).any? do |step|
+ Array(step[:content]).any? { |content| content[:type] == "image" }
+ end
+ rescue StandardError
+ false
+ end
+
+ class StreamingDecoder
+ def initialize
+ @buffer = +""
+ end
+
+ def decode(chunk)
+ @buffer << chunk
+ records = @buffer.split(/\r?\n\r?\n/, -1)
+ @buffer = records.pop || +""
+ parse_records(records)
+ end
+
+ def finish
+ record = @buffer
+ @buffer = +""
+ parse_records([record])
+ end
+
+ private
+
+ def parse_records(records)
+ records.filter_map do |record|
+ data =
+ record
+ .lines
+ .filter_map do |line|
+ line.delete_prefix("data:").strip if line.start_with?("data:")
+ end
+ .join("\n")
+ next if data.blank? || data == "[DONE]"
+
+ JSON.parse(data, symbolize_names: true)
+ rescue JSON::ParserError
+ nil
+ end
+ end
+ end
+
+ def streaming_decoder
+ @streaming_decoder ||= StreamingDecoder.new
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/gemini_interactions_message_processor.rb b/plugins/discourse-ai/lib/completions/gemini_interactions_message_processor.rb
new file mode 100644
index 00000000000..bf700a6a8fd
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/gemini_interactions_message_processor.rb
@@ -0,0 +1,393 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Completions
+ class GeminiInteractionsMessageProcessor
+ PROVIDER_KEY = :gemini_interactions
+ NATIVE_CALL_TYPES = %w[
+ code_execution_call
+ file_search_call
+ google_maps_call
+ google_search_call
+ url_context_call
+ ].freeze
+ NATIVE_RESULT_TYPES = %w[
+ code_execution_result
+ file_search_result
+ google_maps_result
+ google_search_result
+ url_context_result
+ ].freeze
+
+ attr_reader :prompt_tokens, :completion_tokens, :cache_read_tokens
+
+ def initialize(partial_tool_calls: false, output_thinking: false, &content_decoder)
+ @partial_tool_calls = partial_tool_calls
+ @output_thinking = output_thinking
+ @content_decoder = content_decoder
+ @pending_steps = []
+ @current_step = nil
+ @tool_arguments = +""
+ @streaming_parser = nil
+ @has_new_tool_data = false
+ @emitted_google_maps_citations = Set.new
+ end
+
+ def process_message(payload)
+ payload = normalize(payload)
+ result = []
+
+ Array(payload[:steps]).each { |step| result.concat(process_complete_step(step)) }
+ update_usage(payload[:usage])
+ result.concat(flush_pending_steps)
+ result.compact
+ end
+
+ def process_streamed_message(event)
+ event = normalize(event)
+ result = []
+
+ case event[:event_type]
+ when "step.start"
+ result.concat(start_step(event[:step]))
+ when "step.delta"
+ result.concat(process_delta(event[:delta]))
+ when "step.stop"
+ result.concat(finish_current_step(event[:step]))
+ end
+
+ update_usage(event.dig(:interaction, :usage) || event.dig(:metadata, :total_usage))
+ result.compact
+ end
+
+ def notify_progress(key, value)
+ return if !@current_tool
+
+ @current_tool.partial = true
+ @current_tool.parameters[key.to_sym] = value
+ @has_new_tool_data = true
+ end
+
+ def finish
+ result = finish_current_step
+ result.concat(flush_pending_steps)
+ result.compact
+ end
+
+ private
+
+ def process_complete_step(step)
+ step = normalize(step)
+ type = step[:type]
+
+ case type
+ when "thought"
+ @pending_steps << step.deep_dup
+ thought_outputs(step, partial: false)
+ when "function_call"
+ [tool_call_from_step(step, replay_steps: consume_pending_steps + [step.deep_dup])]
+ when "model_output"
+ decode_model_output(step[:content])
+ when *NATIVE_CALL_TYPES, *NATIVE_RESULT_TYPES
+ @pending_steps << step.deep_dup
+ native_tool_outputs(step)
+ else
+ @pending_steps << step.deep_dup if type.present?
+ []
+ end
+ end
+
+ def start_step(step)
+ @current_step = normalize(step || {}).deep_dup
+ @current_step[:content] = [] if @current_step[:type] == "model_output"
+
+ return [] if @current_step[:type] != "function_call"
+
+ initial_arguments = @current_step[:arguments]
+ @tool_arguments =
+ if initial_arguments.is_a?(String)
+ +initial_arguments
+ elsif initial_arguments.present?
+ +initial_arguments.to_json
+ else
+ +""
+ end
+ @current_tool =
+ ToolCall.new(id: @current_step[:id], name: @current_step[:name], parameters: {})
+ @streaming_parser = JsonStreamingTracker.new(self) if @partial_tool_calls
+ @streaming_parser << @tool_arguments if @streaming_parser && @tool_arguments.present?
+ current_tool_progress
+ end
+
+ def process_delta(delta)
+ delta = normalize(delta || {})
+ type = delta[:type]
+
+ case type
+ when "text"
+ append_content_delta(delta)
+ [delta[:text].presence, *google_maps_attribution_outputs([delta])]
+ when "image", "audio", "video", "document"
+ append_content_delta(delta)
+ decode_content([delta])
+ when "thought_signature"
+ @current_step[:signature] = delta[:signature] if @current_step
+ []
+ when "thought_summary"
+ append_thought_summary(delta[:content])
+ thought_summary_delta(delta[:content])
+ when "arguments", "arguments_delta"
+ process_arguments_delta(delta[:arguments] || delta[:partial_arguments])
+ when *NATIVE_CALL_TYPES, *NATIVE_RESULT_TYPES
+ @current_step&.deep_merge!(delta)
+ []
+ else
+ []
+ end
+ end
+
+ def append_content_delta(delta)
+ return if !@current_step
+
+ content = delta.except(:type).merge(type: delta[:type])
+ @current_step[:content] ||= []
+ if content[:type] == "text" && @current_step[:content].last&.dig(:type) == "text"
+ @current_step[:content].last[:text] = @current_step[:content].last[:text].to_s +
+ content[:text].to_s
+ else
+ @current_step[:content] << content
+ end
+ end
+
+ def append_thought_summary(content)
+ return if !@current_step || content.blank?
+
+ @current_step[:summary] ||= []
+ normalized_content = normalize(content)
+ if thought_summary_text?(normalized_content) &&
+ thought_summary_text?(@current_step[:summary].last)
+ @current_step[:summary].last[:text] = @current_step[:summary].last[:text].to_s +
+ normalized_content[:text].to_s
+ else
+ @current_step[:summary] << normalized_content
+ end
+ end
+
+ def thought_summary_delta(content)
+ return [] if !@output_thinking
+
+ text = normalize(content || {})[:text]
+ text.present? ? [Thinking.new(message: text, partial: true)] : []
+ end
+
+ def process_arguments_delta(arguments)
+ return [] if arguments.nil?
+
+ fragment = arguments.is_a?(String) ? arguments : arguments.to_json
+ @tool_arguments << fragment
+ @streaming_parser << fragment if @streaming_parser
+ current_tool_progress
+ end
+
+ def current_tool_progress
+ return [] if !@has_new_tool_data
+
+ @has_new_tool_data = false
+ [@current_tool.dup]
+ end
+
+ def finish_current_step(final_step = nil)
+ return [] if @current_step.blank? && final_step.blank?
+
+ step = @current_step || {}
+ step = step.deep_merge(normalize(final_step)) if final_step.present?
+ type = step[:type]
+ result = []
+
+ case type
+ when "thought"
+ @pending_steps << step.deep_dup
+ result.concat(thought_outputs(step, partial: false))
+ when "function_call"
+ step[:arguments] = parsed_tool_arguments(step)
+ result << tool_call_from_step(step, replay_steps: consume_pending_steps + [step.deep_dup])
+ when "model_output"
+ # Text is emitted by deltas; annotations can arrive only with the completed step.
+ result.concat(google_maps_attribution_outputs(step[:content]))
+ when *NATIVE_CALL_TYPES, *NATIVE_RESULT_TYPES
+ @pending_steps << step.deep_dup
+ result.concat(native_tool_outputs(step))
+ else
+ @pending_steps << step.deep_dup if type.present?
+ end
+
+ reset_current_step
+ result.compact
+ end
+
+ def parsed_tool_arguments(step)
+ return step[:arguments] || {} if @tool_arguments.blank?
+
+ ToolArgumentsParser.parse(@tool_arguments)
+ rescue JSON::ParserError
+ {}
+ end
+
+ def reset_current_step
+ @current_step = nil
+ @current_tool = nil
+ @tool_arguments = +""
+ @streaming_parser = nil
+ @has_new_tool_data = false
+ end
+
+ def tool_call_from_step(step, replay_steps:)
+ ToolCall.new(
+ id: step[:id],
+ name: step[:name],
+ parameters: normalize(step[:arguments] || {}),
+ provider_data: {
+ PROVIDER_KEY => {
+ steps: replay_steps,
+ },
+ },
+ )
+ end
+
+ def thought_outputs(step, partial:)
+ return [] if !@output_thinking
+
+ message =
+ Array(step[:summary])
+ .filter_map do |content|
+ content = normalize(content)
+ if thought_summary_text?(content)
+ content[:text]
+ else
+ @content_decoder.call(content)&.strip
+ end
+ end
+ .join("\n\n")
+ message.present? ? [Thinking.new(message:, partial:)] : []
+ end
+
+ def thought_summary_text?(content)
+ content.present? && (content[:type].blank? || content[:type] == "text")
+ end
+
+ def native_tool_outputs(step)
+ return [] if !@output_thinking || NATIVE_CALL_TYPES.exclude?(step[:type])
+
+ message =
+ case step[:type]
+ when "google_search_call"
+ queries = Array(step.dig(:arguments, :queries)).compact_blank
+ queries.present? ? "Web search: #{queries.join(", ")}" : "Web search"
+ when "url_context_call"
+ urls = Array(step.dig(:arguments, :urls)).compact_blank
+ urls.present? ? "Web fetch: #{urls.join(", ")}" : "Web fetch"
+ when "code_execution_call"
+ code_execution_message(step)
+ when "file_search_call"
+ "File search"
+ when "google_maps_call"
+ "Google Maps search"
+ end
+
+ [Thinking.new(message:, partial: false)]
+ end
+
+ def decode_model_output(content)
+ outputs = decode_content(content)
+ attribution = google_maps_attribution_outputs(content).join
+ return outputs if attribution.blank?
+
+ last_text_index = outputs.rindex { |output| output.is_a?(String) }
+ if last_text_index
+ outputs[last_text_index] += attribution
+ else
+ outputs << attribution
+ end
+ outputs
+ end
+
+ def code_execution_message(step)
+ code = step.dig(:arguments, :code).to_s
+ return "Code execution" if code.blank?
+
+ language = step.dig(:arguments, :language).to_s.downcase
+ language = "" if !language.match?(/\A[a-z0-9_+-]+\z/)
+ longest_backtick_run = code.scan(/`+/).map(&:length).max.to_i
+ fence = "`" * [3, longest_backtick_run + 1].max
+
+ "Code execution:\n\n#{fence}#{language}\n#{code.rstrip}\n#{fence}"
+ end
+
+ def google_maps_attribution_outputs(content)
+ links =
+ Array(content).flat_map do |item|
+ item = normalize(item)
+ Array(item[:annotations]).filter_map do |annotation|
+ annotation = normalize(annotation)
+ next if annotation[:type] != "place_citation"
+
+ name = annotation[:name].to_s
+ url = annotation[:url].to_s
+ next if name.blank? || !url.start_with?("https://")
+
+ citation_key = [name, url]
+ next if @emitted_google_maps_citations.include?(citation_key)
+
+ @emitted_google_maps_citations << citation_key
+ "[#{escape_markdown_link_text(name)}](#{url})"
+ end
+ end
+
+ links.present? ? ["\n\nGoogle Maps: #{links.join(", ")}"] : []
+ end
+
+ def escape_markdown_link_text(text)
+ text.gsub(/[\\\[\]]/) { |character| "\\#{character}" }
+ end
+
+ def decode_content(content)
+ Array(content).filter_map { |item| @content_decoder.call(normalize(item)) }
+ end
+
+ def context_thinking(steps)
+ return if steps.blank?
+
+ # Stateless continuation requires signed steps even when thinking is hidden.
+ Thinking.new(message: nil, partial: false, provider_info: { PROVIDER_KEY => { steps: } })
+ end
+
+ def flush_pending_steps
+ steps = consume_pending_steps
+ steps.present? ? [context_thinking(steps)] : []
+ end
+
+ def consume_pending_steps
+ steps = @pending_steps
+ @pending_steps = []
+ steps
+ end
+
+ def update_usage(usage)
+ return if usage.blank?
+
+ usage = normalize(usage)
+ cached = usage[:total_cached_tokens].to_i
+ @prompt_tokens = [usage[:total_input_tokens].to_i - cached, 0].max
+ @cache_read_tokens = cached
+ # Gemini reports generated, thought, and tool-use output separately.
+ @completion_tokens =
+ usage[:total_output_tokens].to_i + usage[:total_thought_tokens].to_i +
+ usage[:total_tool_use_tokens].to_i
+ end
+
+ def normalize(value)
+ value.respond_to?(:deep_symbolize_keys) ? value.deep_symbolize_keys : value
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rb
index 0ce833d1673..f551a0a80a3 100644
--- a/plugins/discourse-ai/lib/completions/llm.rb
+++ b/plugins/discourse-ai/lib/completions/llm.rb
@@ -32,6 +32,7 @@ module DiscourseAi
cohere
open_ai
google
+ gemini_interactions
google_vertex_ai
azure
samba_nova
diff --git a/plugins/discourse-ai/lib/completions/llm_presets.rb b/plugins/discourse-ai/lib/completions/llm_presets.rb
index 15806bfd483..f671fa1921a 100644
--- a/plugins/discourse-ai/lib/completions/llm_presets.rb
+++ b/plugins/discourse-ai/lib/completions/llm_presets.rb
@@ -120,32 +120,30 @@ module DiscourseAi
id: "google",
models: [
model(
- name: "gemini-3.1-pro",
+ name: "gemini-3.1-pro-preview",
tokens: 1_000_000,
- display_name: "Gemini 3.1 Pro",
+ display_name: "Gemini 3.1 Pro Preview",
max_output_tokens: 65_000,
input_cost: 2.0,
cached_input_cost: 0.20,
output_cost: 12.0,
vision_enabled: true,
- endpoint:
- "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview",
+ endpoint: "https://generativelanguage.googleapis.com/v1beta/interactions",
),
model(
- name: "gemini-3-flash",
- tokens: 1_000_000,
- display_name: "Gemini 3 Flash",
- max_output_tokens: 65_000,
- input_cost: 0.50,
- cached_input_cost: 0.05,
- output_cost: 3.0,
+ name: "gemini-3.6-flash",
+ tokens: 1_048_576,
+ display_name: "Gemini 3.6 Flash",
+ max_output_tokens: 65_536,
+ input_cost: 1.50,
+ cached_input_cost: 0.15,
+ output_cost: 7.50,
vision_enabled: true,
- endpoint:
- "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",
),
],
tokenizer: DiscourseAi::Tokenizer::GeminiTokenizer,
- provider: "google",
+ endpoint: "https://generativelanguage.googleapis.com/v1/interactions",
+ provider: "gemini_interactions",
}
end
diff --git a/plugins/discourse-ai/lib/completions/native_tools.rb b/plugins/discourse-ai/lib/completions/native_tools.rb
index fa6ad99ae57..09ca731253d 100644
--- a/plugins/discourse-ai/lib/completions/native_tools.rb
+++ b/plugins/discourse-ai/lib/completions/native_tools.rb
@@ -15,6 +15,8 @@ module DiscourseAi
WEB_SEARCH = "web_search"
WEB_FETCH = "web_fetch"
+ GOOGLE_MAPS = "google_maps"
+ CODE_EXECUTION = "code_execution"
# open_ai/azure only expose web search through the Responses API
RESPONSES_API_PROVIDERS = %w[open_ai azure].freeze
@@ -50,8 +52,13 @@ module DiscourseAi
end
DEFINITIONS = [
- Definition.new(id: WEB_SEARCH, providers: %w[google anthropic open_ai azure]),
- Definition.new(id: WEB_FETCH, providers: %w[google anthropic]),
+ Definition.new(
+ id: WEB_SEARCH,
+ providers: %w[google gemini_interactions anthropic open_ai azure],
+ ),
+ Definition.new(id: WEB_FETCH, providers: %w[google gemini_interactions anthropic]),
+ Definition.new(id: GOOGLE_MAPS, providers: %w[gemini_interactions]),
+ Definition.new(id: CODE_EXECUTION, providers: %w[gemini_interactions]),
].freeze
def self.all
diff --git a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
index 5ced9238051..2a56f441ca0 100644
--- a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
+++ b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
@@ -171,6 +171,17 @@ module DiscourseAi
guardian = Guardian.new(post.user)
context.reverse_each do |raw, username, custom_prompt, upload_ids, created_at|
+ filtered_upload_ids =
+ filtered_upload_ids_for_prompt(
+ upload_ids,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ guardian: guardian,
+ )
+ remaining_upload_ids = Array(filtered_upload_ids).dup
+ uploads_by_id = Upload.where(id: remaining_upload_ids).index_by(&:id)
+
custom_prompt_translation =
Proc.new do |message|
# We can't keep backwards-compatibility for stored functions.
@@ -190,6 +201,19 @@ module DiscourseAi
custom_context[:provider_data] = provider_data if provider_data.is_a?(Hash)
custom_context[:created_at] = created_at
+ if custom_context[:type] == :model && remaining_upload_ids.present?
+ content_text = message_text(custom_context[:content])
+ referenced_upload_ids =
+ remaining_upload_ids.select do |upload_id|
+ upload = uploads_by_id[upload_id]
+ upload && content_text.include?(upload.short_url)
+ end
+ if referenced_upload_ids.present?
+ custom_context[:upload_ids] = referenced_upload_ids
+ remaining_upload_ids -= referenced_upload_ids
+ end
+ end
+
builder.push(**custom_context)
end
end
@@ -201,13 +225,7 @@ module DiscourseAi
context[:id] = username if context[:type] == :user
- context[:upload_ids] = filtered_upload_ids_for_prompt(
- upload_ids,
- include_image_uploads: include_image_uploads,
- include_document_uploads: include_document_uploads,
- allowed_attachment_types: allowed_attachment_types,
- guardian: guardian,
- )
+ context[:upload_ids] = filtered_upload_ids
context[:created_at] = created_at
builder.push(**context)
diff --git a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
index a7d377594b0..baaaca428db 100644
--- a/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
+++ b/plugins/discourse-ai/spec/fabricators/llm_model_fabricator.rb
@@ -59,6 +59,15 @@ Fabricator(:fake_model, from: :llm_model) do
url "https://fake.test/"
end
+Fabricator(:gemini_interactions_model, from: :llm_model) do
+ display_name "Gemini Interactions"
+ name "gemini-2.5-flash"
+ provider "gemini_interactions"
+ tokenizer "DiscourseAi::Tokenizer::GeminiTokenizer"
+ max_prompt_tokens 1_000_000
+ url "https://generativelanguage.googleapis.com/v1/interactions"
+end
+
Fabricator(:gemini_model, from: :llm_model) do
display_name "Gemini"
name "gemini-1.5-pro"
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_interactions_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_interactions_spec.rb
new file mode 100644
index 00000000000..4bba9264b84
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_interactions_spec.rb
@@ -0,0 +1,1283 @@
+# frozen_string_literal: true
+
+require_relative "endpoint_compliance"
+
+RSpec.describe DiscourseAi::Completions::Endpoints::GeminiInteractions do
+ subject(:endpoint) { described_class.new(model) }
+
+ fab!(:model) { Fabricate(:gemini_interactions_model, vision_enabled: true) }
+ fab!(:user)
+
+ let(:url) { "https://generativelanguage.googleapis.com/v1/interactions" }
+ let(:llm) { DiscourseAi::Completions::Llm.proxy(model) }
+ let(:echo_tool) do
+ {
+ name: "echo",
+ description: "Echo text",
+ parameters: [{ name: "text", type: "string", description: "Text to echo", required: true }],
+ }
+ end
+
+ before { enable_current_plugin }
+
+ def interaction_response(steps:, usage: nil, status: "completed")
+ {
+ object: "interaction",
+ model: model.name,
+ status:,
+ steps:,
+ usage:
+ usage ||
+ {
+ total_input_tokens: 10,
+ total_cached_tokens: 0,
+ total_output_tokens: 4,
+ total_thought_tokens: 0,
+ total_tool_use_tokens: 0,
+ total_tokens: 14,
+ },
+ }
+ end
+
+ def sse_event(event_type, **fields)
+ "event: #{event_type}\ndata: #{fields.merge(event_type:).to_json}\n\n"
+ end
+
+ it "registers as a separate provider with the Interactions dialect and endpoint" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
+
+ expect(DiscourseAi::Completions::Llm.provider_names).to include("gemini_interactions")
+ expect(DiscourseAi::Completions::Endpoints::Base.endpoint_for(model)).to eq(described_class)
+ expect(DiscourseAi::Completions::Dialects::Dialect.dialect_for(model)).to eq(
+ DiscourseAi::Completions::Dialects::GeminiInteractions,
+ )
+ expect(LlmModel.provider_params[:gemini_interactions].keys).to contain_exactly(
+ :disable_native_tools,
+ :thinking_level,
+ :disable_temperature,
+ :disable_top_p,
+ :service_tier,
+ )
+ expect(LlmModel.provider_params.dig(:gemini_interactions, :thinking_level, :label)).to eq(
+ "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ )
+ expect(LlmModel.provider_params.dig(:google, :thinking_level)).not_to have_key(:label)
+ end
+
+ it "generates text using stateless interactions and records provider usage" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
+ request_body = nil
+ response =
+ interaction_response(
+ steps: [
+ { type: "thought", signature: "signed-thought" },
+ { type: "model_output", content: [{ type: "text", text: "Hello world" }] },
+ ],
+ usage: {
+ total_input_tokens: 120,
+ total_cached_tokens: 20,
+ total_output_tokens: 7,
+ total_thought_tokens: 3,
+ total_tool_use_tokens: 2,
+ total_tokens: 132,
+ },
+ )
+
+ request =
+ stub_request(:post, url).with(
+ headers: {
+ "Content-Type" => "application/json",
+ "X-Goog-Api-Key" => "123",
+ },
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(status: 200, body: response.to_json)
+
+ result =
+ llm.generate(
+ DiscourseAi::Completions::Prompt.new(
+ "Be concise",
+ messages: [{ type: :user, content: "Say hello" }],
+ ),
+ user:,
+ temperature: 0.4,
+ top_p: 0.8,
+ max_tokens: 100,
+ stop_sequences: ["STOP"],
+ )
+
+ expect(result).to eq(
+ [
+ "Hello world",
+ DiscourseAi::Completions::Thinking.new(
+ message: nil,
+ provider_info: {
+ gemini_interactions: {
+ steps: [{ type: "thought", signature: "signed-thought" }],
+ },
+ },
+ ),
+ ],
+ )
+ expect(request).to have_been_requested.once
+ expect(request_body).to eq(
+ {
+ store: false,
+ model: "gemini-2.5-flash",
+ input: [{ type: "user_input", content: [{ type: "text", text: "Say hello" }] }],
+ system_instruction: "Be concise",
+ generation_config: {
+ temperature: 0.4,
+ top_p: 0.8,
+ max_output_tokens: 100,
+ stop_sequences: ["STOP"],
+ },
+ },
+ )
+
+ log = AiApiAuditLog.last
+ expect(log.provider_id).to eq(AiApiAuditLog::Provider::Gemini)
+ expect(log.request_tokens).to eq(100)
+ expect(log.cache_read_tokens).to eq(20)
+ expect(log.response_tokens).to eq(12)
+ end
+
+ it "maps Gemini 2.5 thinking efforts to supported Interactions API levels" do
+ request_bodies = []
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_bodies << JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ %w[minimal low medium high].each do |thinking_effort|
+ expect(llm.generate("Think", user:, thinking_effort:)).to eq("Done")
+ end
+
+ expect(request_bodies.map { |body| body.dig(:generation_config, :thinking_level) }).to eq(
+ %w[low low medium high],
+ )
+ end
+
+ it "omits the unsupported disabled thinking override for Gemini 2.5" do
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ expect(llm.generate("Think", user:, thinking_effort: "none")).to eq("Done")
+ expect(request_body).not_to have_key(:generation_config)
+ end
+
+ it "requests summaries when Gemini 2.5 cannot honor disabled thinking" do
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ expect(
+ llm.generate(
+ "Think",
+ user:,
+ thinking_effort: "none",
+ output_thinking: true,
+ extra_model_params: {
+ include_thought_summaries: true,
+ },
+ ),
+ ).to eq("Done")
+ expect(request_body[:generation_config]).to eq(thinking_summaries: "auto")
+ end
+
+ it "maps the provider-level minimal setting to low for Gemini 2.5" do
+ model.update!(provider_params: { thinking_level: "minimal" })
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ expect(llm.generate("Think", user:)).to eq("Done")
+ expect(request_body.dig(:generation_config, :thinking_level)).to eq("low")
+ end
+
+ it "maps thinking effort to the levels supported by Flash Lite image models" do
+ model.update!(name: "gemini-3.1-flash-lite-image")
+ request_bodies = []
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_bodies << JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ %w[minimal low medium high].each do |thinking_effort|
+ expect(llm.generate("Draw", user:, thinking_effort:)).to eq("Done")
+ end
+
+ expect(request_bodies.map { |body| body.dig(:generation_config, :thinking_level) }).to eq(
+ %w[minimal minimal high high],
+ )
+ end
+
+ it "streams text and thought summaries from fragmented SSE records" do
+ model.update!(provider_params: { thinking_level: "low" })
+ stream = +""
+ stream << sse_event("interaction.created", interaction: { status: "in_progress" })
+ stream << sse_event("step.start", index: 0, step: { type: "thought" })
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "thought_summary",
+ content: {
+ text: "Checking",
+ },
+ },
+ )
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "thought_signature",
+ signature: "sig-1",
+ },
+ )
+ stream << sse_event("step.stop", index: 0)
+ stream << sse_event("step.start", index: 1, step: { type: "model_output" })
+ stream << sse_event("step.delta", index: 1, delta: { type: "text", text: "Hel" })
+ stream << sse_event("step.delta", index: 1, delta: { type: "text", text: "lo" })
+ stream << sse_event("step.stop", index: 1)
+ stream << sse_event(
+ "interaction.completed",
+ interaction: {
+ status: "completed",
+ usage: {
+ total_input_tokens: 8,
+ total_cached_tokens: 0,
+ total_output_tokens: 2,
+ total_thought_tokens: 1,
+ total_tool_use_tokens: 0,
+ },
+ },
+ )
+ stream << "event: done\ndata: [DONE]\n\n"
+
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(status: 200, body: stream.chars.each_slice(17).map(&:join))
+
+ partials = []
+ EndpointMock.with_chunk_array_support do
+ result =
+ llm.generate(
+ "Hello",
+ user:,
+ output_thinking: true,
+ extra_model_params: {
+ include_thought_summaries: true,
+ },
+ ) { |partial| partials << partial }
+ expect(result).to eq("Hello")
+ end
+
+ expect(request_body[:stream]).to eq(true)
+ expect(request_body[:generation_config]).to eq(
+ { thinking_level: "low", thinking_summaries: "auto" },
+ )
+ expect(
+ partials.map do |partial|
+ if partial.is_a?(DiscourseAi::Completions::Thinking)
+ [partial.message, partial.partial?, partial.provider_info]
+ else
+ partial
+ end
+ end,
+ ).to eq(
+ [
+ ["Checking", true, {}],
+ ["Checking", false, {}],
+ "Hel",
+ "lo",
+ [
+ nil,
+ false,
+ {
+ gemini_interactions: {
+ steps: [{ type: "thought", summary: [{ text: "Checking" }], signature: "sig-1" }],
+ },
+ },
+ ],
+ ],
+ )
+ expect(AiApiAuditLog.last.response_tokens).to eq(3)
+ end
+
+ it "renders and preserves non-streamed text and image thought summaries" do
+ image_data =
+ Base64.strict_encode64(
+ File.binread(File.expand_path("../../../fixtures/images/100x100.jpg", __dir__)),
+ )
+ thought_step = {
+ type: "thought",
+ signature: "signed-image-thought",
+ summary: [
+ { type: "text", text: "Visualizing" },
+ { type: "image", mime_type: "image/jpeg", data: image_data },
+ ],
+ }
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [
+ thought_step,
+ { type: "model_output", content: [{ type: "text", text: "Done" }] },
+ ],
+ ).to_json,
+ )
+
+ result = nil
+ expect do
+ result =
+ llm.generate(
+ "Think visually",
+ user:,
+ output_thinking: true,
+ extra_model_params: {
+ include_thought_summaries: true,
+ },
+ )
+ end.to change(Upload, :count).by(1)
+
+ expect(request_body.dig(:generation_config, :thinking_summaries)).to eq("auto")
+ expect(result.first.message).to start_with("Visualizing\n\n![image](upload://")
+ expect(result.last.provider_info.dig(:gemini_interactions, :steps)).to eq([thought_step])
+ end
+
+ it "renders streamed image thought summaries and preserves their signature" do
+ image_data =
+ Base64.strict_encode64(
+ File.binread(File.expand_path("../../../fixtures/images/100x100.jpg", __dir__)),
+ )
+ stream = +""
+ stream << sse_event("step.start", index: 0, step: { type: "thought" })
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "thought_summary",
+ content: {
+ type: "image",
+ mime_type: "image/jpeg",
+ data: image_data,
+ },
+ },
+ )
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "thought_signature",
+ signature: "streamed-image-signature",
+ },
+ )
+ stream << sse_event("step.stop", index: 0)
+ stream << sse_event("step.start", index: 1, step: { type: "model_output" })
+ stream << sse_event("step.delta", index: 1, delta: { type: "text", text: "Done" })
+ stream << sse_event("step.stop", index: 1)
+ stream << sse_event(
+ "interaction.completed",
+ interaction: {
+ status: "completed",
+ usage: {
+ total_input_tokens: 1,
+ total_cached_tokens: 0,
+ total_output_tokens: 1,
+ total_thought_tokens: 1,
+ total_tool_use_tokens: 0,
+ },
+ },
+ )
+ stub_request(:post, url).to_return(status: 200, body: stream)
+ partials = []
+
+ expect do
+ expect(
+ llm.generate("Think visually", user:, output_thinking: true) do |partial|
+ partials << partial
+ end,
+ ).to eq("Done")
+ end.to change(Upload, :count).by(1)
+
+ visible_thinking =
+ partials.find do |partial|
+ partial.is_a?(DiscourseAi::Completions::Thinking) && partial.message.present?
+ end
+ context_thinking =
+ partials.find do |partial|
+ partial.is_a?(DiscourseAi::Completions::Thinking) && partial.provider_info.present?
+ end
+ expect(visible_thinking.message).to start_with("![image](upload://")
+ expect(context_thinking.provider_info.dig(:gemini_interactions, :steps, 0, :signature)).to eq(
+ "streamed-image-signature",
+ )
+ end
+
+ it "preserves signature-only thought steps without rendering a summary" do
+ thought_step = { type: "thought", signature: "signature-only" }
+ stub_request(:post, url).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [
+ thought_step,
+ { type: "model_output", content: [{ type: "text", text: "Done" }] },
+ ],
+ ).to_json,
+ )
+
+ result = llm.generate("Think", user:, output_thinking: true)
+
+ expect(result).to eq(
+ [
+ "Done",
+ DiscourseAi::Completions::Thinking.new(
+ message: nil,
+ provider_info: {
+ gemini_interactions: {
+ steps: [thought_step],
+ },
+ },
+ ),
+ ],
+ )
+ end
+
+ it "replays signed thought steps when summaries are not returned to the caller" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new(messages: [{ type: :user, content: "Remember PLUM" }])
+ thought_step = { type: "thought", signature: "signed-history" }
+ second_request = nil
+
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ parsed = JSON.parse(body, symbolize_names: true)
+ second_request = parsed if parsed[:input].length > 1
+ true
+ end,
+ ).to_return(
+ {
+ status: 200,
+ body:
+ interaction_response(
+ steps: [
+ thought_step,
+ { type: "model_output", content: [{ type: "text", text: "OK" }] },
+ ],
+ ).to_json,
+ },
+ {
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "PLUM" }] }],
+ ).to_json,
+ },
+ )
+
+ first = llm.generate(prompt, user:)
+ prompt.push_model_response(first)
+ prompt.push(type: :user, content: "Repeat it")
+ model.update!(name: "gemini-3.6-flash")
+ expect(llm.generate(prompt, user:)).to eq("PLUM")
+
+ expect(second_request[:model]).to eq("gemini-3.6-flash")
+ expect(second_request[:input]).to eq(
+ [
+ { type: "user_input", content: [{ type: "text", text: "Remember PLUM" }] },
+ thought_step,
+ { type: "model_output", content: [{ type: "text", text: "OK" }] },
+ { type: "user_input", content: [{ type: "text", text: "Repeat it" }] },
+ ],
+ )
+ end
+
+ it "replays signed built-in tool steps when thinking output is hidden" do
+ prompt = DiscourseAi::Completions::Prompt.new(messages: [{ type: :user, content: "Search" }])
+ search_call = {
+ type: "google_search_call",
+ id: "search-1",
+ arguments: {
+ queries: ["Discourse"],
+ },
+ signature: "call-signature",
+ }
+ search_result = {
+ type: "google_search_result",
+ call_id: "search-1",
+ result: {
+ snippets: [],
+ },
+ signature: "result-signature",
+ }
+ second_request = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ parsed = JSON.parse(body, symbolize_names: true)
+ second_request = parsed if parsed[:input].length > 1
+ true
+ end,
+ ).to_return(
+ {
+ status: 200,
+ body:
+ interaction_response(
+ steps: [
+ search_call,
+ search_result,
+ { type: "model_output", content: [{ type: "text", text: "Found" }] },
+ ],
+ ).to_json,
+ },
+ {
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ },
+ )
+
+ first = llm.generate(prompt, user:)
+ prompt.push_model_response(first)
+ prompt.push(type: :user, content: "Continue")
+ expect(llm.generate(prompt, user:)).to eq("Done")
+
+ expect(second_request[:input]).to eq(
+ [
+ { type: "user_input", content: [{ type: "text", text: "Search" }] },
+ search_call,
+ search_result,
+ { type: "model_output", content: [{ type: "text", text: "Found" }] },
+ { type: "user_input", content: [{ type: "text", text: "Continue" }] },
+ ],
+ )
+ end
+
+ it "returns function calls and replays exact steps with the function result" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "Use tools",
+ messages: [{ type: :user, content: "Echo hi" }],
+ tools: [echo_tool],
+ tool_choice: "echo",
+ )
+ thought_step = { type: "thought", signature: "tool-thought" }
+ call_step = { type: "function_call", id: "call-123", name: "echo", arguments: { text: "hi" } }
+ first_body = nil
+ second_body = nil
+
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ parsed = JSON.parse(body, symbolize_names: true)
+ if first_body
+ second_body = parsed
+ else
+ first_body = parsed
+ end
+ true
+ end,
+ ).to_return(
+ {
+ status: 200,
+ body:
+ interaction_response(status: "requires_action", steps: [thought_step, call_step]).to_json,
+ },
+ {
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "hi" }] }],
+ ).to_json,
+ },
+ )
+
+ tool_call = llm.generate(prompt, user:)
+ expect(tool_call).to eq(
+ DiscourseAi::Completions::ToolCall.new(
+ id: "call-123",
+ name: "echo",
+ parameters: {
+ text: "hi",
+ },
+ provider_data: {
+ gemini_interactions: {
+ steps: [thought_step, call_step],
+ },
+ },
+ ),
+ )
+
+ prompt.push_model_response(tool_call)
+ prompt.push(type: :tool, id: tool_call.id, name: tool_call.name, content: '{"value":"hi"}')
+ prompt.tool_choice = :none
+ expect(llm.generate(prompt, user:)).to eq("hi")
+
+ expect(first_body[:tools]).to eq(
+ [
+ {
+ type: "function",
+ name: "echo",
+ description: "Echo text",
+ parameters: {
+ type: "object",
+ properties: {
+ text: {
+ type: "string",
+ description: "Text to echo",
+ },
+ },
+ required: ["text"],
+ },
+ },
+ ],
+ )
+ expect(first_body.dig(:generation_config, :tool_choice)).to eq(
+ { allowed_tools: { mode: "any", tools: ["echo"] } },
+ )
+ expect(second_body[:input]).to eq(
+ [
+ { type: "user_input", content: [{ type: "text", text: "Echo hi" }] },
+ thought_step,
+ call_step,
+ { type: "function_result", call_id: "call-123", name: "echo", result: '{"value":"hi"}' },
+ ],
+ )
+ expect(second_body.dig(:generation_config, :tool_choice)).to eq("none")
+ end
+
+ it "streams partial and completed function calls" do
+ stream = +""
+ stream << sse_event(
+ "step.start",
+ index: 0,
+ step: {
+ type: "function_call",
+ id: "call-1",
+ name: "echo",
+ arguments: '{"text":"h',
+ },
+ )
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "arguments",
+ partial_arguments: "el",
+ },
+ )
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "arguments",
+ partial_arguments: 'lo"}',
+ },
+ )
+ stream << sse_event("step.stop", index: 0)
+ stream << sse_event(
+ "interaction.completed",
+ interaction: {
+ usage: {
+ total_input_tokens: 1,
+ total_cached_tokens: 0,
+ total_output_tokens: 1,
+ total_thought_tokens: 0,
+ total_tool_use_tokens: 0,
+ },
+ },
+ )
+
+ stub_request(:post, url).to_return(status: 200, body: stream)
+ prompt = DiscourseAi::Completions::Prompt.new("Use tools", tools: [echo_tool])
+ partials = []
+ progress = []
+
+ llm.generate(prompt, user:, partial_tool_calls: true) do |partial|
+ partials << partial.dup
+ progress << partial.parameters[:text].dup if partial.partial?
+ end
+
+ expect(partials.last).to eq(
+ DiscourseAi::Completions::ToolCall.new(
+ id: "call-1",
+ name: "echo",
+ parameters: {
+ text: "hello",
+ },
+ provider_data: {
+ gemini_interactions: {
+ steps: [
+ { type: "function_call", id: "call-1", name: "echo", arguments: { text: "hello" } },
+ ],
+ },
+ },
+ ),
+ )
+ expect(progress).to eq(%w[h hel hello])
+ end
+
+ it "delivers generated images before raising a later error from the same SSE chunk" do
+ image_path = File.expand_path("../../../fixtures/images/100x100.jpg", __dir__)
+ image_data = Base64.strict_encode64(File.binread(image_path))
+ stream = +""
+ stream << sse_event("step.start", index: 0, step: { type: "model_output" })
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "image",
+ mime_type: "image/jpeg",
+ data: image_data,
+ },
+ )
+ stream << sse_event("step.stop", index: 0)
+ stream << sse_event("error", error: { code: "invalid_request", message: "Later failure" })
+ stub_request(:post, url).to_return(status: 200, body: [stream])
+ partials = []
+
+ expect do
+ expect do
+ EndpointMock.with_chunk_array_support do
+ llm.generate("Draw", user:) { |partial| partials << partial }
+ end
+ end.to raise_error(
+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,
+ "Later failure",
+ )
+ end.to change(Upload, :count).by(1)
+
+ expect(partials.grep(String)).to contain_exactly(a_string_matching(%r{!\[image\]\(upload://}))
+ end
+
+ it "preserves streamed native code steps and shows the executed code" do
+ code_call = {
+ type: "code_execution_call",
+ id: "code-1",
+ signature: "code-signature",
+ arguments: {
+ language: "python",
+ code: "print(42)",
+ },
+ }
+ code_result = {
+ type: "code_execution_result",
+ call_id: "code-1",
+ signature: "result-signature",
+ result: "42\n",
+ is_error: false,
+ }
+ chunks = [
+ sse_event("step.start", index: 0, step: code_call.slice(:type, :id, :signature)),
+ sse_event("step.delta", index: 0, delta: code_call.slice(:type, :arguments)),
+ sse_event("step.stop", index: 0),
+ sse_event("step.start", index: 1, step: code_result.slice(:type, :call_id, :signature)),
+ sse_event("step.delta", index: 1, delta: code_result.slice(:type, :result, :is_error)),
+ sse_event("step.stop", index: 1),
+ sse_event("step.start", index: 2, step: { type: "model_output" }),
+ sse_event("step.delta", index: 2, delta: { type: "text", text: "42" }),
+ sse_event("step.stop", index: 2),
+ sse_event(
+ "interaction.completed",
+ interaction: {
+ status: "completed",
+ usage: {
+ total_input_tokens: 4,
+ total_cached_tokens: 0,
+ total_output_tokens: 1,
+ total_thought_tokens: 0,
+ total_tool_use_tokens: 1,
+ },
+ },
+ ),
+ ]
+ stub_request(:post, url).to_return(status: 200, body: chunks)
+ partials = []
+
+ EndpointMock.with_chunk_array_support do
+ expect(
+ llm.generate("Calculate", user:, output_thinking: true) { |partial| partials << partial },
+ ).to eq("42")
+ end
+
+ code_thinking =
+ partials.find do |partial|
+ partial.is_a?(DiscourseAi::Completions::Thinking) && partial.message.present?
+ end
+ expect(code_thinking.message).to eq("Code execution:\n\n```python\nprint(42)\n```")
+
+ replay_context =
+ partials.find do |partial|
+ partial.is_a?(DiscourseAi::Completions::Thinking) && partial.provider_info.present?
+ end
+ expect(replay_context.provider_info).to eq(
+ gemini_interactions: {
+ steps: [code_call, code_result],
+ },
+ )
+ end
+
+ it "supports native search and URL context while preserving their steps" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "Research",
+ messages: [{ type: :user, content: "Find the docs" }],
+ native_tools: %w[web_search web_fetch],
+ )
+ thought_step = { type: "thought", signature: "search-thought" }
+ search_call = {
+ type: "google_search_call",
+ id: "search-1",
+ arguments: {
+ queries: ["Gemini Interactions"],
+ },
+ signature: "search-signature",
+ }
+ search_result = {
+ type: "google_search_result",
+ call_id: "search-1",
+ result: [{ title: "Docs", url: "https://ai.google.dev" }],
+ signature: "result-signature",
+ }
+ output_step = {
+ type: "model_output",
+ content: [
+ {
+ type: "text",
+ text: "Found it",
+ annotations: [{ type: "url_citation", url: "https://ai.google.dev" }],
+ },
+ ],
+ }
+
+ stub_request(:post, url).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [thought_step, search_call, search_result, output_step],
+ ).to_json,
+ )
+ result = llm.generate(prompt, user:, output_thinking: true)
+
+ expect(result.first).to eq(
+ DiscourseAi::Completions::Thinking.new(
+ message: "Web search: Gemini Interactions",
+ partial: false,
+ ),
+ )
+ expect(result.second).to eq("Found it")
+ expect(result.last.provider_info).to eq(
+ gemini_interactions: {
+ steps: [thought_step, search_call, search_result],
+ },
+ )
+
+ prompt.push_model_response(result)
+ prompt.push(type: :user, content: "Summarize that")
+ translated = DiscourseAi::Completions::Dialects::GeminiInteractions.new(prompt, model).translate
+ expect(translated[:input]).to eq(
+ [
+ { type: "user_input", content: [{ type: "text", text: "Find the docs" }] },
+ thought_step,
+ search_call,
+ search_result,
+ output_step.except(:content).merge(content: [{ type: "text", text: "Found it" }]),
+ { type: "user_input", content: [{ type: "text", text: "Summarize that" }] },
+ ],
+ )
+
+ request = JSON.parse(WebMock::RequestRegistry.instance.requested_signatures.hash.keys.last.body)
+ expect(request.dig("generation_config", "tool_choice")).to be_nil
+ expect(request["tools"]).to contain_exactly(
+ { "type" => "google_search" },
+ { "type" => "url_context" },
+ )
+ end
+
+ it "supports native Maps and code execution with Maps attribution" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "Plan accurately",
+ messages: [{ type: :user, content: "Find a cafe and calculate the walking time" }],
+ native_tools: %w[google_maps code_execution],
+ )
+ code_call = {
+ type: "code_execution_call",
+ id: "code-1",
+ arguments: {
+ language: "python",
+ code: "print(1200 / 80)",
+ },
+ signature: "code-signature",
+ }
+ code_result = {
+ type: "code_execution_result",
+ call_id: "code-1",
+ result: "15.0",
+ signature: "code-result-signature",
+ }
+ maps_call = {
+ type: "google_maps_call",
+ id: "maps-1",
+ arguments: {
+ queries: ["coffee near Central Station"],
+ },
+ signature: "maps-signature",
+ }
+ maps_result = {
+ type: "google_maps_result",
+ call_id: "maps-1",
+ result: {
+ places: [{ name: "Central Cafe", url: "https://maps.google.com/example" }],
+ },
+ signature: "maps-result-signature",
+ }
+ output_step = {
+ type: "model_output",
+ content: [
+ {
+ type: "text",
+ text: "Central Cafe is a 15-minute walk away.",
+ annotations: [
+ {
+ type: "place_citation",
+ name: "Central [Cafe]",
+ url: "https://maps.google.com/example",
+ },
+ ],
+ },
+ ],
+ }
+
+ stub_request(:post, url).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [code_call, code_result, maps_call, maps_result, output_step],
+ ).to_json,
+ )
+
+ result = llm.generate(prompt, user:, output_thinking: true)
+
+ expect(result).to eq(
+ [
+ DiscourseAi::Completions::Thinking.new(
+ message: "Code execution:\n\n```python\nprint(1200 / 80)\n```",
+ partial: false,
+ ),
+ DiscourseAi::Completions::Thinking.new(message: "Google Maps search", partial: false),
+ "Central Cafe is a 15-minute walk away.\n\nGoogle Maps: [Central \\[Cafe\\]](https://maps.google.com/example)",
+ DiscourseAi::Completions::Thinking.new(
+ message: nil,
+ partial: false,
+ provider_info: {
+ gemini_interactions: {
+ steps: [code_call, code_result, maps_call, maps_result],
+ },
+ },
+ ),
+ ],
+ )
+
+ request = JSON.parse(WebMock::RequestRegistry.instance.requested_signatures.hash.keys.last.body)
+ expect(request["tools"]).to contain_exactly(
+ { "type" => "google_maps" },
+ { "type" => "code_execution" },
+ )
+ end
+
+ it "emits Google Maps attribution when streaming annotations arrive after text" do
+ stream = +""
+ stream << sse_event("step.start", index: 0, step: { type: "model_output" })
+ stream << sse_event("step.delta", index: 0, delta: { type: "text", text: "Try Central Cafe" })
+ stream << sse_event(
+ "step.delta",
+ index: 0,
+ delta: {
+ type: "text",
+ text: "",
+ annotations: [
+ { type: "place_citation", name: "Central Cafe", url: "https://maps.google.com/example" },
+ ],
+ },
+ )
+ stream << sse_event("step.stop", index: 0)
+ stream << sse_event(
+ "interaction.completed",
+ interaction: {
+ status: "completed",
+ usage: {
+ total_input_tokens: 4,
+ total_cached_tokens: 0,
+ total_output_tokens: 3,
+ total_thought_tokens: 0,
+ total_tool_use_tokens: 0,
+ },
+ },
+ )
+
+ stub_request(:post, url).to_return(status: 200, body: stream)
+ partials = []
+
+ result = llm.generate("Find a cafe", user:) { |partial| partials << partial }
+
+ expect(result).to eq(
+ "Try Central Cafe\n\nGoogle Maps: [Central Cafe](https://maps.google.com/example)",
+ )
+ expect(partials).to eq(
+ ["Try Central Cafe", "\n\nGoogle Maps: [Central Cafe](https://maps.google.com/example)"],
+ )
+ end
+
+ it "uses validated tool choice when custom and server-side tools are combined" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "Use tools",
+ messages: [{ type: :user, content: "Research and echo" }],
+ tools: [echo_tool],
+ native_tools: [DiscourseAi::Completions::NativeTools::WEB_SEARCH],
+ )
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ expect(llm.generate(prompt, user:)).to eq("Done")
+ expect(request_body.dig(:generation_config, :tool_choice)).to eq("validated")
+ end
+
+ it "raises completion failures returned inside successful HTTP responses and streams" do
+ stub_request(:post, url).to_return(
+ { status: 200, body: { status: "failed", error: { message: "Unary failure" } }.to_json },
+ { status: 200, body: sse_event("error", error: { code: 13, message: "Stream failure" }) },
+ {
+ status: 200,
+ body:
+ sse_event(
+ "interaction.status_update",
+ status: "failed",
+ error: {
+ message: "Status failure",
+ },
+ ),
+ },
+ )
+
+ expect { llm.generate("Fail", user:) }.to raise_error(
+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,
+ "Unary failure",
+ )
+ expect { llm.generate("Fail", user:) { |_| } }.to raise_error(
+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,
+ "Stream failure",
+ )
+ expect { llm.generate("Fail", user:) { |_| } }.to raise_error(
+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,
+ "Status failure",
+ )
+ end
+
+ it "supports JSON schema structured output without dropping schema keywords" do
+ schema = {
+ type: "json_schema",
+ json_schema: {
+ name: "reply",
+ schema: {
+ type: "object",
+ properties: {
+ answer: {
+ type: "string",
+ },
+ },
+ required: ["answer"],
+ additionalProperties: false,
+ },
+ },
+ }
+ request_body = nil
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: '{"answer":"yes"}' }] }],
+ ).to_json,
+ )
+
+ result = llm.generate("Answer", user:, response_format: schema)
+
+ expect(result.read_buffered_property(:answer)).to eq("yes")
+ expect(request_body[:response_format]).to eq(
+ { type: "text", mime_type: "application/json", schema: schema.dig(:json_schema, :schema) },
+ )
+ end
+
+ it "encodes image and PDF inputs as typed content" do
+ SiteSetting.authorized_extensions = "*"
+ image_file = File.open(File.expand_path("../../../fixtures/images/100x100.jpg", __dir__))
+ image_upload = UploadCreator.new(image_file, "image.jpg").create_for(Discourse.system_user.id)
+ pdf_file = Tempfile.new(%w[interactions .pdf])
+ pdf_file.binmode
+ pdf_file.write("%PDF-1.4\n%%EOF")
+ pdf_file.rewind
+ pdf_upload = UploadCreator.new(pdf_file, "document.pdf").create_for(Discourse.system_user.id)
+ model.update!(allowed_attachment_types: %w[pdf])
+ request_body = nil
+
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "Inspect files",
+ messages: [
+ {
+ type: :user,
+ content: [
+ "Describe these",
+ { upload_id: image_upload.id },
+ { upload_id: pdf_upload.id },
+ ],
+ },
+ ],
+ )
+ encoded = prompt.encoded_uploads(prompt.messages.last, allow_documents: true)
+
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [{ type: "model_output", content: [{ type: "text", text: "Done" }] }],
+ ).to_json,
+ )
+
+ expect(llm.generate(prompt, user:)).to eq("Done")
+ expect(request_body[:input]).to eq(
+ [
+ {
+ type: "user_input",
+ content: [
+ { type: "text", text: "Describe these" },
+ { type: "image", mime_type: "image/jpeg", data: encoded[0][:base64] },
+ { type: "document", mime_type: "application/pdf", data: encoded[1][:base64] },
+ ],
+ },
+ ],
+ )
+ ensure
+ pdf_file&.close!
+ end
+
+ it "turns generated image content into a Discourse upload" do
+ image_path = File.expand_path("../../../fixtures/images/100x100.jpg", __dir__)
+ image_data = Base64.strict_encode64(File.binread(image_path))
+ stub_request(:post, url).to_return(
+ status: 200,
+ body:
+ interaction_response(
+ steps: [
+ {
+ type: "model_output",
+ content: [{ type: "image", mime_type: "image/jpeg", data: image_data }],
+ },
+ ],
+ ).to_json,
+ )
+
+ expect { expect(llm.generate("Draw", user:)).to match(%r{!\[image\]\(upload://}) }.to change(
+ Upload,
+ :count,
+ ).by(1)
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb
index c2dd337f926..45c156afbe1 100644
--- a/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb
@@ -17,6 +17,20 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
expect(model).not_to have_key(:provider_params)
end
+ it "uses the Gemini Interactions API for all Google models" do
+ preset = described_class.find_provider("google")
+
+ expect(preset).to include(
+ provider: "gemini_interactions",
+ endpoint: "https://generativelanguage.googleapis.com/v1/interactions",
+ )
+ expect(preset[:models].pluck(:name)).to eq(%w[gemini-3.1-pro-preview gemini-3.6-flash])
+ expect(preset[:models].first[:endpoint]).to eq(
+ "https://generativelanguage.googleapis.com/v1beta/interactions",
+ )
+ expect(preset[:models].last).not_to have_key(:endpoint)
+ end
+
it "includes a Google Vertex AI preset" do
preset = described_class.find_provider("google_vertex_ai")
model = preset[:models].first
diff --git a/plugins/discourse-ai/spec/lib/completions/native_tools_spec.rb b/plugins/discourse-ai/spec/lib/completions/native_tools_spec.rb
index 525a4524d54..5219977c2f3 100644
--- a/plugins/discourse-ai/spec/lib/completions/native_tools_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/native_tools_spec.rb
@@ -4,6 +4,7 @@ RSpec.describe DiscourseAi::Completions::NativeTools do
before { enable_current_plugin }
fab!(:gemini_model)
+ fab!(:gemini_interactions_model)
fab!(:anthropic_model)
fab!(:openai_chat_model) do
Fabricate(:llm_model, url: "https://api.openai.com/v1/chat/completions")
@@ -14,8 +15,11 @@ RSpec.describe DiscourseAi::Completions::NativeTools do
fab!(:bedrock_model)
describe ".supported_ids_for" do
- it "supports Gemini grounding and URL context" do
+ it "supports provider-native Gemini tools" do
expect(described_class.supported_ids_for(gemini_model)).to eq(%w[web_search web_fetch])
+ expect(described_class.supported_ids_for(gemini_interactions_model)).to eq(
+ %w[web_search web_fetch google_maps code_execution],
+ )
end
it "supports Anthropic web search and fetch" do
@@ -42,6 +46,10 @@ RSpec.describe DiscourseAi::Completions::NativeTools do
expect(described_class.valid?("native-web_search")).to eq(true)
expect(described_class.valid?("web_fetch")).to eq(true)
expect(described_class.valid?("native-web_fetch")).to eq(true)
+ expect(described_class.valid?("google_maps")).to eq(true)
+ expect(described_class.valid?("native-google_maps")).to eq(true)
+ expect(described_class.valid?("code_execution")).to eq(true)
+ expect(described_class.valid?("native-code_execution")).to eq(true)
expect(described_class.valid?("nope")).to eq(false)
end
@@ -69,6 +77,25 @@ RSpec.describe DiscourseAi::Completions::NativeTools do
expect(dialect.tools).to eq([{ google_search: {} }, { url_context: {} }])
end
+ it "renders Gemini Interactions native tools" do
+ prompt =
+ DiscourseAi::Completions::Prompt.new("system", messages: [{ type: :user, content: "hi" }])
+ prompt.native_tools = %w[web_search web_fetch google_maps code_execution]
+
+ dialect =
+ DiscourseAi::Completions::Dialects::GeminiInteractions.new(
+ prompt,
+ gemini_interactions_model,
+ )
+
+ expect(dialect.tools).to contain_exactly(
+ { type: "google_search" },
+ { type: "url_context" },
+ { type: "google_maps" },
+ { type: "code_execution" },
+ )
+ end
+
it "renders google_search alongside function declarations for Gemini" do
web_search_prompt.tools = [
{
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
index abe5b5df974..ae84e0d9d50 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
@@ -698,6 +698,30 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
}.not_to raise_error
end
+ it "attaches referenced bot post uploads to custom prompt model messages" do
+ second_post.update_columns(raw: "Here is the result ![image](#{image_upload1.short_url})")
+ UploadReference.create!(target: second_post, upload: image_upload1)
+ PostCustomPrompt.create!(
+ post_id: second_post.id,
+ custom_prompt: [
+ [second_post.raw, bot_user.username, nil, nil, { message: "Generated an image" }],
+ ],
+ )
+
+ context =
+ described_class.messages_from_post(
+ third_post,
+ max_posts: 10,
+ bot_usernames: [bot_user.username],
+ include_image_uploads: true,
+ include_document_uploads: false,
+ )
+
+ expect(context.find { |message| message[:type] == :model }).to include(
+ content: [second_post.raw, { upload_id: image_upload1.id }],
+ )
+ end
+
it "handles uploads correctly in topic style messages (and times)" do
freeze_time 32.days.ago
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index f1a62906706..92a509c0d75 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
@@ -199,6 +199,34 @@ RSpec.describe DiscourseAi::AiBot::Playground do
)
end
+ it "closes a fenced thinking block before rendering visible content" do
+ ai_agent.update!(show_thinking: true)
+ agent_klass = AiAgent.all_agents.find { |agent_class| agent_class.name == ai_agent.name }
+ bot = DiscourseAi::Agents::Bot.as(bot_user, agent: agent_klass.new)
+ playground = described_class.new(bot)
+ responses = [
+ [
+ DiscourseAi::Completions::Thinking.new(
+ message: "Code execution:\n\n```python\nprint(42)\n```",
+ ),
+ "![image](https://example.com/image.png)",
+ ],
+ ]
+
+ reply_post = nil
+ DiscourseAi::Completions::Llm.with_prepared_responses(responses) do
+ new_post = Fabricate(:post, raw: "Render a chart")
+ reply_post = playground.reply_to(new_post)
+ end
+
+ expect(reply_post.raw).to include(
+ "```python\nprint(42)\n```\n</details>\n\n![image](https://example.com/image.png)",
+ )
+ expect(reply_post.cooked).to include(
+ "</code></pre>\n</details>\n<p><img src=\"https://example.com/image.png\"",
+ )
+ end
+
it "keeps trailing thinking outside the response text" do
ai_agent.update!(show_thinking: true)
agent_klass = AiAgent.all_agents.find { |agent_class| agent_class.name == ai_agent.name }
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/llm-provider-param-helpers-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/llm-provider-param-helpers-test.js
index 6667285a562..202ea3ca489 100644
--- a/plugins/discourse-ai/test/javascripts/unit/lib/llm-provider-param-helpers-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/llm-provider-param-helpers-test.js
@@ -3,6 +3,7 @@ import {
isParamActive,
isProviderParamHidden,
normalizeProviderParams,
+ providerParamLabel,
} from "discourse/plugins/discourse-ai/discourse/lib/llm-provider-param-helpers";
module("Unit | Lib | llm-provider-param-helpers", function () {
@@ -186,6 +187,27 @@ module("Unit | Lib | llm-provider-param-helpers", function () {
});
});
+ module("providerParamLabel", function () {
+ test("uses a provider-specific label when configured", function (assert) {
+ assert.strictEqual(
+ providerParamLabel("thinking_level", {
+ label:
+ "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ }),
+ "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ "the provider-specific translation key is used"
+ );
+ });
+
+ test("falls back to the shared field label", function (assert) {
+ assert.strictEqual(
+ providerParamLabel("thinking_level", {}),
+ "discourse_ai.llms.provider_fields.thinking_level",
+ "legacy providers keep the shared translation key"
+ );
+ });
+ });
+
module("normalizeProviderParams", function () {
test("returns empty object for null/undefined input", function (assert) {
assert.deepEqual(normalizeProviderParams(null), {});
@@ -240,6 +262,23 @@ module("Unit | Lib | llm-provider-param-helpers", function () {
]);
});
+ test("preserves provider-specific label metadata", function (assert) {
+ const result = normalizeProviderParams({
+ thinking_level: {
+ type: "enum",
+ values: ["default", "low", "high"],
+ label:
+ "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ },
+ });
+
+ assert.strictEqual(
+ result.thinking_level.label,
+ "discourse_ai.llms.provider_fields.gemini_interactions_thinking_level",
+ "the label survives metadata normalization"
+ );
+ });
+
test("preserves tooltip and helpText metadata", function (assert) {
const result = normalizeProviderParams({
reasoning_parser: {