Class: MessageDecorator::MessagePayload
- Inherits:
-
Struct
- Object
- Struct
- MessageDecorator::MessagePayload
- Defined in:
- app/decorators/message_decorator.rb
Overview
Normalizes hash payloads into a Message-like interface so decorators can use #payload, #message_type, etc. uniformly on both AR models and raw EventBus hashes.
Instance Attribute Summary collapse
-
#message_type ⇒ Object
readonly
the message’s type (e.g. “user_message”).
-
#payload ⇒ Object
readonly
string-keyed hash of message 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 Message#estimate_tokens so decorators can call it uniformly on both AR models and hash payloads.
Instance Attribute Details
#message_type ⇒ Object (readonly)
the message’s type (e.g. “user_message”)
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/decorators/message_decorator.rb', line 57 MessagePayload = Struct.new(:message_type, :payload, :timestamp, :token_count, keyword_init: true) do # Heuristic token estimate matching {Message#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 .to_s.in?(%w[tool_call tool_response]) payload.to_json else payload&.dig("content").to_s end [(text.bytesize / Message::BYTES_PER_TOKEN.to_f).ceil, 1].max end end |
#payload ⇒ Object (readonly)
string-keyed hash of message data
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/decorators/message_decorator.rb', line 57 MessagePayload = Struct.new(:message_type, :payload, :timestamp, :token_count, keyword_init: true) do # Heuristic token estimate matching {Message#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 .to_s.in?(%w[tool_call tool_response]) payload.to_json else payload&.dig("content").to_s end [(text.bytesize / Message::BYTES_PER_TOKEN.to_f).ceil, 1].max end end |
#timestamp ⇒ Object (readonly)
nanosecond-precision timestamp
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/decorators/message_decorator.rb', line 57 MessagePayload = Struct.new(:message_type, :payload, :timestamp, :token_count, keyword_init: true) do # Heuristic token estimate matching {Message#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 .to_s.in?(%w[tool_call tool_response]) payload.to_json else payload&.dig("content").to_s end [(text.bytesize / Message::BYTES_PER_TOKEN.to_f).ceil, 1].max end end |
#token_count ⇒ Object (readonly)
cumulative token count
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/decorators/message_decorator.rb', line 57 MessagePayload = Struct.new(:message_type, :payload, :timestamp, :token_count, keyword_init: true) do # Heuristic token estimate matching {Message#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 .to_s.in?(%w[tool_call tool_response]) payload.to_json else payload&.dig("content").to_s end [(text.bytesize / Message::BYTES_PER_TOKEN.to_f).ceil, 1].max end end |
Instance Method Details
#estimate_tokens ⇒ Integer
Heuristic token estimate matching Message#estimate_tokens so decorators can call it uniformly on both AR models and hash payloads.
61 62 63 64 65 66 67 68 |
# File 'app/decorators/message_decorator.rb', line 61 def estimate_tokens text = if .to_s.in?(%w[tool_call tool_response]) payload.to_json else payload&.dig("content").to_s end [(text.bytesize / Message::BYTES_PER_TOKEN.to_f).ceil, 1].max end |