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



65
66
67
68
# File 'lib/llmemory/working_memory.rb', line 65

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

#custom_slotsObject

Slots set by the caller beyond the predefined typed ones.



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

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
44
45
# File 'lib/llmemory/working_memory.rb', line 38

def set(slot, value)
  @store.update(@user_id, @store_key) do |state|
    state = symbolize(state || {})
    state[slot.to_sym] = value
    state
  end
  value
end

#to_hObject



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

def to_h
  read
end

#update(**slots) ⇒ Object

Bulk update in a single write.



48
49
50
51
52
53
54
# File 'lib/llmemory/working_memory.rb', line 48

def update(**slots)
  @store.update(@user_id, @store_key) do |state|
    state = symbolize(state || {})
    slots.each { |k, v| state[k.to_sym] = v }
    state
  end
end