Class: Event
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Event
- Defined in:
- app/models/event.rb
Overview
A persisted record of something that happened during a session. Events are the single source of truth for conversation history —there is no separate chat log, only events attached to a session.
Constant Summary collapse
- TYPES =
%w[system_message user_message agent_message tool_call tool_response].freeze
- LLM_TYPES =
%w[user_message agent_message].freeze
- CONTEXT_TYPES =
%w[user_message agent_message tool_call tool_response].freeze
- ROLE_MAP =
{"user_message" => "user", "agent_message" => "assistant"}.freeze
Instance Attribute Summary collapse
-
#event_type ⇒ String
One of TYPES: system_message, user_message, agent_message, tool_call, tool_response.
-
#payload ⇒ Hash
Event-specific data (content, tool_name, tool_input, etc.).
-
#timestamp ⇒ Integer
Nanoseconds since epoch (Process::CLOCK_REALTIME).
-
#token_count ⇒ Integer
Cached token count for this event’s payload (0 until counted).
-
#tool_use_id ⇒ String?
Anthropic-assigned ID correlating tool_call and tool_response.
Class Method Summary collapse
-
.context_events ⇒ ActiveRecord::Relation
Events included in the LLM context window (messages + tool interactions).
-
.llm_messages ⇒ ActiveRecord::Relation
Events that represent conversation turns sent to the LLM API.
Instance Method Summary collapse
-
#api_role ⇒ String
Maps event_type to the Anthropic Messages API role.
-
#context_event? ⇒ Boolean
True if this event is part of the LLM context window.
-
#llm_message? ⇒ Boolean
True if this event represents an LLM conversation turn.
Instance Attribute Details
#event_type ⇒ String
Returns one of TYPES: system_message, user_message, agent_message, tool_call, tool_response.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/event.rb', line 18 class Event < ApplicationRecord TYPES = %w[system_message user_message agent_message tool_call tool_response].freeze LLM_TYPES = %w[user_message agent_message].freeze CONTEXT_TYPES = %w[user_message agent_message tool_call tool_response].freeze ROLE_MAP = {"user_message" => "user", "agent_message" => "assistant"}.freeze belongs_to :session validates :event_type, presence: true, inclusion: {in: TYPES} validates :payload, presence: true validates :timestamp, presence: true after_create :schedule_token_count, if: :llm_message? # @!method self.llm_messages # Events that represent conversation turns sent to the LLM API. # @return [ActiveRecord::Relation] scope :llm_messages, -> { where(event_type: LLM_TYPES) } # @!method self.context_events # Events included in the LLM context window (messages + tool interactions). # @return [ActiveRecord::Relation] scope :context_events, -> { where(event_type: CONTEXT_TYPES) } # Maps event_type to the Anthropic Messages API role. # @return [String] "user" or "assistant" def api_role ROLE_MAP.fetch(event_type) end # @return [Boolean] true if this event represents an LLM conversation turn def event_type.in?(LLM_TYPES) end # @return [Boolean] true if this event is part of the LLM context window def context_event? event_type.in?(CONTEXT_TYPES) end private def schedule_token_count CountEventTokensJob.perform_later(id) end end |
#payload ⇒ Hash
Returns event-specific data (content, tool_name, tool_input, etc.).
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/event.rb', line 18 class Event < ApplicationRecord TYPES = %w[system_message user_message agent_message tool_call tool_response].freeze LLM_TYPES = %w[user_message agent_message].freeze CONTEXT_TYPES = %w[user_message agent_message tool_call tool_response].freeze ROLE_MAP = {"user_message" => "user", "agent_message" => "assistant"}.freeze belongs_to :session validates :event_type, presence: true, inclusion: {in: TYPES} validates :payload, presence: true validates :timestamp, presence: true after_create :schedule_token_count, if: :llm_message? # @!method self.llm_messages # Events that represent conversation turns sent to the LLM API. # @return [ActiveRecord::Relation] scope :llm_messages, -> { where(event_type: LLM_TYPES) } # @!method self.context_events # Events included in the LLM context window (messages + tool interactions). # @return [ActiveRecord::Relation] scope :context_events, -> { where(event_type: CONTEXT_TYPES) } # Maps event_type to the Anthropic Messages API role. # @return [String] "user" or "assistant" def api_role ROLE_MAP.fetch(event_type) end # @return [Boolean] true if this event represents an LLM conversation turn def event_type.in?(LLM_TYPES) end # @return [Boolean] true if this event is part of the LLM context window def context_event? event_type.in?(CONTEXT_TYPES) end private def schedule_token_count CountEventTokensJob.perform_later(id) end end |
#timestamp ⇒ Integer
Returns nanoseconds since epoch (Process::CLOCK_REALTIME).
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/event.rb', line 18 class Event < ApplicationRecord TYPES = %w[system_message user_message agent_message tool_call tool_response].freeze LLM_TYPES = %w[user_message agent_message].freeze CONTEXT_TYPES = %w[user_message agent_message tool_call tool_response].freeze ROLE_MAP = {"user_message" => "user", "agent_message" => "assistant"}.freeze belongs_to :session validates :event_type, presence: true, inclusion: {in: TYPES} validates :payload, presence: true validates :timestamp, presence: true after_create :schedule_token_count, if: :llm_message? # @!method self.llm_messages # Events that represent conversation turns sent to the LLM API. # @return [ActiveRecord::Relation] scope :llm_messages, -> { where(event_type: LLM_TYPES) } # @!method self.context_events # Events included in the LLM context window (messages + tool interactions). # @return [ActiveRecord::Relation] scope :context_events, -> { where(event_type: CONTEXT_TYPES) } # Maps event_type to the Anthropic Messages API role. # @return [String] "user" or "assistant" def api_role ROLE_MAP.fetch(event_type) end # @return [Boolean] true if this event represents an LLM conversation turn def event_type.in?(LLM_TYPES) end # @return [Boolean] true if this event is part of the LLM context window def context_event? event_type.in?(CONTEXT_TYPES) end private def schedule_token_count CountEventTokensJob.perform_later(id) end end |
#token_count ⇒ Integer
Returns cached token count for this event’s payload (0 until counted).
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/event.rb', line 18 class Event < ApplicationRecord TYPES = %w[system_message user_message agent_message tool_call tool_response].freeze LLM_TYPES = %w[user_message agent_message].freeze CONTEXT_TYPES = %w[user_message agent_message tool_call tool_response].freeze ROLE_MAP = {"user_message" => "user", "agent_message" => "assistant"}.freeze belongs_to :session validates :event_type, presence: true, inclusion: {in: TYPES} validates :payload, presence: true validates :timestamp, presence: true after_create :schedule_token_count, if: :llm_message? # @!method self.llm_messages # Events that represent conversation turns sent to the LLM API. # @return [ActiveRecord::Relation] scope :llm_messages, -> { where(event_type: LLM_TYPES) } # @!method self.context_events # Events included in the LLM context window (messages + tool interactions). # @return [ActiveRecord::Relation] scope :context_events, -> { where(event_type: CONTEXT_TYPES) } # Maps event_type to the Anthropic Messages API role. # @return [String] "user" or "assistant" def api_role ROLE_MAP.fetch(event_type) end # @return [Boolean] true if this event represents an LLM conversation turn def event_type.in?(LLM_TYPES) end # @return [Boolean] true if this event is part of the LLM context window def context_event? event_type.in?(CONTEXT_TYPES) end private def schedule_token_count CountEventTokensJob.perform_later(id) end end |
#tool_use_id ⇒ String?
Returns Anthropic-assigned ID correlating tool_call and tool_response.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/event.rb', line 18 class Event < ApplicationRecord TYPES = %w[system_message user_message agent_message tool_call tool_response].freeze LLM_TYPES = %w[user_message agent_message].freeze CONTEXT_TYPES = %w[user_message agent_message tool_call tool_response].freeze ROLE_MAP = {"user_message" => "user", "agent_message" => "assistant"}.freeze belongs_to :session validates :event_type, presence: true, inclusion: {in: TYPES} validates :payload, presence: true validates :timestamp, presence: true after_create :schedule_token_count, if: :llm_message? # @!method self.llm_messages # Events that represent conversation turns sent to the LLM API. # @return [ActiveRecord::Relation] scope :llm_messages, -> { where(event_type: LLM_TYPES) } # @!method self.context_events # Events included in the LLM context window (messages + tool interactions). # @return [ActiveRecord::Relation] scope :context_events, -> { where(event_type: CONTEXT_TYPES) } # Maps event_type to the Anthropic Messages API role. # @return [String] "user" or "assistant" def api_role ROLE_MAP.fetch(event_type) end # @return [Boolean] true if this event represents an LLM conversation turn def event_type.in?(LLM_TYPES) end # @return [Boolean] true if this event is part of the LLM context window def context_event? event_type.in?(CONTEXT_TYPES) end private def schedule_token_count CountEventTokensJob.perform_later(id) end end |
Class Method Details
.context_events ⇒ ActiveRecord::Relation
Events included in the LLM context window (messages + tool interactions).
41 |
# File 'app/models/event.rb', line 41 scope :context_events, -> { where(event_type: CONTEXT_TYPES) } |
.llm_messages ⇒ ActiveRecord::Relation
Events that represent conversation turns sent to the LLM API.
36 |
# File 'app/models/event.rb', line 36 scope :llm_messages, -> { where(event_type: LLM_TYPES) } |
Instance Method Details
#api_role ⇒ String
Maps event_type to the Anthropic Messages API role.
45 46 47 |
# File 'app/models/event.rb', line 45 def api_role ROLE_MAP.fetch(event_type) end |
#context_event? ⇒ Boolean
Returns true if this event is part of the LLM context window.
55 56 57 |
# File 'app/models/event.rb', line 55 def context_event? event_type.in?(CONTEXT_TYPES) end |
#llm_message? ⇒ Boolean
Returns true if this event represents an LLM conversation turn.
50 51 52 |
# File 'app/models/event.rb', line 50 def event_type.in?(LLM_TYPES) end |