discourse-ai/spec/lib/completions/anthropic_message_processor_spec.rb
Sam f6eedf3e0b
FEATURE: implement thinking token support (#1155)
adds support for "thinking tokens" - a feature that exposes the model's reasoning process before providing the final response. Key improvements include:

- Add a new Thinking class to handle thinking content from LLMs
- Modify endpoints (Claude, AWS Bedrock) to handle thinking output
- Update AI bot to display thinking in collapsible details section
- Fix SEARCH/REPLACE blocks to support empty replacement strings and general improvements to artifact editing
- Allow configurable temperature in triage and report automations
- Various bug fixes and improvements to diff parsing
2025-03-04 12:22:30 +11:00

74 lines
2.2 KiB
Ruby

# 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