Class: Session
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Session
- Defined in:
- app/models/session.rb
Overview
A conversation session — the fundamental unit of agent interaction. Owns an ordered stream of Event records representing everything that happened: user messages, agent responses, tool calls, etc.
Constant Summary collapse
- DEFAULT_TOKEN_BUDGET =
Claude Sonnet 4 context window minus system prompt reserve.
190_000- VIEW_MODES =
%w[basic verbose debug].freeze
Instance Method Summary collapse
-
#messages_for_llm(token_budget: DEFAULT_TOKEN_BUDGET) ⇒ Array<Hash>
Builds the message array expected by the Anthropic Messages API.
-
#next_view_mode ⇒ String
Cycles to the next view mode: basic → verbose → debug → basic.
-
#system_prompt ⇒ String?
Returns the assembled system prompt for this session.
-
#viewport_events(token_budget: DEFAULT_TOKEN_BUDGET) ⇒ Array<Event>
Returns the events currently visible in the LLM context window.
Instance Method Details
#messages_for_llm(token_budget: DEFAULT_TOKEN_BUDGET) ⇒ Array<Hash>
Builds the message array expected by the Anthropic Messages API. Includes user/agent messages and tool call/response events in Anthropic’s wire format. Consecutive tool_call events are grouped into a single assistant message; consecutive tool_response events are grouped into a single user message with tool_result blocks.
64 65 66 |
# File 'app/models/session.rb', line 64 def (token_budget: DEFAULT_TOKEN_BUDGET) ((token_budget: token_budget)) end |
#next_view_mode ⇒ String
Cycles to the next view mode: basic → verbose → debug → basic.
21 22 23 24 |
# File 'app/models/session.rb', line 21 def next_view_mode current_index = VIEW_MODES.index(view_mode) || 0 VIEW_MODES[(current_index + 1) % VIEW_MODES.size] end |
#system_prompt ⇒ String?
Returns the assembled system prompt for this session. The system prompt includes system instructions, goals, and memories. Currently a placeholder — these subsystems are not yet implemented.
52 53 54 |
# File 'app/models/session.rb', line 52 def system_prompt nil end |
#viewport_events(token_budget: DEFAULT_TOKEN_BUDGET) ⇒ Array<Event>
Returns the events currently visible in the LLM context window. Walks events newest-first and includes them until the token budget is exhausted. Events are full-size or excluded entirely.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/session.rb', line 32 def (token_budget: DEFAULT_TOKEN_BUDGET) selected = [] remaining = token_budget events.context_events.reorder(id: :desc).each do |event| cost = (event.token_count > 0) ? event.token_count : estimate_tokens(event) break if cost > remaining && selected.any? selected << event remaining -= cost end selected.reverse end |