Class: EventDecorator::EventPayload
- Inherits:
-
Struct
- Object
- Struct
- EventDecorator::EventPayload
- 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
-
#event_type ⇒ Object
readonly
the event’s type (e.g. “user_message”).
-
#payload ⇒ Object
readonly
string-keyed hash of event data.
-
#timestamp ⇒ Object
readonly
nanosecond-precision timestamp.
-
#token_count ⇒ Object
readonly
cumulative token count.
Instance Method Summary collapse
-
#estimate_tokens ⇒ Integer
Heuristic token estimate matching Event#estimate_tokens so decorators can call it uniformly on both AR models and hash payloads.
Instance Attribute Details
#event_type ⇒ Object (readonly)
the event’s type (e.g. “user_message”)
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/decorators/event_decorator.rb', line 50 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 |
#payload ⇒ Object (readonly)
string-keyed hash of event data
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/decorators/event_decorator.rb', line 50 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 |
#timestamp ⇒ Object (readonly)
nanosecond-precision timestamp
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/decorators/event_decorator.rb', line 50 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_count ⇒ Object (readonly)
cumulative token count
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/decorators/event_decorator.rb', line 50 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_tokens ⇒ Integer
Heuristic token estimate matching Event#estimate_tokens so decorators can call it uniformly on both AR models and hash payloads.
54 55 56 57 58 59 60 61 |
# File 'app/decorators/event_decorator.rb', line 54 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 |