Skip to content

Commit

Permalink
add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Mar 3, 2025
1 parent 699ad5c commit ecb15c9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions spec/lib/completions/anthropic_message_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

describe DiscourseAi::Completions::AnthropicMessageProcessor do
it "correctly handles and combines partial thinking chunks into complete thinking objects" do
processor =
DiscourseAi::Completions::AnthropicMessageProcessor.new(
streaming_mode: true,
partial_tool_calls: false,
output_thinking: true,
)

# Simulate streaming thinking output in multiple chunks
result1 =
processor.process_streamed_message(
{ type: "content_block_start", content_block: { type: "thinking", thinking: "" } },
)

result2 =
processor.process_streamed_message(
{
type: "content_block_delta",
delta: {
type: "thinking_delta",
thinking: "First part of thinking",
},
},
)

result3 =
processor.process_streamed_message(
{
type: "content_block_delta",
delta: {
type: "thinking_delta",
thinking: " and second part",
},
},
)

_result4 =
processor.process_streamed_message(
{
type: "content_block_delta",
delta: {
type: "signature_delta",
signature: "thinking-sig-123",
},
},
)

# Finish the thinking block
final_result = processor.process_streamed_message({ type: "content_block_stop" })

# Verify the partial thinking chunks
expect(result1).to be_a(DiscourseAi::Completions::Thinking)
expect(result1.message).to eq("")
expect(result1.partial?).to eq(true)

expect(result2).to be_a(DiscourseAi::Completions::Thinking)
expect(result2.message).to eq("First part of thinking")
expect(result2.partial?).to eq(true)

expect(result3).to be_a(DiscourseAi::Completions::Thinking)
expect(result3.message).to eq(" and second part")
expect(result3.partial?).to eq(true)

# Verify the final complete thinking object
expect(final_result).to be_a(DiscourseAi::Completions::Thinking)
expect(final_result.message).to eq("First part of thinking and second part")
expect(final_result.signature).to eq("thinking-sig-123")
expect(final_result.partial?).to eq(false)
end
end

0 comments on commit ecb15c9

Please sign in to comment.