Class: Clacky::BedrockStreamAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/bedrock_stream_aggregator.rb

Overview

Reassembles a Bedrock Converse event stream into the same hash shape that MessageFormat::Bedrock.parse_response expects from a non-streaming response, while invoking on_chunk(input_tokens:, output_tokens:) as usage information accumulates.

Bedrock event-stream events handled (passed through as raw event JSON):

messageStart      → { role: "assistant" }
contentBlockStart → { start: {toolUse: {toolUseId, name}} | {}, contentBlockIndex: N }
contentBlockDelta → { delta: {text: "..."} | {toolUse: {input: "..."}}, contentBlockIndex: N }
contentBlockStop  → { contentBlockIndex: N }
messageStop       → { stopReason: "end_turn" | "tool_use" | "max_tokens" | ... }
metadata          → { usage: {inputTokens, outputTokens, cacheReadInputTokens, cacheWriteInputTokens}, metrics: {...} }

Tool-use input is streamed as a sequence of partial JSON strings; we concatenate and let the response parser leave it as a string for downstream tool dispatch (which calls JSON.parse with a {} fallback).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_chunk: nil) ⇒ BedrockStreamAggregator

Returns a new instance of BedrockStreamAggregator.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 24

def initialize(on_chunk: nil)
  @on_chunk = on_chunk
  @role = "assistant"
  @blocks = {}
  @stop_reason = nil
  @usage = {}
  @last_input_tokens = 0
  @last_output_tokens = 0
  @parse_failures = 0
  @frames_seen = 0
  @bytes_seen = 0
end

Instance Attribute Details

#bytes_seenObject (readonly)

Returns the value of attribute bytes_seen.



37
38
39
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 37

def bytes_seen
  @bytes_seen
end

#frames_seenObject (readonly)

Returns the value of attribute frames_seen.



37
38
39
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 37

def frames_seen
  @frames_seen
end

#parse_failuresObject (readonly)

Returns the value of attribute parse_failures.



37
38
39
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 37

def parse_failures
  @parse_failures
end

Instance Method Details

#handle(event, data_str) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 39

def handle(event, data_str)
  @bytes_seen += data_str.to_s.bytesize
  @frames_seen += 1
  data = parse_or_nil(data_str)
  return unless data

  case event
  when "messageStart"
    @role = data["role"] || @role
  when "contentBlockStart"
    idx = data["contentBlockIndex"] || @blocks.size
    start = data["start"] || {}
    if (tu = start["toolUse"])
      @blocks[idx] = { kind: :tool_use, id: tu["toolUseId"], name: tu["name"], input_str: +"" }
    else
      @blocks[idx] = { kind: :text, text: +"" }
    end
  when "contentBlockDelta"
    idx = data["contentBlockIndex"] || 0
    delta = data["delta"] || {}
    block = (@blocks[idx] ||= { kind: :text, text: +"" })
    if delta["text"]
      block[:kind] ||= :text
      block[:text] ||= +""
      block[:text] << delta["text"]
    elsif (tu = delta["toolUse"])
      block[:kind] = :tool_use
      block[:input_str] ||= +""
      block[:input_str] << tu["input"].to_s
      block[:id]   ||= tu["toolUseId"]
      block[:name] ||= tu["name"]
    elsif (rc = delta["reasoningContent"])
      block[:kind] = :reasoning
      block[:reasoning] ||= +""
      block[:reasoning] << rc["text"].to_s
    end
    emit_estimate_progress
  when "contentBlockStop"
    # Nothing to assemble: blocks are kept as-is until messageStop.
  when "messageStop"
    @stop_reason = data["stopReason"] || @stop_reason
  when "metadata"
    if (u = data["usage"])
      @usage.merge!(u)
      emit_usage_progress(u)
    end
  end
end

#to_hObject

Render the canonical non-streaming Bedrock response hash so the existing MessageFormat::Bedrock.parse_response can consume it unchanged.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/clacky/bedrock_stream_aggregator.rb', line 90

def to_h
  content_blocks = @blocks.keys.sort.map do |idx|
    b = @blocks[idx]
    case b[:kind]
    when :tool_use
      input_value = b[:input_str].to_s.empty? ? {} : (JSON.parse(b[:input_str]) rescue b[:input_str])
      { "toolUse" => { "toolUseId" => b[:id], "name" => b[:name], "input" => input_value } }
    else
      { "text" => b[:text].to_s }
    end
  end

  {
    "output"     => { "message" => { "role" => @role, "content" => content_blocks } },
    "stopReason" => @stop_reason,
    "usage"      => @usage
  }
end