Class: EventDecorator::EventPayload

Inherits:
Struct
  • Object
show all
Defined in:
app/decorators/event_decorator.rb

Overview

Normalizes hash payloads into an Event-like interface so decorators can use #payload, #event_type, etc. uniformly on both AR models and raw EventBus hashes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_typeObject (readonly)

the event’s type (e.g. “user_message”)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/decorators/event_decorator.rb', line 57

EventPayload = Struct.new(:event_type, :payload, :timestamp, :token_count, keyword_init: true) do
  # Heuristic token estimate matching {Event#estimate_tokens} so decorators
  # can call it uniformly on both AR models and hash payloads.
  # @return [Integer] at least 1
  def estimate_tokens
    text = if event_type.to_s.in?(%w[tool_call tool_response])
      payload.to_json
    else
      payload&.dig("content").to_s
    end
    [(text.bytesize / Event::BYTES_PER_TOKEN.to_f).ceil, 1].max
  end
end

#payloadObject (readonly)

string-keyed hash of event data



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/decorators/event_decorator.rb', line 57

EventPayload = Struct.new(:event_type, :payload, :timestamp, :token_count, keyword_init: true) do
  # Heuristic token estimate matching {Event#estimate_tokens} so decorators
  # can call it uniformly on both AR models and hash payloads.
  # @return [Integer] at least 1
  def estimate_tokens
    text = if event_type.to_s.in?(%w[tool_call tool_response])
      payload.to_json
    else
      payload&.dig("content").to_s
    end
    [(text.bytesize / Event::BYTES_PER_TOKEN.to_f).ceil, 1].max
  end
end

#timestampObject (readonly)

nanosecond-precision timestamp



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/decorators/event_decorator.rb', line 57

EventPayload = Struct.new(:event_type, :payload, :timestamp, :token_count, keyword_init: true) do
  # Heuristic token estimate matching {Event#estimate_tokens} so decorators
  # can call it uniformly on both AR models and hash payloads.
  # @return [Integer] at least 1
  def estimate_tokens
    text = if event_type.to_s.in?(%w[tool_call tool_response])
      payload.to_json
    else
      payload&.dig("content").to_s
    end
    [(text.bytesize / Event::BYTES_PER_TOKEN.to_f).ceil, 1].max
  end
end

#token_countObject (readonly)

cumulative token count



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/decorators/event_decorator.rb', line 57

EventPayload = Struct.new(:event_type, :payload, :timestamp, :token_count, keyword_init: true) do
  # Heuristic token estimate matching {Event#estimate_tokens} so decorators
  # can call it uniformly on both AR models and hash payloads.
  # @return [Integer] at least 1
  def estimate_tokens
    text = if event_type.to_s.in?(%w[tool_call tool_response])
      payload.to_json
    else
      payload&.dig("content").to_s
    end
    [(text.bytesize / Event::BYTES_PER_TOKEN.to_f).ceil, 1].max
  end
end

Instance Method Details

#estimate_tokensInteger

Heuristic token estimate matching Event#estimate_tokens so decorators can call it uniformly on both AR models and hash payloads.

Returns:

  • (Integer)

    at least 1



61
62
63
64
65
66
67
68
# File 'app/decorators/event_decorator.rb', line 61

def estimate_tokens
  text = if event_type.to_s.in?(%w[tool_call tool_response])
    payload.to_json
  else
    payload&.dig("content").to_s
  end
  [(text.bytesize / Event::BYTES_PER_TOKEN.to_f).ceil, 1].max
end