Class: RubyLLM::Protocol::StreamAccumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/protocol/stream_accumulator.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamAccumulator

Returns a new instance of StreamAccumulator.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 11

def initialize
  @content = +''
  @citations = []
  @thinking_text = nil
  @thinking_signature = nil
  @tool_calls = {}
  @input_tokens = nil
  @output_tokens = nil
  @cache_read_tokens = nil
  @cache_write_tokens = nil
  @thinking_tokens = nil
  @server_tool_use = nil
  @reported_cost = nil
  @server_tool_calls = []
  @raw_content = nil
  @raw_reasoning = nil
  @finish_reason = nil
  @latest_tool_call_id = nil
  @tool_call_ids_by_index = {}
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 9

def content
  @content
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 9

def model
  @model
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls.



9
10
11
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 9

def tool_calls
  @tool_calls
end

Instance Method Details

#add(chunk) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 32

def add(chunk)
  RubyLLM.logger.debug { chunk.inspect } if RubyLLM.config.log_stream_debug
  @model = chunk.model if @model.to_s.empty?

  handle_chunk_content(chunk)
  accumulate_citations(chunk.citations)
  append_thinking_from_chunk(chunk)
  accumulate_server_tool_calls(chunk.server_tool_calls)
  @raw_content = chunk.raw_content if chunk.raw_content
  @raw_reasoning = chunk.raw_reasoning if chunk.raw_reasoning
  @finish_reason = chunk.finish_reason if chunk.finish_reason
  count_tokens chunk
  RubyLLM.logger.debug { inspect } if RubyLLM.config.log_stream_debug
end

#to_message(response) ⇒ Object



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
# File 'lib/ruby_llm/protocol/stream_accumulator.rb', line 47

def to_message(response)
  Message.new(
    role: :assistant,
    content: content.empty? ? nil : content,
    citations: resolved_citations,
    thinking: Thinking.build(
      text: @thinking_text,
      signature: @thinking_signature
    ),
    tokens: Tokens.new(
      input: @input_tokens,
      output: @output_tokens,
      cache_read: @cache_read_tokens,
      cache_write: @cache_write_tokens,
      thinking: @thinking_tokens,
      server_tool_use: @server_tool_use,
      reported_cost: @reported_cost
    ),
    server_tool_calls: @server_tool_calls,
    raw_content: @raw_content,
    raw_reasoning: @raw_reasoning,
    finish_reason: @finish_reason,
    model: model,
    tool_calls: tool_calls_from_stream(response),
    raw: response
  )
end