A platform for community discussion. Free, open, simple.
FEATURE: add AI agent thinking effort (#41280)
Sam834324206eae
diff --git a/db/structure.sql b/db/structure.sqlindex 936a2d1496e..b7c90cbf8ca 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -471,7 +471,8 @@ CREATE TABLE public.ai_agents (max_turn_tokens integer,compression_threshold integer,execution_mode character varying DEFAULT 'default'::character varying NOT NULL,- require_approval boolean DEFAULT false NOT NULL+ require_approval boolean DEFAULT false NOT NULL,+ thinking_effort character varying);@@ -22350,6 +22351,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groupsSET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES+('20260630034050'),('20260629233141'),('20260629081606'),('20260629022603'),diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjsindex d816dda0b62..92f0b0bc264 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs@@ -108,6 +108,22 @@ export default class AgentEditor extends Component {];}+ get thinkingEffortValues() {+ return [+ "default",+ "none",+ "minimal",+ "low",+ "medium",+ "high",+ "xhigh",+ "max",+ ].map((id) => ({+ id,+ name: i18n(`discourse_ai.ai_agent.thinking_effort_options.${id}`),+ }));+ }+get forcedToolStrategies() {const content = [{@@ -773,6 +789,23 @@ export default class AgentEditor extends Component {</form.Field>{{/if}}+ <form.Field+ @name="thinking_effort"+ @title={{i18n "discourse_ai.ai_agent.thinking_effort"}}+ @tooltip={{i18n "discourse_ai.ai_agent.thinking_effort_help"}}+ @format="large"+ @type="select"+ as |field|+ >+ <field.Control as |select|>+ {{#each this.thinkingEffortValues as |effort|}}+ <select.Option+ @value={{effort.id}}+ >{{effort.name}}</select.Option>+ {{/each}}+ </field.Control>+ </form.Field>+{{#if (this.showExamples data)}}<form.Section@title={{i18n "discourse_ai.ai_agent.examples.title"}}diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rbindex 032dbc9c34a..b75174ba29d 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_agents_controller.rb@@ -460,6 +460,7 @@ module DiscourseAi:priority,:top_p,:temperature,+ :thinking_effort,:default_llm_id,:user_id,:max_context_posts,diff --git a/plugins/discourse-ai/app/models/ai_agent.rb b/plugins/discourse-ai/app/models/ai_agent.rbindex 9c610bec8fd..aed1df08730 100644--- a/plugins/discourse-ai/app/models/ai_agent.rb+++ b/plugins/discourse-ai/app/models/ai_agent.rb@@ -39,6 +39,11 @@ class AiAgent < ActiveRecord::Base# leaves some room for growth but sets a maximum to avoid memory issues# we may want to revisit this in the futurevalidates :vision_max_pixels, numericality: { greater_than: 0, maximum: 4_000_000 }+ validates :thinking_effort,+ inclusion: {+ in: ["default", *DiscourseAi::Completions::ThinkingConfig::VALUES],+ },+ allow_nil: truevalidates :rag_chunk_tokens, numericality: { greater_than: 0, maximum: 50_000 }validates :rag_chunk_overlap_tokens, numericality: { greater_than: -1, maximum: 200 }@@ -249,6 +254,7 @@ class AiAgent < ActiveRecord::Basedescriptionallowed_group_idsshow_thinking+ thinking_effortenabledexecution_modemax_turn_tokens@@ -334,6 +340,7 @@ class AiAgent < ActiveRecord::Base# description/name are localizeddefine_singleton_method(key) { value } if key != :description && key != :nameend+ define_method(:thinking_effort) { self.class.thinking_effort }define_method(:options) { options }define_method(:native_tools) { native_tools }end@@ -363,6 +370,7 @@ class AiAgent < ActiveRecord::Basedefine_method(:options) { options }define_method(:temperature) { @ai_agent&.temperature }define_method(:top_p) { @ai_agent&.top_p }+ define_method(:thinking_effort) { @ai_agent&.thinking_effort }define_method(:system_prompt) { @ai_agent&.system_prompt || "You are a helpful bot." }define_method(:uploads) { @ai_agent&.uploads }define_method(:response_format) { @ai_agent&.response_format }@@ -518,6 +526,7 @@ end# system :boolean default(FALSE), not null# system_prompt :string(10000000) not null# temperature :float+# thinking_effort :string# tools :json not null# top_p :float# vision_enabled :boolean default(FALSE), not nulldiff --git a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rbindex 0059e58fa8e..53ac6bca43f 100644--- a/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb+++ b/plugins/discourse-ai/app/serializers/localized_ai_agent_serializer.rb@@ -13,6 +13,7 @@ class LocalizedAiAgentSerializer < ApplicationSerializer:system_prompt,:allowed_group_ids,:temperature,+ :thinking_effort,:top_p,:default_llm_id,:user_id,diff --git a/plugins/discourse-ai/app/services/discourse_ai/agent_exporter.rb b/plugins/discourse-ai/app/services/discourse_ai/agent_exporter.rbindex c795fa0e3f0..bc28dadd2b8 100644--- a/plugins/discourse-ai/app/services/discourse_ai/agent_exporter.rb+++ b/plugins/discourse-ai/app/services/discourse_ai/agent_exporter.rb@@ -53,6 +53,7 @@ module DiscourseAiexamples: ai_agent.examples,temperature: ai_agent.temperature,top_p: ai_agent.top_p,+ thinking_effort: ai_agent.thinking_effort,response_format: ai_agent.response_format,tools: transform_tools_for_export(ai_agent.tools, serialized_custom_tools),mcp_servers: serialize_mcp_servers(ai_agent),diff --git a/plugins/discourse-ai/app/services/discourse_ai/agent_importer.rb b/plugins/discourse-ai/app/services/discourse_ai/agent_importer.rbindex fe5bdc20e92..d476d8d32de 100644--- a/plugins/discourse-ai/app/services/discourse_ai/agent_importer.rb+++ b/plugins/discourse-ai/app/services/discourse_ai/agent_importer.rb@@ -41,6 +41,7 @@ module DiscourseAiexamples: agent_data["examples"],temperature: agent_data["temperature"],top_p: agent_data["top_p"],+ thinking_effort: agent_data["thinking_effort"],response_format: agent_data["response_format"],tools: transform_tools_for_import(agent_data["tools"], tool_name_to_id),}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.jsindex 2b9e091200f..ec134c5381c 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-agent.js@@ -14,6 +14,7 @@ const CREATE_ATTRIBUTES = ["priority","top_p","temperature",+ "thinking_effort","user_id","default_llm_id","force_default_llm",@@ -63,6 +64,7 @@ const SYSTEM_ATTRIBUTES = ["rag_conversation_chunks","rag_llm_model_id","show_thinking",+ "thinking_effort","allow_personal_messages","allow_topic_mentions","allow_chat_channel_mentions",@@ -167,6 +169,7 @@ export default class AiAgent extends RestModel {attrs.mcp_server_ids = attrs.mcp_server_ids || [];attrs.mcp_server_tool_names = attrs.mcp_server_tool_names || {};attrs.forced_tool_count = this.forced_tool_count || -1;+ attrs.thinking_effort = attrs.thinking_effort || "default";attrs.response_format = attrs.response_format || [];attrs.examples = attrs.examples || [];// FormKit uses Immer proxies which cause issues when passed to upload handlers.diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 0865a42da09..79d163fdfed 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -662,6 +662,17 @@ en:temperature_help: "Temperature to use for the LLM. Increase to increase creativity (leave empty to use model default, generally a value from 0.0 to 2.0)"top_p: "Top P"top_p_help: "Top P to use for the LLM, increase to increase randomness (leave empty to use model default, generally a value from 0.0 to 1.0)"+ thinking_effort: "Thinking effort"+ thinking_effort_help: "How much reasoning-capable models should think before answering. Higher values may improve difficult tasks but can increase latency and cost. Unsupported providers ignore this setting."+ thinking_effort_options:+ default: "Model default"+ none: "None"+ minimal: "Minimal"+ low: "Low"+ medium: "Medium"+ high: "High"+ xhigh: "Extra high"+ max: "Max"priority: "Priority"priority_help: "Priority agents are displayed to users at the top of the agent list. If multiple agents have priority, they will be sorted alphabetically."tool_options: "Tool options"diff --git a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rbindex 2f1d15aeb73..dc322a2c05e 100644--- a/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb+++ b/plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb@@ -113,5 +113,9 @@ DiscourseAi::Agents::Agent.system_agents.each do |agent_class, id|agent.system_prompt = instance.system_promptagent.top_p = instance.top_pagent.temperature = instance.temperature+ # Only seed the shipped default when the admin has never set one, so we don't+ # clobber a per-agent choice on every deploy (thinking_effort is DB-driven and+ # editable in the UI, unlike temperature/top_p which are code-owned).+ agent.thinking_effort = instance.thinking_effort if agent.thinking_effort.nil?agent.save!(validate: false)enddiff --git a/plugins/discourse-ai/db/migrate/20260630034050_add_thinking_effort_to_ai_agents.rb b/plugins/discourse-ai/db/migrate/20260630034050_add_thinking_effort_to_ai_agents.rbnew file mode 100644index 00000000000..0d24386b86d--- /dev/null+++ b/plugins/discourse-ai/db/migrate/20260630034050_add_thinking_effort_to_ai_agents.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddThinkingEffortToAiAgents < ActiveRecord::Migration[8.0]+ def change+ add_column :ai_agents, :thinking_effort, :string, null: true+ end+enddiff --git a/plugins/discourse-ai/lib/agents/agent.rb b/plugins/discourse-ai/lib/agents/agent.rbindex 4bf13182e2b..d33ac00c761 100644--- a/plugins/discourse-ai/lib/agents/agent.rb+++ b/plugins/discourse-ai/lib/agents/agent.rb@@ -275,6 +275,10 @@ module DiscourseAinilend+ def thinking_effort+ nil+ end+def options{}enddiff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex bb9a80030d6..7155e5f6dfe 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -91,6 +91,7 @@ module DiscourseAillm_kwargs[:user] = userllm_kwargs[:temperature] = agent.temperature if agent.temperaturellm_kwargs[:top_p] = agent.top_p if agent.top_p+ llm_kwargs[:thinking_effort] = agent.thinking_effort if agent.thinking_effort.present?if !context.bypass_response_format && agent.response_format.present?llm_kwargs[:response_format] = build_json_schema(agent.response_format)diff --git a/plugins/discourse-ai/lib/agents/creative.rb b/plugins/discourse-ai/lib/agents/creative.rbindex 30a54015949..a11b9dcc03b 100644--- a/plugins/discourse-ai/lib/agents/creative.rb+++ b/plugins/discourse-ai/lib/agents/creative.rb@@ -3,6 +3,10 @@module DiscourseAimodule Agentsclass Creative < Agent+ def thinking_effort+ "low"+ end+def tools[]enddiff --git a/plugins/discourse-ai/lib/agents/discourse_helper.rb b/plugins/discourse-ai/lib/agents/discourse_helper.rbindex ca04b3708b6..6b4fcd5c758 100644--- a/plugins/discourse-ai/lib/agents/discourse_helper.rb+++ b/plugins/discourse-ai/lib/agents/discourse_helper.rb@@ -3,6 +3,10 @@module DiscourseAimodule Agentsclass DiscourseHelper < Agent+ def thinking_effort+ "low"+ end+def tools[Tools::DiscourseMetaSearch]enddiff --git a/plugins/discourse-ai/lib/agents/forum_researcher.rb b/plugins/discourse-ai/lib/agents/forum_researcher.rbindex 18edabfe2fd..3f654a205bf 100644--- a/plugins/discourse-ai/lib/agents/forum_researcher.rb+++ b/plugins/discourse-ai/lib/agents/forum_researcher.rb@@ -7,6 +7,10 @@ module DiscourseAifalseend+ def thinking_effort+ "high"+ end+def tools[Tools::Researcher]enddiff --git a/plugins/discourse-ai/lib/agents/general.rb b/plugins/discourse-ai/lib/agents/general.rbindex a7a88cbfca6..0ccdf2c3543 100644--- a/plugins/discourse-ai/lib/agents/general.rb+++ b/plugins/discourse-ai/lib/agents/general.rb@@ -3,6 +3,10 @@module DiscourseAimodule Agentsclass General < Agent+ def thinking_effort+ "low"+ end+def toolsbase_tools = [Tools::Search,diff --git a/plugins/discourse-ai/lib/agents/sql_helper.rb b/plugins/discourse-ai/lib/agents/sql_helper.rbindex 6dca26aa685..3f77681fce8 100644--- a/plugins/discourse-ai/lib/agents/sql_helper.rb+++ b/plugins/discourse-ai/lib/agents/sql_helper.rb@@ -3,6 +3,10 @@module DiscourseAimodule Agentsclass SqlHelper < Agent+ def thinking_effort+ "medium"+ end+def self.schemareturn @schema if defined?(@schema)diff --git a/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb b/plugins/discourse-ai/lib/agents/tool_runner/discourse.rbindex 19893066ddd..a538f415d48 100644--- a/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb+++ b/plugins/discourse-ai/lib/agents/tool_runner/discourse.rb@@ -390,6 +390,7 @@ module DiscourseAi"enabled","system_prompt","temperature",+ "thinking_effort","top_p","vision_enabled","tools",@@ -429,6 +430,11 @@ module DiscourseAiendallowed_updates[:top_p] = updates["top_p"] if updates["top_p"].is_a?(Numeric)+ if DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ updates["thinking_effort"],+ ) || updates["thinking_effort"] == "default"+ allowed_updates[:thinking_effort] = updates["thinking_effort"]+ endif updates["description"].present?allowed_updates[:description] = updates["description"]@@ -450,6 +456,7 @@ module DiscourseAi"enabled","system_prompt","temperature",+ "thinking_effort","top_p",),}diff --git a/plugins/discourse-ai/lib/agents/tool_runner/llm.rb b/plugins/discourse-ai/lib/agents/tool_runner/llm.rbindex e9a986bd70e..9c70b63fc5f 100644--- a/plugins/discourse-ai/lib/agents/tool_runner/llm.rb+++ b/plugins/discourse-ai/lib/agents/tool_runner/llm.rb@@ -32,6 +32,7 @@ module DiscourseAitemperature: options["temperature"],top_p: options["top_p"],max_tokens: options["max_tokens"],+ thinking_effort: options["thinking_effort"],stop_sequences: options["stop_sequences"],)enddiff --git a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rbindex 0b27c3e0cd6..171b6d67455 100644--- a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb+++ b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb@@ -30,7 +30,7 @@ module DiscourseAidef max_prompt_tokens# provide a buffer of 120 tokens - our function counting is not# 100% accurate and getting numbers to align exactly is very hard- buffer = (opts[:max_tokens] || 2500) + 50+ buffer = (opts[:reserved_output_tokens] || opts[:max_tokens] || 2500) + 50if tools.present?# note this is about 100 tokens over, OpenAI have a more optimal representationdiff --git a/plugins/discourse-ai/lib/completions/dialects/claude.rb b/plugins/discourse-ai/lib/completions/dialects/claude.rbindex fda9f4e9968..bea92ebb05b 100644--- a/plugins/discourse-ai/lib/completions/dialects/claude.rb+++ b/plugins/discourse-ai/lib/completions/dialects/claude.rb@@ -71,7 +71,7 @@ module DiscourseAienddef max_prompt_tokens- llm_model.max_prompt_tokens+ max_prompt_tokens_with_reserved_outputenddef native_tool_support?diff --git a/plugins/discourse-ai/lib/completions/dialects/converse.rb b/plugins/discourse-ai/lib/completions/dialects/converse.rbindex fb5aefc58d5..54575f790e1 100644--- a/plugins/discourse-ai/lib/completions/dialects/converse.rb+++ b/plugins/discourse-ai/lib/completions/dialects/converse.rb@@ -56,7 +56,7 @@ module DiscourseAienddef max_prompt_tokens- llm_model.max_prompt_tokens+ max_prompt_tokens_with_reserved_outputenddef native_tool_support?diff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rbindex 53c1e75f8c8..f5919608969 100644--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb@@ -187,6 +187,13 @@ module DiscourseAiraise NotImplementedend+ def max_prompt_tokens_with_reserved_output+ reserved_output_tokens = opts[:reserved_output_tokens].to_i+ return llm_model.max_prompt_tokens if reserved_output_tokens <= 0++ [llm_model.max_prompt_tokens - reserved_output_tokens, 0].max+ end+attr_reader :promptprivatediff --git a/plugins/discourse-ai/lib/completions/dialects/nova.rb b/plugins/discourse-ai/lib/completions/dialects/nova.rbindex 5197d4c1c47..0db7f116f3c 100644--- a/plugins/discourse-ai/lib/completions/dialects/nova.rb+++ b/plugins/discourse-ai/lib/completions/dialects/nova.rb@@ -66,7 +66,7 @@ module DiscourseAienddef max_prompt_tokens- llm_model.max_prompt_tokens+ max_prompt_tokens_with_reserved_outputenddef native_tool_support?diff --git a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rbindex 699e322d5b1..dea4aed5795 100644--- a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb+++ b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb@@ -16,7 +16,7 @@ module DiscourseAienddef max_prompt_tokens- buffer = (opts[:max_tokens] || 2500) + 50+ buffer = (opts[:reserved_output_tokens] || opts[:max_tokens] || 2500) + 50if tools.present?@function_size ||= llm_model.tokenizer_class.size(tools.to_json.to_s)diff --git a/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb b/plugins/discourse-ai/lib/completions/endpoints/anthropic.rbindex 7056e328896..dba3477adab 100644--- a/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/anthropic.rb@@ -42,28 +42,12 @@ module DiscourseAimax_tokens = 8192 if mapped_model.match?(/3.[57]/)options = { model: mapped_model, max_tokens: max_tokens }-- if llm_model.lookup_custom_param("adaptive_thinking")- options[:thinking] = { type: "adaptive" }- options[:max_tokens] = 32_000- elsif llm_model.lookup_custom_param("enable_reasoning")- reasoning_tokens =- llm_model.lookup_custom_param("reasoning_tokens").to_i.clamp(1024, 32_768)-- # this allows for lots of tokens beyond reasoning- options[:max_tokens] = reasoning_tokens + 30_000- options[:thinking] = { type: "enabled", budget_tokens: reasoning_tokens }- end+ apply_anthropic_thinking_config!(options)+ apply_anthropic_effort_config!(options)options[:stop_sequences] = ["</function_calls>"] if !dialect.native_tool_support? &&dialect.prompt.has_tools?- # effort parameter- effort = llm_model.lookup_custom_param("effort")- options[:output_config] = { effort: effort } if AnthropicShared::EFFORT_VALUES.include?(- effort,- )-optionsenddiff --git a/plugins/discourse-ai/lib/completions/endpoints/anthropic_shared.rb b/plugins/discourse-ai/lib/completions/endpoints/anthropic_shared.rbindex a40fb6c26cc..aed8ec26743 100644--- a/plugins/discourse-ai/lib/completions/endpoints/anthropic_shared.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/anthropic_shared.rb@@ -5,17 +5,24 @@ module DiscourseAimodule Endpointsmodule AnthropicSharedEFFORT_VALUES = %w[low medium high xhigh max].freeze+ THINKING_BUDGETS = {+ "minimal" => 1024,+ "low" => 4096,+ "medium" => 8192,+ "high" => 16_384,+ "xhigh" => 32_768,+ "max" => 32_768,+ }.freeze+ DEFAULT_VISIBLE_OUTPUT_TOKENS = 30_000+ DEFAULT_ADAPTIVE_OUTPUT_TOKENS = 32_000+ MIN_THINKING_BUDGET = 1024+ MIN_VISIBLE_OUTPUT_TOKENS = 1024def normalize_model_params(model_params)model_params = model_params.dup- thinking_enabled =- llm_model.lookup_custom_param("adaptive_thinking") ||- llm_model.lookup_custom_param("enable_reasoning")-- if thinking_enabled- model_params.delete(:temperature)- model_params.delete(:top_p)+ if thinking_config.present? && thinking_config.enabled?+ strip_sampling_params_for_thinking!(model_params)elsemodel_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")if llm_model.lookup_custom_param("disable_temperature")@@ -30,6 +37,58 @@ module DiscourseAiAiApiAuditLog::Provider::Anthropicend+ def resolve_thinking_config(model_params)+ effort =+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ model_params[:thinking_effort],+ )++ if effort.blank?+ provider_param_config = provider_param_thinking_config(model_params)+ return provider_param_config if provider_param_config+ return DiscourseAi::Completions::ThinkingConfig.disabled+ end++ if !supports_anthropic_thinking?+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ return DiscourseAi::Completions::ThinkingConfig.explicit_none if effort == "none"++ if requires_adaptive_thinking?+ total_output_tokens = adaptive_total_output_tokens(model_params)+ return(+ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: effort,+ enabled: true,+ provider_effort: "adaptive",+ # output_config.effort only accepts low/medium/high/xhigh/max —+ # no "minimal" — same collapse as the OpenAI effort scale.+ output_effort: effort == "minimal" ? "low" : effort,+ provider_output_tokens: total_output_tokens,+ reserved_output_tokens: total_output_tokens,+ strip_temperature: true,+ strip_top_p: true,+ )+ )+ end++ budget = THINKING_BUDGETS[effort]+ if budget.blank?+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ config =+ budget_thinking_config(+ canonical_effort: effort,+ budget: budget,+ model_params: model_params,+ )+ return config if config++ DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end+def xml_tags_to_strip(dialect)if dialect.prompt.has_tools?%w[thinking search_quality_reflection search_quality_score]@@ -40,6 +99,139 @@ module DiscourseAiprivate+ def supports_anthropic_thinking?+ true+ end++ def requires_adaptive_thinking?+ llm_model.lookup_custom_param("adaptive_thinking")+ end++ def provider_param_thinking_config(model_params)+ return if !supports_anthropic_thinking?++ if llm_model.lookup_custom_param("adaptive_thinking")+ total_output_tokens = adaptive_total_output_tokens(model_params)+ return(+ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: "adaptive",+ enabled: true,+ provider_effort: "adaptive",+ provider_output_tokens: total_output_tokens,+ reserved_output_tokens: total_output_tokens,+ strip_temperature: true,+ strip_top_p: true,+ )+ )+ end++ if llm_model.lookup_custom_param("enable_reasoning")+ budget = llm_model.lookup_custom_param("reasoning_tokens").to_i.clamp(1024, 32_768)+ config =+ budget_thinking_config(+ canonical_effort: "custom",+ budget: budget,+ model_params: model_params,+ )+ return config if config++ DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: "custom")+ end+ end++ def adaptive_total_output_tokens(model_params)+ requested_output_tokens = model_params[:max_tokens].presence&.to_i+ output_token_limit = llm_model.max_output_tokens.to_i++ if output_token_limit.positive?+ if requested_output_tokens&.positive?+ [requested_output_tokens, output_token_limit].min+ else+ output_token_limit+ end+ else+ if requested_output_tokens&.positive?+ requested_output_tokens+ else+ DEFAULT_ADAPTIVE_OUTPUT_TOKENS+ end+ end+ end++ def budget_thinking_config(canonical_effort:, budget:, model_params:)+ requested_visible_output_tokens = model_params[:max_tokens].presence&.to_i+ output_token_limit = llm_model.max_output_tokens.to_i++ if output_token_limit.positive?+ provider_output_tokens = output_token_limit+ return if provider_output_tokens <= MIN_THINKING_BUDGET++ max_visible_output_tokens = provider_output_tokens - MIN_THINKING_BUDGET+ visible_output_floor = [MIN_VISIBLE_OUTPUT_TOKENS, max_visible_output_tokens].min++ visible_output_tokens = provider_output_tokens - budget+ visible_output_tokens =+ requested_visible_output_tokens if requested_visible_output_tokens&.positive?+ visible_output_tokens = [visible_output_tokens, visible_output_floor].max+ visible_output_tokens = [visible_output_tokens, max_visible_output_tokens].min++ budget = provider_output_tokens - visible_output_tokens+ return if budget < MIN_THINKING_BUDGET+ else+ visible_output_tokens = requested_visible_output_tokens || DEFAULT_VISIBLE_OUTPUT_TOKENS+ provider_output_tokens = visible_output_tokens + budget+ end++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: canonical_effort,+ enabled: true,+ thinking_token_budget: budget,+ visible_output_tokens: visible_output_tokens,+ provider_output_tokens: provider_output_tokens,+ reserved_output_tokens: provider_output_tokens,+ strip_temperature: true,+ strip_top_p: true,+ )+ end++ def apply_anthropic_effort_config!(options)+ # a per-call thinking_effort that resolved to adaptive mode takes priority+ # over the static admin-configured "effort" param+ return if thinking_config.present? && thinking_config.output_effort.present?++ effort = llm_model.lookup_custom_param("effort")+ options[:output_config] = { effort: effort } if AnthropicShared::EFFORT_VALUES.include?(+ effort,+ )+ end++ def apply_anthropic_thinking_config!(options)+ @thinking_config ||=+ provider_param_thinking_config({}) || DiscourseAi::Completions::ThinkingConfig.disabled+ return if thinking_config.blank? || thinking_config.unsupported?++ if thinking_config.explicit_none?+ options.delete(:thinking)+ return+ end++ if thinking_config.provider_effort == "adaptive"+ options[:thinking] = { type: "adaptive" }+ if thinking_config.output_effort.present?+ options[:output_config] = { effort: thinking_config.output_effort }+ end+ elsif thinking_config.thinking_token_budget+ options[:thinking] = {+ type: "enabled",+ budget_tokens: thinking_config.thinking_token_budget,+ }+ end++ return if thinking_config.provider_output_tokens.blank?++ options[:max_tokens] = thinking_config.provider_output_tokens+ end+def prompt_size(prompt)tokenizer.size(prompt.system_prompt.to_s + " " + prompt.messages.to_s)end@@ -85,6 +277,7 @@ module DiscourseAidefault_options(dialect).merge(model_params.except(:response_format)).merge(messages: prompt.messages,)+ apply_anthropic_thinking_config!(payload)if prompt.has_tools?payload[:tools] = prompt.toolsdiff --git a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rbindex bc352b5f7ac..8bb6d783131 100644--- a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock.rb@@ -20,27 +20,9 @@ module DiscourseAimax_tokens = 4096max_tokens = 8192 if bedrock_model_id.match?(/3.[57]/)- result = { anthropic_version: "bedrock-2023-05-31" }- if llm_model.lookup_custom_param("adaptive_thinking")- max_tokens = 32_000- result[:thinking] = { type: "adaptive" }- elsif llm_model.lookup_custom_param("enable_reasoning")- # we require special headers to go over 64k output tokens, lets- # wait for feature requests before enabling this- reasoning_tokens =- llm_model.lookup_custom_param("reasoning_tokens").to_i.clamp(1024, 32_768)-- # this allows for ample tokens beyond reasoning- max_tokens = reasoning_tokens + 30_000- result[:thinking] = { type: "enabled", budget_tokens: reasoning_tokens }- end- result[:max_tokens] = max_tokens-- # effort parameter- effort = llm_model.lookup_custom_param("effort")- if AnthropicShared::EFFORT_VALUES.include?(effort)- result[:output_config] = { effort: effort }- end+ result = { anthropic_version: "bedrock-2023-05-31", max_tokens: max_tokens }+ apply_anthropic_thinking_config!(result)+ apply_anthropic_effort_config!(result)resultelse@@ -54,6 +36,10 @@ module DiscourseAiprivate+ def supports_anthropic_thinking?+ bedrock_model_id.include?("anthropic") || bedrock_model_id.include?("claude")+ end+def bedrock_model_idcase llm_model.namewhen "claude-2"diff --git a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rbindex e73b4eb5e02..73ea3481534 100644--- a/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/aws_bedrock_converse.rb@@ -4,6 +4,8 @@ module DiscourseAimodule Completionsmodule Endpointsclass AwsBedrockConverse < Base+ include AnthropicShared+def self.can_contact?(llm_model)llm_model.provider == "aws_bedrock_converse"end@@ -60,6 +62,8 @@ module DiscourseAimax_tokens = enforce_max_output_tokens(model_params[:max_tokens])model_params[:max_tokens] = max_tokens if max_tokens+ @thinking_config ||= resolve_thinking_config(model_params)+ model_params = apply_thinking_config_to_model_params(model_params)model_params = normalize_model_params(model_params)prompt = dialect.translate@@ -81,7 +85,7 @@ module DiscourseAiraw_response = +""log = nil- sdk_params = build_converse_params(prompt, model_params, dialect)+ sdk_params = build_converse_params(prompt, provider_model_params(model_params), dialect)# Image bytes in sdk_params are raw binary (ASCII-8BIT) and cannot be# encoded as UTF-8 JSON for logging. Fall back to a placeholder so the# request can still proceed when binary payloads are present.@@ -237,21 +241,14 @@ module DiscourseAiraise if !cancelledend+ def supports_anthropic_thinking?+ llm_model.name.include?("anthropic") || llm_model.name.include?("claude")+ end+def default_options(dialect)options = {}-- if llm_model.lookup_custom_param("adaptive_thinking")- options[:thinking] = { type: "adaptive" }- elsif llm_model.lookup_custom_param("enable_reasoning")- reasoning_tokens =- llm_model.lookup_custom_param("reasoning_tokens").to_i.clamp(1024, 32_768)- options[:thinking] = { type: "enabled", budget_tokens: reasoning_tokens }- end-- effort = llm_model.lookup_custom_param("effort")- if AnthropicShared::EFFORT_VALUES.include?(effort)- options[:output_config] = { effort: effort }- end+ apply_anthropic_thinking_config!(options) if supports_anthropic_thinking?+ apply_anthropic_effort_config!(options) if supports_anthropic_thinking?optionsend@@ -261,13 +258,8 @@ module DiscourseAidef normalize_model_params(model_params)model_params = model_params.dup- thinking_enabled =- llm_model.lookup_custom_param("adaptive_thinking") ||- llm_model.lookup_custom_param("enable_reasoning")-- if thinking_enabled- model_params.delete(:temperature)- model_params.delete(:top_p)+ if thinking_config.present? && thinking_config.enabled?+ strip_sampling_params_for_thinking!(model_params)elsemodel_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")if llm_model.lookup_custom_param("disable_temperature")@@ -315,6 +307,7 @@ module DiscourseAidef build_converse_params(prompt, model_params, dialect)options = default_options(dialect).merge(model_params.except(:response_format))+ apply_anthropic_thinking_config!(options)params = { model_id: llm_model.name, messages: prompt.messages }diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rbindex 3412a61ae49..638d90c2a1f 100644--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb@@ -4,7 +4,7 @@ module DiscourseAimodule Completionsmodule Endpointsclass Base- attr_reader :partial_tool_calls, :output_thinking+ attr_reader :partial_tool_calls, :output_thinking, :thinking_configCompletionFailed = Class.new(StandardError)FAIL_THRESHOLD = 5@@ -101,6 +101,11 @@ module DiscourseAi@llm_model = llm_modelend+ def prepare_model_params(model_params)+ @thinking_config = resolve_thinking_config(model_params)+ apply_thinking_config_to_model_params(model_params.dup)+ end+def enforce_max_output_tokens(value)if @llm_model.max_output_tokens.to_i > 0value = @llm_model.max_output_tokens if (value.to_i > @llm_model.max_output_tokens) ||@@ -149,6 +154,8 @@ module DiscourseAimax_tokens = enforce_max_output_tokens(model_params[:max_tokens])model_params[:max_tokens] = max_tokens if max_tokens+ @thinking_config ||= resolve_thinking_config(model_params)+ model_params = apply_thinking_config_to_model_params(model_params)model_params = normalize_model_params(model_params)if block_given? && disable_streaming?@@ -251,7 +258,8 @@ module DiscourseAiretried = falsenext_attempt_delay_ms = 0@forced_json_through_prefill = false- request_body = prepare_payload(prompt, model_params, dialect).to_json+ request_body =+ prepare_payload(prompt, provider_model_params(model_params), dialect).to_jsonlog =start_completion_log(request_body: request_body,@@ -552,6 +560,33 @@ module DiscourseAiraise NotImplementedErrorend+ def resolve_thinking_config(_model_params)+ DiscourseAi::Completions::ThinkingConfig.disabled+ end++ def apply_thinking_config_to_model_params(model_params)+ return model_params if thinking_config.blank?++ if thinking_config.reserved_output_tokens+ model_params[:reserved_output_tokens] = thinking_config.reserved_output_tokens+ end++ model_params+ end++ def thinking_configured?+ thinking_config.present? && !thinking_config.unsupported? &&+ (thinking_config.enabled? || thinking_config.explicit_none?)+ end++ def strip_sampling_params_for_thinking!(model_params)+ return model_params if thinking_config.blank?++ model_params.delete(:temperature) if thinking_config.strip_temperature?+ model_params.delete(:top_p) if thinking_config.strip_top_p?+ model_params+ end+def model_uriraise NotImplementedErrorend@@ -560,6 +595,10 @@ module DiscourseAiraise NotImplementedErrorend+ def provider_model_params(model_params)+ model_params.except(:thinking_effort, :reserved_output_tokens, :provider_output_tokens)+ end+def prepare_request(_payload)raise NotImplementedErrorenddiff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini.rbindex d13ef5bf0fd..97619f7b162 100644--- a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb@@ -7,6 +7,19 @@ module DiscourseAiGEMINI_PROVIDER_KEY = :geminiGROUNDING_METADATA_KEYS = %i[webSearchQueries groundingChunks groundingSupports]THOUGHT_SIGNATURE_PROVIDER_KEY = :thought_signature_parts+ THINKING_LEVELS = %w[minimal low medium high].freeze+ THINKING_LEVEL_BY_EFFORT = {+ "minimal" => "minimal",+ "low" => "low",+ "medium" => "medium",+ "high" => "high",+ "xhigh" => "high",+ "max" => "high",+ }.freeze+ THINKING_LEVEL_WITHOUT_MINIMAL_BY_EFFORT =+ THINKING_LEVEL_BY_EFFORT.merge("minimal" => "low").freeze+ LOW_HIGH_THINKING_LEVEL_BY_EFFORT =+ THINKING_LEVEL_WITHOUT_MINIMAL_BY_EFFORT.merge("medium" => "high").freezedef self.can_contact?(llm_model)llm_model.provider == "google"@@ -40,9 +53,7 @@ module DiscourseAimodel_params[:topP] = model_params.delete(:top_p) if model_params[:top_p]- thinking_enabled =- %w[minimal low medium high].include?(llm_model.lookup_custom_param("thinking_level")) ||- llm_model.lookup_custom_param("enable_thinking")+ thinking_enabled = thinking_config.present? && thinking_config.enabled?if thinking_enabledmodel_params.delete(:temperature)@@ -59,6 +70,39 @@ module DiscourseAiAiApiAuditLog::Provider::Geminiend+ def resolve_thinking_config(model_params)+ effort =+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ model_params[:thinking_effort],+ )++ if effort.blank?+ legacy_config = legacy_thinking_config+ return legacy_config if legacy_config+ return DiscourseAi::Completions::ThinkingConfig.disabled+ end++ if effort == "none"+ # 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.+ return DiscourseAi::Completions::ThinkingConfig.disabled if always_thinking_model?+ return DiscourseAi::Completions::ThinkingConfig.explicit_none+ end++ provider_effort = thinking_level_for_effort(effort)+ if provider_effort.blank?+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: effort,+ provider_effort: provider_effort,+ enabled: true,+ strip_temperature: true,+ )+ end+privatedef model_uri@@ -125,16 +169,9 @@ module DiscourseAiendend- thinking_level = llm_model.lookup_custom_param("thinking_level")- if %w[minimal low medium high].include?(thinking_level)- payload[:generationConfig][:thinkingConfig] = { thinkingLevel: thinking_level }- elsif llm_model.lookup_custom_param("enable_thinking")- thinking_tokens = llm_model.lookup_custom_param("thinking_tokens").to_i- thinking_tokens = thinking_tokens.clamp(0, 24_576)- payload[:generationConfig][:thinkingConfig] = { thinkingBudget: thinking_tokens }- end+ apply_thinking_config!(payload)- if @include_thought_summaries+ if @include_thought_summaries && !thinking_config&.explicit_none?payload[:generationConfig][:thinkingConfig] ||= {}payload[:generationConfig][:thinkingConfig][:includeThoughts] = trueend@@ -144,6 +181,82 @@ module DiscourseAipayloadend+ def thinking_level_for_effort(effort)+ return if !THINKING_LEVEL_BY_EFFORT.key?(effort)++ if gemini_3_pro_preview_model?+ LOW_HIGH_THINKING_LEVEL_BY_EFFORT[effort]+ elsif supports_minimal_thinking_level?+ THINKING_LEVEL_BY_EFFORT[effort]+ elsif gemini_3_model?+ THINKING_LEVEL_WITHOUT_MINIMAL_BY_EFFORT[effort]+ end+ end++ def gemini_3_pro_preview_model?+ gemini_model_id.include?("gemini-3-pro") && !gemini_model_id.include?("gemini-3.1-pro")+ end++ def supports_minimal_thinking_level?+ gemini_model_id.include?("gemini-3-flash") ||+ gemini_model_id.include?("gemini-3.1-flash-lite")+ end++ def gemini_3_model?+ gemini_model_id.include?("gemini-3")+ end++ def always_thinking_model?+ gemini_3_model? && gemini_model_id.include?("-pro")+ end++ def gemini_model_id+ @gemini_model_id ||= [llm_model.name, llm_model.url].compact.join(" ")+ end++ def legacy_thinking_config+ thinking_level = llm_model.lookup_custom_param("thinking_level")+ provider_effort = thinking_level_for_effort(thinking_level)+ if provider_effort+ return(+ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: thinking_level,+ provider_effort: provider_effort,+ enabled: true,+ strip_temperature: true,+ )+ )+ end++ if llm_model.lookup_custom_param("enable_thinking")+ 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?++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: "custom",+ enabled: true,+ thinking_token_budget: thinking_tokens,+ strip_temperature: true,+ )+ end+ end++ def apply_thinking_config!(payload)+ return if thinking_config.blank? || thinking_config.unsupported?++ if thinking_config.explicit_none?+ payload[:generationConfig][:thinkingConfig] = { thinkingBudget: 0 }+ elsif thinking_config.provider_effort.present?+ payload[:generationConfig][:thinkingConfig] = {+ thinkingLevel: thinking_config.provider_effort,+ }+ elsif thinking_config.thinking_token_budget+ payload[:generationConfig][:thinkingConfig] = {+ thinkingBudget: thinking_config.thinking_token_budget,+ }+ end+ end+def service_tierreturn @service_tier if defined?(@service_tier)diff --git a/plugins/discourse-ai/lib/completions/endpoints/open_ai_shared.rb b/plugins/discourse-ai/lib/completions/endpoints/open_ai_shared.rbindex c783bd2724d..4aa24f5e046 100644--- a/plugins/discourse-ai/lib/completions/endpoints/open_ai_shared.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/open_ai_shared.rb@@ -4,6 +4,13 @@ module DiscourseAimodule Completionsmodule Endpointsmodule OpenAiShared+ OPEN_AI_REASONING_EFFORTS = %w[none minimal low medium high xhigh].freeze+ OPEN_AI_REASONING_OUTPUT_RESERVATION = 25_000+ # "minimal" isn't a distinct tier on some OpenAI reasoning models (e.g.+ # gpt-5.5 rejects it outright) — collapse it into "low", same as vLLM's+ # own reasoning effort scale already does.+ OPEN_AI_EFFORT_ALIASES = { "max" => "xhigh", "minimal" => "low" }.freeze+def default_options{ model: llm_model.name }end@@ -35,9 +42,8 @@ module DiscourseAimodel_params[:stop] = model_params.delete(:stop_sequences)end- if reasoning_effort- model_params.delete(:temperature)- model_params.delete(:top_p)+ if thinking_configured?+ strip_sampling_params_for_thinking!(model_params)elsemodel_params.delete(:top_p) if llm_model.lookup_custom_param("disable_top_p")if llm_model.lookup_custom_param("disable_temperature")@@ -54,13 +60,56 @@ module DiscourseAi@disable_streaming ||= llm_model.lookup_custom_param("disable_streaming")end+ def resolve_thinking_config(model_params)+ effort =+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ model_params[:thinking_effort],+ )+ effort ||= legacy_reasoning_effort++ return DiscourseAi::Completions::ThinkingConfig.disabled if effort.blank?++ provider_effort = OPEN_AI_EFFORT_ALIASES[effort] || effort+ if !OPEN_AI_REASONING_EFFORTS.include?(provider_effort)+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ output_reservation = open_ai_reasoning_output_reservation(model_params, provider_effort)++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: effort,+ provider_effort: provider_effort,+ enabled: provider_effort != "none",+ explicit_none: provider_effort == "none",+ reserved_output_tokens: output_reservation,+ strip_temperature: provider_effort != "none",+ strip_top_p: provider_effort != "none",+ )+ end++ def open_ai_reasoning_output_reservation(model_params, provider_effort)+ return if provider_effort == "none"++ requested_output_tokens = model_params[:max_tokens].presence&.to_i+ return requested_output_tokens if requested_output_tokens&.positive?++ model_output_tokens = llm_model.max_output_tokens.to_i+ return model_output_tokens if model_output_tokens.positive?++ OPEN_AI_REASONING_OUTPUT_RESERVATION+ end+def reasoning_effort- return @reasoning_effort if defined?(@reasoning_effort)- @reasoning_effort = llm_model.lookup_custom_param("reasoning_effort")- @reasoning_effort = nil if !%w[none minimal low medium high xhigh].include?(- @reasoning_effort,+ thinking_config&.provider_effort+ end++ def legacy_reasoning_effort+ return @legacy_reasoning_effort if defined?(@legacy_reasoning_effort)+ @legacy_reasoning_effort = llm_model.lookup_custom_param("reasoning_effort")+ @legacy_reasoning_effort = nil if !OPEN_AI_REASONING_EFFORTS.include?(+ @legacy_reasoning_effort,)- @reasoning_effort+ @legacy_reasoning_effortenddef service_tierdiff --git a/plugins/discourse-ai/lib/completions/endpoints/open_router.rb b/plugins/discourse-ai/lib/completions/endpoints/open_router.rbindex 1a0c68fba6a..fd2a11e4e33 100644--- a/plugins/discourse-ai/lib/completions/endpoints/open_router.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/open_router.rb@@ -4,10 +4,33 @@ module DiscourseAimodule Completionsmodule Endpointsclass OpenRouter < OpenAi+ OPEN_ROUTER_REASONING_EFFORTS = %w[none minimal low medium high xhigh max].freeze+def self.can_contact?(llm_model)llm_model.provider == "open_router"end+ def resolve_thinking_config(model_params)+ effort =+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ model_params[:thinking_effort],+ )+ effort ||= legacy_reasoning_effort++ return DiscourseAi::Completions::ThinkingConfig.disabled if effort.blank?++ if !OPEN_ROUTER_REASONING_EFFORTS.include?(effort)+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: effort,+ provider_effort: effort,+ enabled: effort != "none",+ explicit_none: effort == "none",+ )+ end+def normalize_model_params(model_params)model_params = model_params.dup@@ -35,6 +58,8 @@ module DiscourseAidef prepare_payload(prompt, model_params, dialect)payload = super+ payload.delete(:reasoning_effort)+ payload[:reasoning] = { effort: thinking_config.provider_effort } if thinking_configured?if quantizations = llm_model.provider_params["provider_quantizations"].presenceoptions = quantizations.split(",").map(&:strip)@@ -50,6 +75,13 @@ module DiscourseAipayloadend++ private++ def legacy_reasoning_effort+ effort = llm_model.lookup_custom_param("reasoning_effort")+ effort if OPEN_ROUTER_REASONING_EFFORTS.include?(effort)+ endendendenddiff --git a/plugins/discourse-ai/lib/completions/endpoints/vllm.rb b/plugins/discourse-ai/lib/completions/endpoints/vllm.rbindex 6654c021857..7bd58f2bba9 100644--- a/plugins/discourse-ai/lib/completions/endpoints/vllm.rb+++ b/plugins/discourse-ai/lib/completions/endpoints/vllm.rb@@ -7,6 +7,15 @@ module DiscourseAiENABLE_THINKING_PARSERS = %w[qwen3 gemma4 deepseek_v4].freezeTHINKING_PARSERS = %w[granite deepseek_v3 holo2].freezeVLLM_REASONING_EFFORTS = %w[none low medium high].freeze+ VLLM_REASONING_EFFORT_BY_CANONICAL = {+ "none" => "none",+ "minimal" => "low",+ "low" => "low",+ "medium" => "medium",+ "high" => "high",+ "xhigh" => "high",+ "max" => "high",+ }.freezedef self.can_contact?(llm_model)llm_model.provider == "vllm"@@ -71,6 +80,35 @@ module DiscourseAiresult.concat(processor.finish)end+ def resolve_thinking_config(model_params)+ effort =+ DiscourseAi::Completions::ThinkingConfig.normalize_effort(+ model_params[:thinking_effort],+ )++ if effort.present?+ provider_effort = VLLM_REASONING_EFFORT_BY_CANONICAL[effort]+ else+ provider_effort = raw_custom_param("reasoning_effort")+ effort = provider_effort+ end++ return DiscourseAi::Completions::ThinkingConfig.disabled if effort.blank?++ if provider_effort.blank? || !VLLM_REASONING_EFFORTS.include?(provider_effort)+ return DiscourseAi::Completions::ThinkingConfig.unsupported(canonical_effort: effort)+ end++ DiscourseAi::Completions::ThinkingConfig.new(+ canonical_effort: effort,+ provider_effort: provider_effort,+ enabled: provider_effort != "none",+ explicit_none: provider_effort == "none",+ strip_temperature: provider_effort != "none",+ strip_top_p: provider_effort != "none",+ )+ end+privatedef prepare_payload(prompt, model_params, dialect)@@ -134,11 +172,7 @@ module DiscourseAienddef reasoning_effort- return @reasoning_effort if defined?(@reasoning_effort)-- @reasoning_effort = raw_custom_param("reasoning_effort")- @reasoning_effort = nil if !VLLM_REASONING_EFFORTS.include?(@reasoning_effort)- @reasoning_effort+ thinking_config&.provider_effortenddef raw_custom_param(key)diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rbindex 8cf9d11ba85..f0244d603fd 100644--- a/plugins/discourse-ai/lib/completions/llm.rb+++ b/plugins/discourse-ai/lib/completions/llm.rb@@ -134,6 +134,7 @@ module DiscourseAi# @param feature_context { Hash - Optional } - The feature context to use for the completion.# @param partial_tool_calls { Boolean - Optional } - If true, the completion will return partial tool calls.# @param output_thinking { Boolean - Optional } - If true, the completion will return the thinking output for thinking models.+ # @param thinking_effort { String - Optional } - Provider-agnostic per-call thinking effort override.# @param response_format { Hash - Optional } - JSON schema passed to the API as the desired structured output.# @param [Experimental] extra_model_params { Hash - Optional } - Other params that are not available accross models. e.g. response_format JSON schema.# @param execution_context { DiscourseAi::Completions::ExecutionContext - Optional } - Explicit per-call context for token tracking and audit logging.@@ -155,6 +156,7 @@ module DiscourseAipartial_tool_calls: false,output_thinking: false,response_format: nil,+ thinking_effort: nil,extra_model_params: nil,cancel_manager: nil,execution_context: nil,@@ -173,11 +175,16 @@ module DiscourseAipartial_tool_calls: partial_tool_calls,output_thinking: output_thinking,response_format: response_format,+ thinking_effort: thinking_effort,extra_model_params: extra_model_params,},)- model_params = { max_tokens: max_tokens, stop_sequences: stop_sequences }+ model_params = {+ max_tokens: max_tokens,+ stop_sequences: stop_sequences,+ thinking_effort: thinking_effort,+ }if SiteSetting.ai_llm_temperature_top_p_enabledmodel_params[:temperature] = temperature if temperature@@ -208,9 +215,12 @@ module DiscourseAimodel_params.keys.each { |key| model_params.delete(key) if model_params[key].nil? }+ gateway = @gateway || gateway_klass.new(llm_model)+ model_params = gateway.prepare_model_params(model_params) if gateway.respond_to?(+ :prepare_model_params,+ )dialect = dialect_klass.new(prompt, llm_model, opts: model_params)- gateway = @gateway || gateway_klass.new(llm_model)gateway.perform_completion!(dialect,user,diff --git a/plugins/discourse-ai/lib/completions/llm_presets.rb b/plugins/discourse-ai/lib/completions/llm_presets.rbindex 6808327d749..a68edd98626 100644--- a/plugins/discourse-ai/lib/completions/llm_presets.rb+++ b/plugins/discourse-ai/lib/completions/llm_presets.rb@@ -32,6 +32,23 @@ module DiscourseAi{id: "anthropic",models: [+ model(+ name: "claude-opus-4-8",+ tokens: 1_000_000,+ display_name: "Claude Opus 4.8",+ max_output_tokens: 128_000,+ input_cost: 5.0,+ cached_input_cost: 0.50,+ cache_write_cost: 6.25,+ output_cost: 25.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",tokens: 1_000_000,@@ -42,6 +59,26 @@ module DiscourseAicache_write_cost: 6.25,output_cost: 25.0,vision_enabled: true,+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ },+ ),+ model(+ 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,+ 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",@@ -227,7 +264,8 @@ module DiscourseAicache_write_cost: nil,output_cost: nil,vision_enabled: false,- endpoint: nil+ endpoint: nil,+ provider_params: nil)result = { name: name, tokens: tokens, display_name: display_name }result[:max_output_tokens] = max_output_tokens if max_output_tokens@@ -237,6 +275,7 @@ module DiscourseAiresult[:output_cost] = output_cost if output_costresult[:vision_enabled] = vision_enabled if vision_enabledresult[:endpoint] = endpoint if endpoint+ result[:provider_params] = provider_params if provider_paramsresultendenddiff --git a/plugins/discourse-ai/lib/completions/thinking_config.rb b/plugins/discourse-ai/lib/completions/thinking_config.rbnew file mode 100644index 00000000000..57723591a14--- /dev/null+++ b/plugins/discourse-ai/lib/completions/thinking_config.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++module DiscourseAi+ module Completions+ class ThinkingConfig+ VALUES = %w[none minimal low medium high xhigh max].freeze++ attr_reader :canonical_effort,+ :provider_effort,+ :output_effort,+ :thinking_token_budget,+ :visible_output_tokens,+ :provider_output_tokens,+ :reserved_output_tokens++ def self.normalize_effort(value)+ value = value.to_s.strip+ return if value.blank? || value == "default"+ value if VALUES.include?(value)+ end++ def self.disabled(canonical_effort: nil)+ new(canonical_effort: canonical_effort, enabled: false)+ end++ def self.explicit_none+ new(canonical_effort: "none", enabled: false, explicit_none: true)+ end++ def self.unsupported(canonical_effort: nil)+ new(canonical_effort: canonical_effort, enabled: false, unsupported: true)+ end++ def initialize(+ canonical_effort: nil,+ provider_effort: nil,+ output_effort: nil,+ enabled: false,+ explicit_none: false,+ unsupported: false,+ thinking_token_budget: nil,+ visible_output_tokens: nil,+ provider_output_tokens: nil,+ reserved_output_tokens: nil,+ strip_temperature: false,+ strip_top_p: false+ )+ @canonical_effort = canonical_effort+ @provider_effort = provider_effort+ @output_effort = output_effort+ @enabled = enabled+ @explicit_none = explicit_none+ @unsupported = unsupported+ @thinking_token_budget = thinking_token_budget+ @visible_output_tokens = visible_output_tokens+ @provider_output_tokens = provider_output_tokens+ @reserved_output_tokens = reserved_output_tokens+ @strip_temperature = strip_temperature+ @strip_top_p = strip_top_p+ end++ def enabled?+ @enabled+ end++ def explicit_none?+ @explicit_none+ end++ def unsupported?+ @unsupported+ end++ def strip_temperature?+ @strip_temperature+ end++ def strip_top_p?+ @strip_top_p+ end+ end+ end+enddiff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rbindex 60da5352f18..74309804b7b 100644--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb@@ -30,7 +30,7 @@ RSpec.describe DiscourseAi::Agents::Bot dolet(:llm_responses) { [function_call, response] }describe "#reply" do- it "sets top_p and temperature params" do+ it "sets top_p, temperature, and thinking_effort params" doSiteSetting.ai_llm_temperature_top_p_enabled = trueDiscourseAi::Completions::Endpoints::Fake.delays = []DiscourseAi::Completions::Endpoints::Fake.last_call = nil@@ -43,6 +43,7 @@ RSpec.describe DiscourseAi::Agents::Bot doname: "TestAgent",top_p: 0.5,temperature: 0.4,+ thinking_effort: "high",system_prompt: "test",description: "test",allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],@@ -56,8 +57,11 @@ RSpec.describe DiscourseAi::Agents::Bot do) { |_partial, _cancel, _placeholder| }last_call = DiscourseAi::Completions::Endpoints::Fake.last_call- expect(last_call[:model_params][:top_p]).to eq(0.5)- expect(last_call[:model_params][:temperature]).to eq(0.4)+ expect(last_call[:model_params]).to include(+ top_p: 0.5,+ temperature: 0.4,+ thinking_effort: "high",+ )endit "requests Gemini thought summaries when thinking is shown" dodiff --git a/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rbindex 5b56fb23ec2..9e18b803700 100644--- a/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb@@ -7,6 +7,16 @@ RSpec.describe DiscourseAi::Completions::Dialects::Claude dobefore { enable_current_plugin }+ describe "#max_prompt_tokens" do+ it "reserves configured output tokens" do+ llm_model.update!(max_prompt_tokens: 100_000)+ prompt = DiscourseAi::Completions::Prompt.new("You are a helpful bot", messages: [])+ dialect = opus_dialect_klass.new(prompt, llm_model, opts: { reserved_output_tokens: 32_000 })++ expect(dialect.max_prompt_tokens).to eq(68_000)+ end+ end+describe "#translate" doit "can insert OKs to make stuff interleve properly" domessages = [diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rbindex eb0431e07be..baaa29b8c6a 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/anthropic_spec.rb@@ -534,6 +534,186 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic doexpect(parsed_body).not_to have_key(:top_p)end+ it "configures adaptive thinking with effort" do+ model.update!(+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ effort: "medium",+ },+ )++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)++ expect(parsed_body[:thinking]).to eq(type: "adaptive")+ expect(parsed_body[:output_config]).to eq(effort: "medium")+ expect(parsed_body[:max_tokens]).to eq(32_000)+ end++ it "uses adaptive thinking for models that only support it, with the per-call effort" do+ model.update!(+ name: "claude-opus-4-7",+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ },+ )++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "response" }],+ model: "claude-opus-4-7",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user, thinking_effort: "high")++ expect(parsed_body[:thinking]).to eq(type: "adaptive")+ expect(parsed_body[:output_config]).to eq(effort: "high")+ expect(parsed_body).not_to have_key(:budget_tokens)+ end++ it "collapses minimal to low in output_config.effort for adaptive-only models, since Anthropic rejects minimal there" do+ model.update!(+ name: "claude-opus-4-7",+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ },+ )++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "response" }],+ model: "claude-opus-4-7",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user, thinking_effort: "minimal")++ expect(parsed_body[:output_config]).to eq(effort: "low")+ end++ it "honors the adaptive_thinking custom param as an escape hatch even with an explicit thinking_effort" do+ model.update!(provider_params: { enable_reasoning: true, adaptive_thinking: true })++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user, thinking_effort: "medium")++ expect(parsed_body[:thinking]).to eq(type: "adaptive")+ expect(parsed_body[:output_config]).to eq(effort: "medium")+ end++ it "lets a per-call adaptive thinking_effort win over the static effort custom param" do+ model.update!(+ name: "claude-opus-4-7",+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ effort: "low",+ },+ )++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "response" }],+ model: "claude-opus-4-7",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user, thinking_effort: "xhigh")++ expect(parsed_body[:output_config]).to eq(effort: "xhigh")+ end+it "strips temperature and top_p when adaptive thinking is enabled" domodel.update!(provider_params: { enable_reasoning: true, adaptive_thinking: true })@@ -1129,9 +1309,89 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic dollm.generate(prompt, user: Discourse.system_user, max_tokens: 2500)expect(parsed_body[:max_tokens]).to eq(2000)+ expect(parsed_body[:thinking]).to eq(type: "enabled", budget_tokens: 1024)llm.generate(prompt, user: Discourse.system_user)expect(parsed_body[:max_tokens]).to eq(2000)+ expect(parsed_body[:thinking]).to eq(type: "enabled", budget_tokens: 1024)+ end++ it "respects model max output tokens with adaptive thinking" do+ model.update!(+ provider_params: {+ enable_reasoning: true,+ adaptive_thinking: true,+ },+ max_output_tokens: 2000,+ )++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user)++ expect(parsed_body[:thinking]).to eq(type: "adaptive")+ expect(parsed_body[:max_tokens]).to eq(2000)+ end++ it "clamps thinking effort budget below the model max output tokens" do+ model.update!(max_output_tokens: 8192)++ parsed_body = nil+ stub_request(:post, url).with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ headers: {+ "Content-Type" => "application/json",+ "X-Api-Key" => "123",+ "Anthropic-Version" => "2023-06-01",+ },+ ).to_return(+ status: 200,+ body: {+ id: "msg_123",+ type: "message",+ role: "assistant",+ content: [{ type: "text", text: "test response" }],+ model: "claude-3-opus-20240229",+ usage: {+ input_tokens: 10,+ output_tokens: 5,+ },+ }.to_json,+ )++ llm.generate(prompt, user: Discourse.system_user, thinking_effort: "high")++ expect(parsed_body[:max_tokens]).to eq(8192)+ expect(parsed_body[:thinking]).to eq(type: "enabled", budget_tokens: 7168)endenddiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rbindex 8950de19072..6ceb04bed00 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/aws_bedrock_converse_spec.rb@@ -169,6 +169,54 @@ RSpec.describe DiscourseAi::Completions::Endpoints::AwsBedrockConverse doexpect(AiApiAuditLog.last.response_tokens).to eq(8)end+ it "passes thinking config for Claude models" do+ response = mock_converse_response+ client = stub_sdk_client(response: response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user, thinking_effort: "low")++ expect(client).to have_received(:converse) do |params|+ expect(params.dig(:additional_model_request_fields, :thinking)).to eq(+ type: "enabled",+ budget_tokens: 4096,+ )+ end+ end++ it "uses adaptive thinking for Claude models that only support it" do+ model.update!(+ name: "global.anthropic.claude-opus-4-7-v1:0",+ provider_params:+ model.provider_params.merge("enable_reasoning" => true, "adaptive_thinking" => true),+ )+ response = mock_converse_response+ client = stub_sdk_client(response: response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user, thinking_effort: "high")++ expect(client).to have_received(:converse) do |params|+ expect(params.dig(:additional_model_request_fields, :thinking)).to eq(type: "adaptive")+ expect(params.dig(:additional_model_request_fields, :output_config)).to eq(effort: "high")+ end+ end++ it "does not pass Anthropic thinking config for non-Claude models" do+ SiteSetting.ai_llm_temperature_top_p_enabled = true+ model.update!(name: "meta.llama3-1-70b-instruct-v1:0")+ response = mock_converse_response+ client = stub_sdk_client(response: response)++ llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")+ llm.generate("hello", user: user, thinking_effort: "low", temperature: 0.7)++ expect(client).to have_received(:converse) do |params|+ expect(params[:additional_model_request_fields]).to be_blank+ expect(params.dig(:inference_config, :temperature)).to eq(0.7)+ end+ end+it "passes model name directly as model_id to SDK" doresponse = mock_converse_responseclient = stub_sdk_client(response: response)diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rbindex b3441a5c123..fbe3bba600e 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb@@ -505,7 +505,14 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doendit "correctly configures thinking level when set" do- model.update!(provider_params: { enable_thinking: true, thinking_level: "high" })+ model.update!(+ name: "gemini-3-flash",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",+ provider_params: {+ enable_thinking: true,+ thinking_level: "high",+ },+ )response = gemini_mock.response("Using thinking level").to_jsonreq_body = nil@@ -528,7 +535,14 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doendit "requests Gemini thought summaries when enabled" do- model.update!(provider_params: { enable_thinking: true, thinking_level: "medium" })+ model.update!(+ name: "gemini-3-flash",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",+ provider_params: {+ enable_thinking: true,+ thinking_level: "medium",+ },+ )response = gemini_mock.response("Using thought summaries").to_jsonreq_body = nil@@ -628,6 +642,8 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doit "thinking_level takes priority over enable_thinking" domodel.update!(+ name: "gemini-3-flash",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",provider_params: {enable_thinking: true,thinking_level: "medium",@@ -678,6 +694,160 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doexpect(parsed.dig(:generationConfig, :thinkingConfig)).to be_nilend+ it "maps thinking_effort to Gemini 3 thinking levels" do+ model.update!(+ name: "gemini-3-flash",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",+ )++ response = gemini_mock.response("Using thinking effort").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate("Hello", user: user, thinking_effort: "xhigh", temperature: 0.5)+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to eq({ thinkingLevel: "high" })+ expect(parsed.dig(:generationConfig, :temperature)).to be_nil+ end++ it "maps minimal thinking_effort to low for Gemini 3.1 Pro" do+ model.update!(+ name: "gemini-3.1-pro",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview",+ )++ response = gemini_mock.response("Using minimal effort").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate("Hello", user: user, thinking_effort: "minimal")+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to eq({ thinkingLevel: "low" })+ end++ it "maps unsupported Gemini 3 Pro thinking levels to supported levels" do+ model.update!(+ name: "gemini-3-pro",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-preview",+ )++ response = gemini_mock.response("Using medium effort").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate("Hello", user: user, thinking_effort: "medium")+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to eq({ thinkingLevel: "high" })+ end++ it "uses thinkingBudget 0 for explicit none thinking_effort" do+ response = gemini_mock.response("No thinking").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate(+ "Hello",+ user: user,+ thinking_effort: "none",+ output_thinking: true,+ extra_model_params: {+ include_thought_summaries: true,+ },+ )+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to eq({ thinkingBudget: 0 })+ end++ it "omits thinkingConfig entirely for none on Gemini 3 Pro-tier models that can't disable thinking" do+ model.update!(+ name: "gemini-3.1-pro",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview",+ )+ response = gemini_mock.response("Still thinking").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate("Hello", user: user, thinking_effort: "none")+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to be_nil+ end++ it "does not send thinking levels to pre-Gemini 3 generateContent models" do+ response = gemini_mock.response("Unsupported thinking level").to_json+ req_body = nil++ llm = DiscourseAi::Completions::Llm.proxy(model)+ url = "#{model.url}:generateContent?key=123"++ stub_request(:post, url).with(+ body:+ proc do |_req_body|+ req_body = _req_body+ true+ end,+ ).to_return(status: 200, body: response)++ llm.generate("Hello", user: user, thinking_effort: "high")+ parsed = JSON.parse(req_body, symbolize_names: true)++ expect(parsed.dig(:generationConfig, :thinkingConfig)).to be_nil+ end+it "adds configured service tier using the Gemini API field name" doresponse = gemini_mock.response("Configured service tier").to_jsonurl = "#{model.url}:generateContent?key=123"@@ -754,7 +924,14 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doendit "strips temperature when thinking_level is set" do- model.update!(provider_params: { enable_thinking: true, thinking_level: "high" })+ model.update!(+ name: "gemini-3-flash",+ url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview",+ provider_params: {+ enable_thinking: true,+ thinking_level: "high",+ },+ )response = gemini_mock.response("Stripped temp").to_jsonreq_body = nildiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_responses_api_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_responses_api_spec.rbindex 5add48ac6c3..705c353f96a 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_responses_api_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_responses_api_spec.rb@@ -420,7 +420,7 @@ data: {"type":"response.reasoning_summary_part.added","sequence_number":3,"item_endit "uses reasoning object format for responses API" do- model.update!(provider_params: { reasoning_effort: "minimal" })+ model.update!(provider_params: { reasoning_effort: "medium" })parsed_body = nilstub_request(:post, "https://api.openai.com/v1/responses").with(@@ -441,7 +441,7 @@ data: {"type":"response.reasoning_summary_part.added","sequence_number":3,"item_endpoint.perform_completion!(dialect, Discourse.system_user)- expect(parsed_body[:reasoning]).to include(effort: "minimal", summary: "auto")+ expect(parsed_body[:reasoning]).to include(effort: "medium", summary: "auto")expect(parsed_body).not_to have_key(:reasoning_effort)expect(parsed_body).to have_key(:input)enddiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rbindex 7180f2d7c46..3766d35244d 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rb@@ -1163,6 +1163,21 @@ TEXTexpect(parsed_body[:reasoning_effort]).to eq("xhigh")end+ it "collapses minimal to low since some models reject it outright" do+ parsed_body = nil+ stub_request(:post, "https://api.openai.com/v1/chat/completions").with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(status: 200, body: { choices: [{ message: { content: "test" } }] }.to_json)++ endpoint.perform_completion!(dialect, user, { thinking_effort: "minimal" })++ expect(parsed_body[:reasoning_effort]).to eq("low")+ end+it "strips temperature and top_p when reasoning_effort is set" domodel.update!(provider_params: { reasoning_effort: "low" })@@ -1182,6 +1197,44 @@ TEXTexpect(parsed_body).not_to have_key(:top_p)end+ it "uses per-call thinking_effort before model reasoning_effort" do+ model.update!(provider_params: { reasoning_effort: "low" })++ parsed_body = nil+ stub_request(:post, "https://api.openai.com/v1/chat/completions").with(+ body:+ proc do |req_body|+ parsed_body = JSON.parse(req_body, symbolize_names: true)+ true+ end,+ ).to_return(status: 200, body: { choices: [{ message: { content: "test" } }] }.to_json)++ endpoint.perform_completion!(dialect, user, { thinking_effort: "max", temperature: 0.7 })++ expect(parsed_body[:reasoning_effort]).to eq("xhigh")+ expect(parsed_body).not_to have_key(:temperature)+ end++ it "reserves OpenAI recommended output room for reasoning when no token cap is configured" do+ prepared_params = endpoint.prepare_model_params(thinking_effort: "high")++ expect(prepared_params[:reserved_output_tokens]).to eq(25_000)+ end++ it "uses the model output limit as the reasoning output reservation when configured" do+ model.update!(max_output_tokens: 12_000)++ prepared_params = endpoint.prepare_model_params(thinking_effort: "high")++ expect(prepared_params[:reserved_output_tokens]).to eq(12_000)+ end++ it "does not reserve output room when reasoning is explicitly disabled" do+ prepared_params = endpoint.prepare_model_params(thinking_effort: "none")++ expect(prepared_params).not_to have_key(:reserved_output_tokens)+ end+it "omits reasoning parameters when not configured" doparsed_body = nilstub_request(:post, "https://api.openai.com/v1/chat/completions").with(diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/open_router_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/open_router_spec.rbindex e9f84bb2adf..6e376caf9af 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/open_router_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/open_router_spec.rb@@ -47,6 +47,42 @@ RSpec.describe DiscourseAi::Completions::Endpoints::OpenRouter doexpect(parsed_body).to eq(expected)end+ it "sends thinking effort using OpenRouter reasoning parameter" do+ parsed_body = nil+ stub_request(:post, open_router_model.url).with(+ body: proc { |body| parsed_body = JSON.parse(body, symbolize_names: true) },+ ).to_return(+ status: 200,+ body: { "choices" => [message: { role: "assistant", content: "reasoned" }] }.to_json,+ )++ proxy = DiscourseAi::Completions::Llm.proxy(open_router_model)+ result = proxy.generate("hello", user: user, thinking_effort: "max")++ expect(result).to eq("reasoned")+ expect(parsed_body[:reasoning]).to eq(effort: "max")+ expect(parsed_body).not_to have_key(:reasoning_effort)+ end++ it "preserves legacy reasoning_effort custom parameter using OpenRouter reasoning parameter" do+ open_router_model.update!(provider_params: { reasoning_effort: "high" })++ parsed_body = nil+ stub_request(:post, open_router_model.url).with(+ body: proc { |body| parsed_body = JSON.parse(body, symbolize_names: true) },+ ).to_return(+ status: 200,+ body: { "choices" => [message: { role: "assistant", content: "legacy" }] }.to_json,+ )++ proxy = DiscourseAi::Completions::Llm.proxy(open_router_model)+ result = proxy.generate("hello", user: user)++ expect(result).to eq("legacy")+ expect(parsed_body[:reasoning]).to eq(effort: "high")+ expect(parsed_body).not_to have_key(:reasoning_effort)+ end+it "excludes disabled parameters from the request" doopen_router_model.update!(provider_params: { disable_top_p: true, disable_temperature: true })diff --git a/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rbnew file mode 100644index 00000000000..455d73ac71e--- /dev/null+++ b/plugins/discourse-ai/spec/lib/completions/llm_presets_spec.rb@@ -0,0 +1,28 @@+# frozen_string_literal: true++RSpec.describe DiscourseAi::Completions::LlmPresets do+ before { described_class.reset_cache! }+ 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")++ expect(model[:provider_params]).to eq(enable_reasoning: true, adaptive_thinking: true)+ end++ it "omits provider_params for models that don't need any" do+ model = described_class.find_model("anthropic", "claude-sonnet-4-6")++ expect(model).not_to have_key(:provider_params)+ 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+ end+ end+enddiff --git a/plugins/discourse-ai/spec/models/ai_agent_spec.rb b/plugins/discourse-ai/spec/models/ai_agent_spec.rbindex 433e405e6cf..897b49aae36 100644--- a/plugins/discourse-ai/spec/models/ai_agent_spec.rb+++ b/plugins/discourse-ai/spec/models/ai_agent_spec.rb@@ -16,6 +16,44 @@ RSpec.describe AiAgent dobefore { enable_current_plugin }+ it "exposes system agent thinking effort on class instances" do+ agent_record =+ AiAgent.find(DiscourseAi::Agents::Agent.system_agents[DiscourseAi::Agents::Creative])+ agent_record.update!(thinking_effort: "max")++ agent = agent_record.class_instance.new++ expect(agent.thinking_effort).to eq("max")+ end++ it "declares a default thinking effort for reasoning-enabled system agents" do+ {+ DiscourseAi::Agents::Creative => "low",+ DiscourseAi::Agents::General => "low",+ DiscourseAi::Agents::DiscourseHelper => "low",+ DiscourseAi::Agents::SqlHelper => "medium",+ DiscourseAi::Agents::ForumResearcher => "high",+ }.each do |klass, effort|+ expect(klass.new.thinking_effort).to eq(effort),+ "expected #{klass} to default to #{effort.inspect} thinking effort, got #{klass.new.thinking_effort.inspect}"+ end+ end++ it "seeds the default thinking effort on deploy without clobbering admin choices" do+ creative_id = DiscourseAi::Agents::Agent.system_agents[DiscourseAi::Agents::Creative]+ general_id = DiscourseAi::Agents::Agent.system_agents[DiscourseAi::Agents::General]++ # an agent that was never configured, and one an admin has customized+ AiAgent.where(id: creative_id).update_all(thinking_effort: nil)+ AiAgent.where(id: general_id).update_all(thinking_effort: "high")++ # load (not require_relative) so the seeding script actually re-executes here+ load Rails.root.join("plugins/discourse-ai/db/fixtures/agents/603_ai_agents.rb") # rubocop:disable Discourse/Plugins/UseRequireRelative++ expect(AiAgent.find(creative_id).thinking_effort).to eq("low") # seeded default+ expect(AiAgent.find(general_id).thinking_effort).to eq("high") # admin choice preserved+ end+it "validates context settings" doexpect(basic_agent.valid?).to eq(true)diff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rbindex 9cc6fd397db..f2541b9f35c 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb@@ -273,6 +273,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController dotools: [["search", { "base_query" => "test" }, true]],top_p: 0.1,temperature: 0.5,+ thinking_effort: "high",allow_topic_mentions: true,allow_personal_messages: true,allow_chat_channel_mentions: true,@@ -305,6 +306,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent_json["name"]).to eq("superbot")expect(agent_json["top_p"]).to eq(0.1)expect(agent_json["temperature"]).to eq(0.5)+ expect(agent_json["thinking_effort"]).to eq("high")expect(agent_json["default_llm_id"]).to eq(llm_model.id)expect(agent_json["forced_tool_count"]).to eq(2)expect(agent_json["execution_mode"]).to eq("agentic")@@ -328,6 +330,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent.ai_agent_mcp_servers.first.selected_tool_names).to eq(["search_issues"])expect(agent.top_p).to eq(0.1)expect(agent.temperature).to eq(0.5)+ expect(agent.thinking_effort).to eq("high")}.to change(AiAgent, :count).by(1)end@@ -738,6 +741,25 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(response.parsed_body["errors"].join).not_to include("en.discourse")end+ it "allows editing thinking effort and refreshes the cached agent class" do+ agent_id = DiscourseAi::Agents::Agent.system_agents.values.first+ cached_agent = AiAgent.all_agents(enabled_only: false).find { |agent| agent.id == agent_id }+ expect(cached_agent.thinking_effort).not_to eq("high")++ put "/admin/plugins/discourse-ai/ai-agents/#{agent_id}.json",+ params: {+ ai_agent: {+ thinking_effort: "high",+ },+ }++ expect(response).to be_successful+ expect(AiAgent.find(agent_id).thinking_effort).to eq("high")++ cached_agent = AiAgent.all_agents(enabled_only: false).find { |agent| agent.id == agent_id }+ expect(cached_agent.thinking_effort).to eq("high")+ end+it "does allow some actions" doput "/admin/plugins/discourse-ai/ai-agents/#{DiscourseAi::Agents::Agent.system_agents.values.first}.json",params: {@@ -790,6 +812,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do],temperature: 0.8,top_p: 0.9,+ thinking_effort: "high",response_format: [{ type: "string", key: "summary" }],examples: [["user example", "assistant example"]],default_llm_id: llm_model.id,@@ -813,6 +836,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent_data["system_prompt"]).to eq("You are a tool master")expect(agent_data["temperature"]).to eq(0.8)expect(agent_data["top_p"]).to eq(0.9)+ expect(agent_data["thinking_effort"]).to eq("high")expect(agent_data["response_format"]).to eq([{ "type" => "string", "key" => "summary" }])expect(agent_data["examples"]).to eq([["user example", "assistant example"]])@@ -866,6 +890,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController dosystem_prompt: "You are an imported assistant",temperature: 0.7,top_p: 0.8,+ thinking_effort: "medium",response_format: [{ type: "string", key: "answer" }],examples: [["hello", "hi there"]],tools: ["SearchCommand", ["ReadCommand", { max_length: 1000 }, true]],@@ -888,6 +913,7 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController doexpect(agent.system_prompt).to eq("You are an imported assistant")expect(agent.temperature).to eq(0.7)expect(agent.top_p).to eq(0.8)+ expect(agent.thinking_effort).to eq("medium")expect(agent.response_format).to eq([{ "type" => "string", "key" => "answer" }])expect(agent.examples).to eq([["hello", "hi there"]])expect(agent.tools).to eq(["SearchCommand", ["ReadCommand", { "max_length" => 1000 }, true]])