Class: RCrewAI::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/rcrewai/memory.rb,
lib/rcrewai/memory/base_memory.rb,
lib/rcrewai/memory/tool_memory.rb,
lib/rcrewai/memory/sqlite_store.rb,
lib/rcrewai/memory/entity_memory.rb,
lib/rcrewai/memory/in_memory_store.rb,
lib/rcrewai/memory/long_term_memory.rb,
lib/rcrewai/memory/short_term_memory.rb,
lib/rcrewai/memory/llm_entity_extractor.rb

Overview

Cognitive memory facade. Preserves the original public API (add_execution / add_tool_usage / relevant_executions / tool_usage_for / clear_*! / stats) while delegating to semantic, optionally-persistent memory types (short-term, long-term, entity, tool).

Zero-config: Memory.new uses an in-memory store and, when no embedder is available, falls back to lexical similarity — so existing code behaves as before, just with better recall once an embedder is configured.

Defined Under Namespace

Classes: BaseMemory, EntityMemory, InMemoryStore, LlmEntityExtractor, LongTermMemory, ShortTermMemory, SqliteStore, ToolMemory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope: 'default', embedder: nil, store: nil, short_term_limit: 100, entity_extractor: nil) ⇒ Memory

Returns a new instance of Memory.



15
16
17
18
19
20
# File 'lib/rcrewai/memory.rb', line 15

def initialize(scope: 'default', embedder: nil, store: nil, short_term_limit: 100, entity_extractor: nil)
  @short_term = ShortTermMemory.new(scope: scope, embedder: embedder, store: store, limit: short_term_limit)
  @long_term  = LongTermMemory.new(scope: scope, embedder: embedder, store: store)
  @entity     = EntityMemory.new(scope: scope, embedder: embedder, store: store, extractor: entity_extractor)
  @tool       = ToolMemory.new(scope: scope, embedder: embedder, store: store)
end

Instance Attribute Details

#entityObject (readonly)

--- new surface (optional direct access) --------------------------------



84
85
86
# File 'lib/rcrewai/memory.rb', line 84

def entity
  @entity
end

#long_termObject (readonly)

--- new surface (optional direct access) --------------------------------



84
85
86
# File 'lib/rcrewai/memory.rb', line 84

def long_term
  @long_term
end

#short_termObject (readonly)

--- new surface (optional direct access) --------------------------------



84
85
86
# File 'lib/rcrewai/memory.rb', line 84

def short_term
  @short_term
end

#toolObject (readonly)

--- new surface (optional direct access) --------------------------------



84
85
86
# File 'lib/rcrewai/memory.rb', line 84

def tool
  @tool
end

Instance Method Details

#add_execution(task, result, execution_time) ⇒ Object

--- original API --------------------------------------------------------



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rcrewai/memory.rb', line 24

def add_execution(task, result, execution_time)
  success = !result.to_s.downcase.include?('failed')
  text = "Task: #{task.name}\nDescription: #{task.description}\nResult: #{truncate(result, 300)}"
   = {
    'task' => task.name,
    'success' => success,
    'execution_time' => execution_time,
    'result' => result.to_s
  }

  @short_term.record(text, )
  if success
    @long_term.record(text, )
    @entity.observe("#{task.description} #{result}")
  end
  nil
end

#add_tool_usage(tool_name, params, result) ⇒ Object



42
43
44
45
# File 'lib/rcrewai/memory.rb', line 42

def add_tool_usage(tool_name, params, result)
  @tool.record_call(tool_name, params, result)
  nil
end

#clear_all!Object



66
67
68
69
70
71
# File 'lib/rcrewai/memory.rb', line 66

def clear_all!
  @short_term.clear!
  @long_term.clear!
  @entity.clear!
  @tool.clear!
end

#clear_short_term!Object



62
63
64
# File 'lib/rcrewai/memory.rb', line 62

def clear_short_term!
  @short_term.clear!
end

#relevant_executions(task, limit = 3) ⇒ Object

Returns a formatted string of the most relevant past executions, or nil.



48
49
50
51
52
53
54
55
56
# File 'lib/rcrewai/memory.rb', line 48

def relevant_executions(task, limit = 3)
  query = "#{task.name} #{task.description}"
  recalled = (@short_term.recall(query, limit: limit) + @long_term.recall(query, limit: limit))
  seen = {}
  unique = recalled.reject { |r| seen[r[:text]].tap { seen[r[:text]] = true } }
  return nil if unique.empty?

  unique.first(limit).map { |r| format_execution(r) }.join("\n---\n")
end

#statsObject



73
74
75
76
77
78
79
80
# File 'lib/rcrewai/memory.rb', line 73

def stats
  {
    short_term_count: @short_term.count,
    long_term_total: @long_term.count,
    entity_count: @entity.entities.length,
    tool_usage_count: @tool.count
  }
end

#tool_usage_for(tool_name, limit = 5) ⇒ Object



58
59
60
# File 'lib/rcrewai/memory.rb', line 58

def tool_usage_for(tool_name, limit = 5)
  @tool.usage_for(tool_name, limit: limit).join("\n")
end