Class: Relay::Models::Context
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Relay::Models::Context
- Includes:
- Relay::Model
- Defined in:
- app/models/context.rb
Overview
The Context model stores the accumulated model context for a user, provider, and model combination. It persists the underlying LLM::Session state as JSON in the “data” column so future turns can continue from the same context window.
Instance Method Summary collapse
-
#compacted? ⇒ Boolean
Returns true when the underlying llm.rb context is in a post-compaction state.
-
#compactor ⇒ LLM::Compactor
Returns the runtime compactor for the persisted context.
- #context_window ⇒ Integer
-
#mcps ⇒ Array<Relay::Models::MCP>
Enabled MCP servers for this context’s user.
-
#messages ⇒ Array<Hash>
Returns persisted user and assistant messages.
-
#title ⇒ String?
Returns the first persisted user message content.
Methods included from Relay::Model
Instance Method Details
#compacted? ⇒ Boolean
Returns true when the underlying llm.rb context is in a post-compaction state.
41 42 43 |
# File 'app/models/context.rb', line 41 def compacted? ctx.compacted? end |
#compactor ⇒ LLM::Compactor
Returns the runtime compactor for the persisted context.
33 34 35 |
# File 'app/models/context.rb', line 33 def compactor ctx.compactor end |
#context_window ⇒ Integer
61 62 63 64 65 |
# File 'app/models/context.rb', line 61 def context_window super rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError 0 end |
#mcps ⇒ Array<Relay::Models::MCP>
Returns Enabled MCP servers for this context’s user.
26 27 28 |
# File 'app/models/context.rb', line 26 def mcps user ? user.mcps_dataset.where(enabled: true).all : [] end |
#messages ⇒ Array<Hash>
This method excludes tool calls, tool returns, and system messages. It is safe to render in the UI.
Returns persisted user and assistant messages
51 52 53 54 55 56 57 |
# File 'app/models/context.rb', line 51 def ctx..filter_map do || next if .tool_call? || .tool_return? || .compaction? next unless .user? || .assistant? {role: .role.to_sym, content: .content.to_s} end end |
#title ⇒ String?
Returns the first persisted user message content.
19 20 21 |
# File 'app/models/context.rb', line 19 def title ctx..find(&:user?)&.content end |