class Riffer::Providers::AnthropicCompatible::MessagesAPI::Stream
Constants
- SERVER_TOOL_RESULT_TYPE
-
Each server tool has its own result type (web search run from code execution returns
code_execution_tool_resultblocks too), and the API rejects a server_tool_use sent back without its result.mcp_tool_resultpairs withmcp_tool_use, which riffer drops, so its result is dropped too.
Public Class Methods
Source
# File lib/riffer/providers/anthropic_compatible/messages_api/stream.rb, line 53 def initialize(tools:) @tools = tools @message = { content: [] } @partial_json = {} @web_search_query = nil @completed = false end
Source
# File lib/riffer/providers/anthropic_compatible/messages_api/stream.rb, line 21 def self.reasoning_part(block) format = Riffer::Providers::AnthropicCompatible::MessagesAPI::REASONING_FORMAT case block[:type] when "thinking" Riffer::Messages::Assistant::ReasoningPart.new( type: :text, text: block[:thinking], signature: block[:signature], format: format, ) when "redacted_thinking" Riffer::Messages::Assistant::ReasoningPart.new(type: :encrypted, data: block[:data], format: format) end end
Source
# File lib/riffer/providers/anthropic_compatible/messages_api/stream.rb, line 39 def self.server_tool_part(block) call = block[:type] == "server_tool_use" Riffer::Messages::Assistant::ServerToolPart.new( name: call ? block[:name] : block[:type].delete_suffix("_tool_result"), kind: call ? :call : :result, data: JSON.generate(block), id: call ? block[:id] : block[:tool_use_id], format: Riffer::Providers::AnthropicCompatible::MessagesAPI::REASONING_FORMAT, ) end
Public Instance Methods
Source
# File lib/riffer/providers/anthropic_compatible/messages_api/stream.rb, line 80 def finish! raise Riffer::IncompleteStreamError, "Anthropic stream ended without a message_stop event" unless @completed @message end
The caller reads the text, finish reason and usage from here, since one turn can take several streams.
Source
# File lib/riffer/providers/anthropic_compatible/messages_api/stream.rb, line 64 def handle(event, yielder) case event[:type] when "message_start" then start_message(event) when "content_block_start" then start_block(event) when "content_block_delta" then apply_delta(event, yielder) when "content_block_stop" then stop_block(event, yielder) when "message_delta" then apply_message_delta(event) when "message_stop" then @completed = true when "error" then raise_stream_error(event) end end
Raises Riffer::APIError on a stream error event.