Class: Xeno::SessionState

Inherits:
Object
  • Object
show all
Defined in:
lib/xeno/session_state.rb

Overview

The per-session KV store: working state for ONE conversation. JSON-typed — values round-trip through JSON, so symbols become strings and anything unserializable is rejected up front. Reads always hit the database (a tool in a fresh worker sees what the last worker wrote); writes take the session row lock, so read-modify-write merges never lose entries. Cleared by reset; it is NOT long-term memory — durable knowledge belongs in your own models.

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ SessionState

Returns a new instance of SessionState.



10
11
12
# File 'lib/xeno/session_state.rb', line 10

def initialize(session)
  @session = session
end

Instance Method Details

#delete(key) ⇒ Object



38
39
40
41
42
43
# File 'lib/xeno/session_state.rb', line 38

def delete(key)
  @session.with_lock do
    @session.update!(state: (@session[:state] || {}).except(key.to_s))
  end
  self
end

#get(key) ⇒ Object Also known as: []



14
15
16
# File 'lib/xeno/session_state.rb', line 14

def get(key)
  current[key.to_s]
end

#set(key, value) ⇒ Object Also known as: []=



33
34
35
# File 'lib/xeno/session_state.rb', line 33

def set(key, value)
  update(key => value)
end

#to_hObject



19
20
21
# File 'lib/xeno/session_state.rb', line 19

def to_h
  current
end

#update(entries) ⇒ Object

Merges the entries in one locked write. Keys stringify; values must be JSON-serializable (Xeno::Error otherwise).



25
26
27
28
29
30
31
# File 'lib/xeno/session_state.rb', line 25

def update(entries)
  normalized = normalize(entries)
  @session.with_lock do
    @session.update!(state: (@session[:state] || {}).merge(normalized))
  end
  self
end