Skip to content

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

FIX: properly terminate chains of tool calls across multiple providers (#36750)

Samf930e0ea98b3
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rb
index e786d90503d..1f6268a4a0b 100644
--- a/plugins/discourse-ai/app/models/llm_model.rb
+++ b/plugins/discourse-ai/app/models/llm_model.rb
@@ -126,7 +126,7 @@ class LlmModel < ActiveRecord::Base
disable_native_tools: :checkbox,
reasoning_effort: {
type: :enum,
- values: %w[default minimal low medium high],
+ values: %w[default minimal low medium high xhigh],
default: "default",
},
disable_temperature: :checkbox,
diff --git a/plugins/discourse-ai/config/eval-llms.yml b/plugins/discourse-ai/config/eval-llms.yml
index 0c43dcc6c9f..d279009e690 100644
--- a/plugins/discourse-ai/config/eval-llms.yml
+++ b/plugins/discourse-ai/config/eval-llms.yml
@@ -1,40 +1,29 @@
llms:
- o3:
- display_name: O3
- name: o3
+ gpt-5-nano:
+ display_name: GPT-5-Nano
+ name: gpt-5-nano
tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer
api_key_env: OPENAI_API_KEY
provider: open_ai
url: https://api.openai.com/v1/chat/completions
max_prompt_tokens: 131072
vision_enabled: true
- provider_params:
- disable_top_p: true
- disable_temperature: true
- gpt-41:
- display_name: GPT-4.1
- name: gpt-4.1
+ gpt-5-nano-thinking:
+ display_name: GPT-5-Nano Thinking
+ name: gpt-5-nano
tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer
api_key_env: OPENAI_API_KEY
provider: open_ai
- url: https://api.openai.com/v1/chat/completions
- max_prompt_tokens: 131072
- vision_enabled: true
-
- gpt-4o:
- display_name: GPT-4o
- name: gpt-4o
- tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer
- api_key_env: OPENAI_API_KEY
- provider: open_ai
- url: https://api.openai.com/v1/chat/completions
+ url: https://api.openai.com/v1/responses
max_prompt_tokens: 131072
vision_enabled: true
+ provider_params:
+ reasoning_effort: low
- gpt-4o-mini:
- display_name: GPT-4o-mini
- name: gpt-4o-mini
+ gpt-5.2:
+ display_name: GPT-5.2
+ name: gpt-5.2
tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer
api_key_env: OPENAI_API_KEY
provider: open_ai
@@ -42,9 +31,9 @@ llms:
max_prompt_tokens: 131072
vision_enabled: true
- claude-3.5-haiku:
- display_name: Claude 3.5 Haiku
- name: claude-3-5-haiku-latest
+ claude-4.5-haiku:
+ display_name: Claude 4.5 Haiku
+ name: claude-haiku-4-5
tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer
api_key_env: ANTHROPIC_API_KEY
provider: anthropic
@@ -52,19 +41,24 @@ llms:
max_prompt_tokens: 200000
vision_enabled: false
- claude-3.5-sonnet:
- display_name: Claude 3.5 Sonnet
- name: claude-3-5-sonnet-latest
+ claude-4.5-haiku-thinking:
+ display_name: Claude 4.5 Haiku
+ name: claude-haiku-4-5
tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer
api_key_env: ANTHROPIC_API_KEY
provider: anthropic
url: https://api.anthropic.com/v1/messages
max_prompt_tokens: 200000
vision_enabled: true
+ provider_params:
+ disable_top_p: true
+ disable_temperature: true
+ enable_reasoning: true
+ reasoning_tokens: 1024
- claude-3.7-sonnet:
- display_name: Claude 3.7 Sonnet
- name: claude-3-7-sonnet-latest
+ claude-4.5-sonnet:
+ display_name: Claude 4.5 Sonnet
+ name: claude-sonnet-4-5
tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer
api_key_env: ANTHROPIC_API_KEY
provider: anthropic
@@ -72,9 +66,9 @@ llms:
max_prompt_tokens: 200000
vision_enabled: true
- claude-3.7-sonnet-thinking:
- display_name: Claude 3.7 Sonnet
- name: claude-3-7-sonnet-latest
+ claude-4.5-sonnet-thinking:
+ display_name: Claude 4.5 Sonnet
+ name: claude-sonnet-4-5
tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer
api_key_env: ANTHROPIC_API_KEY
provider: anthropic
@@ -110,12 +104,27 @@ llms:
disable_top_p: true
disable_temperature: true
- gemini-2.0-pro:
- display_name: Gemini 2.0 pro
- name: gemini-2-0-pro
+ gemini-3-flash-thinking:
+ display_name: Gemini 3.0 Flash
+ name: gemini-3-0-flash
+ tokenizer: DiscourseAi::Tokenizer::GeminiTokenizer
+ api_key_env: GEMINI_API_KEY
+ provider: google
+ url: https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview
+ max_prompt_tokens: 1000000
+ vision_enabled: true
+ provider_params:
+ disable_top_p: true
+ disable_temperature: true
+ enable_thinking: true
+ thinking_tokens: 1024
+
+ gemini-3-pro:
+ display_name: Gemini 3.0 pro
+ name: gemini-3-pro-preview
tokenizer: DiscourseAi::Tokenizer::GeminiTokenizer
api_key_env: GEMINI_API_KEY
provider: google
- url: https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-pro
+ url: https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-preview
max_prompt_tokens: 1000000
vision_enabled: true
diff --git a/plugins/discourse-ai/evals/lib/prompts/prompt_evaluator.rb b/plugins/discourse-ai/evals/lib/prompts/prompt_evaluator.rb
index 7ca415fef28..068b0199953 100644
--- a/plugins/discourse-ai/evals/lib/prompts/prompt_evaluator.rb
+++ b/plugins/discourse-ai/evals/lib/prompts/prompt_evaluator.rb
@@ -13,19 +13,30 @@ class DiscourseAi::Evals::PromptEvaluator
args.flat_map do |test|
bump_progress.call
- prompts, messages, followups, output_thinking, stream, temperature, tools =
- symbolize_test_args(test)
+ prompts = test[:prompts] || [test[:prompt]]
+ messages = test[:messages] || [test[:message]]
+ followups = symbolize_followups(test)
+ output_thinking = test[:output_thinking] || false
+ stream = test[:stream] || false
+ temperature = test[:temperature]
+ tools = symbolize_tools(test[:tools])
+ tool_results = test[:tool_results]
+ chain_length = test[:chain_length] || 1
+ max_tool_calls = test[:max_tool_calls]
prompts.flat_map do |prompt|
messages.map do |message|
runner.run_single_test(
- prompt,
- message,
- followups,
- output_thinking,
- stream,
- temperature,
- tools,
+ prompt:,
+ message:,
+ followups:,
+ output_thinking:,
+ stream:,
+ temperature:,
+ tools:,
+ tool_results:,
+ chain_length:,
+ max_tool_calls:,
)
end
end
@@ -35,17 +46,6 @@ class DiscourseAi::Evals::PromptEvaluator
private
- def symbolize_test_args(args)
- prompts = args[:prompts] || [args[:prompt]]
- messages = args[:messages] || [args[:message]]
- followups = symbolize_followups(args)
- output_thinking = args[:output_thinking] || false
- stream = args[:stream] || false
- temperature = args[:temperature]
- tools = symbolize_tools(args[:tools])
- [prompts, messages, followups, output_thinking, stream, temperature, tools]
- end
-
def symbolize_followups(args)
return nil if args[:followups].nil? && args[:followup].nil?
followups = args[:followups] || [args[:followup]]
diff --git a/plugins/discourse-ai/evals/lib/prompts/single_test_runner.rb b/plugins/discourse-ai/evals/lib/prompts/single_test_runner.rb
index 6e7c43f88f1..c8d7c575607 100644
--- a/plugins/discourse-ai/evals/lib/prompts/single_test_runner.rb
+++ b/plugins/discourse-ai/evals/lib/prompts/single_test_runner.rb
@@ -14,11 +14,33 @@ class DiscourseAi::Evals::PromptSingleTestRunner
# @param temperature [Float] the temperature to use when generating completions
# @param tools [Array<Hash>] an array of tools to use when generating completions
# @return [Hash] the prompt, message, and result of the test
- def run_single_test(prompt, message, followups, output_thinking, stream, temperature, tools)
+ def run_single_test(
+ prompt:,
+ message:,
+ followups:,
+ output_thinking:,
+ stream:,
+ temperature:,
+ tools:,
+ tool_results: nil,
+ chain_length: 1,
+ max_tool_calls: nil
+ )
@c_prompt =
DiscourseAi::Completions::Prompt.new(prompt, messages: [{ type: :user, content: message }])
@c_prompt.tools = tools if tools
- generate_result(temperature, output_thinking, stream)
+ @tool_results = tool_results || {}
+
+ while chain_length > 0
+ generate_result(temperature, output_thinking, stream)
+ chain_length -= 1
+ if chain_length > 0
+ populate_reply(max_tool_calls:)
+ break if @c_prompt.messages.last[:type] != :tool
+ end
+
+ @c_prompt.tool_choice = :none if chain_length == 1
+ end
if followups
followups.each do |followup|
@@ -31,6 +53,39 @@ class DiscourseAi::Evals::PromptSingleTestRunner
private
+ def populate_reply(max_tool_calls:)
+ # @c_prompt contains the prompt
+ # @result contains the last result
+ #
+ # we need to append the result to the prompt so we can proceed with the chain
+
+ current = @result
+ current = [current] if !current.is_a?(Array)
+
+ result = []
+ current.each do |part|
+ if part.is_a?(DiscourseAi::Completions::ToolCall)
+ proposed_result = @tool_results[part.name]
+ raise "No tool result provided for tool #{part.name}" if !proposed_result
+
+ part.parameters.each do |key, value|
+ proposed_result = proposed_result.gsub("{{#{key}}}", value.to_s)
+ end
+
+ break if max_tool_calls && ((max_tool_calls -= 1) < 0)
+
+ result.push(part)
+ result.push(
+ DiscourseAi::Completions::ToolResult.new(content: proposed_result, tool_call: part),
+ )
+ else
+ result.push(part)
+ end
+ end
+
+ @c_prompt.push_model_response(result)
+ end
+
def generate_followup(followup, output_thinking, stream, temperature)
@c_prompt.push_model_response(@result)
followup_message = set_followup_tool(followup)
@@ -40,7 +95,6 @@ class DiscourseAi::Evals::PromptSingleTestRunner
rescue => e
# should not happen but it helps debugging...
puts e
- result = []
end
end
diff --git a/plugins/discourse-ai/evals/lib/recorder.rb b/plugins/discourse-ai/evals/lib/recorder.rb
index 62abd1c4073..ec0bdea8d76 100644
--- a/plugins/discourse-ai/evals/lib/recorder.rb
+++ b/plugins/discourse-ai/evals/lib/recorder.rb
@@ -240,7 +240,6 @@ module DiscourseAi
formatter.finalize if finalize_formatter
detach_thread_loggers
-
structured_logger.save
output.puts
diff --git a/plugins/discourse-ai/evals/run b/plugins/discourse-ai/evals/run
old mode 100644
new mode 100755
diff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
index 3ba9d96245d..fc713840484 100644
--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
@@ -63,11 +63,11 @@ module DiscourseAi
def self.no_more_tool_calls_text
# note, Anthropic must never prefill with an ending whitespace
- "I WILL NOT USE TOOLS IN THIS REPLY, user expressed they wanted to stop using tool calls.\nHere is the best, complete, answer I can come up with given the information I have."
+ "Tool budget EXHAUSTED for this response, no more tools will be called in this response.\nHere is the best, complete, answer I can come up with given the information I have to address the original user query."
end
def self.no_more_tool_calls_text_user
- "DO NOT USE TOOLS IN YOUR REPLY. Return the best answer you can given the information I supplied you."
+ "IT IS CRITICAL you do not use any tools or function calls in your response. JUST REPLY with the best answer you can provide based on your existing knowledge."
end
def no_more_tool_calls_text
diff --git a/plugins/discourse-ai/lib/completions/dialects/gemini.rb b/plugins/discourse-ai/lib/completions/dialects/gemini.rb
index a718c545789..31c694706d3 100644
--- a/plugins/discourse-ai/lib/completions/dialects/gemini.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/gemini.rb
@@ -47,6 +47,10 @@ module DiscourseAi
previous_message = message
end
+ if tool_choice == :none && interleving_messages.length > 0
+ interleving_messages << { role: "user", parts: { text: no_more_tool_calls_text_user } }
+ end
+
{ messages: interleving_messages, system_instruction: system_instruction }
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
index ff06d52bbe7..40e304e29a7 100644
--- a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
@@ -69,7 +69,7 @@ module DiscourseAi
if disable_native_tools?
super
else
- tools_dialect.from_raw_tool_call(msg)
+ [thinking_signature_node(message: msg), tools_dialect.from_raw_tool_call(msg)].compact
end
end
@@ -178,7 +178,8 @@ module DiscourseAi
def hoist_reasoning(messages)
new_messages = []
- messages.each do |msg|
+ # Flatten because tool_call_msg can return arrays (e.g., [reasoning_node, function_call_node])
+ messages.flatten.each do |msg|
if msg[:content].is_a?(Array)
reasoning = []
msg[:content].delete_if do |item|
diff --git a/plugins/discourse-ai/lib/completions/dialects/open_ai_tools.rb b/plugins/discourse-ai/lib/completions/dialects/open_ai_tools.rb
index 3411dbfe91e..97ee105d4c9 100644
--- a/plugins/discourse-ai/lib/completions/dialects/open_ai_tools.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/open_ai_tools.rb
@@ -43,12 +43,19 @@ module DiscourseAi
call_details[:name] = raw_message[:name]
if @responses_api
- {
+ provider_data = raw_message[:provider_data] || {}
+ open_ai_data =
+ provider_data[:open_ai_responses] || provider_data["open_ai_responses"] || {}
+ tool_item_id = open_ai_data[:id] || open_ai_data["id"]
+
+ payload = {
type: "function_call",
+ id: tool_item_id,
call_id: raw_message[:id],
name: call_details[:name],
arguments: call_details[:arguments],
}
+ payload.compact
else
{
role: "assistant",
diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rb
index ed1fde77b9c..37d700671ca 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb
@@ -489,7 +489,7 @@ module DiscourseAi
feature_name: feature_name,
llm_id: llm_model&.id,
language_model: llm_model.name,
- feature_context: feature_context.present? ? feature_context.as_json : nil,
+ feature_context: feature_context.presence&.as_json,
)
end
@@ -534,6 +534,7 @@ module DiscourseAi
# this is to keep stuff backwards compatible
response_data = response_data.first if response_data.length == 1
+ response_data = "" if response_data.nil?
response_data
end
diff --git a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
index 5746a863594..05b988ab8c5 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/gemini.rb
@@ -210,7 +210,9 @@ module DiscourseAi
nil
end
end
- end
+ # we could get a nil here cause part can be nil
+ # interface expects an array
+ end || []
end
def decode_chunk(chunk)
diff --git a/plugins/discourse-ai/lib/completions/open_ai_responses_message_processor.rb b/plugins/discourse-ai/lib/completions/open_ai_responses_message_processor.rb
index a386d10d632..86a57fa6a68 100644
--- a/plugins/discourse-ai/lib/completions/open_ai_responses_message_processor.rb
+++ b/plugins/discourse-ai/lib/completions/open_ai_responses_message_processor.rb
@@ -149,12 +149,20 @@ module DiscourseAi::Completions
end
def build_tool_call_from_item(item)
- id = item[:call_id]
+ call_id = item[:call_id]
+ item_id = item[:id]
name = item[:name]
arguments = item[:arguments] || ""
params = arguments.empty? ? {} : JSON.parse(arguments, symbolize_names: true)
- ToolCall.new(id: id, name: name, parameters: params)
+ ToolCall.new(
+ id: call_id,
+ name: name,
+ parameters: params,
+ provider_data: {
+ PROVIDER_KEY => { id: item_id, call_id: call_id }.compact,
+ },
+ )
end
def handle_tool_stream(event_type, json)
@@ -173,14 +181,19 @@ module DiscourseAi::Completions
end
def start_tool_stream(data)
- # important note... streaming API has both id and call_id
- # both seem to work as identifiers, api examples seem to favor call_id
- # so I am using it here
- id = data[:call_id]
+ call_id = data[:call_id]
+ item_id = data[:id]
name = data[:name]
@tool_arguments = +""
- @tool = ToolCall.new(id: id, name: name)
+ @tool =
+ ToolCall.new(
+ id: call_id,
+ name: name,
+ provider_data: {
+ PROVIDER_KEY => { id: item_id, call_id: call_id }.compact,
+ },
+ )
@streaming_parser = JsonStreamingTracker.new(self) if @partial_tool_calls
end
diff --git a/plugins/discourse-ai/lib/completions/prompt.rb b/plugins/discourse-ai/lib/completions/prompt.rb
index ea376ece353..5379446f231 100644
--- a/plugins/discourse-ai/lib/completions/prompt.rb
+++ b/plugins/discourse-ai/lib/completions/prompt.rb
@@ -69,18 +69,26 @@ module DiscourseAi
# this new api tries to create symmetry between responses and prompts
# this means anything we get back from the model via endpoint can be easily appended
def push_model_response(response)
- response = [response] if !response.is_a? Array
-
- thinking_text = nil
- thinking_provider_info = {}
+ pending_thinking = nil
+
+ thinking_attrs =
+ lambda do
+ return {} unless pending_thinking
+
+ attrs = {
+ thinking: pending_thinking.message,
+ thinking_provider_info: pending_thinking.provider_info.presence,
+ }
+ pending_thinking = nil
+ attrs
+ end
- response.each do |message|
- if message.is_a?(Thinking)
+ Array(response).each do |message|
+ case message
+ when Thinking
next if message.partial?
- thinking_text = message.message if message.message.present?
- thinking_provider_info =
- Thinking.merge_provider_info(thinking_provider_info, message.provider_info)
- elsif message.is_a?(ToolCall)
+ pending_thinking = message
+ when ToolCall
next if message.partial?
push(
type: :tool_call,
@@ -88,18 +96,23 @@ module DiscourseAi
id: message.id,
name: message.name,
provider_data: message.provider_data,
+ **thinking_attrs.call,
+ )
+ when String
+ if messages.last&.dig(:type) == :model
+ messages.last[:content] = messages.last[:content] + message
+ else
+ push(type: :model, content: message, **thinking_attrs.call)
+ end
+ when ToolResult
+ push(
+ type: :tool,
+ content: message.content,
+ id: message.tool_call.id,
+ name: message.tool_call.name,
)
- elsif message.is_a?(String)
- push(type: :model, content: message)
else
- raise ArgumentError, "response must be an array of strings, ToolCalls, or Thinkings"
- end
- end
-
- if thinking_text || thinking_provider_info.present?
- messages.last[:thinking] = thinking_text if thinking_text
- if thinking_provider_info.present?
- messages.last[:thinking_provider_info] = thinking_provider_info
+ raise ArgumentError, "unexpected message type: #{message.class}"
end
end
end
diff --git a/plugins/discourse-ai/lib/completions/tool_result.rb b/plugins/discourse-ai/lib/completions/tool_result.rb
new file mode 100644
index 00000000000..79f1e4c2d01
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/tool_result.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Completions
+ class ToolResult
+ attr_reader :content, :tool_call
+
+ def initialize(content:, tool_call:)
+ @content = content
+ @tool_call = tool_call
+ end
+
+ def to_s
+ "ToolResult for #{tool_call.name} (#{tool_call.id}): #{content}"
+ end
+
+ def ==(other)
+ return nil if !other.is_a?(ToolResult)
+ content == other.content && tool_call == other.tool_call
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/personas/bot.rb b/plugins/discourse-ai/lib/personas/bot.rb
index 428344ff62e..62739c427f7 100644
--- a/plugins/discourse-ai/lib/personas/bot.rb
+++ b/plugins/discourse-ai/lib/personas/bot.rb
@@ -270,6 +270,7 @@ module DiscourseAi
tool_call_message[:thinking] = thinking_message if thinking_message
tool_call_message[:thinking_provider_info] = provider_payload if provider_payload.present?
+ current_thinking.clear
end
tool_message = {
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
index 09932ed10ba..f92fd8e2391 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
@@ -19,5 +19,51 @@ RSpec.describe DiscourseAi::Completions::Dialects::OpenAiResponses do
expect(user_message).not_to have_key(:name)
expect(user_message[:content]).to eq([{ type: "input_text", text: "user_1: Hello there" }])
end
+
+ it "preserves encrypted reasoning before tool calls" do
+ prompt = DiscourseAi::Completions::Prompt.new("You are a bot")
+ prompt.push(type: :user, content: "Please run the tool")
+ prompt.push(
+ type: :tool_call,
+ id: "call_1",
+ name: "echo",
+ content: { arguments: { string: "hello" } }.to_json,
+ provider_data: {
+ open_ai_responses: {
+ id: "fc_1",
+ call_id: "call_1",
+ },
+ },
+ thinking: "summary",
+ thinking_provider_info: {
+ open_ai_responses: {
+ reasoning_id: "rs_1",
+ encrypted_content: "ENC",
+ },
+ },
+ )
+
+ dialect = described_class.new(prompt, llm_model)
+ translated = dialect.translate
+
+ reasoning_index = translated.index { |msg| msg[:type] == "reasoning" }
+ function_call_index = translated.index { |msg| msg[:type] == "function_call" }
+
+ expect(reasoning_index).to be_present
+ expect(function_call_index).to be_present
+ expect(reasoning_index).to be < function_call_index
+
+ expect(translated[reasoning_index]).to include(
+ type: "reasoning",
+ id: "rs_1",
+ encrypted_content: "ENC",
+ )
+ expect(translated[function_call_index]).to include(
+ type: "function_call",
+ id: "fc_1",
+ call_id: "call_1",
+ name: "echo",
+ )
+ end
end
end
diff --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.rb
index c5c7e7df878..85ae77ac70c 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
@@ -240,6 +240,12 @@ data: {"type":"response.reasoning_summary_part.added","sequence_number":3,"item_
expect(partials.first.name).to eq("echo")
expect(partials.first.parameters).to eq({ string: "hello" })
expect(partials.first.id).to eq("call_TQyfNmFnKblzXl5rlcGeIsg5")
+ expect(partials.first.provider_data).to include(
+ open_ai_responses: {
+ id: "fc_684910c8b68881a3b43610e1d57ef00702d35c1819a50f63",
+ call_id: "call_TQyfNmFnKblzXl5rlcGeIsg5",
+ },
+ )
end
it "can handle non streaming tool calls" do
@@ -329,6 +335,12 @@ data: {"type":"response.reasoning_summary_part.added","sequence_number":3,"item_
expect(result.name).to eq("echo")
expect(result.parameters).to eq({ string: "sam" })
expect(result.id).to eq("call_UdxBpinIVc5nRZ0VnWJIgneA")
+ expect(result.provider_data).to include(
+ open_ai_responses: {
+ id: "fc_68491ed75e0c819f87462ff642c58d2e08901d75ebf6c66e",
+ call_id: "call_UdxBpinIVc5nRZ0VnWJIgneA",
+ },
+ )
end
it "uses reasoning object format for responses API" do
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
index 98c8e81d807..71bad6c1a02 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
@@ -127,4 +127,76 @@ RSpec.describe DiscourseAi::Completions::Prompt do
expect(system_message[:id]).to eq(username)
end
end
+
+ describe "#push_model_response" do
+ it "buffers streamed string chunks into a single model message" do
+ prompt.push(type: :user, content: user_msg, id: username)
+ prompt.push_model_response(["Hello", " ", "World"])
+ expect(prompt.messages.last).to include(type: :model, content: "Hello World")
+ end
+
+ it "attaches thinking to next response" do
+ prompt.push(type: :user, content: user_msg, id: username)
+
+ thinking =
+ DiscourseAi::Completions::Thinking.new(
+ message: "summary",
+ partial: false,
+ provider_info: {
+ open_ai_responses: {
+ reasoning_id: "rs_1",
+ encrypted_content: "ENC",
+ },
+ },
+ )
+
+ prompt.push_model_response([thinking, "Hello"])
+
+ expect(prompt.messages.last).to include(type: :model, content: "Hello", thinking: "summary")
+ expect(prompt.messages.last[:thinking_provider_info]).to include(
+ open_ai_responses: include(reasoning_id: "rs_1", encrypted_content: "ENC"),
+ )
+ end
+
+ it "appends additional streamed text to the existing model message" do
+ prompt.push(type: :user, content: user_msg, id: username)
+
+ prompt.push_model_response("Hello")
+ prompt.push_model_response(" World")
+
+ model_messages = prompt.messages.select { |m| m[:type] == :model }
+ expect(model_messages.length).to eq(1)
+ expect(model_messages.first[:content]).to eq("Hello World")
+ end
+
+ it "attaches thinking metadata to the tool call message" do
+ prompt.push(type: :user, content: user_msg, id: username)
+
+ prompt.push_model_response(
+ [
+ DiscourseAi::Completions::Thinking.new(
+ message: "summary",
+ provider_info: {
+ open_ai_responses: {
+ reasoning_id: "rs_1",
+ encrypted_content: "ENC",
+ },
+ },
+ ),
+ DiscourseAi::Completions::ToolCall.new(
+ id: "call_1",
+ name: "echo",
+ parameters: {
+ string: "hello",
+ },
+ ),
+ ],
+ )
+
+ expect(prompt.messages.last).to include(type: :tool_call, thinking: "summary")
+ expect(prompt.messages.last[:thinking_provider_info]).to include(
+ open_ai_responses: include(reasoning_id: "rs_1", encrypted_content: "ENC"),
+ )
+ end
+ end
end
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
index 953a2bcbd86..782b826de0d 100644
--- a/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
@@ -356,9 +356,9 @@ RSpec.describe DiscourseAi::AiBot::SharedAiConversationsController do
it "renders the shared conversation" do
get "#{path}/#{shared_conversation.share_key}"
expect(response).to have_http_status(:success)
- expect(response.headers["Cache-Control"]).to eq("max-age=60, public")
expect(response.headers["X-Robots-Tag"]).to eq("noindex")
expect(response.body).not_to include("Translation missing")
+ expect(response.headers["Cache-Control"]).to eq("max-age=60, public")
end
it "is also able to render in json format" do