Class: OllamaAgent::Memory::ShortTerm

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/memory/short_term.rb

Overview

In-memory sliding window of recent tool calls and observations. Cleared at the end of each run. Never persisted.

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULT_MAX_ENTRIES =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max: DEFAULT_MAX_ENTRIES) ⇒ ShortTerm

Returns a new instance of ShortTerm.



14
15
16
17
# File 'lib/ollama_agent/memory/short_term.rb', line 14

def initialize(max: DEFAULT_MAX_ENTRIES)
  @max     = max
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



12
13
14
# File 'lib/ollama_agent/memory/short_term.rb', line 12

def entries
  @entries
end

Instance Method Details

#by_type(type) ⇒ Object

Return all entries of a given type.



34
35
36
# File 'lib/ollama_agent/memory/short_term.rb', line 34

def by_type(type)
  @entries.select { |e| e.type == type.to_sym }
end

#clear!Object



56
57
58
59
# File 'lib/ollama_agent/memory/short_term.rb', line 56

def clear!
  @entries.clear
  nil
end

#recent(n = 5) ⇒ Object

Return the N most recent entries.



29
30
31
# File 'lib/ollama_agent/memory/short_term.rb', line 29

def recent(n = 5)
  @entries.last(n)
end

#recent_tool_calls(n = 5) ⇒ Object

Last N tool calls (name + args)



39
40
41
# File 'lib/ollama_agent/memory/short_term.rb', line 39

def recent_tool_calls(n = 5)
  by_type(:tool_call).last(n)
end

#recently_called?(tool_name, args, window: 6) ⇒ Boolean

True if the same tool+args was called recently (loop hint).

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/ollama_agent/memory/short_term.rb', line 44

def recently_called?(tool_name, args, window: 6)
  recent(window).any? do |e|
    e.type == :tool_call &&
      e.content[:tool] == tool_name.to_s &&
      e.content[:args].to_s == args.to_s
  end
end

#record(type, content) ⇒ Object

Record a new entry.

Parameters:

  • type (Symbol)

    :tool_call, :tool_result, :observation, :reasoning

  • content (Object)

    anything serialisable



22
23
24
25
26
# File 'lib/ollama_agent/memory/short_term.rb', line 22

def record(type, content)
  @entries << Entry.new(type: type.to_sym, content: content, ts: Time.now.to_f)
  @entries.shift if @entries.size > @max
  nil
end

#sizeObject



52
53
54
# File 'lib/ollama_agent/memory/short_term.rb', line 52

def size
  @entries.size
end

#to_aObject



61
62
63
# File 'lib/ollama_agent/memory/short_term.rb', line 61

def to_a
  @entries.map(&:to_h)
end