Class: RubynCode::LLM::Adapters::AnthropicStreaming
- Inherits:
-
Object
- Object
- RubynCode::LLM::Adapters::AnthropicStreaming
- Includes:
- JsonParsing
- Defined in:
- lib/rubyn_code/llm/adapters/anthropic_streaming.rb
Overview
SSE streaming parser for the Anthropic Messages API.
Handles Anthropic-specific event types: message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop, error. Accumulates content blocks and produces a normalized LLM::Response via #finalize.
Defined Under Namespace
Classes: Event, OverloadError, ParseError
Constant Summary collapse
- HANDLERS =
{ 'message_start' => :handle_message_start, 'content_block_start' => :handle_content_block_start, 'content_block_delta' => :handle_content_block_delta, 'content_block_stop' => :handle_content_block_stop, 'message_delta' => :handle_message_delta, 'message_stop' => :handle_message_stop, 'error' => :handle_error }.freeze
Instance Method Summary collapse
- #feed(chunk) ⇒ Object
- #finalize ⇒ Object
-
#initialize(&block) ⇒ AnthropicStreaming
constructor
A new instance of AnthropicStreaming.
Constructor Details
#initialize(&block) ⇒ AnthropicStreaming
Returns a new instance of AnthropicStreaming.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rubyn_code/llm/adapters/anthropic_streaming.rb', line 31 def initialize(&block) @callback = block @buffer = +'' @response_id = nil @content_blocks = [] @current_block_index = nil @current_text = +'' @current_tool_input_json = +'' @stop_reason = nil @usage = nil end |
Instance Method Details
#feed(chunk) ⇒ Object
43 44 45 46 |
# File 'lib/rubyn_code/llm/adapters/anthropic_streaming.rb', line 43 def feed(chunk) @buffer << chunk consume_events end |
#finalize ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/rubyn_code/llm/adapters/anthropic_streaming.rb', line 48 def finalize flush_pending_block Response.new( id: @response_id, content: @content_blocks.compact, stop_reason: @stop_reason, usage: @usage ) end |