Class: Llmemory::WorkingMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/llmemory/working_memory.rb

Overview

CoALA’s “working memory”: a structured, symbolic scratch space that persists across LLM calls within a session — distinct from the raw message buffer (Checkpoint). It is the central hub an agent reads from and writes to while reasoning (goals, current task, retrieved context, intermediate reasoning, last observation, free-form scratchpad), plus arbitrary custom slots.

Backed by the same pluggable short-term stores as Checkpoint, but under a namespaced session key so working-memory slots never collide with messages.

Constant Summary collapse

DEFAULT_SESSION_ID =
"default"
SESSION_SUFFIX =
":working_memory"
SLOTS =
%i[goals current_task retrieved_context scratchpad last_observation intermediate_reasoning].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id:, session_id: DEFAULT_SESSION_ID, store: nil) ⇒ WorkingMemory

Returns a new instance of WorkingMemory.



21
22
23
24
25
26
# File 'lib/llmemory/working_memory.rb', line 21

def initialize(user_id:, session_id: DEFAULT_SESSION_ID, store: nil)
  @user_id = user_id
  @session_id = session_id
  @store_key = "#{session_id}#{SESSION_SUFFIX}"
  @store = store || ShortTerm::Stores.build
end

Instance Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



19
20
21
# File 'lib/llmemory/working_memory.rb', line 19

def session_id
  @session_id
end

#user_idObject (readonly)

Returns the value of attribute user_id.



19
20
21
# File 'lib/llmemory/working_memory.rb', line 19

def user_id
  @user_id
end

Instance Method Details

#clear!Object



62
63
64
65
# File 'lib/llmemory/working_memory.rb', line 62

def clear!
  @store.delete(@user_id, @store_key)
  true
end

#custom_slotsObject

Slots set by the caller beyond the predefined typed ones.



54
55
56
# File 'lib/llmemory/working_memory.rb', line 54

def custom_slots
  read.reject { |k, _| SLOTS.include?(k) }
end

#get(slot) ⇒ Object

Read/write an arbitrary slot (typed or custom).



34
35
36
# File 'lib/llmemory/working_memory.rb', line 34

def get(slot)
  read[slot.to_sym]
end

#set(slot, value) ⇒ Object



38
39
40
41
42
43
# File 'lib/llmemory/working_memory.rb', line 38

def set(slot, value)
  state = read
  state[slot.to_sym] = value
  persist(state)
  value
end

#to_hObject



58
59
60
# File 'lib/llmemory/working_memory.rb', line 58

def to_h
  read
end

#update(**slots) ⇒ Object

Bulk update in a single write.



46
47
48
49
50
51
# File 'lib/llmemory/working_memory.rb', line 46

def update(**slots)
  state = read
  slots.each { |k, v| state[k.to_sym] = v }
  persist(state)
  state
end