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



11
12
13
14
15
# File 'app/models/silas/session.rb', line 11

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).



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/silas/session.rb', line 25

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



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

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