Skip to content

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

FIX: Handle thinking events and refresh models (#43162)

Sam0b3d44ac9282
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index a240154d8e5..8701559ad1b 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1237,20 +1237,23 @@ en:
model_description:
none: "General settings that work for most language models"
- 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-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"
- open_router-moonshotai-kimi-k2-6: "Moonshot's multimodal model with vision capabilities"
- open_router-deepseek-deepseek-v4-flash: "DeepSeek's latest high-performance model with 1M context"
- open_router-minimax-minimax-m2-7: "MiniMax's model with large context"
- open_router-x-ai-grok-4-1-fast: "xAI's fast multimodal model with very large context"
- open_router-z-ai-glm-5-1: "Z-AI's reasoning-focused model"
- open_router-qwen-qwen3-6-plus: "Qwen's multimodal model with 1M context"
+ anthropic-claude-fable-5-1: "Anthropic's maximum-capability model for demanding reasoning and long-running agents"
+ anthropic-claude-opus-5: "Anthropic's flagship for complex agentic coding, deep reasoning, and enterprise work"
+ anthropic-claude-sonnet-5: "Best balance of speed and intelligence for coding, agents, and everyday knowledge work"
+ anthropic-claude-haiku-4-5-20251001: "Fast, economical model for high-volume and latency-sensitive workloads"
+ google-gemini-3-1-pro-preview: "Google's Pro model for advanced reasoning, complex problem-solving, and agentic coding"
+ google-gemini-3-8-flash: "Google's most intelligent Flash model for long-horizon software engineering and autonomous agents"
+ google_vertex_ai-google-gemini-3-8-flash: "Gemini 3.8 Flash through Vertex AI for multimodal, agentic, and enterprise workloads"
+ open_ai-gpt-5-6-sol: "OpenAI's flagship GPT-5.6 model for complex professional, coding, and agentic work"
+ open_ai-gpt-5-6-terra: "Balanced GPT-5.6 model for strong intelligence at lower cost"
+ open_ai-gpt-5-6-luna: "Fast, economical GPT-5.6 model for high-volume and well-defined tasks"
+ open_router-z-ai-glm-5-3-flash: "Fast, inexpensive multimodal reasoning model for coding and long-running agents"
+ open_router-deepseek-deepseek-v4-flash-vision-exp: "Experimental multimodal DeepSeek model for economical coding, reasoning, and image understanding"
+ open_router-tencent-hy4-preview: "Preview reasoning model for coding agents, complex tool use, and sustained multi-step work"
+ open_router-xiaomi-mimo-v2-5: "Low-cost omnimodal model for agents and image, audio, and video understanding"
+ open_router-moonshotai-kimi-k3: "Frontier multimodal model for complex coding, repository navigation, and long-running agents"
+ open_router-minimax-minimax-m3: "Long-context multimodal model for sustained coding, tool use, and multi-step agents"
+ open_router-x-ai-grok-4-6: "Frontier multimodal reasoning model for coding, tool use, and long-context workloads"
preseeded_model_description: "Pre-configured open-source model utilizing %{model}"
diff --git a/plugins/discourse-ai/lib/agents/artifact_update_strategies/base.rb b/plugins/discourse-ai/lib/agents/artifact_update_strategies/base.rb
index 496aaf0a29d..1deb921624a 100644
--- a/plugins/discourse-ai/lib/agents/artifact_update_strategies/base.rb
+++ b/plugins/discourse-ai/lib/agents/artifact_update_strategies/base.rb
@@ -56,6 +56,8 @@ module DiscourseAi
cancel_manager: cancel_manager,
execution_context:,
) do |partial|
+ next if !partial.is_a?(String)
+
progress.call(partial) if progress
response << partial
end
diff --git a/plugins/discourse-ai/lib/agents/tools/create_artifact.rb b/plugins/discourse-ai/lib/agents/tools/create_artifact.rb
index 9e3255843f0..d8d19478679 100644
--- a/plugins/discourse-ai/lib/agents/tools/create_artifact.rb
+++ b/plugins/discourse-ai/lib/agents/tools/create_artifact.rb
@@ -158,6 +158,8 @@ module DiscourseAi
feature_name: "create_artifact",
cancel_manager: context.cancel_manager,
) do |partial_response|
+ next if !partial_response.is_a?(String)
+
response << partial_response
yield partial_response
end
diff --git a/plugins/discourse-ai/lib/agents/tools/researcher.rb b/plugins/discourse-ai/lib/agents/tools/researcher.rb
index e134c7bceb7..8dcc8bb31ee 100644
--- a/plugins/discourse-ai/lib/agents/tools/researcher.rb
+++ b/plugins/discourse-ai/lib/agents/tools/researcher.rb
@@ -223,7 +223,7 @@ module DiscourseAi
user: post.user,
feature_name: context.feature_name,
cancel_manager: context.cancel_manager,
- ) { |partial| results << partial }
+ ) { |partial| results << partial if partial.is_a?(String) }
@progress_dots ||= 0
@progress_dots += 1
diff --git a/plugins/discourse-ai/lib/agents/tools/view_image.rb b/plugins/discourse-ai/lib/agents/tools/view_image.rb
index db08939cac0..4d2585895c9 100644
--- a/plugins/discourse-ai/lib/agents/tools/view_image.rb
+++ b/plugins/discourse-ai/lib/agents/tools/view_image.rb
@@ -79,21 +79,23 @@ module DiscourseAi
context.view_image_invocations += 1
vision_llm = DiscourseAi::Completions::Llm.proxy(target)
analysis =
- vision_llm.generate(
- vision_prompt(question, encoded_uploads),
- user: context.user || acting_user,
- feature_name: context.feature_name,
- feature_context:
- context
- .feature_context
- .merge(
- vision_delegation: true,
- primary_llm_model_id: llm.llm_model.id,
- ai_agent_id: agent&.id,
- )
- .compact,
- max_tokens: MAX_OUTPUT_TOKENS,
- cancel_manager: context.cancel_manager,
+ DiscourseAi::Completions::Llm.text_from_response(
+ vision_llm.generate(
+ vision_prompt(question, encoded_uploads),
+ user: context.user || acting_user,
+ feature_name: context.feature_name,
+ feature_context:
+ context
+ .feature_context
+ .merge(
+ vision_delegation: true,
+ primary_llm_model_id: llm.llm_model.id,
+ ai_agent_id: agent&.id,
+ )
+ .compact,
+ max_tokens: MAX_OUTPUT_TOKENS,
+ cancel_manager: context.cancel_manager,
+ ),
)
return delegate_error if analysis.blank?
@@ -101,7 +103,7 @@ module DiscourseAi
{
status: "success",
analysis:
- "#{ANALYSIS_PREFIX}#{truncate(analysis.to_s, llm: vision_llm, max_length: MAX_OUTPUT_TOKENS)}",
+ "#{ANALYSIS_PREFIX}#{truncate(analysis, llm: vision_llm, max_length: MAX_OUTPUT_TOKENS)}",
images: resolved.map { |result| result.except(:encoded_upload) },
}
rescue LlmQuotaUsage::QuotaExceededError,
diff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
index 97619f7b162..486462bfe4f 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
@@ -20,6 +20,15 @@ module DiscourseAi
THINKING_LEVEL_BY_EFFORT.merge("minimal" => "low").freeze
LOW_HIGH_THINKING_LEVEL_BY_EFFORT =
THINKING_LEVEL_WITHOUT_MINIMAL_BY_EFFORT.merge("medium" => "high").freeze
+ GEMINI_3_8_UNSUPPORTED_MODEL_PARAMS = %i[
+ temperature
+ top_p
+ topP
+ top_k
+ topK
+ thinking_budget
+ thinkingBudget
+ ].freeze
def self.can_contact?(llm_model)
llm_model.provider == "google"
@@ -62,6 +71,7 @@ module DiscourseAi
end
model_params.delete(:topP) if llm_model.lookup_custom_param("disable_top_p")
+ strip_unsupported_gemini_3_8_params!(model_params)
model_params
end
@@ -83,6 +93,17 @@ module DiscourseAi
end
if effort == "none"
+ if gemini_3_8_flash?
+ return(
+ DiscourseAi::Completions::ThinkingConfig.new(
+ canonical_effort: effort,
+ provider_effort: "low",
+ enabled: true,
+ strip_temperature: true,
+ )
+ )
+ end
+
# some Gemini 3 Pro-tier models can't run with thinking disabled at
# all and reject a forced thinkingBudget: 0 outright — omit the
# override entirely and let the model use its own default instead.
@@ -214,6 +235,32 @@ module DiscourseAi
@gemini_model_id ||= [llm_model.name, llm_model.url].compact.join(" ")
end
+ def gemini_3_8_flash?
+ gemini_model_id.include?("gemini-3.8-flash")
+ end
+
+ def strip_unsupported_gemini_3_8_params!(model_params)
+ return if !gemini_3_8_flash?
+
+ model_params.except!(*GEMINI_3_8_UNSUPPORTED_MODEL_PARAMS)
+
+ thinking_config = model_params[:thinkingConfig]
+ return if !thinking_config.is_a?(Hash)
+
+ thinking_config =
+ thinking_config.except(
+ :thinkingBudget,
+ :thinking_budget,
+ "thinkingBudget",
+ "thinking_budget",
+ )
+ if thinking_config.empty?
+ model_params.delete(:thinkingConfig)
+ else
+ model_params[:thinkingConfig] = thinking_config
+ end
+ end
+
def legacy_thinking_config
thinking_level = llm_model.lookup_custom_param("thinking_level")
provider_effort = thinking_level_for_effort(thinking_level)
@@ -229,6 +276,8 @@ module DiscourseAi
end
if llm_model.lookup_custom_param("enable_thinking")
+ return DiscourseAi::Completions::ThinkingConfig.disabled if gemini_3_8_flash?
+
thinking_tokens = llm_model.lookup_custom_param("thinking_tokens").to_i.clamp(0, 24_576)
return DiscourseAi::Completions::ThinkingConfig.explicit_none if thinking_tokens.zero?
diff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb
index 06cc9c87731..0fd5c307624 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini_interactions.rb
@@ -28,6 +28,7 @@ module DiscourseAi
model_params.delete(:temperature)
end
model_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")
+ strip_unsupported_gemini_3_8_params!(model_params)
model_params
end
@@ -138,6 +139,8 @@ module DiscourseAi
end
def supports_interactions_minimal_thinking?
+ return false if gemini_3_8_flash?
+
gemini_model_id.match?(/gemini-3(?:\.\d+)?-flash(?:\s|\z|-)/) ||
gemini_model_id.include?("gemini-3.1-flash-lite")
end
diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rb
index 3cba24cf503..1173b84102a 100644
--- a/plugins/discourse-ai/lib/completions/llm.rb
+++ b/plugins/discourse-ai/lib/completions/llm.rb
@@ -112,8 +112,10 @@ module DiscourseAi
def text_from_response(response)
if response.is_a?(Array)
response.select { |content| content.is_a?(String) }.join
+ elsif response.respond_to?(:to_str)
+ response.to_str
else
- response
+ ""
end
end
diff --git a/plugins/discourse-ai/lib/completions/llm_presets.rb b/plugins/discourse-ai/lib/completions/llm_presets.rb
index f671fa1921a..4789a0dcc50 100644
--- a/plugins/discourse-ai/lib/completions/llm_presets.rb
+++ b/plugins/discourse-ai/lib/completions/llm_presets.rb
@@ -39,26 +39,24 @@ module DiscourseAi
id: "anthropic",
models: [
model(
- name: "claude-opus-4-8",
+ name: "claude-fable-5-1",
tokens: 1_000_000,
- display_name: "Claude Opus 4.8",
+ display_name: "Claude Fable 5.1",
max_output_tokens: 128_000,
- input_cost: 5.0,
- cached_input_cost: 0.50,
- cache_write_cost: 6.25,
- output_cost: 25.0,
+ input_cost: 10.0,
+ cached_input_cost: 0.25,
+ cache_write_cost: 12.50,
+ output_cost: 50.0,
vision_enabled: true,
- # Opus 4.7+ rejects the classic thinking.type=enabled request
- # and requires thinking.type=adaptive instead.
provider_params: {
enable_reasoning: true,
adaptive_thinking: true,
},
),
model(
- name: "claude-opus-4-7",
+ name: "claude-opus-5",
tokens: 1_000_000,
- display_name: "Claude Opus 4.7",
+ display_name: "Claude Opus 5",
max_output_tokens: 128_000,
input_cost: 5.0,
cached_input_cost: 0.50,
@@ -74,31 +72,19 @@ module DiscourseAi
name: "claude-sonnet-5",
tokens: 1_000_000,
display_name: "Claude Sonnet 5",
- max_output_tokens: 64_000,
- input_cost: 3.0,
- cached_input_cost: 0.30,
- cache_write_cost: 3.75,
- output_cost: 15.0,
+ max_output_tokens: 128_000,
+ input_cost: 2.0,
+ cached_input_cost: 0.20,
+ cache_write_cost: 2.50,
+ output_cost: 10.0,
vision_enabled: true,
- # Sonnet 5 also requires adaptive thinking, same as Opus 4.7+.
provider_params: {
enable_reasoning: true,
adaptive_thinking: true,
},
),
model(
- name: "claude-sonnet-4-6",
- tokens: 1_000_000,
- display_name: "Claude Sonnet 4.6",
- max_output_tokens: 64_000,
- input_cost: 3.0,
- cached_input_cost: 0.30,
- cache_write_cost: 3.75,
- output_cost: 15.0,
- vision_enabled: true,
- ),
- model(
- name: "claude-haiku-4-5",
+ name: "claude-haiku-4-5-20251001",
tokens: 200_000,
display_name: "Claude Haiku 4.5",
max_output_tokens: 64_000,
@@ -121,28 +107,27 @@ module DiscourseAi
models: [
model(
name: "gemini-3.1-pro-preview",
- tokens: 1_000_000,
+ tokens: 1_048_576,
display_name: "Gemini 3.1 Pro Preview",
- max_output_tokens: 65_000,
+ max_output_tokens: 65_536,
input_cost: 2.0,
cached_input_cost: 0.20,
output_cost: 12.0,
vision_enabled: true,
- endpoint: "https://generativelanguage.googleapis.com/v1beta/interactions",
),
model(
- name: "gemini-3.6-flash",
+ name: "gemini-3.8-flash",
tokens: 1_048_576,
- display_name: "Gemini 3.6 Flash",
+ display_name: "Gemini 3.8 Flash",
max_output_tokens: 65_536,
- input_cost: 1.50,
- cached_input_cost: 0.15,
- output_cost: 7.50,
+ input_cost: 0.75,
+ cached_input_cost: 0.075,
+ output_cost: 3.75,
vision_enabled: true,
),
],
tokenizer: DiscourseAi::Tokenizer::GeminiTokenizer,
- endpoint: "https://generativelanguage.googleapis.com/v1/interactions",
+ endpoint: "https://generativelanguage.googleapis.com/v1beta/interactions",
provider: "gemini_interactions",
}
end
@@ -152,13 +137,13 @@ module DiscourseAi
id: "google_vertex_ai",
models: [
model(
- name: "google/gemini-3-flash",
- tokens: 1_000_000,
- display_name: "Gemini 3 Flash (Vertex)",
- max_output_tokens: 65_000,
- input_cost: 0.50,
- cached_input_cost: 0.05,
- output_cost: 3.0,
+ name: "google/gemini-3.8-flash",
+ tokens: 1_048_576,
+ display_name: "Gemini 3.8 Flash (Vertex)",
+ max_output_tokens: 65_536,
+ input_cost: 0.75,
+ cached_input_cost: 0.075,
+ output_cost: 3.75,
vision_enabled: true,
provider_params: {
region: "global",
@@ -179,10 +164,10 @@ module DiscourseAi
tokens: 1_050_000,
display_name: "GPT-5.6 Sol",
max_output_tokens: 128_000,
- input_cost: 5.0,
- cached_input_cost: 0.50,
- cache_write_cost: 6.25,
- output_cost: 30.0,
+ input_cost: 4.0,
+ cached_input_cost: 0.40,
+ cache_write_cost: 5.0,
+ output_cost: 20.0,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
),
@@ -191,10 +176,10 @@ module DiscourseAi
tokens: 1_050_000,
display_name: "GPT-5.6 Terra",
max_output_tokens: 128_000,
- input_cost: 2.50,
- cached_input_cost: 0.25,
- cache_write_cost: 3.125,
- output_cost: 15.0,
+ input_cost: 2.0,
+ cached_input_cost: 0.20,
+ cache_write_cost: 2.50,
+ output_cost: 12.0,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
),
@@ -203,10 +188,10 @@ module DiscourseAi
tokens: 1_050_000,
display_name: "GPT-5.6 Luna",
max_output_tokens: 128_000,
- input_cost: 1.0,
- cached_input_cost: 0.10,
- cache_write_cost: 1.25,
- output_cost: 6.0,
+ input_cost: 0.20,
+ cached_input_cost: 0.02,
+ cache_write_cost: 0.25,
+ output_cost: 1.20,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
),
@@ -222,59 +207,72 @@ module DiscourseAi
id: "open_router",
models: [
model(
- name: "moonshotai/kimi-k2.6",
- tokens: 262_144,
- display_name: "Moonshot Kimi K2.6",
+ name: "z-ai/glm-5.3-flash",
+ tokens: 1_310_720,
+ display_name: "Z.AI GLM 5.3 Flash",
max_output_tokens: 131_072,
- input_cost: 0.74,
- cached_input_cost: 0.14,
- output_cost: 3.49,
+ input_cost: 0.075,
+ cached_input_cost: 0.015,
+ output_cost: 0.25,
+ vision_enabled: true,
+ ),
+ model(
+ name: "deepseek/deepseek-v4-flash-vision-exp",
+ tokens: 1_048_576,
+ display_name: "DeepSeek V4 Flash Vision Exp",
+ max_output_tokens: 384_000,
+ input_cost: 0.44,
+ cached_input_cost: 0.014,
+ output_cost: 1.32,
vision_enabled: true,
),
model(
- name: "deepseek/deepseek-v4-flash",
+ name: "tencent/hy4-preview",
tokens: 1_048_576,
- display_name: "DeepSeek V4 Flash",
- max_output_tokens: 192_000,
+ display_name: "Tencent Hy4 Preview",
+ max_output_tokens: 64_000,
+ input_cost: 0.834,
+ cached_input_cost: 0.042,
+ output_cost: 2.501,
+ ),
+ model(
+ name: "xiaomi/mimo-v2.5",
+ tokens: 1_050_000,
+ display_name: "Xiaomi MiMo-V2.5",
+ max_output_tokens: 131_072,
input_cost: 0.14,
cached_input_cost: 0.0028,
output_cost: 0.28,
+ vision_enabled: true,
),
model(
- name: "minimax/minimax-m2.7",
- tokens: 196_608,
- display_name: "MiniMax M2.7",
- max_output_tokens: 65_536,
+ name: "moonshotai/kimi-k3",
+ tokens: 1_048_576,
+ display_name: "Moonshot Kimi K3",
+ max_output_tokens: 943_718,
+ input_cost: 3.0,
+ cached_input_cost: 0.30,
+ output_cost: 15.0,
+ vision_enabled: true,
+ ),
+ model(
+ name: "minimax/minimax-m3",
+ tokens: 1_048_576,
+ display_name: "MiniMax M3",
+ max_output_tokens: 512_000,
input_cost: 0.30,
cached_input_cost: 0.06,
output_cost: 1.20,
- ),
- model(
- name: "x-ai/grok-4.1-fast",
- tokens: 2_000_000,
- display_name: "xAI Grok 4.1 Fast",
- max_output_tokens: 30_000,
- input_cost: 0.20,
- cached_input_cost: 0.05,
- output_cost: 0.50,
vision_enabled: true,
),
model(
- name: "z-ai/glm-5.1",
- tokens: 202_752,
- display_name: "Z-AI GLM-5.1",
- max_output_tokens: 65_536,
- input_cost: 1.05,
- cached_input_cost: 0.525,
- output_cost: 3.50,
- ),
- model(
- name: "qwen/qwen3.6-plus",
- tokens: 1_000_000,
- display_name: "Qwen3.6 Plus",
- max_output_tokens: 65_536,
- input_cost: 0.325,
- output_cost: 1.95,
+ name: "x-ai/grok-4.6",
+ tokens: 500_000,
+ display_name: "xAI Grok 4.6",
+ max_output_tokens: 450_000,
+ input_cost: 2.0,
+ cached_input_cost: 0.50,
+ output_cost: 6.0,
vision_enabled: true,
),
],
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/create_artifact_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/create_artifact_spec.rb
index a3c947e9f37..3b4e265a3e1 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/create_artifact_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/create_artifact_spec.rb
@@ -11,6 +11,44 @@ RSpec.describe DiscourseAi::Agents::Tools::CreateArtifact do
end
describe "#process" do
+ it "ignores non-text completion events while generating artifact code" do
+ artifact_source = <<~TXT.strip
+ [HTML]
+ <div>Generated</div>
+ [/HTML]
+ TXT
+ thinking =
+ DiscourseAi::Completions::Thinking.new(
+ message: nil,
+ provider_info: {
+ gemini_interactions: {
+ steps: [{ signature: "signed-context" }],
+ },
+ },
+ )
+ tool = nil
+ result = nil
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([[artifact_source, thinking]]) do
+ tool =
+ described_class.new(
+ { html_body: "hello" },
+ bot_user: Fabricate(:user),
+ llm: llm,
+ context: DiscourseAi::Agents::BotContext.new(post: post),
+ )
+ tool.parameters = { name: "generated", specification: "generate an artifact" }
+
+ result = tool.invoke {}
+ end
+
+ expect(result[:status]).to eq("success")
+ expect(AiArtifact.order(:id).last.html).to eq("<div>Generated</div>")
+ expect(tool.custom_raw).not_to include(
+ thinking.provider_info_for(:gemini_interactions)[:steps].first[:signature],
+ )
+ end
+
it "correctly adds details block on final invoke" do
responses = [<<~TXT.strip]
[HTML]
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/researcher_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/researcher_spec.rb
index da0dc8d499e..22622914676 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/researcher_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/researcher_spec.rb
@@ -73,6 +73,33 @@ RSpec.describe DiscourseAi::Agents::Tools::Researcher do
end
describe "#invoke" do
+ it "excludes non-text completion events from research results" do
+ thinking =
+ DiscourseAi::Completions::Thinking.new(
+ message: "Researching privately",
+ provider_info: {
+ gemini_interactions: {
+ steps: [{ signature: "signed-context" }],
+ },
+ },
+ )
+ researcher =
+ described_class.new(
+ { filter: "topic:#{topic_with_tags.id}", goals: "analyze topic content", dry_run: false },
+ bot_user: bot_user,
+ llm: llm,
+ context: DiscourseAi::Agents::BotContext.new(user: user, post: post),
+ )
+
+ results = nil
+ DiscourseAi::Completions::Llm.with_prepared_responses([["Public findings", thinking]]) do
+ researcher.llm = llm_model.to_llm
+ results = researcher.invoke(&progress_blk)
+ end
+
+ expect(results[:results]).to eq(["Public findings"])
+ end
+
it "can correctly filter to a topic id" do
researcher =
described_class.new(
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/update_artifact_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/update_artifact_spec.rb
index 290c9b8a9bf..651fab9198f 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/update_artifact_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/update_artifact_spec.rb
@@ -21,6 +21,46 @@ RSpec.describe DiscourseAi::Agents::Tools::UpdateArtifact do
end
describe "#process" do
+ it "ignores non-text completion events while updating artifact code" do
+ updated_source = <<~TXT.strip
+ [HTML]
+ <div>Updated safely</div>
+ [/HTML]
+ TXT
+ thinking =
+ DiscourseAi::Completions::Thinking.new(
+ message: nil,
+ provider_info: {
+ gemini_interactions: {
+ steps: [{ signature: "signed-context" }],
+ },
+ },
+ )
+ tool = nil
+ result = nil
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([[updated_source, thinking]]) do
+ tool =
+ described_class.new(
+ { artifact_id: artifact.id, instructions: "Update safely" },
+ bot_user: bot_user,
+ llm: llm_model.to_llm,
+ agent_options: {
+ "update_algorithm" => "full",
+ },
+ context: DiscourseAi::Agents::BotContext.new(messages: [], post: post),
+ )
+
+ result = tool.invoke {}
+ end
+
+ expect(result[:status]).to eq("success")
+ expect(artifact.versions.order(:version_number).last.html).to eq("<div>Updated safely</div>")
+ expect(tool.custom_raw).not_to include(
+ thinking.provider_info_for(:gemini_interactions)[:steps].first[:signature],
+ )
+ end
+
it "correctly updates artifact using section markers" do
responses = [<<~TXT.strip]
[HTML]
diff --git a/plugins/discourse-ai/spec/lib/agents/tools/view_image_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/view_image_spec.rb
index 99f7f1c5420..e682faecacb 100644
--- a/plugins/discourse-ai/spec/lib/agents/tools/view_image_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/tools/view_image_spec.rb
@@ -39,6 +39,38 @@ RSpec.describe DiscourseAi::Agents::Tools::ViewImage do
)
end
+ it "excludes non-text completion events from the analysis" do
+ thinking =
+ DiscourseAi::Completions::Thinking.new(
+ message: "Private reasoning",
+ provider_info: {
+ gemini_interactions: {
+ steps: [{ signature: "signed-context" }],
+ },
+ },
+ )
+ result = nil
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([["A small test image", thinking]]) do
+ result = build_tool.invoke
+ end
+
+ expect(result).to include(status: "success")
+ expect(result[:analysis]).to eq("#{described_class::ANALYSIS_PREFIX}A small test image")
+ end
+
+ it "returns an error when delegated vision produces no text" do
+ thinking = DiscourseAi::Completions::Thinking.new(message: "Private reasoning")
+ result = nil
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([thinking]) { result = build_tool.invoke }
+
+ expect(result).to eq(
+ status: "error",
+ error: "The configured vision model could not analyze the image.",
+ )
+ end
+
it "analyzes an authorized upload with a tool-free native request" do
result = nil
prompts = nil
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
index 4bba9264b84..5ca1de8bb5c 100644
--- a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_interactions_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_interactions_spec.rb
@@ -170,6 +170,63 @@ RSpec.describe DiscourseAi::Completions::Endpoints::GeminiInteractions do
)
end
+ it "maps Gemini 3.8 agent and LLM minimal thinking settings to low" do
+ model.update!(name: "gemini-3.8-flash", provider_params: { thinking_level: "minimal" })
+ 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,
+ )
+
+ expect(llm.generate("Think", user:)).to eq("Done")
+ expect(llm.generate("Think", user:, thinking_effort: "minimal")).to eq("Done")
+ expect(request_bodies.map { |body| body.dig(:generation_config, :thinking_level) }).to eq(
+ %w[low low],
+ )
+ end
+
+ it "strips deprecated agent sampling settings and unsupported extra parameters for Gemini 3.8" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
+ model.update!(name: "gemini-3.8-flash")
+ 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:,
+ temperature: 0.4,
+ top_p: 0.8,
+ extra_model_params: {
+ top_k: 40,
+ thinking_budget: 1_000,
+ },
+ ),
+ ).to eq("Done")
+ expect(request_body).not_to have_key(:generation_config)
+ end
+
it "omits the unsupported disabled thinking override for Gemini 2.5" do
request_body = nil
stub_request(:post, url).with(
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/google_vertex_ai_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/google_vertex_ai_spec.rb
index 36f2b879d50..bf42d9fb416 100644
--- a/plugins/discourse-ai/spec/lib/completions/endpoints/google_vertex_ai_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/google_vertex_ai_spec.rb
@@ -3,6 +3,8 @@
RSpec.describe DiscourseAi::Completions::Endpoints::GoogleVertexAi do
subject(:endpoint) { described_class.new(model) }
+ fab!(:user)
+
let(:model) do
Fabricate(
:llm_model,
@@ -57,6 +59,88 @@ RSpec.describe DiscourseAi::Completions::Endpoints::GoogleVertexAi do
expect(request["Authorization"]).to eq("Bearer vertex-token")
end
+ it "strips unsupported generation parameters for Gemini 3.8" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
+ model.update!(
+ name: "google/gemini-3.8-flash",
+ api_key: "vertex-token",
+ provider_params: {
+ project_id: "discourse-project",
+ region: "global",
+ },
+ )
+ request_body = nil
+ url =
+ "https://aiplatform.googleapis.com/v1/projects/discourse-project/locations/global/publishers/google/models/gemini-3.8-flash:generateContent"
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_body = JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body: {
+ candidates: [
+ { content: { parts: [{ text: "Done" }], role: "model" }, finishReason: "STOP" },
+ ],
+ }.to_json,
+ )
+
+ expect(
+ DiscourseAi::Completions::Llm.proxy(model).generate(
+ "Think",
+ user:,
+ temperature: 0.4,
+ top_p: 0.8,
+ extra_model_params: {
+ topK: 40,
+ thinkingConfig: {
+ thinkingBudget: 1_000,
+ },
+ },
+ ),
+ ).to eq("Done")
+ expect(request_body[:generationConfig]).to be_empty
+ end
+
+ it "omits legacy thinking budgets for Gemini 3.8" do
+ model.update!(
+ name: "google/gemini-3.8-flash",
+ api_key: "vertex-token",
+ provider_params: {
+ project_id: "discourse-project",
+ region: "global",
+ enable_thinking: true,
+ thinking_tokens: 10_000,
+ },
+ )
+ request_bodies = []
+ url =
+ "https://aiplatform.googleapis.com/v1/projects/discourse-project/locations/global/publishers/google/models/gemini-3.8-flash:generateContent"
+ stub_request(:post, url).with(
+ body:
+ proc do |body|
+ request_bodies << JSON.parse(body, symbolize_names: true)
+ true
+ end,
+ ).to_return(
+ status: 200,
+ body: {
+ candidates: [
+ { content: { parts: [{ text: "Done" }], role: "model" }, finishReason: "STOP" },
+ ],
+ }.to_json,
+ )
+
+ llm = DiscourseAi::Completions::Llm.proxy(model)
+ expect(llm.generate("Think", user:)).to eq("Done")
+ expect(llm.generate("Think", user:, thinking_effort: "none")).to eq("Done")
+ expect(request_bodies.map { |body| body[:generationConfig] }).to eq(
+ [{}, { thinkingConfig: { thinkingLevel: "low" } }],
+ )
+ end
+
describe "credential resolution" do
it "prefers a configured API key over environment credentials" do
model.update!(api_key: "configured-key")
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 45c156afbe1..9ac532eea80 100644
--- a/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb
@@ -5,41 +5,41 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
after { described_class.reset_cache! }
describe ".find_model" do
- it "carries provider_params through for models that need them, e.g. adaptive thinking" do
- model = described_class.find_model("anthropic", "claude-opus-4-7")
+ it "carries adaptive thinking parameters through for current Anthropic reasoning models" do
+ %w[claude-fable-5-1 claude-opus-5 claude-sonnet-5].each do |name|
+ model = described_class.find_model("anthropic", name)
- expect(model[:provider_params]).to eq(enable_reasoning: true, adaptive_thinking: true)
+ expect(model[:provider_params]).to eq(enable_reasoning: true, adaptive_thinking: true),
+ "expected #{name} preset to require adaptive thinking"
+ end
end
- it "omits provider_params for models that don't need any" do
- model = described_class.find_model("anthropic", "claude-sonnet-4-6")
+ it "omits provider parameters for models that do not need any" do
+ model = described_class.find_model("anthropic", "claude-haiku-4-5-20251001")
expect(model).not_to have_key(:provider_params)
end
- it "uses the Gemini Interactions API for all Google models" do
+ it "uses the beta 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",
+ endpoint: "https://generativelanguage.googleapis.com/v1beta/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)
+ expect(preset[:models].pluck(:name)).to eq(%w[gemini-3.1-pro-preview gemini-3.8-flash])
+ expect(preset[:models]).to all(satisfy { |model| !model.key?(:endpoint) })
end
- it "includes a Google Vertex AI preset" do
+ it "includes the current Google Vertex AI preset" do
preset = described_class.find_provider("google_vertex_ai")
model = preset[:models].first
expect(preset[:provider]).to eq("google_vertex_ai")
expect(preset[:tokenizer]).to eq(DiscourseAi::Tokenizer::GeminiTokenizer)
expect(model).to include(
- name: "google/gemini-3-flash",
- display_name: "Gemini 3 Flash (Vertex)",
+ name: "google/gemini-3.8-flash",
+ display_name: "Gemini 3.8 Flash (Vertex)",
)
expect(model[:provider_params]).to include(region: "global")
end
@@ -54,10 +54,10 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
tokens: 1_050_000,
display_name: "GPT-5.6 Sol",
max_output_tokens: 128_000,
- input_cost: 5.0,
- cached_input_cost: 0.50,
- cache_write_cost: 6.25,
- output_cost: 30.0,
+ input_cost: 4.0,
+ cached_input_cost: 0.40,
+ cache_write_cost: 5.0,
+ output_cost: 20.0,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
},
@@ -66,10 +66,10 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
tokens: 1_050_000,
display_name: "GPT-5.6 Terra",
max_output_tokens: 128_000,
- input_cost: 2.50,
- cached_input_cost: 0.25,
- cache_write_cost: 3.125,
- output_cost: 15.0,
+ input_cost: 2.0,
+ cached_input_cost: 0.20,
+ cache_write_cost: 2.50,
+ output_cost: 12.0,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
},
@@ -78,10 +78,10 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
tokens: 1_050_000,
display_name: "GPT-5.6 Luna",
max_output_tokens: 128_000,
- input_cost: 1.0,
- cached_input_cost: 0.10,
- cache_write_cost: 1.25,
- output_cost: 6.0,
+ input_cost: 0.20,
+ cached_input_cost: 0.02,
+ cache_write_cost: 0.25,
+ output_cost: 1.20,
vision_enabled: true,
endpoint: "https://api.openai.com/v1/responses",
},
@@ -89,12 +89,37 @@ RSpec.describe DiscourseAi::Completions::LlmPresets do
)
end
- it "marks every Anthropic model known to require adaptive-only thinking" do
- %w[claude-opus-4-7 claude-opus-4-8 claude-sonnet-5].each do |name|
- model = described_class.find_model("anthropic", name)
- expect(model[:provider_params]).to eq(enable_reasoning: true, adaptive_thinking: true),
- "expected #{name} preset to require adaptive thinking"
- end
+ it "includes the selected OpenRouter models and capabilities" do
+ preset = described_class.find_provider("open_router")
+
+ expect(preset[:models].pluck(:name)).to eq(
+ %w[
+ z-ai/glm-5.3-flash
+ deepseek/deepseek-v4-flash-vision-exp
+ tencent/hy4-preview
+ xiaomi/mimo-v2.5
+ moonshotai/kimi-k3
+ minimax/minimax-m3
+ x-ai/grok-4.6
+ ],
+ )
+ expect(
+ described_class.find_model("open_router", "deepseek/deepseek-v4-flash-vision-exp"),
+ ).to include(vision_enabled: true)
+ end
+ end
+
+ describe ".all" do
+ it "has an English description for every model" do
+ missing_keys =
+ described_class.all.flat_map do |preset|
+ preset[:models].filter_map do |model|
+ key = "#{preset[:id]}-#{model[:name]}".gsub(%r{[./:]}, "-")
+ key if !I18n.exists?("js.discourse_ai.llms.model_description.#{key}", :en)
+ end
+ end
+
+ expect(missing_keys).to be_empty
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
index 35ed1ac3014..f142dab9cfc 100644
--- a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
@@ -71,6 +71,29 @@ RSpec.describe DiscourseAi::Completions::Llm do
SSE
end
+ describe ".text_from_response" do
+ it "returns only text from heterogeneous completion responses" do
+ thinking = DiscourseAi::Completions::Thinking.new(message: "Private reasoning")
+ tool_call =
+ DiscourseAi::Completions::ToolCall.new(id: "tool-1", name: "search", parameters: {})
+ structured_output =
+ DiscourseAi::Completions::StructuredOutput.new(message: { type: "string" })
+ structured_output << '{"message":"Visible response"}'
+ structured_output.finish
+
+ expect(described_class.text_from_response(["Visible", thinking, " response"])).to eq(
+ "Visible response",
+ )
+ expect(described_class.text_from_response("Visible response")).to eq("Visible response")
+ expect(described_class.text_from_response(structured_output)).to eq(
+ '{"message":"Visible response"}',
+ )
+ expect(described_class.text_from_response(thinking)).to eq("")
+ expect(described_class.text_from_response(tool_call)).to eq("")
+ expect(described_class.text_from_response(nil)).to eq("")
+ end
+ end
+
describe ".proxy" do
it "raises for unknown model identifiers" do
expect { described_class.proxy("unknown:v2") }.to raise_error(described_class::UNKNOWN_MODEL)
diff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
index bd07c07cd12..322d89e30a1 100644
--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
@@ -16,7 +16,7 @@ RSpec.describe "Managing LLM configurations" do
it "correctly sets defaults" do
visit "/admin/plugins/discourse-ai/ai-llms"
- find("[data-llm-id='anthropic-claude-opus-4-7'] button").click()
+ find("[data-llm-id='anthropic-claude-opus-5'] button").click()
secret_selector = PageObjects::Components::SelectKit.new(".ai-secret-selector__dropdown")
secret_selector.expand
@@ -32,9 +32,9 @@ RSpec.describe "Managing LLM configurations" do
expect(llm.api_key).to eq("abcd")
preset = DiscourseAi::Completions::Llm.presets.find { |p| p[:id] == "anthropic" }
- model_preset = preset[:models].find { |m| m[:name] == "claude-opus-4-7" }
+ model_preset = preset[:models].find { |m| m[:name] == "claude-opus-5" }
- expect(llm.name).to eq("claude-opus-4-7")
+ expect(llm.name).to eq("claude-opus-5")
expect(llm.url).to eq(preset[:endpoint])
expect(llm.tokenizer).to eq(preset[:tokenizer].to_s)
expect(llm.max_prompt_tokens.to_i).to eq(model_preset[:tokens])