Class: RubyLLM::Contract::Pipeline::Trace

Inherits:
Object
  • Object
show all
Includes:
Concerns::DeepFreeze, Concerns::TraceEquality
Defined in:
lib/ruby_llm/contract/pipeline/trace.rb

Constant Summary collapse

KNOWN_KEYS =
%i[trace_id total_latency_ms total_usage step_traces total_cost].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::TraceEquality

#==

Constructor Details

#initialize(trace_id: nil, total_latency_ms: nil, total_usage: nil, step_traces: nil) ⇒ Trace

Returns a new instance of Trace.



12
13
14
15
16
17
18
19
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 12

def initialize(trace_id: nil, total_latency_ms: nil, total_usage: nil, step_traces: nil)
  @trace_id = trace_id
  @total_latency_ms = total_latency_ms
  @total_usage = deep_dup_freeze(total_usage)
  @step_traces = step_traces&.dup&.freeze
  @total_cost = calculate_total_cost
  freeze
end

Instance Attribute Details

#step_tracesObject (readonly)

Returns the value of attribute step_traces.



10
11
12
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 10

def step_traces
  @step_traces
end

#total_costObject (readonly)

Returns the value of attribute total_cost.



10
11
12
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 10

def total_cost
  @total_cost
end

#total_latency_msObject (readonly)

Returns the value of attribute total_latency_ms.



10
11
12
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 10

def total_latency_ms
  @total_latency_ms
end

#total_usageObject (readonly)

Returns the value of attribute total_usage.



10
11
12
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 10

def total_usage
  @total_usage
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



10
11
12
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 10

def trace_id
  @trace_id
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
26
27
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 23

def [](key)
  return nil unless KNOWN_KEYS.include?(key.to_sym)

  public_send(key)
end

#dig(key, *rest) ⇒ Object



29
30
31
32
33
34
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 29

def dig(key, *rest)
  value = self[key]
  return value if rest.empty? || value.nil?

  value.dig(*rest)
end

#to_hObject



36
37
38
39
40
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 36

def to_h
  { trace_id: @trace_id, total_latency_ms: @total_latency_ms,
    total_usage: @total_usage, step_traces: @step_traces,
    total_cost: @total_cost }.compact
end

#to_sObject



42
43
44
# File 'lib/ruby_llm/contract/pipeline/trace.rb', line 42

def to_s
  build_summary_parts.join(" ")
end