Skip to content

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

FIX: Handle multiple streamed OpenAI tool calls (#44270)

Sama84ac1190bfe
diff --git a/plugins/discourse-ai/lib/completions/open_ai_message_processor.rb b/plugins/discourse-ai/lib/completions/open_ai_message_processor.rb
index 376dfa6afb4..be0621335e2 100644
--- a/plugins/discourse-ai/lib/completions/open_ai_message_processor.rb
+++ b/plugins/discourse-ai/lib/completions/open_ai_message_processor.rb
@@ -4,8 +4,8 @@ module DiscourseAi::Completions
attr_reader :prompt_tokens, :completion_tokens, :cache_read_tokens, :cache_write_tokens
def initialize(partial_tool_calls: false)
- @tool = nil
- @tool_arguments = +""
+ @tools = {}
+ @current_tool_key = nil
@prompt_tokens = nil
@completion_tokens = nil
@cache_read_tokens = nil
@@ -36,91 +36,120 @@ module DiscourseAi::Completions
end
def process_streamed_message(json)
- rval = nil
-
+ result = []
tool_calls = json.dig(:choices, 0, :delta, :tool_calls)
content = json.dig(:choices, 0, :delta, :content)
-
finished_tools = json.dig(:choices, 0, :finish_reason) || tool_calls == []
- if tool_calls.present?
- id = tool_calls.dig(0, :id)
- name = tool_calls.dig(0, :function, :name)
- arguments = tool_calls.dig(0, :function, :arguments)
-
- # TODO: multiple tool support may require index
- #index = tool_calls[0].dig(:index)
-
- if id.present? && @tool && @tool.id != id
- process_arguments
- @tool.partial = false
- rval = @tool
- @tool = nil
+ tool_calls&.each do |tool_call|
+ key =
+ if !tool_call[:index].nil?
+ [:index, tool_call[:index]]
+ elsif tool_call[:id].present?
+ [:id, tool_call[:id]]
+ else
+ @current_tool_key
+ end
+
+ state = @tools[key]
+ id = tool_call[:id]
+ name = tool_call.dig(:function, :name)
+
+ # A continuation's explicit ID is more reliable than a conflicting index.
+ if id.present? && name.blank? && (!state || state.id != id)
+ indexed_state = state
+ state = @tools.values.find { |candidate| candidate.id == id }
+ key = [:id, id] if state && (!state.index.nil? || indexed_state)
end
- if id.present? && name.present?
- @tool_arguments = +""
- @tool = ToolCall.new(id: id, name: name)
- @streaming_parser = JsonStreamingTracker.new(self) if @partial_tool_calls
+ # Some compatible servers reuse an index for successive calls.
+ if state && id.present? && name.present? && state.id != id
+ result << state.finish
+ @tools.delete_if { |_, candidate| candidate.equal?(state) }
+ state = nil
end
- @tool_arguments << arguments.to_s
- @streaming_parser << arguments.to_s if @streaming_parser && !arguments.to_s.empty?
- rval = current_tool_progress if !rval
- elsif finished_tools && @tool
- parsed_args = ToolArgumentsParser.parse(@tool_arguments)
- @tool.parameters = parsed_args
- @tool.partial = false
- rval = @tool
- @tool = nil
- elsif !content.to_s.empty?
- # we don't want to strip empty content like "\n", do not use present?
- rval = content
+ if !state && id.present?
+ state =
+ @tools.values.find do |candidate|
+ candidate.id == id &&
+ (
+ tool_call[:index].nil? || candidate.index.nil? ||
+ candidate.index == tool_call[:index]
+ )
+ end
+ elsif !state && id.blank? && @current_tool_key && @tools[@current_tool_key]&.index.nil?
+ state = @tools[@current_tool_key]
+ end
+
+ if !state && id.present? && name.present?
+ state = ToolState.new(tool_call, partial_tool_calls: @partial_tool_calls)
+ end
+ next if !state
+
+ state.index ||= tool_call[:index]
+ @tools[key] = state
+ @current_tool_key = key
+ state.append(tool_call.dig(:function, :arguments).to_s)
+ progress = state.progress
+ result << progress if progress && !finished_tools
end
+ # Switching indexes does not complete a call: argument fragments can interleave.
+ result.concat(finish) if finished_tools
+ # Content and tool calls may share a delta; preserve whitespace-only text.
+ result.unshift(content) if !content.to_s.empty?
update_usage(json)
- rval
+ result.length > 1 ? result : result.first
end
- def notify_progress(key, value)
- if @tool
+ def finish
+ result = @tools.values.uniq.map(&:finish)
+ @tools.clear
+ @current_tool_key = nil
+ result
+ end
+
+ private
+
+ class ToolState
+ attr_reader :id
+ attr_accessor :index
+
+ def initialize(tool_call, partial_tool_calls:)
+ @id = tool_call[:id]
+ @index = tool_call[:index]
+ @tool = ToolCall.new(id: @id, name: tool_call.dig(:function, :name))
+ @arguments = +""
+ @streaming_parser = JsonStreamingTracker.new(self) if partial_tool_calls
+ end
+
+ def append(arguments)
+ @arguments << arguments
+ @streaming_parser << arguments if @streaming_parser && !arguments.empty?
+ end
+
+ def notify_progress(key, value)
@tool.partial = true
@tool.parameters[key.to_sym] = value
@has_new_data = true
end
- end
- def current_tool_progress
- if @has_new_data
+ def progress
+ return if !@has_new_data
+
@has_new_data = false
@tool
- else
- nil
end
- end
- def finish
- rval = []
- if @tool
- process_arguments
+ def finish
+ @tool.parameters = ToolArgumentsParser.parse(@arguments) if @arguments.present?
@tool.partial = false
- rval << @tool
- @tool = nil
- end
-
- rval
- end
-
- private
-
- def process_arguments
- if @tool_arguments.present?
- parsed_args = ToolArgumentsParser.parse(@tool_arguments)
- @tool.parameters = parsed_args
- @tool_arguments = nil
+ @tool
end
end
+ private_constant :ToolState
def update_usage(json)
usage = json.dig(:usage)
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/open_ai_spec.rb
index 5cd89ee4493..1cc87c8e580 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
@@ -909,6 +909,51 @@ RSpec.describe DiscourseAi::Completions::Endpoints::OpenAi do
compliance.streaming_mode_tools(open_ai_mock)
end
+ it "emits every tool call when a streamed event contains multiple calls" do
+ event = {
+ choices: [
+ {
+ index: 0,
+ delta: {
+ content: "Searching\n",
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_sam",
+ function: {
+ name: "search",
+ arguments: '{"search_query":"sam"}',
+ },
+ },
+ {
+ index: 1,
+ id: "call_dan",
+ function: {
+ name: "search",
+ arguments: '{"search_query":"dan"}',
+ },
+ },
+ ],
+ },
+ finish_reason: "tool_calls",
+ },
+ ],
+ }
+ open_ai_mock.stub_raw("data: #{event.to_json}\n\ndata: [DONE]\n\n")
+ dialect = compliance.dialect(prompt: compliance.generic_prompt(tools: tools))
+ response = []
+
+ endpoint.perform_completion!(dialect, user) { |partial| response << partial }
+
+ expect(response.first).to eq("Searching\n")
+ tool_calls = response.drop(1)
+ expect(tool_calls.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(tool_calls.map(&:parameters)).to eq(
+ [{ search_query: "sam" }, { search_query: "dan" }],
+ )
+ expect(tool_calls).to all(have_attributes(partial: false))
+ end
+
it "properly handles multiple tool calls" do
raw_data = <<~TEXT.strip
data: {"id":"chatcmpl-8xjcr5ZOGZ9v8BDYCx0iwe57lJAGk","object":"chat.completion.chunk","created":1709247429,"model":"gpt-4-0125-preview","system_fingerprint":"fp_91aa3742b1","choices":[{"index":0,"delta":{"role":"assistant","content":null},"logprobs":null,"finish_reason":null}]}
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
index b0e8712913c..83ffec39e47 100644
--- a/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
@@ -196,6 +196,116 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Vllm do
end
describe "tool support" do
+ %w[coalesced split].each do |delivery|
+ it "preserves interleaved tool progress with #{delivery} transport chunks" do
+ events = [
+ {
+ choices: [
+ {
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "tool-1",
+ function: {
+ name: "echo",
+ arguments: '{"text":"one',
+ },
+ },
+ {
+ index: 1,
+ id: "tool-2",
+ function: {
+ name: "echo",
+ arguments: '{"text":"two',
+ },
+ },
+ ],
+ },
+ },
+ ],
+ },
+ {
+ choices: [
+ {
+ delta: {
+ tool_calls: [
+ { index: 1, function: { arguments: '-done"}' } },
+ { index: 0, function: { arguments: '-done"}' } },
+ ],
+ },
+ },
+ ],
+ },
+ { choices: [{ delta: {}, finish_reason: "tool_calls" }] },
+ ]
+ chunks =
+ events.map { |event| "data: #{event.merge(id: "chatcmpl-tool-batch").to_json}\n\n" }
+ chunks << "data: [DONE]\n\n"
+ body = delivery == "coalesced" ? [chunks.join] : chunks.flat_map(&:chars)
+ response = []
+
+ vllm_mock.with_chunk_array_support do
+ stub_request(:post, "https://test.dev/v1/chat/completions").to_return(
+ status: 200,
+ body: body,
+ )
+ llm.generate(
+ "use both tools",
+ user: Discourse.system_user,
+ partial_tool_calls: true,
+ ) { |partial| response << partial.dup }
+ end
+
+ completed = response.reject(&:partial?)
+ expect(completed.map(&:id)).to eq(%w[tool-1 tool-2])
+ expect(completed.map(&:parameters)).to eq([{ text: "one-done" }, { text: "two-done" }])
+ expect(response.map(&:provider_data)).to all(
+ eq(vllm: { tool_batch_id: "chatcmpl-tool-batch" }),
+ )
+ progress = response.select(&:partial?)
+ if delivery == "split"
+ expect(progress.map(&:parameters)).to eq(
+ [{ text: "one" }, { text: "two" }, { text: "two-done" }, { text: "one-done" }],
+ )
+ else
+ expect(progress).to eq([])
+ end
+ end
+ end
+
+ it "assigns the provider batch to every tool call in a streamed event" do
+ event = {
+ id: "chatcmpl-tool-batch",
+ choices: [
+ {
+ index: 0,
+ delta: {
+ tool_calls: [
+ { index: 0, id: "tool-1", function: { name: "echo", arguments: '{"text":"one"}' } },
+ { index: 1, id: "tool-2", function: { name: "echo", arguments: '{"text":"two"}' } },
+ ],
+ },
+ finish_reason: "tool_calls",
+ },
+ ],
+ }
+ stub_request(:post, "https://test.dev/v1/chat/completions").to_return(
+ status: 200,
+ body: "data: #{event.to_json}\n\ndata: [DONE]\n\n",
+ )
+ response = []
+
+ llm.generate("use both tools", user: Discourse.system_user) { |partial| response << partial }
+
+ expect(response.map(&:id)).to eq(%w[tool-1 tool-2])
+ expect(response.map(&:parameters)).to eq([{ text: "one" }, { text: "two" }])
+ expect(response.map(&:provider_data)).to all(
+ eq(vllm: { tool_batch_id: "chatcmpl-tool-batch" }),
+ )
+ expect(response).to all(have_attributes(partial: false))
+ end
+
it "is able to invoke XML tools correctly" do
llm_model.update!(provider_params: { "disable_native_tools" => true })
diff --git a/plugins/discourse-ai/spec/lib/completions/open_ai_message_processor_spec.rb b/plugins/discourse-ai/spec/lib/completions/open_ai_message_processor_spec.rb
index 9470827434d..cbb0fd430f0 100644
--- a/plugins/discourse-ai/spec/lib/completions/open_ai_message_processor_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/open_ai_message_processor_spec.rb
@@ -9,7 +9,407 @@ RSpec.describe DiscourseAi::Completions::OpenAiMessageProcessor do
{ choices: [choice] }
end
- it "marks completed tool calls as non-partial when streaming switches tools" do
+ it "retains every tool call in a single streamed chunk" do
+ [false, true].each do |partial_tool_calls|
+ processor = described_class.new(partial_tool_calls: partial_tool_calls)
+ calls = [
+ { index: 0, id: "call_sam", function: { name: "search", arguments: '{"query":"sam"}' } },
+ { index: 1, id: "call_dan", function: { name: "search", arguments: '{"query":"dan"}' } },
+ { index: 2, id: "call_alex", function: { name: "search", arguments: '{"query":"alex"}' } },
+ ]
+
+ emitted =
+ Array(
+ processor.process_streamed_message(
+ chunk(delta: { tool_calls: calls }, finish_reason: "tool_calls"),
+ ),
+ )
+ emitted.concat(processor.finish)
+ completed = emitted.reject(&:partial?)
+
+ expect(completed.map(&:id)).to eq(%w[call_sam call_dan call_alex])
+ expect(completed.map(&:parameters)).to eq(
+ [{ query: "sam" }, { query: "dan" }, { query: "alex" }],
+ )
+ expect(processor.finish).to eq([])
+ end
+ end
+
+ it "assembles interleaved argument fragments by tool index" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 0, id: "call_sam", function: { name: "search", arguments: '{"query":"' } },
+ { index: 1, id: "call_dan", function: { name: "search", arguments: '{"query":"' } },
+ ],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 1, function: { arguments: 'dan"}' } },
+ { index: 0, function: { arguments: 'sam"}' } },
+ ],
+ },
+ ),
+ )
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(completed).to all(have_attributes(partial: false))
+ end
+
+ it "tracks partial progress independently for each tool call" do
+ processor = described_class.new(partial_tool_calls: true)
+ progress =
+ Array(
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_sam",
+ function: {
+ name: "search",
+ arguments: '{"query":"sam',
+ },
+ },
+ {
+ index: 1,
+ id: "call_dan",
+ function: {
+ name: "search",
+ arguments: '{"query":"dan',
+ },
+ },
+ ],
+ },
+ ),
+ ),
+ )
+
+ expect(progress.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(progress.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(progress).to all(have_attributes(partial: true))
+
+ progress =
+ Array(
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 1, function: { arguments: 'iel"}' } },
+ { index: 0, function: { arguments: 'uel"}' } },
+ ],
+ },
+ ),
+ ),
+ )
+
+ expect(progress.map(&:parameters)).to eq([{ query: "daniel" }, { query: "samuel" }])
+ completed =
+ Array(processor.process_streamed_message(chunk(delta: {}, finish_reason: "tool_calls")))
+ expect(completed.map(&:parameters)).to eq([{ query: "samuel" }, { query: "daniel" }])
+ expect(completed).to all(have_attributes(partial: false))
+ expect(processor.finish).to eq([])
+ end
+
+ it "assembles calls by ID when indexes are omitted" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { id: "call_sam", function: { name: "search", arguments: '{"query":"' } },
+ { id: "call_dan", function: { name: "search", arguments: '{"query":"' } },
+ ],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { id: "call_dan", function: { arguments: 'dan"}' } },
+ { id: "call_sam", function: { arguments: 'sam"}' } },
+ ],
+ },
+ ),
+ )
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ end
+
+ it "retains successive calls when a server reuses a tool index" do
+ processor = described_class.new
+ emitted = []
+ %w[sam dan].each do |query|
+ emitted.concat(
+ Array(
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_#{query}",
+ function: {
+ name: "search",
+ arguments: { query: query }.to_json,
+ },
+ },
+ ],
+ },
+ ),
+ ),
+ ),
+ )
+ end
+ emitted.concat(processor.finish)
+
+ expect(emitted.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(emitted.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(emitted).to all(have_attributes(partial: false))
+ end
+
+ it "assembles fragments when a call switches between index and ID identification" do
+ [true, false].each do |indexed_header|
+ processor = described_class.new
+ header = { id: "call_sam", function: { name: "search", arguments: '{"query":"' } }
+ header[:index] = 0 if indexed_header
+ continuation = { function: { arguments: 'sam"}' } }
+ continuation[indexed_header ? :id : :index] = indexed_header ? "call_sam" : 0
+ processor.process_streamed_message(chunk(delta: { tool_calls: [header] }))
+ processor.process_streamed_message(chunk(delta: { tool_calls: [continuation] }))
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(["call_sam"])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }])
+ expect(processor.finish).to eq([])
+ end
+ end
+
+ it "matches a continuation by ID when its index changes" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 0, id: "call_sam", function: { name: "search", arguments: '{"query":' } },
+ { index: 1, id: "call_dan", function: { name: "search", arguments: '{"query":' } },
+ ],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 2, id: "call_sam", function: { arguments: '"sam"}' } },
+ { index: 0, id: "call_dan", function: { arguments: '"dan"}' } },
+ ],
+ },
+ ),
+ )
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(processor.finish).to eq([])
+ end
+
+ it "registers an index introduced by an ID-bearing continuation" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [{ id: "call_sam", function: { name: "search", arguments: '{"query":"' } }],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(delta: { tool_calls: [{ index: 0, id: "call_sam", function: { arguments: "sam" } }] }),
+ )
+ processor.process_streamed_message(
+ chunk(delta: { tool_calls: [{ index: 0, function: { arguments: '"}' } }] }),
+ )
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(["call_sam"])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }])
+ expect(processor.finish).to eq([])
+ end
+
+ it "keeps an unknown continuation ID from corrupting an indexed call" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_sam",
+ function: {
+ name: "search",
+ arguments: '{"query":"sam"}',
+ },
+ },
+ ],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [{ index: 0, id: "call_dan", function: { arguments: '{"query":"dan"}' } }],
+ },
+ ),
+ )
+
+ completed = processor.finish
+
+ expect(completed.map(&:id)).to eq(["call_sam"])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }])
+ end
+
+ it "keeps distinct indexed headers separate when their IDs match" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_search",
+ function: {
+ name: "search",
+ arguments: '{"query":"sam"}',
+ },
+ },
+ {
+ index: 1,
+ id: "call_search",
+ function: {
+ name: "search",
+ arguments: '{"query":"dan"}',
+ },
+ },
+ ],
+ },
+ ),
+ )
+
+ expect(processor.finish.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ end
+
+ it "removes every alias when a tool index is reused" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ { index: 0, id: "call_sam", function: { name: "search", arguments: '{"query":' } },
+ ],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(delta: { tool_calls: [{ id: "call_sam", function: { arguments: '"sam"}' } }] }),
+ )
+ emitted =
+ Array(
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_dan",
+ function: {
+ name: "search",
+ arguments: '{"query":"dan"}',
+ },
+ },
+ ],
+ },
+ ),
+ ),
+ )
+ emitted.concat(processor.finish)
+
+ expect(emitted.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(emitted.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(emitted).to all(have_attributes(partial: false))
+ expect(processor.finish).to eq([])
+ end
+
+ it "accepts unindexed argument fragments for the current call" do
+ processor = described_class.new
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ tool_calls: [{ id: "call_sam", function: { name: "search", arguments: '{"query":"' } }],
+ },
+ ),
+ )
+ processor.process_streamed_message(
+ chunk(delta: { tool_calls: [{ function: { arguments: 'sam"}' } }] }),
+ )
+
+ expect(processor.finish.map(&:parameters)).to eq([{ query: "sam" }])
+ end
+
+ it "preserves content and finalizes all calls on an empty tool delta" do
+ processor = described_class.new
+ content =
+ processor.process_streamed_message(
+ chunk(
+ delta: {
+ content: "\n",
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_sam",
+ function: {
+ name: "search",
+ arguments: '{"query":"sam"}',
+ },
+ },
+ {
+ index: 1,
+ id: "call_dan",
+ function: {
+ name: "search",
+ arguments: '{"query":"dan"}',
+ },
+ },
+ ],
+ },
+ ),
+ )
+
+ completed = processor.process_streamed_message(chunk(delta: { tool_calls: [] }))
+
+ expect(content).to eq("\n")
+ expect(completed.map(&:id)).to eq(%w[call_sam call_dan])
+ expect(completed.map(&:parameters)).to eq([{ query: "sam" }, { query: "dan" }])
+ expect(processor.finish).to eq([])
+ end
+
+ it "keeps earlier tool calls partial until the stream finishes" do
processor = described_class.new(partial_tool_calls: true)
# Start streaming first tool call
@@ -33,8 +433,7 @@ RSpec.describe DiscourseAi::Completions::OpenAiMessageProcessor do
expect(partial.partial?).to eq(true)
expect(partial.parameters).to eq({ location: "SFO" })
- # Start streaming a second tool call – this should finalize the first one
- completed =
+ progress =
processor.process_streamed_message(
chunk(
delta: {
@@ -52,10 +451,8 @@ RSpec.describe DiscourseAi::Completions::OpenAiMessageProcessor do
),
)
- expect(completed).to be_a(DiscourseAi::Completions::ToolCall)
- expect(completed.name).to eq("lookup_weather")
- expect(completed.parameters).to eq({ location: "SFO" })
- expect(completed.partial?).to eq(false), "completed tool should not remain marked as partial"
+ expect(progress).to be_nil
+ expect(partial.partial?).to eq(true)
# Stream argument content for the second tool
processor.process_streamed_message(
@@ -66,13 +463,13 @@ RSpec.describe DiscourseAi::Completions::OpenAiMessageProcessor do
),
)
- # Finish streaming – the final tool should also be marked as not partial
- final_tool = processor.process_streamed_message(chunk(delta: {}, finish_reason: "stop"))
+ completed = processor.process_streamed_message(chunk(delta: {}, finish_reason: "stop"))
- expect(final_tool).to be_a(DiscourseAi::Completions::ToolCall)
- expect(final_tool.name).to eq("create_calendar_event")
- expect(final_tool.parameters).to eq({ title: "Pack", time: "18:00" })
- expect(final_tool.partial?).to eq(false)
+ expect(completed.map(&:name)).to eq(%w[lookup_weather create_calendar_event])
+ expect(completed.map(&:parameters)).to eq(
+ [{ location: "SFO" }, { title: "Pack", time: "18:00" }],
+ )
+ expect(completed).to all(have_attributes(partial: false))
end
it "parses streamed arguments with padding" do