From ecb15c9b3e22fa2d0db5fe05bfb820331a9ae165 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Mon, 3 Mar 2025 14:59:16 +1100 Subject: [PATCH] add spec --- .../anthropic_message_processor_spec.rb | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/lib/completions/anthropic_message_processor_spec.rb diff --git a/spec/lib/completions/anthropic_message_processor_spec.rb b/spec/lib/completions/anthropic_message_processor_spec.rb new file mode 100644 index 000000000..94e5a0ee5 --- /dev/null +++ b/spec/lib/completions/anthropic_message_processor_spec.rb @@ -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