Class: Anthropic::BetaRefusalFallbackMiddleware::BlockTracker
- Inherits:
-
Object
- Object
- Anthropic::BetaRefusalFallbackMiddleware::BlockTracker
- Defined in:
- lib/anthropic/helpers/refusal_fallback.rb
Overview
Block bookkeeping for one stream of the splice: accumulates each content
block from its deltas (for the continuation prefill), shifts wire indices
by index_base so they stay monotonic across hops, and tracks which
blocks are still open so a refusal that cuts mid-block can close them.
Instance Attribute Summary collapse
-
#next_index ⇒ Integer
readonly
One past the highest shifted block index seen.
Instance Method Summary collapse
- #close_open_blocks(y) ⇒ Object
-
#content_blocks ⇒ Array<Hash>
The accumulated content blocks, in start order.
- #delta(event) ⇒ Object
-
#initialize(index_base = 0) ⇒ BlockTracker
constructor
A new instance of BlockTracker.
- #start(event) ⇒ Object
- #stop(event) ⇒ Object
Constructor Details
#initialize(index_base = 0) ⇒ BlockTracker
Returns a new instance of BlockTracker.
632 633 634 635 636 637 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 632 def initialize(index_base = 0) @index_base = index_base @next_index = index_base @blocks = [] @open = [] end |
Instance Attribute Details
#next_index ⇒ Integer (readonly)
Returns one past the highest shifted block index seen.
629 630 631 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 629 def next_index @next_index end |
Instance Method Details
#close_open_blocks(y) ⇒ Object
664 665 666 667 668 669 670 671 672 673 674 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 664 def close_open_blocks(y) @open.each do |index| y << "event: content_block_stop\ndata: #{JSON.generate( { type: 'content_block_stop', index: index } )}\n\n" end @open.clear end |
#content_blocks ⇒ Array<Hash>
Returns the accumulated content blocks, in start order.
640 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 640 def content_blocks = @blocks.map { _1[:block] } |
#delta(event) ⇒ Object
651 652 653 654 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 651 def delta(event) apply_delta(event["index"], event["delta"]) event["index"] += @index_base end |
#start(event) ⇒ Object
643 644 645 646 647 648 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 643 def start(event) @blocks << {index: event["index"], block: deep_dup(event["content_block"])} event["index"] += @index_base @open << event["index"] @next_index = [@next_index, event["index"] + 1].max end |
#stop(event) ⇒ Object
657 658 659 660 661 |
# File 'lib/anthropic/helpers/refusal_fallback.rb', line 657 def stop(event) event["index"] += @index_base @open.delete(event["index"]) @next_index = [@next_index, event["index"] + 1].max end |