Class: AgentHarness::Api::RubyLlmChatAdapter::StreamState

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/api/ruby_llm_chat_adapter.rb

Overview

RubyLLM keys streamed tool-call chunks by a stream index while continuation chunks carry no call id, so correlation state must live between chunks. Also remembers the latest cumulative token counts so duplicate cumulative reports are not re-emitted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ StreamState

Returns a new instance of StreamState.



66
67
68
69
70
71
72
73
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 66

def initialize
  @provider_id_by_key = {}
  @started_ids = {}
  @latest_provider_id = nil
  @input_tokens = nil
  @output_tokens = nil
  @refusal = false
end

Instance Attribute Details

#input_tokens ⇒ Object

Returns the value of attribute input_tokens.



63
64
65
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 63

def input_tokens
  @input_tokens
end

#output_tokens ⇒ Object

Returns the value of attribute output_tokens.



63
64
65
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 63

def output_tokens
  @output_tokens
end

#refusal ⇒ Object (readonly)

Returns the value of attribute refusal.



64
65
66
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 64

def refusal
  @refusal
end

Instance Method Details

#observe(chunk) ⇒ Object



94
95
96
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 94

def observe(chunk)
  @refusal ||= chunk.is_a?(RubyLlmResponsesStreamingRefusal::RefusalChunk)
end

#provider_id(stream_key) ⇒ Object



85
86
87
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 85

def provider_id(stream_key)
  stream_key.nil? ? @latest_provider_id : @provider_id_by_key[stream_key]
end

#start(stream_key, provider_id) ⇒ Object

Links a stream chunk key to its provider call id, returning true only the first time provider_id is seen.



77
78
79
80
81
82
83
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 77

def start(stream_key, provider_id)
  @provider_id_by_key[stream_key] = provider_id unless stream_key.nil?
  @latest_provider_id = provider_id
  return false if @started_ids.key?(provider_id)

  @started_ids[provider_id] = true
end

#usage ⇒ Object



89
90
91
92
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 89

def usage
  total = (input_tokens + output_tokens) if input_tokens && output_tokens
  {input_tokens: input_tokens, output_tokens: output_tokens, total_tokens: total}
end