Class: Relay::Models::Context

Inherits:
Sequel::Model
  • Object
show all
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

Methods included from Relay::Model

included

Instance Method Details

#compacted?Boolean

Returns true when the underlying llm.rb context is in a post-compaction state.

Returns:

  • (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

#compactorLLM::Compactor

Returns the runtime compactor for the persisted context.

Returns:

  • (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_windowInteger

Returns:

  • (Integer)


61
62
63
64
65
# File 'app/models/context.rb', line 61

def context_window
  super
rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError
  0
end

#mcpsArray<Relay::Models::MCP>

Returns Enabled MCP servers for this context’s user.

Returns:



26
27
28
# File 'app/models/context.rb', line 26

def mcps
  user ? user.mcps_dataset.where(enabled: true).all : []
end

#messagesArray<Hash>

Note:

This method excludes tool calls and system messages. It is safe to render in the UI.

Returns persisted user and assistant messages

Returns:

  • (Array<Hash>)

    Returns persisted user and assistant messages



51
52
53
54
55
56
57
# File 'app/models/context.rb', line 51

def messages
  ctx.messages.filter_map do |message|
    next if message.tool_call? || message.compaction?
    next unless message.user? || message.assistant?
    {role: message.role.to_sym, content: message.content.to_s}
  end
end

#titleString?

Returns the first persisted user message content.

Returns:

  • (String, nil)

    Returns the first persisted user message content.



19
20
21
# File 'app/models/context.rb', line 19

def title
  ctx.messages.find(&:user?)&.content
end