Class: Silas::Session

Inherits:
ApplicationRecord show all
Defined in:
app/models/silas/session.rb

Constant Summary collapse

STATUSES =
%w[active archived].freeze

Instance Method Summary collapse

Instance Method Details

#active_turnObject



17
18
19
20
21
# File 'app/models/silas/session.rb', line 17

def active_turn
  # Fresh relation query, never the cached association — callers mix reads
  # with turn creation in the same objects.
  turns.where(status: Turn::ACTIVE_STATUSES).order(:index).first
end

#continue(input:, enqueue: true) ⇒ Object

Enqueue the next turn. One active turn per session — the partial unique index is the backstop; this is the friendly front door. enqueue: false creates the turn without scheduling it (callers that drive it themselves, e.g. an awaited handoff running the loop inline).



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/silas/session.rb', line 31

def continue(input:, enqueue: true)
  if active_turn
    raise TurnInProgressError, "session #{id} already has an active turn (##{active_turn.index})"
  end

  next_index = (turns.maximum(:index) || -1) + 1
  turn = turns.create!(index: next_index, input: input)
  AgentLoopJob.perform_later(turn.id) if enqueue
  turn
rescue ActiveRecord::RecordNotUnique
  raise TurnInProgressError, "session #{id} already has an active turn"
end

#pending_approvalsObject



23
24
25
# File 'app/models/silas/session.rb', line 23

def pending_approvals
  ToolInvocation.joins(:turn).where(silas_turns: { session_id: id }, approval_state: "required")
end