Class: RailsErrorDashboard::ValueObjects::LlmCallEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/value_objects/llm_call_event.rb

Overview

Immutable value object representing a single LLM call observed in the host application. One canonical shape that all capture paths (OTel SpanProcessor, Faraday middleware, future native subscribers) normalize to before handing off to BreadcrumbCollector.

Fields are read-only after initialization. Unknown / unavailable fields are nil — never raise, never block, never allocate large strings.

Constant Summary collapse

STATUSES =
[ :success, :error, :timeout ].freeze
MAX_TOOL_ARG_LENGTH =
500
MAX_TOOL_RESULT_LENGTH =
500
MAX_ERROR_MESSAGE_LENGTH =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, model:, status:, input_tokens: nil, output_tokens: nil, duration_ms: nil, error_class: nil, error_message: nil, tool_name: nil, tool_arguments: nil, tool_result: nil, cost_usd_estimate: nil) ⇒ LlmCallEvent

Returns a new instance of LlmCallEvent.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 23

def initialize(provider:, model:, status:,
               input_tokens: nil, output_tokens: nil, duration_ms: nil,
               error_class: nil, error_message: nil,
               tool_name: nil, tool_arguments: nil, tool_result: nil,
               cost_usd_estimate: nil)
  @provider = provider.to_s
  @model = model.to_s
  @status = STATUSES.include?(status) ? status : :success
  @input_tokens = input_tokens
  @output_tokens = output_tokens
  @duration_ms = duration_ms
  @error_class = error_class
  @error_message = truncate(error_message, MAX_ERROR_MESSAGE_LENGTH)
  @tool_name = tool_name
  @tool_arguments_truncated = truncate(tool_arguments, MAX_TOOL_ARG_LENGTH)
  @tool_result_truncated = truncate(tool_result, MAX_TOOL_RESULT_LENGTH)
  @cost_usd_estimate = cost_usd_estimate
  freeze
end

Instance Attribute Details

#cost_usd_estimateObject (readonly)

Returns the value of attribute cost_usd_estimate.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def cost_usd_estimate
  @cost_usd_estimate
end

#duration_msObject (readonly)

Returns the value of attribute duration_ms.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def duration_ms
  @duration_ms
end

#error_classObject (readonly)

Returns the value of attribute error_class.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def error_message
  @error_message
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def input_tokens
  @input_tokens
end

#modelObject (readonly)

Returns the value of attribute model.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def model
  @model
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def output_tokens
  @output_tokens
end

#providerObject (readonly)

Returns the value of attribute provider.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def provider
  @provider
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def status
  @status
end

#tool_arguments_truncatedObject (readonly)

Returns the value of attribute tool_arguments_truncated.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def tool_arguments_truncated
  @tool_arguments_truncated
end

#tool_nameObject (readonly)

Returns the value of attribute tool_name.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def tool_name
  @tool_name
end

#tool_result_truncatedObject (readonly)

Returns the value of attribute tool_result_truncated.



18
19
20
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 18

def tool_result_truncated
  @tool_result_truncated
end

Instance Method Details

#to_breadcrumb_messageObject

Short human-readable message for the breadcrumb (rendered in UI).



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 67

def to_breadcrumb_message
  if tool_call?
    "tool: #{@tool_name}"
  else
    parts = [ @provider, @model ]
    if @input_tokens && @output_tokens
      parts << "in:#{@input_tokens}/out:#{@output_tokens}"
    end
    parts << @status.to_s if @status != :success
    parts.compact.join(" · ")
  end
end

#to_breadcrumb_metadataObject

Hash shape passed to BreadcrumbCollector.add(…, metadata:). Only includes non-nil keys — keeps the breadcrumb JSON compact.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 49

def 
  {
    provider: @provider,
    model: @model,
    status: @status.to_s,
    input_tokens: @input_tokens,
    output_tokens: @output_tokens,
    duration_ms: @duration_ms,
    error_class: @error_class,
    error_message: @error_message,
    tool_name: @tool_name,
    tool_arguments: @tool_arguments_truncated,
    tool_result: @tool_result_truncated,
    cost_usd: @cost_usd_estimate
  }.compact
end

#tool_call?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rails_error_dashboard/value_objects/llm_call_event.rb', line 43

def tool_call?
  !@tool_name.nil?
end